blob: 7971c30b488fb5d287f0f708a501dcb3924a7cf1 [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 L. Jonesbe1557a2016-12-21 00:17:49 +0000122 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000123 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000124 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000125 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000126 void MergeDefinitionData(ObjCInterfaceDecl *D,
127 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000128
Richard Smithd08aeb62014-08-28 01:33:39 +0000129 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
130 DeclContext *DC,
131 unsigned Index);
132 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
133 unsigned Index, NamedDecl *D);
134
Richard Smith0144f512015-08-22 02:09:38 +0000135 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000136 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000137 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000138 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000139 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000140
Douglas Gregor022857e2011-12-22 01:48:48 +0000141 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000142 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
143 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000144
Douglas Gregor022857e2011-12-22 01:48:48 +0000145 /// \brief Retrieve the first ID.
146 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000147
Richard Smith0144f512015-08-22 02:09:38 +0000148 /// \brief Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000149 bool isKeyDecl() const { return IsKeyDecl; }
150
Richard Smith5a4737c2015-02-06 02:42:59 +0000151 /// \brief Get a known declaration that this should be merged with, if
152 /// any.
153 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000154 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000155
Douglas Gregor022857e2011-12-22 01:48:48 +0000156 /// \brief Class used to capture the result of searching for an existing
157 /// declaration of a specific kind and name, along with the ability
158 /// to update the place where this result was found (the declaration
159 /// chain hanging off an identifier or the DeclContext we searched in)
160 /// if requested.
161 class FindExistingResult {
162 ASTReader &Reader;
163 NamedDecl *New;
164 NamedDecl *Existing;
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000165 bool AddResult;
Richard Smith70d58502014-08-30 00:04:23 +0000166
Richard Smithd08aeb62014-08-28 01:33:39 +0000167 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +0000168 IdentifierInfo *TypedefNameForLinkage;
Richard Smithd08aeb62014-08-28 01:33:39 +0000169
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000170 void operator=(FindExistingResult &&) = delete;
Richard Smithd08aeb62014-08-28 01:33:39 +0000171
Douglas Gregor022857e2011-12-22 01:48:48 +0000172 public:
173 FindExistingResult(ASTReader &Reader)
Richard Smithd08aeb62014-08-28 01:33:39 +0000174 : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false),
Hans Wennborgdcfba332015-10-06 23:40:43 +0000175 AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000176
Richard Smithd08aeb62014-08-28 01:33:39 +0000177 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000178 unsigned AnonymousDeclNumber,
179 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000180 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000181 AnonymousDeclNumber(AnonymousDeclNumber),
182 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000183
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000184 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000185 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
186 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000187 AnonymousDeclNumber(Other.AnonymousDeclNumber),
188 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000189 Other.AddResult = false;
190 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000191
Douglas Gregor022857e2011-12-22 01:48:48 +0000192 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000193
Douglas Gregor2009cee2012-01-03 22:46:00 +0000194 /// \brief Suppress the addition of this result into the known set of
195 /// names.
196 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000197
Douglas Gregor022857e2011-12-22 01:48:48 +0000198 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000199
Douglas Gregor022857e2011-12-22 01:48:48 +0000200 template<typename T>
201 operator T*() const { return dyn_cast_or_null<T>(Existing); }
202 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000203
Richard Smith8a639892015-01-24 01:07:20 +0000204 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
205 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000206 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000207
Chris Lattner487412d2009-04-27 05:27:42 +0000208 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000209 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
210 ASTReader::RecordLocation Loc,
211 DeclID thisDeclID, SourceLocation ThisDeclLoc)
212 : Reader(Reader), Record(Record), Loc(Loc),
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000213 ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc),
David L. Jonesbe1557a2016-12-21 00:17:49 +0000214 TypeIDForTypeDecl(0), NamedDeclForTagDecl(0),
Vassil Vassilev928c8252016-04-28 14:13:28 +0000215 TypedefNameForLinkage(nullptr), HasPendingBody(false),
216 IsDeclMarkedUsed(false) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000217
Richard Smithb321ecb2014-05-13 01:15:00 +0000218 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000219 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
220 static Decl *getMostRecentDeclImpl(...);
221 static Decl *getMostRecentDecl(Decl *D);
222
223 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000224 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000225 Redeclarable<DeclT> *D, Decl *Previous,
226 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000227 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000228 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
229 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000230
231 template <typename DeclT>
232 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
233 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000234 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000235
Richard Smith851072e2014-05-19 20:59:20 +0000236 template <typename DeclT>
237 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
238 static void markIncompleteDeclChainImpl(...);
239
Douglas Gregora6017bb2012-10-09 17:21:28 +0000240 /// \brief Determine whether this declaration has a pending body.
241 bool hasPendingBody() const { return HasPendingBody; }
242
David Blaikieac4345c2017-02-12 18:45:31 +0000243 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000244 void Visit(Decl *D);
245
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000246 void UpdateDecl(Decl *D);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000247
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000248 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
249 ObjCCategoryDecl *Next) {
250 Cat->NextClassCategory = Next;
251 }
252
Chris Lattner487412d2009-04-27 05:27:42 +0000253 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000254 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000255 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000256 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
257 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000258 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000259 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000260 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
261 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000262 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000263 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000264 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000265 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000266 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000267 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000268 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000269 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
270 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
271 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
272 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
273 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000274 ClassTemplateSpecializationDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000275 void VisitClassTemplateSpecializationDecl(
276 ClassTemplateSpecializationDecl *D) {
277 VisitClassTemplateSpecializationDeclImpl(D);
278 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000279 void VisitClassTemplatePartialSpecializationDecl(
280 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000281 void VisitClassScopeFunctionSpecializationDecl(
282 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000283 RedeclarableResult
284 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
285 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
286 VisitVarTemplateSpecializationDeclImpl(D);
287 }
288 void VisitVarTemplatePartialSpecializationDecl(
289 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000290 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000291 void VisitValueDecl(ValueDecl *VD);
292 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000293 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000294 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000295 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000296 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000297 void VisitCXXMethodDecl(CXXMethodDecl *D);
298 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
299 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
300 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000301 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000302 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000303 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000304 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
305 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000306 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
307 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000308 void VisitDecompositionDecl(DecompositionDecl *DD);
309 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000310 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000311 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000312 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000313 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000314 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000315 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000316 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000317 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000318 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000319 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000320 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000321 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000322 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000323 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000324 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000325 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000326 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000327 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000328 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000329 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
330 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000331 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000332 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000333 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000334
Chris Lattner487412d2009-04-27 05:27:42 +0000335 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000336
337 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000338 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000339
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000340 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000341 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
342 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000343
344 template<typename T>
345 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000346 RedeclarableResult &Redecl,
347 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000348
Richard Smith0b87e072013-10-07 08:02:11 +0000349 template<typename T>
350 void mergeMergeable(Mergeable<T> *D);
351
Richard Smithf17fdbd2014-04-24 02:25:27 +0000352 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
353 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000354 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000355
Douglas Gregor85f3f952015-07-07 03:57:15 +0000356 ObjCTypeParamList *ReadObjCTypeParamList();
357
Alexis Hunted053252010-05-30 07:21:58 +0000358 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000359 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000360 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000361 void VisitObjCContainerDecl(ObjCContainerDecl *D);
362 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
363 void VisitObjCIvarDecl(ObjCIvarDecl *D);
364 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
365 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000366 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
367 void VisitObjCImplDecl(ObjCImplDecl *D);
368 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
369 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
370 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
371 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
372 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000373 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000374 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000375 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000376 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000377} // end namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000378
Richard Smith86dfc1e2015-06-18 22:07:00 +0000379namespace {
380/// Iterator over the redeclarations of a declaration that have already
381/// been merged into the same redeclaration chain.
382template<typename DeclT>
383class MergedRedeclIterator {
384 DeclT *Start, *Canonical, *Current;
385public:
386 MergedRedeclIterator() : Current(nullptr) {}
387 MergedRedeclIterator(DeclT *Start)
388 : Start(Start), Canonical(nullptr), Current(Start) {}
389
390 DeclT *operator*() { return Current; }
391
392 MergedRedeclIterator &operator++() {
393 if (Current->isFirstDecl()) {
394 Canonical = Current;
395 Current = Current->getMostRecentDecl();
396 } else
397 Current = Current->getPreviousDecl();
398
399 // If we started in the merged portion, we'll reach our start position
400 // eventually. Otherwise, we'll never reach it, but the second declaration
401 // we reached was the canonical declaration, so stop when we see that one
402 // again.
403 if (Current == Start || Current == Canonical)
404 Current = nullptr;
405 return *this;
406 }
407
408 friend bool operator!=(const MergedRedeclIterator &A,
409 const MergedRedeclIterator &B) {
410 return A.Current != B.Current;
411 }
412};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000413} // end anonymous namespace
414
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000415template <typename DeclT>
416static llvm::iterator_range<MergedRedeclIterator<DeclT>>
417merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000418 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
419 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000420}
421
Sebastian Redlb3298c32010-08-18 23:56:48 +0000422uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000423 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000424}
425
David Blaikieac4345c2017-02-12 18:45:31 +0000426void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
427 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
428 CD->NumCtorInitializers = Record.readInt();
429 if (CD->NumCtorInitializers)
430 CD->CtorInitializers = ReadGlobalOffset();
431 }
432 // Store the offset of the body so we can lazily load it later.
433 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
434 HasPendingBody = true;
435}
436
Sebastian Redlb3298c32010-08-18 23:56:48 +0000437void ASTDeclReader::Visit(Decl *D) {
438 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000439
Vassil Vassilev928c8252016-04-28 14:13:28 +0000440 // At this point we have deserialized and merged the decl and it is safe to
441 // update its canonical decl to signal that the entire entity is used.
442 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
443 IsDeclMarkedUsed = false;
444
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000445 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
446 if (DD->DeclInfo) {
447 DeclaratorDecl::ExtInfo *Info =
448 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000449 Info->TInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000450 }
451 else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000452 DD->DeclInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000453 }
454 }
455
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000456 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000457 // We have a fully initialized TypeDecl. Read its type now.
Douglas Gregor0cdc8322010-12-10 17:03:06 +0000458 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000459
460 // If this is a tag declaration with a typedef name for linkage, it's safe
461 // to load that typedef now.
462 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000463 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
464 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000465 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
466 // if we have a fully initialized TypeDecl, we can safely read its type now.
467 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000468 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
469 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000470 // We only read it if FD doesn't already have a body (e.g., from another
471 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000472 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000473 if (Record.readInt())
474 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000475 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000476}
477
Sebastian Redlb3298c32010-08-18 23:56:48 +0000478void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000479 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
480 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000481 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000482 // parameter or of a parameter of a function template immediately. These
483 // entities might be used in the formulation of its DeclContext (for
484 // example, a function parameter can be used in decltype() in trailing
485 // return type of the function). Use the translation unit DeclContext as a
486 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000487 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
488 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000489 if (!LexicalDCIDForTemplateParmDecl)
490 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000491 Reader.addPendingDeclContextInfo(D,
492 SemaDCIDForTemplateParmDecl,
493 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000494 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000495 } else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000496 DeclContext *SemaDC = ReadDeclAs<DeclContext>();
497 DeclContext *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000498 if (!LexicalDC)
499 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000500 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000501 // Avoid calling setLexicalDeclContext() directly because it uses
502 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000503 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
504 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000505 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000506 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000507 D->setInvalidDecl(Record.readInt());
508 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000509 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000510 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000511 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
512 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000513 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000514 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000515 D->setImplicit(Record.readInt());
516 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000517 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000518 D->setReferenced(Record.readInt());
519 D->setTopLevelDeclInObjCContainer(Record.readInt());
520 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000521 D->FromASTFile = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000522 D->setModulePrivate(Record.readInt());
Douglas Gregor781f7132012-01-06 16:59:53 +0000523 D->Hidden = D->isModulePrivate();
Richard Smith42413142015-05-15 20:05:43 +0000524
Douglas Gregorcf68c582011-12-01 22:20:10 +0000525 // Determine whether this declaration is part of a (sub)module. If so, it
526 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000527 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000528 // Store the owning submodule ID in the declaration.
529 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000530
531 if (D->Hidden) {
532 // Module-private declarations are never visible, so there is no work to do.
533 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
534 // If local visibility is being tracked, this declaration will become
535 // hidden and visible as the owning module does. Inform Sema that this
536 // declaration might not be visible.
537 D->Hidden = true;
538 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
539 if (Owner->NameVisibility != Module::AllVisible) {
540 // The owning module is not visible. Mark this declaration as hidden.
541 D->Hidden = true;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000542
Richard Smith42413142015-05-15 20:05:43 +0000543 // Note that this declaration was hidden because its owning module is
544 // not yet visible.
545 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000546 }
547 }
548 }
Chris Lattner487412d2009-04-27 05:27:42 +0000549}
550
Nico Weber66220292016-03-02 17:28:48 +0000551void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
552 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000553 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000554 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000555 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000556 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
557 D->getTrailingObjects<char>()[Arg.size()] = '\0';
558}
559
Nico Webercbbaeb12016-03-02 19:28:54 +0000560void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
561 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000562 D->setLocation(ReadSourceLocation());
563 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000564 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
565 D->getTrailingObjects<char>()[Name.size()] = '\0';
566
567 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000568 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000569 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
570 Value.size());
571 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
572}
573
Sebastian Redlb3298c32010-08-18 23:56:48 +0000574void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000575 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000576}
577
Sebastian Redlb3298c32010-08-18 23:56:48 +0000578void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000579 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000580 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000581 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000582}
583
Sebastian Redlb3298c32010-08-18 23:56:48 +0000584void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000585 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000586 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000587 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000588 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000589}
590
Richard Smith43ccec8e2014-08-26 03:52:16 +0000591ASTDeclReader::RedeclarableResult
592ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000593 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000594 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000595 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000596 if (Record.readInt()) { // isModed
597 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000598 TD->setModedTypeSourceInfo(TInfo, modedT);
599 } else
600 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000601 // Read and discard the declaration for which this is a typedef name for
602 // linkage, if it exists. We cannot rely on our type to pull in this decl,
603 // because it might have been merged with a type from another module and
604 // thus might not refer to our version of the declaration.
605 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000606 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000607}
608
609void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000610 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
611 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000612}
613
Richard Smithdda56e42011-04-15 14:24:37 +0000614void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000615 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000616 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000617 // Merged when we merge the template.
618 TD->setDescribedAliasTemplate(Template);
619 else
620 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000621}
622
Richard Smith1d209d02013-05-23 01:49:11 +0000623ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000624 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000625 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000626
David L. Jonesbe1557a2016-12-21 00:17:49 +0000627 TD->IdentifierNamespace = Record.readInt();
628 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000629 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000630 TD->setCompleteDefinition(Record.readInt());
631 TD->setEmbeddedInDeclarator(Record.readInt());
632 TD->setFreeStanding(Record.readInt());
633 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000634 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000635
David L. Jonesbe1557a2016-12-21 00:17:49 +0000636 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000637 case 0:
638 break;
639 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000640 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000641 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000642 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000643 break;
644 }
645 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000646 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000647 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000648 break;
Richard Smith70d58502014-08-30 00:04:23 +0000649 default:
650 llvm_unreachable("unexpected tag info kind");
651 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000652
Richard Smithcd45dbc2014-04-19 03:48:30 +0000653 if (!isa<CXXRecordDecl>(TD))
654 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000655 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000656}
657
Sebastian Redlb3298c32010-08-18 23:56:48 +0000658void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000659 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000660 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000661 ED->setIntegerTypeSourceInfo(TI);
662 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000663 ED->setIntegerType(Record.readType());
664 ED->setPromotionType(Record.readType());
665 ED->setNumPositiveBits(Record.readInt());
666 ED->setNumNegativeBits(Record.readInt());
667 ED->IsScoped = Record.readInt();
668 ED->IsScopedUsingClassTag = Record.readInt();
669 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000670
Richard Smith01a73372013-10-15 22:02:41 +0000671 // If this is a definition subject to the ODR, and we already have a
672 // definition, merge this one into it.
673 if (ED->IsCompleteDefinition &&
674 Reader.getContext().getLangOpts().Modules &&
675 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000676 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
677 if (!OldDef) {
678 // This is the first time we've seen an imported definition. Look for a
679 // local definition before deciding that we are the first definition.
680 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
681 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
682 OldDef = D;
683 break;
684 }
685 }
686 }
687 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000688 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
689 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000690 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000691 } else {
692 OldDef = ED;
693 }
694 }
695
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000696 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000697 TemplateSpecializationKind TSK =
698 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000699 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000700 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
701 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
702 }
Chris Lattner487412d2009-04-27 05:27:42 +0000703}
704
Richard Smith1d209d02013-05-23 01:49:11 +0000705ASTDeclReader::RedeclarableResult
706ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
707 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000708 RD->setHasFlexibleArrayMember(Record.readInt());
709 RD->setAnonymousStructOrUnion(Record.readInt());
710 RD->setHasObjectMember(Record.readInt());
711 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000712 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000713}
714
Sebastian Redlb3298c32010-08-18 23:56:48 +0000715void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000716 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000717 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000718}
719
Sebastian Redlb3298c32010-08-18 23:56:48 +0000720void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000721 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000722 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000723 ECD->setInitExpr(Record.readExpr());
724 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000725 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000726}
727
Sebastian Redlb3298c32010-08-18 23:56:48 +0000728void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000729 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000730 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000731 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000732 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000733 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000734 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000735 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000736 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000737}
738
Sebastian Redlb3298c32010-08-18 23:56:48 +0000739void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000740 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000741 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000742
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000743 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000744 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000745
Douglas Gregorb2585692012-01-04 17:13:46 +0000746 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
747 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000748
David L. Jonesbe1557a2016-12-21 00:17:49 +0000749 FD->SClass = (StorageClass)Record.readInt();
750 FD->IsInline = Record.readInt();
751 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000752 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000753 FD->IsVirtualAsWritten = Record.readInt();
754 FD->IsPure = Record.readInt();
755 FD->HasInheritedPrototype = Record.readInt();
756 FD->HasWrittenPrototype = Record.readInt();
757 FD->IsDeleted = Record.readInt();
758 FD->IsTrivial = Record.readInt();
759 FD->IsDefaulted = Record.readInt();
760 FD->IsExplicitlyDefaulted = Record.readInt();
761 FD->HasImplicitReturnZero = Record.readInt();
762 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000763 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000764 FD->HasSkippedBody = Record.readInt();
765 FD->IsLateTemplateParsed = Record.readInt();
766 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000767 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000768
David L. Jonesbe1557a2016-12-21 00:17:49 +0000769 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000770 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000771 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000772 break;
773 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000774 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000775 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000776 break;
777 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000778 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000779 TemplateSpecializationKind TSK =
780 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000781 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000782 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000783 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000784 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000785 break;
786 }
787 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000788 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000789 TemplateSpecializationKind TSK =
790 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000791
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000792 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000793 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000794 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000795
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000796 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000797 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000798 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000799 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000800 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000801 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000802 TemplArgLocs.reserve(NumTemplateArgLocs);
803 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000804 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000805
806 LAngleLoc = ReadSourceLocation();
807 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000808 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000809
810 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000811
Douglas Gregor4163aca2011-09-09 21:34:22 +0000812 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000813 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000814 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000815 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000816 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000817 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000818 FunctionTemplateSpecializationInfo *FTInfo
819 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
820 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000821 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
822 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000823 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000824 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000825
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000826 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000827 // The template that contains the specializations set. It's not safe to
828 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000829 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000830 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
831 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
832 // FunctionTemplateSpecializationInfo's Profile().
833 // We avoid getASTContext because a decl in the parent hierarchy may
834 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000835 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000836 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000837 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000838 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000839 FunctionTemplateSpecializationInfo *ExistingInfo =
840 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000841 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000842 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
843 else {
844 assert(Reader.getContext().getLangOpts().Modules &&
845 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000846 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000847 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000848 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000849 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000850 }
851 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
852 // Templates.
853 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000854 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000855 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000856 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
857
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000858 // Templates args.
859 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000860 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000861 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000862 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000863 TemplArgs.setLAngleLoc(ReadSourceLocation());
864 TemplArgs.setRAngleLoc(ReadSourceLocation());
865
Douglas Gregor4163aca2011-09-09 21:34:22 +0000866 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000867 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000868 // These are not merged; we don't need to merge redeclarations of dependent
869 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000870 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000871 }
872 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000873
Chris Lattnerca025db2010-05-07 21:43:38 +0000874 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000875 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000876 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000877 Params.reserve(NumParams);
878 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000879 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000880 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000881}
882
Sebastian Redlb3298c32010-08-18 23:56:48 +0000883void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000884 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000885 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000886 // Load the body on-demand. Most clients won't care, because method
887 // definitions rarely show up in headers.
888 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
889 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000890 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
891 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000892 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000893 MD->setInstanceMethod(Record.readInt());
894 MD->setVariadic(Record.readInt());
895 MD->setPropertyAccessor(Record.readInt());
896 MD->setDefined(Record.readInt());
897 MD->IsOverriding = Record.readInt();
898 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000899
David L. Jonesbe1557a2016-12-21 00:17:49 +0000900 MD->IsRedeclaration = Record.readInt();
901 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000902 if (MD->HasRedeclaration)
903 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000904 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000905
David L. Jonesbe1557a2016-12-21 00:17:49 +0000906 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
907 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
908 MD->SetRelatedResultType(Record.readInt());
909 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000910 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
911 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000912 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000913 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000914 Params.reserve(NumParams);
915 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000916 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000917
David L. Jonesbe1557a2016-12-21 00:17:49 +0000918 MD->SelLocsKind = Record.readInt();
919 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000920 SmallVector<SourceLocation, 16> SelLocs;
921 SelLocs.reserve(NumStoredSelLocs);
922 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000923 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000924
925 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000926}
927
Douglas Gregor85f3f952015-07-07 03:57:15 +0000928void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000929 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000930
David L. Jonesbe1557a2016-12-21 00:17:49 +0000931 D->Variance = Record.readInt();
932 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000933 D->VarianceLoc = ReadSourceLocation();
934 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000935}
936
Sebastian Redlb3298c32010-08-18 23:56:48 +0000937void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000938 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000939 CD->setAtStartLoc(ReadSourceLocation());
940 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000941}
942
Douglas Gregor85f3f952015-07-07 03:57:15 +0000943ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000944 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000945 if (numParams == 0)
946 return nullptr;
947
948 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
949 typeParams.reserve(numParams);
950 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000951 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000952 if (!typeParam)
953 return nullptr;
954
955 typeParams.push_back(typeParam);
956 }
957
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000958 SourceLocation lAngleLoc = ReadSourceLocation();
959 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000960
961 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
962 typeParams, rAngleLoc);
963}
964
Manman Renec315f12016-09-09 23:48:27 +0000965void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000966 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000967 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000968 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000969
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000970 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000971 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000972
Manman Renec315f12016-09-09 23:48:27 +0000973 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000974 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000975 SmallVector<ObjCProtocolDecl *, 16> Protocols;
976 Protocols.reserve(NumProtocols);
977 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000978 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000979 SmallVector<SourceLocation, 16> ProtoLocs;
980 ProtoLocs.reserve(NumProtocols);
981 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000982 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +0000983 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
984 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000985
Manman Renec315f12016-09-09 23:48:27 +0000986 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000987 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000988 Protocols.clear();
989 Protocols.reserve(NumProtocols);
990 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000991 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000992 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
993 Reader.getContext());
994}
995
996void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
997 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
998 // FIXME: odr checking?
999}
1000
Sebastian Redlb3298c32010-08-18 23:56:48 +00001001void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001002 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001003 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001004 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001005 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001006
1007 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001008 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001009 // Read the definition.
1010 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001011
David L. Jonesbe1557a2016-12-21 00:17:49 +00001012 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001013 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1014 if (Canon->Data.getPointer()) {
1015 // If we already have a definition, keep the definition invariant and
1016 // merge the data.
1017 MergeDefinitionData(Canon, std::move(ID->data()));
1018 ID->Data = Canon->Data;
1019 } else {
1020 // Set the definition data of the canonical declaration, so other
1021 // redeclarations will see it.
1022 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001023
Manman Renec315f12016-09-09 23:48:27 +00001024 // We will rebuild this list lazily.
1025 ID->setIvarList(nullptr);
1026 }
Craig Toppera13603a2014-05-22 05:54:18 +00001027
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001028 // Note that we have deserialized a definition.
1029 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001030
Douglas Gregor404cdde2012-01-27 01:47:08 +00001031 // Note that we've loaded this Objective-C class.
1032 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001033 } else {
1034 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001035 }
Chris Lattner487412d2009-04-27 05:27:42 +00001036}
1037
Sebastian Redlb3298c32010-08-18 23:56:48 +00001038void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001039 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001040 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001041 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001042 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001043 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001044 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001045}
1046
Sebastian Redlb3298c32010-08-18 23:56:48 +00001047void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
Douglas Gregorda389302012-01-01 21:47:52 +00001048 RedeclarableResult Redecl = VisitRedeclarable(PD);
Chris Lattner487412d2009-04-27 05:27:42 +00001049 VisitObjCContainerDecl(PD);
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001050 mergeRedeclarable(PD, Redecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001051
David L. Jonesbe1557a2016-12-21 00:17:49 +00001052 if (Record.readInt()) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00001053 // Read the definition.
1054 PD->allocateDefinitionData();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001055
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001056 // Set the definition data of the canonical declaration, so other
1057 // redeclarations will see it.
1058 PD->getCanonicalDecl()->Data = PD->Data;
1059
David L. Jonesbe1557a2016-12-21 00:17:49 +00001060 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001061 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1062 ProtoRefs.reserve(NumProtoRefs);
1063 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001064 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001065 SmallVector<SourceLocation, 16> ProtoLocs;
1066 ProtoLocs.reserve(NumProtoRefs);
1067 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001068 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001069 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1070 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001071
Douglas Gregora715bff2012-01-01 19:51:50 +00001072 // Note that we have deserialized a definition.
1073 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001074 } else {
1075 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001076 }
Chris Lattner487412d2009-04-27 05:27:42 +00001077}
1078
Sebastian Redlb3298c32010-08-18 23:56:48 +00001079void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001080 VisitFieldDecl(FD);
1081}
1082
Sebastian Redlb3298c32010-08-18 23:56:48 +00001083void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001084 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001085 CD->setCategoryNameLoc(ReadSourceLocation());
1086 CD->setIvarLBraceLoc(ReadSourceLocation());
1087 CD->setIvarRBraceLoc(ReadSourceLocation());
1088
Douglas Gregor404cdde2012-01-27 01:47:08 +00001089 // Note that this category has been deserialized. We do this before
1090 // deserializing the interface declaration, so that it will consider this
1091 /// category.
1092 Reader.CategoriesDeserialized.insert(CD);
1093
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001095 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001096 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001097 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001098 ProtoRefs.reserve(NumProtoRefs);
1099 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001100 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001101 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001102 ProtoLocs.reserve(NumProtoRefs);
1103 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001105 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001106 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001107}
1108
Sebastian Redlb3298c32010-08-18 23:56:48 +00001109void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001110 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001111 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001112}
1113
Sebastian Redlb3298c32010-08-18 23:56:48 +00001114void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001115 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001116 D->setAtLoc(ReadSourceLocation());
1117 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001118 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001119 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001120 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001121 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001122 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001123 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001124 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001125 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001126 (ObjCPropertyDecl::PropertyControl)Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001127 D->setGetterName(Record.readDeclarationName().getObjCSelector());
1128 D->setSetterName(Record.readDeclarationName().getObjCSelector());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001129 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1130 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1131 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001132}
1133
Sebastian Redlb3298c32010-08-18 23:56:48 +00001134void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001135 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001136 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001137}
1138
Sebastian Redlb3298c32010-08-18 23:56:48 +00001139void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001140 VisitObjCImplDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001141 D->setIdentifier(Record.getIdentifierInfo());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001142 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001143}
1144
Sebastian Redlb3298c32010-08-18 23:56:48 +00001145void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001146 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001147 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1148 D->SuperLoc = ReadSourceLocation();
1149 D->setIvarLBraceLoc(ReadSourceLocation());
1150 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001151 D->setHasNonZeroConstructors(Record.readInt());
1152 D->setHasDestructors(Record.readInt());
1153 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001154 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001155 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001156}
1157
Sebastian Redlb3298c32010-08-18 23:56:48 +00001158void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001159 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001160 D->setAtLoc(ReadSourceLocation());
1161 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1162 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1163 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001164 D->setGetterCXXConstructor(Record.readExpr());
1165 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001166}
1167
Sebastian Redlb3298c32010-08-18 23:56:48 +00001168void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001169 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001170 FD->Mutable = Record.readInt();
1171 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001172 FD->InitStorage.setInt(
1173 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1174 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001175 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001176 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001177 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001178 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001179 }
Richard Smith2b013182012-06-10 03:12:00 +00001180 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001181 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001182 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001183 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001184 }
Richard Smith0b87e072013-10-07 08:02:11 +00001185 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001186}
1187
John McCall5e77d762013-04-16 07:28:30 +00001188void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1189 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001190 PD->GetterId = Record.getIdentifierInfo();
1191 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001192}
1193
Francois Pichet783dd6e2010-11-21 06:08:52 +00001194void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1195 VisitValueDecl(FD);
1196
David L. Jonesbe1557a2016-12-21 00:17:49 +00001197 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001198 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001199 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001200
1201 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001202 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001203
1204 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001205}
1206
Larisse Voufo39a1e502013-08-06 01:03:05 +00001207ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001208 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001209 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001210
David L. Jonesbe1557a2016-12-21 00:17:49 +00001211 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1212 VD->VarDeclBits.TSCSpec = Record.readInt();
1213 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001214 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001215 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1216 Record.readInt();
1217 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1218 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1219 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1220 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1221 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1222 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1223 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1224 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1225 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001226 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001227 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001228 VD->setCachedLinkage(VarLinkage);
1229
1230 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001231 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001232 VD->getLexicalDeclContext()->isFunctionOrMethod())
1233 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001234
David L. Jonesbe1557a2016-12-21 00:17:49 +00001235 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001236 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001237 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001238 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1239 Eval->CheckedICE = true;
1240 Eval->IsICE = Val == 3;
1241 }
1242 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001243
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001244 enum VarKind {
1245 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1246 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001247 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001248 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001249 // Only true variables (not parameters or implicit parameters) can be
1250 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001251 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1252 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001253 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001254 break;
1255 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001256 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001257 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001258 break;
1259 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001260 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001261 TemplateSpecializationKind TSK =
1262 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001263 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001264 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001265 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001266 break;
1267 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001268 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001269
1270 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001271}
1272
Sebastian Redlb3298c32010-08-18 23:56:48 +00001273void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001274 VisitVarDecl(PD);
1275}
1276
Sebastian Redlb3298c32010-08-18 23:56:48 +00001277void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001278 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001279 unsigned isObjCMethodParam = Record.readInt();
1280 unsigned scopeDepth = Record.readInt();
1281 unsigned scopeIndex = Record.readInt();
1282 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001283 if (isObjCMethodParam) {
1284 assert(scopeDepth == 0);
1285 PD->setObjCMethodScopeInfo(scopeIndex);
1286 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1287 } else {
1288 PD->setScopeInfo(scopeDepth, scopeIndex);
1289 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001290 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1291 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1292 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001293 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001294
1295 // FIXME: If this is a redeclaration of a function from another module, handle
1296 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001297}
1298
Richard Smith7b76d812016-08-12 02:21:25 +00001299void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1300 VisitVarDecl(DD);
1301 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1302 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001303 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001304}
1305
1306void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1307 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001308 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001309}
1310
Sebastian Redlb3298c32010-08-18 23:56:48 +00001311void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001312 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001313 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001314 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001315}
1316
Sebastian Redlb3298c32010-08-18 23:56:48 +00001317void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001318 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001319 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001320 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001321 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001322 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001323 Params.reserve(NumParams);
1324 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001325 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001326 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001327
David L. Jonesbe1557a2016-12-21 00:17:49 +00001328 BD->setIsVariadic(Record.readInt());
1329 BD->setBlockMissingReturnType(Record.readInt());
1330 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001331
David L. Jonesbe1557a2016-12-21 00:17:49 +00001332 bool capturesCXXThis = Record.readInt();
1333 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001334 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001335 captures.reserve(numCaptures);
1336 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001337 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001338 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001339 bool byRef = (flags & 1);
1340 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001341 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001342
1343 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1344 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001345 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001346}
1347
Ben Langmuirce914fc2013-05-03 19:20:19 +00001348void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1349 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001350 unsigned ContextParamPos = Record.readInt();
1351 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001352 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001353 for (unsigned I = 0; I < CD->NumParams; ++I) {
1354 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001355 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001356 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001357 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001358 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001359}
1360
Sebastian Redlb3298c32010-08-18 23:56:48 +00001361void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001362 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001363 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001364 D->setExternLoc(ReadSourceLocation());
1365 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001366}
1367
Richard Smith8df390f2016-09-08 23:14:54 +00001368void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1369 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001370 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001371}
1372
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001373void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1374 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001375 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001376}
1377
Sebastian Redlb3298c32010-08-18 23:56:48 +00001378void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001379 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001380 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001381 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001382 D->LocStart = ReadSourceLocation();
1383 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001384
Richard Smith15e32fd2015-03-17 02:23:11 +00001385 // Defer loading the anonymous namespace until we've finished merging
1386 // this namespace; loading it might load a later declaration of the
1387 // same namespace, and we have an invariant that older declarations
1388 // get merged before newer ones try to merge.
1389 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001390 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001391 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001392 } else {
1393 // Link this namespace back to the first declaration, which has already
1394 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001395 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001396 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001397
1398 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001399
1400 if (AnonNamespace) {
1401 // Each module has its own anonymous namespace, which is disjoint from
1402 // any other module's anonymous namespaces, so don't attach the anonymous
1403 // namespace at all.
1404 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001405 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001406 D->setAnonymousNamespace(Anon);
1407 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001408}
1409
Sebastian Redlb3298c32010-08-18 23:56:48 +00001410void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001411 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001412 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001413 D->NamespaceLoc = ReadSourceLocation();
1414 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001415 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001416 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001417 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001418}
1419
Sebastian Redlb3298c32010-08-18 23:56:48 +00001420void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001421 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001422 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001423 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001424 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1425 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001426 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001427 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001428 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001429 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001430}
1431
Richard Smith151c4562016-12-20 21:35:28 +00001432void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1433 VisitNamedDecl(D);
1434 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1435 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1436 for (unsigned I = 0; I != D->NumExpansions; ++I)
1437 Expansions[I] = ReadDeclAs<NamedDecl>();
1438 mergeMergeable(D);
1439}
1440
Sebastian Redlb3298c32010-08-18 23:56:48 +00001441void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001442 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001443 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001444 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1445 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1446 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001447 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001448 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001449 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001450}
1451
Richard Smith5179eb72016-06-28 19:03:57 +00001452void ASTDeclReader::VisitConstructorUsingShadowDecl(
1453 ConstructorUsingShadowDecl *D) {
1454 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001455 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1456 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001457 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001458}
1459
Sebastian Redlb3298c32010-08-18 23:56:48 +00001460void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001461 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001462 D->UsingLoc = ReadSourceLocation();
1463 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001464 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001465 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1466 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001467}
1468
Sebastian Redlb3298c32010-08-18 23:56:48 +00001469void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001470 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001471 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001472 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001473 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001474 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001475 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001476}
1477
Sebastian Redlb3298c32010-08-18 23:56:48 +00001478void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001479 UnresolvedUsingTypenameDecl *D) {
1480 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001481 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001482 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001483 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001484 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001485}
1486
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001487void ASTDeclReader::ReadCXXDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001488 struct CXXRecordDecl::DefinitionData &Data) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001489 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001490 Data.UserDeclaredConstructor = Record.readInt();
1491 Data.UserDeclaredSpecialMembers = Record.readInt();
1492 Data.Aggregate = Record.readInt();
1493 Data.PlainOldData = Record.readInt();
1494 Data.Empty = Record.readInt();
1495 Data.Polymorphic = Record.readInt();
1496 Data.Abstract = Record.readInt();
1497 Data.IsStandardLayout = Record.readInt();
1498 Data.HasNoNonEmptyBases = Record.readInt();
1499 Data.HasPrivateFields = Record.readInt();
1500 Data.HasProtectedFields = Record.readInt();
1501 Data.HasPublicFields = Record.readInt();
1502 Data.HasMutableFields = Record.readInt();
1503 Data.HasVariantMembers = Record.readInt();
1504 Data.HasOnlyCMembers = Record.readInt();
1505 Data.HasInClassInitializer = Record.readInt();
1506 Data.HasUninitializedReferenceMember = Record.readInt();
1507 Data.HasUninitializedFields = Record.readInt();
1508 Data.HasInheritedConstructor = Record.readInt();
1509 Data.HasInheritedAssignment = Record.readInt();
1510 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1511 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1512 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1513 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1514 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1515 Data.DefaultedDestructorIsDeleted = Record.readInt();
1516 Data.HasTrivialSpecialMembers = Record.readInt();
1517 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1518 Data.HasIrrelevantDestructor = Record.readInt();
1519 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1520 Data.HasDefaultedDefaultConstructor = Record.readInt();
1521 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1522 Data.HasConstexprDefaultConstructor = Record.readInt();
1523 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1524 Data.ComputedVisibleConversions = Record.readInt();
1525 Data.UserProvidedDefaultConstructor = Record.readInt();
1526 Data.DeclaredSpecialMembers = Record.readInt();
1527 Data.ImplicitCopyConstructorHasConstParam = Record.readInt();
1528 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1529 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1530 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001531 Data.ODRHash = Record.readInt();
Anders Carlsson703d6e62011-01-22 18:11:02 +00001532
David L. Jonesbe1557a2016-12-21 00:17:49 +00001533 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001534 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001535 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001536 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001537 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001538 Data.VBases = ReadGlobalOffset();
1539
David L. Jonesb6a8f022016-12-21 04:34:52 +00001540 Record.readUnresolvedSet(Data.Conversions);
1541 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001542 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001543 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001544
Douglas Gregor99ae8062012-02-14 17:54:36 +00001545 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001546 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001547 CXXRecordDecl::LambdaDefinitionData &Lambda
1548 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001549 Lambda.Dependent = Record.readInt();
1550 Lambda.IsGenericLambda = Record.readInt();
1551 Lambda.CaptureDefault = Record.readInt();
1552 Lambda.NumCaptures = Record.readInt();
1553 Lambda.NumExplicitCaptures = Record.readInt();
1554 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001555 Lambda.ContextDecl = ReadDeclID();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001556 Lambda.Captures
1557 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1558 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001559 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001560 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001561 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001562 bool IsImplicit = Record.readInt();
1563 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001564 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001565 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001566 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001567 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001568 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001569 break;
1570 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001571 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001572 VarDecl *Var = ReadDeclAs<VarDecl>();
1573 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001574 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1575 break;
1576 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001577 }
1578 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001579}
1580
Richard Smithcd45dbc2014-04-19 03:48:30 +00001581void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001582 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001583 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001584 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001585 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001586
Richard Smith02793752015-03-27 21:16:39 +00001587 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001588 // Track that we merged the definitions.
1589 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1590 DD.Definition));
1591 Reader.PendingDefinitions.erase(MergeDD.Definition);
1592 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001593 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001594 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1595 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001596 }
1597
Richard Smith2a9e5c52015-02-03 03:32:14 +00001598 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1599 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1600 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001601 // We faked up this definition data because we found a class for which we'd
1602 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001603 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001604 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001605
1606 // Don't change which declaration is the definition; that is required
1607 // to be invariant once we select it.
1608 auto *Def = DD.Definition;
1609 DD = std::move(MergeDD);
1610 DD.Definition = Def;
1611 return;
1612 }
1613
Richard Smithcd45dbc2014-04-19 03:48:30 +00001614 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001615 bool DetectedOdrViolation = false;
1616#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1617#define MATCH_FIELD(Field) \
1618 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1619 OR_FIELD(Field)
1620 MATCH_FIELD(UserDeclaredConstructor)
1621 MATCH_FIELD(UserDeclaredSpecialMembers)
1622 MATCH_FIELD(Aggregate)
1623 MATCH_FIELD(PlainOldData)
1624 MATCH_FIELD(Empty)
1625 MATCH_FIELD(Polymorphic)
1626 MATCH_FIELD(Abstract)
1627 MATCH_FIELD(IsStandardLayout)
1628 MATCH_FIELD(HasNoNonEmptyBases)
1629 MATCH_FIELD(HasPrivateFields)
1630 MATCH_FIELD(HasProtectedFields)
1631 MATCH_FIELD(HasPublicFields)
1632 MATCH_FIELD(HasMutableFields)
1633 MATCH_FIELD(HasVariantMembers)
1634 MATCH_FIELD(HasOnlyCMembers)
1635 MATCH_FIELD(HasInClassInitializer)
1636 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001637 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001638 MATCH_FIELD(HasInheritedConstructor)
1639 MATCH_FIELD(HasInheritedAssignment)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001640 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1641 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1642 MATCH_FIELD(NeedOverloadResolutionForDestructor)
1643 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1644 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1645 MATCH_FIELD(DefaultedDestructorIsDeleted)
1646 OR_FIELD(HasTrivialSpecialMembers)
1647 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1648 MATCH_FIELD(HasIrrelevantDestructor)
1649 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001650 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001651 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1652 OR_FIELD(HasConstexprDefaultConstructor)
1653 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1654 // ComputedVisibleConversions is handled below.
1655 MATCH_FIELD(UserProvidedDefaultConstructor)
1656 OR_FIELD(DeclaredSpecialMembers)
1657 MATCH_FIELD(ImplicitCopyConstructorHasConstParam)
1658 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1659 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1660 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1661 MATCH_FIELD(IsLambda)
Richard Trieub6adf542017-02-18 02:09:28 +00001662 MATCH_FIELD(ODRHash)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001663#undef OR_FIELD
1664#undef MATCH_FIELD
1665
1666 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1667 DetectedOdrViolation = true;
1668 // FIXME: Issue a diagnostic if the base classes don't match when we come
1669 // to lazily load them.
1670
1671 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1672 // match when we come to lazily load them.
1673 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1674 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1675 DD.ComputedVisibleConversions = true;
1676 }
1677
1678 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1679 // lazily load it.
1680
1681 if (DD.IsLambda) {
1682 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1683 // when they occur within the body of a function template specialization).
1684 }
1685
1686 if (DetectedOdrViolation)
1687 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1688}
1689
Richard Smith2a9e5c52015-02-03 03:32:14 +00001690void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001691 struct CXXRecordDecl::DefinitionData *DD;
1692 ASTContext &C = Reader.getContext();
1693
1694 // Determine whether this is a lambda closure type, so that we can
1695 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001696 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001697 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001698 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001699 LCD_None);
1700 else
1701 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1702
David L. Jonesbe1557a2016-12-21 00:17:49 +00001703 ReadCXXDefinitionData(*DD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001704
Richard Smith5156c382015-01-22 01:41:56 +00001705 // We might already have a definition for this record. This can happen either
1706 // because we're reading an update record, or because we've already done some
1707 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001708 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001709 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001710 MergeDefinitionData(Canon, std::move(*DD));
1711 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001712 return;
1713 }
1714
Richard Smith97100e32015-06-20 00:22:34 +00001715 // Mark this declaration as being a definition.
1716 D->IsCompleteDefinition = true;
1717 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001718
Richard Smith97100e32015-06-20 00:22:34 +00001719 // If this is not the first declaration or is an update record, we can have
1720 // other redeclarations already. Make a note that we need to propagate the
1721 // DefinitionData pointer onto them.
1722 if (Update || Canon != D) {
1723 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001724 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001725 }
1726}
1727
Richard Smith1d209d02013-05-23 01:49:11 +00001728ASTDeclReader::RedeclarableResult
1729ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1730 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001731
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001732 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001733
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001734 enum CXXRecKind {
1735 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1736 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001737 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001738 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001739 // Merged when we merge the folding set entry in the primary template.
1740 if (!isa<ClassTemplateSpecializationDecl>(D))
1741 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001742 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001743 case CXXRecTemplate: {
1744 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001745 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001746 D->TemplateOrInstantiation = Template;
1747 if (!Template->getTemplatedDecl()) {
1748 // We've not actually loaded the ClassTemplateDecl yet, because we're
1749 // currently being loaded as its pattern. Rely on it to set up our
1750 // TypeForDecl (see VisitClassTemplateDecl).
1751 //
1752 // Beware: we do not yet know our canonical declaration, and may still
1753 // get merged once the surrounding class template has got off the ground.
1754 TypeIDForTypeDecl = 0;
1755 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001756 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001757 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001758 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001759 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001760 TemplateSpecializationKind TSK =
1761 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001762 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001763 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1764 MSI->setPointOfInstantiation(POI);
1765 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001766 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001767 break;
1768 }
1769 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001770
David L. Jonesbe1557a2016-12-21 00:17:49 +00001771 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001772 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001773 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001774 else
1775 // Propagate DefinitionData pointer from the canonical declaration.
1776 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1777
Richard Smith676c4042013-08-29 23:59:27 +00001778 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001779 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001780 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001781 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001782 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001783 // FIXME: This is wrong for the ARM ABI, where some other module may have
1784 // made this function no longer be a key function. We need an update
1785 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001786 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001787 }
Richard Smith1d209d02013-05-23 01:49:11 +00001788
1789 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001790}
1791
Richard Smithbc491202017-02-17 20:05:37 +00001792void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1793 VisitFunctionDecl(D);
1794}
1795
Sebastian Redlb3298c32010-08-18 23:56:48 +00001796void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001797 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001798
David L. Jonesbe1557a2016-12-21 00:17:49 +00001799 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001800 if (D->isCanonicalDecl()) {
1801 while (NumOverridenMethods--) {
1802 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1803 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001804 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001805 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1806 }
1807 } else {
1808 // We don't care about which declarations this used to override; we get
1809 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001810 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001811 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001812}
1813
Sebastian Redlb3298c32010-08-18 23:56:48 +00001814void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001815 // We need the inherited constructor information to merge the declaration,
1816 // so we have to read it before we call VisitCXXMethodDecl.
1817 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001818 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1819 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001820 *D->getTrailingObjects<InheritedConstructor>() =
1821 InheritedConstructor(Shadow, Ctor);
1822 }
1823
Chris Lattnerca025db2010-05-07 21:43:38 +00001824 VisitCXXMethodDecl(D);
1825}
1826
Sebastian Redlb3298c32010-08-18 23:56:48 +00001827void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001828 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001829
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001830 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001831 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1832 // FIXME: Check consistency if we have an old and new operator delete.
1833 if (!Canon->OperatorDelete)
1834 Canon->OperatorDelete = OperatorDelete;
1835 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001836}
1837
Sebastian Redlb3298c32010-08-18 23:56:48 +00001838void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001839 VisitCXXMethodDecl(D);
1840}
1841
Douglas Gregorba345522011-12-02 23:23:56 +00001842void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1843 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001844 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001845 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001846 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001847 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001848 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001849 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001850}
1851
Sebastian Redlb3298c32010-08-18 23:56:48 +00001852void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001853 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001854 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001855}
1856
Sebastian Redlb3298c32010-08-18 23:56:48 +00001857void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001858 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001859 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001860 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001861 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001862 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001863 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001864 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001865 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001866 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001867 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001868 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001869}
1870
Sebastian Redlb3298c32010-08-18 23:56:48 +00001871void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001872 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001873 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001874 D->NumParams = NumParams;
1875 D->Params = new TemplateParameterList*[NumParams];
1876 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001877 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001878 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001879 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001880 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001881 D->Friend = GetTypeSourceInfo();
1882 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001883}
1884
Richard Smithf17fdbd2014-04-24 02:25:27 +00001885DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001886 VisitNamedDecl(D);
1887
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001888 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001889 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001890 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001891 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001892 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001893
Richard Smithf17fdbd2014-04-24 02:25:27 +00001894 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001895}
1896
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001897ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001898ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001899 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001900
Douglas Gregor68444de2012-01-14 15:13:49 +00001901 // Make sure we've allocated the Common pointer first. We do this before
1902 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1903 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1904 if (!CanonD->Common) {
1905 CanonD->Common = CanonD->newCommon(Reader.getContext());
1906 Reader.PendingDefinitions.insert(CanonD);
1907 }
1908 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001909
Douglas Gregor68444de2012-01-14 15:13:49 +00001910 // If this is the first declaration of the template, fill in the information
1911 // for the 'common' pointer.
1912 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001913 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001914 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001915 assert(RTD->getKind() == D->getKind() &&
1916 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001917 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001918 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001919 D->setMemberSpecialization();
1920 }
1921 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001922
Richard Smithf17fdbd2014-04-24 02:25:27 +00001923 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001924 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001925
Richard Smithf17fdbd2014-04-24 02:25:27 +00001926 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001927
Richard Smithd46d6de2013-10-13 23:50:45 +00001928 // If we merged the template with a prior declaration chain, merge the common
1929 // pointer.
1930 // FIXME: Actually merge here, don't just overwrite.
1931 D->Common = D->getCanonicalDecl()->Common;
1932
Douglas Gregor68444de2012-01-14 15:13:49 +00001933 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001934}
1935
Richard Smith0b884372015-02-27 00:25:58 +00001936static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1937 SmallVectorImpl<DeclID> &IDs) {
1938 assert(!IDs.empty() && "no IDs to add to list");
Richard Smith36befce2015-02-28 01:45:19 +00001939 if (Old) {
1940 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1941 std::sort(IDs.begin(), IDs.end());
1942 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1943 }
Richard Smith0b884372015-02-27 00:25:58 +00001944
Richard Smith36befce2015-02-28 01:45:19 +00001945 auto *Result = new (Context) DeclID[1 + IDs.size()];
1946 *Result = IDs.size();
1947 std::copy(IDs.begin(), IDs.end(), Result + 1);
Richard Smith0b884372015-02-27 00:25:58 +00001948 return Result;
1949}
1950
Sebastian Redlb3298c32010-08-18 23:56:48 +00001951void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001952 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001953
Douglas Gregor68444de2012-01-14 15:13:49 +00001954 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001955 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1956 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001957 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001958 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001959
Richard Smith0b884372015-02-27 00:25:58 +00001960 if (!SpecIDs.empty()) {
1961 auto *CommonPtr = D->getCommonPtr();
1962 CommonPtr->LazySpecializations = newDeclIDList(
1963 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001964 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001965 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00001966
1967 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1968 // We were loaded before our templated declaration was. We've not set up
1969 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1970 // it now.
1971 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00001972 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00001973 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001974}
1975
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001976void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
1977 llvm_unreachable("BuiltinTemplates are not serialized");
1978}
1979
Larisse Voufo30616382013-08-23 22:21:36 +00001980/// TODO: Unify with ClassTemplateDecl version?
1981/// May require unifying ClassTemplateDecl and
1982/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00001983void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
1984 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
1985
1986 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001987 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00001988 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001989 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001990 ReadDeclIDList(SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001991
Richard Smith0b884372015-02-27 00:25:58 +00001992 if (!SpecIDs.empty()) {
1993 auto *CommonPtr = D->getCommonPtr();
1994 CommonPtr->LazySpecializations = newDeclIDList(
1995 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001996 }
1997 }
1998}
1999
Richard Smith1d209d02013-05-23 01:49:11 +00002000ASTDeclReader::RedeclarableResult
2001ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2002 ClassTemplateSpecializationDecl *D) {
2003 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002004
Douglas Gregor4163aca2011-09-09 21:34:22 +00002005 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002006 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002007 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002008 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002009 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002010 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002011 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002012 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002013 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002014 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2015 = new (C) ClassTemplateSpecializationDecl::
2016 SpecializedPartialSpecialization();
2017 PS->PartialSpecialization
2018 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2019 PS->TemplateArgs = ArgList;
2020 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002021 }
2022 }
2023
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002024 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002025 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002026 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002027 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002028 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002029
David L. Jonesbe1557a2016-12-21 00:17:49 +00002030 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002031 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002032 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002033 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002034 // Set this as, or find, the canonical declaration for this specialization
2035 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002036 if (ClassTemplatePartialSpecializationDecl *Partial =
2037 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002038 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002039 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002040 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002041 CanonSpec =
2042 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2043 }
2044 // If there was already a canonical specialization, merge into it.
2045 if (CanonSpec != D) {
2046 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2047
2048 // This declaration might be a definition. Merge with any existing
2049 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002050 if (auto *DDD = D->DefinitionData) {
2051 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002052 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002053 else
Richard Smith053f6c62014-05-16 23:01:30 +00002054 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002055 }
Richard Smith547864d2014-07-11 18:22:58 +00002056 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002057 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002058 }
2059 }
Richard Smith1d209d02013-05-23 01:49:11 +00002060
Richard Smithd55889a2013-09-09 16:55:27 +00002061 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002062 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002063 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2064 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2065 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002066 ExplicitInfo->ExternLoc = ReadSourceLocation();
2067 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002068 D->ExplicitInfo = ExplicitInfo;
2069 }
2070
Richard Smith1d209d02013-05-23 01:49:11 +00002071 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002072}
2073
Sebastian Redlb3298c32010-08-18 23:56:48 +00002074void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002075 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002076 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002077
David L. Jonesb6a8f022016-12-21 04:34:52 +00002078 D->TemplateParams = Record.readTemplateParameterList();
2079 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002080
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002081 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002082 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002083 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002084 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002085 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002086 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002087}
2088
Sebastian Redlb7448632011-08-31 13:59:56 +00002089void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2090 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002091 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002092 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002093}
2094
Sebastian Redlb3298c32010-08-18 23:56:48 +00002095void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002096 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002097
Douglas Gregor68444de2012-01-14 15:13:49 +00002098 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002099 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002100 SmallVector<serialization::DeclID, 32> SpecIDs;
2101 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002102
Richard Smith0b884372015-02-27 00:25:58 +00002103 if (!SpecIDs.empty()) {
2104 auto *CommonPtr = D->getCommonPtr();
2105 CommonPtr->LazySpecializations = newDeclIDList(
2106 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Richard Smithfeb3e1a2013-06-28 04:37:53 +00002107 }
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002108 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002109}
2110
Larisse Voufo30616382013-08-23 22:21:36 +00002111/// TODO: Unify with ClassTemplateSpecializationDecl version?
2112/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2113/// VarTemplate(Partial)SpecializationDecl with a new data
2114/// structure Template(Partial)SpecializationDecl, and
2115/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002116ASTDeclReader::RedeclarableResult
2117ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2118 VarTemplateSpecializationDecl *D) {
2119 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2120
2121 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002122 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002123 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2124 D->SpecializedTemplate = VTD;
2125 } else {
2126 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002127 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002128 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002129 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002130 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2131 new (C)
2132 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2133 PS->PartialSpecialization =
2134 cast<VarTemplatePartialSpecializationDecl>(InstD);
2135 PS->TemplateArgs = ArgList;
2136 D->SpecializedTemplate = PS;
2137 }
2138 }
2139
2140 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002141 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002142 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2143 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2144 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002145 ExplicitInfo->ExternLoc = ReadSourceLocation();
2146 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002147 D->ExplicitInfo = ExplicitInfo;
2148 }
2149
2150 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002151 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002152 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002153 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002154 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002155
David L. Jonesbe1557a2016-12-21 00:17:49 +00002156 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002157 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002158 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002159 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002160 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002161 if (VarTemplatePartialSpecializationDecl *Partial =
2162 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002163 CanonPattern->getCommonPtr()->PartialSpecializations
2164 .GetOrInsertNode(Partial);
2165 } else {
2166 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2167 }
2168 }
2169 }
2170
2171 return Redecl;
2172}
2173
Larisse Voufo30616382013-08-23 22:21:36 +00002174/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2175/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2176/// VarTemplate(Partial)SpecializationDecl with a new data
2177/// structure Template(Partial)SpecializationDecl, and
2178/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002179void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2180 VarTemplatePartialSpecializationDecl *D) {
2181 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2182
David L. Jonesb6a8f022016-12-21 04:34:52 +00002183 D->TemplateParams = Record.readTemplateParameterList();
2184 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002185
2186 // These are read/set from/to the first declaration.
2187 if (ThisDeclID == Redecl.getFirstID()) {
2188 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002189 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002190 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002191 }
2192}
2193
Sebastian Redlb3298c32010-08-18 23:56:48 +00002194void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002195 VisitTypeDecl(D);
2196
David L. Jonesbe1557a2016-12-21 00:17:49 +00002197 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002198
David L. Jonesbe1557a2016-12-21 00:17:49 +00002199 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002200 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002201}
2202
Sebastian Redlb3298c32010-08-18 23:56:48 +00002203void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002204 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002205 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002206 D->setDepth(Record.readInt());
2207 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002208 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002209 auto TypesAndInfos =
2210 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002211 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002212 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002213 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002214 }
2215 } else {
2216 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002217 D->ParameterPack = Record.readInt();
2218 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002219 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002220 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002221}
2222
Sebastian Redlb3298c32010-08-18 23:56:48 +00002223void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002224 VisitTemplateDecl(D);
2225 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002226 D->setDepth(Record.readInt());
2227 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002228 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002229 TemplateParameterList **Data =
2230 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002231 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2232 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002233 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002234 } else {
2235 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002236 D->ParameterPack = Record.readInt();
2237 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002238 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002239 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002240 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002241}
2242
Richard Smith3f1b5d02011-05-05 21:57:07 +00002243void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2244 VisitRedeclarableTemplateDecl(D);
2245}
2246
Sebastian Redlb3298c32010-08-18 23:56:48 +00002247void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002248 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002249 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002250 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002251 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002252 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002253}
2254
Michael Han84324352013-02-22 17:15:32 +00002255void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2256 VisitDecl(D);
2257}
2258
Mike Stump11289f42009-09-09 15:08:12 +00002259std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002260ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002261 uint64_t LexicalOffset = ReadLocalOffset();
2262 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002263 return std::make_pair(LexicalOffset, VisibleOffset);
2264}
2265
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002266template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002267ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002268ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002269 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002270 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002271
Richard Smith5fc18a92015-07-12 23:43:21 +00002272 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002273 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002274
Richard Smithd61d4ac2015-08-22 20:13:39 +00002275 uint64_t RedeclOffset = 0;
2276
Douglas Gregor358cd442012-01-15 16:58:34 +00002277 // 0 indicates that this declaration was the only declaration of its entity,
2278 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002279 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002280 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002281 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002282 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002283 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002284 // This declaration was the first local declaration, but may have imported
2285 // other declarations.
2286 IsKeyDecl = N == 1;
2287 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002288
Richard Smithfe620d22015-03-05 23:24:12 +00002289 // We have some declarations that must be before us in our redeclaration
2290 // chain. Read them now, and remember that we ought to merge with one of
2291 // them.
2292 // FIXME: Provide a known merge target to the second and subsequent such
2293 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002294 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002295 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002296
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002297 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002298 } else {
2299 // This declaration was not the first local declaration. Read the first
2300 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002301 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002302 }
2303
Douglas Gregor358cd442012-01-15 16:58:34 +00002304 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2305 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002306 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2307 // We temporarily set the first (canonical) declaration as the previous one
2308 // which is the one that matters and mark the real previous DeclID to be
2309 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002310 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002311 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002312 }
Richard Smithd8a83712015-08-22 01:47:18 +00002313
Richard Smithd8a83712015-08-22 01:47:18 +00002314 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002315
2316 // Note that we need to load local redeclarations of this decl and build a
2317 // decl chain for them. This must happen *after* we perform the preloading
2318 // above; this ensures that the redeclaration chain is built in the correct
2319 // order.
2320 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002321 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002322
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002323 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002324}
2325
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002326/// \brief Attempts to merge the given declaration (D) with another declaration
2327/// of the same entity.
2328template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002329void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002330 RedeclarableResult &Redecl,
2331 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002332 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002333 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002334 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002335
Richard Smith202850a2015-03-10 02:57:50 +00002336 // If we're not the canonical declaration, we don't need to merge.
2337 if (!DBase->isFirstDecl())
2338 return;
2339
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002340 T *D = static_cast<T*>(DBase);
2341
Richard Smith5a4737c2015-02-06 02:42:59 +00002342 if (auto *Existing = Redecl.getKnownMergeTarget())
2343 // We already know of an existing declaration we should merge with.
2344 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2345 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002346 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002347 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2348}
2349
2350/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2351/// We use this to put code in a template that will only be valid for certain
2352/// instantiations.
2353template<typename T> static T assert_cast(T t) { return t; }
2354template<typename T> static T assert_cast(...) {
2355 llvm_unreachable("bad assert_cast");
2356}
2357
2358/// \brief Merge together the pattern declarations from two template
2359/// declarations.
2360void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2361 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002362 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002363 auto *DPattern = D->getTemplatedDecl();
2364 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002365 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2366 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002367 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002368
Richard Smith547864d2014-07-11 18:22:58 +00002369 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2370 // Merge with any existing definition.
2371 // FIXME: This is duplicated in several places. Refactor.
2372 auto *ExistingClass =
2373 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002374 if (auto *DDD = DClass->DefinitionData) {
2375 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002376 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002377 } else {
2378 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002379 // We may have skipped this before because we thought that DClass
2380 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002381 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002382 }
2383 }
2384 DClass->DefinitionData = ExistingClass->DefinitionData;
2385
Richard Smithf17fdbd2014-04-24 02:25:27 +00002386 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2387 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002388 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002389 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2390 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2391 Result);
2392 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2393 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002394 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2395 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2396 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002397 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002398}
2399
2400/// \brief Attempts to merge the given declaration (D) with another declaration
2401/// of the same entity.
2402template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002403void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2404 RedeclarableResult &Redecl,
2405 DeclID TemplatePatternID) {
2406 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002407 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002408 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002409 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002410 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2411 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002412
Richard Smithd55889a2013-09-09 16:55:27 +00002413 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002414 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002415 // appropriate canonical declaration.
2416 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002417 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002418 ExistingCanon->Used |= D->Used;
2419 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002420
2421 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002422 // We cannot have loaded any redeclarations of this declaration yet, so
2423 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002424 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002425 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002426 assert_cast<NamespaceDecl*>(ExistingCanon));
2427
2428 // When we merge a template, merge its pattern.
2429 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2430 mergeTemplatePattern(
2431 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002432 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002433
Richard Smith5fc18a92015-07-12 23:43:21 +00002434 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002435 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002436 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002437 }
2438}
2439
Richard Smith0b87e072013-10-07 08:02:11 +00002440/// \brief Attempts to merge the given declaration (D) with another declaration
2441/// of the same entity, for the case where the entity is not actually
2442/// redeclarable. This happens, for instance, when merging the fields of
2443/// identical class definitions from two different modules.
2444template<typename T>
2445void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2446 // If modules are not available, there is no reason to perform this merge.
2447 if (!Reader.getContext().getLangOpts().Modules)
2448 return;
2449
Richard Smith01a73372013-10-15 22:02:41 +00002450 // ODR-based merging is only performed in C++. In C, identically-named things
2451 // in different translation units are not redeclarations (but may still have
2452 // compatible types).
2453 if (!Reader.getContext().getLangOpts().CPlusPlus)
2454 return;
2455
Richard Smith0b87e072013-10-07 08:02:11 +00002456 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2457 if (T *Existing = ExistingRes)
2458 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2459 Existing->getCanonicalDecl());
2460}
2461
Alexey Bataeva769e072013-03-22 06:34:35 +00002462void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2463 VisitDecl(D);
2464 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002465 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002466 Vars.reserve(NumVars);
2467 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002468 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002469 }
2470 D->setVars(Vars);
2471}
2472
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002473void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002474 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002475 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002476 D->setCombiner(Record.readExpr());
2477 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002478 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002479}
2480
Alexey Bataev4244be22016-02-11 05:35:55 +00002481void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002482 VisitVarDecl(D);
2483}
2484
Chris Lattner487412d2009-04-27 05:27:42 +00002485//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002486// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002487//===----------------------------------------------------------------------===//
2488
Chris Lattner8f63ab52009-04-27 06:01:06 +00002489/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002490void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2491 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002492 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002493 attr::Kind Kind = (attr::Kind)Record.readInt();
2494 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002495
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002496#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002497
2498 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002499 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002500 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002501}
2502
2503//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002504// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002505//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002506
2507/// \brief Note that we have loaded the declaration with the given
2508/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002509///
Chris Lattner487412d2009-04-27 05:27:42 +00002510/// This routine notes that this declaration has already been loaded,
2511/// so that future GetDecl calls will return this declaration rather
2512/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002513inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002514 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2515 DeclsLoaded[Index] = D;
2516}
2517
2518
2519/// \brief Determine whether the consumer will be interested in seeing
2520/// this declaration (via HandleTopLevelDecl).
2521///
2522/// This routine should return true for anything that might affect
2523/// code generation, e.g., inline function definitions, Objective-C
2524/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002525static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002526 // An ObjCMethodDecl is never considered as "interesting" because its
2527 // implementation container always is.
2528
Richard Smithdc1f0422016-07-20 19:10:16 +00002529 // An ImportDecl or VarDecl imported from a module will get emitted when
2530 // we import the relevant module.
2531 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && Ctx.DeclMustBeEmitted(D) &&
2532 D->getImportedOwningModule())
2533 return false;
2534
Douglas Gregor87d81242011-09-10 00:22:34 +00002535 if (isa<FileScopeAsmDecl>(D) ||
2536 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002537 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002538 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002539 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002540 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002541 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002542 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2543 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002544 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002545 return Var->isFileVarDecl() &&
2546 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002547 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002548 return Func->doesThisDeclarationHaveABody() || HasBody;
Douglas Gregor87d81242011-09-10 00:22:34 +00002549
2550 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002551}
2552
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002553/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002554ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002555ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002556 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2557 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002558 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002559 const DeclOffset &DOffs =
2560 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002561 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002562 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002563}
2564
Douglas Gregord32f0352011-07-22 06:10:01 +00002565ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002566 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002567 = GlobalBitOffsetsMap.find(GlobalOffset);
2568
2569 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2570 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2571}
2572
Douglas Gregorde3ef502011-11-30 23:21:26 +00002573uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002574 return LocalOffset + M.GlobalBitOffset;
2575}
2576
Richard Smithbf78e642013-06-24 22:51:00 +00002577static bool isSameTemplateParameterList(const TemplateParameterList *X,
2578 const TemplateParameterList *Y);
2579
2580/// \brief Determine whether two template parameters are similar enough
2581/// that they may be used in declarations of the same template.
2582static bool isSameTemplateParameter(const NamedDecl *X,
2583 const NamedDecl *Y) {
2584 if (X->getKind() != Y->getKind())
2585 return false;
2586
2587 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2588 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2589 return TX->isParameterPack() == TY->isParameterPack();
2590 }
2591
2592 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2593 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2594 return TX->isParameterPack() == TY->isParameterPack() &&
2595 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2596 }
2597
2598 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2599 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2600 return TX->isParameterPack() == TY->isParameterPack() &&
2601 isSameTemplateParameterList(TX->getTemplateParameters(),
2602 TY->getTemplateParameters());
2603}
2604
Richard Smith32952e12014-10-14 02:00:47 +00002605static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2606 if (auto *NS = X->getAsNamespace())
2607 return NS;
2608 if (auto *NAS = X->getAsNamespaceAlias())
2609 return NAS->getNamespace();
2610 return nullptr;
2611}
2612
2613static bool isSameQualifier(const NestedNameSpecifier *X,
2614 const NestedNameSpecifier *Y) {
2615 if (auto *NSX = getNamespace(X)) {
2616 auto *NSY = getNamespace(Y);
2617 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2618 return false;
2619 } else if (X->getKind() != Y->getKind())
2620 return false;
2621
2622 // FIXME: For namespaces and types, we're permitted to check that the entity
2623 // is named via the same tokens. We should probably do so.
2624 switch (X->getKind()) {
2625 case NestedNameSpecifier::Identifier:
2626 if (X->getAsIdentifier() != Y->getAsIdentifier())
2627 return false;
2628 break;
2629 case NestedNameSpecifier::Namespace:
2630 case NestedNameSpecifier::NamespaceAlias:
2631 // We've already checked that we named the same namespace.
2632 break;
2633 case NestedNameSpecifier::TypeSpec:
2634 case NestedNameSpecifier::TypeSpecWithTemplate:
2635 if (X->getAsType()->getCanonicalTypeInternal() !=
2636 Y->getAsType()->getCanonicalTypeInternal())
2637 return false;
2638 break;
2639 case NestedNameSpecifier::Global:
2640 case NestedNameSpecifier::Super:
2641 return true;
2642 }
2643
2644 // Recurse into earlier portion of NNS, if any.
2645 auto *PX = X->getPrefix();
2646 auto *PY = Y->getPrefix();
2647 if (PX && PY)
2648 return isSameQualifier(PX, PY);
2649 return !PX && !PY;
2650}
2651
Richard Smithbf78e642013-06-24 22:51:00 +00002652/// \brief Determine whether two template parameter lists are similar enough
2653/// that they may be used in declarations of the same template.
2654static bool isSameTemplateParameterList(const TemplateParameterList *X,
2655 const TemplateParameterList *Y) {
2656 if (X->size() != Y->size())
2657 return false;
2658
2659 for (unsigned I = 0, N = X->size(); I != N; ++I)
2660 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2661 return false;
2662
2663 return true;
2664}
2665
George Burgess IV95845082017-02-15 22:43:27 +00002666/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002667/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002668static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2669 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002670 // Note that pass_object_size attributes are represented in the function's
2671 // ExtParameterInfo, so we don't need to check them here.
2672
George Burgess IV95845082017-02-15 22:43:27 +00002673 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2674 // Since this is an equality check, we can ignore that enable_if attrs show up
2675 // in reverse order.
2676 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2677 AEnableIfs.push_back(EIA);
2678
2679 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2680 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2681 BEnableIfs.push_back(EIA);
2682
2683 // Two very common cases: either we have 0 enable_if attrs, or we have an
2684 // unequal number of enable_if attrs.
2685 if (AEnableIfs.empty() && BEnableIfs.empty())
2686 return true;
2687
2688 if (AEnableIfs.size() != BEnableIfs.size())
2689 return false;
2690
2691 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2692 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2693 Cand1ID.clear();
2694 Cand2ID.clear();
2695
2696 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2697 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2698 if (Cand1ID != Cand2ID)
2699 return false;
2700 }
2701
George Burgess IV95845082017-02-15 22:43:27 +00002702 return true;
2703}
2704
Douglas Gregor022857e2011-12-22 01:48:48 +00002705/// \brief Determine whether the two declarations refer to the same entity.
2706static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2707 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002708
Douglas Gregor022857e2011-12-22 01:48:48 +00002709 if (X == Y)
2710 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002711
Douglas Gregor022857e2011-12-22 01:48:48 +00002712 // Must be in the same context.
2713 if (!X->getDeclContext()->getRedeclContext()->Equals(
2714 Y->getDeclContext()->getRedeclContext()))
2715 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002716
2717 // Two typedefs refer to the same entity if they have the same underlying
2718 // type.
2719 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2720 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2721 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2722 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002723
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002724 // Must have the same kind.
2725 if (X->getKind() != Y->getKind())
2726 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002727
Douglas Gregorda389302012-01-01 21:47:52 +00002728 // Objective-C classes and protocols with the same name always match.
2729 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002730 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002731
2732 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002733 // No need to handle these here: we merge them when adding them to the
2734 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002735 return false;
2736 }
Richard Smithd55889a2013-09-09 16:55:27 +00002737
Douglas Gregor2009cee2012-01-03 22:46:00 +00002738 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002739 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2740 TagDecl *TagY = cast<TagDecl>(Y);
2741 return (TagX->getTagKind() == TagY->getTagKind()) ||
2742 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2743 TagX->getTagKind() == TTK_Interface) &&
2744 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2745 TagY->getTagKind() == TTK_Interface));
2746 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002747
Joao Matose9a3ed42012-08-31 22:18:20 +00002748 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002749 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2750 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002751 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2752 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002753 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2754 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2755 if (CtorX->getInheritedConstructor() &&
2756 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2757 CtorY->getInheritedConstructor().getConstructor()))
2758 return false;
2759 }
George Burgess IV95845082017-02-15 22:43:27 +00002760 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
2761 FuncX->getASTContext().hasSameType(FuncX->getType(),
2762 FuncY->getType()) &&
2763 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002764 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002765
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002766 // Variables with the same type and linkage match.
2767 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2768 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002769 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2770 ASTContext &C = VarX->getASTContext();
2771 if (C.hasSameType(VarX->getType(), VarY->getType()))
2772 return true;
2773
2774 // We can get decls with different types on the redecl chain. Eg.
2775 // template <typename T> struct S { static T Var[]; }; // #1
2776 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2777 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002778 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002779 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2780 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2781 if (!VarXTy || !VarYTy)
2782 return false;
2783 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2784 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2785 }
2786 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002787 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002788
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002789 // Namespaces with the same name and inlinedness match.
2790 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2791 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2792 return NamespaceX->isInline() == NamespaceY->isInline();
2793 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002794
Richard Smith8f8f05c2013-06-24 04:45:28 +00002795 // Identical template names and kinds match if their template parameter lists
2796 // and patterns match.
2797 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002798 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002799 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002800 TemplateY->getTemplatedDecl()) &&
2801 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2802 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002803 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002804
Richard Smith0b87e072013-10-07 08:02:11 +00002805 // Fields with the same name and the same type match.
2806 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2807 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002808 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002809 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2810 }
2811
Richard Smith8cbd8952015-08-04 02:05:09 +00002812 // Indirect fields with the same target field match.
2813 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2814 auto *IFDY = cast<IndirectFieldDecl>(Y);
2815 return IFDX->getAnonField()->getCanonicalDecl() ==
2816 IFDY->getAnonField()->getCanonicalDecl();
2817 }
2818
Richard Smith01a73372013-10-15 22:02:41 +00002819 // Enumerators with the same name match.
2820 if (isa<EnumConstantDecl>(X))
2821 // FIXME: Also check the value is odr-equivalent.
2822 return true;
2823
Richard Smithfd8634a2013-10-23 02:17:46 +00002824 // Using shadow declarations with the same target match.
2825 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2826 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2827 return USX->getTargetDecl() == USY->getTargetDecl();
2828 }
2829
Richard Smith32952e12014-10-14 02:00:47 +00002830 // Using declarations with the same qualifier match. (We already know that
2831 // the name matches.)
2832 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2833 auto *UY = cast<UsingDecl>(Y);
2834 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2835 UX->hasTypename() == UY->hasTypename() &&
2836 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2837 }
2838 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2839 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2840 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2841 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2842 }
2843 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2844 return isSameQualifier(
2845 UX->getQualifier(),
2846 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2847
Richard Smithf4634362014-09-03 23:11:22 +00002848 // Namespace alias definitions with the same target match.
2849 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2850 auto *NAY = cast<NamespaceAliasDecl>(Y);
2851 return NAX->getNamespace()->Equals(NAY->getNamespace());
2852 }
2853
Douglas Gregor022857e2011-12-22 01:48:48 +00002854 return false;
2855}
2856
Richard Smithd55889a2013-09-09 16:55:27 +00002857/// Find the context in which we should search for previous declarations when
2858/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002859DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2860 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002861 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2862 return ND->getOriginalNamespace();
2863
Richard Smith8a639892015-01-24 01:07:20 +00002864 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2865 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002866 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002867 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002868 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002869
2870 // If there's no definition yet, then DC's definition is added by an update
2871 // record, but we've not yet loaded that update record. In this case, we
2872 // commit to DC being the canonical definition now, and will fix this when
2873 // we load the update record.
2874 if (!DD) {
2875 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2876 RD->IsCompleteDefinition = true;
2877 RD->DefinitionData = DD;
2878 RD->getCanonicalDecl()->DefinitionData = DD;
2879
2880 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002881 Reader.PendingFakeDefinitionData.insert(
2882 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002883 }
2884
2885 return DD->Definition;
2886 }
Richard Smithd55889a2013-09-09 16:55:27 +00002887
Richard Smith01a73372013-10-15 22:02:41 +00002888 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002889 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2890 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002891
Richard Smith4eca9b92015-02-04 23:37:59 +00002892 // We can see the TU here only if we have no Sema object. In that case,
2893 // there's no TU scope to look in, so using the DC alone is sufficient.
2894 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2895 return TU;
2896
Craig Toppera13603a2014-05-22 05:54:18 +00002897 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002898}
2899
Douglas Gregor022857e2011-12-22 01:48:48 +00002900ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002901 // Record that we had a typedef name for linkage whether or not we merge
2902 // with that declaration.
2903 if (TypedefNameForLinkage) {
2904 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2905 Reader.ImportedTypedefNamesForLinkage.insert(
2906 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2907 return;
2908 }
2909
Douglas Gregor9b47f942012-01-09 17:38:47 +00002910 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002911 return;
Richard Smith95d99302013-07-13 02:00:19 +00002912
Richard Smith70d58502014-08-30 00:04:23 +00002913 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002914 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002915 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002916 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2917 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002918 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002919 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002920 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002921 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2922 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002923 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002924 // Add the declaration to its redeclaration context so later merging
2925 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002926 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002927 }
2928}
2929
Richard Smith88ebade2014-08-23 01:45:27 +00002930/// Find the declaration that should be merged into, given the declaration found
2931/// by name lookup. If we're merging an anonymous declaration within a typedef,
2932/// we need a matching typedef, and we merge with the type inside it.
2933static NamedDecl *getDeclForMerging(NamedDecl *Found,
2934 bool IsTypedefNameForLinkage) {
2935 if (!IsTypedefNameForLinkage)
2936 return Found;
2937
2938 // If we found a typedef declaration that gives a name to some other
2939 // declaration, then we want that inner declaration. Declarations from
2940 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002941 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002942 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002943
2944 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00002945 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00002946
Hans Wennborgdcfba332015-10-06 23:40:43 +00002947 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00002948}
2949
Richard Smithd08aeb62014-08-28 01:33:39 +00002950NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2951 DeclContext *DC,
2952 unsigned Index) {
2953 // If the lexical context has been merged, look into the now-canonical
2954 // definition.
2955 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2956 DC = Merged;
2957
2958 // If we've seen this before, return the canonical declaration.
2959 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2960 if (Index < Previous.size() && Previous[Index])
2961 return Previous[Index];
2962
2963 // If this is the first time, but we have parsed a declaration of the context,
2964 // build the anonymous declaration list from the parsed declaration.
2965 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00002966 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
2967 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00002968 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
2969 else
Richard Smith2b560572015-02-07 03:11:11 +00002970 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
2971 });
Richard Smithd08aeb62014-08-28 01:33:39 +00002972 }
2973
2974 return Index < Previous.size() ? Previous[Index] : nullptr;
2975}
2976
2977void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
2978 DeclContext *DC, unsigned Index,
2979 NamedDecl *D) {
2980 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2981 DC = Merged;
2982
2983 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2984 if (Index >= Previous.size())
2985 Previous.resize(Index + 1);
2986 if (!Previous[Index])
2987 Previous[Index] = D;
2988}
2989
Douglas Gregor022857e2011-12-22 01:48:48 +00002990ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00002991 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
2992 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00002993
Richard Smithd08aeb62014-08-28 01:33:39 +00002994 if (!Name && !needsAnonymousDeclarationNumber(D)) {
2995 // Don't bother trying to find unnamed declarations that are in
2996 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00002997 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
2998 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00002999 Result.suppress();
3000 return Result;
3001 }
Richard Smith95d99302013-07-13 02:00:19 +00003002
Douglas Gregor022857e2011-12-22 01:48:48 +00003003 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003004 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003005 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003006 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003007 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3008 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003009 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3010 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003011 // Go on to check in other places in case an existing typedef name
3012 // was not imported.
3013 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003014
Richard Smith2b560572015-02-07 03:11:11 +00003015 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003016 // This is an anonymous declaration that we may need to merge. Look it up
3017 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003018 if (auto *Existing = getAnonymousDeclForMerging(
3019 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3020 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003021 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3022 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003023 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003024 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003025 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003026
3027 // Temporarily consider the identifier to be up-to-date. We don't want to
3028 // cause additional lookups here.
3029 class UpToDateIdentifierRAII {
3030 IdentifierInfo *II;
3031 bool WasOutToDate;
3032
3033 public:
3034 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3035 : II(II), WasOutToDate(false)
3036 {
3037 if (II) {
3038 WasOutToDate = II->isOutOfDate();
3039 if (WasOutToDate)
3040 II->setOutOfDate(false);
3041 }
3042 }
3043
3044 ~UpToDateIdentifierRAII() {
3045 if (WasOutToDate)
3046 II->setOutOfDate(true);
3047 }
3048 } UpToDate(Name.getAsIdentifierInfo());
3049
Douglas Gregor2009cee2012-01-03 22:46:00 +00003050 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003051 IEnd = IdResolver.end();
3052 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003053 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003054 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003055 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3056 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003057 }
Richard Smith8a639892015-01-24 01:07:20 +00003058 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003059 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003060 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003061 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003062 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003063 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3064 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003065 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003066 } else {
3067 // Not in a mergeable context.
3068 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003069 }
Richard Smithd55889a2013-09-09 16:55:27 +00003070
Richard Smith2b9e3e32013-10-18 06:05:18 +00003071 // If this declaration is from a merged context, make a note that we need to
3072 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003073 //
3074 // FIXME: We should do something similar if we merge two definitions of the
3075 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003076 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3077 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3078 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003079 Reader.PendingOdrMergeChecks.push_back(D);
3080
Richard Smithd08aeb62014-08-28 01:33:39 +00003081 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003082 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003083}
3084
Richard Smithb321ecb2014-05-13 01:15:00 +00003085template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003086Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3087 return D->RedeclLink.getLatestNotUpdated();
3088}
3089Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3090 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3091}
3092
3093Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3094 assert(D);
3095
3096 switch (D->getKind()) {
3097#define ABSTRACT_DECL(TYPE)
3098#define DECL(TYPE, BASE) \
3099 case Decl::TYPE: \
3100 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3101#include "clang/AST/DeclNodes.inc"
3102 }
3103 llvm_unreachable("unknown decl kind");
3104}
3105
Richard Smith8ce51082015-03-11 01:44:51 +00003106Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3107 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3108}
3109
Richard Smithc3a53252015-02-28 05:57:02 +00003110template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003111void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3112 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003113 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003114 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003115 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003116}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003117
Richard Smith24d166c2014-07-31 23:52:38 +00003118namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003119template<>
3120void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003121 Redeclarable<VarDecl> *D,
3122 Decl *Previous, Decl *Canon) {
3123 VarDecl *VD = static_cast<VarDecl*>(D);
3124 VarDecl *PrevVD = cast<VarDecl>(Previous);
3125 D->RedeclLink.setPrevious(PrevVD);
3126 D->First = PrevVD->First;
3127
3128 // We should keep at most one definition on the chain.
3129 // FIXME: Cache the definition once we've found it. Building a chain with
3130 // N definitions currently takes O(N^2) time here.
3131 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3132 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3133 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3134 Reader.mergeDefinitionVisibility(CurD, VD);
3135 VD->demoteThisDefinitionToDeclaration();
3136 break;
3137 }
3138 }
3139 }
3140}
3141
3142template<>
3143void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003144 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003145 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003146 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3147 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3148
3149 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003150 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003151
3152 // If the previous declaration is an inline function declaration, then this
3153 // declaration is too.
3154 if (PrevFD->IsInline != FD->IsInline) {
3155 // FIXME: [dcl.fct.spec]p4:
3156 // If a function with external linkage is declared inline in one
3157 // translation unit, it shall be declared inline in all translation
3158 // units in which it appears.
3159 //
3160 // Be careful of this case:
3161 //
3162 // module A:
3163 // template<typename T> struct X { void f(); };
3164 // template<typename T> inline void X<T>::f() {}
3165 //
3166 // module B instantiates the declaration of X<int>::f
3167 // module C instantiates the definition of X<int>::f
3168 //
3169 // If module B and C are merged, we do not have a violation of this rule.
3170 FD->IsInline = true;
3171 }
3172
Richard Smith9e2341d2015-03-23 03:25:59 +00003173 // If we need to propagate an exception specification along the redecl
3174 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003175 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3176 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003177 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003178 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3179 bool WasUnresolved =
3180 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3181 if (IsUnresolved != WasUnresolved)
3182 Reader.PendingExceptionSpecUpdates.insert(
3183 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003184 }
3185}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003186} // end namespace clang
3187
Richard Smith6de7a242014-07-31 23:46:44 +00003188void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003189 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3190}
3191
Richard Smith8346e522015-06-10 01:47:58 +00003192/// Inherit the default template argument from \p From to \p To. Returns
3193/// \c false if there is no default template for \p From.
3194template <typename ParmDecl>
3195static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3196 Decl *ToD) {
3197 auto *To = cast<ParmDecl>(ToD);
3198 if (!From->hasDefaultArgument())
3199 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003200 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003201 return true;
3202}
3203
3204static void inheritDefaultTemplateArguments(ASTContext &Context,
3205 TemplateDecl *From,
3206 TemplateDecl *To) {
3207 auto *FromTP = From->getTemplateParameters();
3208 auto *ToTP = To->getTemplateParameters();
3209 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3210
3211 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3212 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003213 if (FromParam->isParameterPack())
3214 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003215 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3216
3217 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003218 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003219 break;
3220 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003221 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003222 break;
3223 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003224 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003225 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3226 break;
3227 }
3228 }
3229}
3230
Richard Smith6de7a242014-07-31 23:46:44 +00003231void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003232 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003233 assert(D && Previous);
3234
3235 switch (D->getKind()) {
3236#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003237#define DECL(TYPE, BASE) \
3238 case Decl::TYPE: \
3239 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003240 break;
3241#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003242 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003243
3244 // If the declaration was visible in one module, a redeclaration of it in
3245 // another module remains visible even if it wouldn't be visible by itself.
3246 //
3247 // FIXME: In this case, the declaration should only be visible if a module
3248 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003249 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003250 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003251 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003252
Richard Smith8346e522015-06-10 01:47:58 +00003253 // If the declaration declares a template, it may inherit default arguments
3254 // from the previous declaration.
3255 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3256 inheritDefaultTemplateArguments(Reader.getContext(),
3257 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003258}
3259
Richard Smithb321ecb2014-05-13 01:15:00 +00003260template<typename DeclT>
3261void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003262 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003263}
3264void ASTDeclReader::attachLatestDeclImpl(...) {
3265 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3266}
3267
Douglas Gregor05f10352011-12-17 23:38:30 +00003268void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3269 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003270
3271 switch (D->getKind()) {
3272#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003273#define DECL(TYPE, BASE) \
3274 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003275 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3276 break;
3277#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003278 }
3279}
3280
Richard Smith851072e2014-05-19 20:59:20 +00003281template<typename DeclT>
3282void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3283 D->RedeclLink.markIncomplete();
3284}
3285void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3286 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3287}
3288
3289void ASTReader::markIncompleteDeclChain(Decl *D) {
3290 switch (D->getKind()) {
3291#define ABSTRACT_DECL(TYPE)
3292#define DECL(TYPE, BASE) \
3293 case Decl::TYPE: \
3294 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3295 break;
3296#include "clang/AST/DeclNodes.inc"
3297 }
3298}
3299
Sebastian Redlb3298c32010-08-18 23:56:48 +00003300/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003301Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003302 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003303 SourceLocation DeclLoc;
3304 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003305 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003306 // Keep track of where we are in the stream, then jump back there
3307 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003308 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003309
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003310 ReadingKindTracker ReadingKind(Read_Decl, *this);
3311
Douglas Gregor1342e842009-07-06 18:54:52 +00003312 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003313 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003314
Sebastian Redl2c373b92010-10-05 15:59:54 +00003315 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003316 ASTRecordReader Record(*this, *Loc.F);
3317 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003318 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003319
Craig Toppera13603a2014-05-22 05:54:18 +00003320 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003321 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003322 case DECL_CONTEXT_LEXICAL:
3323 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003324 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003325 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003326 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003327 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003328 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003329 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003330 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003331 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003332 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003333 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003334 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003335 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003336 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003337 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003338 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003339 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003340 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003341 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003342 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003343 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003344 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003345 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003346 case DECL_EXPORT:
3347 D = ExportDecl::CreateDeserialized(Context, ID);
3348 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003349 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003350 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003351 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003353 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003354 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003355 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003356 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003357 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003358 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003359 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003360 break;
Richard Smith151c4562016-12-20 21:35:28 +00003361 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003362 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003363 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003364 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003365 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003366 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003367 case DECL_CONSTRUCTOR_USING_SHADOW:
3368 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3369 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003370 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003371 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003372 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003373 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003374 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003375 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003376 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003377 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003378 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003379 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003380 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003381 break;
Richard Smithbc491202017-02-17 20:05:37 +00003382 case DECL_CXX_DEDUCTION_GUIDE:
3383 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3384 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003385 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003386 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003387 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003388 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003389 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3390 break;
3391 case DECL_CXX_INHERITED_CONSTRUCTOR:
3392 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003393 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003394 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003395 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003396 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003397 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003398 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003399 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003400 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003401 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003402 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003403 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003404 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003405 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003406 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003407 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003408 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003409 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003410 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003411 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003412 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003413 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003414 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003415 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003416 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003417 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003418 case DECL_VAR_TEMPLATE:
3419 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3420 break;
3421 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3422 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3423 break;
3424 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3425 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3426 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003427 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003428 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003429 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003430 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003431 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003432 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003433 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003434 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003435 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003436 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003437 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003438 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003439 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003440 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3441 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003442 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003443 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003444 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003445 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003446 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3447 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003448 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003449 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003450 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003451 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003452 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003453 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003454 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003455 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003456 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003457 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003458 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003459 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003460 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003461 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003462 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003463 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003464 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003465 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003466 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003467 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003468 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003469 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003470 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003471 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003472 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003473 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003474 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003475 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003476 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003477 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003478 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003479 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003480 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003481 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003482 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003483 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003484 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003485 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003486 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003487 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003488 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003489 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003490 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003491 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003492 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003493 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003494 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003495 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003496 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003497 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003498 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003499 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003500 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003501 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003502 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003503 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003504 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003505 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003506 break;
3507 case DECL_BINDING:
3508 D = BindingDecl::CreateDeserialized(Context, ID);
3509 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003510 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003511 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003512 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003513 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003514 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003515 break;
John McCall5e77d762013-04-16 07:28:30 +00003516 case DECL_MS_PROPERTY:
3517 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3518 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003519 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003520 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003521 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003522 case DECL_CXX_BASE_SPECIFIERS:
3523 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003524 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003525 case DECL_CXX_CTOR_INITIALIZERS:
3526 Error("attempt to read a C++ ctor initializer record as a declaration");
3527 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003528 case DECL_IMPORT:
3529 // Note: last entry of the ImportDecl record is the number of stored source
3530 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003531 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003532 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003533 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003534 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003535 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003536 case DECL_OMP_DECLARE_REDUCTION:
3537 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3538 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003539 case DECL_OMP_CAPTUREDEXPR:
3540 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003541 break;
Nico Weber66220292016-03-02 17:28:48 +00003542 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003543 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003544 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003545 case DECL_PRAGMA_DETECT_MISMATCH:
3546 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003547 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003548 break;
Michael Han84324352013-02-22 17:15:32 +00003549 case DECL_EMPTY:
3550 D = EmptyDecl::CreateDeserialized(Context, ID);
3551 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003552 case DECL_OBJC_TYPE_PARAM:
3553 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3554 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003555 }
Chris Lattner487412d2009-04-27 05:27:42 +00003556
Sebastian Redlb3298c32010-08-18 23:56:48 +00003557 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003558 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003559 // Set the DeclContext before doing any deserialization, to make sure internal
3560 // calls to Decl::getASTContext() by Decl's methods will find the
3561 // TranslationUnitDecl without crashing.
3562 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003563 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003564
3565 // If this declaration is also a declaration context, get the
3566 // offsets for its tables of lexical and visible declarations.
3567 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3568 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003569 if (Offsets.first &&
3570 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3571 return nullptr;
3572 if (Offsets.second &&
3573 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3574 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003575 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003576 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003577
Douglas Gregordab42432011-08-12 00:15:20 +00003578 // Load any relevant update records.
Richard Smithd1c46742014-04-30 02:24:17 +00003579 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003580
Douglas Gregor404cdde2012-01-27 01:47:08 +00003581 // Load the categories after recursive loading is finished.
3582 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003583 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3584 // we put the Decl in PendingDefinitions so we can pull the categories here.
3585 if (Class->isThisDeclarationADefinition() ||
3586 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003587 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003588
Richard Smith13fb8602016-07-15 21:33:46 +00003589 // If we have deserialized a declaration that has a definition the
3590 // AST consumer might need to know about, queue it.
3591 // We don't pass it to the consumer immediately because we may be in recursive
3592 // loading, and some declarations may still be initializing.
Richard Smithdc1f0422016-07-20 19:10:16 +00003593 if (isConsumerInterestedIn(Context, D, Reader.hasPendingBody()))
Richard Smith13fb8602016-07-15 21:33:46 +00003594 InterestingDecls.push_back(D);
3595
Douglas Gregordab42432011-08-12 00:15:20 +00003596 return D;
3597}
3598
3599void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003600 // The declaration may have been modified by files later in the chain.
3601 // If this is the case, read the record containing the updates from each file
3602 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003603 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003604 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3605 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003606 auto UpdateOffsets = std::move(UpdI->second);
3607 DeclUpdateOffsets.erase(UpdI);
3608
Richard Smithdc1f0422016-07-20 19:10:16 +00003609 bool WasInteresting = isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003610 for (auto &FileAndOffset : UpdateOffsets) {
3611 ModuleFile *F = FileAndOffset.first;
3612 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003613 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3614 SavedStreamPosition SavedPosition(Cursor);
3615 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003616 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003617 ASTRecordReader Record(*this, *F);
3618 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003619 (void)RecCode;
3620 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003621
David L. Jonesbe1557a2016-12-21 00:17:49 +00003622 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3623 SourceLocation());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003624 Reader.UpdateDecl(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003625
3626 // We might have made this declaration interesting. If so, remember that
3627 // we need to hand it off to the consumer.
3628 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003629 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Richard Smith13fb8602016-07-15 21:33:46 +00003630 InterestingDecls.push_back(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003631 WasInteresting = true;
3632 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003633 }
3634 }
Richard Smith1e8e91b2016-04-08 20:53:26 +00003635
3636 // Load the pending visible updates for this decl context, if it has any.
3637 auto I = PendingVisibleUpdates.find(ID);
3638 if (I != PendingVisibleUpdates.end()) {
3639 auto VisibleUpdates = std::move(I->second);
3640 PendingVisibleUpdates.erase(I);
3641
3642 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3643 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3644 Lookups[DC].Table.add(
3645 Update.Mod, Update.Data,
3646 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3647 DC->setHasExternalVisibleStorage(true);
3648 }
Chris Lattner487412d2009-04-27 05:27:42 +00003649}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003650
Richard Smithd61d4ac2015-08-22 20:13:39 +00003651void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3652 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003653 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003654 if (FirstLocal != CanonDecl) {
3655 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3656 ASTDeclReader::attachPreviousDecl(
3657 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3658 CanonDecl);
3659 }
3660
3661 if (!LocalOffset) {
3662 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3663 return;
3664 }
3665
3666 // Load the list of other redeclarations from this module file.
3667 ModuleFile *M = getOwningModuleFile(FirstLocal);
3668 assert(M && "imported decl from no module file");
3669
3670 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3671 SavedStreamPosition SavedPosition(Cursor);
3672 Cursor.JumpToBit(LocalOffset);
3673
3674 RecordData Record;
3675 unsigned Code = Cursor.ReadCode();
3676 unsigned RecCode = Cursor.readRecord(Code, Record);
3677 (void)RecCode;
3678 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3679
3680 // FIXME: We have several different dispatches on decl kind here; maybe
3681 // we should instead generate one loop per kind and dispatch up-front?
3682 Decl *MostRecent = FirstLocal;
3683 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3684 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003685 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3686 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003687 }
Richard Smithc3a53252015-02-28 05:57:02 +00003688 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003689}
3690
3691namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003692 /// \brief Given an ObjC interface, goes through the modules and links to the
3693 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003694 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003695 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003696 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003697 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003698 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003699 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003700 serialization::GlobalDeclID InterfaceID;
3701 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003702
3703 void add(ObjCCategoryDecl *Cat) {
3704 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003705 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003706 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003707
3708 // Check for duplicate categories.
3709 if (Cat->getDeclName()) {
3710 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3711 if (Existing &&
3712 Reader.getOwningModuleFile(Existing)
3713 != Reader.getOwningModuleFile(Cat)) {
3714 // FIXME: We should not warn for duplicates in diamond:
3715 //
3716 // MT //
3717 // / \ //
3718 // ML MR //
3719 // \ / //
3720 // MB //
3721 //
3722 // If there are duplicates in ML/MR, there will be warning when
3723 // creating MB *and* when importing MB. We should not warn when
3724 // importing.
3725 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3726 << Interface->getDeclName() << Cat->getDeclName();
3727 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3728 } else if (!Existing) {
3729 // Record this category.
3730 Existing = Cat;
3731 }
3732 }
3733
3734 // Add this category to the end of the chain.
3735 if (Tail)
3736 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3737 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003738 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003739 Tail = Cat;
3740 }
3741
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003742 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003743 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003744 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003745 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3746 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003747 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003748 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3749 Tail(nullptr), InterfaceID(InterfaceID),
3750 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003751 {
3752 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003753 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003754 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003755 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003756
3757 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003758 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003759 }
3760 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003761
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003762 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003763 // If we've loaded all of the category information we care about from
3764 // this module file, we're done.
3765 if (M.Generation <= PreviousGeneration)
3766 return true;
3767
3768 // Map global ID of the definition down to the local ID used in this
3769 // module file. If there is no such mapping, we'll find nothing here
3770 // (or in any module it imports).
3771 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3772 if (!LocalID)
3773 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003774
Douglas Gregor404cdde2012-01-27 01:47:08 +00003775 // Perform a binary search to find the local redeclarations for this
3776 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003777 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003778 const ObjCCategoriesInfo *Result
3779 = std::lower_bound(M.ObjCCategoriesMap,
3780 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003781 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003782 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3783 Result->DefinitionID != LocalID) {
3784 // We didn't find anything. If the class definition is in this module
3785 // file, then the module files it depends on cannot have any categories,
3786 // so suppress further lookup.
3787 return Reader.isDeclIDFromModule(InterfaceID, M);
3788 }
3789
3790 // We found something. Dig out all of the categories.
3791 unsigned Offset = Result->Offset;
3792 unsigned N = M.ObjCCategories[Offset];
3793 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3794 for (unsigned I = 0; I != N; ++I)
3795 add(cast_or_null<ObjCCategoryDecl>(
3796 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3797 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003798 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003799 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003800} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003801
Douglas Gregor404cdde2012-01-27 01:47:08 +00003802void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3803 ObjCInterfaceDecl *D,
3804 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003805 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003806 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003807 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003808}
Douglas Gregordab42432011-08-12 00:15:20 +00003809
Richard Smithd6db68c2014-08-07 20:58:41 +00003810template<typename DeclT, typename Fn>
3811static void forAllLaterRedecls(DeclT *D, Fn F) {
3812 F(D);
3813
3814 // Check whether we've already merged D into its redeclaration chain.
3815 // MostRecent may or may not be nullptr if D has not been merged. If
3816 // not, walk the merged redecl chain and see if it's there.
3817 auto *MostRecent = D->getMostRecentDecl();
3818 bool Found = false;
3819 for (auto *Redecl = MostRecent; Redecl && !Found;
3820 Redecl = Redecl->getPreviousDecl())
3821 Found = (Redecl == D);
3822
3823 // If this declaration is merged, apply the functor to all later decls.
3824 if (Found) {
3825 for (auto *Redecl = MostRecent; Redecl != D;
3826 Redecl = Redecl->getPreviousDecl())
3827 F(Redecl);
3828 }
3829}
3830
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003831void ASTDeclReader::UpdateDecl(Decl *D) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003832 while (Record.getIdx() < Record.size()) {
3833 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003834 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003835 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003836 // FIXME: If we also have an update record for instantiating the
3837 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003838 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003839 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003840 // FIXME: We should call addHiddenDecl instead, to add the member
3841 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003842 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003843 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003844 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003845
3846 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3847 // It will be added to the template's specializations set when loaded.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003848 (void)Record.readDecl();
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003849 break;
3850
3851 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003852 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3853
Douglas Gregor540fd812012-01-09 18:07:24 +00003854 // Each module has its own anonymous namespace, which is disjoint from
3855 // any other module's anonymous namespaces, so don't attach the anonymous
3856 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003857 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003858 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3859 TU->setAnonymousNamespace(Anon);
3860 else
3861 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3862 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003863 break;
3864 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003865
3866 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3867 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003868 ReadSourceLocation());
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003869 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00003870
John McCall32791cc2016-01-06 22:34:54 +00003871 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3872 auto Param = cast<ParmVarDecl>(D);
3873
3874 // We have to read the default argument regardless of whether we use it
3875 // so that hypothetical further update records aren't messed up.
3876 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003877 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003878
3879 // Only apply the update if the parameter still has an uninstantiated
3880 // default argument.
3881 if (Param->hasUninstantiatedDefaultArg())
3882 Param->setDefaultArg(DefaultArg);
3883 break;
3884 }
3885
Richard Smith4b054b22016-08-24 21:25:37 +00003886 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3887 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003888 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003889
3890 // Only apply the update if the field still has an uninstantiated
3891 // default member initializer.
3892 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3893 if (DefaultInit)
3894 FD->setInClassInitializer(DefaultInit);
3895 else
3896 // Instantiation failed. We can get here if we serialized an AST for
3897 // an invalid program.
3898 FD->removeInClassInitializer();
3899 }
3900 break;
3901 }
3902
Richard Smith4d235792014-08-07 18:53:08 +00003903 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003904 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003905 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003906 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003907 // It's safe to stop now because this update record is always last.
3908 return;
3909 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003910
David L. Jonesbe1557a2016-12-21 00:17:49 +00003911 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00003912 // Maintain AST consistency: any later redeclarations of this function
3913 // are inline if this one is. (We might have merged another declaration
3914 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00003915 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3916 FD->setImplicitlyInline();
3917 });
Richard Smith195d8ef2014-05-29 03:15:31 +00003918 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003919 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00003920 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003921 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00003922 break;
3923 }
3924
Richard Smithcd45dbc2014-04-19 03:48:30 +00003925 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
3926 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00003927 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00003928 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00003929 OldDD && (OldDD->Definition != RD ||
3930 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00003931 ReadCXXRecordDefinition(RD, /*Update*/true);
3932
Richard Smithcd45dbc2014-04-19 03:48:30 +00003933 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003934 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00003935 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00003936 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00003937 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003938 }
3939
David L. Jonesbe1557a2016-12-21 00:17:49 +00003940 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003941 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003942 if (MemberSpecializationInfo *MSInfo =
3943 RD->getMemberSpecializationInfo()) {
3944 MSInfo->setTemplateSpecializationKind(TSK);
3945 MSInfo->setPointOfInstantiation(POI);
3946 } else {
3947 ClassTemplateSpecializationDecl *Spec =
3948 cast<ClassTemplateSpecializationDecl>(RD);
3949 Spec->setTemplateSpecializationKind(TSK);
3950 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00003951
David L. Jonesbe1557a2016-12-21 00:17:49 +00003952 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00003953 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003954 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00003955 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003956 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00003957 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00003958 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00003959
3960 // FIXME: If we already have a partial specialization set,
3961 // check that it matches.
3962 if (!Spec->getSpecializedTemplateOrPartial()
3963 .is<ClassTemplatePartialSpecializationDecl *>())
3964 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00003965 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003966 }
3967
David L. Jonesbe1557a2016-12-21 00:17:49 +00003968 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003969 RD->setLocation(ReadSourceLocation());
3970 RD->setLocStart(ReadSourceLocation());
3971 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00003972
David L. Jonesbe1557a2016-12-21 00:17:49 +00003973 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003974 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003975 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00003976 // If the declaration already has attributes, we assume that some other
3977 // AST file already loaded them.
3978 if (!D->hasAttrs())
3979 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00003980 }
3981 break;
3982 }
3983
Richard Smithf8134002015-03-10 01:41:22 +00003984 case UPD_CXX_RESOLVED_DTOR_DELETE: {
3985 // Set the 'operator delete' directly to avoid emitting another update
3986 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003987 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00003988 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
3989 // FIXME: Check consistency if we have an old and new operator delete.
3990 if (!First->OperatorDelete)
3991 First->OperatorDelete = Del;
3992 break;
3993 }
3994
Richard Smith564417a2014-03-20 21:47:22 +00003995 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00003996 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00003997 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003998 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00003999
4000 // Update this declaration's exception specification, if needed.
4001 auto *FD = cast<FunctionDecl>(D);
4002 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4003 // FIXME: If the exception specification is already present, check that it
4004 // matches.
4005 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00004006 FD->setType(Reader.Context.getFunctionType(
4007 FPT->getReturnType(), FPT->getParamTypes(),
4008 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004009
4010 // When we get to the end of deserializing, see if there are other decls
4011 // that we need to propagate this exception specification onto.
4012 Reader.PendingExceptionSpecUpdates.insert(
4013 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004014 }
Richard Smith564417a2014-03-20 21:47:22 +00004015 break;
4016 }
4017
Richard Smith1fa5d642013-05-11 05:45:24 +00004018 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004019 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004020 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004021 for (auto *Redecl : merged_redecls(D)) {
4022 // FIXME: If the return type is already deduced, check that it matches.
4023 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
4024 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
4025 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004026 break;
4027 }
Eli Friedman276dd182013-09-05 00:02:25 +00004028
4029 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004030 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004031 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00004032 break;
4033 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004034
4035 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004036 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004037 break;
4038
4039 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004040 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004041 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004042
Alexey Bataev97720002014-11-11 04:05:39 +00004043 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4044 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004045 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004046 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004047
Alex Denisovfde64952015-06-26 05:28:36 +00004048 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004049 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004050 auto *Exported = cast<NamedDecl>(D);
4051 if (auto *TD = dyn_cast<TagDecl>(Exported))
4052 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004053 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004054 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004055 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4056 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004057 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4058 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004059 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004060 // If Owner is made visible at some later point, make this declaration
4061 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004062 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004063 } else {
4064 // The declaration is now visible.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004065 Exported->Hidden = false;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004066 }
4067 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004068 }
Alex Denisovfde64952015-06-26 05:28:36 +00004069
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004070 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004071 case UPD_ADDED_ATTR_TO_RECORD:
4072 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004073 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004074 assert(Attrs.size() == 1);
4075 D->addAttr(Attrs[0]);
4076 break;
4077 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004078 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004079}