blob: db7d55ec0b877c395b6b84d2c82a284d68fe0fc0 [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
David L. Jonesc4808b9e2016-12-15 20:53:26 +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;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000525 D->setModulePrivate(Record.readInt());
Douglas Gregor781f7132012-01-06 16:59:53 +0000526 D->Hidden = D->isModulePrivate();
Richard Smith42413142015-05-15 20:05:43 +0000527
Douglas Gregorcf68c582011-12-01 22:20:10 +0000528 // Determine whether this declaration is part of a (sub)module. If so, it
529 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000530 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000531 // Store the owning submodule ID in the declaration.
532 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000533
534 if (D->Hidden) {
535 // Module-private declarations are never visible, so there is no work to do.
536 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
537 // If local visibility is being tracked, this declaration will become
538 // hidden and visible as the owning module does. Inform Sema that this
539 // declaration might not be visible.
540 D->Hidden = true;
541 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
542 if (Owner->NameVisibility != Module::AllVisible) {
543 // The owning module is not visible. Mark this declaration as hidden.
544 D->Hidden = true;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000545
Richard Smith42413142015-05-15 20:05:43 +0000546 // Note that this declaration was hidden because its owning module is
547 // not yet visible.
548 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000549 }
550 }
551 }
Chris Lattner487412d2009-04-27 05:27:42 +0000552}
553
Nico Weber66220292016-03-02 17:28:48 +0000554void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
555 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000556 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000557 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000558 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000559 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
560 D->getTrailingObjects<char>()[Arg.size()] = '\0';
561}
562
Nico Webercbbaeb12016-03-02 19:28:54 +0000563void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
564 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000565 D->setLocation(ReadSourceLocation());
566 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000567 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
568 D->getTrailingObjects<char>()[Name.size()] = '\0';
569
570 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000571 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000572 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
573 Value.size());
574 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
575}
576
Sebastian Redlb3298c32010-08-18 23:56:48 +0000577void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000578 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000579}
580
Sebastian Redlb3298c32010-08-18 23:56:48 +0000581void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000582 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000583 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000584 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000585}
586
Sebastian Redlb3298c32010-08-18 23:56:48 +0000587void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000588 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000589 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000590 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000591 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000592}
593
Richard Smith43ccec8e2014-08-26 03:52:16 +0000594ASTDeclReader::RedeclarableResult
595ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000596 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000597 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000598 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000599 if (Record.readInt()) { // isModed
600 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000601 TD->setModedTypeSourceInfo(TInfo, modedT);
602 } else
603 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000604 // Read and discard the declaration for which this is a typedef name for
605 // linkage, if it exists. We cannot rely on our type to pull in this decl,
606 // because it might have been merged with a type from another module and
607 // thus might not refer to our version of the declaration.
608 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000609 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000610}
611
612void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000613 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
614 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000615}
616
Richard Smithdda56e42011-04-15 14:24:37 +0000617void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000618 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000619 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000620 // Merged when we merge the template.
621 TD->setDescribedAliasTemplate(Template);
622 else
623 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000624}
625
Richard Smith1d209d02013-05-23 01:49:11 +0000626ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000627 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000628 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000629
David L. Jonesbe1557a2016-12-21 00:17:49 +0000630 TD->IdentifierNamespace = Record.readInt();
631 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000632 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000633 TD->setCompleteDefinition(Record.readInt());
634 TD->setEmbeddedInDeclarator(Record.readInt());
635 TD->setFreeStanding(Record.readInt());
636 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000637 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000638
David L. Jonesbe1557a2016-12-21 00:17:49 +0000639 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000640 case 0:
641 break;
642 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000643 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000644 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000645 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000646 break;
647 }
648 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000649 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000650 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000651 break;
Richard Smith70d58502014-08-30 00:04:23 +0000652 default:
653 llvm_unreachable("unexpected tag info kind");
654 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000655
Richard Smithcd45dbc2014-04-19 03:48:30 +0000656 if (!isa<CXXRecordDecl>(TD))
657 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000658 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000659}
660
Sebastian Redlb3298c32010-08-18 23:56:48 +0000661void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000662 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000663 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000664 ED->setIntegerTypeSourceInfo(TI);
665 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000666 ED->setIntegerType(Record.readType());
667 ED->setPromotionType(Record.readType());
668 ED->setNumPositiveBits(Record.readInt());
669 ED->setNumNegativeBits(Record.readInt());
670 ED->IsScoped = Record.readInt();
671 ED->IsScopedUsingClassTag = Record.readInt();
672 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000673
Richard Smith01a73372013-10-15 22:02:41 +0000674 // If this is a definition subject to the ODR, and we already have a
675 // definition, merge this one into it.
676 if (ED->IsCompleteDefinition &&
677 Reader.getContext().getLangOpts().Modules &&
678 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000679 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
680 if (!OldDef) {
681 // This is the first time we've seen an imported definition. Look for a
682 // local definition before deciding that we are the first definition.
683 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
684 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
685 OldDef = D;
686 break;
687 }
688 }
689 }
690 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000691 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
692 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000693 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000694 } else {
695 OldDef = ED;
696 }
697 }
698
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000699 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000700 TemplateSpecializationKind TSK =
701 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000702 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000703 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
704 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
705 }
Chris Lattner487412d2009-04-27 05:27:42 +0000706}
707
Richard Smith1d209d02013-05-23 01:49:11 +0000708ASTDeclReader::RedeclarableResult
709ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
710 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000711 RD->setHasFlexibleArrayMember(Record.readInt());
712 RD->setAnonymousStructOrUnion(Record.readInt());
713 RD->setHasObjectMember(Record.readInt());
714 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000715 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000716}
717
Sebastian Redlb3298c32010-08-18 23:56:48 +0000718void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000719 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000720 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000721}
722
Sebastian Redlb3298c32010-08-18 23:56:48 +0000723void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000724 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000725 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000726 ECD->setInitExpr(Record.readExpr());
727 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000728 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000729}
730
Sebastian Redlb3298c32010-08-18 23:56:48 +0000731void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000732 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000733 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000734 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000735 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000736 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000737 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000738 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000739 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000740}
741
Sebastian Redlb3298c32010-08-18 23:56:48 +0000742void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000743 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000744 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000745
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000746 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000747 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000748
Douglas Gregorb2585692012-01-04 17:13:46 +0000749 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
750 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000751
David L. Jonesbe1557a2016-12-21 00:17:49 +0000752 FD->SClass = (StorageClass)Record.readInt();
753 FD->IsInline = Record.readInt();
754 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000755 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000756 FD->IsVirtualAsWritten = Record.readInt();
757 FD->IsPure = Record.readInt();
758 FD->HasInheritedPrototype = Record.readInt();
759 FD->HasWrittenPrototype = Record.readInt();
760 FD->IsDeleted = Record.readInt();
761 FD->IsTrivial = Record.readInt();
762 FD->IsDefaulted = Record.readInt();
763 FD->IsExplicitlyDefaulted = Record.readInt();
764 FD->HasImplicitReturnZero = Record.readInt();
765 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000766 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000767 FD->HasSkippedBody = Record.readInt();
768 FD->IsLateTemplateParsed = Record.readInt();
769 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000770 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000771
David L. Jonesbe1557a2016-12-21 00:17:49 +0000772 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000773 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000774 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000775 break;
776 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000777 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000778 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000779 break;
780 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000781 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000782 TemplateSpecializationKind TSK =
783 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000784 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000785 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000786 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000787 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000788 break;
789 }
790 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000791 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000792 TemplateSpecializationKind TSK =
793 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000794
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000795 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000796 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000797 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000798
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000799 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000800 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000801 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000802 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000803 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000804 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000805 TemplArgLocs.reserve(NumTemplateArgLocs);
806 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000807 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000808
809 LAngleLoc = ReadSourceLocation();
810 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000811 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000812
813 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000814
Douglas Gregor4163aca2011-09-09 21:34:22 +0000815 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000816 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000817 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000818 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000819 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000820 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000821 FunctionTemplateSpecializationInfo *FTInfo
822 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
823 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000824 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
825 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000826 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000827 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000828
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000829 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000830 // The template that contains the specializations set. It's not safe to
831 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000832 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000833 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
834 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
835 // FunctionTemplateSpecializationInfo's Profile().
836 // We avoid getASTContext because a decl in the parent hierarchy may
837 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000838 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000839 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000840 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000841 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000842 FunctionTemplateSpecializationInfo *ExistingInfo =
843 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000844 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000845 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
846 else {
847 assert(Reader.getContext().getLangOpts().Modules &&
848 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000849 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000850 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000851 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000852 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000853 }
854 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
855 // Templates.
856 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000857 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000858 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000859 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
860
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000861 // Templates args.
862 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000863 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000864 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000865 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000866 TemplArgs.setLAngleLoc(ReadSourceLocation());
867 TemplArgs.setRAngleLoc(ReadSourceLocation());
868
Douglas Gregor4163aca2011-09-09 21:34:22 +0000869 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000870 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000871 // These are not merged; we don't need to merge redeclarations of dependent
872 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000873 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000874 }
875 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000876
Chris Lattnerca025db2010-05-07 21:43:38 +0000877 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000878 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000879 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000880 Params.reserve(NumParams);
881 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000882 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000883 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000884}
885
Sebastian Redlb3298c32010-08-18 23:56:48 +0000886void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000887 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000888 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000889 // Load the body on-demand. Most clients won't care, because method
890 // definitions rarely show up in headers.
891 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
892 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000893 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
894 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000895 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000896 MD->setInstanceMethod(Record.readInt());
897 MD->setVariadic(Record.readInt());
898 MD->setPropertyAccessor(Record.readInt());
899 MD->setDefined(Record.readInt());
900 MD->IsOverriding = Record.readInt();
901 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000902
David L. Jonesbe1557a2016-12-21 00:17:49 +0000903 MD->IsRedeclaration = Record.readInt();
904 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000905 if (MD->HasRedeclaration)
906 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000907 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000908
David L. Jonesbe1557a2016-12-21 00:17:49 +0000909 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
910 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
911 MD->SetRelatedResultType(Record.readInt());
912 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000913 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
914 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000915 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000916 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000917 Params.reserve(NumParams);
918 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000919 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000920
David L. Jonesbe1557a2016-12-21 00:17:49 +0000921 MD->SelLocsKind = Record.readInt();
922 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000923 SmallVector<SourceLocation, 16> SelLocs;
924 SelLocs.reserve(NumStoredSelLocs);
925 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000926 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000927
928 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000929}
930
Douglas Gregor85f3f952015-07-07 03:57:15 +0000931void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000932 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000933
David L. Jonesbe1557a2016-12-21 00:17:49 +0000934 D->Variance = Record.readInt();
935 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000936 D->VarianceLoc = ReadSourceLocation();
937 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000938}
939
Sebastian Redlb3298c32010-08-18 23:56:48 +0000940void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000941 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000942 CD->setAtStartLoc(ReadSourceLocation());
943 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000944}
945
Douglas Gregor85f3f952015-07-07 03:57:15 +0000946ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000947 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000948 if (numParams == 0)
949 return nullptr;
950
951 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
952 typeParams.reserve(numParams);
953 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000954 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000955 if (!typeParam)
956 return nullptr;
957
958 typeParams.push_back(typeParam);
959 }
960
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000961 SourceLocation lAngleLoc = ReadSourceLocation();
962 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000963
964 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
965 typeParams, rAngleLoc);
966}
967
Manman Renec315f12016-09-09 23:48:27 +0000968void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000969 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000970 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000971 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000972
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000973 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000974 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000975
Manman Renec315f12016-09-09 23:48:27 +0000976 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000977 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000978 SmallVector<ObjCProtocolDecl *, 16> Protocols;
979 Protocols.reserve(NumProtocols);
980 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000981 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000982 SmallVector<SourceLocation, 16> ProtoLocs;
983 ProtoLocs.reserve(NumProtocols);
984 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000985 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +0000986 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
987 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000988
Manman Renec315f12016-09-09 23:48:27 +0000989 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000990 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000991 Protocols.clear();
992 Protocols.reserve(NumProtocols);
993 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000994 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000995 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
996 Reader.getContext());
997}
998
999void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1000 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1001 // FIXME: odr checking?
1002}
1003
Sebastian Redlb3298c32010-08-18 23:56:48 +00001004void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001005 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001006 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001007 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001008 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001009
1010 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001011 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001012 // Read the definition.
1013 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001014
David L. Jonesbe1557a2016-12-21 00:17:49 +00001015 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001016 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1017 if (Canon->Data.getPointer()) {
1018 // If we already have a definition, keep the definition invariant and
1019 // merge the data.
1020 MergeDefinitionData(Canon, std::move(ID->data()));
1021 ID->Data = Canon->Data;
1022 } else {
1023 // Set the definition data of the canonical declaration, so other
1024 // redeclarations will see it.
1025 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001026
Manman Renec315f12016-09-09 23:48:27 +00001027 // We will rebuild this list lazily.
1028 ID->setIvarList(nullptr);
1029 }
Craig Toppera13603a2014-05-22 05:54:18 +00001030
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001031 // Note that we have deserialized a definition.
1032 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001033
Douglas Gregor404cdde2012-01-27 01:47:08 +00001034 // Note that we've loaded this Objective-C class.
1035 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001036 } else {
1037 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001038 }
Chris Lattner487412d2009-04-27 05:27:42 +00001039}
1040
Sebastian Redlb3298c32010-08-18 23:56:48 +00001041void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001042 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001043 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001044 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001045 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001046 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001047 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001048}
1049
Sebastian Redlb3298c32010-08-18 23:56:48 +00001050void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
Douglas Gregorda389302012-01-01 21:47:52 +00001051 RedeclarableResult Redecl = VisitRedeclarable(PD);
Chris Lattner487412d2009-04-27 05:27:42 +00001052 VisitObjCContainerDecl(PD);
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001053 mergeRedeclarable(PD, Redecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001054
David L. Jonesbe1557a2016-12-21 00:17:49 +00001055 if (Record.readInt()) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00001056 // Read the definition.
1057 PD->allocateDefinitionData();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001058
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001059 // Set the definition data of the canonical declaration, so other
1060 // redeclarations will see it.
1061 PD->getCanonicalDecl()->Data = PD->Data;
1062
David L. Jonesbe1557a2016-12-21 00:17:49 +00001063 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001064 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1065 ProtoRefs.reserve(NumProtoRefs);
1066 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001067 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001068 SmallVector<SourceLocation, 16> ProtoLocs;
1069 ProtoLocs.reserve(NumProtoRefs);
1070 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001071 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001072 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1073 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001074
Douglas Gregora715bff2012-01-01 19:51:50 +00001075 // Note that we have deserialized a definition.
1076 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001077 } else {
1078 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001079 }
Chris Lattner487412d2009-04-27 05:27:42 +00001080}
1081
Sebastian Redlb3298c32010-08-18 23:56:48 +00001082void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001083 VisitFieldDecl(FD);
1084}
1085
Sebastian Redlb3298c32010-08-18 23:56:48 +00001086void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001087 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001088 CD->setCategoryNameLoc(ReadSourceLocation());
1089 CD->setIvarLBraceLoc(ReadSourceLocation());
1090 CD->setIvarRBraceLoc(ReadSourceLocation());
1091
Douglas Gregor404cdde2012-01-27 01:47:08 +00001092 // Note that this category has been deserialized. We do this before
1093 // deserializing the interface declaration, so that it will consider this
1094 /// category.
1095 Reader.CategoriesDeserialized.insert(CD);
1096
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001097 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001098 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001099 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001100 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001101 ProtoRefs.reserve(NumProtoRefs);
1102 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001103 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001104 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001105 ProtoLocs.reserve(NumProtoRefs);
1106 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001107 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001108 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001109 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001110}
1111
Sebastian Redlb3298c32010-08-18 23:56:48 +00001112void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001113 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001114 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001115}
1116
Sebastian Redlb3298c32010-08-18 23:56:48 +00001117void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001118 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001119 D->setAtLoc(ReadSourceLocation());
1120 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001121 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001122 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001123 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001124 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001125 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001126 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001127 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001128 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001129 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001130 DeclarationName GetterName = Record.readDeclarationName();
1131 SourceLocation GetterLoc = ReadSourceLocation();
1132 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1133 DeclarationName SetterName = Record.readDeclarationName();
1134 SourceLocation SetterLoc = ReadSourceLocation();
1135 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001136 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1137 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1138 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001139}
1140
Sebastian Redlb3298c32010-08-18 23:56:48 +00001141void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001142 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001143 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001144}
1145
Sebastian Redlb3298c32010-08-18 23:56:48 +00001146void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001147 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001148 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001149}
1150
Sebastian Redlb3298c32010-08-18 23:56:48 +00001151void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001152 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001153 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1154 D->SuperLoc = ReadSourceLocation();
1155 D->setIvarLBraceLoc(ReadSourceLocation());
1156 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001157 D->setHasNonZeroConstructors(Record.readInt());
1158 D->setHasDestructors(Record.readInt());
1159 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001160 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001161 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001162}
1163
Sebastian Redlb3298c32010-08-18 23:56:48 +00001164void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001165 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001166 D->setAtLoc(ReadSourceLocation());
1167 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1168 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1169 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001170 D->setGetterCXXConstructor(Record.readExpr());
1171 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001172}
1173
Sebastian Redlb3298c32010-08-18 23:56:48 +00001174void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001175 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001176 FD->Mutable = Record.readInt();
1177 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001178 FD->InitStorage.setInt(
1179 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1180 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001181 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001182 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001183 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001184 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001185 }
Richard Smith2b013182012-06-10 03:12:00 +00001186 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001187 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001188 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001189 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001190 }
Richard Smith0b87e072013-10-07 08:02:11 +00001191 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001192}
1193
John McCall5e77d762013-04-16 07:28:30 +00001194void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1195 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001196 PD->GetterId = Record.getIdentifierInfo();
1197 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001198}
1199
Francois Pichet783dd6e2010-11-21 06:08:52 +00001200void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1201 VisitValueDecl(FD);
1202
David L. Jonesbe1557a2016-12-21 00:17:49 +00001203 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001204 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001205 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001206
1207 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001208 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001209
1210 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001211}
1212
Larisse Voufo39a1e502013-08-06 01:03:05 +00001213ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001214 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001215 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001216
David L. Jonesbe1557a2016-12-21 00:17:49 +00001217 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1218 VD->VarDeclBits.TSCSpec = Record.readInt();
1219 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001220 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001221 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1222 Record.readInt();
1223 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1224 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1225 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1226 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1227 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1228 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1229 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1230 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1231 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001232 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001233 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001234 VD->setCachedLinkage(VarLinkage);
1235
1236 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001237 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001238 VD->getLexicalDeclContext()->isFunctionOrMethod())
1239 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001240
David L. Jonesbe1557a2016-12-21 00:17:49 +00001241 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001242 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001243 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001244 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1245 Eval->CheckedICE = true;
1246 Eval->IsICE = Val == 3;
1247 }
1248 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001249
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001250 enum VarKind {
1251 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1252 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001253 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001254 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001255 // Only true variables (not parameters or implicit parameters) can be
1256 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001257 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1258 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001259 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001260 break;
1261 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001262 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001263 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001264 break;
1265 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001266 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001267 TemplateSpecializationKind TSK =
1268 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001269 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001270 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001271 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001272 break;
1273 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001274 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001275
1276 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001277}
1278
Sebastian Redlb3298c32010-08-18 23:56:48 +00001279void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001280 VisitVarDecl(PD);
1281}
1282
Sebastian Redlb3298c32010-08-18 23:56:48 +00001283void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001284 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001285 unsigned isObjCMethodParam = Record.readInt();
1286 unsigned scopeDepth = Record.readInt();
1287 unsigned scopeIndex = Record.readInt();
1288 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001289 if (isObjCMethodParam) {
1290 assert(scopeDepth == 0);
1291 PD->setObjCMethodScopeInfo(scopeIndex);
1292 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1293 } else {
1294 PD->setScopeInfo(scopeDepth, scopeIndex);
1295 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001296 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1297 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1298 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001299 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001300
1301 // FIXME: If this is a redeclaration of a function from another module, handle
1302 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001303}
1304
Richard Smith7b76d812016-08-12 02:21:25 +00001305void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1306 VisitVarDecl(DD);
1307 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1308 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001309 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001310}
1311
1312void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1313 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001314 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001315}
1316
Sebastian Redlb3298c32010-08-18 23:56:48 +00001317void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001318 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001319 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001320 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001321}
1322
Sebastian Redlb3298c32010-08-18 23:56:48 +00001323void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001324 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001325 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001326 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001327 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001328 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001329 Params.reserve(NumParams);
1330 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001331 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001332 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001333
David L. Jonesbe1557a2016-12-21 00:17:49 +00001334 BD->setIsVariadic(Record.readInt());
1335 BD->setBlockMissingReturnType(Record.readInt());
1336 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001337
David L. Jonesbe1557a2016-12-21 00:17:49 +00001338 bool capturesCXXThis = Record.readInt();
1339 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001340 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001341 captures.reserve(numCaptures);
1342 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001343 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001344 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001345 bool byRef = (flags & 1);
1346 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001347 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001348
1349 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1350 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001351 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001352}
1353
Ben Langmuirce914fc2013-05-03 19:20:19 +00001354void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1355 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001356 unsigned ContextParamPos = Record.readInt();
1357 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001358 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001359 for (unsigned I = 0; I < CD->NumParams; ++I) {
1360 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001361 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001362 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001363 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001364 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001365}
1366
Sebastian Redlb3298c32010-08-18 23:56:48 +00001367void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001368 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001369 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001370 D->setExternLoc(ReadSourceLocation());
1371 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001372}
1373
Richard Smith8df390f2016-09-08 23:14:54 +00001374void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1375 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001376 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001377}
1378
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001379void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1380 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001381 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001382}
1383
Sebastian Redlb3298c32010-08-18 23:56:48 +00001384void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001385 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001386 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001387 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001388 D->LocStart = ReadSourceLocation();
1389 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001390
Richard Smith15e32fd2015-03-17 02:23:11 +00001391 // Defer loading the anonymous namespace until we've finished merging
1392 // this namespace; loading it might load a later declaration of the
1393 // same namespace, and we have an invariant that older declarations
1394 // get merged before newer ones try to merge.
1395 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001396 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001397 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001398 } else {
1399 // Link this namespace back to the first declaration, which has already
1400 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001401 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001402 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001403
1404 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001405
1406 if (AnonNamespace) {
1407 // Each module has its own anonymous namespace, which is disjoint from
1408 // any other module's anonymous namespaces, so don't attach the anonymous
1409 // namespace at all.
1410 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001411 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001412 D->setAnonymousNamespace(Anon);
1413 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001414}
1415
Sebastian Redlb3298c32010-08-18 23:56:48 +00001416void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001417 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001418 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001419 D->NamespaceLoc = ReadSourceLocation();
1420 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001421 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001422 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001423 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001424}
1425
Sebastian Redlb3298c32010-08-18 23:56:48 +00001426void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001427 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001428 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001429 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001430 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1431 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001432 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001433 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001434 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001435 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001436}
1437
Richard Smith151c4562016-12-20 21:35:28 +00001438void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1439 VisitNamedDecl(D);
1440 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1441 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1442 for (unsigned I = 0; I != D->NumExpansions; ++I)
1443 Expansions[I] = ReadDeclAs<NamedDecl>();
1444 mergeMergeable(D);
1445}
1446
Sebastian Redlb3298c32010-08-18 23:56:48 +00001447void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001448 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001449 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001450 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1451 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1452 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001453 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001454 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001455 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001456}
1457
Richard Smith5179eb72016-06-28 19:03:57 +00001458void ASTDeclReader::VisitConstructorUsingShadowDecl(
1459 ConstructorUsingShadowDecl *D) {
1460 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001461 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1462 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001463 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001464}
1465
Sebastian Redlb3298c32010-08-18 23:56:48 +00001466void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001467 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001468 D->UsingLoc = ReadSourceLocation();
1469 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001470 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001471 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1472 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001473}
1474
Sebastian Redlb3298c32010-08-18 23:56:48 +00001475void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001476 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001477 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001478 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001479 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001480 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001481 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001482}
1483
Sebastian Redlb3298c32010-08-18 23:56:48 +00001484void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001485 UnresolvedUsingTypenameDecl *D) {
1486 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001487 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001488 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001489 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001490 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001491}
1492
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001493void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001494 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001495 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001496 Data.UserDeclaredConstructor = Record.readInt();
1497 Data.UserDeclaredSpecialMembers = Record.readInt();
1498 Data.Aggregate = Record.readInt();
1499 Data.PlainOldData = Record.readInt();
1500 Data.Empty = Record.readInt();
1501 Data.Polymorphic = Record.readInt();
1502 Data.Abstract = Record.readInt();
1503 Data.IsStandardLayout = Record.readInt();
1504 Data.HasNoNonEmptyBases = Record.readInt();
1505 Data.HasPrivateFields = Record.readInt();
1506 Data.HasProtectedFields = Record.readInt();
1507 Data.HasPublicFields = Record.readInt();
1508 Data.HasMutableFields = Record.readInt();
1509 Data.HasVariantMembers = Record.readInt();
1510 Data.HasOnlyCMembers = Record.readInt();
1511 Data.HasInClassInitializer = Record.readInt();
1512 Data.HasUninitializedReferenceMember = Record.readInt();
1513 Data.HasUninitializedFields = Record.readInt();
1514 Data.HasInheritedConstructor = Record.readInt();
1515 Data.HasInheritedAssignment = Record.readInt();
1516 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1517 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1518 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1519 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1520 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1521 Data.DefaultedDestructorIsDeleted = Record.readInt();
1522 Data.HasTrivialSpecialMembers = Record.readInt();
1523 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1524 Data.HasIrrelevantDestructor = Record.readInt();
1525 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1526 Data.HasDefaultedDefaultConstructor = Record.readInt();
1527 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1528 Data.HasConstexprDefaultConstructor = Record.readInt();
1529 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1530 Data.ComputedVisibleConversions = Record.readInt();
1531 Data.UserProvidedDefaultConstructor = Record.readInt();
1532 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001533 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1534 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001535 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1536 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1537 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001538 Data.ODRHash = Record.readInt();
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)
Richard Trieub6adf542017-02-18 02:09:28 +00001676 MATCH_FIELD(ODRHash)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001677#undef OR_FIELD
1678#undef MATCH_FIELD
1679
1680 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1681 DetectedOdrViolation = true;
1682 // FIXME: Issue a diagnostic if the base classes don't match when we come
1683 // to lazily load them.
1684
1685 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1686 // match when we come to lazily load them.
1687 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1688 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1689 DD.ComputedVisibleConversions = true;
1690 }
1691
1692 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1693 // lazily load it.
1694
1695 if (DD.IsLambda) {
1696 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1697 // when they occur within the body of a function template specialization).
1698 }
1699
1700 if (DetectedOdrViolation)
1701 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1702}
1703
Richard Smith2a9e5c52015-02-03 03:32:14 +00001704void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001705 struct CXXRecordDecl::DefinitionData *DD;
1706 ASTContext &C = Reader.getContext();
1707
1708 // Determine whether this is a lambda closure type, so that we can
1709 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001710 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001711 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001712 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001713 LCD_None);
1714 else
1715 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1716
David Blaikie1ac9c982017-04-11 21:13:37 +00001717 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001718
Richard Smith5156c382015-01-22 01:41:56 +00001719 // We might already have a definition for this record. This can happen either
1720 // because we're reading an update record, or because we've already done some
1721 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001722 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001723 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001724 MergeDefinitionData(Canon, std::move(*DD));
1725 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001726 return;
1727 }
1728
Richard Smith97100e32015-06-20 00:22:34 +00001729 // Mark this declaration as being a definition.
1730 D->IsCompleteDefinition = true;
1731 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001732
Richard Smith97100e32015-06-20 00:22:34 +00001733 // If this is not the first declaration or is an update record, we can have
1734 // other redeclarations already. Make a note that we need to propagate the
1735 // DefinitionData pointer onto them.
1736 if (Update || Canon != D) {
1737 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001738 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001739 }
1740}
1741
Richard Smith1d209d02013-05-23 01:49:11 +00001742ASTDeclReader::RedeclarableResult
1743ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1744 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001745
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001746 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001747
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001748 enum CXXRecKind {
1749 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1750 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001751 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001752 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001753 // Merged when we merge the folding set entry in the primary template.
1754 if (!isa<ClassTemplateSpecializationDecl>(D))
1755 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001756 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001757 case CXXRecTemplate: {
1758 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001759 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001760 D->TemplateOrInstantiation = Template;
1761 if (!Template->getTemplatedDecl()) {
1762 // We've not actually loaded the ClassTemplateDecl yet, because we're
1763 // currently being loaded as its pattern. Rely on it to set up our
1764 // TypeForDecl (see VisitClassTemplateDecl).
1765 //
1766 // Beware: we do not yet know our canonical declaration, and may still
1767 // get merged once the surrounding class template has got off the ground.
1768 TypeIDForTypeDecl = 0;
1769 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001770 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001771 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001772 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001773 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001774 TemplateSpecializationKind TSK =
1775 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001776 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001777 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1778 MSI->setPointOfInstantiation(POI);
1779 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001780 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001781 break;
1782 }
1783 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001784
David L. Jonesbe1557a2016-12-21 00:17:49 +00001785 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001786 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001787 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001788 else
1789 // Propagate DefinitionData pointer from the canonical declaration.
1790 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1791
Richard Smith676c4042013-08-29 23:59:27 +00001792 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001793 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001794 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001795 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001796 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001797 // FIXME: This is wrong for the ARM ABI, where some other module may have
1798 // made this function no longer be a key function. We need an update
1799 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001800 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001801 }
Richard Smith1d209d02013-05-23 01:49:11 +00001802
1803 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001804}
1805
Richard Smithbc491202017-02-17 20:05:37 +00001806void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1807 VisitFunctionDecl(D);
1808}
1809
Sebastian Redlb3298c32010-08-18 23:56:48 +00001810void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001811 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001812
David L. Jonesbe1557a2016-12-21 00:17:49 +00001813 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001814 if (D->isCanonicalDecl()) {
1815 while (NumOverridenMethods--) {
1816 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1817 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001818 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001819 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1820 }
1821 } else {
1822 // We don't care about which declarations this used to override; we get
1823 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001824 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001825 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001826}
1827
Sebastian Redlb3298c32010-08-18 23:56:48 +00001828void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001829 // We need the inherited constructor information to merge the declaration,
1830 // so we have to read it before we call VisitCXXMethodDecl.
1831 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001832 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1833 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001834 *D->getTrailingObjects<InheritedConstructor>() =
1835 InheritedConstructor(Shadow, Ctor);
1836 }
1837
Chris Lattnerca025db2010-05-07 21:43:38 +00001838 VisitCXXMethodDecl(D);
1839}
1840
Sebastian Redlb3298c32010-08-18 23:56:48 +00001841void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001842 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001843
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001844 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001845 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1846 // FIXME: Check consistency if we have an old and new operator delete.
1847 if (!Canon->OperatorDelete)
1848 Canon->OperatorDelete = OperatorDelete;
1849 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001850}
1851
Sebastian Redlb3298c32010-08-18 23:56:48 +00001852void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001853 VisitCXXMethodDecl(D);
1854}
1855
Douglas Gregorba345522011-12-02 23:23:56 +00001856void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1857 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001858 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001859 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001860 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001861 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001862 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001863 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001864}
1865
Sebastian Redlb3298c32010-08-18 23:56:48 +00001866void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001867 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001868 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001869}
1870
Sebastian Redlb3298c32010-08-18 23:56:48 +00001871void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001872 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001873 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001874 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001875 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001876 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001877 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001878 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001879 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001880 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001881 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001882 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001883}
1884
Sebastian Redlb3298c32010-08-18 23:56:48 +00001885void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001886 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001887 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001888 D->NumParams = NumParams;
1889 D->Params = new TemplateParameterList*[NumParams];
1890 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001891 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001892 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001893 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001894 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001895 D->Friend = GetTypeSourceInfo();
1896 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001897}
1898
Richard Smithf17fdbd2014-04-24 02:25:27 +00001899DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001900 VisitNamedDecl(D);
1901
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001902 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001903 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001904 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001905 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001906 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001907
Richard Smithf17fdbd2014-04-24 02:25:27 +00001908 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001909}
1910
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001911ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001912ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001913 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001914
Douglas Gregor68444de2012-01-14 15:13:49 +00001915 // Make sure we've allocated the Common pointer first. We do this before
1916 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1917 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1918 if (!CanonD->Common) {
1919 CanonD->Common = CanonD->newCommon(Reader.getContext());
1920 Reader.PendingDefinitions.insert(CanonD);
1921 }
1922 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001923
Douglas Gregor68444de2012-01-14 15:13:49 +00001924 // If this is the first declaration of the template, fill in the information
1925 // for the 'common' pointer.
1926 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001927 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001928 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001929 assert(RTD->getKind() == D->getKind() &&
1930 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001931 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001932 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001933 D->setMemberSpecialization();
1934 }
1935 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001936
Richard Smithf17fdbd2014-04-24 02:25:27 +00001937 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001938 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001939
Richard Smithf17fdbd2014-04-24 02:25:27 +00001940 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001941
Richard Smithd46d6de2013-10-13 23:50:45 +00001942 // If we merged the template with a prior declaration chain, merge the common
1943 // pointer.
1944 // FIXME: Actually merge here, don't just overwrite.
1945 D->Common = D->getCanonicalDecl()->Common;
1946
Douglas Gregor68444de2012-01-14 15:13:49 +00001947 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001948}
1949
Richard Smith0b884372015-02-27 00:25:58 +00001950static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1951 SmallVectorImpl<DeclID> &IDs) {
1952 assert(!IDs.empty() && "no IDs to add to list");
Richard Smith36befce2015-02-28 01:45:19 +00001953 if (Old) {
1954 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1955 std::sort(IDs.begin(), IDs.end());
1956 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1957 }
Richard Smith0b884372015-02-27 00:25:58 +00001958
Richard Smith36befce2015-02-28 01:45:19 +00001959 auto *Result = new (Context) DeclID[1 + IDs.size()];
1960 *Result = IDs.size();
1961 std::copy(IDs.begin(), IDs.end(), Result + 1);
Richard Smith0b884372015-02-27 00:25:58 +00001962 return Result;
1963}
1964
Sebastian Redlb3298c32010-08-18 23:56:48 +00001965void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001966 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001967
Douglas Gregor68444de2012-01-14 15:13:49 +00001968 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001969 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1970 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001971 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001972 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001973
Richard Smith0b884372015-02-27 00:25:58 +00001974 if (!SpecIDs.empty()) {
1975 auto *CommonPtr = D->getCommonPtr();
1976 CommonPtr->LazySpecializations = newDeclIDList(
1977 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001978 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001979 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00001980
1981 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1982 // We were loaded before our templated declaration was. We've not set up
1983 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1984 // it now.
1985 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00001986 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00001987 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001988}
1989
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001990void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
1991 llvm_unreachable("BuiltinTemplates are not serialized");
1992}
1993
Larisse Voufo30616382013-08-23 22:21:36 +00001994/// TODO: Unify with ClassTemplateDecl version?
1995/// May require unifying ClassTemplateDecl and
1996/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00001997void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
1998 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
1999
2000 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002001 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002002 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002003 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002004 ReadDeclIDList(SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002005
Richard Smith0b884372015-02-27 00:25:58 +00002006 if (!SpecIDs.empty()) {
2007 auto *CommonPtr = D->getCommonPtr();
2008 CommonPtr->LazySpecializations = newDeclIDList(
2009 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002010 }
2011 }
2012}
2013
Richard Smith1d209d02013-05-23 01:49:11 +00002014ASTDeclReader::RedeclarableResult
2015ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2016 ClassTemplateSpecializationDecl *D) {
2017 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002018
Douglas Gregor4163aca2011-09-09 21:34:22 +00002019 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002020 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002021 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002022 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002023 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002024 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002025 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002026 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002027 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002028 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2029 = new (C) ClassTemplateSpecializationDecl::
2030 SpecializedPartialSpecialization();
2031 PS->PartialSpecialization
2032 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2033 PS->TemplateArgs = ArgList;
2034 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002035 }
2036 }
2037
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002038 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002039 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002040 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002041 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002042 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002043
David L. Jonesbe1557a2016-12-21 00:17:49 +00002044 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002045 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002046 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002047 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002048 // Set this as, or find, the canonical declaration for this specialization
2049 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002050 if (ClassTemplatePartialSpecializationDecl *Partial =
2051 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002052 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002053 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002054 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002055 CanonSpec =
2056 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2057 }
2058 // If there was already a canonical specialization, merge into it.
2059 if (CanonSpec != D) {
2060 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2061
2062 // This declaration might be a definition. Merge with any existing
2063 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002064 if (auto *DDD = D->DefinitionData) {
2065 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002066 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002067 else
Richard Smith053f6c62014-05-16 23:01:30 +00002068 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002069 }
Richard Smith547864d2014-07-11 18:22:58 +00002070 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002071 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002072 }
2073 }
Richard Smith1d209d02013-05-23 01:49:11 +00002074
Richard Smithd55889a2013-09-09 16:55:27 +00002075 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002076 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002077 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2078 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2079 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002080 ExplicitInfo->ExternLoc = ReadSourceLocation();
2081 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002082 D->ExplicitInfo = ExplicitInfo;
2083 }
2084
Richard Smith1d209d02013-05-23 01:49:11 +00002085 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002086}
2087
Sebastian Redlb3298c32010-08-18 23:56:48 +00002088void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002089 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002090 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002091
David L. Jonesb6a8f022016-12-21 04:34:52 +00002092 D->TemplateParams = Record.readTemplateParameterList();
2093 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002094
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002095 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002096 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002097 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002098 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002099 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002100 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002101}
2102
Sebastian Redlb7448632011-08-31 13:59:56 +00002103void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2104 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002105 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002106 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002107}
2108
Sebastian Redlb3298c32010-08-18 23:56:48 +00002109void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002110 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002111
Douglas Gregor68444de2012-01-14 15:13:49 +00002112 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002113 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002114 SmallVector<serialization::DeclID, 32> SpecIDs;
2115 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002116
Richard Smith0b884372015-02-27 00:25:58 +00002117 if (!SpecIDs.empty()) {
2118 auto *CommonPtr = D->getCommonPtr();
2119 CommonPtr->LazySpecializations = newDeclIDList(
2120 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Richard Smithfeb3e1a2013-06-28 04:37:53 +00002121 }
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002122 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002123}
2124
Larisse Voufo30616382013-08-23 22:21:36 +00002125/// TODO: Unify with ClassTemplateSpecializationDecl version?
2126/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2127/// VarTemplate(Partial)SpecializationDecl with a new data
2128/// structure Template(Partial)SpecializationDecl, and
2129/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002130ASTDeclReader::RedeclarableResult
2131ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2132 VarTemplateSpecializationDecl *D) {
2133 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2134
2135 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002136 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002137 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2138 D->SpecializedTemplate = VTD;
2139 } else {
2140 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002141 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002142 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002143 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002144 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2145 new (C)
2146 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2147 PS->PartialSpecialization =
2148 cast<VarTemplatePartialSpecializationDecl>(InstD);
2149 PS->TemplateArgs = ArgList;
2150 D->SpecializedTemplate = PS;
2151 }
2152 }
2153
2154 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002155 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002156 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2157 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2158 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002159 ExplicitInfo->ExternLoc = ReadSourceLocation();
2160 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002161 D->ExplicitInfo = ExplicitInfo;
2162 }
2163
2164 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002165 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002166 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002167 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002168 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002169
David L. Jonesbe1557a2016-12-21 00:17:49 +00002170 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002171 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002172 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002173 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002174 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002175 if (VarTemplatePartialSpecializationDecl *Partial =
2176 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002177 CanonPattern->getCommonPtr()->PartialSpecializations
2178 .GetOrInsertNode(Partial);
2179 } else {
2180 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2181 }
2182 }
2183 }
2184
2185 return Redecl;
2186}
2187
Larisse Voufo30616382013-08-23 22:21:36 +00002188/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2189/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2190/// VarTemplate(Partial)SpecializationDecl with a new data
2191/// structure Template(Partial)SpecializationDecl, and
2192/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002193void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2194 VarTemplatePartialSpecializationDecl *D) {
2195 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2196
David L. Jonesb6a8f022016-12-21 04:34:52 +00002197 D->TemplateParams = Record.readTemplateParameterList();
2198 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002199
2200 // These are read/set from/to the first declaration.
2201 if (ThisDeclID == Redecl.getFirstID()) {
2202 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002203 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002204 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002205 }
2206}
2207
Sebastian Redlb3298c32010-08-18 23:56:48 +00002208void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002209 VisitTypeDecl(D);
2210
David L. Jonesbe1557a2016-12-21 00:17:49 +00002211 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002212
David L. Jonesbe1557a2016-12-21 00:17:49 +00002213 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002214 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002215}
2216
Sebastian Redlb3298c32010-08-18 23:56:48 +00002217void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002218 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002219 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002220 D->setDepth(Record.readInt());
2221 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002222 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002223 auto TypesAndInfos =
2224 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002225 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002226 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002227 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002228 }
2229 } else {
2230 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002231 D->ParameterPack = Record.readInt();
2232 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002233 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002234 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002235}
2236
Sebastian Redlb3298c32010-08-18 23:56:48 +00002237void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002238 VisitTemplateDecl(D);
2239 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002240 D->setDepth(Record.readInt());
2241 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002242 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002243 TemplateParameterList **Data =
2244 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002245 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2246 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002247 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002248 } else {
2249 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002250 D->ParameterPack = Record.readInt();
2251 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002252 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002253 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002254 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002255}
2256
Richard Smith3f1b5d02011-05-05 21:57:07 +00002257void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2258 VisitRedeclarableTemplateDecl(D);
2259}
2260
Sebastian Redlb3298c32010-08-18 23:56:48 +00002261void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002262 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002263 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002264 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002265 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002266 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002267}
2268
Michael Han84324352013-02-22 17:15:32 +00002269void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2270 VisitDecl(D);
2271}
2272
Mike Stump11289f42009-09-09 15:08:12 +00002273std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002274ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002275 uint64_t LexicalOffset = ReadLocalOffset();
2276 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002277 return std::make_pair(LexicalOffset, VisibleOffset);
2278}
2279
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002280template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002281ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002282ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002283 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002284 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002285
Richard Smith5fc18a92015-07-12 23:43:21 +00002286 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002287 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002288
Richard Smithd61d4ac2015-08-22 20:13:39 +00002289 uint64_t RedeclOffset = 0;
2290
Douglas Gregor358cd442012-01-15 16:58:34 +00002291 // 0 indicates that this declaration was the only declaration of its entity,
2292 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002293 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002294 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002295 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002296 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002297 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002298 // This declaration was the first local declaration, but may have imported
2299 // other declarations.
2300 IsKeyDecl = N == 1;
2301 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002302
Richard Smithfe620d22015-03-05 23:24:12 +00002303 // We have some declarations that must be before us in our redeclaration
2304 // chain. Read them now, and remember that we ought to merge with one of
2305 // them.
2306 // FIXME: Provide a known merge target to the second and subsequent such
2307 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002308 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002309 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002310
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002311 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002312 } else {
2313 // This declaration was not the first local declaration. Read the first
2314 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002315 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002316 }
2317
Douglas Gregor358cd442012-01-15 16:58:34 +00002318 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2319 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002320 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2321 // We temporarily set the first (canonical) declaration as the previous one
2322 // which is the one that matters and mark the real previous DeclID to be
2323 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002324 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002325 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002326 }
Richard Smithd8a83712015-08-22 01:47:18 +00002327
Richard Smithd8a83712015-08-22 01:47:18 +00002328 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002329
2330 // Note that we need to load local redeclarations of this decl and build a
2331 // decl chain for them. This must happen *after* we perform the preloading
2332 // above; this ensures that the redeclaration chain is built in the correct
2333 // order.
2334 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002335 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002336
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002337 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002338}
2339
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002340/// \brief Attempts to merge the given declaration (D) with another declaration
2341/// of the same entity.
2342template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002343void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002344 RedeclarableResult &Redecl,
2345 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002346 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002347 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002348 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002349
Richard Smith202850a2015-03-10 02:57:50 +00002350 // If we're not the canonical declaration, we don't need to merge.
2351 if (!DBase->isFirstDecl())
2352 return;
2353
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002354 T *D = static_cast<T*>(DBase);
2355
Richard Smith5a4737c2015-02-06 02:42:59 +00002356 if (auto *Existing = Redecl.getKnownMergeTarget())
2357 // We already know of an existing declaration we should merge with.
2358 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2359 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002360 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002361 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2362}
2363
2364/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2365/// We use this to put code in a template that will only be valid for certain
2366/// instantiations.
2367template<typename T> static T assert_cast(T t) { return t; }
2368template<typename T> static T assert_cast(...) {
2369 llvm_unreachable("bad assert_cast");
2370}
2371
2372/// \brief Merge together the pattern declarations from two template
2373/// declarations.
2374void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2375 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002376 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002377 auto *DPattern = D->getTemplatedDecl();
2378 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002379 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2380 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002381 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002382
Richard Smith547864d2014-07-11 18:22:58 +00002383 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2384 // Merge with any existing definition.
2385 // FIXME: This is duplicated in several places. Refactor.
2386 auto *ExistingClass =
2387 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002388 if (auto *DDD = DClass->DefinitionData) {
2389 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002390 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002391 } else {
2392 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002393 // We may have skipped this before because we thought that DClass
2394 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002395 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002396 }
2397 }
2398 DClass->DefinitionData = ExistingClass->DefinitionData;
2399
Richard Smithf17fdbd2014-04-24 02:25:27 +00002400 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2401 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002402 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002403 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2404 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2405 Result);
2406 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2407 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002408 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2409 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2410 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002411 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002412}
2413
2414/// \brief Attempts to merge the given declaration (D) with another declaration
2415/// of the same entity.
2416template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002417void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2418 RedeclarableResult &Redecl,
2419 DeclID TemplatePatternID) {
2420 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002421 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002422 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002423 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002424 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2425 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002426
Richard Smithd55889a2013-09-09 16:55:27 +00002427 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002428 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002429 // appropriate canonical declaration.
2430 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002431 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002432 ExistingCanon->Used |= D->Used;
2433 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002434
2435 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002436 // We cannot have loaded any redeclarations of this declaration yet, so
2437 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002438 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002439 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002440 assert_cast<NamespaceDecl*>(ExistingCanon));
2441
2442 // When we merge a template, merge its pattern.
2443 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2444 mergeTemplatePattern(
2445 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002446 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002447
Richard Smith5fc18a92015-07-12 23:43:21 +00002448 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002449 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002450 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002451 }
2452}
2453
Richard Smith0b87e072013-10-07 08:02:11 +00002454/// \brief Attempts to merge the given declaration (D) with another declaration
2455/// of the same entity, for the case where the entity is not actually
2456/// redeclarable. This happens, for instance, when merging the fields of
2457/// identical class definitions from two different modules.
2458template<typename T>
2459void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2460 // If modules are not available, there is no reason to perform this merge.
2461 if (!Reader.getContext().getLangOpts().Modules)
2462 return;
2463
Richard Smith01a73372013-10-15 22:02:41 +00002464 // ODR-based merging is only performed in C++. In C, identically-named things
2465 // in different translation units are not redeclarations (but may still have
2466 // compatible types).
2467 if (!Reader.getContext().getLangOpts().CPlusPlus)
2468 return;
2469
Richard Smith0b87e072013-10-07 08:02:11 +00002470 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2471 if (T *Existing = ExistingRes)
2472 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2473 Existing->getCanonicalDecl());
2474}
2475
Alexey Bataeva769e072013-03-22 06:34:35 +00002476void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2477 VisitDecl(D);
2478 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002479 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002480 Vars.reserve(NumVars);
2481 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002482 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002483 }
2484 D->setVars(Vars);
2485}
2486
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002487void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002488 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002489 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002490 D->setCombiner(Record.readExpr());
2491 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002492 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002493}
2494
Alexey Bataev4244be22016-02-11 05:35:55 +00002495void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002496 VisitVarDecl(D);
2497}
2498
Chris Lattner487412d2009-04-27 05:27:42 +00002499//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002500// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002501//===----------------------------------------------------------------------===//
2502
Chris Lattner8f63ab52009-04-27 06:01:06 +00002503/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002504void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2505 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002506 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002507 attr::Kind Kind = (attr::Kind)Record.readInt();
2508 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002509
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002510#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002511
2512 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002513 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002514 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002515}
2516
2517//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002518// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002519//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002520
2521/// \brief Note that we have loaded the declaration with the given
2522/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002523///
Chris Lattner487412d2009-04-27 05:27:42 +00002524/// This routine notes that this declaration has already been loaded,
2525/// so that future GetDecl calls will return this declaration rather
2526/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002527inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002528 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2529 DeclsLoaded[Index] = D;
2530}
2531
2532
2533/// \brief Determine whether the consumer will be interested in seeing
2534/// this declaration (via HandleTopLevelDecl).
2535///
2536/// This routine should return true for anything that might affect
2537/// code generation, e.g., inline function definitions, Objective-C
2538/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002539static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002540 // An ObjCMethodDecl is never considered as "interesting" because its
2541 // implementation container always is.
2542
Richard Smithdc1f0422016-07-20 19:10:16 +00002543 // An ImportDecl or VarDecl imported from a module will get emitted when
2544 // we import the relevant module.
Bruno Cardoso Lopes84df6e99b2017-03-01 19:18:42 +00002545 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && D->getImportedOwningModule() &&
2546 Ctx.DeclMustBeEmitted(D))
Richard Smithdc1f0422016-07-20 19:10:16 +00002547 return false;
2548
Douglas Gregor87d81242011-09-10 00:22:34 +00002549 if (isa<FileScopeAsmDecl>(D) ||
2550 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002551 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002552 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002553 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002554 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002555 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002556 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2557 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002558 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002559 return Var->isFileVarDecl() &&
2560 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002561 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002562 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002563
2564 if (auto *ES = D->getASTContext().getExternalSource())
2565 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2566 return true;
2567
Douglas Gregor87d81242011-09-10 00:22:34 +00002568 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002569}
2570
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002571/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002572ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002573ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002574 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2575 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002576 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002577 const DeclOffset &DOffs =
2578 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002579 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002580 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002581}
2582
Douglas Gregord32f0352011-07-22 06:10:01 +00002583ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002584 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002585 = GlobalBitOffsetsMap.find(GlobalOffset);
2586
2587 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2588 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2589}
2590
Douglas Gregorde3ef502011-11-30 23:21:26 +00002591uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002592 return LocalOffset + M.GlobalBitOffset;
2593}
2594
Richard Smithbf78e642013-06-24 22:51:00 +00002595static bool isSameTemplateParameterList(const TemplateParameterList *X,
2596 const TemplateParameterList *Y);
2597
2598/// \brief Determine whether two template parameters are similar enough
2599/// that they may be used in declarations of the same template.
2600static bool isSameTemplateParameter(const NamedDecl *X,
2601 const NamedDecl *Y) {
2602 if (X->getKind() != Y->getKind())
2603 return false;
2604
2605 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2606 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2607 return TX->isParameterPack() == TY->isParameterPack();
2608 }
2609
2610 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2611 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2612 return TX->isParameterPack() == TY->isParameterPack() &&
2613 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2614 }
2615
2616 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2617 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2618 return TX->isParameterPack() == TY->isParameterPack() &&
2619 isSameTemplateParameterList(TX->getTemplateParameters(),
2620 TY->getTemplateParameters());
2621}
2622
Richard Smith32952e12014-10-14 02:00:47 +00002623static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2624 if (auto *NS = X->getAsNamespace())
2625 return NS;
2626 if (auto *NAS = X->getAsNamespaceAlias())
2627 return NAS->getNamespace();
2628 return nullptr;
2629}
2630
2631static bool isSameQualifier(const NestedNameSpecifier *X,
2632 const NestedNameSpecifier *Y) {
2633 if (auto *NSX = getNamespace(X)) {
2634 auto *NSY = getNamespace(Y);
2635 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2636 return false;
2637 } else if (X->getKind() != Y->getKind())
2638 return false;
2639
2640 // FIXME: For namespaces and types, we're permitted to check that the entity
2641 // is named via the same tokens. We should probably do so.
2642 switch (X->getKind()) {
2643 case NestedNameSpecifier::Identifier:
2644 if (X->getAsIdentifier() != Y->getAsIdentifier())
2645 return false;
2646 break;
2647 case NestedNameSpecifier::Namespace:
2648 case NestedNameSpecifier::NamespaceAlias:
2649 // We've already checked that we named the same namespace.
2650 break;
2651 case NestedNameSpecifier::TypeSpec:
2652 case NestedNameSpecifier::TypeSpecWithTemplate:
2653 if (X->getAsType()->getCanonicalTypeInternal() !=
2654 Y->getAsType()->getCanonicalTypeInternal())
2655 return false;
2656 break;
2657 case NestedNameSpecifier::Global:
2658 case NestedNameSpecifier::Super:
2659 return true;
2660 }
2661
2662 // Recurse into earlier portion of NNS, if any.
2663 auto *PX = X->getPrefix();
2664 auto *PY = Y->getPrefix();
2665 if (PX && PY)
2666 return isSameQualifier(PX, PY);
2667 return !PX && !PY;
2668}
2669
Richard Smithbf78e642013-06-24 22:51:00 +00002670/// \brief Determine whether two template parameter lists are similar enough
2671/// that they may be used in declarations of the same template.
2672static bool isSameTemplateParameterList(const TemplateParameterList *X,
2673 const TemplateParameterList *Y) {
2674 if (X->size() != Y->size())
2675 return false;
2676
2677 for (unsigned I = 0, N = X->size(); I != N; ++I)
2678 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2679 return false;
2680
2681 return true;
2682}
2683
George Burgess IV95845082017-02-15 22:43:27 +00002684/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002685/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002686static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2687 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002688 // Note that pass_object_size attributes are represented in the function's
2689 // ExtParameterInfo, so we don't need to check them here.
2690
George Burgess IV95845082017-02-15 22:43:27 +00002691 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2692 // Since this is an equality check, we can ignore that enable_if attrs show up
2693 // in reverse order.
2694 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2695 AEnableIfs.push_back(EIA);
2696
2697 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2698 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2699 BEnableIfs.push_back(EIA);
2700
2701 // Two very common cases: either we have 0 enable_if attrs, or we have an
2702 // unequal number of enable_if attrs.
2703 if (AEnableIfs.empty() && BEnableIfs.empty())
2704 return true;
2705
2706 if (AEnableIfs.size() != BEnableIfs.size())
2707 return false;
2708
2709 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2710 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2711 Cand1ID.clear();
2712 Cand2ID.clear();
2713
2714 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2715 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2716 if (Cand1ID != Cand2ID)
2717 return false;
2718 }
2719
George Burgess IV95845082017-02-15 22:43:27 +00002720 return true;
2721}
2722
Douglas Gregor022857e2011-12-22 01:48:48 +00002723/// \brief Determine whether the two declarations refer to the same entity.
2724static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2725 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002726
Douglas Gregor022857e2011-12-22 01:48:48 +00002727 if (X == Y)
2728 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002729
Douglas Gregor022857e2011-12-22 01:48:48 +00002730 // Must be in the same context.
2731 if (!X->getDeclContext()->getRedeclContext()->Equals(
2732 Y->getDeclContext()->getRedeclContext()))
2733 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002734
2735 // Two typedefs refer to the same entity if they have the same underlying
2736 // type.
2737 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2738 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2739 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2740 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002741
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002742 // Must have the same kind.
2743 if (X->getKind() != Y->getKind())
2744 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002745
Douglas Gregorda389302012-01-01 21:47:52 +00002746 // Objective-C classes and protocols with the same name always match.
2747 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002748 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002749
2750 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002751 // No need to handle these here: we merge them when adding them to the
2752 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002753 return false;
2754 }
Richard Smithd55889a2013-09-09 16:55:27 +00002755
Douglas Gregor2009cee2012-01-03 22:46:00 +00002756 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002757 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2758 TagDecl *TagY = cast<TagDecl>(Y);
2759 return (TagX->getTagKind() == TagY->getTagKind()) ||
2760 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2761 TagX->getTagKind() == TTK_Interface) &&
2762 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2763 TagY->getTagKind() == TTK_Interface));
2764 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002765
Joao Matose9a3ed42012-08-31 22:18:20 +00002766 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002767 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2768 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002769 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2770 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002771 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2772 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2773 if (CtorX->getInheritedConstructor() &&
2774 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2775 CtorY->getInheritedConstructor().getConstructor()))
2776 return false;
2777 }
Richard Smitha54d3242017-03-08 23:00:26 +00002778 ASTContext &C = FuncX->getASTContext();
2779 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) {
2780 // We can get functions with different types on the redecl chain in C++17
2781 // if they have differing exception specifications and at least one of
2782 // the excpetion specs is unresolved.
2783 // FIXME: Do we need to check for C++14 deduced return types here too?
2784 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>();
2785 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>();
2786 if (C.getLangOpts().CPlusPlus1z && XFPT && YFPT &&
2787 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2788 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
2789 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(),
2790 FuncY->getType()))
2791 return true;
2792 return false;
2793 }
George Burgess IV95845082017-02-15 22:43:27 +00002794 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002795 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002796 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002797
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002798 // Variables with the same type and linkage match.
2799 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2800 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002801 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2802 ASTContext &C = VarX->getASTContext();
2803 if (C.hasSameType(VarX->getType(), VarY->getType()))
2804 return true;
2805
2806 // We can get decls with different types on the redecl chain. Eg.
2807 // template <typename T> struct S { static T Var[]; }; // #1
2808 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2809 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002810 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002811 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2812 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2813 if (!VarXTy || !VarYTy)
2814 return false;
2815 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2816 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2817 }
2818 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002819 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002820
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002821 // Namespaces with the same name and inlinedness match.
2822 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2823 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2824 return NamespaceX->isInline() == NamespaceY->isInline();
2825 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002826
Richard Smith8f8f05c2013-06-24 04:45:28 +00002827 // Identical template names and kinds match if their template parameter lists
2828 // and patterns match.
2829 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002830 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002831 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002832 TemplateY->getTemplatedDecl()) &&
2833 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2834 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002835 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002836
Richard Smith0b87e072013-10-07 08:02:11 +00002837 // Fields with the same name and the same type match.
2838 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2839 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002840 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002841 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2842 }
2843
Richard Smith8cbd8952015-08-04 02:05:09 +00002844 // Indirect fields with the same target field match.
2845 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2846 auto *IFDY = cast<IndirectFieldDecl>(Y);
2847 return IFDX->getAnonField()->getCanonicalDecl() ==
2848 IFDY->getAnonField()->getCanonicalDecl();
2849 }
2850
Richard Smith01a73372013-10-15 22:02:41 +00002851 // Enumerators with the same name match.
2852 if (isa<EnumConstantDecl>(X))
2853 // FIXME: Also check the value is odr-equivalent.
2854 return true;
2855
Richard Smithfd8634a2013-10-23 02:17:46 +00002856 // Using shadow declarations with the same target match.
2857 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2858 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2859 return USX->getTargetDecl() == USY->getTargetDecl();
2860 }
2861
Richard Smith32952e12014-10-14 02:00:47 +00002862 // Using declarations with the same qualifier match. (We already know that
2863 // the name matches.)
2864 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2865 auto *UY = cast<UsingDecl>(Y);
2866 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2867 UX->hasTypename() == UY->hasTypename() &&
2868 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2869 }
2870 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2871 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2872 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2873 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2874 }
2875 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2876 return isSameQualifier(
2877 UX->getQualifier(),
2878 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2879
Richard Smithf4634362014-09-03 23:11:22 +00002880 // Namespace alias definitions with the same target match.
2881 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2882 auto *NAY = cast<NamespaceAliasDecl>(Y);
2883 return NAX->getNamespace()->Equals(NAY->getNamespace());
2884 }
2885
Douglas Gregor022857e2011-12-22 01:48:48 +00002886 return false;
2887}
2888
Richard Smithd55889a2013-09-09 16:55:27 +00002889/// Find the context in which we should search for previous declarations when
2890/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002891DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2892 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002893 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2894 return ND->getOriginalNamespace();
2895
Richard Smith8a639892015-01-24 01:07:20 +00002896 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2897 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002898 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002899 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002900 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002901
2902 // If there's no definition yet, then DC's definition is added by an update
2903 // record, but we've not yet loaded that update record. In this case, we
2904 // commit to DC being the canonical definition now, and will fix this when
2905 // we load the update record.
2906 if (!DD) {
2907 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2908 RD->IsCompleteDefinition = true;
2909 RD->DefinitionData = DD;
2910 RD->getCanonicalDecl()->DefinitionData = DD;
2911
2912 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002913 Reader.PendingFakeDefinitionData.insert(
2914 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002915 }
2916
2917 return DD->Definition;
2918 }
Richard Smithd55889a2013-09-09 16:55:27 +00002919
Richard Smith01a73372013-10-15 22:02:41 +00002920 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002921 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2922 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002923
Richard Smith4eca9b92015-02-04 23:37:59 +00002924 // We can see the TU here only if we have no Sema object. In that case,
2925 // there's no TU scope to look in, so using the DC alone is sufficient.
2926 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2927 return TU;
2928
Craig Toppera13603a2014-05-22 05:54:18 +00002929 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002930}
2931
Douglas Gregor022857e2011-12-22 01:48:48 +00002932ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002933 // Record that we had a typedef name for linkage whether or not we merge
2934 // with that declaration.
2935 if (TypedefNameForLinkage) {
2936 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2937 Reader.ImportedTypedefNamesForLinkage.insert(
2938 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2939 return;
2940 }
2941
Douglas Gregor9b47f942012-01-09 17:38:47 +00002942 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002943 return;
Richard Smith95d99302013-07-13 02:00:19 +00002944
Richard Smith70d58502014-08-30 00:04:23 +00002945 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002946 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002947 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002948 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2949 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002950 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002951 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002952 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002953 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2954 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002955 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002956 // Add the declaration to its redeclaration context so later merging
2957 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002958 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002959 }
2960}
2961
Richard Smith88ebade2014-08-23 01:45:27 +00002962/// Find the declaration that should be merged into, given the declaration found
2963/// by name lookup. If we're merging an anonymous declaration within a typedef,
2964/// we need a matching typedef, and we merge with the type inside it.
2965static NamedDecl *getDeclForMerging(NamedDecl *Found,
2966 bool IsTypedefNameForLinkage) {
2967 if (!IsTypedefNameForLinkage)
2968 return Found;
2969
2970 // If we found a typedef declaration that gives a name to some other
2971 // declaration, then we want that inner declaration. Declarations from
2972 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002973 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002974 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002975
2976 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00002977 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00002978
Hans Wennborgdcfba332015-10-06 23:40:43 +00002979 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00002980}
2981
Richard Smithd08aeb62014-08-28 01:33:39 +00002982NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2983 DeclContext *DC,
2984 unsigned Index) {
2985 // If the lexical context has been merged, look into the now-canonical
2986 // definition.
2987 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2988 DC = Merged;
2989
2990 // If we've seen this before, return the canonical declaration.
2991 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2992 if (Index < Previous.size() && Previous[Index])
2993 return Previous[Index];
2994
2995 // If this is the first time, but we have parsed a declaration of the context,
2996 // build the anonymous declaration list from the parsed declaration.
2997 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00002998 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
2999 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003000 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3001 else
Richard Smith2b560572015-02-07 03:11:11 +00003002 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3003 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003004 }
3005
3006 return Index < Previous.size() ? Previous[Index] : nullptr;
3007}
3008
3009void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3010 DeclContext *DC, unsigned Index,
3011 NamedDecl *D) {
3012 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3013 DC = Merged;
3014
3015 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3016 if (Index >= Previous.size())
3017 Previous.resize(Index + 1);
3018 if (!Previous[Index])
3019 Previous[Index] = D;
3020}
3021
Douglas Gregor022857e2011-12-22 01:48:48 +00003022ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003023 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3024 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003025
Richard Smithd08aeb62014-08-28 01:33:39 +00003026 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3027 // Don't bother trying to find unnamed declarations that are in
3028 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003029 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3030 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003031 Result.suppress();
3032 return Result;
3033 }
Richard Smith95d99302013-07-13 02:00:19 +00003034
Douglas Gregor022857e2011-12-22 01:48:48 +00003035 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003036 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003037 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003038 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003039 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3040 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003041 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3042 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003043 // Go on to check in other places in case an existing typedef name
3044 // was not imported.
3045 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003046
Richard Smith2b560572015-02-07 03:11:11 +00003047 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003048 // This is an anonymous declaration that we may need to merge. Look it up
3049 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003050 if (auto *Existing = getAnonymousDeclForMerging(
3051 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3052 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003053 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3054 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003055 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003056 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003057 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003058
3059 // Temporarily consider the identifier to be up-to-date. We don't want to
3060 // cause additional lookups here.
3061 class UpToDateIdentifierRAII {
3062 IdentifierInfo *II;
3063 bool WasOutToDate;
3064
3065 public:
3066 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3067 : II(II), WasOutToDate(false)
3068 {
3069 if (II) {
3070 WasOutToDate = II->isOutOfDate();
3071 if (WasOutToDate)
3072 II->setOutOfDate(false);
3073 }
3074 }
3075
3076 ~UpToDateIdentifierRAII() {
3077 if (WasOutToDate)
3078 II->setOutOfDate(true);
3079 }
3080 } UpToDate(Name.getAsIdentifierInfo());
3081
Douglas Gregor2009cee2012-01-03 22:46:00 +00003082 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003083 IEnd = IdResolver.end();
3084 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003085 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003086 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003087 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3088 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003089 }
Richard Smith8a639892015-01-24 01:07:20 +00003090 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003091 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003092 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003093 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003094 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003095 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3096 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003097 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003098 } else {
3099 // Not in a mergeable context.
3100 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003101 }
Richard Smithd55889a2013-09-09 16:55:27 +00003102
Richard Smith2b9e3e32013-10-18 06:05:18 +00003103 // If this declaration is from a merged context, make a note that we need to
3104 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003105 //
3106 // FIXME: We should do something similar if we merge two definitions of the
3107 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003108 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3109 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3110 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003111 Reader.PendingOdrMergeChecks.push_back(D);
3112
Richard Smithd08aeb62014-08-28 01:33:39 +00003113 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003114 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003115}
3116
Richard Smithb321ecb2014-05-13 01:15:00 +00003117template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003118Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3119 return D->RedeclLink.getLatestNotUpdated();
3120}
3121Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3122 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3123}
3124
3125Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3126 assert(D);
3127
3128 switch (D->getKind()) {
3129#define ABSTRACT_DECL(TYPE)
3130#define DECL(TYPE, BASE) \
3131 case Decl::TYPE: \
3132 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3133#include "clang/AST/DeclNodes.inc"
3134 }
3135 llvm_unreachable("unknown decl kind");
3136}
3137
Richard Smith8ce51082015-03-11 01:44:51 +00003138Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3139 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3140}
3141
Richard Smithc3a53252015-02-28 05:57:02 +00003142template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003143void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3144 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003145 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003146 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003147 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003148}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003149
Richard Smith24d166c2014-07-31 23:52:38 +00003150namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003151template<>
3152void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003153 Redeclarable<VarDecl> *D,
3154 Decl *Previous, Decl *Canon) {
3155 VarDecl *VD = static_cast<VarDecl*>(D);
3156 VarDecl *PrevVD = cast<VarDecl>(Previous);
3157 D->RedeclLink.setPrevious(PrevVD);
3158 D->First = PrevVD->First;
3159
3160 // We should keep at most one definition on the chain.
3161 // FIXME: Cache the definition once we've found it. Building a chain with
3162 // N definitions currently takes O(N^2) time here.
3163 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3164 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3165 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3166 Reader.mergeDefinitionVisibility(CurD, VD);
3167 VD->demoteThisDefinitionToDeclaration();
3168 break;
3169 }
3170 }
3171 }
3172}
3173
3174template<>
3175void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003176 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003177 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003178 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3179 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3180
3181 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003182 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003183
3184 // If the previous declaration is an inline function declaration, then this
3185 // declaration is too.
3186 if (PrevFD->IsInline != FD->IsInline) {
3187 // FIXME: [dcl.fct.spec]p4:
3188 // If a function with external linkage is declared inline in one
3189 // translation unit, it shall be declared inline in all translation
3190 // units in which it appears.
3191 //
3192 // Be careful of this case:
3193 //
3194 // module A:
3195 // template<typename T> struct X { void f(); };
3196 // template<typename T> inline void X<T>::f() {}
3197 //
3198 // module B instantiates the declaration of X<int>::f
3199 // module C instantiates the definition of X<int>::f
3200 //
3201 // If module B and C are merged, we do not have a violation of this rule.
3202 FD->IsInline = true;
3203 }
3204
Richard Smith9e2341d2015-03-23 03:25:59 +00003205 // If we need to propagate an exception specification along the redecl
3206 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003207 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3208 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003209 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003210 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3211 bool WasUnresolved =
3212 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3213 if (IsUnresolved != WasUnresolved)
3214 Reader.PendingExceptionSpecUpdates.insert(
3215 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003216 }
3217}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003218} // end namespace clang
3219
Richard Smith6de7a242014-07-31 23:46:44 +00003220void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003221 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3222}
3223
Richard Smith8346e522015-06-10 01:47:58 +00003224/// Inherit the default template argument from \p From to \p To. Returns
3225/// \c false if there is no default template for \p From.
3226template <typename ParmDecl>
3227static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3228 Decl *ToD) {
3229 auto *To = cast<ParmDecl>(ToD);
3230 if (!From->hasDefaultArgument())
3231 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003232 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003233 return true;
3234}
3235
3236static void inheritDefaultTemplateArguments(ASTContext &Context,
3237 TemplateDecl *From,
3238 TemplateDecl *To) {
3239 auto *FromTP = From->getTemplateParameters();
3240 auto *ToTP = To->getTemplateParameters();
3241 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3242
3243 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3244 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003245 if (FromParam->isParameterPack())
3246 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003247 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3248
3249 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003250 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003251 break;
3252 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003253 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003254 break;
3255 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003256 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003257 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3258 break;
3259 }
3260 }
3261}
3262
Richard Smith6de7a242014-07-31 23:46:44 +00003263void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003264 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003265 assert(D && Previous);
3266
3267 switch (D->getKind()) {
3268#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003269#define DECL(TYPE, BASE) \
3270 case Decl::TYPE: \
3271 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003272 break;
3273#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003274 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003275
3276 // If the declaration was visible in one module, a redeclaration of it in
3277 // another module remains visible even if it wouldn't be visible by itself.
3278 //
3279 // FIXME: In this case, the declaration should only be visible if a module
3280 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003281 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003282 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003283 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003284
Richard Smith8346e522015-06-10 01:47:58 +00003285 // If the declaration declares a template, it may inherit default arguments
3286 // from the previous declaration.
3287 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3288 inheritDefaultTemplateArguments(Reader.getContext(),
3289 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003290}
3291
Richard Smithb321ecb2014-05-13 01:15:00 +00003292template<typename DeclT>
3293void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003294 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003295}
3296void ASTDeclReader::attachLatestDeclImpl(...) {
3297 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3298}
3299
Douglas Gregor05f10352011-12-17 23:38:30 +00003300void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3301 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003302
3303 switch (D->getKind()) {
3304#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003305#define DECL(TYPE, BASE) \
3306 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003307 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3308 break;
3309#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003310 }
3311}
3312
Richard Smith851072e2014-05-19 20:59:20 +00003313template<typename DeclT>
3314void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3315 D->RedeclLink.markIncomplete();
3316}
3317void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3318 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3319}
3320
3321void ASTReader::markIncompleteDeclChain(Decl *D) {
3322 switch (D->getKind()) {
3323#define ABSTRACT_DECL(TYPE)
3324#define DECL(TYPE, BASE) \
3325 case Decl::TYPE: \
3326 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3327 break;
3328#include "clang/AST/DeclNodes.inc"
3329 }
3330}
3331
Sebastian Redlb3298c32010-08-18 23:56:48 +00003332/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003333Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003334 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003335 SourceLocation DeclLoc;
3336 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003337 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003338 // Keep track of where we are in the stream, then jump back there
3339 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003340 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003341
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003342 ReadingKindTracker ReadingKind(Read_Decl, *this);
3343
Douglas Gregor1342e842009-07-06 18:54:52 +00003344 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003345 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003346
Sebastian Redl2c373b92010-10-05 15:59:54 +00003347 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003348 ASTRecordReader Record(*this, *Loc.F);
3349 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003350 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003351
Craig Toppera13603a2014-05-22 05:54:18 +00003352 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003353 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003354 case DECL_CONTEXT_LEXICAL:
3355 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003356 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003357 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003358 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003359 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003360 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003361 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003362 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003363 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003364 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003365 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003366 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003367 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003368 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003369 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003370 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003371 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003372 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003373 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003374 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003375 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003376 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003377 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003378 case DECL_EXPORT:
3379 D = ExportDecl::CreateDeserialized(Context, ID);
3380 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003381 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003382 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003383 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003384 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003385 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003386 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003387 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003388 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003389 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003390 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003391 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003392 break;
Richard Smith151c4562016-12-20 21:35:28 +00003393 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003394 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003395 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003396 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003397 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003398 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003399 case DECL_CONSTRUCTOR_USING_SHADOW:
3400 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3401 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003402 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003403 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003404 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003405 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003406 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003407 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003408 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003409 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003410 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003411 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003412 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003413 break;
Richard Smithbc491202017-02-17 20:05:37 +00003414 case DECL_CXX_DEDUCTION_GUIDE:
3415 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3416 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003417 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003418 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003419 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003420 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003421 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3422 break;
3423 case DECL_CXX_INHERITED_CONSTRUCTOR:
3424 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003425 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003426 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003427 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003428 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003429 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003430 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003431 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003432 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003433 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003434 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003435 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003436 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003437 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003438 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003439 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003440 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003441 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003442 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003443 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003445 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003446 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003447 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003448 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003449 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003450 case DECL_VAR_TEMPLATE:
3451 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3452 break;
3453 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3454 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3455 break;
3456 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3457 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3458 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003459 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003460 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003461 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003462 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003463 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003464 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003465 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003466 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003467 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003468 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003469 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003470 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003471 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003472 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3473 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003474 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003475 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003476 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003477 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003478 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3479 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003480 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003481 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003482 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003483 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003484 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003485 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003486 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003487 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003488 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003489 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003490 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003491 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003492 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003493 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003494 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003495 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003496 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003497 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003498 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003499 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003500 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003501 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003502 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003503 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003504 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003505 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003506 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003507 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003508 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003510 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003511 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003512 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003513 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003514 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003516 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003517 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003518 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003519 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003520 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003521 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003522 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003523 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003524 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003525 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003526 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003527 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003528 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003529 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003530 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003531 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003532 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003533 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003534 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003535 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003536 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003537 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003538 break;
3539 case DECL_BINDING:
3540 D = BindingDecl::CreateDeserialized(Context, ID);
3541 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003542 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003543 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003544 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003545 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003546 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003547 break;
John McCall5e77d762013-04-16 07:28:30 +00003548 case DECL_MS_PROPERTY:
3549 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3550 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003551 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003552 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003553 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003554 case DECL_CXX_BASE_SPECIFIERS:
3555 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003556 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003557 case DECL_CXX_CTOR_INITIALIZERS:
3558 Error("attempt to read a C++ ctor initializer record as a declaration");
3559 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003560 case DECL_IMPORT:
3561 // Note: last entry of the ImportDecl record is the number of stored source
3562 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003563 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003564 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003565 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003566 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003567 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003568 case DECL_OMP_DECLARE_REDUCTION:
3569 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3570 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003571 case DECL_OMP_CAPTUREDEXPR:
3572 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003573 break;
Nico Weber66220292016-03-02 17:28:48 +00003574 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003575 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003576 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003577 case DECL_PRAGMA_DETECT_MISMATCH:
3578 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003579 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003580 break;
Michael Han84324352013-02-22 17:15:32 +00003581 case DECL_EMPTY:
3582 D = EmptyDecl::CreateDeserialized(Context, ID);
3583 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003584 case DECL_OBJC_TYPE_PARAM:
3585 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3586 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003587 }
Chris Lattner487412d2009-04-27 05:27:42 +00003588
Sebastian Redlb3298c32010-08-18 23:56:48 +00003589 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003590 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003591 // Set the DeclContext before doing any deserialization, to make sure internal
3592 // calls to Decl::getASTContext() by Decl's methods will find the
3593 // TranslationUnitDecl without crashing.
3594 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003595 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003596
3597 // If this declaration is also a declaration context, get the
3598 // offsets for its tables of lexical and visible declarations.
3599 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3600 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003601 if (Offsets.first &&
3602 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3603 return nullptr;
3604 if (Offsets.second &&
3605 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3606 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003607 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003608 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003609
Douglas Gregordab42432011-08-12 00:15:20 +00003610 // Load any relevant update records.
Richard Smithd1c46742014-04-30 02:24:17 +00003611 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003612
Douglas Gregor404cdde2012-01-27 01:47:08 +00003613 // Load the categories after recursive loading is finished.
3614 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003615 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3616 // we put the Decl in PendingDefinitions so we can pull the categories here.
3617 if (Class->isThisDeclarationADefinition() ||
3618 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003619 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003620
Richard Smith13fb8602016-07-15 21:33:46 +00003621 // If we have deserialized a declaration that has a definition the
3622 // AST consumer might need to know about, queue it.
3623 // We don't pass it to the consumer immediately because we may be in recursive
3624 // loading, and some declarations may still be initializing.
Richard Smithdc1f0422016-07-20 19:10:16 +00003625 if (isConsumerInterestedIn(Context, D, Reader.hasPendingBody()))
Richard Smith13fb8602016-07-15 21:33:46 +00003626 InterestingDecls.push_back(D);
3627
Douglas Gregordab42432011-08-12 00:15:20 +00003628 return D;
3629}
3630
3631void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003632 // The declaration may have been modified by files later in the chain.
3633 // If this is the case, read the record containing the updates from each file
3634 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003635 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003636 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3637 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003638 auto UpdateOffsets = std::move(UpdI->second);
3639 DeclUpdateOffsets.erase(UpdI);
3640
Richard Smithdc1f0422016-07-20 19:10:16 +00003641 bool WasInteresting = isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003642 for (auto &FileAndOffset : UpdateOffsets) {
3643 ModuleFile *F = FileAndOffset.first;
3644 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003645 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3646 SavedStreamPosition SavedPosition(Cursor);
3647 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003648 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003649 ASTRecordReader Record(*this, *F);
3650 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003651 (void)RecCode;
3652 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003653
David L. Jonesbe1557a2016-12-21 00:17:49 +00003654 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3655 SourceLocation());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003656 Reader.UpdateDecl(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003657
3658 // We might have made this declaration interesting. If so, remember that
3659 // we need to hand it off to the consumer.
3660 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003661 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Richard Smith13fb8602016-07-15 21:33:46 +00003662 InterestingDecls.push_back(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003663 WasInteresting = true;
3664 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003665 }
3666 }
Richard Smith1e8e91b2016-04-08 20:53:26 +00003667
3668 // Load the pending visible updates for this decl context, if it has any.
3669 auto I = PendingVisibleUpdates.find(ID);
3670 if (I != PendingVisibleUpdates.end()) {
3671 auto VisibleUpdates = std::move(I->second);
3672 PendingVisibleUpdates.erase(I);
3673
3674 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3675 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3676 Lookups[DC].Table.add(
3677 Update.Mod, Update.Data,
3678 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3679 DC->setHasExternalVisibleStorage(true);
3680 }
Chris Lattner487412d2009-04-27 05:27:42 +00003681}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003682
Richard Smithd61d4ac2015-08-22 20:13:39 +00003683void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3684 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003685 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003686 if (FirstLocal != CanonDecl) {
3687 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3688 ASTDeclReader::attachPreviousDecl(
3689 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3690 CanonDecl);
3691 }
3692
3693 if (!LocalOffset) {
3694 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3695 return;
3696 }
3697
3698 // Load the list of other redeclarations from this module file.
3699 ModuleFile *M = getOwningModuleFile(FirstLocal);
3700 assert(M && "imported decl from no module file");
3701
3702 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3703 SavedStreamPosition SavedPosition(Cursor);
3704 Cursor.JumpToBit(LocalOffset);
3705
3706 RecordData Record;
3707 unsigned Code = Cursor.ReadCode();
3708 unsigned RecCode = Cursor.readRecord(Code, Record);
3709 (void)RecCode;
3710 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3711
3712 // FIXME: We have several different dispatches on decl kind here; maybe
3713 // we should instead generate one loop per kind and dispatch up-front?
3714 Decl *MostRecent = FirstLocal;
3715 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3716 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003717 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3718 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003719 }
Richard Smithc3a53252015-02-28 05:57:02 +00003720 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003721}
3722
3723namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003724 /// \brief Given an ObjC interface, goes through the modules and links to the
3725 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003726 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003727 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003728 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003729 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003730 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003731 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003732 serialization::GlobalDeclID InterfaceID;
3733 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003734
3735 void add(ObjCCategoryDecl *Cat) {
3736 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003737 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003738 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003739
3740 // Check for duplicate categories.
3741 if (Cat->getDeclName()) {
3742 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3743 if (Existing &&
3744 Reader.getOwningModuleFile(Existing)
3745 != Reader.getOwningModuleFile(Cat)) {
3746 // FIXME: We should not warn for duplicates in diamond:
3747 //
3748 // MT //
3749 // / \ //
3750 // ML MR //
3751 // \ / //
3752 // MB //
3753 //
3754 // If there are duplicates in ML/MR, there will be warning when
3755 // creating MB *and* when importing MB. We should not warn when
3756 // importing.
3757 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3758 << Interface->getDeclName() << Cat->getDeclName();
3759 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3760 } else if (!Existing) {
3761 // Record this category.
3762 Existing = Cat;
3763 }
3764 }
3765
3766 // Add this category to the end of the chain.
3767 if (Tail)
3768 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3769 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003770 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003771 Tail = Cat;
3772 }
3773
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003774 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003775 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003776 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003777 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3778 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003779 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003780 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3781 Tail(nullptr), InterfaceID(InterfaceID),
3782 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003783 {
3784 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003785 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003786 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003787 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003788
3789 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003790 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003791 }
3792 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003793
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003794 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003795 // If we've loaded all of the category information we care about from
3796 // this module file, we're done.
3797 if (M.Generation <= PreviousGeneration)
3798 return true;
3799
3800 // Map global ID of the definition down to the local ID used in this
3801 // module file. If there is no such mapping, we'll find nothing here
3802 // (or in any module it imports).
3803 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3804 if (!LocalID)
3805 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003806
Douglas Gregor404cdde2012-01-27 01:47:08 +00003807 // Perform a binary search to find the local redeclarations for this
3808 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003809 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003810 const ObjCCategoriesInfo *Result
3811 = std::lower_bound(M.ObjCCategoriesMap,
3812 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003813 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003814 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3815 Result->DefinitionID != LocalID) {
3816 // We didn't find anything. If the class definition is in this module
3817 // file, then the module files it depends on cannot have any categories,
3818 // so suppress further lookup.
3819 return Reader.isDeclIDFromModule(InterfaceID, M);
3820 }
3821
3822 // We found something. Dig out all of the categories.
3823 unsigned Offset = Result->Offset;
3824 unsigned N = M.ObjCCategories[Offset];
3825 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3826 for (unsigned I = 0; I != N; ++I)
3827 add(cast_or_null<ObjCCategoryDecl>(
3828 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3829 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003830 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003831 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003832} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003833
Douglas Gregor404cdde2012-01-27 01:47:08 +00003834void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3835 ObjCInterfaceDecl *D,
3836 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003837 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003838 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003839 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003840}
Douglas Gregordab42432011-08-12 00:15:20 +00003841
Richard Smithd6db68c2014-08-07 20:58:41 +00003842template<typename DeclT, typename Fn>
3843static void forAllLaterRedecls(DeclT *D, Fn F) {
3844 F(D);
3845
3846 // Check whether we've already merged D into its redeclaration chain.
3847 // MostRecent may or may not be nullptr if D has not been merged. If
3848 // not, walk the merged redecl chain and see if it's there.
3849 auto *MostRecent = D->getMostRecentDecl();
3850 bool Found = false;
3851 for (auto *Redecl = MostRecent; Redecl && !Found;
3852 Redecl = Redecl->getPreviousDecl())
3853 Found = (Redecl == D);
3854
3855 // If this declaration is merged, apply the functor to all later decls.
3856 if (Found) {
3857 for (auto *Redecl = MostRecent; Redecl != D;
3858 Redecl = Redecl->getPreviousDecl())
3859 F(Redecl);
3860 }
3861}
3862
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003863void ASTDeclReader::UpdateDecl(Decl *D) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003864 while (Record.getIdx() < Record.size()) {
3865 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003866 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003867 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003868 // FIXME: If we also have an update record for instantiating the
3869 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003870 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003871 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003872 // FIXME: We should call addHiddenDecl instead, to add the member
3873 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003874 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003875 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003876 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003877
3878 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3879 // It will be added to the template's specializations set when loaded.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003880 (void)Record.readDecl();
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003881 break;
3882
3883 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003884 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3885
Douglas Gregor540fd812012-01-09 18:07:24 +00003886 // Each module has its own anonymous namespace, which is disjoint from
3887 // any other module's anonymous namespaces, so don't attach the anonymous
3888 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003889 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003890 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3891 TU->setAnonymousNamespace(Anon);
3892 else
3893 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3894 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003895 break;
3896 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003897
3898 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3899 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003900 ReadSourceLocation());
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003901 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00003902
John McCall32791cc2016-01-06 22:34:54 +00003903 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3904 auto Param = cast<ParmVarDecl>(D);
3905
3906 // We have to read the default argument regardless of whether we use it
3907 // so that hypothetical further update records aren't messed up.
3908 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003909 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003910
3911 // Only apply the update if the parameter still has an uninstantiated
3912 // default argument.
3913 if (Param->hasUninstantiatedDefaultArg())
3914 Param->setDefaultArg(DefaultArg);
3915 break;
3916 }
3917
Richard Smith4b054b22016-08-24 21:25:37 +00003918 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3919 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003920 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003921
3922 // Only apply the update if the field still has an uninstantiated
3923 // default member initializer.
3924 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3925 if (DefaultInit)
3926 FD->setInClassInitializer(DefaultInit);
3927 else
3928 // Instantiation failed. We can get here if we serialized an AST for
3929 // an invalid program.
3930 FD->removeInClassInitializer();
3931 }
3932 break;
3933 }
3934
Richard Smith4d235792014-08-07 18:53:08 +00003935 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003936 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003937 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003938 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003939 // It's safe to stop now because this update record is always last.
3940 return;
3941 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003942
David L. Jonesbe1557a2016-12-21 00:17:49 +00003943 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00003944 // Maintain AST consistency: any later redeclarations of this function
3945 // are inline if this one is. (We might have merged another declaration
3946 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00003947 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3948 FD->setImplicitlyInline();
3949 });
Richard Smith195d8ef2014-05-29 03:15:31 +00003950 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003951 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00003952 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003953 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00003954 break;
3955 }
3956
Richard Smithcd45dbc2014-04-19 03:48:30 +00003957 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
3958 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00003959 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00003960 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00003961 OldDD && (OldDD->Definition != RD ||
3962 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00003963 ReadCXXRecordDefinition(RD, /*Update*/true);
3964
Richard Smithcd45dbc2014-04-19 03:48:30 +00003965 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003966 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00003967 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00003968 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00003969 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003970 }
3971
David L. Jonesbe1557a2016-12-21 00:17:49 +00003972 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003973 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003974 if (MemberSpecializationInfo *MSInfo =
3975 RD->getMemberSpecializationInfo()) {
3976 MSInfo->setTemplateSpecializationKind(TSK);
3977 MSInfo->setPointOfInstantiation(POI);
3978 } else {
3979 ClassTemplateSpecializationDecl *Spec =
3980 cast<ClassTemplateSpecializationDecl>(RD);
3981 Spec->setTemplateSpecializationKind(TSK);
3982 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00003983
David L. Jonesbe1557a2016-12-21 00:17:49 +00003984 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00003985 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003986 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00003987 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003988 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00003989 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00003990 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00003991
3992 // FIXME: If we already have a partial specialization set,
3993 // check that it matches.
3994 if (!Spec->getSpecializedTemplateOrPartial()
3995 .is<ClassTemplatePartialSpecializationDecl *>())
3996 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00003997 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003998 }
3999
David L. Jonesbe1557a2016-12-21 00:17:49 +00004000 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004001 RD->setLocation(ReadSourceLocation());
4002 RD->setLocStart(ReadSourceLocation());
4003 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004004
David L. Jonesbe1557a2016-12-21 00:17:49 +00004005 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004006 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004007 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004008 // If the declaration already has attributes, we assume that some other
4009 // AST file already loaded them.
4010 if (!D->hasAttrs())
4011 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004012 }
4013 break;
4014 }
4015
Richard Smithf8134002015-03-10 01:41:22 +00004016 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4017 // Set the 'operator delete' directly to avoid emitting another update
4018 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004019 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004020 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
4021 // FIXME: Check consistency if we have an old and new operator delete.
4022 if (!First->OperatorDelete)
4023 First->OperatorDelete = Del;
4024 break;
4025 }
4026
Richard Smith564417a2014-03-20 21:47:22 +00004027 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004028 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004029 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004030 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004031
4032 // Update this declaration's exception specification, if needed.
4033 auto *FD = cast<FunctionDecl>(D);
4034 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4035 // FIXME: If the exception specification is already present, check that it
4036 // matches.
4037 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00004038 FD->setType(Reader.Context.getFunctionType(
4039 FPT->getReturnType(), FPT->getParamTypes(),
4040 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004041
4042 // When we get to the end of deserializing, see if there are other decls
4043 // that we need to propagate this exception specification onto.
4044 Reader.PendingExceptionSpecUpdates.insert(
4045 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004046 }
Richard Smith564417a2014-03-20 21:47:22 +00004047 break;
4048 }
4049
Richard Smith1fa5d642013-05-11 05:45:24 +00004050 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004051 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004052 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004053 for (auto *Redecl : merged_redecls(D)) {
4054 // FIXME: If the return type is already deduced, check that it matches.
4055 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
4056 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
4057 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004058 break;
4059 }
Eli Friedman276dd182013-09-05 00:02:25 +00004060
4061 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004062 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004063 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00004064 break;
4065 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004066
4067 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004068 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004069 break;
4070
4071 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004072 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004073 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004074
Alexey Bataev97720002014-11-11 04:05:39 +00004075 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4076 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004077 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004078 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004079
Alex Denisovfde64952015-06-26 05:28:36 +00004080 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004081 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004082 auto *Exported = cast<NamedDecl>(D);
4083 if (auto *TD = dyn_cast<TagDecl>(Exported))
4084 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004085 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004086 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004087 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4088 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004089 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4090 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004091 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004092 // If Owner is made visible at some later point, make this declaration
4093 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004094 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004095 } else {
4096 // The declaration is now visible.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004097 Exported->Hidden = false;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004098 }
4099 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004100 }
Alex Denisovfde64952015-06-26 05:28:36 +00004101
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004102 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004103 case UPD_ADDED_ATTR_TO_RECORD:
4104 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004105 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004106 assert(Attrs.size() == 1);
4107 D->addAttr(Attrs[0]);
4108 break;
4109 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004110 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004111}