blob: 085341571ced498689b061a2c05b808527d82450 [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
Douglas Gregora6017bb2012-10-09 17:21:28 +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),
Vassil Vassilev928c8252016-04-28 14:13:28 +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]);
235 std::sort(IDs.begin(), IDs.end());
236 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
Douglas Gregora6017bb2012-10-09 17:21:28 +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;
423 Current = Current->getMostRecentDecl();
424 } 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) {
David Blaikiee6b7c282017-04-11 20:46:34 +0000455 if (Record.readInt())
456 Reader.BodySource[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.
463 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
464 HasPendingBody = true;
465}
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());
Richard Smith1d209d02013-05-23 01:49:11 +0000742 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000743}
744
Sebastian Redlb3298c32010-08-18 23:56:48 +0000745void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000746 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000747 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000748}
749
Sebastian Redlb3298c32010-08-18 23:56:48 +0000750void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000751 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000752 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000753 ECD->setInitExpr(Record.readExpr());
754 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000755 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000756}
757
Sebastian Redlb3298c32010-08-18 23:56:48 +0000758void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000759 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000760 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000761 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000762 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000763 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000764 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000765 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000766 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000767}
768
Sebastian Redlb3298c32010-08-18 23:56:48 +0000769void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000770 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000771 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000772
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000773 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000774 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000775
Douglas Gregorb2585692012-01-04 17:13:46 +0000776 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
777 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000778
David L. Jonesbe1557a2016-12-21 00:17:49 +0000779 FD->SClass = (StorageClass)Record.readInt();
780 FD->IsInline = Record.readInt();
781 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000782 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000783 FD->IsVirtualAsWritten = Record.readInt();
784 FD->IsPure = Record.readInt();
785 FD->HasInheritedPrototype = Record.readInt();
786 FD->HasWrittenPrototype = Record.readInt();
787 FD->IsDeleted = Record.readInt();
788 FD->IsTrivial = Record.readInt();
789 FD->IsDefaulted = Record.readInt();
790 FD->IsExplicitlyDefaulted = Record.readInt();
791 FD->HasImplicitReturnZero = Record.readInt();
792 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000793 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000794 FD->HasSkippedBody = Record.readInt();
795 FD->IsLateTemplateParsed = Record.readInt();
796 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000797 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000798
David L. Jonesbe1557a2016-12-21 00:17:49 +0000799 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000800 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000801 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000802 break;
803 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000804 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000805 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000806 break;
807 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000808 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000809 TemplateSpecializationKind TSK =
810 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000811 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000812 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000813 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000814 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000815 break;
816 }
817 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000818 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000819 TemplateSpecializationKind TSK =
820 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000821
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000822 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000823 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000824 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000825
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000826 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000827 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000828 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000829 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000830 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000831 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000832 TemplArgLocs.reserve(NumTemplateArgLocs);
833 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000834 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000835
836 LAngleLoc = ReadSourceLocation();
837 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000838 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000839
840 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000841
Douglas Gregor4163aca2011-09-09 21:34:22 +0000842 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000843 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000844 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000845 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000846 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000847 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000848 FunctionTemplateSpecializationInfo *FTInfo
849 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
850 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000851 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
852 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000853 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000854 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000855
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000856 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000857 // The template that contains the specializations set. It's not safe to
858 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000859 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000860 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
861 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
862 // FunctionTemplateSpecializationInfo's Profile().
863 // We avoid getASTContext because a decl in the parent hierarchy may
864 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000865 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000866 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000867 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000868 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000869 FunctionTemplateSpecializationInfo *ExistingInfo =
870 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000871 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000872 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
873 else {
874 assert(Reader.getContext().getLangOpts().Modules &&
875 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000876 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000877 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000878 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000879 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000880 }
881 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
882 // Templates.
883 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000884 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000885 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000886 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
887
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000888 // Templates args.
889 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000890 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000891 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000892 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000893 TemplArgs.setLAngleLoc(ReadSourceLocation());
894 TemplArgs.setRAngleLoc(ReadSourceLocation());
895
Douglas Gregor4163aca2011-09-09 21:34:22 +0000896 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000897 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000898 // These are not merged; we don't need to merge redeclarations of dependent
899 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000900 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000901 }
902 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000903
Chris Lattnerca025db2010-05-07 21:43:38 +0000904 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000905 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000906 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000907 Params.reserve(NumParams);
908 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000909 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000910 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000911}
912
Sebastian Redlb3298c32010-08-18 23:56:48 +0000913void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000914 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000915 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000916 // Load the body on-demand. Most clients won't care, because method
917 // definitions rarely show up in headers.
918 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
919 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000920 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
921 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000922 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000923 MD->setInstanceMethod(Record.readInt());
924 MD->setVariadic(Record.readInt());
925 MD->setPropertyAccessor(Record.readInt());
926 MD->setDefined(Record.readInt());
927 MD->IsOverriding = Record.readInt();
928 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000929
David L. Jonesbe1557a2016-12-21 00:17:49 +0000930 MD->IsRedeclaration = Record.readInt();
931 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000932 if (MD->HasRedeclaration)
933 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000934 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000935
David L. Jonesbe1557a2016-12-21 00:17:49 +0000936 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
937 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
938 MD->SetRelatedResultType(Record.readInt());
939 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000940 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
941 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000942 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000943 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000944 Params.reserve(NumParams);
945 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000946 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000947
David L. Jonesbe1557a2016-12-21 00:17:49 +0000948 MD->SelLocsKind = Record.readInt();
949 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000950 SmallVector<SourceLocation, 16> SelLocs;
951 SelLocs.reserve(NumStoredSelLocs);
952 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000953 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000954
955 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000956}
957
Douglas Gregor85f3f952015-07-07 03:57:15 +0000958void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000959 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000960
David L. Jonesbe1557a2016-12-21 00:17:49 +0000961 D->Variance = Record.readInt();
962 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000963 D->VarianceLoc = ReadSourceLocation();
964 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000965}
966
Sebastian Redlb3298c32010-08-18 23:56:48 +0000967void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000968 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000969 CD->setAtStartLoc(ReadSourceLocation());
970 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000971}
972
Douglas Gregor85f3f952015-07-07 03:57:15 +0000973ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000974 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000975 if (numParams == 0)
976 return nullptr;
977
978 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
979 typeParams.reserve(numParams);
980 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000981 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000982 if (!typeParam)
983 return nullptr;
984
985 typeParams.push_back(typeParam);
986 }
987
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000988 SourceLocation lAngleLoc = ReadSourceLocation();
989 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000990
991 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
992 typeParams, rAngleLoc);
993}
994
Manman Renec315f12016-09-09 23:48:27 +0000995void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000996 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000997 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000998 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000999
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001000 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001001 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001002
Manman Renec315f12016-09-09 23:48:27 +00001003 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001004 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001005 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1006 Protocols.reserve(NumProtocols);
1007 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001008 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001009 SmallVector<SourceLocation, 16> ProtoLocs;
1010 ProtoLocs.reserve(NumProtocols);
1011 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001012 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001013 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1014 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001015
Manman Renec315f12016-09-09 23:48:27 +00001016 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001017 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001018 Protocols.clear();
1019 Protocols.reserve(NumProtocols);
1020 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001021 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001022 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1023 Reader.getContext());
1024}
1025
1026void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1027 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1028 // FIXME: odr checking?
1029}
1030
Sebastian Redlb3298c32010-08-18 23:56:48 +00001031void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001032 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001033 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001034 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001035 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001036
1037 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001038 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001039 // Read the definition.
1040 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001041
David L. Jonesbe1557a2016-12-21 00:17:49 +00001042 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001043 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1044 if (Canon->Data.getPointer()) {
1045 // If we already have a definition, keep the definition invariant and
1046 // merge the data.
1047 MergeDefinitionData(Canon, std::move(ID->data()));
1048 ID->Data = Canon->Data;
1049 } else {
1050 // Set the definition data of the canonical declaration, so other
1051 // redeclarations will see it.
1052 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001053
Manman Renec315f12016-09-09 23:48:27 +00001054 // We will rebuild this list lazily.
1055 ID->setIvarList(nullptr);
1056 }
Craig Toppera13603a2014-05-22 05:54:18 +00001057
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001058 // Note that we have deserialized a definition.
1059 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001060
Douglas Gregor404cdde2012-01-27 01:47:08 +00001061 // Note that we've loaded this Objective-C class.
1062 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001063 } else {
1064 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001065 }
Chris Lattner487412d2009-04-27 05:27:42 +00001066}
1067
Sebastian Redlb3298c32010-08-18 23:56:48 +00001068void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001069 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001070 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001071 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001072 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001073 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001074 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001075}
1076
Graydon Hoaree0a68352017-06-28 18:36:27 +00001077void ASTDeclReader::ReadObjCDefinitionData(
1078 struct ObjCProtocolDecl::DefinitionData &Data) {
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001079
David L. Jonesbe1557a2016-12-21 00:17:49 +00001080 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001081 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1082 ProtoRefs.reserve(NumProtoRefs);
1083 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001084 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001085 SmallVector<SourceLocation, 16> ProtoLocs;
1086 ProtoLocs.reserve(NumProtoRefs);
1087 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001088 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001089 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1090 ProtoLocs.data(), Reader.getContext());
1091}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001092
Graydon Hoaree0a68352017-06-28 18:36:27 +00001093void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1094 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1095 // FIXME: odr checking?
1096}
1097
1098void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1099 RedeclarableResult Redecl = VisitRedeclarable(PD);
1100 VisitObjCContainerDecl(PD);
1101 mergeRedeclarable(PD, Redecl);
1102
1103 if (Record.readInt()) {
1104 // Read the definition.
1105 PD->allocateDefinitionData();
1106
1107 ReadObjCDefinitionData(PD->data());
1108
1109 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1110 if (Canon->Data.getPointer()) {
1111 // If we already have a definition, keep the definition invariant and
1112 // merge the data.
1113 MergeDefinitionData(Canon, std::move(PD->data()));
1114 PD->Data = Canon->Data;
1115 } else {
1116 // Set the definition data of the canonical declaration, so other
1117 // redeclarations will see it.
1118 PD->getCanonicalDecl()->Data = PD->Data;
1119 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001120 // Note that we have deserialized a definition.
1121 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001122 } else {
1123 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001124 }
Chris Lattner487412d2009-04-27 05:27:42 +00001125}
1126
Sebastian Redlb3298c32010-08-18 23:56:48 +00001127void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001128 VisitFieldDecl(FD);
1129}
1130
Sebastian Redlb3298c32010-08-18 23:56:48 +00001131void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001132 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001133 CD->setCategoryNameLoc(ReadSourceLocation());
1134 CD->setIvarLBraceLoc(ReadSourceLocation());
1135 CD->setIvarRBraceLoc(ReadSourceLocation());
1136
Douglas Gregor404cdde2012-01-27 01:47:08 +00001137 // Note that this category has been deserialized. We do this before
1138 // deserializing the interface declaration, so that it will consider this
1139 /// category.
1140 Reader.CategoriesDeserialized.insert(CD);
1141
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001142 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001143 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001144 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001145 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001146 ProtoRefs.reserve(NumProtoRefs);
1147 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001148 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001149 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001150 ProtoLocs.reserve(NumProtoRefs);
1151 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001152 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001153 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001154 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001155}
1156
Sebastian Redlb3298c32010-08-18 23:56:48 +00001157void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001158 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001159 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001160}
1161
Sebastian Redlb3298c32010-08-18 23:56:48 +00001162void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001163 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001164 D->setAtLoc(ReadSourceLocation());
1165 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001166 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001167 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001168 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001169 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001170 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001171 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001172 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001173 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001174 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001175 DeclarationName GetterName = Record.readDeclarationName();
1176 SourceLocation GetterLoc = ReadSourceLocation();
1177 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1178 DeclarationName SetterName = Record.readDeclarationName();
1179 SourceLocation SetterLoc = ReadSourceLocation();
1180 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001181 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1182 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1183 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001184}
1185
Sebastian Redlb3298c32010-08-18 23:56:48 +00001186void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001187 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001188 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001189}
1190
Sebastian Redlb3298c32010-08-18 23:56:48 +00001191void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001192 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001193 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001194}
1195
Sebastian Redlb3298c32010-08-18 23:56:48 +00001196void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001197 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001198 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1199 D->SuperLoc = ReadSourceLocation();
1200 D->setIvarLBraceLoc(ReadSourceLocation());
1201 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001202 D->setHasNonZeroConstructors(Record.readInt());
1203 D->setHasDestructors(Record.readInt());
1204 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001205 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001206 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001207}
1208
Sebastian Redlb3298c32010-08-18 23:56:48 +00001209void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001210 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001211 D->setAtLoc(ReadSourceLocation());
1212 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1213 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1214 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001215 D->setGetterCXXConstructor(Record.readExpr());
1216 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001217}
1218
Sebastian Redlb3298c32010-08-18 23:56:48 +00001219void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001220 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001221 FD->Mutable = Record.readInt();
1222 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001223 FD->InitStorage.setInt(
1224 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1225 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001226 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001227 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001228 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001229 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001230 }
Richard Smith2b013182012-06-10 03:12:00 +00001231 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001232 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001233 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001234 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001235 }
Richard Smith0b87e072013-10-07 08:02:11 +00001236 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001237}
1238
John McCall5e77d762013-04-16 07:28:30 +00001239void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1240 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001241 PD->GetterId = Record.getIdentifierInfo();
1242 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001243}
1244
Francois Pichet783dd6e2010-11-21 06:08:52 +00001245void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1246 VisitValueDecl(FD);
1247
David L. Jonesbe1557a2016-12-21 00:17:49 +00001248 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001249 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001250 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001251
1252 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001253 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001254
1255 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001256}
1257
Larisse Voufo39a1e502013-08-06 01:03:05 +00001258ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001259 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001260 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001261
David L. Jonesbe1557a2016-12-21 00:17:49 +00001262 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1263 VD->VarDeclBits.TSCSpec = Record.readInt();
1264 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001265 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001266 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1267 Record.readInt();
1268 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1269 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1270 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1271 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1272 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1273 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1274 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1275 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1276 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001277 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001278 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001279 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001280 VD->setCachedLinkage(VarLinkage);
1281
1282 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001283 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001284 VD->getLexicalDeclContext()->isFunctionOrMethod())
1285 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001286
David L. Jonesbe1557a2016-12-21 00:17:49 +00001287 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001288 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001289 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001290 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1291 Eval->CheckedICE = true;
1292 Eval->IsICE = Val == 3;
1293 }
1294 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001295
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001296 enum VarKind {
1297 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1298 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001299 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001300 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001301 // Only true variables (not parameters or implicit parameters) can be
1302 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001303 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1304 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001305 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001306 break;
1307 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001308 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001309 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001310 break;
1311 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001312 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001313 TemplateSpecializationKind TSK =
1314 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001315 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001316 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001317 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001318 break;
1319 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001320 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001321
1322 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001323}
1324
Sebastian Redlb3298c32010-08-18 23:56:48 +00001325void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001326 VisitVarDecl(PD);
1327}
1328
Sebastian Redlb3298c32010-08-18 23:56:48 +00001329void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001330 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001331 unsigned isObjCMethodParam = Record.readInt();
1332 unsigned scopeDepth = Record.readInt();
1333 unsigned scopeIndex = Record.readInt();
1334 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001335 if (isObjCMethodParam) {
1336 assert(scopeDepth == 0);
1337 PD->setObjCMethodScopeInfo(scopeIndex);
1338 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1339 } else {
1340 PD->setScopeInfo(scopeDepth, scopeIndex);
1341 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001342 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1343 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1344 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001345 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001346
1347 // FIXME: If this is a redeclaration of a function from another module, handle
1348 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001349}
1350
Richard Smith7b76d812016-08-12 02:21:25 +00001351void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1352 VisitVarDecl(DD);
1353 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1354 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001355 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001356}
1357
1358void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1359 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001360 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001361}
1362
Sebastian Redlb3298c32010-08-18 23:56:48 +00001363void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001364 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001365 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001366 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001367}
1368
Sebastian Redlb3298c32010-08-18 23:56:48 +00001369void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001370 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001371 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001372 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001373 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001374 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001375 Params.reserve(NumParams);
1376 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001377 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001378 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001379
David L. Jonesbe1557a2016-12-21 00:17:49 +00001380 BD->setIsVariadic(Record.readInt());
1381 BD->setBlockMissingReturnType(Record.readInt());
1382 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001383
David L. Jonesbe1557a2016-12-21 00:17:49 +00001384 bool capturesCXXThis = Record.readInt();
1385 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001386 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001387 captures.reserve(numCaptures);
1388 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001389 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001390 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001391 bool byRef = (flags & 1);
1392 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001393 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001394
1395 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1396 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001397 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001398}
1399
Ben Langmuirce914fc2013-05-03 19:20:19 +00001400void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1401 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001402 unsigned ContextParamPos = Record.readInt();
1403 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001404 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001405 for (unsigned I = 0; I < CD->NumParams; ++I) {
1406 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001407 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001408 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001409 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001410 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001411}
1412
Sebastian Redlb3298c32010-08-18 23:56:48 +00001413void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001414 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001415 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001416 D->setExternLoc(ReadSourceLocation());
1417 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001418}
1419
Richard Smith8df390f2016-09-08 23:14:54 +00001420void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1421 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001422 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001423}
1424
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001425void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1426 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001427 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001428}
1429
Sebastian Redlb3298c32010-08-18 23:56:48 +00001430void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001431 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001432 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001433 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001434 D->LocStart = ReadSourceLocation();
1435 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001436
Richard Smith15e32fd2015-03-17 02:23:11 +00001437 // Defer loading the anonymous namespace until we've finished merging
1438 // this namespace; loading it might load a later declaration of the
1439 // same namespace, and we have an invariant that older declarations
1440 // get merged before newer ones try to merge.
1441 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001442 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001443 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001444 } else {
1445 // Link this namespace back to the first declaration, which has already
1446 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001447 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001448 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001449
1450 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001451
1452 if (AnonNamespace) {
1453 // Each module has its own anonymous namespace, which is disjoint from
1454 // any other module's anonymous namespaces, so don't attach the anonymous
1455 // namespace at all.
1456 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001457 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001458 D->setAnonymousNamespace(Anon);
1459 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001460}
1461
Sebastian Redlb3298c32010-08-18 23:56:48 +00001462void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001463 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001464 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001465 D->NamespaceLoc = ReadSourceLocation();
1466 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001467 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001468 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001469 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001470}
1471
Sebastian Redlb3298c32010-08-18 23:56:48 +00001472void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001473 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001474 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001475 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001476 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1477 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001478 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001479 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001480 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001481 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001482}
1483
Richard Smith151c4562016-12-20 21:35:28 +00001484void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1485 VisitNamedDecl(D);
1486 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1487 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1488 for (unsigned I = 0; I != D->NumExpansions; ++I)
1489 Expansions[I] = ReadDeclAs<NamedDecl>();
1490 mergeMergeable(D);
1491}
1492
Sebastian Redlb3298c32010-08-18 23:56:48 +00001493void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001494 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001495 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001496 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1497 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1498 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001499 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001500 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001501 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001502}
1503
Richard Smith5179eb72016-06-28 19:03:57 +00001504void ASTDeclReader::VisitConstructorUsingShadowDecl(
1505 ConstructorUsingShadowDecl *D) {
1506 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001507 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1508 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001509 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001510}
1511
Sebastian Redlb3298c32010-08-18 23:56:48 +00001512void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001513 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001514 D->UsingLoc = ReadSourceLocation();
1515 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001516 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001517 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1518 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001519}
1520
Sebastian Redlb3298c32010-08-18 23:56:48 +00001521void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001522 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001523 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001524 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001525 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001526 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001527 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001528}
1529
Sebastian Redlb3298c32010-08-18 23:56:48 +00001530void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001531 UnresolvedUsingTypenameDecl *D) {
1532 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001533 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001534 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001535 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001536 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001537}
1538
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001539void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001540 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001541 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001542 Data.UserDeclaredConstructor = Record.readInt();
1543 Data.UserDeclaredSpecialMembers = Record.readInt();
1544 Data.Aggregate = Record.readInt();
1545 Data.PlainOldData = Record.readInt();
1546 Data.Empty = Record.readInt();
1547 Data.Polymorphic = Record.readInt();
1548 Data.Abstract = Record.readInt();
1549 Data.IsStandardLayout = Record.readInt();
1550 Data.HasNoNonEmptyBases = Record.readInt();
1551 Data.HasPrivateFields = Record.readInt();
1552 Data.HasProtectedFields = Record.readInt();
1553 Data.HasPublicFields = Record.readInt();
1554 Data.HasMutableFields = Record.readInt();
1555 Data.HasVariantMembers = Record.readInt();
1556 Data.HasOnlyCMembers = Record.readInt();
1557 Data.HasInClassInitializer = Record.readInt();
1558 Data.HasUninitializedReferenceMember = Record.readInt();
1559 Data.HasUninitializedFields = Record.readInt();
1560 Data.HasInheritedConstructor = Record.readInt();
1561 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001562 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001563 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1564 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1565 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001566 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001567 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1568 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1569 Data.DefaultedDestructorIsDeleted = Record.readInt();
1570 Data.HasTrivialSpecialMembers = Record.readInt();
1571 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1572 Data.HasIrrelevantDestructor = Record.readInt();
1573 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1574 Data.HasDefaultedDefaultConstructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001575 Data.CanPassInRegisters = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001576 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1577 Data.HasConstexprDefaultConstructor = Record.readInt();
1578 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1579 Data.ComputedVisibleConversions = Record.readInt();
1580 Data.UserProvidedDefaultConstructor = Record.readInt();
1581 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001582 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1583 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001584 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1585 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1586 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001587 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001588 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001589
David Blaikie1ac9c982017-04-11 21:13:37 +00001590 if (Record.readInt()) {
1591 Reader.BodySource[D] = Loc.F->Kind == ModuleKind::MK_MainFile
1592 ? ExternalASTSource::EK_Never
1593 : ExternalASTSource::EK_Always;
1594 }
1595
David L. Jonesbe1557a2016-12-21 00:17:49 +00001596 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001597 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001598 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001599 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001600 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001601 Data.VBases = ReadGlobalOffset();
1602
David L. Jonesb6a8f022016-12-21 04:34:52 +00001603 Record.readUnresolvedSet(Data.Conversions);
1604 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001605 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001606 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001607
Douglas Gregor99ae8062012-02-14 17:54:36 +00001608 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001609 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001610 CXXRecordDecl::LambdaDefinitionData &Lambda
1611 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001612 Lambda.Dependent = Record.readInt();
1613 Lambda.IsGenericLambda = Record.readInt();
1614 Lambda.CaptureDefault = Record.readInt();
1615 Lambda.NumCaptures = Record.readInt();
1616 Lambda.NumExplicitCaptures = Record.readInt();
1617 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001618 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001619 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1620 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001621 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001622 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001623 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001624 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001625 bool IsImplicit = Record.readInt();
1626 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001627 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001628 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001629 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001630 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001631 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001632 break;
1633 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001634 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001635 VarDecl *Var = ReadDeclAs<VarDecl>();
1636 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001637 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1638 break;
1639 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001640 }
1641 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001642}
1643
Richard Smithcd45dbc2014-04-19 03:48:30 +00001644void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001645 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001646 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001647 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001648 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001649
Richard Smith02793752015-03-27 21:16:39 +00001650 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001651 // Track that we merged the definitions.
1652 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1653 DD.Definition));
1654 Reader.PendingDefinitions.erase(MergeDD.Definition);
1655 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001656 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001657 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1658 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001659 }
1660
Richard Smith2a9e5c52015-02-03 03:32:14 +00001661 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1662 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1663 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001664 // We faked up this definition data because we found a class for which we'd
1665 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001666 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001667 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001668
1669 // Don't change which declaration is the definition; that is required
1670 // to be invariant once we select it.
1671 auto *Def = DD.Definition;
1672 DD = std::move(MergeDD);
1673 DD.Definition = Def;
1674 return;
1675 }
1676
Richard Smithcd45dbc2014-04-19 03:48:30 +00001677 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001678 bool DetectedOdrViolation = false;
1679#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1680#define MATCH_FIELD(Field) \
1681 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1682 OR_FIELD(Field)
1683 MATCH_FIELD(UserDeclaredConstructor)
1684 MATCH_FIELD(UserDeclaredSpecialMembers)
1685 MATCH_FIELD(Aggregate)
1686 MATCH_FIELD(PlainOldData)
1687 MATCH_FIELD(Empty)
1688 MATCH_FIELD(Polymorphic)
1689 MATCH_FIELD(Abstract)
1690 MATCH_FIELD(IsStandardLayout)
1691 MATCH_FIELD(HasNoNonEmptyBases)
1692 MATCH_FIELD(HasPrivateFields)
1693 MATCH_FIELD(HasProtectedFields)
1694 MATCH_FIELD(HasPublicFields)
1695 MATCH_FIELD(HasMutableFields)
1696 MATCH_FIELD(HasVariantMembers)
1697 MATCH_FIELD(HasOnlyCMembers)
1698 MATCH_FIELD(HasInClassInitializer)
1699 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001700 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001701 MATCH_FIELD(HasInheritedConstructor)
1702 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001703 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001704 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1705 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1706 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001707 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001708 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1709 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1710 MATCH_FIELD(DefaultedDestructorIsDeleted)
1711 OR_FIELD(HasTrivialSpecialMembers)
1712 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1713 MATCH_FIELD(HasIrrelevantDestructor)
1714 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001715 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001716 MATCH_FIELD(CanPassInRegisters)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001717 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1718 OR_FIELD(HasConstexprDefaultConstructor)
1719 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1720 // ComputedVisibleConversions is handled below.
1721 MATCH_FIELD(UserProvidedDefaultConstructor)
1722 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001723 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1724 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001725 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1726 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1727 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1728 MATCH_FIELD(IsLambda)
1729#undef OR_FIELD
1730#undef MATCH_FIELD
1731
1732 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1733 DetectedOdrViolation = true;
1734 // FIXME: Issue a diagnostic if the base classes don't match when we come
1735 // to lazily load them.
1736
1737 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1738 // match when we come to lazily load them.
1739 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1740 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1741 DD.ComputedVisibleConversions = true;
1742 }
1743
1744 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1745 // lazily load it.
1746
1747 if (DD.IsLambda) {
1748 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1749 // when they occur within the body of a function template specialization).
1750 }
1751
Richard Trieufd1acbb2017-04-11 21:31:00 +00001752 if (D->getODRHash() != MergeDD.ODRHash) {
1753 DetectedOdrViolation = true;
1754 }
1755
Richard Smithcd45dbc2014-04-19 03:48:30 +00001756 if (DetectedOdrViolation)
1757 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1758}
1759
Richard Smith2a9e5c52015-02-03 03:32:14 +00001760void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001761 struct CXXRecordDecl::DefinitionData *DD;
1762 ASTContext &C = Reader.getContext();
1763
1764 // Determine whether this is a lambda closure type, so that we can
1765 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001766 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001767 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001768 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001769 LCD_None);
1770 else
1771 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1772
David Blaikie1ac9c982017-04-11 21:13:37 +00001773 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001774
Richard Smith5156c382015-01-22 01:41:56 +00001775 // We might already have a definition for this record. This can happen either
1776 // because we're reading an update record, or because we've already done some
1777 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001778 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001779 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001780 MergeDefinitionData(Canon, std::move(*DD));
1781 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001782 return;
1783 }
1784
Richard Smith97100e32015-06-20 00:22:34 +00001785 // Mark this declaration as being a definition.
1786 D->IsCompleteDefinition = true;
1787 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001788
Richard Smith97100e32015-06-20 00:22:34 +00001789 // If this is not the first declaration or is an update record, we can have
1790 // other redeclarations already. Make a note that we need to propagate the
1791 // DefinitionData pointer onto them.
1792 if (Update || Canon != D) {
1793 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001794 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001795 }
1796}
1797
Richard Smith1d209d02013-05-23 01:49:11 +00001798ASTDeclReader::RedeclarableResult
1799ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1800 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001801
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001802 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001803
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001804 enum CXXRecKind {
1805 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1806 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001807 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001808 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001809 // Merged when we merge the folding set entry in the primary template.
1810 if (!isa<ClassTemplateSpecializationDecl>(D))
1811 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001812 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001813 case CXXRecTemplate: {
1814 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001815 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001816 D->TemplateOrInstantiation = Template;
1817 if (!Template->getTemplatedDecl()) {
1818 // We've not actually loaded the ClassTemplateDecl yet, because we're
1819 // currently being loaded as its pattern. Rely on it to set up our
1820 // TypeForDecl (see VisitClassTemplateDecl).
1821 //
1822 // Beware: we do not yet know our canonical declaration, and may still
1823 // get merged once the surrounding class template has got off the ground.
1824 TypeIDForTypeDecl = 0;
1825 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001826 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001827 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001828 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001829 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001830 TemplateSpecializationKind TSK =
1831 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001832 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001833 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1834 MSI->setPointOfInstantiation(POI);
1835 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001836 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001837 break;
1838 }
1839 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001840
David L. Jonesbe1557a2016-12-21 00:17:49 +00001841 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001842 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001843 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001844 else
1845 // Propagate DefinitionData pointer from the canonical declaration.
1846 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1847
Richard Smith676c4042013-08-29 23:59:27 +00001848 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001849 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001850 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001851 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001852 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001853 // FIXME: This is wrong for the ARM ABI, where some other module may have
1854 // made this function no longer be a key function. We need an update
1855 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001856 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001857 }
Richard Smith1d209d02013-05-23 01:49:11 +00001858
1859 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001860}
1861
Richard Smithbc491202017-02-17 20:05:37 +00001862void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1863 VisitFunctionDecl(D);
1864}
1865
Sebastian Redlb3298c32010-08-18 23:56:48 +00001866void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001867 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001868
David L. Jonesbe1557a2016-12-21 00:17:49 +00001869 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001870 if (D->isCanonicalDecl()) {
1871 while (NumOverridenMethods--) {
1872 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1873 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001874 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001875 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1876 }
1877 } else {
1878 // We don't care about which declarations this used to override; we get
1879 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001880 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001881 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001882}
1883
Sebastian Redlb3298c32010-08-18 23:56:48 +00001884void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001885 // We need the inherited constructor information to merge the declaration,
1886 // so we have to read it before we call VisitCXXMethodDecl.
1887 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001888 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1889 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001890 *D->getTrailingObjects<InheritedConstructor>() =
1891 InheritedConstructor(Shadow, Ctor);
1892 }
1893
Chris Lattnerca025db2010-05-07 21:43:38 +00001894 VisitCXXMethodDecl(D);
1895}
1896
Sebastian Redlb3298c32010-08-18 23:56:48 +00001897void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001898 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001899
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001900 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001901 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1902 // FIXME: Check consistency if we have an old and new operator delete.
1903 if (!Canon->OperatorDelete)
1904 Canon->OperatorDelete = OperatorDelete;
1905 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001906}
1907
Sebastian Redlb3298c32010-08-18 23:56:48 +00001908void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001909 VisitCXXMethodDecl(D);
1910}
1911
Douglas Gregorba345522011-12-02 23:23:56 +00001912void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1913 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001914 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001915 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001916 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001917 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001918 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001919 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001920}
1921
Sebastian Redlb3298c32010-08-18 23:56:48 +00001922void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001923 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001924 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001925}
1926
Sebastian Redlb3298c32010-08-18 23:56:48 +00001927void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001928 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001929 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001930 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001931 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001932 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001933 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001934 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001935 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001936 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001937 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001938 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001939}
1940
Sebastian Redlb3298c32010-08-18 23:56:48 +00001941void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001942 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001943 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001944 D->NumParams = NumParams;
1945 D->Params = new TemplateParameterList*[NumParams];
1946 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001947 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001948 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001949 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001950 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001951 D->Friend = GetTypeSourceInfo();
1952 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001953}
1954
Richard Smithf17fdbd2014-04-24 02:25:27 +00001955DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001956 VisitNamedDecl(D);
1957
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001958 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001959 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001960 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001961 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001962 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001963
Richard Smithf17fdbd2014-04-24 02:25:27 +00001964 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001965}
1966
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001967ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001968ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001969 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001970
Douglas Gregor68444de2012-01-14 15:13:49 +00001971 // Make sure we've allocated the Common pointer first. We do this before
1972 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1973 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1974 if (!CanonD->Common) {
1975 CanonD->Common = CanonD->newCommon(Reader.getContext());
1976 Reader.PendingDefinitions.insert(CanonD);
1977 }
1978 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001979
Douglas Gregor68444de2012-01-14 15:13:49 +00001980 // If this is the first declaration of the template, fill in the information
1981 // for the 'common' pointer.
1982 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001983 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001984 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001985 assert(RTD->getKind() == D->getKind() &&
1986 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001987 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001988 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001989 D->setMemberSpecialization();
1990 }
1991 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001992
Richard Smithf17fdbd2014-04-24 02:25:27 +00001993 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001994 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001995
Richard Smithf17fdbd2014-04-24 02:25:27 +00001996 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001997
Richard Smithd46d6de2013-10-13 23:50:45 +00001998 // If we merged the template with a prior declaration chain, merge the common
1999 // pointer.
2000 // FIXME: Actually merge here, don't just overwrite.
2001 D->Common = D->getCanonicalDecl()->Common;
2002
Douglas Gregor68444de2012-01-14 15:13:49 +00002003 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002004}
2005
Sebastian Redlb3298c32010-08-18 23:56:48 +00002006void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002007 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002008
Douglas Gregor68444de2012-01-14 15:13:49 +00002009 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002010 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2011 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002012 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002013 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002014 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002015 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002016
2017 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2018 // We were loaded before our templated declaration was. We've not set up
2019 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2020 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002021 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002022 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002023 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002024}
2025
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002026void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2027 llvm_unreachable("BuiltinTemplates are not serialized");
2028}
2029
Larisse Voufo30616382013-08-23 22:21:36 +00002030/// TODO: Unify with ClassTemplateDecl version?
2031/// May require unifying ClassTemplateDecl and
2032/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002033void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2034 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2035
2036 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002037 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002038 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002039 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002040 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002041 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002042 }
2043}
2044
Richard Smith1d209d02013-05-23 01:49:11 +00002045ASTDeclReader::RedeclarableResult
2046ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2047 ClassTemplateSpecializationDecl *D) {
2048 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002049
Douglas Gregor4163aca2011-09-09 21:34:22 +00002050 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002051 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002052 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002053 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002054 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002055 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002056 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002057 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002058 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002059 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2060 = new (C) ClassTemplateSpecializationDecl::
2061 SpecializedPartialSpecialization();
2062 PS->PartialSpecialization
2063 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2064 PS->TemplateArgs = ArgList;
2065 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002066 }
2067 }
2068
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002069 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002070 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002071 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002072 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002073 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002074
David L. Jonesbe1557a2016-12-21 00:17:49 +00002075 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002076 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002077 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002078 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002079 // Set this as, or find, the canonical declaration for this specialization
2080 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002081 if (ClassTemplatePartialSpecializationDecl *Partial =
2082 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002083 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002084 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002085 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002086 CanonSpec =
2087 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2088 }
2089 // If there was already a canonical specialization, merge into it.
2090 if (CanonSpec != D) {
2091 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2092
2093 // This declaration might be a definition. Merge with any existing
2094 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002095 if (auto *DDD = D->DefinitionData) {
2096 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002097 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002098 else
Richard Smith053f6c62014-05-16 23:01:30 +00002099 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002100 }
Richard Smith547864d2014-07-11 18:22:58 +00002101 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002102 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002103 }
2104 }
Richard Smith1d209d02013-05-23 01:49:11 +00002105
Richard Smithd55889a2013-09-09 16:55:27 +00002106 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002107 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002108 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2109 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2110 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002111 ExplicitInfo->ExternLoc = ReadSourceLocation();
2112 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002113 D->ExplicitInfo = ExplicitInfo;
2114 }
2115
Richard Smith1d209d02013-05-23 01:49:11 +00002116 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002117}
2118
Sebastian Redlb3298c32010-08-18 23:56:48 +00002119void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002120 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002121 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002122
David L. Jonesb6a8f022016-12-21 04:34:52 +00002123 D->TemplateParams = Record.readTemplateParameterList();
2124 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002125
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002126 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002127 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002128 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002129 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002130 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002131 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002132}
2133
Sebastian Redlb7448632011-08-31 13:59:56 +00002134void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2135 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002136 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002137 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002138}
2139
Sebastian Redlb3298c32010-08-18 23:56:48 +00002140void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002141 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002142
Douglas Gregor68444de2012-01-14 15:13:49 +00002143 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002144 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002145 SmallVector<serialization::DeclID, 32> SpecIDs;
2146 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002147 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002148 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002149}
2150
Larisse Voufo30616382013-08-23 22:21:36 +00002151/// TODO: Unify with ClassTemplateSpecializationDecl version?
2152/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2153/// VarTemplate(Partial)SpecializationDecl with a new data
2154/// structure Template(Partial)SpecializationDecl, and
2155/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002156ASTDeclReader::RedeclarableResult
2157ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2158 VarTemplateSpecializationDecl *D) {
2159 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2160
2161 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002162 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002163 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2164 D->SpecializedTemplate = VTD;
2165 } else {
2166 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002167 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002168 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002169 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002170 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2171 new (C)
2172 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2173 PS->PartialSpecialization =
2174 cast<VarTemplatePartialSpecializationDecl>(InstD);
2175 PS->TemplateArgs = ArgList;
2176 D->SpecializedTemplate = PS;
2177 }
2178 }
2179
2180 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002181 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002182 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2183 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2184 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002185 ExplicitInfo->ExternLoc = ReadSourceLocation();
2186 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002187 D->ExplicitInfo = ExplicitInfo;
2188 }
2189
2190 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002191 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002192 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002193 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002194 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002195
David L. Jonesbe1557a2016-12-21 00:17:49 +00002196 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002197 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002198 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002199 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002200 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002201 if (VarTemplatePartialSpecializationDecl *Partial =
2202 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002203 CanonPattern->getCommonPtr()->PartialSpecializations
2204 .GetOrInsertNode(Partial);
2205 } else {
2206 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2207 }
2208 }
2209 }
2210
2211 return Redecl;
2212}
2213
Larisse Voufo30616382013-08-23 22:21:36 +00002214/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2215/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2216/// VarTemplate(Partial)SpecializationDecl with a new data
2217/// structure Template(Partial)SpecializationDecl, and
2218/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002219void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2220 VarTemplatePartialSpecializationDecl *D) {
2221 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2222
David L. Jonesb6a8f022016-12-21 04:34:52 +00002223 D->TemplateParams = Record.readTemplateParameterList();
2224 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002225
2226 // These are read/set from/to the first declaration.
2227 if (ThisDeclID == Redecl.getFirstID()) {
2228 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002229 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002230 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002231 }
2232}
2233
Sebastian Redlb3298c32010-08-18 23:56:48 +00002234void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002235 VisitTypeDecl(D);
2236
David L. Jonesbe1557a2016-12-21 00:17:49 +00002237 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002238
David L. Jonesbe1557a2016-12-21 00:17:49 +00002239 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002240 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002241}
2242
Sebastian Redlb3298c32010-08-18 23:56:48 +00002243void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002244 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002245 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002246 D->setDepth(Record.readInt());
2247 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002248 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002249 auto TypesAndInfos =
2250 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002251 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002252 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002253 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002254 }
2255 } else {
2256 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002257 D->ParameterPack = Record.readInt();
2258 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002259 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002260 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002261}
2262
Sebastian Redlb3298c32010-08-18 23:56:48 +00002263void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002264 VisitTemplateDecl(D);
2265 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002266 D->setDepth(Record.readInt());
2267 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002268 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002269 TemplateParameterList **Data =
2270 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002271 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2272 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002273 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002274 } else {
2275 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002276 D->ParameterPack = Record.readInt();
2277 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002278 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002279 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002280 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002281}
2282
Richard Smith3f1b5d02011-05-05 21:57:07 +00002283void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2284 VisitRedeclarableTemplateDecl(D);
2285}
2286
Sebastian Redlb3298c32010-08-18 23:56:48 +00002287void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002288 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002289 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002290 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002291 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002292 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002293}
2294
Michael Han84324352013-02-22 17:15:32 +00002295void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2296 VisitDecl(D);
2297}
2298
Mike Stump11289f42009-09-09 15:08:12 +00002299std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002300ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002301 uint64_t LexicalOffset = ReadLocalOffset();
2302 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002303 return std::make_pair(LexicalOffset, VisibleOffset);
2304}
2305
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002306template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002307ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002308ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002309 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002310 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002311
Richard Smith5fc18a92015-07-12 23:43:21 +00002312 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002313 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002314
Richard Smithd61d4ac2015-08-22 20:13:39 +00002315 uint64_t RedeclOffset = 0;
2316
Douglas Gregor358cd442012-01-15 16:58:34 +00002317 // 0 indicates that this declaration was the only declaration of its entity,
2318 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002319 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002320 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002321 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002322 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002323 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002324 // This declaration was the first local declaration, but may have imported
2325 // other declarations.
2326 IsKeyDecl = N == 1;
2327 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002328
Richard Smithfe620d22015-03-05 23:24:12 +00002329 // We have some declarations that must be before us in our redeclaration
2330 // chain. Read them now, and remember that we ought to merge with one of
2331 // them.
2332 // FIXME: Provide a known merge target to the second and subsequent such
2333 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002334 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002335 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002336
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002337 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002338 } else {
2339 // This declaration was not the first local declaration. Read the first
2340 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002341 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002342 }
2343
Douglas Gregor358cd442012-01-15 16:58:34 +00002344 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2345 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002346 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2347 // We temporarily set the first (canonical) declaration as the previous one
2348 // which is the one that matters and mark the real previous DeclID to be
2349 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002350 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002351 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002352 }
Richard Smithd8a83712015-08-22 01:47:18 +00002353
Richard Smithd8a83712015-08-22 01:47:18 +00002354 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002355
2356 // Note that we need to load local redeclarations of this decl and build a
2357 // decl chain for them. This must happen *after* we perform the preloading
2358 // above; this ensures that the redeclaration chain is built in the correct
2359 // order.
2360 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002361 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002362
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002363 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002364}
2365
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002366/// \brief Attempts to merge the given declaration (D) with another declaration
2367/// of the same entity.
2368template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002369void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002370 RedeclarableResult &Redecl,
2371 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002372 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002373 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002374 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002375
Richard Smith202850a2015-03-10 02:57:50 +00002376 // If we're not the canonical declaration, we don't need to merge.
2377 if (!DBase->isFirstDecl())
2378 return;
2379
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002380 T *D = static_cast<T*>(DBase);
2381
Richard Smith5a4737c2015-02-06 02:42:59 +00002382 if (auto *Existing = Redecl.getKnownMergeTarget())
2383 // We already know of an existing declaration we should merge with.
2384 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2385 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002386 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002387 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2388}
2389
2390/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2391/// We use this to put code in a template that will only be valid for certain
2392/// instantiations.
2393template<typename T> static T assert_cast(T t) { return t; }
2394template<typename T> static T assert_cast(...) {
2395 llvm_unreachable("bad assert_cast");
2396}
2397
2398/// \brief Merge together the pattern declarations from two template
2399/// declarations.
2400void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2401 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002402 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002403 auto *DPattern = D->getTemplatedDecl();
2404 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002405 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2406 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002407 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002408
Richard Smith547864d2014-07-11 18:22:58 +00002409 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2410 // Merge with any existing definition.
2411 // FIXME: This is duplicated in several places. Refactor.
2412 auto *ExistingClass =
2413 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002414 if (auto *DDD = DClass->DefinitionData) {
2415 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002416 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002417 } else {
2418 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002419 // We may have skipped this before because we thought that DClass
2420 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002421 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002422 }
2423 }
2424 DClass->DefinitionData = ExistingClass->DefinitionData;
2425
Richard Smithf17fdbd2014-04-24 02:25:27 +00002426 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2427 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002428 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002429 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2430 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2431 Result);
2432 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2433 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002434 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2435 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2436 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002437 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002438}
2439
2440/// \brief Attempts to merge the given declaration (D) with another declaration
2441/// of the same entity.
2442template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002443void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2444 RedeclarableResult &Redecl,
2445 DeclID TemplatePatternID) {
2446 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002447 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002448 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002449 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002450 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2451 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002452
Richard Smithd55889a2013-09-09 16:55:27 +00002453 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002454 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002455 // appropriate canonical declaration.
2456 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002457 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002458 ExistingCanon->Used |= D->Used;
2459 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002460
2461 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002462 // We cannot have loaded any redeclarations of this declaration yet, so
2463 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002464 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002465 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002466 assert_cast<NamespaceDecl*>(ExistingCanon));
2467
2468 // When we merge a template, merge its pattern.
2469 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2470 mergeTemplatePattern(
2471 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002472 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002473
Richard Smith5fc18a92015-07-12 23:43:21 +00002474 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002475 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002476 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002477 }
2478}
2479
Richard Smith0b87e072013-10-07 08:02:11 +00002480/// \brief Attempts to merge the given declaration (D) with another declaration
2481/// of the same entity, for the case where the entity is not actually
2482/// redeclarable. This happens, for instance, when merging the fields of
2483/// identical class definitions from two different modules.
2484template<typename T>
2485void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2486 // If modules are not available, there is no reason to perform this merge.
2487 if (!Reader.getContext().getLangOpts().Modules)
2488 return;
2489
Richard Smith01a73372013-10-15 22:02:41 +00002490 // ODR-based merging is only performed in C++. In C, identically-named things
2491 // in different translation units are not redeclarations (but may still have
2492 // compatible types).
2493 if (!Reader.getContext().getLangOpts().CPlusPlus)
2494 return;
2495
Richard Smith0b87e072013-10-07 08:02:11 +00002496 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2497 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002498 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2499 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002500}
2501
Alexey Bataeva769e072013-03-22 06:34:35 +00002502void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2503 VisitDecl(D);
2504 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002505 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002506 Vars.reserve(NumVars);
2507 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002508 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002509 }
2510 D->setVars(Vars);
2511}
2512
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002513void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002514 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002515 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002516 D->setCombiner(Record.readExpr());
2517 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002518 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002519}
2520
Alexey Bataev4244be22016-02-11 05:35:55 +00002521void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002522 VisitVarDecl(D);
2523}
2524
Chris Lattner487412d2009-04-27 05:27:42 +00002525//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002526// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002527//===----------------------------------------------------------------------===//
2528
Chris Lattner8f63ab52009-04-27 06:01:06 +00002529/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002530void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2531 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002532 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002533 attr::Kind Kind = (attr::Kind)Record.readInt();
2534 SourceRange Range = Record.readSourceRange();
Richard Smithdbafb6c2017-06-29 23:23:46 +00002535 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002536
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002537#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002538
2539 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002540 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002541 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002542}
2543
2544//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002545// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002546//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002547
2548/// \brief Note that we have loaded the declaration with the given
2549/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002550///
Chris Lattner487412d2009-04-27 05:27:42 +00002551/// This routine notes that this declaration has already been loaded,
2552/// so that future GetDecl calls will return this declaration rather
2553/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002554inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002555 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2556 DeclsLoaded[Index] = D;
2557}
2558
2559
2560/// \brief Determine whether the consumer will be interested in seeing
2561/// this declaration (via HandleTopLevelDecl).
2562///
2563/// This routine should return true for anything that might affect
2564/// code generation, e.g., inline function definitions, Objective-C
2565/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002566static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002567 // An ObjCMethodDecl is never considered as "interesting" because its
2568 // implementation container always is.
2569
Richard Smithdc1f0422016-07-20 19:10:16 +00002570 // An ImportDecl or VarDecl imported from a module will get emitted when
2571 // we import the relevant module.
Bruno Cardoso Lopes84df6e99b2017-03-01 19:18:42 +00002572 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && D->getImportedOwningModule() &&
2573 Ctx.DeclMustBeEmitted(D))
Richard Smithdc1f0422016-07-20 19:10:16 +00002574 return false;
2575
Douglas Gregor87d81242011-09-10 00:22:34 +00002576 if (isa<FileScopeAsmDecl>(D) ||
2577 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002578 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002579 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002580 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002581 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002582 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002583 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2584 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002585 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002586 return Var->isFileVarDecl() &&
2587 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002588 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002589 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002590
2591 if (auto *ES = D->getASTContext().getExternalSource())
2592 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2593 return true;
2594
Douglas Gregor87d81242011-09-10 00:22:34 +00002595 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002596}
2597
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002598/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002599ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002600ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002601 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2602 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002603 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002604 const DeclOffset &DOffs =
2605 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002606 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002607 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002608}
2609
Douglas Gregord32f0352011-07-22 06:10:01 +00002610ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002611 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002612 = GlobalBitOffsetsMap.find(GlobalOffset);
2613
2614 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2615 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2616}
2617
Douglas Gregorde3ef502011-11-30 23:21:26 +00002618uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002619 return LocalOffset + M.GlobalBitOffset;
2620}
2621
Richard Smithbf78e642013-06-24 22:51:00 +00002622static bool isSameTemplateParameterList(const TemplateParameterList *X,
2623 const TemplateParameterList *Y);
2624
2625/// \brief Determine whether two template parameters are similar enough
2626/// that they may be used in declarations of the same template.
2627static bool isSameTemplateParameter(const NamedDecl *X,
2628 const NamedDecl *Y) {
2629 if (X->getKind() != Y->getKind())
2630 return false;
2631
2632 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2633 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2634 return TX->isParameterPack() == TY->isParameterPack();
2635 }
2636
2637 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2638 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2639 return TX->isParameterPack() == TY->isParameterPack() &&
2640 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2641 }
2642
2643 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2644 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2645 return TX->isParameterPack() == TY->isParameterPack() &&
2646 isSameTemplateParameterList(TX->getTemplateParameters(),
2647 TY->getTemplateParameters());
2648}
2649
Richard Smith32952e12014-10-14 02:00:47 +00002650static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2651 if (auto *NS = X->getAsNamespace())
2652 return NS;
2653 if (auto *NAS = X->getAsNamespaceAlias())
2654 return NAS->getNamespace();
2655 return nullptr;
2656}
2657
2658static bool isSameQualifier(const NestedNameSpecifier *X,
2659 const NestedNameSpecifier *Y) {
2660 if (auto *NSX = getNamespace(X)) {
2661 auto *NSY = getNamespace(Y);
2662 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2663 return false;
2664 } else if (X->getKind() != Y->getKind())
2665 return false;
2666
2667 // FIXME: For namespaces and types, we're permitted to check that the entity
2668 // is named via the same tokens. We should probably do so.
2669 switch (X->getKind()) {
2670 case NestedNameSpecifier::Identifier:
2671 if (X->getAsIdentifier() != Y->getAsIdentifier())
2672 return false;
2673 break;
2674 case NestedNameSpecifier::Namespace:
2675 case NestedNameSpecifier::NamespaceAlias:
2676 // We've already checked that we named the same namespace.
2677 break;
2678 case NestedNameSpecifier::TypeSpec:
2679 case NestedNameSpecifier::TypeSpecWithTemplate:
2680 if (X->getAsType()->getCanonicalTypeInternal() !=
2681 Y->getAsType()->getCanonicalTypeInternal())
2682 return false;
2683 break;
2684 case NestedNameSpecifier::Global:
2685 case NestedNameSpecifier::Super:
2686 return true;
2687 }
2688
2689 // Recurse into earlier portion of NNS, if any.
2690 auto *PX = X->getPrefix();
2691 auto *PY = Y->getPrefix();
2692 if (PX && PY)
2693 return isSameQualifier(PX, PY);
2694 return !PX && !PY;
2695}
2696
Richard Smithbf78e642013-06-24 22:51:00 +00002697/// \brief Determine whether two template parameter lists are similar enough
2698/// that they may be used in declarations of the same template.
2699static bool isSameTemplateParameterList(const TemplateParameterList *X,
2700 const TemplateParameterList *Y) {
2701 if (X->size() != Y->size())
2702 return false;
2703
2704 for (unsigned I = 0, N = X->size(); I != N; ++I)
2705 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2706 return false;
2707
2708 return true;
2709}
2710
George Burgess IV95845082017-02-15 22:43:27 +00002711/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002712/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002713static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2714 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002715 // Note that pass_object_size attributes are represented in the function's
2716 // ExtParameterInfo, so we don't need to check them here.
2717
George Burgess IV95845082017-02-15 22:43:27 +00002718 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2719 // Since this is an equality check, we can ignore that enable_if attrs show up
2720 // in reverse order.
2721 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2722 AEnableIfs.push_back(EIA);
2723
2724 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2725 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2726 BEnableIfs.push_back(EIA);
2727
2728 // Two very common cases: either we have 0 enable_if attrs, or we have an
2729 // unequal number of enable_if attrs.
2730 if (AEnableIfs.empty() && BEnableIfs.empty())
2731 return true;
2732
2733 if (AEnableIfs.size() != BEnableIfs.size())
2734 return false;
2735
2736 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2737 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2738 Cand1ID.clear();
2739 Cand2ID.clear();
2740
2741 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2742 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2743 if (Cand1ID != Cand2ID)
2744 return false;
2745 }
2746
George Burgess IV95845082017-02-15 22:43:27 +00002747 return true;
2748}
2749
Douglas Gregor022857e2011-12-22 01:48:48 +00002750/// \brief Determine whether the two declarations refer to the same entity.
2751static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2752 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002753
Douglas Gregor022857e2011-12-22 01:48:48 +00002754 if (X == Y)
2755 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002756
Douglas Gregor022857e2011-12-22 01:48:48 +00002757 // Must be in the same context.
2758 if (!X->getDeclContext()->getRedeclContext()->Equals(
2759 Y->getDeclContext()->getRedeclContext()))
2760 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002761
2762 // Two typedefs refer to the same entity if they have the same underlying
2763 // type.
2764 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2765 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2766 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2767 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002768
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002769 // Must have the same kind.
2770 if (X->getKind() != Y->getKind())
2771 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002772
Douglas Gregorda389302012-01-01 21:47:52 +00002773 // Objective-C classes and protocols with the same name always match.
2774 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002775 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002776
2777 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002778 // No need to handle these here: we merge them when adding them to the
2779 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002780 return false;
2781 }
Richard Smithd55889a2013-09-09 16:55:27 +00002782
Douglas Gregor2009cee2012-01-03 22:46:00 +00002783 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002784 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2785 TagDecl *TagY = cast<TagDecl>(Y);
2786 return (TagX->getTagKind() == TagY->getTagKind()) ||
2787 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2788 TagX->getTagKind() == TTK_Interface) &&
2789 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2790 TagY->getTagKind() == TTK_Interface));
2791 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002792
Joao Matose9a3ed42012-08-31 22:18:20 +00002793 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002794 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2795 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002796 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2797 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002798 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2799 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2800 if (CtorX->getInheritedConstructor() &&
2801 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2802 CtorY->getInheritedConstructor().getConstructor()))
2803 return false;
2804 }
Richard Smitha54d3242017-03-08 23:00:26 +00002805 ASTContext &C = FuncX->getASTContext();
2806 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) {
2807 // We can get functions with different types on the redecl chain in C++17
2808 // if they have differing exception specifications and at least one of
2809 // the excpetion specs is unresolved.
2810 // FIXME: Do we need to check for C++14 deduced return types here too?
2811 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>();
2812 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>();
2813 if (C.getLangOpts().CPlusPlus1z && XFPT && YFPT &&
2814 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2815 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
2816 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(),
2817 FuncY->getType()))
2818 return true;
2819 return false;
2820 }
George Burgess IV95845082017-02-15 22:43:27 +00002821 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002822 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002823 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002824
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002825 // Variables with the same type and linkage match.
2826 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2827 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002828 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2829 ASTContext &C = VarX->getASTContext();
2830 if (C.hasSameType(VarX->getType(), VarY->getType()))
2831 return true;
2832
2833 // We can get decls with different types on the redecl chain. Eg.
2834 // template <typename T> struct S { static T Var[]; }; // #1
2835 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2836 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002837 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002838 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2839 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2840 if (!VarXTy || !VarYTy)
2841 return false;
2842 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2843 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2844 }
2845 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002846 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002847
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002848 // Namespaces with the same name and inlinedness match.
2849 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2850 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2851 return NamespaceX->isInline() == NamespaceY->isInline();
2852 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002853
Richard Smith8f8f05c2013-06-24 04:45:28 +00002854 // Identical template names and kinds match if their template parameter lists
2855 // and patterns match.
2856 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002857 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002858 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002859 TemplateY->getTemplatedDecl()) &&
2860 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2861 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002862 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002863
Richard Smith0b87e072013-10-07 08:02:11 +00002864 // Fields with the same name and the same type match.
2865 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2866 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002867 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002868 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2869 }
2870
Richard Smith8cbd8952015-08-04 02:05:09 +00002871 // Indirect fields with the same target field match.
2872 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2873 auto *IFDY = cast<IndirectFieldDecl>(Y);
2874 return IFDX->getAnonField()->getCanonicalDecl() ==
2875 IFDY->getAnonField()->getCanonicalDecl();
2876 }
2877
Richard Smith01a73372013-10-15 22:02:41 +00002878 // Enumerators with the same name match.
2879 if (isa<EnumConstantDecl>(X))
2880 // FIXME: Also check the value is odr-equivalent.
2881 return true;
2882
Richard Smithfd8634a2013-10-23 02:17:46 +00002883 // Using shadow declarations with the same target match.
2884 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2885 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2886 return USX->getTargetDecl() == USY->getTargetDecl();
2887 }
2888
Richard Smith32952e12014-10-14 02:00:47 +00002889 // Using declarations with the same qualifier match. (We already know that
2890 // the name matches.)
2891 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2892 auto *UY = cast<UsingDecl>(Y);
2893 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2894 UX->hasTypename() == UY->hasTypename() &&
2895 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2896 }
2897 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2898 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2899 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2900 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2901 }
2902 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2903 return isSameQualifier(
2904 UX->getQualifier(),
2905 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2906
Richard Smithf4634362014-09-03 23:11:22 +00002907 // Namespace alias definitions with the same target match.
2908 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2909 auto *NAY = cast<NamespaceAliasDecl>(Y);
2910 return NAX->getNamespace()->Equals(NAY->getNamespace());
2911 }
2912
Douglas Gregor022857e2011-12-22 01:48:48 +00002913 return false;
2914}
2915
Richard Smithd55889a2013-09-09 16:55:27 +00002916/// Find the context in which we should search for previous declarations when
2917/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002918DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2919 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002920 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2921 return ND->getOriginalNamespace();
2922
Richard Smith8a639892015-01-24 01:07:20 +00002923 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2924 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002925 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002926 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002927 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002928
2929 // If there's no definition yet, then DC's definition is added by an update
2930 // record, but we've not yet loaded that update record. In this case, we
2931 // commit to DC being the canonical definition now, and will fix this when
2932 // we load the update record.
2933 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00002934 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Richard Smith8a639892015-01-24 01:07:20 +00002935 RD->IsCompleteDefinition = true;
2936 RD->DefinitionData = DD;
2937 RD->getCanonicalDecl()->DefinitionData = DD;
2938
2939 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002940 Reader.PendingFakeDefinitionData.insert(
2941 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002942 }
2943
2944 return DD->Definition;
2945 }
Richard Smithd55889a2013-09-09 16:55:27 +00002946
Richard Smith01a73372013-10-15 22:02:41 +00002947 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002948 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2949 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002950
Richard Smith4eca9b92015-02-04 23:37:59 +00002951 // We can see the TU here only if we have no Sema object. In that case,
2952 // there's no TU scope to look in, so using the DC alone is sufficient.
2953 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2954 return TU;
2955
Craig Toppera13603a2014-05-22 05:54:18 +00002956 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002957}
2958
Douglas Gregor022857e2011-12-22 01:48:48 +00002959ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002960 // Record that we had a typedef name for linkage whether or not we merge
2961 // with that declaration.
2962 if (TypedefNameForLinkage) {
2963 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2964 Reader.ImportedTypedefNamesForLinkage.insert(
2965 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2966 return;
2967 }
2968
Douglas Gregor9b47f942012-01-09 17:38:47 +00002969 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002970 return;
Richard Smith95d99302013-07-13 02:00:19 +00002971
Richard Smith70d58502014-08-30 00:04:23 +00002972 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002973 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002974 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002975 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2976 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002977 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002978 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002979 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002980 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2981 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002982 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002983 // Add the declaration to its redeclaration context so later merging
2984 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002985 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002986 }
2987}
2988
Richard Smith88ebade2014-08-23 01:45:27 +00002989/// Find the declaration that should be merged into, given the declaration found
2990/// by name lookup. If we're merging an anonymous declaration within a typedef,
2991/// we need a matching typedef, and we merge with the type inside it.
2992static NamedDecl *getDeclForMerging(NamedDecl *Found,
2993 bool IsTypedefNameForLinkage) {
2994 if (!IsTypedefNameForLinkage)
2995 return Found;
2996
2997 // If we found a typedef declaration that gives a name to some other
2998 // declaration, then we want that inner declaration. Declarations from
2999 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003000 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003001 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003002
3003 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003004 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003005
Hans Wennborgdcfba332015-10-06 23:40:43 +00003006 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003007}
3008
Richard Smithd08aeb62014-08-28 01:33:39 +00003009NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3010 DeclContext *DC,
3011 unsigned Index) {
3012 // If the lexical context has been merged, look into the now-canonical
3013 // definition.
3014 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3015 DC = Merged;
3016
3017 // If we've seen this before, return the canonical declaration.
3018 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3019 if (Index < Previous.size() && Previous[Index])
3020 return Previous[Index];
3021
3022 // If this is the first time, but we have parsed a declaration of the context,
3023 // build the anonymous declaration list from the parsed declaration.
3024 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00003025 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
3026 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003027 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3028 else
Richard Smith2b560572015-02-07 03:11:11 +00003029 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3030 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003031 }
3032
3033 return Index < Previous.size() ? Previous[Index] : nullptr;
3034}
3035
3036void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3037 DeclContext *DC, unsigned Index,
3038 NamedDecl *D) {
3039 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3040 DC = Merged;
3041
3042 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3043 if (Index >= Previous.size())
3044 Previous.resize(Index + 1);
3045 if (!Previous[Index])
3046 Previous[Index] = D;
3047}
3048
Douglas Gregor022857e2011-12-22 01:48:48 +00003049ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003050 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3051 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003052
Richard Smithd08aeb62014-08-28 01:33:39 +00003053 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3054 // Don't bother trying to find unnamed declarations that are in
3055 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003056 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3057 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003058 Result.suppress();
3059 return Result;
3060 }
Richard Smith95d99302013-07-13 02:00:19 +00003061
Douglas Gregor022857e2011-12-22 01:48:48 +00003062 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003063 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003064 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003065 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003066 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3067 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003068 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3069 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003070 // Go on to check in other places in case an existing typedef name
3071 // was not imported.
3072 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003073
Richard Smith2b560572015-02-07 03:11:11 +00003074 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003075 // This is an anonymous declaration that we may need to merge. Look it up
3076 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003077 if (auto *Existing = getAnonymousDeclForMerging(
3078 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3079 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003080 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3081 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003082 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003083 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003084 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003085
3086 // Temporarily consider the identifier to be up-to-date. We don't want to
3087 // cause additional lookups here.
3088 class UpToDateIdentifierRAII {
3089 IdentifierInfo *II;
3090 bool WasOutToDate;
3091
3092 public:
3093 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3094 : II(II), WasOutToDate(false)
3095 {
3096 if (II) {
3097 WasOutToDate = II->isOutOfDate();
3098 if (WasOutToDate)
3099 II->setOutOfDate(false);
3100 }
3101 }
3102
3103 ~UpToDateIdentifierRAII() {
3104 if (WasOutToDate)
3105 II->setOutOfDate(true);
3106 }
3107 } UpToDate(Name.getAsIdentifierInfo());
3108
Douglas Gregor2009cee2012-01-03 22:46:00 +00003109 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003110 IEnd = IdResolver.end();
3111 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003112 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003113 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003114 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3115 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003116 }
Richard Smith8a639892015-01-24 01:07:20 +00003117 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003118 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003119 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003120 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003121 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003122 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3123 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003124 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003125 } else {
3126 // Not in a mergeable context.
3127 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003128 }
Richard Smithd55889a2013-09-09 16:55:27 +00003129
Richard Smith2b9e3e32013-10-18 06:05:18 +00003130 // If this declaration is from a merged context, make a note that we need to
3131 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003132 //
3133 // FIXME: We should do something similar if we merge two definitions of the
3134 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003135 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3136 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3137 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003138 Reader.PendingOdrMergeChecks.push_back(D);
3139
Richard Smithd08aeb62014-08-28 01:33:39 +00003140 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003141 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003142}
3143
Richard Smithb321ecb2014-05-13 01:15:00 +00003144template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003145Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3146 return D->RedeclLink.getLatestNotUpdated();
3147}
3148Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3149 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3150}
3151
3152Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3153 assert(D);
3154
3155 switch (D->getKind()) {
3156#define ABSTRACT_DECL(TYPE)
3157#define DECL(TYPE, BASE) \
3158 case Decl::TYPE: \
3159 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3160#include "clang/AST/DeclNodes.inc"
3161 }
3162 llvm_unreachable("unknown decl kind");
3163}
3164
Richard Smith8ce51082015-03-11 01:44:51 +00003165Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3166 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3167}
3168
Richard Smithc3a53252015-02-28 05:57:02 +00003169template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003170void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3171 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003172 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003173 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003174 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003175}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003176
Richard Smith24d166c2014-07-31 23:52:38 +00003177namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003178template<>
3179void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003180 Redeclarable<VarDecl> *D,
3181 Decl *Previous, Decl *Canon) {
3182 VarDecl *VD = static_cast<VarDecl*>(D);
3183 VarDecl *PrevVD = cast<VarDecl>(Previous);
3184 D->RedeclLink.setPrevious(PrevVD);
3185 D->First = PrevVD->First;
3186
3187 // We should keep at most one definition on the chain.
3188 // FIXME: Cache the definition once we've found it. Building a chain with
3189 // N definitions currently takes O(N^2) time here.
3190 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3191 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3192 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3193 Reader.mergeDefinitionVisibility(CurD, VD);
3194 VD->demoteThisDefinitionToDeclaration();
3195 break;
3196 }
3197 }
3198 }
3199}
3200
3201template<>
3202void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003203 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003204 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003205 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3206 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3207
3208 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003209 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003210
3211 // If the previous declaration is an inline function declaration, then this
3212 // declaration is too.
3213 if (PrevFD->IsInline != FD->IsInline) {
3214 // FIXME: [dcl.fct.spec]p4:
3215 // If a function with external linkage is declared inline in one
3216 // translation unit, it shall be declared inline in all translation
3217 // units in which it appears.
3218 //
3219 // Be careful of this case:
3220 //
3221 // module A:
3222 // template<typename T> struct X { void f(); };
3223 // template<typename T> inline void X<T>::f() {}
3224 //
3225 // module B instantiates the declaration of X<int>::f
3226 // module C instantiates the definition of X<int>::f
3227 //
3228 // If module B and C are merged, we do not have a violation of this rule.
3229 FD->IsInline = true;
3230 }
3231
Richard Smith9e2341d2015-03-23 03:25:59 +00003232 // If we need to propagate an exception specification along the redecl
3233 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003234 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3235 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003236 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003237 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3238 bool WasUnresolved =
3239 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3240 if (IsUnresolved != WasUnresolved)
3241 Reader.PendingExceptionSpecUpdates.insert(
3242 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003243 }
3244}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003245} // end namespace clang
3246
Richard Smith6de7a242014-07-31 23:46:44 +00003247void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003248 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3249}
3250
Richard Smith8346e522015-06-10 01:47:58 +00003251/// Inherit the default template argument from \p From to \p To. Returns
3252/// \c false if there is no default template for \p From.
3253template <typename ParmDecl>
3254static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3255 Decl *ToD) {
3256 auto *To = cast<ParmDecl>(ToD);
3257 if (!From->hasDefaultArgument())
3258 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003259 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003260 return true;
3261}
3262
3263static void inheritDefaultTemplateArguments(ASTContext &Context,
3264 TemplateDecl *From,
3265 TemplateDecl *To) {
3266 auto *FromTP = From->getTemplateParameters();
3267 auto *ToTP = To->getTemplateParameters();
3268 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3269
3270 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3271 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003272 if (FromParam->isParameterPack())
3273 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003274 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3275
3276 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003277 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003278 break;
3279 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003280 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003281 break;
3282 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003283 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003284 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3285 break;
3286 }
3287 }
3288}
3289
Richard Smith6de7a242014-07-31 23:46:44 +00003290void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003291 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003292 assert(D && Previous);
3293
3294 switch (D->getKind()) {
3295#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003296#define DECL(TYPE, BASE) \
3297 case Decl::TYPE: \
3298 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003299 break;
3300#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003301 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003302
3303 // If the declaration was visible in one module, a redeclaration of it in
3304 // another module remains visible even if it wouldn't be visible by itself.
3305 //
3306 // FIXME: In this case, the declaration should only be visible if a module
3307 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003308 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003309 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003310 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003311
Richard Smith8346e522015-06-10 01:47:58 +00003312 // If the declaration declares a template, it may inherit default arguments
3313 // from the previous declaration.
3314 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3315 inheritDefaultTemplateArguments(Reader.getContext(),
3316 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003317}
3318
Richard Smithb321ecb2014-05-13 01:15:00 +00003319template<typename DeclT>
3320void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003321 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003322}
3323void ASTDeclReader::attachLatestDeclImpl(...) {
3324 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3325}
3326
Douglas Gregor05f10352011-12-17 23:38:30 +00003327void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3328 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003329
3330 switch (D->getKind()) {
3331#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003332#define DECL(TYPE, BASE) \
3333 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003334 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3335 break;
3336#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003337 }
3338}
3339
Richard Smith851072e2014-05-19 20:59:20 +00003340template<typename DeclT>
3341void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3342 D->RedeclLink.markIncomplete();
3343}
3344void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3345 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3346}
3347
3348void ASTReader::markIncompleteDeclChain(Decl *D) {
3349 switch (D->getKind()) {
3350#define ABSTRACT_DECL(TYPE)
3351#define DECL(TYPE, BASE) \
3352 case Decl::TYPE: \
3353 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3354 break;
3355#include "clang/AST/DeclNodes.inc"
3356 }
3357}
3358
Sebastian Redlb3298c32010-08-18 23:56:48 +00003359/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003360Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003361 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003362 SourceLocation DeclLoc;
3363 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003364 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003365 // Keep track of where we are in the stream, then jump back there
3366 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003367 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003368
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003369 ReadingKindTracker ReadingKind(Read_Decl, *this);
3370
Douglas Gregor1342e842009-07-06 18:54:52 +00003371 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003372 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003373
Sebastian Redl2c373b92010-10-05 15:59:54 +00003374 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003375 ASTRecordReader Record(*this, *Loc.F);
3376 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003377 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003378
Richard Smithdbafb6c2017-06-29 23:23:46 +00003379 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003380 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003381 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003382 case DECL_CONTEXT_LEXICAL:
3383 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003384 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003385 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003386 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003387 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003388 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003389 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003390 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003391 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003392 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003393 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003394 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003395 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003396 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003397 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003398 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003399 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003400 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003401 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003402 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003403 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003404 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003405 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003406 case DECL_EXPORT:
3407 D = ExportDecl::CreateDeserialized(Context, ID);
3408 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003409 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003410 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003411 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003412 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003413 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003414 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003415 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003416 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003417 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003418 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003419 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003420 break;
Richard Smith151c4562016-12-20 21:35:28 +00003421 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003422 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003423 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003424 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003425 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003426 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003427 case DECL_CONSTRUCTOR_USING_SHADOW:
3428 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3429 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003430 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003431 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003432 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003433 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003434 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003435 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003436 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003437 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003438 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003439 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003440 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003441 break;
Richard Smithbc491202017-02-17 20:05:37 +00003442 case DECL_CXX_DEDUCTION_GUIDE:
3443 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3444 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003445 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003446 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003447 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003448 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003449 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3450 break;
3451 case DECL_CXX_INHERITED_CONSTRUCTOR:
3452 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003453 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003454 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003455 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003456 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003458 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003459 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003460 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003461 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003462 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003463 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003464 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003465 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003466 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003467 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003468 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003469 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003470 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003471 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003472 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003473 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003474 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003475 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003476 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003477 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003478 case DECL_VAR_TEMPLATE:
3479 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3480 break;
3481 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3482 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3483 break;
3484 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3485 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3486 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003487 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003488 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003489 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003490 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003491 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003492 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003493 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003494 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003495 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003496 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003497 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003498 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003499 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003500 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3501 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003502 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003503 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003504 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003505 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003506 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3507 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003508 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003509 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003510 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003511 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003512 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003513 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003514 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003515 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003516 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003517 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003518 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003519 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003520 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003521 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003522 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003523 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003524 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003525 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003526 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003527 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003528 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003529 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003530 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003531 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003532 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003533 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003534 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003535 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003536 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003537 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003538 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003539 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003540 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003541 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003542 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003543 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003544 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003545 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003546 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003547 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003548 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003549 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003550 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003551 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003552 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003553 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003554 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003555 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003556 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003557 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003558 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003559 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003560 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003561 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003562 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003563 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003564 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003565 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003566 break;
3567 case DECL_BINDING:
3568 D = BindingDecl::CreateDeserialized(Context, ID);
3569 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003570 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003571 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003572 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003573 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003574 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003575 break;
John McCall5e77d762013-04-16 07:28:30 +00003576 case DECL_MS_PROPERTY:
3577 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3578 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003579 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003580 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003581 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003582 case DECL_CXX_BASE_SPECIFIERS:
3583 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003584 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003585 case DECL_CXX_CTOR_INITIALIZERS:
3586 Error("attempt to read a C++ ctor initializer record as a declaration");
3587 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003588 case DECL_IMPORT:
3589 // Note: last entry of the ImportDecl record is the number of stored source
3590 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003591 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003592 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003593 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003594 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003595 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003596 case DECL_OMP_DECLARE_REDUCTION:
3597 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3598 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003599 case DECL_OMP_CAPTUREDEXPR:
3600 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003601 break;
Nico Weber66220292016-03-02 17:28:48 +00003602 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003603 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003604 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003605 case DECL_PRAGMA_DETECT_MISMATCH:
3606 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003607 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003608 break;
Michael Han84324352013-02-22 17:15:32 +00003609 case DECL_EMPTY:
3610 D = EmptyDecl::CreateDeserialized(Context, ID);
3611 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003612 case DECL_OBJC_TYPE_PARAM:
3613 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3614 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003615 }
Chris Lattner487412d2009-04-27 05:27:42 +00003616
Sebastian Redlb3298c32010-08-18 23:56:48 +00003617 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003618 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003619 // Set the DeclContext before doing any deserialization, to make sure internal
3620 // calls to Decl::getASTContext() by Decl's methods will find the
3621 // TranslationUnitDecl without crashing.
3622 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003623 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003624
3625 // If this declaration is also a declaration context, get the
3626 // offsets for its tables of lexical and visible declarations.
3627 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3628 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003629 if (Offsets.first &&
3630 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3631 return nullptr;
3632 if (Offsets.second &&
3633 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3634 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003635 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003636 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003637
Douglas Gregordab42432011-08-12 00:15:20 +00003638 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003639 PendingUpdateRecords.push_back(
3640 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003641
Douglas Gregor404cdde2012-01-27 01:47:08 +00003642 // Load the categories after recursive loading is finished.
3643 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003644 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3645 // we put the Decl in PendingDefinitions so we can pull the categories here.
3646 if (Class->isThisDeclarationADefinition() ||
3647 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003648 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003649
Richard Smith13fb8602016-07-15 21:33:46 +00003650 // If we have deserialized a declaration that has a definition the
3651 // AST consumer might need to know about, queue it.
3652 // We don't pass it to the consumer immediately because we may be in recursive
3653 // loading, and some declarations may still be initializing.
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003654 PotentiallyInterestingDecls.push_back(
3655 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003656
Douglas Gregordab42432011-08-12 00:15:20 +00003657 return D;
3658}
3659
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003660void ASTReader::PassInterestingDeclsToConsumer() {
3661 assert(Consumer);
3662
3663 if (PassingDeclsToConsumer)
3664 return;
3665
3666 // Guard variable to avoid recursively redoing the process of passing
3667 // decls to consumer.
3668 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3669 true);
3670
3671 // Ensure that we've loaded all potentially-interesting declarations
3672 // that need to be eagerly loaded.
3673 for (auto ID : EagerlyDeserializedDecls)
3674 GetDecl(ID);
3675 EagerlyDeserializedDecls.clear();
3676
3677 while (!PotentiallyInterestingDecls.empty()) {
3678 InterestingDecl D = PotentiallyInterestingDecls.front();
3679 PotentiallyInterestingDecls.pop_front();
Richard Smithdbafb6c2017-06-29 23:23:46 +00003680 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003681 PassInterestingDeclToConsumer(D.getDecl());
3682 }
3683}
3684
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003685void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003686 // The declaration may have been modified by files later in the chain.
3687 // If this is the case, read the record containing the updates from each file
3688 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003689 serialization::GlobalDeclID ID = Record.ID;
3690 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003691 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003692 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003693
3694 llvm::SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
3695
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003696 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003697 auto UpdateOffsets = std::move(UpdI->second);
3698 DeclUpdateOffsets.erase(UpdI);
3699
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003700 // Check if this decl was interesting to the consumer. If we just loaded
3701 // the declaration, then we know it was interesting and we skip the call
3702 // to isConsumerInterestedIn because it is unsafe to call in the
3703 // current ASTReader state.
3704 bool WasInteresting =
Richard Smithdbafb6c2017-06-29 23:23:46 +00003705 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003706 for (auto &FileAndOffset : UpdateOffsets) {
3707 ModuleFile *F = FileAndOffset.first;
3708 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003709 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3710 SavedStreamPosition SavedPosition(Cursor);
3711 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003712 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003713 ASTRecordReader Record(*this, *F);
3714 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003715 (void)RecCode;
3716 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003717
David L. Jonesbe1557a2016-12-21 00:17:49 +00003718 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3719 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003720 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003721
3722 // We might have made this declaration interesting. If so, remember that
3723 // we need to hand it off to the consumer.
3724 if (!WasInteresting &&
Richard Smithdbafb6c2017-06-29 23:23:46 +00003725 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003726 PotentiallyInterestingDecls.push_back(
3727 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003728 WasInteresting = true;
3729 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003730 }
3731 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003732 // Add the lazy specializations to the template.
3733 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3734 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3735 "Must not have pending specializations");
3736 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3737 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3738 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3739 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3740 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3741 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3742 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003743
3744 // Load the pending visible updates for this decl context, if it has any.
3745 auto I = PendingVisibleUpdates.find(ID);
3746 if (I != PendingVisibleUpdates.end()) {
3747 auto VisibleUpdates = std::move(I->second);
3748 PendingVisibleUpdates.erase(I);
3749
3750 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3751 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3752 Lookups[DC].Table.add(
3753 Update.Mod, Update.Data,
3754 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3755 DC->setHasExternalVisibleStorage(true);
3756 }
Chris Lattner487412d2009-04-27 05:27:42 +00003757}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003758
Richard Smithd61d4ac2015-08-22 20:13:39 +00003759void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3760 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003761 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003762 if (FirstLocal != CanonDecl) {
3763 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3764 ASTDeclReader::attachPreviousDecl(
3765 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3766 CanonDecl);
3767 }
3768
3769 if (!LocalOffset) {
3770 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3771 return;
3772 }
3773
3774 // Load the list of other redeclarations from this module file.
3775 ModuleFile *M = getOwningModuleFile(FirstLocal);
3776 assert(M && "imported decl from no module file");
3777
3778 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3779 SavedStreamPosition SavedPosition(Cursor);
3780 Cursor.JumpToBit(LocalOffset);
3781
3782 RecordData Record;
3783 unsigned Code = Cursor.ReadCode();
3784 unsigned RecCode = Cursor.readRecord(Code, Record);
3785 (void)RecCode;
3786 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3787
3788 // FIXME: We have several different dispatches on decl kind here; maybe
3789 // we should instead generate one loop per kind and dispatch up-front?
3790 Decl *MostRecent = FirstLocal;
3791 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3792 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003793 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3794 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003795 }
Richard Smithc3a53252015-02-28 05:57:02 +00003796 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003797}
3798
3799namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003800 /// \brief Given an ObjC interface, goes through the modules and links to the
3801 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003802 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003803 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003804 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003805 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003806 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003807 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003808 serialization::GlobalDeclID InterfaceID;
3809 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003810
3811 void add(ObjCCategoryDecl *Cat) {
3812 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003813 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003814 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003815
3816 // Check for duplicate categories.
3817 if (Cat->getDeclName()) {
3818 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3819 if (Existing &&
3820 Reader.getOwningModuleFile(Existing)
3821 != Reader.getOwningModuleFile(Cat)) {
3822 // FIXME: We should not warn for duplicates in diamond:
3823 //
3824 // MT //
3825 // / \ //
3826 // ML MR //
3827 // \ / //
3828 // MB //
3829 //
3830 // If there are duplicates in ML/MR, there will be warning when
3831 // creating MB *and* when importing MB. We should not warn when
3832 // importing.
3833 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3834 << Interface->getDeclName() << Cat->getDeclName();
3835 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3836 } else if (!Existing) {
3837 // Record this category.
3838 Existing = Cat;
3839 }
3840 }
3841
3842 // Add this category to the end of the chain.
3843 if (Tail)
3844 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3845 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003846 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003847 Tail = Cat;
3848 }
3849
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003850 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003851 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003852 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003853 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3854 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003855 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003856 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3857 Tail(nullptr), InterfaceID(InterfaceID),
3858 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003859 {
3860 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003861 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003862 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003863 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003864
3865 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003866 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003867 }
3868 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003869
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003870 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003871 // If we've loaded all of the category information we care about from
3872 // this module file, we're done.
3873 if (M.Generation <= PreviousGeneration)
3874 return true;
3875
3876 // Map global ID of the definition down to the local ID used in this
3877 // module file. If there is no such mapping, we'll find nothing here
3878 // (or in any module it imports).
3879 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3880 if (!LocalID)
3881 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003882
Douglas Gregor404cdde2012-01-27 01:47:08 +00003883 // Perform a binary search to find the local redeclarations for this
3884 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003885 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003886 const ObjCCategoriesInfo *Result
3887 = std::lower_bound(M.ObjCCategoriesMap,
3888 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003889 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003890 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3891 Result->DefinitionID != LocalID) {
3892 // We didn't find anything. If the class definition is in this module
3893 // file, then the module files it depends on cannot have any categories,
3894 // so suppress further lookup.
3895 return Reader.isDeclIDFromModule(InterfaceID, M);
3896 }
3897
3898 // We found something. Dig out all of the categories.
3899 unsigned Offset = Result->Offset;
3900 unsigned N = M.ObjCCategories[Offset];
3901 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3902 for (unsigned I = 0; I != N; ++I)
3903 add(cast_or_null<ObjCCategoryDecl>(
3904 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3905 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003906 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003907 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003908} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003909
Douglas Gregor404cdde2012-01-27 01:47:08 +00003910void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3911 ObjCInterfaceDecl *D,
3912 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003913 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003914 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003915 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003916}
Douglas Gregordab42432011-08-12 00:15:20 +00003917
Richard Smithd6db68c2014-08-07 20:58:41 +00003918template<typename DeclT, typename Fn>
3919static void forAllLaterRedecls(DeclT *D, Fn F) {
3920 F(D);
3921
3922 // Check whether we've already merged D into its redeclaration chain.
3923 // MostRecent may or may not be nullptr if D has not been merged. If
3924 // not, walk the merged redecl chain and see if it's there.
3925 auto *MostRecent = D->getMostRecentDecl();
3926 bool Found = false;
3927 for (auto *Redecl = MostRecent; Redecl && !Found;
3928 Redecl = Redecl->getPreviousDecl())
3929 Found = (Redecl == D);
3930
3931 // If this declaration is merged, apply the functor to all later decls.
3932 if (Found) {
3933 for (auto *Redecl = MostRecent; Redecl != D;
3934 Redecl = Redecl->getPreviousDecl())
3935 F(Redecl);
3936 }
3937}
3938
Vassil Vassilev7d264622017-06-30 22:40:17 +00003939void ASTDeclReader::UpdateDecl(Decl *D,
3940 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003941 while (Record.getIdx() < Record.size()) {
3942 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003943 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003944 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003945 // FIXME: If we also have an update record for instantiating the
3946 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003947 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003948 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003949 // FIXME: We should call addHiddenDecl instead, to add the member
3950 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003951 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003952 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003953 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003954
3955 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00003956 // It will be added to the template's lazy specialization set.
3957 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003958 break;
3959
3960 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003961 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3962
Douglas Gregor540fd812012-01-09 18:07:24 +00003963 // Each module has its own anonymous namespace, which is disjoint from
3964 // any other module's anonymous namespaces, so don't attach the anonymous
3965 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003966 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003967 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3968 TU->setAnonymousNamespace(Anon);
3969 else
3970 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3971 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003972 break;
3973 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003974
Richard Smith05a21352017-06-22 22:18:46 +00003975 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: {
3976 VarDecl *VD = cast<VarDecl>(D);
3977 VD->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003978 ReadSourceLocation());
Richard Smith05a21352017-06-22 22:18:46 +00003979 uint64_t Val = Record.readInt();
3980 if (Val && !VD->getInit()) {
3981 VD->setInit(Record.readExpr());
3982 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
3983 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
3984 Eval->CheckedICE = true;
3985 Eval->IsICE = Val == 3;
3986 }
3987 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003988 break;
Richard Smith05a21352017-06-22 22:18:46 +00003989 }
Richard Smith1fa5d642013-05-11 05:45:24 +00003990
John McCall32791cc2016-01-06 22:34:54 +00003991 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3992 auto Param = cast<ParmVarDecl>(D);
3993
3994 // We have to read the default argument regardless of whether we use it
3995 // so that hypothetical further update records aren't messed up.
3996 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003997 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003998
3999 // Only apply the update if the parameter still has an uninstantiated
4000 // default argument.
4001 if (Param->hasUninstantiatedDefaultArg())
4002 Param->setDefaultArg(DefaultArg);
4003 break;
4004 }
4005
Richard Smith4b054b22016-08-24 21:25:37 +00004006 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
4007 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00004008 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004009
4010 // Only apply the update if the field still has an uninstantiated
4011 // default member initializer.
4012 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4013 if (DefaultInit)
4014 FD->setInClassInitializer(DefaultInit);
4015 else
4016 // Instantiation failed. We can get here if we serialized an AST for
4017 // an invalid program.
4018 FD->removeInClassInitializer();
4019 }
4020 break;
4021 }
4022
Richard Smith4d235792014-08-07 18:53:08 +00004023 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00004024 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004025 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00004026 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004027 // It's safe to stop now because this update record is always last.
4028 return;
4029 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00004030
David L. Jonesbe1557a2016-12-21 00:17:49 +00004031 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004032 // Maintain AST consistency: any later redeclarations of this function
4033 // are inline if this one is. (We might have merged another declaration
4034 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004035 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4036 FD->setImplicitlyInline();
4037 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004038 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004039 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004040 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004041 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004042 break;
4043 }
4044
Richard Smithcd45dbc2014-04-19 03:48:30 +00004045 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4046 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004047 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004048 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004049 OldDD && (OldDD->Definition != RD ||
4050 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00004051 ReadCXXRecordDefinition(RD, /*Update*/true);
4052
Richard Smithcd45dbc2014-04-19 03:48:30 +00004053 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004054 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004055 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004056 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004057 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004058 }
4059
David L. Jonesbe1557a2016-12-21 00:17:49 +00004060 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004061 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004062 if (MemberSpecializationInfo *MSInfo =
4063 RD->getMemberSpecializationInfo()) {
4064 MSInfo->setTemplateSpecializationKind(TSK);
4065 MSInfo->setPointOfInstantiation(POI);
4066 } else {
4067 ClassTemplateSpecializationDecl *Spec =
4068 cast<ClassTemplateSpecializationDecl>(RD);
4069 Spec->setTemplateSpecializationKind(TSK);
4070 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004071
David L. Jonesbe1557a2016-12-21 00:17:49 +00004072 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00004073 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004074 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004075 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004076 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004077 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004078 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004079
4080 // FIXME: If we already have a partial specialization set,
4081 // check that it matches.
4082 if (!Spec->getSpecializedTemplateOrPartial()
4083 .is<ClassTemplatePartialSpecializationDecl *>())
4084 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004085 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004086 }
4087
David L. Jonesbe1557a2016-12-21 00:17:49 +00004088 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004089 RD->setLocation(ReadSourceLocation());
4090 RD->setLocStart(ReadSourceLocation());
4091 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004092
David L. Jonesbe1557a2016-12-21 00:17:49 +00004093 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004094 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004095 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004096 // If the declaration already has attributes, we assume that some other
4097 // AST file already loaded them.
4098 if (!D->hasAttrs())
4099 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004100 }
4101 break;
4102 }
4103
Richard Smithf8134002015-03-10 01:41:22 +00004104 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4105 // Set the 'operator delete' directly to avoid emitting another update
4106 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004107 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004108 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
4109 // FIXME: Check consistency if we have an old and new operator delete.
4110 if (!First->OperatorDelete)
4111 First->OperatorDelete = Del;
4112 break;
4113 }
4114
Richard Smith564417a2014-03-20 21:47:22 +00004115 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004116 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004117 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004118 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004119
4120 // Update this declaration's exception specification, if needed.
4121 auto *FD = cast<FunctionDecl>(D);
4122 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4123 // FIXME: If the exception specification is already present, check that it
4124 // matches.
4125 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004126 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004127 FPT->getReturnType(), FPT->getParamTypes(),
4128 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004129
4130 // When we get to the end of deserializing, see if there are other decls
4131 // that we need to propagate this exception specification onto.
4132 Reader.PendingExceptionSpecUpdates.insert(
4133 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004134 }
Richard Smith564417a2014-03-20 21:47:22 +00004135 break;
4136 }
4137
Richard Smith1fa5d642013-05-11 05:45:24 +00004138 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004139 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004140 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004141 for (auto *Redecl : merged_redecls(D)) {
4142 // FIXME: If the return type is already deduced, check that it matches.
4143 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
Richard Smithdbafb6c2017-06-29 23:23:46 +00004144 Reader.getContext().adjustDeducedFunctionResultType(FD,
4145 DeducedResultType);
Richard Smithd6db68c2014-08-07 20:58:41 +00004146 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004147 break;
4148 }
Eli Friedman276dd182013-09-05 00:02:25 +00004149
4150 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004151 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004152 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004153 break;
4154 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004155
4156 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004157 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4158 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004159 break;
4160
4161 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004162 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4163 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004164 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004165
Alexey Bataev97720002014-11-11 04:05:39 +00004166 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004167 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4168 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004169 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004170
Alex Denisovfde64952015-06-26 05:28:36 +00004171 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004172 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004173 auto *Exported = cast<NamedDecl>(D);
4174 if (auto *TD = dyn_cast<TagDecl>(Exported))
4175 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004176 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004177 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004178 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4179 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004180 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4181 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004182 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004183 // If Owner is made visible at some later point, make this declaration
4184 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004185 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004186 } else {
4187 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004188 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004189 }
4190 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004191 }
Alex Denisovfde64952015-06-26 05:28:36 +00004192
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004193 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004194 case UPD_ADDED_ATTR_TO_RECORD:
4195 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004196 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004197 assert(Attrs.size() == 1);
4198 D->addAttr(Attrs[0]);
4199 break;
4200 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004201 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004202}