blob: 07ab421cfc51daf47b805a28c2c8808b81d3243b [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);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000129
Richard Smithd08aeb62014-08-28 01:33:39 +0000130 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
131 DeclContext *DC,
132 unsigned Index);
133 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
134 unsigned Index, NamedDecl *D);
135
Richard Smith0144f512015-08-22 02:09:38 +0000136 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000137 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000138 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000139 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000140 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000141
Douglas Gregor022857e2011-12-22 01:48:48 +0000142 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000143 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
144 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000145
Douglas Gregor022857e2011-12-22 01:48:48 +0000146 /// \brief Retrieve the first ID.
147 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000148
Richard Smith0144f512015-08-22 02:09:38 +0000149 /// \brief Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000150 bool isKeyDecl() const { return IsKeyDecl; }
151
Richard Smith5a4737c2015-02-06 02:42:59 +0000152 /// \brief Get a known declaration that this should be merged with, if
153 /// any.
154 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000155 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000156
Douglas Gregor022857e2011-12-22 01:48:48 +0000157 /// \brief Class used to capture the result of searching for an existing
158 /// declaration of a specific kind and name, along with the ability
159 /// to update the place where this result was found (the declaration
160 /// chain hanging off an identifier or the DeclContext we searched in)
161 /// if requested.
162 class FindExistingResult {
163 ASTReader &Reader;
164 NamedDecl *New;
165 NamedDecl *Existing;
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000166 bool AddResult;
Richard Smith70d58502014-08-30 00:04:23 +0000167
Richard Smithd08aeb62014-08-28 01:33:39 +0000168 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +0000169 IdentifierInfo *TypedefNameForLinkage;
Richard Smithd08aeb62014-08-28 01:33:39 +0000170
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000171 void operator=(FindExistingResult &&) = delete;
Richard Smithd08aeb62014-08-28 01:33:39 +0000172
Douglas Gregor022857e2011-12-22 01:48:48 +0000173 public:
174 FindExistingResult(ASTReader &Reader)
Richard Smithd08aeb62014-08-28 01:33:39 +0000175 : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false),
Hans Wennborgdcfba332015-10-06 23:40:43 +0000176 AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000177
Richard Smithd08aeb62014-08-28 01:33:39 +0000178 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000179 unsigned AnonymousDeclNumber,
180 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000182 AnonymousDeclNumber(AnonymousDeclNumber),
183 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000184
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000185 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000186 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
187 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000188 AnonymousDeclNumber(Other.AnonymousDeclNumber),
189 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000190 Other.AddResult = false;
191 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000192
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000194
Douglas Gregor2009cee2012-01-03 22:46:00 +0000195 /// \brief Suppress the addition of this result into the known set of
196 /// names.
197 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000198
Douglas Gregor022857e2011-12-22 01:48:48 +0000199 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000200
Douglas Gregor022857e2011-12-22 01:48:48 +0000201 template<typename T>
202 operator T*() const { return dyn_cast_or_null<T>(Existing); }
203 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000204
Richard Smith8a639892015-01-24 01:07:20 +0000205 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
206 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000207 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000208
Chris Lattner487412d2009-04-27 05:27:42 +0000209 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000210 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
211 ASTReader::RecordLocation Loc,
212 DeclID thisDeclID, SourceLocation ThisDeclLoc)
213 : Reader(Reader), Record(Record), Loc(Loc),
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000214 ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc),
David L. Jonesbe1557a2016-12-21 00:17:49 +0000215 TypeIDForTypeDecl(0), NamedDeclForTagDecl(0),
Vassil Vassilev928c8252016-04-28 14:13:28 +0000216 TypedefNameForLinkage(nullptr), HasPendingBody(false),
217 IsDeclMarkedUsed(false) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000218
Richard Smithb321ecb2014-05-13 01:15:00 +0000219 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000220 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
221 static Decl *getMostRecentDeclImpl(...);
222 static Decl *getMostRecentDecl(Decl *D);
223
224 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000225 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000226 Redeclarable<DeclT> *D, Decl *Previous,
227 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000228 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000229 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
230 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000231
232 template <typename DeclT>
233 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
234 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000235 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000236
Richard Smith851072e2014-05-19 20:59:20 +0000237 template <typename DeclT>
238 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
239 static void markIncompleteDeclChainImpl(...);
240
Douglas Gregora6017bb2012-10-09 17:21:28 +0000241 /// \brief Determine whether this declaration has a pending body.
242 bool hasPendingBody() const { return HasPendingBody; }
243
David Blaikieac4345c2017-02-12 18:45:31 +0000244 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000245 void Visit(Decl *D);
246
Vassil Vassileva91cbdc2017-06-15 11:05:32 +0000247 void UpdateDecl(Decl *D);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000248
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000249 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
250 ObjCCategoryDecl *Next) {
251 Cat->NextClassCategory = Next;
252 }
253
Chris Lattner487412d2009-04-27 05:27:42 +0000254 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000255 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000256 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000257 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
258 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000259 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000260 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000261 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
262 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000263 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000264 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000265 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000266 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000267 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000268 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000269 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000270 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
271 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
272 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
273 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
274 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000275 ClassTemplateSpecializationDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000276 void VisitClassTemplateSpecializationDecl(
277 ClassTemplateSpecializationDecl *D) {
278 VisitClassTemplateSpecializationDeclImpl(D);
279 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000280 void VisitClassTemplatePartialSpecializationDecl(
281 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000282 void VisitClassScopeFunctionSpecializationDecl(
283 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000284 RedeclarableResult
285 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
286 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
287 VisitVarTemplateSpecializationDeclImpl(D);
288 }
289 void VisitVarTemplatePartialSpecializationDecl(
290 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000291 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000292 void VisitValueDecl(ValueDecl *VD);
293 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000294 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000295 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000296 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000297 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000298 void VisitCXXMethodDecl(CXXMethodDecl *D);
299 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
300 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
301 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000302 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000303 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000304 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000305 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
306 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000307 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
308 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000309 void VisitDecompositionDecl(DecompositionDecl *DD);
310 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000311 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000312 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000313 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000314 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000315 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000316 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000317 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000318 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000319 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000320 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000321 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000322 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000323 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000324 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000325 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000326 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000327 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000328 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000329 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000330 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
331 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000332 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000333 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000334 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000335
Chris Lattner487412d2009-04-27 05:27:42 +0000336 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000337
338 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000339 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000340
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000341 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000342 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
343 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000344
345 template<typename T>
346 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000347 RedeclarableResult &Redecl,
348 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000349
Richard Smith0b87e072013-10-07 08:02:11 +0000350 template<typename T>
351 void mergeMergeable(Mergeable<T> *D);
352
Richard Smithf17fdbd2014-04-24 02:25:27 +0000353 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
354 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000355 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000356
Douglas Gregor85f3f952015-07-07 03:57:15 +0000357 ObjCTypeParamList *ReadObjCTypeParamList();
358
Alexis Hunted053252010-05-30 07:21:58 +0000359 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000360 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000361 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000362 void VisitObjCContainerDecl(ObjCContainerDecl *D);
363 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
364 void VisitObjCIvarDecl(ObjCIvarDecl *D);
365 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
366 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000367 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
368 void VisitObjCImplDecl(ObjCImplDecl *D);
369 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
370 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
371 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
372 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
373 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000374 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000375 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000376 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000377 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000378} // end namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000379
Richard Smith86dfc1e2015-06-18 22:07:00 +0000380namespace {
381/// Iterator over the redeclarations of a declaration that have already
382/// been merged into the same redeclaration chain.
383template<typename DeclT>
384class MergedRedeclIterator {
385 DeclT *Start, *Canonical, *Current;
386public:
387 MergedRedeclIterator() : Current(nullptr) {}
388 MergedRedeclIterator(DeclT *Start)
389 : Start(Start), Canonical(nullptr), Current(Start) {}
390
391 DeclT *operator*() { return Current; }
392
393 MergedRedeclIterator &operator++() {
394 if (Current->isFirstDecl()) {
395 Canonical = Current;
396 Current = Current->getMostRecentDecl();
397 } else
398 Current = Current->getPreviousDecl();
399
400 // If we started in the merged portion, we'll reach our start position
401 // eventually. Otherwise, we'll never reach it, but the second declaration
402 // we reached was the canonical declaration, so stop when we see that one
403 // again.
404 if (Current == Start || Current == Canonical)
405 Current = nullptr;
406 return *this;
407 }
408
409 friend bool operator!=(const MergedRedeclIterator &A,
410 const MergedRedeclIterator &B) {
411 return A.Current != B.Current;
412 }
413};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000414} // end anonymous namespace
415
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000416template <typename DeclT>
417static llvm::iterator_range<MergedRedeclIterator<DeclT>>
418merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000419 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
420 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000421}
422
Sebastian Redlb3298c32010-08-18 23:56:48 +0000423uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000424 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000425}
426
David Blaikieac4345c2017-02-12 18:45:31 +0000427void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
David Blaikiee6b7c282017-04-11 20:46:34 +0000428 if (Record.readInt())
429 Reader.BodySource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000430 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
431 CD->NumCtorInitializers = Record.readInt();
432 if (CD->NumCtorInitializers)
433 CD->CtorInitializers = ReadGlobalOffset();
434 }
435 // Store the offset of the body so we can lazily load it later.
436 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
437 HasPendingBody = true;
438}
439
Sebastian Redlb3298c32010-08-18 23:56:48 +0000440void ASTDeclReader::Visit(Decl *D) {
441 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000442
Vassil Vassilev928c8252016-04-28 14:13:28 +0000443 // At this point we have deserialized and merged the decl and it is safe to
444 // update its canonical decl to signal that the entire entity is used.
445 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
446 IsDeclMarkedUsed = false;
447
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000448 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
449 if (DD->DeclInfo) {
450 DeclaratorDecl::ExtInfo *Info =
451 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000452 Info->TInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000453 }
454 else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000455 DD->DeclInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000456 }
457 }
458
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000459 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000460 // We have a fully initialized TypeDecl. Read its type now.
Douglas Gregor0cdc8322010-12-10 17:03:06 +0000461 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000462
463 // If this is a tag declaration with a typedef name for linkage, it's safe
464 // to load that typedef now.
465 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000466 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
467 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000468 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
469 // if we have a fully initialized TypeDecl, we can safely read its type now.
470 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000471 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
472 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000473 // We only read it if FD doesn't already have a body (e.g., from another
474 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000475 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000476 if (Record.readInt())
477 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000478 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000479}
480
Sebastian Redlb3298c32010-08-18 23:56:48 +0000481void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000482 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
483 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000484 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000485 // parameter or of a parameter of a function template immediately. These
486 // entities might be used in the formulation of its DeclContext (for
487 // example, a function parameter can be used in decltype() in trailing
488 // return type of the function). Use the translation unit DeclContext as a
489 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000490 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
491 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000492 if (!LexicalDCIDForTemplateParmDecl)
493 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000494 Reader.addPendingDeclContextInfo(D,
495 SemaDCIDForTemplateParmDecl,
496 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000497 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000498 } else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000499 DeclContext *SemaDC = ReadDeclAs<DeclContext>();
500 DeclContext *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000501 if (!LexicalDC)
502 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000503 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000504 // Avoid calling setLexicalDeclContext() directly because it uses
505 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000506 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
507 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000508 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000509 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000510 D->setInvalidDecl(Record.readInt());
511 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000512 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000513 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000514 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
515 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000516 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000517 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000518 D->setImplicit(Record.readInt());
519 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000520 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000521 D->setReferenced(Record.readInt());
522 D->setTopLevelDeclInObjCContainer(Record.readInt());
523 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000524 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000525 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000526
Douglas Gregorcf68c582011-12-01 22:20:10 +0000527 // Determine whether this declaration is part of a (sub)module. If so, it
528 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000529 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000530 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000531 D->setModuleOwnershipKind(
532 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
533 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000534 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000535
Richard Smith90dc5252017-06-23 01:04:34 +0000536 if (ModulePrivate) {
537 // Module-private declarations are never visible, so there is no work to
538 // do.
Richard Smith42413142015-05-15 20:05:43 +0000539 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
540 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000541 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000542 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000543 // Mark the declaration as visible when its owning module becomes visible.
544 if (Owner->NameVisibility == Module::AllVisible)
545 D->setVisibleDespiteOwningModule();
546 else
Richard Smith42413142015-05-15 20:05:43 +0000547 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000548 }
549 }
Chris Lattner487412d2009-04-27 05:27:42 +0000550}
551
Nico Weber66220292016-03-02 17:28:48 +0000552void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
553 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000554 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000555 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000556 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000557 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
558 D->getTrailingObjects<char>()[Arg.size()] = '\0';
559}
560
Nico Webercbbaeb12016-03-02 19:28:54 +0000561void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
562 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000563 D->setLocation(ReadSourceLocation());
564 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000565 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
566 D->getTrailingObjects<char>()[Name.size()] = '\0';
567
568 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000569 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000570 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
571 Value.size());
572 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
573}
574
Sebastian Redlb3298c32010-08-18 23:56:48 +0000575void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000576 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000577}
578
Sebastian Redlb3298c32010-08-18 23:56:48 +0000579void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000580 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000581 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000582 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000583}
584
Sebastian Redlb3298c32010-08-18 23:56:48 +0000585void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000586 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000587 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000588 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000589 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000590}
591
Richard Smith43ccec8e2014-08-26 03:52:16 +0000592ASTDeclReader::RedeclarableResult
593ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000594 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000595 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000596 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000597 if (Record.readInt()) { // isModed
598 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000599 TD->setModedTypeSourceInfo(TInfo, modedT);
600 } else
601 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000602 // Read and discard the declaration for which this is a typedef name for
603 // linkage, if it exists. We cannot rely on our type to pull in this decl,
604 // because it might have been merged with a type from another module and
605 // thus might not refer to our version of the declaration.
606 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000607 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000608}
609
610void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000611 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
612 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000613}
614
Richard Smithdda56e42011-04-15 14:24:37 +0000615void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000616 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000617 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000618 // Merged when we merge the template.
619 TD->setDescribedAliasTemplate(Template);
620 else
621 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000622}
623
Richard Smith1d209d02013-05-23 01:49:11 +0000624ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000625 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000626 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000627
David L. Jonesbe1557a2016-12-21 00:17:49 +0000628 TD->IdentifierNamespace = Record.readInt();
629 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000630 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000631 TD->setCompleteDefinition(Record.readInt());
632 TD->setEmbeddedInDeclarator(Record.readInt());
633 TD->setFreeStanding(Record.readInt());
634 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000635 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000636
David L. Jonesbe1557a2016-12-21 00:17:49 +0000637 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000638 case 0:
639 break;
640 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000641 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000642 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000643 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000644 break;
645 }
646 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000647 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000648 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000649 break;
Richard Smith70d58502014-08-30 00:04:23 +0000650 default:
651 llvm_unreachable("unexpected tag info kind");
652 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000653
Richard Smithcd45dbc2014-04-19 03:48:30 +0000654 if (!isa<CXXRecordDecl>(TD))
655 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000656 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000657}
658
Sebastian Redlb3298c32010-08-18 23:56:48 +0000659void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000660 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000661 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000662 ED->setIntegerTypeSourceInfo(TI);
663 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000664 ED->setIntegerType(Record.readType());
665 ED->setPromotionType(Record.readType());
666 ED->setNumPositiveBits(Record.readInt());
667 ED->setNumNegativeBits(Record.readInt());
668 ED->IsScoped = Record.readInt();
669 ED->IsScopedUsingClassTag = Record.readInt();
670 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000671
Richard Smith01a73372013-10-15 22:02:41 +0000672 // If this is a definition subject to the ODR, and we already have a
673 // definition, merge this one into it.
674 if (ED->IsCompleteDefinition &&
675 Reader.getContext().getLangOpts().Modules &&
676 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000677 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
678 if (!OldDef) {
679 // This is the first time we've seen an imported definition. Look for a
680 // local definition before deciding that we are the first definition.
681 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
682 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
683 OldDef = D;
684 break;
685 }
686 }
687 }
688 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000689 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
690 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000691 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000692 } else {
693 OldDef = ED;
694 }
695 }
696
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000697 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000698 TemplateSpecializationKind TSK =
699 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000700 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000701 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
702 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
703 }
Chris Lattner487412d2009-04-27 05:27:42 +0000704}
705
Richard Smith1d209d02013-05-23 01:49:11 +0000706ASTDeclReader::RedeclarableResult
707ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
708 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000709 RD->setHasFlexibleArrayMember(Record.readInt());
710 RD->setAnonymousStructOrUnion(Record.readInt());
711 RD->setHasObjectMember(Record.readInt());
712 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000713 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000714}
715
Sebastian Redlb3298c32010-08-18 23:56:48 +0000716void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000717 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000718 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000719}
720
Sebastian Redlb3298c32010-08-18 23:56:48 +0000721void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000722 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000723 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000724 ECD->setInitExpr(Record.readExpr());
725 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000726 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000727}
728
Sebastian Redlb3298c32010-08-18 23:56:48 +0000729void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000730 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000731 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000732 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000733 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000734 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000735 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000736 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000737 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000738}
739
Sebastian Redlb3298c32010-08-18 23:56:48 +0000740void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000741 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000742 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000743
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000744 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000745 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000746
Douglas Gregorb2585692012-01-04 17:13:46 +0000747 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
748 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000749
David L. Jonesbe1557a2016-12-21 00:17:49 +0000750 FD->SClass = (StorageClass)Record.readInt();
751 FD->IsInline = Record.readInt();
752 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000753 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000754 FD->IsVirtualAsWritten = Record.readInt();
755 FD->IsPure = Record.readInt();
756 FD->HasInheritedPrototype = Record.readInt();
757 FD->HasWrittenPrototype = Record.readInt();
758 FD->IsDeleted = Record.readInt();
759 FD->IsTrivial = Record.readInt();
760 FD->IsDefaulted = Record.readInt();
761 FD->IsExplicitlyDefaulted = Record.readInt();
762 FD->HasImplicitReturnZero = Record.readInt();
763 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000764 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000765 FD->HasSkippedBody = Record.readInt();
766 FD->IsLateTemplateParsed = Record.readInt();
767 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000768 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000769
David L. Jonesbe1557a2016-12-21 00:17:49 +0000770 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000771 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000772 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000773 break;
774 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000775 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000776 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000777 break;
778 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000779 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000780 TemplateSpecializationKind TSK =
781 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000782 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000783 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000784 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000785 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000786 break;
787 }
788 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000789 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000790 TemplateSpecializationKind TSK =
791 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000792
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000793 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000794 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000795 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000796
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000797 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000798 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000799 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000800 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000801 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000802 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000803 TemplArgLocs.reserve(NumTemplateArgLocs);
804 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000805 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000806
807 LAngleLoc = ReadSourceLocation();
808 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000809 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000810
811 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000812
Douglas Gregor4163aca2011-09-09 21:34:22 +0000813 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000814 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000815 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000816 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000817 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000818 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000819 FunctionTemplateSpecializationInfo *FTInfo
820 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
821 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000822 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
823 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000824 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000825 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000826
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000827 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000828 // The template that contains the specializations set. It's not safe to
829 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000830 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000831 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
832 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
833 // FunctionTemplateSpecializationInfo's Profile().
834 // We avoid getASTContext because a decl in the parent hierarchy may
835 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000836 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000837 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000838 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000839 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000840 FunctionTemplateSpecializationInfo *ExistingInfo =
841 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000842 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000843 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
844 else {
845 assert(Reader.getContext().getLangOpts().Modules &&
846 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000847 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000848 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000849 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000850 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000851 }
852 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
853 // Templates.
854 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000855 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000856 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000857 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
858
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000859 // Templates args.
860 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000861 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000862 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000863 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000864 TemplArgs.setLAngleLoc(ReadSourceLocation());
865 TemplArgs.setRAngleLoc(ReadSourceLocation());
866
Douglas Gregor4163aca2011-09-09 21:34:22 +0000867 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000868 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000869 // These are not merged; we don't need to merge redeclarations of dependent
870 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000871 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000872 }
873 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000874
Chris Lattnerca025db2010-05-07 21:43:38 +0000875 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000876 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000877 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000878 Params.reserve(NumParams);
879 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000880 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000881 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000882}
883
Sebastian Redlb3298c32010-08-18 23:56:48 +0000884void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000885 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000886 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000887 // Load the body on-demand. Most clients won't care, because method
888 // definitions rarely show up in headers.
889 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
890 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000891 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
892 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000893 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000894 MD->setInstanceMethod(Record.readInt());
895 MD->setVariadic(Record.readInt());
896 MD->setPropertyAccessor(Record.readInt());
897 MD->setDefined(Record.readInt());
898 MD->IsOverriding = Record.readInt();
899 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000900
David L. Jonesbe1557a2016-12-21 00:17:49 +0000901 MD->IsRedeclaration = Record.readInt();
902 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000903 if (MD->HasRedeclaration)
904 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000905 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000906
David L. Jonesbe1557a2016-12-21 00:17:49 +0000907 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
908 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
909 MD->SetRelatedResultType(Record.readInt());
910 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000911 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
912 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000913 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000914 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000915 Params.reserve(NumParams);
916 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000917 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000918
David L. Jonesbe1557a2016-12-21 00:17:49 +0000919 MD->SelLocsKind = Record.readInt();
920 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000921 SmallVector<SourceLocation, 16> SelLocs;
922 SelLocs.reserve(NumStoredSelLocs);
923 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000924 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000925
926 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000927}
928
Douglas Gregor85f3f952015-07-07 03:57:15 +0000929void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000930 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000931
David L. Jonesbe1557a2016-12-21 00:17:49 +0000932 D->Variance = Record.readInt();
933 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000934 D->VarianceLoc = ReadSourceLocation();
935 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000936}
937
Sebastian Redlb3298c32010-08-18 23:56:48 +0000938void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000939 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000940 CD->setAtStartLoc(ReadSourceLocation());
941 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000942}
943
Douglas Gregor85f3f952015-07-07 03:57:15 +0000944ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000945 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000946 if (numParams == 0)
947 return nullptr;
948
949 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
950 typeParams.reserve(numParams);
951 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000952 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000953 if (!typeParam)
954 return nullptr;
955
956 typeParams.push_back(typeParam);
957 }
958
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000959 SourceLocation lAngleLoc = ReadSourceLocation();
960 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000961
962 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
963 typeParams, rAngleLoc);
964}
965
Manman Renec315f12016-09-09 23:48:27 +0000966void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000967 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000968 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000969 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000970
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000971 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000972 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000973
Manman Renec315f12016-09-09 23:48:27 +0000974 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000975 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000976 SmallVector<ObjCProtocolDecl *, 16> Protocols;
977 Protocols.reserve(NumProtocols);
978 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000979 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000980 SmallVector<SourceLocation, 16> ProtoLocs;
981 ProtoLocs.reserve(NumProtocols);
982 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000983 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +0000984 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
985 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000986
Manman Renec315f12016-09-09 23:48:27 +0000987 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000988 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000989 Protocols.clear();
990 Protocols.reserve(NumProtocols);
991 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000992 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000993 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
994 Reader.getContext());
995}
996
997void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
998 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
999 // FIXME: odr checking?
1000}
1001
Sebastian Redlb3298c32010-08-18 23:56:48 +00001002void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001003 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001004 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001005 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001006 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001007
1008 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001009 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001010 // Read the definition.
1011 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001012
David L. Jonesbe1557a2016-12-21 00:17:49 +00001013 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001014 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1015 if (Canon->Data.getPointer()) {
1016 // If we already have a definition, keep the definition invariant and
1017 // merge the data.
1018 MergeDefinitionData(Canon, std::move(ID->data()));
1019 ID->Data = Canon->Data;
1020 } else {
1021 // Set the definition data of the canonical declaration, so other
1022 // redeclarations will see it.
1023 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001024
Manman Renec315f12016-09-09 23:48:27 +00001025 // We will rebuild this list lazily.
1026 ID->setIvarList(nullptr);
1027 }
Craig Toppera13603a2014-05-22 05:54:18 +00001028
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001029 // Note that we have deserialized a definition.
1030 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001031
Douglas Gregor404cdde2012-01-27 01:47:08 +00001032 // Note that we've loaded this Objective-C class.
1033 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001034 } else {
1035 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001036 }
Chris Lattner487412d2009-04-27 05:27:42 +00001037}
1038
Sebastian Redlb3298c32010-08-18 23:56:48 +00001039void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001040 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001041 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001042 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001043 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001044 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001045 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001046}
1047
Sebastian Redlb3298c32010-08-18 23:56:48 +00001048void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
Douglas Gregorda389302012-01-01 21:47:52 +00001049 RedeclarableResult Redecl = VisitRedeclarable(PD);
Chris Lattner487412d2009-04-27 05:27:42 +00001050 VisitObjCContainerDecl(PD);
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001051 mergeRedeclarable(PD, Redecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001052
David L. Jonesbe1557a2016-12-21 00:17:49 +00001053 if (Record.readInt()) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00001054 // Read the definition.
1055 PD->allocateDefinitionData();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001056
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001057 // Set the definition data of the canonical declaration, so other
1058 // redeclarations will see it.
1059 PD->getCanonicalDecl()->Data = PD->Data;
1060
David L. Jonesbe1557a2016-12-21 00:17:49 +00001061 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001062 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1063 ProtoRefs.reserve(NumProtoRefs);
1064 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001065 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001066 SmallVector<SourceLocation, 16> ProtoLocs;
1067 ProtoLocs.reserve(NumProtoRefs);
1068 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001069 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001070 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1071 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001072
Douglas Gregora715bff2012-01-01 19:51:50 +00001073 // Note that we have deserialized a definition.
1074 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001075 } else {
1076 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001077 }
Chris Lattner487412d2009-04-27 05:27:42 +00001078}
1079
Sebastian Redlb3298c32010-08-18 23:56:48 +00001080void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001081 VisitFieldDecl(FD);
1082}
1083
Sebastian Redlb3298c32010-08-18 23:56:48 +00001084void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001085 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001086 CD->setCategoryNameLoc(ReadSourceLocation());
1087 CD->setIvarLBraceLoc(ReadSourceLocation());
1088 CD->setIvarRBraceLoc(ReadSourceLocation());
1089
Douglas Gregor404cdde2012-01-27 01:47:08 +00001090 // Note that this category has been deserialized. We do this before
1091 // deserializing the interface declaration, so that it will consider this
1092 /// category.
1093 Reader.CategoriesDeserialized.insert(CD);
1094
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001095 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001096 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001097 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001098 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001099 ProtoRefs.reserve(NumProtoRefs);
1100 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001101 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001102 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001103 ProtoLocs.reserve(NumProtoRefs);
1104 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001105 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001106 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001107 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001108}
1109
Sebastian Redlb3298c32010-08-18 23:56:48 +00001110void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001111 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001112 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001113}
1114
Sebastian Redlb3298c32010-08-18 23:56:48 +00001115void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001116 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001117 D->setAtLoc(ReadSourceLocation());
1118 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001119 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001120 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001121 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001122 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001123 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001124 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001125 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001126 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001127 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001128 DeclarationName GetterName = Record.readDeclarationName();
1129 SourceLocation GetterLoc = ReadSourceLocation();
1130 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1131 DeclarationName SetterName = Record.readDeclarationName();
1132 SourceLocation SetterLoc = ReadSourceLocation();
1133 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001134 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1135 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1136 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001137}
1138
Sebastian Redlb3298c32010-08-18 23:56:48 +00001139void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001140 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001141 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001142}
1143
Sebastian Redlb3298c32010-08-18 23:56:48 +00001144void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001145 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001146 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001147}
1148
Sebastian Redlb3298c32010-08-18 23:56:48 +00001149void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001150 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001151 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1152 D->SuperLoc = ReadSourceLocation();
1153 D->setIvarLBraceLoc(ReadSourceLocation());
1154 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001155 D->setHasNonZeroConstructors(Record.readInt());
1156 D->setHasDestructors(Record.readInt());
1157 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001158 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001159 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001160}
1161
Sebastian Redlb3298c32010-08-18 23:56:48 +00001162void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001163 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001164 D->setAtLoc(ReadSourceLocation());
1165 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1166 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1167 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001168 D->setGetterCXXConstructor(Record.readExpr());
1169 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001170}
1171
Sebastian Redlb3298c32010-08-18 23:56:48 +00001172void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001173 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001174 FD->Mutable = Record.readInt();
1175 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001176 FD->InitStorage.setInt(
1177 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1178 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001179 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001180 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001181 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001182 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001183 }
Richard Smith2b013182012-06-10 03:12:00 +00001184 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001185 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001186 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001187 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001188 }
Richard Smith0b87e072013-10-07 08:02:11 +00001189 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001190}
1191
John McCall5e77d762013-04-16 07:28:30 +00001192void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1193 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001194 PD->GetterId = Record.getIdentifierInfo();
1195 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001196}
1197
Francois Pichet783dd6e2010-11-21 06:08:52 +00001198void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1199 VisitValueDecl(FD);
1200
David L. Jonesbe1557a2016-12-21 00:17:49 +00001201 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001202 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001203 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001204
1205 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001206 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001207
1208 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001209}
1210
Larisse Voufo39a1e502013-08-06 01:03:05 +00001211ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001212 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001213 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001214
David L. Jonesbe1557a2016-12-21 00:17:49 +00001215 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1216 VD->VarDeclBits.TSCSpec = Record.readInt();
1217 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001218 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001219 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1220 Record.readInt();
1221 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1222 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1223 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1224 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1225 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1226 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1227 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1228 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1229 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001230 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001231 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001232 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001233 VD->setCachedLinkage(VarLinkage);
1234
1235 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001236 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001237 VD->getLexicalDeclContext()->isFunctionOrMethod())
1238 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001239
David L. Jonesbe1557a2016-12-21 00:17:49 +00001240 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001241 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001242 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001243 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1244 Eval->CheckedICE = true;
1245 Eval->IsICE = Val == 3;
1246 }
1247 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001248
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001249 enum VarKind {
1250 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1251 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001252 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001253 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001254 // Only true variables (not parameters or implicit parameters) can be
1255 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001256 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1257 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001258 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001259 break;
1260 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001261 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001262 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001263 break;
1264 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001265 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001266 TemplateSpecializationKind TSK =
1267 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001268 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001269 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001270 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001271 break;
1272 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001273 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001274
1275 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001276}
1277
Sebastian Redlb3298c32010-08-18 23:56:48 +00001278void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001279 VisitVarDecl(PD);
1280}
1281
Sebastian Redlb3298c32010-08-18 23:56:48 +00001282void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001283 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001284 unsigned isObjCMethodParam = Record.readInt();
1285 unsigned scopeDepth = Record.readInt();
1286 unsigned scopeIndex = Record.readInt();
1287 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001288 if (isObjCMethodParam) {
1289 assert(scopeDepth == 0);
1290 PD->setObjCMethodScopeInfo(scopeIndex);
1291 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1292 } else {
1293 PD->setScopeInfo(scopeDepth, scopeIndex);
1294 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001295 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1296 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1297 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001298 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001299
1300 // FIXME: If this is a redeclaration of a function from another module, handle
1301 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001302}
1303
Richard Smith7b76d812016-08-12 02:21:25 +00001304void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1305 VisitVarDecl(DD);
1306 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1307 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001308 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001309}
1310
1311void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1312 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001313 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001314}
1315
Sebastian Redlb3298c32010-08-18 23:56:48 +00001316void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001317 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001318 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001319 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001320}
1321
Sebastian Redlb3298c32010-08-18 23:56:48 +00001322void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001323 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001324 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001325 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001326 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001327 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001328 Params.reserve(NumParams);
1329 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001330 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001331 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001332
David L. Jonesbe1557a2016-12-21 00:17:49 +00001333 BD->setIsVariadic(Record.readInt());
1334 BD->setBlockMissingReturnType(Record.readInt());
1335 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001336
David L. Jonesbe1557a2016-12-21 00:17:49 +00001337 bool capturesCXXThis = Record.readInt();
1338 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001339 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001340 captures.reserve(numCaptures);
1341 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001342 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001343 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001344 bool byRef = (flags & 1);
1345 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001346 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001347
1348 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1349 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001350 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001351}
1352
Ben Langmuirce914fc2013-05-03 19:20:19 +00001353void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1354 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001355 unsigned ContextParamPos = Record.readInt();
1356 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001357 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001358 for (unsigned I = 0; I < CD->NumParams; ++I) {
1359 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001360 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001361 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001362 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001363 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001364}
1365
Sebastian Redlb3298c32010-08-18 23:56:48 +00001366void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001367 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001368 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001369 D->setExternLoc(ReadSourceLocation());
1370 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001371}
1372
Richard Smith8df390f2016-09-08 23:14:54 +00001373void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1374 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001375 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001376}
1377
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001378void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1379 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001380 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001381}
1382
Sebastian Redlb3298c32010-08-18 23:56:48 +00001383void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001384 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001385 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001386 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001387 D->LocStart = ReadSourceLocation();
1388 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001389
Richard Smith15e32fd2015-03-17 02:23:11 +00001390 // Defer loading the anonymous namespace until we've finished merging
1391 // this namespace; loading it might load a later declaration of the
1392 // same namespace, and we have an invariant that older declarations
1393 // get merged before newer ones try to merge.
1394 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001395 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001396 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001397 } else {
1398 // Link this namespace back to the first declaration, which has already
1399 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001400 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001401 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001402
1403 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001404
1405 if (AnonNamespace) {
1406 // Each module has its own anonymous namespace, which is disjoint from
1407 // any other module's anonymous namespaces, so don't attach the anonymous
1408 // namespace at all.
1409 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001410 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001411 D->setAnonymousNamespace(Anon);
1412 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001413}
1414
Sebastian Redlb3298c32010-08-18 23:56:48 +00001415void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001416 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001417 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001418 D->NamespaceLoc = ReadSourceLocation();
1419 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001420 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001421 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001422 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001423}
1424
Sebastian Redlb3298c32010-08-18 23:56:48 +00001425void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001426 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001427 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001428 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001429 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1430 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001431 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001432 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001433 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001434 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001435}
1436
Richard Smith151c4562016-12-20 21:35:28 +00001437void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1438 VisitNamedDecl(D);
1439 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1440 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1441 for (unsigned I = 0; I != D->NumExpansions; ++I)
1442 Expansions[I] = ReadDeclAs<NamedDecl>();
1443 mergeMergeable(D);
1444}
1445
Sebastian Redlb3298c32010-08-18 23:56:48 +00001446void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001447 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001448 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001449 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1450 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1451 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001452 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001453 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001454 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001455}
1456
Richard Smith5179eb72016-06-28 19:03:57 +00001457void ASTDeclReader::VisitConstructorUsingShadowDecl(
1458 ConstructorUsingShadowDecl *D) {
1459 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001460 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1461 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001462 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001463}
1464
Sebastian Redlb3298c32010-08-18 23:56:48 +00001465void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001466 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001467 D->UsingLoc = ReadSourceLocation();
1468 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001469 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001470 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1471 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001472}
1473
Sebastian Redlb3298c32010-08-18 23:56:48 +00001474void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001475 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001476 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001477 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001478 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001479 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001480 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001481}
1482
Sebastian Redlb3298c32010-08-18 23:56:48 +00001483void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001484 UnresolvedUsingTypenameDecl *D) {
1485 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001486 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001487 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001488 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001489 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001490}
1491
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001492void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001493 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001494 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001495 Data.UserDeclaredConstructor = Record.readInt();
1496 Data.UserDeclaredSpecialMembers = Record.readInt();
1497 Data.Aggregate = Record.readInt();
1498 Data.PlainOldData = Record.readInt();
1499 Data.Empty = Record.readInt();
1500 Data.Polymorphic = Record.readInt();
1501 Data.Abstract = Record.readInt();
1502 Data.IsStandardLayout = Record.readInt();
1503 Data.HasNoNonEmptyBases = Record.readInt();
1504 Data.HasPrivateFields = Record.readInt();
1505 Data.HasProtectedFields = Record.readInt();
1506 Data.HasPublicFields = Record.readInt();
1507 Data.HasMutableFields = Record.readInt();
1508 Data.HasVariantMembers = Record.readInt();
1509 Data.HasOnlyCMembers = Record.readInt();
1510 Data.HasInClassInitializer = Record.readInt();
1511 Data.HasUninitializedReferenceMember = Record.readInt();
1512 Data.HasUninitializedFields = Record.readInt();
1513 Data.HasInheritedConstructor = Record.readInt();
1514 Data.HasInheritedAssignment = Record.readInt();
1515 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1516 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1517 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1518 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1519 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1520 Data.DefaultedDestructorIsDeleted = Record.readInt();
1521 Data.HasTrivialSpecialMembers = Record.readInt();
1522 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1523 Data.HasIrrelevantDestructor = Record.readInt();
1524 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1525 Data.HasDefaultedDefaultConstructor = Record.readInt();
1526 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1527 Data.HasConstexprDefaultConstructor = Record.readInt();
1528 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1529 Data.ComputedVisibleConversions = Record.readInt();
1530 Data.UserProvidedDefaultConstructor = Record.readInt();
1531 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001532 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1533 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001534 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1535 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1536 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001537 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001538 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001539
David Blaikie1ac9c982017-04-11 21:13:37 +00001540 if (Record.readInt()) {
1541 Reader.BodySource[D] = Loc.F->Kind == ModuleKind::MK_MainFile
1542 ? ExternalASTSource::EK_Never
1543 : ExternalASTSource::EK_Always;
1544 }
1545
David L. Jonesbe1557a2016-12-21 00:17:49 +00001546 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001547 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001548 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001549 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001550 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001551 Data.VBases = ReadGlobalOffset();
1552
David L. Jonesb6a8f022016-12-21 04:34:52 +00001553 Record.readUnresolvedSet(Data.Conversions);
1554 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001555 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001556 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001557
Douglas Gregor99ae8062012-02-14 17:54:36 +00001558 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001559 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001560 CXXRecordDecl::LambdaDefinitionData &Lambda
1561 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001562 Lambda.Dependent = Record.readInt();
1563 Lambda.IsGenericLambda = Record.readInt();
1564 Lambda.CaptureDefault = Record.readInt();
1565 Lambda.NumCaptures = Record.readInt();
1566 Lambda.NumExplicitCaptures = Record.readInt();
1567 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001568 Lambda.ContextDecl = ReadDeclID();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001569 Lambda.Captures
1570 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1571 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001572 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001573 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001574 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001575 bool IsImplicit = Record.readInt();
1576 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001577 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001578 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001579 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001580 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001581 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001582 break;
1583 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001584 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001585 VarDecl *Var = ReadDeclAs<VarDecl>();
1586 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001587 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1588 break;
1589 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001590 }
1591 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001592}
1593
Richard Smithcd45dbc2014-04-19 03:48:30 +00001594void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001595 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001596 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001597 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001598 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001599
Richard Smith02793752015-03-27 21:16:39 +00001600 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001601 // Track that we merged the definitions.
1602 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1603 DD.Definition));
1604 Reader.PendingDefinitions.erase(MergeDD.Definition);
1605 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001606 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001607 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1608 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001609 }
1610
Richard Smith2a9e5c52015-02-03 03:32:14 +00001611 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1612 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1613 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001614 // We faked up this definition data because we found a class for which we'd
1615 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001616 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001617 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001618
1619 // Don't change which declaration is the definition; that is required
1620 // to be invariant once we select it.
1621 auto *Def = DD.Definition;
1622 DD = std::move(MergeDD);
1623 DD.Definition = Def;
1624 return;
1625 }
1626
Richard Smithcd45dbc2014-04-19 03:48:30 +00001627 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001628 bool DetectedOdrViolation = false;
1629#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1630#define MATCH_FIELD(Field) \
1631 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1632 OR_FIELD(Field)
1633 MATCH_FIELD(UserDeclaredConstructor)
1634 MATCH_FIELD(UserDeclaredSpecialMembers)
1635 MATCH_FIELD(Aggregate)
1636 MATCH_FIELD(PlainOldData)
1637 MATCH_FIELD(Empty)
1638 MATCH_FIELD(Polymorphic)
1639 MATCH_FIELD(Abstract)
1640 MATCH_FIELD(IsStandardLayout)
1641 MATCH_FIELD(HasNoNonEmptyBases)
1642 MATCH_FIELD(HasPrivateFields)
1643 MATCH_FIELD(HasProtectedFields)
1644 MATCH_FIELD(HasPublicFields)
1645 MATCH_FIELD(HasMutableFields)
1646 MATCH_FIELD(HasVariantMembers)
1647 MATCH_FIELD(HasOnlyCMembers)
1648 MATCH_FIELD(HasInClassInitializer)
1649 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001650 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001651 MATCH_FIELD(HasInheritedConstructor)
1652 MATCH_FIELD(HasInheritedAssignment)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001653 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1654 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1655 MATCH_FIELD(NeedOverloadResolutionForDestructor)
1656 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1657 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1658 MATCH_FIELD(DefaultedDestructorIsDeleted)
1659 OR_FIELD(HasTrivialSpecialMembers)
1660 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1661 MATCH_FIELD(HasIrrelevantDestructor)
1662 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001663 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001664 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1665 OR_FIELD(HasConstexprDefaultConstructor)
1666 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1667 // ComputedVisibleConversions is handled below.
1668 MATCH_FIELD(UserProvidedDefaultConstructor)
1669 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001670 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1671 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001672 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1673 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1674 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1675 MATCH_FIELD(IsLambda)
1676#undef OR_FIELD
1677#undef MATCH_FIELD
1678
1679 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1680 DetectedOdrViolation = true;
1681 // FIXME: Issue a diagnostic if the base classes don't match when we come
1682 // to lazily load them.
1683
1684 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1685 // match when we come to lazily load them.
1686 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1687 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1688 DD.ComputedVisibleConversions = true;
1689 }
1690
1691 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1692 // lazily load it.
1693
1694 if (DD.IsLambda) {
1695 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1696 // when they occur within the body of a function template specialization).
1697 }
1698
Richard Trieufd1acbb2017-04-11 21:31:00 +00001699 if (D->getODRHash() != MergeDD.ODRHash) {
1700 DetectedOdrViolation = true;
1701 }
1702
Richard Smithcd45dbc2014-04-19 03:48:30 +00001703 if (DetectedOdrViolation)
1704 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1705}
1706
Richard Smith2a9e5c52015-02-03 03:32:14 +00001707void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001708 struct CXXRecordDecl::DefinitionData *DD;
1709 ASTContext &C = Reader.getContext();
1710
1711 // Determine whether this is a lambda closure type, so that we can
1712 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001713 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001714 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001715 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001716 LCD_None);
1717 else
1718 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1719
David Blaikie1ac9c982017-04-11 21:13:37 +00001720 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001721
Richard Smith5156c382015-01-22 01:41:56 +00001722 // We might already have a definition for this record. This can happen either
1723 // because we're reading an update record, or because we've already done some
1724 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001725 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001726 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001727 MergeDefinitionData(Canon, std::move(*DD));
1728 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001729 return;
1730 }
1731
Richard Smith97100e32015-06-20 00:22:34 +00001732 // Mark this declaration as being a definition.
1733 D->IsCompleteDefinition = true;
1734 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001735
Richard Smith97100e32015-06-20 00:22:34 +00001736 // If this is not the first declaration or is an update record, we can have
1737 // other redeclarations already. Make a note that we need to propagate the
1738 // DefinitionData pointer onto them.
1739 if (Update || Canon != D) {
1740 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001741 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001742 }
1743}
1744
Richard Smith1d209d02013-05-23 01:49:11 +00001745ASTDeclReader::RedeclarableResult
1746ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1747 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001748
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001749 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001750
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001751 enum CXXRecKind {
1752 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1753 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001754 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001755 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001756 // Merged when we merge the folding set entry in the primary template.
1757 if (!isa<ClassTemplateSpecializationDecl>(D))
1758 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001759 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001760 case CXXRecTemplate: {
1761 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001762 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001763 D->TemplateOrInstantiation = Template;
1764 if (!Template->getTemplatedDecl()) {
1765 // We've not actually loaded the ClassTemplateDecl yet, because we're
1766 // currently being loaded as its pattern. Rely on it to set up our
1767 // TypeForDecl (see VisitClassTemplateDecl).
1768 //
1769 // Beware: we do not yet know our canonical declaration, and may still
1770 // get merged once the surrounding class template has got off the ground.
1771 TypeIDForTypeDecl = 0;
1772 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001773 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001774 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001775 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001776 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001777 TemplateSpecializationKind TSK =
1778 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001779 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001780 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1781 MSI->setPointOfInstantiation(POI);
1782 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001783 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001784 break;
1785 }
1786 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001787
David L. Jonesbe1557a2016-12-21 00:17:49 +00001788 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001789 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001790 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001791 else
1792 // Propagate DefinitionData pointer from the canonical declaration.
1793 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1794
Richard Smith676c4042013-08-29 23:59:27 +00001795 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001796 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001797 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001798 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001799 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001800 // FIXME: This is wrong for the ARM ABI, where some other module may have
1801 // made this function no longer be a key function. We need an update
1802 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001803 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001804 }
Richard Smith1d209d02013-05-23 01:49:11 +00001805
1806 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001807}
1808
Richard Smithbc491202017-02-17 20:05:37 +00001809void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1810 VisitFunctionDecl(D);
1811}
1812
Sebastian Redlb3298c32010-08-18 23:56:48 +00001813void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001814 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001815
David L. Jonesbe1557a2016-12-21 00:17:49 +00001816 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001817 if (D->isCanonicalDecl()) {
1818 while (NumOverridenMethods--) {
1819 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1820 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001821 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001822 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1823 }
1824 } else {
1825 // We don't care about which declarations this used to override; we get
1826 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001827 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001828 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001829}
1830
Sebastian Redlb3298c32010-08-18 23:56:48 +00001831void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001832 // We need the inherited constructor information to merge the declaration,
1833 // so we have to read it before we call VisitCXXMethodDecl.
1834 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001835 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1836 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001837 *D->getTrailingObjects<InheritedConstructor>() =
1838 InheritedConstructor(Shadow, Ctor);
1839 }
1840
Chris Lattnerca025db2010-05-07 21:43:38 +00001841 VisitCXXMethodDecl(D);
1842}
1843
Sebastian Redlb3298c32010-08-18 23:56:48 +00001844void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001845 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001846
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001847 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001848 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1849 // FIXME: Check consistency if we have an old and new operator delete.
1850 if (!Canon->OperatorDelete)
1851 Canon->OperatorDelete = OperatorDelete;
1852 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001853}
1854
Sebastian Redlb3298c32010-08-18 23:56:48 +00001855void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001856 VisitCXXMethodDecl(D);
1857}
1858
Douglas Gregorba345522011-12-02 23:23:56 +00001859void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1860 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001861 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001862 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001863 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001864 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001865 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001866 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001867}
1868
Sebastian Redlb3298c32010-08-18 23:56:48 +00001869void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001870 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001871 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001872}
1873
Sebastian Redlb3298c32010-08-18 23:56:48 +00001874void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001875 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001876 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001877 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001878 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001879 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001880 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001881 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001882 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001883 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001884 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001885 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001886}
1887
Sebastian Redlb3298c32010-08-18 23:56:48 +00001888void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001889 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001890 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001891 D->NumParams = NumParams;
1892 D->Params = new TemplateParameterList*[NumParams];
1893 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001894 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001895 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001896 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001897 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001898 D->Friend = GetTypeSourceInfo();
1899 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001900}
1901
Richard Smithf17fdbd2014-04-24 02:25:27 +00001902DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001903 VisitNamedDecl(D);
1904
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001905 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001906 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001907 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001908 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001909 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001910
Richard Smithf17fdbd2014-04-24 02:25:27 +00001911 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001912}
1913
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001914ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001915ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001916 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001917
Douglas Gregor68444de2012-01-14 15:13:49 +00001918 // Make sure we've allocated the Common pointer first. We do this before
1919 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1920 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1921 if (!CanonD->Common) {
1922 CanonD->Common = CanonD->newCommon(Reader.getContext());
1923 Reader.PendingDefinitions.insert(CanonD);
1924 }
1925 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001926
Douglas Gregor68444de2012-01-14 15:13:49 +00001927 // If this is the first declaration of the template, fill in the information
1928 // for the 'common' pointer.
1929 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001930 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001931 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001932 assert(RTD->getKind() == D->getKind() &&
1933 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001934 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001935 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001936 D->setMemberSpecialization();
1937 }
1938 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001939
Richard Smithf17fdbd2014-04-24 02:25:27 +00001940 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001941 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001942
Richard Smithf17fdbd2014-04-24 02:25:27 +00001943 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001944
Richard Smithd46d6de2013-10-13 23:50:45 +00001945 // If we merged the template with a prior declaration chain, merge the common
1946 // pointer.
1947 // FIXME: Actually merge here, don't just overwrite.
1948 D->Common = D->getCanonicalDecl()->Common;
1949
Douglas Gregor68444de2012-01-14 15:13:49 +00001950 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001951}
1952
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00001953static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1954 SmallVectorImpl<DeclID> &IDs) {
1955 assert(!IDs.empty() && "no IDs to add to list");
1956 if (Old) {
1957 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1958 std::sort(IDs.begin(), IDs.end());
1959 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1960 }
1961
1962 auto *Result = new (Context) DeclID[1 + IDs.size()];
1963 *Result = IDs.size();
1964 std::copy(IDs.begin(), IDs.end(), Result + 1);
1965 return Result;
1966}
1967
Sebastian Redlb3298c32010-08-18 23:56:48 +00001968void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001969 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001970
Douglas Gregor68444de2012-01-14 15:13:49 +00001971 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001972 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1973 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001974 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001975 ReadDeclIDList(SpecIDs);
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00001976
1977 if (!SpecIDs.empty()) {
1978 auto *CommonPtr = D->getCommonPtr();
1979 CommonPtr->LazySpecializations = newDeclIDList(
1980 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
1981 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001982 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00001983
1984 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1985 // We were loaded before our templated declaration was. We've not set up
1986 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1987 // it now.
1988 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00001989 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00001990 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001991}
1992
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001993void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
1994 llvm_unreachable("BuiltinTemplates are not serialized");
1995}
1996
Larisse Voufo30616382013-08-23 22:21:36 +00001997/// TODO: Unify with ClassTemplateDecl version?
1998/// May require unifying ClassTemplateDecl and
1999/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002000void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2001 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2002
2003 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002004 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002005 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002006 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002007 ReadDeclIDList(SpecIDs);
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00002008
2009 if (!SpecIDs.empty()) {
2010 auto *CommonPtr = D->getCommonPtr();
2011 CommonPtr->LazySpecializations = newDeclIDList(
2012 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
2013 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00002014 }
2015}
2016
Richard Smith1d209d02013-05-23 01:49:11 +00002017ASTDeclReader::RedeclarableResult
2018ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2019 ClassTemplateSpecializationDecl *D) {
2020 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002021
Douglas Gregor4163aca2011-09-09 21:34:22 +00002022 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002023 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002024 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002025 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002026 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002027 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002028 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002029 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002030 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002031 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2032 = new (C) ClassTemplateSpecializationDecl::
2033 SpecializedPartialSpecialization();
2034 PS->PartialSpecialization
2035 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2036 PS->TemplateArgs = ArgList;
2037 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002038 }
2039 }
2040
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002041 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002042 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002043 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002044 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002045 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002046
David L. Jonesbe1557a2016-12-21 00:17:49 +00002047 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002048 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002049 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002050 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002051 // Set this as, or find, the canonical declaration for this specialization
2052 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002053 if (ClassTemplatePartialSpecializationDecl *Partial =
2054 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002055 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002056 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002057 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002058 CanonSpec =
2059 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2060 }
2061 // If there was already a canonical specialization, merge into it.
2062 if (CanonSpec != D) {
2063 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2064
2065 // This declaration might be a definition. Merge with any existing
2066 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002067 if (auto *DDD = D->DefinitionData) {
2068 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002069 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002070 else
Richard Smith053f6c62014-05-16 23:01:30 +00002071 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002072 }
Richard Smith547864d2014-07-11 18:22:58 +00002073 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002074 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002075 }
2076 }
Richard Smith1d209d02013-05-23 01:49:11 +00002077
Richard Smithd55889a2013-09-09 16:55:27 +00002078 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002079 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002080 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2081 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2082 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002083 ExplicitInfo->ExternLoc = ReadSourceLocation();
2084 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002085 D->ExplicitInfo = ExplicitInfo;
2086 }
2087
Richard Smith1d209d02013-05-23 01:49:11 +00002088 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002089}
2090
Sebastian Redlb3298c32010-08-18 23:56:48 +00002091void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002092 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002093 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002094
David L. Jonesb6a8f022016-12-21 04:34:52 +00002095 D->TemplateParams = Record.readTemplateParameterList();
2096 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002097
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002098 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002099 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002100 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002101 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002102 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002103 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002104}
2105
Sebastian Redlb7448632011-08-31 13:59:56 +00002106void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2107 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002108 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002109 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002110}
2111
Sebastian Redlb3298c32010-08-18 23:56:48 +00002112void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002113 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002114
Douglas Gregor68444de2012-01-14 15:13:49 +00002115 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002116 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002117 SmallVector<serialization::DeclID, 32> SpecIDs;
2118 ReadDeclIDList(SpecIDs);
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00002119
2120 if (!SpecIDs.empty()) {
2121 auto *CommonPtr = D->getCommonPtr();
2122 CommonPtr->LazySpecializations = newDeclIDList(
2123 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
2124 }
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002125 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002126}
2127
Larisse Voufo30616382013-08-23 22:21:36 +00002128/// TODO: Unify with ClassTemplateSpecializationDecl version?
2129/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2130/// VarTemplate(Partial)SpecializationDecl with a new data
2131/// structure Template(Partial)SpecializationDecl, and
2132/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002133ASTDeclReader::RedeclarableResult
2134ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2135 VarTemplateSpecializationDecl *D) {
2136 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2137
2138 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002139 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002140 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2141 D->SpecializedTemplate = VTD;
2142 } else {
2143 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002144 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002145 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002146 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002147 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2148 new (C)
2149 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2150 PS->PartialSpecialization =
2151 cast<VarTemplatePartialSpecializationDecl>(InstD);
2152 PS->TemplateArgs = ArgList;
2153 D->SpecializedTemplate = PS;
2154 }
2155 }
2156
2157 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002158 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002159 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2160 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2161 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002162 ExplicitInfo->ExternLoc = ReadSourceLocation();
2163 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002164 D->ExplicitInfo = ExplicitInfo;
2165 }
2166
2167 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002168 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002169 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002170 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002171 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002172
David L. Jonesbe1557a2016-12-21 00:17:49 +00002173 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002174 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002175 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002176 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002177 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002178 if (VarTemplatePartialSpecializationDecl *Partial =
2179 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002180 CanonPattern->getCommonPtr()->PartialSpecializations
2181 .GetOrInsertNode(Partial);
2182 } else {
2183 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2184 }
2185 }
2186 }
2187
2188 return Redecl;
2189}
2190
Larisse Voufo30616382013-08-23 22:21:36 +00002191/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2192/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2193/// VarTemplate(Partial)SpecializationDecl with a new data
2194/// structure Template(Partial)SpecializationDecl, and
2195/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002196void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2197 VarTemplatePartialSpecializationDecl *D) {
2198 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2199
David L. Jonesb6a8f022016-12-21 04:34:52 +00002200 D->TemplateParams = Record.readTemplateParameterList();
2201 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002202
2203 // These are read/set from/to the first declaration.
2204 if (ThisDeclID == Redecl.getFirstID()) {
2205 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002206 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002207 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002208 }
2209}
2210
Sebastian Redlb3298c32010-08-18 23:56:48 +00002211void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002212 VisitTypeDecl(D);
2213
David L. Jonesbe1557a2016-12-21 00:17:49 +00002214 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002215
David L. Jonesbe1557a2016-12-21 00:17:49 +00002216 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002217 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002218}
2219
Sebastian Redlb3298c32010-08-18 23:56:48 +00002220void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002221 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002222 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002223 D->setDepth(Record.readInt());
2224 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002225 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002226 auto TypesAndInfos =
2227 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002228 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002229 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002230 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002231 }
2232 } else {
2233 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002234 D->ParameterPack = Record.readInt();
2235 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002236 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002237 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002238}
2239
Sebastian Redlb3298c32010-08-18 23:56:48 +00002240void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002241 VisitTemplateDecl(D);
2242 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002243 D->setDepth(Record.readInt());
2244 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002245 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002246 TemplateParameterList **Data =
2247 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002248 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2249 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002250 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002251 } else {
2252 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002253 D->ParameterPack = Record.readInt();
2254 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002255 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002256 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002257 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002258}
2259
Richard Smith3f1b5d02011-05-05 21:57:07 +00002260void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2261 VisitRedeclarableTemplateDecl(D);
2262}
2263
Sebastian Redlb3298c32010-08-18 23:56:48 +00002264void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002265 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002266 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002267 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002268 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002269 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002270}
2271
Michael Han84324352013-02-22 17:15:32 +00002272void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2273 VisitDecl(D);
2274}
2275
Mike Stump11289f42009-09-09 15:08:12 +00002276std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002277ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002278 uint64_t LexicalOffset = ReadLocalOffset();
2279 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002280 return std::make_pair(LexicalOffset, VisibleOffset);
2281}
2282
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002283template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002284ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002285ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002286 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002287 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002288
Richard Smith5fc18a92015-07-12 23:43:21 +00002289 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002290 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002291
Richard Smithd61d4ac2015-08-22 20:13:39 +00002292 uint64_t RedeclOffset = 0;
2293
Douglas Gregor358cd442012-01-15 16:58:34 +00002294 // 0 indicates that this declaration was the only declaration of its entity,
2295 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002296 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002297 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002298 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002299 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002300 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002301 // This declaration was the first local declaration, but may have imported
2302 // other declarations.
2303 IsKeyDecl = N == 1;
2304 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002305
Richard Smithfe620d22015-03-05 23:24:12 +00002306 // We have some declarations that must be before us in our redeclaration
2307 // chain. Read them now, and remember that we ought to merge with one of
2308 // them.
2309 // FIXME: Provide a known merge target to the second and subsequent such
2310 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002311 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002312 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002313
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002314 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002315 } else {
2316 // This declaration was not the first local declaration. Read the first
2317 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002318 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002319 }
2320
Douglas Gregor358cd442012-01-15 16:58:34 +00002321 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2322 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002323 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2324 // We temporarily set the first (canonical) declaration as the previous one
2325 // which is the one that matters and mark the real previous DeclID to be
2326 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002327 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002328 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002329 }
Richard Smithd8a83712015-08-22 01:47:18 +00002330
Richard Smithd8a83712015-08-22 01:47:18 +00002331 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002332
2333 // Note that we need to load local redeclarations of this decl and build a
2334 // decl chain for them. This must happen *after* we perform the preloading
2335 // above; this ensures that the redeclaration chain is built in the correct
2336 // order.
2337 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002338 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002339
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002340 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002341}
2342
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002343/// \brief Attempts to merge the given declaration (D) with another declaration
2344/// of the same entity.
2345template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002346void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002347 RedeclarableResult &Redecl,
2348 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002349 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002350 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002351 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002352
Richard Smith202850a2015-03-10 02:57:50 +00002353 // If we're not the canonical declaration, we don't need to merge.
2354 if (!DBase->isFirstDecl())
2355 return;
2356
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002357 T *D = static_cast<T*>(DBase);
2358
Richard Smith5a4737c2015-02-06 02:42:59 +00002359 if (auto *Existing = Redecl.getKnownMergeTarget())
2360 // We already know of an existing declaration we should merge with.
2361 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2362 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002363 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002364 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2365}
2366
2367/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2368/// We use this to put code in a template that will only be valid for certain
2369/// instantiations.
2370template<typename T> static T assert_cast(T t) { return t; }
2371template<typename T> static T assert_cast(...) {
2372 llvm_unreachable("bad assert_cast");
2373}
2374
2375/// \brief Merge together the pattern declarations from two template
2376/// declarations.
2377void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2378 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002379 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002380 auto *DPattern = D->getTemplatedDecl();
2381 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002382 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2383 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002384 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002385
Richard Smith547864d2014-07-11 18:22:58 +00002386 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2387 // Merge with any existing definition.
2388 // FIXME: This is duplicated in several places. Refactor.
2389 auto *ExistingClass =
2390 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002391 if (auto *DDD = DClass->DefinitionData) {
2392 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002393 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002394 } else {
2395 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002396 // We may have skipped this before because we thought that DClass
2397 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002398 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002399 }
2400 }
2401 DClass->DefinitionData = ExistingClass->DefinitionData;
2402
Richard Smithf17fdbd2014-04-24 02:25:27 +00002403 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2404 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002405 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002406 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2407 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2408 Result);
2409 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2410 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002411 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2412 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2413 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002414 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002415}
2416
2417/// \brief Attempts to merge the given declaration (D) with another declaration
2418/// of the same entity.
2419template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002420void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2421 RedeclarableResult &Redecl,
2422 DeclID TemplatePatternID) {
2423 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002424 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002425 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002426 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002427 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2428 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002429
Richard Smithd55889a2013-09-09 16:55:27 +00002430 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002431 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002432 // appropriate canonical declaration.
2433 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002434 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002435 ExistingCanon->Used |= D->Used;
2436 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002437
2438 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002439 // We cannot have loaded any redeclarations of this declaration yet, so
2440 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002441 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002442 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002443 assert_cast<NamespaceDecl*>(ExistingCanon));
2444
2445 // When we merge a template, merge its pattern.
2446 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2447 mergeTemplatePattern(
2448 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002449 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002450
Richard Smith5fc18a92015-07-12 23:43:21 +00002451 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002452 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002453 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002454 }
2455}
2456
Richard Smith0b87e072013-10-07 08:02:11 +00002457/// \brief Attempts to merge the given declaration (D) with another declaration
2458/// of the same entity, for the case where the entity is not actually
2459/// redeclarable. This happens, for instance, when merging the fields of
2460/// identical class definitions from two different modules.
2461template<typename T>
2462void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2463 // If modules are not available, there is no reason to perform this merge.
2464 if (!Reader.getContext().getLangOpts().Modules)
2465 return;
2466
Richard Smith01a73372013-10-15 22:02:41 +00002467 // ODR-based merging is only performed in C++. In C, identically-named things
2468 // in different translation units are not redeclarations (but may still have
2469 // compatible types).
2470 if (!Reader.getContext().getLangOpts().CPlusPlus)
2471 return;
2472
Richard Smith0b87e072013-10-07 08:02:11 +00002473 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2474 if (T *Existing = ExistingRes)
2475 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2476 Existing->getCanonicalDecl());
2477}
2478
Alexey Bataeva769e072013-03-22 06:34:35 +00002479void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2480 VisitDecl(D);
2481 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002482 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002483 Vars.reserve(NumVars);
2484 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002485 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002486 }
2487 D->setVars(Vars);
2488}
2489
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002490void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002491 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002492 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002493 D->setCombiner(Record.readExpr());
2494 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002495 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002496}
2497
Alexey Bataev4244be22016-02-11 05:35:55 +00002498void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002499 VisitVarDecl(D);
2500}
2501
Chris Lattner487412d2009-04-27 05:27:42 +00002502//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002503// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002504//===----------------------------------------------------------------------===//
2505
Chris Lattner8f63ab52009-04-27 06:01:06 +00002506/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002507void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2508 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002509 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002510 attr::Kind Kind = (attr::Kind)Record.readInt();
2511 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002512
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002513#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002514
2515 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002516 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002517 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002518}
2519
2520//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002521// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002522//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002523
2524/// \brief Note that we have loaded the declaration with the given
2525/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002526///
Chris Lattner487412d2009-04-27 05:27:42 +00002527/// This routine notes that this declaration has already been loaded,
2528/// so that future GetDecl calls will return this declaration rather
2529/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002530inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002531 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2532 DeclsLoaded[Index] = D;
2533}
2534
2535
2536/// \brief Determine whether the consumer will be interested in seeing
2537/// this declaration (via HandleTopLevelDecl).
2538///
2539/// This routine should return true for anything that might affect
2540/// code generation, e.g., inline function definitions, Objective-C
2541/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002542static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002543 // An ObjCMethodDecl is never considered as "interesting" because its
2544 // implementation container always is.
2545
Richard Smithdc1f0422016-07-20 19:10:16 +00002546 // An ImportDecl or VarDecl imported from a module will get emitted when
2547 // we import the relevant module.
Bruno Cardoso Lopes84df6e99b2017-03-01 19:18:42 +00002548 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && D->getImportedOwningModule() &&
2549 Ctx.DeclMustBeEmitted(D))
Richard Smithdc1f0422016-07-20 19:10:16 +00002550 return false;
2551
Douglas Gregor87d81242011-09-10 00:22:34 +00002552 if (isa<FileScopeAsmDecl>(D) ||
2553 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002554 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002555 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002556 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002557 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002558 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002559 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2560 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002561 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002562 return Var->isFileVarDecl() &&
2563 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002564 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002565 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002566
2567 if (auto *ES = D->getASTContext().getExternalSource())
2568 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2569 return true;
2570
Douglas Gregor87d81242011-09-10 00:22:34 +00002571 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002572}
2573
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002574/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002575ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002576ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002577 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2578 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002579 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002580 const DeclOffset &DOffs =
2581 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002582 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002583 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002584}
2585
Douglas Gregord32f0352011-07-22 06:10:01 +00002586ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002587 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002588 = GlobalBitOffsetsMap.find(GlobalOffset);
2589
2590 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2591 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2592}
2593
Douglas Gregorde3ef502011-11-30 23:21:26 +00002594uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002595 return LocalOffset + M.GlobalBitOffset;
2596}
2597
Richard Smithbf78e642013-06-24 22:51:00 +00002598static bool isSameTemplateParameterList(const TemplateParameterList *X,
2599 const TemplateParameterList *Y);
2600
2601/// \brief Determine whether two template parameters are similar enough
2602/// that they may be used in declarations of the same template.
2603static bool isSameTemplateParameter(const NamedDecl *X,
2604 const NamedDecl *Y) {
2605 if (X->getKind() != Y->getKind())
2606 return false;
2607
2608 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2609 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2610 return TX->isParameterPack() == TY->isParameterPack();
2611 }
2612
2613 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2614 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2615 return TX->isParameterPack() == TY->isParameterPack() &&
2616 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2617 }
2618
2619 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2620 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2621 return TX->isParameterPack() == TY->isParameterPack() &&
2622 isSameTemplateParameterList(TX->getTemplateParameters(),
2623 TY->getTemplateParameters());
2624}
2625
Richard Smith32952e12014-10-14 02:00:47 +00002626static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2627 if (auto *NS = X->getAsNamespace())
2628 return NS;
2629 if (auto *NAS = X->getAsNamespaceAlias())
2630 return NAS->getNamespace();
2631 return nullptr;
2632}
2633
2634static bool isSameQualifier(const NestedNameSpecifier *X,
2635 const NestedNameSpecifier *Y) {
2636 if (auto *NSX = getNamespace(X)) {
2637 auto *NSY = getNamespace(Y);
2638 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2639 return false;
2640 } else if (X->getKind() != Y->getKind())
2641 return false;
2642
2643 // FIXME: For namespaces and types, we're permitted to check that the entity
2644 // is named via the same tokens. We should probably do so.
2645 switch (X->getKind()) {
2646 case NestedNameSpecifier::Identifier:
2647 if (X->getAsIdentifier() != Y->getAsIdentifier())
2648 return false;
2649 break;
2650 case NestedNameSpecifier::Namespace:
2651 case NestedNameSpecifier::NamespaceAlias:
2652 // We've already checked that we named the same namespace.
2653 break;
2654 case NestedNameSpecifier::TypeSpec:
2655 case NestedNameSpecifier::TypeSpecWithTemplate:
2656 if (X->getAsType()->getCanonicalTypeInternal() !=
2657 Y->getAsType()->getCanonicalTypeInternal())
2658 return false;
2659 break;
2660 case NestedNameSpecifier::Global:
2661 case NestedNameSpecifier::Super:
2662 return true;
2663 }
2664
2665 // Recurse into earlier portion of NNS, if any.
2666 auto *PX = X->getPrefix();
2667 auto *PY = Y->getPrefix();
2668 if (PX && PY)
2669 return isSameQualifier(PX, PY);
2670 return !PX && !PY;
2671}
2672
Richard Smithbf78e642013-06-24 22:51:00 +00002673/// \brief Determine whether two template parameter lists are similar enough
2674/// that they may be used in declarations of the same template.
2675static bool isSameTemplateParameterList(const TemplateParameterList *X,
2676 const TemplateParameterList *Y) {
2677 if (X->size() != Y->size())
2678 return false;
2679
2680 for (unsigned I = 0, N = X->size(); I != N; ++I)
2681 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2682 return false;
2683
2684 return true;
2685}
2686
George Burgess IV95845082017-02-15 22:43:27 +00002687/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002688/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002689static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2690 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002691 // Note that pass_object_size attributes are represented in the function's
2692 // ExtParameterInfo, so we don't need to check them here.
2693
George Burgess IV95845082017-02-15 22:43:27 +00002694 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2695 // Since this is an equality check, we can ignore that enable_if attrs show up
2696 // in reverse order.
2697 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2698 AEnableIfs.push_back(EIA);
2699
2700 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2701 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2702 BEnableIfs.push_back(EIA);
2703
2704 // Two very common cases: either we have 0 enable_if attrs, or we have an
2705 // unequal number of enable_if attrs.
2706 if (AEnableIfs.empty() && BEnableIfs.empty())
2707 return true;
2708
2709 if (AEnableIfs.size() != BEnableIfs.size())
2710 return false;
2711
2712 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2713 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2714 Cand1ID.clear();
2715 Cand2ID.clear();
2716
2717 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2718 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2719 if (Cand1ID != Cand2ID)
2720 return false;
2721 }
2722
George Burgess IV95845082017-02-15 22:43:27 +00002723 return true;
2724}
2725
Douglas Gregor022857e2011-12-22 01:48:48 +00002726/// \brief Determine whether the two declarations refer to the same entity.
2727static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2728 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002729
Douglas Gregor022857e2011-12-22 01:48:48 +00002730 if (X == Y)
2731 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002732
Douglas Gregor022857e2011-12-22 01:48:48 +00002733 // Must be in the same context.
2734 if (!X->getDeclContext()->getRedeclContext()->Equals(
2735 Y->getDeclContext()->getRedeclContext()))
2736 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002737
2738 // Two typedefs refer to the same entity if they have the same underlying
2739 // type.
2740 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2741 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2742 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2743 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002744
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002745 // Must have the same kind.
2746 if (X->getKind() != Y->getKind())
2747 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002748
Douglas Gregorda389302012-01-01 21:47:52 +00002749 // Objective-C classes and protocols with the same name always match.
2750 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002751 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002752
2753 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002754 // No need to handle these here: we merge them when adding them to the
2755 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002756 return false;
2757 }
Richard Smithd55889a2013-09-09 16:55:27 +00002758
Douglas Gregor2009cee2012-01-03 22:46:00 +00002759 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002760 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2761 TagDecl *TagY = cast<TagDecl>(Y);
2762 return (TagX->getTagKind() == TagY->getTagKind()) ||
2763 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2764 TagX->getTagKind() == TTK_Interface) &&
2765 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2766 TagY->getTagKind() == TTK_Interface));
2767 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002768
Joao Matose9a3ed42012-08-31 22:18:20 +00002769 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002770 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2771 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002772 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2773 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002774 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2775 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2776 if (CtorX->getInheritedConstructor() &&
2777 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2778 CtorY->getInheritedConstructor().getConstructor()))
2779 return false;
2780 }
Richard Smitha54d3242017-03-08 23:00:26 +00002781 ASTContext &C = FuncX->getASTContext();
2782 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) {
2783 // We can get functions with different types on the redecl chain in C++17
2784 // if they have differing exception specifications and at least one of
2785 // the excpetion specs is unresolved.
2786 // FIXME: Do we need to check for C++14 deduced return types here too?
2787 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>();
2788 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>();
2789 if (C.getLangOpts().CPlusPlus1z && XFPT && YFPT &&
2790 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2791 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
2792 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(),
2793 FuncY->getType()))
2794 return true;
2795 return false;
2796 }
George Burgess IV95845082017-02-15 22:43:27 +00002797 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002798 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002799 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002800
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002801 // Variables with the same type and linkage match.
2802 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2803 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002804 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2805 ASTContext &C = VarX->getASTContext();
2806 if (C.hasSameType(VarX->getType(), VarY->getType()))
2807 return true;
2808
2809 // We can get decls with different types on the redecl chain. Eg.
2810 // template <typename T> struct S { static T Var[]; }; // #1
2811 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2812 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002813 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002814 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2815 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2816 if (!VarXTy || !VarYTy)
2817 return false;
2818 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2819 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2820 }
2821 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002822 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002823
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002824 // Namespaces with the same name and inlinedness match.
2825 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2826 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2827 return NamespaceX->isInline() == NamespaceY->isInline();
2828 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002829
Richard Smith8f8f05c2013-06-24 04:45:28 +00002830 // Identical template names and kinds match if their template parameter lists
2831 // and patterns match.
2832 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002833 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002834 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002835 TemplateY->getTemplatedDecl()) &&
2836 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2837 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002838 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002839
Richard Smith0b87e072013-10-07 08:02:11 +00002840 // Fields with the same name and the same type match.
2841 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2842 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002843 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002844 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2845 }
2846
Richard Smith8cbd8952015-08-04 02:05:09 +00002847 // Indirect fields with the same target field match.
2848 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2849 auto *IFDY = cast<IndirectFieldDecl>(Y);
2850 return IFDX->getAnonField()->getCanonicalDecl() ==
2851 IFDY->getAnonField()->getCanonicalDecl();
2852 }
2853
Richard Smith01a73372013-10-15 22:02:41 +00002854 // Enumerators with the same name match.
2855 if (isa<EnumConstantDecl>(X))
2856 // FIXME: Also check the value is odr-equivalent.
2857 return true;
2858
Richard Smithfd8634a2013-10-23 02:17:46 +00002859 // Using shadow declarations with the same target match.
2860 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2861 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2862 return USX->getTargetDecl() == USY->getTargetDecl();
2863 }
2864
Richard Smith32952e12014-10-14 02:00:47 +00002865 // Using declarations with the same qualifier match. (We already know that
2866 // the name matches.)
2867 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2868 auto *UY = cast<UsingDecl>(Y);
2869 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2870 UX->hasTypename() == UY->hasTypename() &&
2871 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2872 }
2873 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2874 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2875 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2876 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2877 }
2878 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2879 return isSameQualifier(
2880 UX->getQualifier(),
2881 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2882
Richard Smithf4634362014-09-03 23:11:22 +00002883 // Namespace alias definitions with the same target match.
2884 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2885 auto *NAY = cast<NamespaceAliasDecl>(Y);
2886 return NAX->getNamespace()->Equals(NAY->getNamespace());
2887 }
2888
Douglas Gregor022857e2011-12-22 01:48:48 +00002889 return false;
2890}
2891
Richard Smithd55889a2013-09-09 16:55:27 +00002892/// Find the context in which we should search for previous declarations when
2893/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002894DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2895 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002896 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2897 return ND->getOriginalNamespace();
2898
Richard Smith8a639892015-01-24 01:07:20 +00002899 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2900 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002901 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002902 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002903 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002904
2905 // If there's no definition yet, then DC's definition is added by an update
2906 // record, but we've not yet loaded that update record. In this case, we
2907 // commit to DC being the canonical definition now, and will fix this when
2908 // we load the update record.
2909 if (!DD) {
2910 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2911 RD->IsCompleteDefinition = true;
2912 RD->DefinitionData = DD;
2913 RD->getCanonicalDecl()->DefinitionData = DD;
2914
2915 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002916 Reader.PendingFakeDefinitionData.insert(
2917 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002918 }
2919
2920 return DD->Definition;
2921 }
Richard Smithd55889a2013-09-09 16:55:27 +00002922
Richard Smith01a73372013-10-15 22:02:41 +00002923 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002924 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2925 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002926
Richard Smith4eca9b92015-02-04 23:37:59 +00002927 // We can see the TU here only if we have no Sema object. In that case,
2928 // there's no TU scope to look in, so using the DC alone is sufficient.
2929 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2930 return TU;
2931
Craig Toppera13603a2014-05-22 05:54:18 +00002932 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002933}
2934
Douglas Gregor022857e2011-12-22 01:48:48 +00002935ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002936 // Record that we had a typedef name for linkage whether or not we merge
2937 // with that declaration.
2938 if (TypedefNameForLinkage) {
2939 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2940 Reader.ImportedTypedefNamesForLinkage.insert(
2941 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2942 return;
2943 }
2944
Douglas Gregor9b47f942012-01-09 17:38:47 +00002945 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002946 return;
Richard Smith95d99302013-07-13 02:00:19 +00002947
Richard Smith70d58502014-08-30 00:04:23 +00002948 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002949 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002950 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002951 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2952 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002953 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002954 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002955 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002956 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2957 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002958 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002959 // Add the declaration to its redeclaration context so later merging
2960 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002961 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002962 }
2963}
2964
Richard Smith88ebade2014-08-23 01:45:27 +00002965/// Find the declaration that should be merged into, given the declaration found
2966/// by name lookup. If we're merging an anonymous declaration within a typedef,
2967/// we need a matching typedef, and we merge with the type inside it.
2968static NamedDecl *getDeclForMerging(NamedDecl *Found,
2969 bool IsTypedefNameForLinkage) {
2970 if (!IsTypedefNameForLinkage)
2971 return Found;
2972
2973 // If we found a typedef declaration that gives a name to some other
2974 // declaration, then we want that inner declaration. Declarations from
2975 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002976 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002977 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002978
2979 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00002980 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00002981
Hans Wennborgdcfba332015-10-06 23:40:43 +00002982 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00002983}
2984
Richard Smithd08aeb62014-08-28 01:33:39 +00002985NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2986 DeclContext *DC,
2987 unsigned Index) {
2988 // If the lexical context has been merged, look into the now-canonical
2989 // definition.
2990 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2991 DC = Merged;
2992
2993 // If we've seen this before, return the canonical declaration.
2994 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2995 if (Index < Previous.size() && Previous[Index])
2996 return Previous[Index];
2997
2998 // If this is the first time, but we have parsed a declaration of the context,
2999 // build the anonymous declaration list from the parsed declaration.
3000 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00003001 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
3002 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003003 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3004 else
Richard Smith2b560572015-02-07 03:11:11 +00003005 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3006 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003007 }
3008
3009 return Index < Previous.size() ? Previous[Index] : nullptr;
3010}
3011
3012void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3013 DeclContext *DC, unsigned Index,
3014 NamedDecl *D) {
3015 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3016 DC = Merged;
3017
3018 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3019 if (Index >= Previous.size())
3020 Previous.resize(Index + 1);
3021 if (!Previous[Index])
3022 Previous[Index] = D;
3023}
3024
Douglas Gregor022857e2011-12-22 01:48:48 +00003025ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003026 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3027 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003028
Richard Smithd08aeb62014-08-28 01:33:39 +00003029 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3030 // Don't bother trying to find unnamed declarations that are in
3031 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003032 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3033 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003034 Result.suppress();
3035 return Result;
3036 }
Richard Smith95d99302013-07-13 02:00:19 +00003037
Douglas Gregor022857e2011-12-22 01:48:48 +00003038 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003039 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003040 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003041 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003042 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3043 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003044 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3045 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003046 // Go on to check in other places in case an existing typedef name
3047 // was not imported.
3048 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003049
Richard Smith2b560572015-02-07 03:11:11 +00003050 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003051 // This is an anonymous declaration that we may need to merge. Look it up
3052 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003053 if (auto *Existing = getAnonymousDeclForMerging(
3054 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3055 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003056 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3057 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003058 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003059 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003060 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003061
3062 // Temporarily consider the identifier to be up-to-date. We don't want to
3063 // cause additional lookups here.
3064 class UpToDateIdentifierRAII {
3065 IdentifierInfo *II;
3066 bool WasOutToDate;
3067
3068 public:
3069 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3070 : II(II), WasOutToDate(false)
3071 {
3072 if (II) {
3073 WasOutToDate = II->isOutOfDate();
3074 if (WasOutToDate)
3075 II->setOutOfDate(false);
3076 }
3077 }
3078
3079 ~UpToDateIdentifierRAII() {
3080 if (WasOutToDate)
3081 II->setOutOfDate(true);
3082 }
3083 } UpToDate(Name.getAsIdentifierInfo());
3084
Douglas Gregor2009cee2012-01-03 22:46:00 +00003085 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003086 IEnd = IdResolver.end();
3087 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003088 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003089 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003090 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3091 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003092 }
Richard Smith8a639892015-01-24 01:07:20 +00003093 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003094 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003095 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003096 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003097 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003098 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3099 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003100 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003101 } else {
3102 // Not in a mergeable context.
3103 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003104 }
Richard Smithd55889a2013-09-09 16:55:27 +00003105
Richard Smith2b9e3e32013-10-18 06:05:18 +00003106 // If this declaration is from a merged context, make a note that we need to
3107 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003108 //
3109 // FIXME: We should do something similar if we merge two definitions of the
3110 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003111 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3112 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3113 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003114 Reader.PendingOdrMergeChecks.push_back(D);
3115
Richard Smithd08aeb62014-08-28 01:33:39 +00003116 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003117 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003118}
3119
Richard Smithb321ecb2014-05-13 01:15:00 +00003120template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003121Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3122 return D->RedeclLink.getLatestNotUpdated();
3123}
3124Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3125 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3126}
3127
3128Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3129 assert(D);
3130
3131 switch (D->getKind()) {
3132#define ABSTRACT_DECL(TYPE)
3133#define DECL(TYPE, BASE) \
3134 case Decl::TYPE: \
3135 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3136#include "clang/AST/DeclNodes.inc"
3137 }
3138 llvm_unreachable("unknown decl kind");
3139}
3140
Richard Smith8ce51082015-03-11 01:44:51 +00003141Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3142 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3143}
3144
Richard Smithc3a53252015-02-28 05:57:02 +00003145template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003146void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3147 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003148 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003149 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003150 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003151}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003152
Richard Smith24d166c2014-07-31 23:52:38 +00003153namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003154template<>
3155void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003156 Redeclarable<VarDecl> *D,
3157 Decl *Previous, Decl *Canon) {
3158 VarDecl *VD = static_cast<VarDecl*>(D);
3159 VarDecl *PrevVD = cast<VarDecl>(Previous);
3160 D->RedeclLink.setPrevious(PrevVD);
3161 D->First = PrevVD->First;
3162
3163 // We should keep at most one definition on the chain.
3164 // FIXME: Cache the definition once we've found it. Building a chain with
3165 // N definitions currently takes O(N^2) time here.
3166 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3167 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3168 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3169 Reader.mergeDefinitionVisibility(CurD, VD);
3170 VD->demoteThisDefinitionToDeclaration();
3171 break;
3172 }
3173 }
3174 }
3175}
3176
3177template<>
3178void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003179 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003180 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003181 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3182 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3183
3184 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003185 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003186
3187 // If the previous declaration is an inline function declaration, then this
3188 // declaration is too.
3189 if (PrevFD->IsInline != FD->IsInline) {
3190 // FIXME: [dcl.fct.spec]p4:
3191 // If a function with external linkage is declared inline in one
3192 // translation unit, it shall be declared inline in all translation
3193 // units in which it appears.
3194 //
3195 // Be careful of this case:
3196 //
3197 // module A:
3198 // template<typename T> struct X { void f(); };
3199 // template<typename T> inline void X<T>::f() {}
3200 //
3201 // module B instantiates the declaration of X<int>::f
3202 // module C instantiates the definition of X<int>::f
3203 //
3204 // If module B and C are merged, we do not have a violation of this rule.
3205 FD->IsInline = true;
3206 }
3207
Richard Smith9e2341d2015-03-23 03:25:59 +00003208 // If we need to propagate an exception specification along the redecl
3209 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003210 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3211 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003212 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003213 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3214 bool WasUnresolved =
3215 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3216 if (IsUnresolved != WasUnresolved)
3217 Reader.PendingExceptionSpecUpdates.insert(
3218 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003219 }
3220}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003221} // end namespace clang
3222
Richard Smith6de7a242014-07-31 23:46:44 +00003223void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003224 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3225}
3226
Richard Smith8346e522015-06-10 01:47:58 +00003227/// Inherit the default template argument from \p From to \p To. Returns
3228/// \c false if there is no default template for \p From.
3229template <typename ParmDecl>
3230static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3231 Decl *ToD) {
3232 auto *To = cast<ParmDecl>(ToD);
3233 if (!From->hasDefaultArgument())
3234 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003235 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003236 return true;
3237}
3238
3239static void inheritDefaultTemplateArguments(ASTContext &Context,
3240 TemplateDecl *From,
3241 TemplateDecl *To) {
3242 auto *FromTP = From->getTemplateParameters();
3243 auto *ToTP = To->getTemplateParameters();
3244 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3245
3246 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3247 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003248 if (FromParam->isParameterPack())
3249 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003250 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3251
3252 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003253 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003254 break;
3255 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003256 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003257 break;
3258 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003259 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003260 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3261 break;
3262 }
3263 }
3264}
3265
Richard Smith6de7a242014-07-31 23:46:44 +00003266void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003267 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003268 assert(D && Previous);
3269
3270 switch (D->getKind()) {
3271#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003272#define DECL(TYPE, BASE) \
3273 case Decl::TYPE: \
3274 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003275 break;
3276#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003277 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003278
3279 // If the declaration was visible in one module, a redeclaration of it in
3280 // another module remains visible even if it wouldn't be visible by itself.
3281 //
3282 // FIXME: In this case, the declaration should only be visible if a module
3283 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003284 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003285 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003286 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003287
Richard Smith8346e522015-06-10 01:47:58 +00003288 // If the declaration declares a template, it may inherit default arguments
3289 // from the previous declaration.
3290 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3291 inheritDefaultTemplateArguments(Reader.getContext(),
3292 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003293}
3294
Richard Smithb321ecb2014-05-13 01:15:00 +00003295template<typename DeclT>
3296void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003297 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003298}
3299void ASTDeclReader::attachLatestDeclImpl(...) {
3300 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3301}
3302
Douglas Gregor05f10352011-12-17 23:38:30 +00003303void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3304 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003305
3306 switch (D->getKind()) {
3307#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003308#define DECL(TYPE, BASE) \
3309 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003310 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3311 break;
3312#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003313 }
3314}
3315
Richard Smith851072e2014-05-19 20:59:20 +00003316template<typename DeclT>
3317void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3318 D->RedeclLink.markIncomplete();
3319}
3320void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3321 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3322}
3323
3324void ASTReader::markIncompleteDeclChain(Decl *D) {
3325 switch (D->getKind()) {
3326#define ABSTRACT_DECL(TYPE)
3327#define DECL(TYPE, BASE) \
3328 case Decl::TYPE: \
3329 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3330 break;
3331#include "clang/AST/DeclNodes.inc"
3332 }
3333}
3334
Sebastian Redlb3298c32010-08-18 23:56:48 +00003335/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003336Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003337 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003338 SourceLocation DeclLoc;
3339 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003340 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003341 // Keep track of where we are in the stream, then jump back there
3342 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003343 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003344
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003345 ReadingKindTracker ReadingKind(Read_Decl, *this);
3346
Douglas Gregor1342e842009-07-06 18:54:52 +00003347 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003348 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003349
Sebastian Redl2c373b92010-10-05 15:59:54 +00003350 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003351 ASTRecordReader Record(*this, *Loc.F);
3352 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003353 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003354
Craig Toppera13603a2014-05-22 05:54:18 +00003355 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003356 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003357 case DECL_CONTEXT_LEXICAL:
3358 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003359 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003360 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003361 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003362 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003363 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003364 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003365 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003366 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003367 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003368 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003369 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003370 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003371 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003372 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003373 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003374 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003375 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003376 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003377 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003378 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003379 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003380 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003381 case DECL_EXPORT:
3382 D = ExportDecl::CreateDeserialized(Context, ID);
3383 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003384 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003385 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003386 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003387 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003388 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003389 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003390 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003391 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003392 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003393 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003394 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003395 break;
Richard Smith151c4562016-12-20 21:35:28 +00003396 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003397 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003398 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003399 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003400 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003401 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003402 case DECL_CONSTRUCTOR_USING_SHADOW:
3403 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3404 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003405 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003406 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003407 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003408 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003409 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003410 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003411 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003412 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003413 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003414 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003415 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003416 break;
Richard Smithbc491202017-02-17 20:05:37 +00003417 case DECL_CXX_DEDUCTION_GUIDE:
3418 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3419 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003420 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003421 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003422 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003423 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003424 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3425 break;
3426 case DECL_CXX_INHERITED_CONSTRUCTOR:
3427 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003428 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003429 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003430 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003431 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003432 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003433 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003434 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003435 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003436 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003437 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003438 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003439 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003440 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003441 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003442 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003443 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003445 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003446 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003447 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003448 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003449 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003450 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003451 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003452 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003453 case DECL_VAR_TEMPLATE:
3454 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3455 break;
3456 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3457 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3458 break;
3459 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3460 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3461 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003462 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003463 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003464 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003465 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003466 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003467 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003468 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003469 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003470 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003471 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003472 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003473 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003474 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003475 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3476 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003477 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003478 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003479 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003480 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003481 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3482 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003483 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003484 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003485 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003486 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003487 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003488 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003489 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003490 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003491 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003492 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003493 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003494 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003495 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003496 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003497 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003498 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003499 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003500 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003501 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003502 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003503 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003504 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003505 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003506 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003507 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003508 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003510 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003511 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003512 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003513 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003514 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003516 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003517 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003518 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003519 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003520 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003521 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003522 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003523 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003524 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003525 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003526 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003527 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003528 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003529 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003530 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003531 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003532 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003533 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003534 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003535 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003536 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003537 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003538 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003539 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003540 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003541 break;
3542 case DECL_BINDING:
3543 D = BindingDecl::CreateDeserialized(Context, ID);
3544 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003545 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003546 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003547 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003548 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003549 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003550 break;
John McCall5e77d762013-04-16 07:28:30 +00003551 case DECL_MS_PROPERTY:
3552 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3553 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003554 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003555 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003556 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003557 case DECL_CXX_BASE_SPECIFIERS:
3558 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003559 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003560 case DECL_CXX_CTOR_INITIALIZERS:
3561 Error("attempt to read a C++ ctor initializer record as a declaration");
3562 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003563 case DECL_IMPORT:
3564 // Note: last entry of the ImportDecl record is the number of stored source
3565 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003566 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003567 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003568 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003569 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003570 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003571 case DECL_OMP_DECLARE_REDUCTION:
3572 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3573 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003574 case DECL_OMP_CAPTUREDEXPR:
3575 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003576 break;
Nico Weber66220292016-03-02 17:28:48 +00003577 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003578 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003579 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003580 case DECL_PRAGMA_DETECT_MISMATCH:
3581 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003582 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003583 break;
Michael Han84324352013-02-22 17:15:32 +00003584 case DECL_EMPTY:
3585 D = EmptyDecl::CreateDeserialized(Context, ID);
3586 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003587 case DECL_OBJC_TYPE_PARAM:
3588 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3589 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003590 }
Chris Lattner487412d2009-04-27 05:27:42 +00003591
Sebastian Redlb3298c32010-08-18 23:56:48 +00003592 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003593 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003594 // Set the DeclContext before doing any deserialization, to make sure internal
3595 // calls to Decl::getASTContext() by Decl's methods will find the
3596 // TranslationUnitDecl without crashing.
3597 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003598 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003599
3600 // If this declaration is also a declaration context, get the
3601 // offsets for its tables of lexical and visible declarations.
3602 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3603 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003604 if (Offsets.first &&
3605 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3606 return nullptr;
3607 if (Offsets.second &&
3608 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3609 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003610 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003611 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003612
Douglas Gregordab42432011-08-12 00:15:20 +00003613 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003614 PendingUpdateRecords.push_back(
3615 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003616
Douglas Gregor404cdde2012-01-27 01:47:08 +00003617 // Load the categories after recursive loading is finished.
3618 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003619 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3620 // we put the Decl in PendingDefinitions so we can pull the categories here.
3621 if (Class->isThisDeclarationADefinition() ||
3622 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003623 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003624
Richard Smith13fb8602016-07-15 21:33:46 +00003625 // If we have deserialized a declaration that has a definition the
3626 // AST consumer might need to know about, queue it.
3627 // We don't pass it to the consumer immediately because we may be in recursive
3628 // loading, and some declarations may still be initializing.
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003629 PotentiallyInterestingDecls.push_back(
3630 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003631
Douglas Gregordab42432011-08-12 00:15:20 +00003632 return D;
3633}
3634
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003635void ASTReader::PassInterestingDeclsToConsumer() {
3636 assert(Consumer);
3637
3638 if (PassingDeclsToConsumer)
3639 return;
3640
3641 // Guard variable to avoid recursively redoing the process of passing
3642 // decls to consumer.
3643 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3644 true);
3645
3646 // Ensure that we've loaded all potentially-interesting declarations
3647 // that need to be eagerly loaded.
3648 for (auto ID : EagerlyDeserializedDecls)
3649 GetDecl(ID);
3650 EagerlyDeserializedDecls.clear();
3651
3652 while (!PotentiallyInterestingDecls.empty()) {
3653 InterestingDecl D = PotentiallyInterestingDecls.front();
3654 PotentiallyInterestingDecls.pop_front();
3655 if (isConsumerInterestedIn(Context, D.getDecl(), D.hasPendingBody()))
3656 PassInterestingDeclToConsumer(D.getDecl());
3657 }
3658}
3659
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003660void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003661 // The declaration may have been modified by files later in the chain.
3662 // If this is the case, read the record containing the updates from each file
3663 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003664 serialization::GlobalDeclID ID = Record.ID;
3665 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003666 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003667 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3668 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003669 auto UpdateOffsets = std::move(UpdI->second);
3670 DeclUpdateOffsets.erase(UpdI);
3671
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003672 // Check if this decl was interesting to the consumer. If we just loaded
3673 // the declaration, then we know it was interesting and we skip the call
3674 // to isConsumerInterestedIn because it is unsafe to call in the
3675 // current ASTReader state.
3676 bool WasInteresting =
3677 Record.JustLoaded || isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003678 for (auto &FileAndOffset : UpdateOffsets) {
3679 ModuleFile *F = FileAndOffset.first;
3680 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003681 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3682 SavedStreamPosition SavedPosition(Cursor);
3683 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003684 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003685 ASTRecordReader Record(*this, *F);
3686 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003687 (void)RecCode;
3688 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003689
David L. Jonesbe1557a2016-12-21 00:17:49 +00003690 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3691 SourceLocation());
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00003692 Reader.UpdateDecl(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003693
3694 // We might have made this declaration interesting. If so, remember that
3695 // we need to hand it off to the consumer.
3696 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003697 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003698 PotentiallyInterestingDecls.push_back(
3699 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003700 WasInteresting = true;
3701 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003702 }
3703 }
Richard Smith1e8e91b2016-04-08 20:53:26 +00003704
3705 // Load the pending visible updates for this decl context, if it has any.
3706 auto I = PendingVisibleUpdates.find(ID);
3707 if (I != PendingVisibleUpdates.end()) {
3708 auto VisibleUpdates = std::move(I->second);
3709 PendingVisibleUpdates.erase(I);
3710
3711 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3712 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3713 Lookups[DC].Table.add(
3714 Update.Mod, Update.Data,
3715 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3716 DC->setHasExternalVisibleStorage(true);
3717 }
Chris Lattner487412d2009-04-27 05:27:42 +00003718}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003719
Richard Smithd61d4ac2015-08-22 20:13:39 +00003720void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3721 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003722 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003723 if (FirstLocal != CanonDecl) {
3724 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3725 ASTDeclReader::attachPreviousDecl(
3726 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3727 CanonDecl);
3728 }
3729
3730 if (!LocalOffset) {
3731 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3732 return;
3733 }
3734
3735 // Load the list of other redeclarations from this module file.
3736 ModuleFile *M = getOwningModuleFile(FirstLocal);
3737 assert(M && "imported decl from no module file");
3738
3739 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3740 SavedStreamPosition SavedPosition(Cursor);
3741 Cursor.JumpToBit(LocalOffset);
3742
3743 RecordData Record;
3744 unsigned Code = Cursor.ReadCode();
3745 unsigned RecCode = Cursor.readRecord(Code, Record);
3746 (void)RecCode;
3747 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3748
3749 // FIXME: We have several different dispatches on decl kind here; maybe
3750 // we should instead generate one loop per kind and dispatch up-front?
3751 Decl *MostRecent = FirstLocal;
3752 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3753 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003754 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3755 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003756 }
Richard Smithc3a53252015-02-28 05:57:02 +00003757 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003758}
3759
3760namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003761 /// \brief Given an ObjC interface, goes through the modules and links to the
3762 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003763 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003764 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003765 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003766 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003767 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003768 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003769 serialization::GlobalDeclID InterfaceID;
3770 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003771
3772 void add(ObjCCategoryDecl *Cat) {
3773 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003774 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003775 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003776
3777 // Check for duplicate categories.
3778 if (Cat->getDeclName()) {
3779 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3780 if (Existing &&
3781 Reader.getOwningModuleFile(Existing)
3782 != Reader.getOwningModuleFile(Cat)) {
3783 // FIXME: We should not warn for duplicates in diamond:
3784 //
3785 // MT //
3786 // / \ //
3787 // ML MR //
3788 // \ / //
3789 // MB //
3790 //
3791 // If there are duplicates in ML/MR, there will be warning when
3792 // creating MB *and* when importing MB. We should not warn when
3793 // importing.
3794 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3795 << Interface->getDeclName() << Cat->getDeclName();
3796 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3797 } else if (!Existing) {
3798 // Record this category.
3799 Existing = Cat;
3800 }
3801 }
3802
3803 // Add this category to the end of the chain.
3804 if (Tail)
3805 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3806 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003807 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003808 Tail = Cat;
3809 }
3810
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003811 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003812 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003813 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003814 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3815 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003816 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003817 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3818 Tail(nullptr), InterfaceID(InterfaceID),
3819 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003820 {
3821 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003822 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003823 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003824 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003825
3826 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003827 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003828 }
3829 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003830
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003831 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003832 // If we've loaded all of the category information we care about from
3833 // this module file, we're done.
3834 if (M.Generation <= PreviousGeneration)
3835 return true;
3836
3837 // Map global ID of the definition down to the local ID used in this
3838 // module file. If there is no such mapping, we'll find nothing here
3839 // (or in any module it imports).
3840 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3841 if (!LocalID)
3842 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003843
Douglas Gregor404cdde2012-01-27 01:47:08 +00003844 // Perform a binary search to find the local redeclarations for this
3845 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003846 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003847 const ObjCCategoriesInfo *Result
3848 = std::lower_bound(M.ObjCCategoriesMap,
3849 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003850 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003851 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3852 Result->DefinitionID != LocalID) {
3853 // We didn't find anything. If the class definition is in this module
3854 // file, then the module files it depends on cannot have any categories,
3855 // so suppress further lookup.
3856 return Reader.isDeclIDFromModule(InterfaceID, M);
3857 }
3858
3859 // We found something. Dig out all of the categories.
3860 unsigned Offset = Result->Offset;
3861 unsigned N = M.ObjCCategories[Offset];
3862 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3863 for (unsigned I = 0; I != N; ++I)
3864 add(cast_or_null<ObjCCategoryDecl>(
3865 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3866 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003867 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003868 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003869} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003870
Douglas Gregor404cdde2012-01-27 01:47:08 +00003871void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3872 ObjCInterfaceDecl *D,
3873 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003874 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003875 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003876 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003877}
Douglas Gregordab42432011-08-12 00:15:20 +00003878
Richard Smithd6db68c2014-08-07 20:58:41 +00003879template<typename DeclT, typename Fn>
3880static void forAllLaterRedecls(DeclT *D, Fn F) {
3881 F(D);
3882
3883 // Check whether we've already merged D into its redeclaration chain.
3884 // MostRecent may or may not be nullptr if D has not been merged. If
3885 // not, walk the merged redecl chain and see if it's there.
3886 auto *MostRecent = D->getMostRecentDecl();
3887 bool Found = false;
3888 for (auto *Redecl = MostRecent; Redecl && !Found;
3889 Redecl = Redecl->getPreviousDecl())
3890 Found = (Redecl == D);
3891
3892 // If this declaration is merged, apply the functor to all later decls.
3893 if (Found) {
3894 for (auto *Redecl = MostRecent; Redecl != D;
3895 Redecl = Redecl->getPreviousDecl())
3896 F(Redecl);
3897 }
3898}
3899
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00003900void ASTDeclReader::UpdateDecl(Decl *D) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003901 while (Record.getIdx() < Record.size()) {
3902 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003903 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003904 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003905 // FIXME: If we also have an update record for instantiating the
3906 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003907 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003908 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003909 // FIXME: We should call addHiddenDecl instead, to add the member
3910 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003911 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003912 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003913 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003914
3915 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00003916 // It will be added to the template's specializations set when loaded.
3917 (void)Record.readDecl();
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003918 break;
3919
3920 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003921 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3922
Douglas Gregor540fd812012-01-09 18:07:24 +00003923 // Each module has its own anonymous namespace, which is disjoint from
3924 // any other module's anonymous namespaces, so don't attach the anonymous
3925 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003926 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003927 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3928 TU->setAnonymousNamespace(Anon);
3929 else
3930 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3931 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003932 break;
3933 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003934
Richard Smith05a21352017-06-22 22:18:46 +00003935 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: {
3936 VarDecl *VD = cast<VarDecl>(D);
3937 VD->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003938 ReadSourceLocation());
Richard Smith05a21352017-06-22 22:18:46 +00003939 uint64_t Val = Record.readInt();
3940 if (Val && !VD->getInit()) {
3941 VD->setInit(Record.readExpr());
3942 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
3943 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
3944 Eval->CheckedICE = true;
3945 Eval->IsICE = Val == 3;
3946 }
3947 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003948 break;
Richard Smith05a21352017-06-22 22:18:46 +00003949 }
Richard Smith1fa5d642013-05-11 05:45:24 +00003950
John McCall32791cc2016-01-06 22:34:54 +00003951 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3952 auto Param = cast<ParmVarDecl>(D);
3953
3954 // We have to read the default argument regardless of whether we use it
3955 // so that hypothetical further update records aren't messed up.
3956 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003957 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003958
3959 // Only apply the update if the parameter still has an uninstantiated
3960 // default argument.
3961 if (Param->hasUninstantiatedDefaultArg())
3962 Param->setDefaultArg(DefaultArg);
3963 break;
3964 }
3965
Richard Smith4b054b22016-08-24 21:25:37 +00003966 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3967 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003968 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003969
3970 // Only apply the update if the field still has an uninstantiated
3971 // default member initializer.
3972 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3973 if (DefaultInit)
3974 FD->setInClassInitializer(DefaultInit);
3975 else
3976 // Instantiation failed. We can get here if we serialized an AST for
3977 // an invalid program.
3978 FD->removeInClassInitializer();
3979 }
3980 break;
3981 }
3982
Richard Smith4d235792014-08-07 18:53:08 +00003983 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003984 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003985 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003986 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003987 // It's safe to stop now because this update record is always last.
3988 return;
3989 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003990
David L. Jonesbe1557a2016-12-21 00:17:49 +00003991 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00003992 // Maintain AST consistency: any later redeclarations of this function
3993 // are inline if this one is. (We might have merged another declaration
3994 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00003995 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3996 FD->setImplicitlyInline();
3997 });
Richard Smith195d8ef2014-05-29 03:15:31 +00003998 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003999 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004000 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004001 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004002 break;
4003 }
4004
Richard Smithcd45dbc2014-04-19 03:48:30 +00004005 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4006 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004007 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004008 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004009 OldDD && (OldDD->Definition != RD ||
4010 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00004011 ReadCXXRecordDefinition(RD, /*Update*/true);
4012
Richard Smithcd45dbc2014-04-19 03:48:30 +00004013 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004014 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004015 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004016 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004017 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004018 }
4019
David L. Jonesbe1557a2016-12-21 00:17:49 +00004020 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004021 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004022 if (MemberSpecializationInfo *MSInfo =
4023 RD->getMemberSpecializationInfo()) {
4024 MSInfo->setTemplateSpecializationKind(TSK);
4025 MSInfo->setPointOfInstantiation(POI);
4026 } else {
4027 ClassTemplateSpecializationDecl *Spec =
4028 cast<ClassTemplateSpecializationDecl>(RD);
4029 Spec->setTemplateSpecializationKind(TSK);
4030 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004031
David L. Jonesbe1557a2016-12-21 00:17:49 +00004032 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00004033 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004034 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004035 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004036 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004037 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004038 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004039
4040 // FIXME: If we already have a partial specialization set,
4041 // check that it matches.
4042 if (!Spec->getSpecializedTemplateOrPartial()
4043 .is<ClassTemplatePartialSpecializationDecl *>())
4044 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004045 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004046 }
4047
David L. Jonesbe1557a2016-12-21 00:17:49 +00004048 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004049 RD->setLocation(ReadSourceLocation());
4050 RD->setLocStart(ReadSourceLocation());
4051 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004052
David L. Jonesbe1557a2016-12-21 00:17:49 +00004053 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004054 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004055 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004056 // If the declaration already has attributes, we assume that some other
4057 // AST file already loaded them.
4058 if (!D->hasAttrs())
4059 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004060 }
4061 break;
4062 }
4063
Richard Smithf8134002015-03-10 01:41:22 +00004064 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4065 // Set the 'operator delete' directly to avoid emitting another update
4066 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004067 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004068 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
4069 // FIXME: Check consistency if we have an old and new operator delete.
4070 if (!First->OperatorDelete)
4071 First->OperatorDelete = Del;
4072 break;
4073 }
4074
Richard Smith564417a2014-03-20 21:47:22 +00004075 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004076 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004077 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004078 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004079
4080 // Update this declaration's exception specification, if needed.
4081 auto *FD = cast<FunctionDecl>(D);
4082 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4083 // FIXME: If the exception specification is already present, check that it
4084 // matches.
4085 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00004086 FD->setType(Reader.Context.getFunctionType(
4087 FPT->getReturnType(), FPT->getParamTypes(),
4088 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004089
4090 // When we get to the end of deserializing, see if there are other decls
4091 // that we need to propagate this exception specification onto.
4092 Reader.PendingExceptionSpecUpdates.insert(
4093 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004094 }
Richard Smith564417a2014-03-20 21:47:22 +00004095 break;
4096 }
4097
Richard Smith1fa5d642013-05-11 05:45:24 +00004098 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004099 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004100 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004101 for (auto *Redecl : merged_redecls(D)) {
4102 // FIXME: If the return type is already deduced, check that it matches.
4103 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
4104 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
4105 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004106 break;
4107 }
Eli Friedman276dd182013-09-05 00:02:25 +00004108
4109 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004110 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004111 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00004112 break;
4113 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004114
4115 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004116 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004117 break;
4118
4119 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004120 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004121 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004122
Alexey Bataev97720002014-11-11 04:05:39 +00004123 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4124 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004125 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004126 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004127
Alex Denisovfde64952015-06-26 05:28:36 +00004128 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004129 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004130 auto *Exported = cast<NamedDecl>(D);
4131 if (auto *TD = dyn_cast<TagDecl>(Exported))
4132 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004133 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004134 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004135 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4136 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004137 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4138 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004139 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004140 // If Owner is made visible at some later point, make this declaration
4141 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004142 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004143 } else {
4144 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004145 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004146 }
4147 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004148 }
Alex Denisovfde64952015-06-26 05:28:36 +00004149
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004150 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004151 case UPD_ADDED_ATTR_TO_RECORD:
4152 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004153 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004154 assert(Attrs.size() == 1);
4155 D->addAttr(Attrs[0]);
4156 break;
4157 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004158 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004159}