blob: 4a7e24c007a46bf35650bbaf0919e67c539d46f4 [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
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000243 void Visit(Decl *D);
244
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000245 void UpdateDecl(Decl *D);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000246
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000247 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
248 ObjCCategoryDecl *Next) {
249 Cat->NextClassCategory = Next;
250 }
251
Chris Lattner487412d2009-04-27 05:27:42 +0000252 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000253 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000254 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000255 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
256 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000257 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000258 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000259 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
260 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000261 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000262 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000263 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000264 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000265 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000266 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000267 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000268 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
269 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
270 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
271 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
272 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000273 ClassTemplateSpecializationDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000274 void VisitClassTemplateSpecializationDecl(
275 ClassTemplateSpecializationDecl *D) {
276 VisitClassTemplateSpecializationDeclImpl(D);
277 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000278 void VisitClassTemplatePartialSpecializationDecl(
279 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000280 void VisitClassScopeFunctionSpecializationDecl(
281 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000282 RedeclarableResult
283 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
284 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
285 VisitVarTemplateSpecializationDeclImpl(D);
286 }
287 void VisitVarTemplatePartialSpecializationDecl(
288 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000289 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000290 void VisitValueDecl(ValueDecl *VD);
291 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000292 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000293 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000294 void VisitFunctionDecl(FunctionDecl *FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000295 void VisitCXXMethodDecl(CXXMethodDecl *D);
296 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
297 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
298 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000299 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000300 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000301 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000302 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
303 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000304 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
305 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000306 void VisitDecompositionDecl(DecompositionDecl *DD);
307 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000308 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000309 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000310 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000311 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000312 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000313 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000314 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000315 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000316 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000317 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000318 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000319 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000320 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000321 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000322 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000323 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000324 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000325 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000326 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000327 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
328 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000329 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000330 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000331 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000332
Chris Lattner487412d2009-04-27 05:27:42 +0000333 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000334
335 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000336 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000337
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000338 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000339 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
340 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000341
342 template<typename T>
343 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000344 RedeclarableResult &Redecl,
345 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000346
Richard Smith0b87e072013-10-07 08:02:11 +0000347 template<typename T>
348 void mergeMergeable(Mergeable<T> *D);
349
Richard Smithf17fdbd2014-04-24 02:25:27 +0000350 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
351 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000352 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000353
Douglas Gregor85f3f952015-07-07 03:57:15 +0000354 ObjCTypeParamList *ReadObjCTypeParamList();
355
Alexis Hunted053252010-05-30 07:21:58 +0000356 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000357 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000358 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000359 void VisitObjCContainerDecl(ObjCContainerDecl *D);
360 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
361 void VisitObjCIvarDecl(ObjCIvarDecl *D);
362 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
363 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000364 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
365 void VisitObjCImplDecl(ObjCImplDecl *D);
366 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
367 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
368 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
369 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
370 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000371 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000372 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000373 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000374 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000375} // end namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000376
Richard Smith86dfc1e2015-06-18 22:07:00 +0000377namespace {
378/// Iterator over the redeclarations of a declaration that have already
379/// been merged into the same redeclaration chain.
380template<typename DeclT>
381class MergedRedeclIterator {
382 DeclT *Start, *Canonical, *Current;
383public:
384 MergedRedeclIterator() : Current(nullptr) {}
385 MergedRedeclIterator(DeclT *Start)
386 : Start(Start), Canonical(nullptr), Current(Start) {}
387
388 DeclT *operator*() { return Current; }
389
390 MergedRedeclIterator &operator++() {
391 if (Current->isFirstDecl()) {
392 Canonical = Current;
393 Current = Current->getMostRecentDecl();
394 } else
395 Current = Current->getPreviousDecl();
396
397 // If we started in the merged portion, we'll reach our start position
398 // eventually. Otherwise, we'll never reach it, but the second declaration
399 // we reached was the canonical declaration, so stop when we see that one
400 // again.
401 if (Current == Start || Current == Canonical)
402 Current = nullptr;
403 return *this;
404 }
405
406 friend bool operator!=(const MergedRedeclIterator &A,
407 const MergedRedeclIterator &B) {
408 return A.Current != B.Current;
409 }
410};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000411} // end anonymous namespace
412
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000413template <typename DeclT>
414static llvm::iterator_range<MergedRedeclIterator<DeclT>>
415merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000416 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
417 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000418}
419
Sebastian Redlb3298c32010-08-18 23:56:48 +0000420uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000421 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000422}
423
Sebastian Redlb3298c32010-08-18 23:56:48 +0000424void ASTDeclReader::Visit(Decl *D) {
425 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000426
Vassil Vassilev928c8252016-04-28 14:13:28 +0000427 // At this point we have deserialized and merged the decl and it is safe to
428 // update its canonical decl to signal that the entire entity is used.
429 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
430 IsDeclMarkedUsed = false;
431
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000432 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
433 if (DD->DeclInfo) {
434 DeclaratorDecl::ExtInfo *Info =
435 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000436 Info->TInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000437 }
438 else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000439 DD->DeclInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000440 }
441 }
442
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000443 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000444 // We have a fully initialized TypeDecl. Read its type now.
Douglas Gregor0cdc8322010-12-10 17:03:06 +0000445 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000446
447 // If this is a tag declaration with a typedef name for linkage, it's safe
448 // to load that typedef now.
449 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000450 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
451 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000452 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
453 // if we have a fully initialized TypeDecl, we can safely read its type now.
454 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000455 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
456 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000457 // We only read it if FD doesn't already have a body (e.g., from another
458 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000459 // FIXME: Can we diagnose ODR violations somehow?
David L. Jonesbe1557a2016-12-21 00:17:49 +0000460 if (Record.readInt()) {
Richard Smithc2bb8182015-03-24 06:36:48 +0000461 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000462 CD->NumCtorInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +0000463 if (CD->NumCtorInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000464 CD->CtorInitializers = ReadGlobalOffset();
Richard Smithc2bb8182015-03-24 06:36:48 +0000465 }
Douglas Gregora6017bb2012-10-09 17:21:28 +0000466 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
467 HasPendingBody = true;
468 }
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000469 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000470}
471
Sebastian Redlb3298c32010-08-18 23:56:48 +0000472void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000473 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
474 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000475 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000476 // parameter or of a parameter of a function template immediately. These
477 // entities might be used in the formulation of its DeclContext (for
478 // example, a function parameter can be used in decltype() in trailing
479 // return type of the function). Use the translation unit DeclContext as a
480 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000481 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
482 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000483 if (!LexicalDCIDForTemplateParmDecl)
484 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000485 Reader.addPendingDeclContextInfo(D,
486 SemaDCIDForTemplateParmDecl,
487 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000488 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000489 } else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000490 DeclContext *SemaDC = ReadDeclAs<DeclContext>();
491 DeclContext *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000492 if (!LexicalDC)
493 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000494 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000495 // Avoid calling setLexicalDeclContext() directly because it uses
496 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000497 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
498 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000499 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000500 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000501 D->setInvalidDecl(Record.readInt());
502 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000503 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000504 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000505 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
506 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000507 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000508 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000509 D->setImplicit(Record.readInt());
510 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000511 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000512 D->setReferenced(Record.readInt());
513 D->setTopLevelDeclInObjCContainer(Record.readInt());
514 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000515 D->FromASTFile = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000516 D->setModulePrivate(Record.readInt());
Douglas Gregor781f7132012-01-06 16:59:53 +0000517 D->Hidden = D->isModulePrivate();
Richard Smith42413142015-05-15 20:05:43 +0000518
Douglas Gregorcf68c582011-12-01 22:20:10 +0000519 // Determine whether this declaration is part of a (sub)module. If so, it
520 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000521 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000522 // Store the owning submodule ID in the declaration.
523 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000524
525 if (D->Hidden) {
526 // Module-private declarations are never visible, so there is no work to do.
527 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
528 // If local visibility is being tracked, this declaration will become
529 // hidden and visible as the owning module does. Inform Sema that this
530 // declaration might not be visible.
531 D->Hidden = true;
532 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
533 if (Owner->NameVisibility != Module::AllVisible) {
534 // The owning module is not visible. Mark this declaration as hidden.
535 D->Hidden = true;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000536
Richard Smith42413142015-05-15 20:05:43 +0000537 // Note that this declaration was hidden because its owning module is
538 // not yet visible.
539 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000540 }
541 }
542 }
Chris Lattner487412d2009-04-27 05:27:42 +0000543}
544
Nico Weber66220292016-03-02 17:28:48 +0000545void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
546 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000547 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000548 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000549 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000550 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
551 D->getTrailingObjects<char>()[Arg.size()] = '\0';
552}
553
Nico Webercbbaeb12016-03-02 19:28:54 +0000554void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
555 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000556 D->setLocation(ReadSourceLocation());
557 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000558 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
559 D->getTrailingObjects<char>()[Name.size()] = '\0';
560
561 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000562 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000563 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
564 Value.size());
565 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
566}
567
Sebastian Redlb3298c32010-08-18 23:56:48 +0000568void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000569 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000570}
571
Sebastian Redlb3298c32010-08-18 23:56:48 +0000572void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000573 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000574 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000575 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000576}
577
Sebastian Redlb3298c32010-08-18 23:56:48 +0000578void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000579 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000580 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000581 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000582 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000583}
584
Richard Smith43ccec8e2014-08-26 03:52:16 +0000585ASTDeclReader::RedeclarableResult
586ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000587 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000588 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000589 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000590 if (Record.readInt()) { // isModed
591 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000592 TD->setModedTypeSourceInfo(TInfo, modedT);
593 } else
594 TD->setTypeSourceInfo(TInfo);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000595 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000596}
597
598void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000599 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
600 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000601}
602
Richard Smithdda56e42011-04-15 14:24:37 +0000603void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000604 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000605 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000606 // Merged when we merge the template.
607 TD->setDescribedAliasTemplate(Template);
608 else
609 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000610}
611
Richard Smith1d209d02013-05-23 01:49:11 +0000612ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000613 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000614 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000615
David L. Jonesbe1557a2016-12-21 00:17:49 +0000616 TD->IdentifierNamespace = Record.readInt();
617 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000618 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000619 TD->setCompleteDefinition(Record.readInt());
620 TD->setEmbeddedInDeclarator(Record.readInt());
621 TD->setFreeStanding(Record.readInt());
622 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000623 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000624
David L. Jonesbe1557a2016-12-21 00:17:49 +0000625 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000626 case 0:
627 break;
628 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000629 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000630 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000631 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000632 break;
633 }
634 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000635 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000636 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000637 break;
Richard Smith70d58502014-08-30 00:04:23 +0000638 default:
639 llvm_unreachable("unexpected tag info kind");
640 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000641
Richard Smithcd45dbc2014-04-19 03:48:30 +0000642 if (!isa<CXXRecordDecl>(TD))
643 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000644 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000645}
646
Sebastian Redlb3298c32010-08-18 23:56:48 +0000647void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000648 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000649 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000650 ED->setIntegerTypeSourceInfo(TI);
651 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000652 ED->setIntegerType(Record.readType());
653 ED->setPromotionType(Record.readType());
654 ED->setNumPositiveBits(Record.readInt());
655 ED->setNumNegativeBits(Record.readInt());
656 ED->IsScoped = Record.readInt();
657 ED->IsScopedUsingClassTag = Record.readInt();
658 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000659
Richard Smith01a73372013-10-15 22:02:41 +0000660 // If this is a definition subject to the ODR, and we already have a
661 // definition, merge this one into it.
662 if (ED->IsCompleteDefinition &&
663 Reader.getContext().getLangOpts().Modules &&
664 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000665 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
666 if (!OldDef) {
667 // This is the first time we've seen an imported definition. Look for a
668 // local definition before deciding that we are the first definition.
669 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
670 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
671 OldDef = D;
672 break;
673 }
674 }
675 }
676 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000677 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
678 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000679 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000680 } else {
681 OldDef = ED;
682 }
683 }
684
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000685 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000686 TemplateSpecializationKind TSK =
687 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000688 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000689 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
690 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
691 }
Chris Lattner487412d2009-04-27 05:27:42 +0000692}
693
Richard Smith1d209d02013-05-23 01:49:11 +0000694ASTDeclReader::RedeclarableResult
695ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
696 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000697 RD->setHasFlexibleArrayMember(Record.readInt());
698 RD->setAnonymousStructOrUnion(Record.readInt());
699 RD->setHasObjectMember(Record.readInt());
700 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000701 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000702}
703
Sebastian Redlb3298c32010-08-18 23:56:48 +0000704void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000705 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000706 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000707}
708
Sebastian Redlb3298c32010-08-18 23:56:48 +0000709void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000710 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000711 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000712 ECD->setInitExpr(Record.readExpr());
713 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000714 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000715}
716
Sebastian Redlb3298c32010-08-18 23:56:48 +0000717void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000718 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000719 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000720 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000721 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000722 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000723 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000724 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000725 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000726}
727
Sebastian Redlb3298c32010-08-18 23:56:48 +0000728void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000729 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000730 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000731
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000732 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000733 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000734
Douglas Gregorb2585692012-01-04 17:13:46 +0000735 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
736 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000737
David L. Jonesbe1557a2016-12-21 00:17:49 +0000738 FD->SClass = (StorageClass)Record.readInt();
739 FD->IsInline = Record.readInt();
740 FD->IsInlineSpecified = Record.readInt();
741 FD->IsVirtualAsWritten = Record.readInt();
742 FD->IsPure = Record.readInt();
743 FD->HasInheritedPrototype = Record.readInt();
744 FD->HasWrittenPrototype = Record.readInt();
745 FD->IsDeleted = Record.readInt();
746 FD->IsTrivial = Record.readInt();
747 FD->IsDefaulted = Record.readInt();
748 FD->IsExplicitlyDefaulted = Record.readInt();
749 FD->HasImplicitReturnZero = Record.readInt();
750 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000751 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000752 FD->HasSkippedBody = Record.readInt();
753 FD->IsLateTemplateParsed = Record.readInt();
754 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000755 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000756
David L. Jonesbe1557a2016-12-21 00:17:49 +0000757 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000758 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000759 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000760 break;
761 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000762 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000763 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000764 break;
765 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000766 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000767 TemplateSpecializationKind TSK =
768 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000769 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000770 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000771 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000772 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000773 break;
774 }
775 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000776 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000777 TemplateSpecializationKind TSK =
778 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000779
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000780 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000781 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000782 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000783
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000784 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000785 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000786 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000787 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000788 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000789 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000790 TemplArgLocs.reserve(NumTemplateArgLocs);
791 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000792 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000793
794 LAngleLoc = ReadSourceLocation();
795 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000796 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000797
798 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000799
Douglas Gregor4163aca2011-09-09 21:34:22 +0000800 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000801 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000802 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000803 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000804 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000805 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000806 FunctionTemplateSpecializationInfo *FTInfo
807 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
808 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000809 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
810 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000811 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000812 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000813
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000814 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000815 // The template that contains the specializations set. It's not safe to
816 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000817 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000818 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
819 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
820 // FunctionTemplateSpecializationInfo's Profile().
821 // We avoid getASTContext because a decl in the parent hierarchy may
822 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000823 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000824 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000825 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000826 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000827 FunctionTemplateSpecializationInfo *ExistingInfo =
828 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000829 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000830 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
831 else {
832 assert(Reader.getContext().getLangOpts().Modules &&
833 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000834 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000835 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000836 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000837 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000838 }
839 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
840 // Templates.
841 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000842 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000843 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000844 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
845
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000846 // Templates args.
847 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000848 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000849 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000850 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000851 TemplArgs.setLAngleLoc(ReadSourceLocation());
852 TemplArgs.setRAngleLoc(ReadSourceLocation());
853
Douglas Gregor4163aca2011-09-09 21:34:22 +0000854 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000855 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000856 // These are not merged; we don't need to merge redeclarations of dependent
857 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000858 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000859 }
860 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000861
Chris Lattnerca025db2010-05-07 21:43:38 +0000862 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000863 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000864 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000865 Params.reserve(NumParams);
866 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000867 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000868 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000869}
870
Sebastian Redlb3298c32010-08-18 23:56:48 +0000871void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000872 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000873 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000874 // Load the body on-demand. Most clients won't care, because method
875 // definitions rarely show up in headers.
876 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
877 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000878 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
879 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000880 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000881 MD->setInstanceMethod(Record.readInt());
882 MD->setVariadic(Record.readInt());
883 MD->setPropertyAccessor(Record.readInt());
884 MD->setDefined(Record.readInt());
885 MD->IsOverriding = Record.readInt();
886 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000887
David L. Jonesbe1557a2016-12-21 00:17:49 +0000888 MD->IsRedeclaration = Record.readInt();
889 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000890 if (MD->HasRedeclaration)
891 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000892 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000893
David L. Jonesbe1557a2016-12-21 00:17:49 +0000894 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
895 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
896 MD->SetRelatedResultType(Record.readInt());
897 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000898 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
899 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000900 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000901 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000902 Params.reserve(NumParams);
903 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000904 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000905
David L. Jonesbe1557a2016-12-21 00:17:49 +0000906 MD->SelLocsKind = Record.readInt();
907 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000908 SmallVector<SourceLocation, 16> SelLocs;
909 SelLocs.reserve(NumStoredSelLocs);
910 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000911 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000912
913 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000914}
915
Douglas Gregor85f3f952015-07-07 03:57:15 +0000916void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000917 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000918
David L. Jonesbe1557a2016-12-21 00:17:49 +0000919 D->Variance = Record.readInt();
920 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000921 D->VarianceLoc = ReadSourceLocation();
922 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000923}
924
Sebastian Redlb3298c32010-08-18 23:56:48 +0000925void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000926 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000927 CD->setAtStartLoc(ReadSourceLocation());
928 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000929}
930
Douglas Gregor85f3f952015-07-07 03:57:15 +0000931ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000932 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000933 if (numParams == 0)
934 return nullptr;
935
936 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
937 typeParams.reserve(numParams);
938 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000939 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000940 if (!typeParam)
941 return nullptr;
942
943 typeParams.push_back(typeParam);
944 }
945
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000946 SourceLocation lAngleLoc = ReadSourceLocation();
947 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000948
949 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
950 typeParams, rAngleLoc);
951}
952
Manman Renec315f12016-09-09 23:48:27 +0000953void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000954 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000955 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000956 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000957
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000958 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000959 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000960
Manman Renec315f12016-09-09 23:48:27 +0000961 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000962 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000963 SmallVector<ObjCProtocolDecl *, 16> Protocols;
964 Protocols.reserve(NumProtocols);
965 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000966 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000967 SmallVector<SourceLocation, 16> ProtoLocs;
968 ProtoLocs.reserve(NumProtocols);
969 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000970 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +0000971 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
972 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000973
Manman Renec315f12016-09-09 23:48:27 +0000974 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000975 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000976 Protocols.clear();
977 Protocols.reserve(NumProtocols);
978 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000979 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000980 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
981 Reader.getContext());
982}
983
984void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
985 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
986 // FIXME: odr checking?
987}
988
Sebastian Redlb3298c32010-08-18 23:56:48 +0000989void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000990 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +0000991 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000992 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000993 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000994
995 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000996 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000997 // Read the definition.
998 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000999
David L. Jonesbe1557a2016-12-21 00:17:49 +00001000 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001001 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1002 if (Canon->Data.getPointer()) {
1003 // If we already have a definition, keep the definition invariant and
1004 // merge the data.
1005 MergeDefinitionData(Canon, std::move(ID->data()));
1006 ID->Data = Canon->Data;
1007 } else {
1008 // Set the definition data of the canonical declaration, so other
1009 // redeclarations will see it.
1010 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001011
Manman Renec315f12016-09-09 23:48:27 +00001012 // We will rebuild this list lazily.
1013 ID->setIvarList(nullptr);
1014 }
Craig Toppera13603a2014-05-22 05:54:18 +00001015
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001016 // Note that we have deserialized a definition.
1017 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001018
Douglas Gregor404cdde2012-01-27 01:47:08 +00001019 // Note that we've loaded this Objective-C class.
1020 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001021 } else {
1022 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001023 }
Chris Lattner487412d2009-04-27 05:27:42 +00001024}
1025
Sebastian Redlb3298c32010-08-18 23:56:48 +00001026void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001027 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001028 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001029 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001030 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001031 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001032 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001033}
1034
Sebastian Redlb3298c32010-08-18 23:56:48 +00001035void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
Douglas Gregorda389302012-01-01 21:47:52 +00001036 RedeclarableResult Redecl = VisitRedeclarable(PD);
Chris Lattner487412d2009-04-27 05:27:42 +00001037 VisitObjCContainerDecl(PD);
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001038 mergeRedeclarable(PD, Redecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001039
David L. Jonesbe1557a2016-12-21 00:17:49 +00001040 if (Record.readInt()) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00001041 // Read the definition.
1042 PD->allocateDefinitionData();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001043
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001044 // Set the definition data of the canonical declaration, so other
1045 // redeclarations will see it.
1046 PD->getCanonicalDecl()->Data = PD->Data;
1047
David L. Jonesbe1557a2016-12-21 00:17:49 +00001048 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001049 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1050 ProtoRefs.reserve(NumProtoRefs);
1051 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001052 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001053 SmallVector<SourceLocation, 16> ProtoLocs;
1054 ProtoLocs.reserve(NumProtoRefs);
1055 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001056 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001057 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1058 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001059
Douglas Gregora715bff2012-01-01 19:51:50 +00001060 // Note that we have deserialized a definition.
1061 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001062 } else {
1063 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001064 }
Chris Lattner487412d2009-04-27 05:27:42 +00001065}
1066
Sebastian Redlb3298c32010-08-18 23:56:48 +00001067void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001068 VisitFieldDecl(FD);
1069}
1070
Sebastian Redlb3298c32010-08-18 23:56:48 +00001071void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001072 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001073 CD->setCategoryNameLoc(ReadSourceLocation());
1074 CD->setIvarLBraceLoc(ReadSourceLocation());
1075 CD->setIvarRBraceLoc(ReadSourceLocation());
1076
Douglas Gregor404cdde2012-01-27 01:47:08 +00001077 // Note that this category has been deserialized. We do this before
1078 // deserializing the interface declaration, so that it will consider this
1079 /// category.
1080 Reader.CategoriesDeserialized.insert(CD);
1081
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001082 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001083 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001084 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001085 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001086 ProtoRefs.reserve(NumProtoRefs);
1087 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001088 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001089 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001090 ProtoLocs.reserve(NumProtoRefs);
1091 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001092 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001093 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001094 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001095}
1096
Sebastian Redlb3298c32010-08-18 23:56:48 +00001097void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001098 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001099 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001100}
1101
Sebastian Redlb3298c32010-08-18 23:56:48 +00001102void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001103 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 D->setAtLoc(ReadSourceLocation());
1105 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001106 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001107 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001108 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001109 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001110 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001111 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001112 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001113 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001114 (ObjCPropertyDecl::PropertyControl)Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001115 D->setGetterName(Record.readDeclarationName().getObjCSelector());
1116 D->setSetterName(Record.readDeclarationName().getObjCSelector());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001117 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1118 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1119 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001120}
1121
Sebastian Redlb3298c32010-08-18 23:56:48 +00001122void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001123 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001124 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001125}
1126
Sebastian Redlb3298c32010-08-18 23:56:48 +00001127void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001128 VisitObjCImplDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001129 D->setIdentifier(Record.getIdentifierInfo());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001130 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001131}
1132
Sebastian Redlb3298c32010-08-18 23:56:48 +00001133void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001134 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001135 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1136 D->SuperLoc = ReadSourceLocation();
1137 D->setIvarLBraceLoc(ReadSourceLocation());
1138 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001139 D->setHasNonZeroConstructors(Record.readInt());
1140 D->setHasDestructors(Record.readInt());
1141 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001142 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001143 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001144}
1145
Sebastian Redlb3298c32010-08-18 23:56:48 +00001146void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001147 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001148 D->setAtLoc(ReadSourceLocation());
1149 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1150 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1151 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001152 D->setGetterCXXConstructor(Record.readExpr());
1153 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001154}
1155
Sebastian Redlb3298c32010-08-18 23:56:48 +00001156void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001157 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001158 FD->Mutable = Record.readInt();
1159 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001160 FD->InitStorage.setInt(
1161 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1162 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001163 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001164 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001165 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001166 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001167 }
Richard Smith2b013182012-06-10 03:12:00 +00001168 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001169 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001170 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001171 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001172 }
Richard Smith0b87e072013-10-07 08:02:11 +00001173 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001174}
1175
John McCall5e77d762013-04-16 07:28:30 +00001176void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1177 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001178 PD->GetterId = Record.getIdentifierInfo();
1179 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001180}
1181
Francois Pichet783dd6e2010-11-21 06:08:52 +00001182void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1183 VisitValueDecl(FD);
1184
David L. Jonesbe1557a2016-12-21 00:17:49 +00001185 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001186 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001187 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001188
1189 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001190 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001191
1192 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001193}
1194
Larisse Voufo39a1e502013-08-06 01:03:05 +00001195ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001196 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001197 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001198
David L. Jonesbe1557a2016-12-21 00:17:49 +00001199 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1200 VD->VarDeclBits.TSCSpec = Record.readInt();
1201 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001202 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001203 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1204 Record.readInt();
1205 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1206 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1207 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1208 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1209 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1210 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1211 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1212 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1213 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001214 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001215 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001216 VD->setCachedLinkage(VarLinkage);
1217
1218 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001219 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001220 VD->getLexicalDeclContext()->isFunctionOrMethod())
1221 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001222
David L. Jonesbe1557a2016-12-21 00:17:49 +00001223 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001224 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001225 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001226 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1227 Eval->CheckedICE = true;
1228 Eval->IsICE = Val == 3;
1229 }
1230 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001231
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001232 enum VarKind {
1233 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1234 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001235 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001236 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001237 // Only true variables (not parameters or implicit parameters) can be
1238 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001239 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1240 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001241 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001242 break;
1243 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001244 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001245 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001246 break;
1247 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001248 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001249 TemplateSpecializationKind TSK =
1250 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001251 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001252 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001253 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001254 break;
1255 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001256 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001257
1258 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001259}
1260
Sebastian Redlb3298c32010-08-18 23:56:48 +00001261void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001262 VisitVarDecl(PD);
1263}
1264
Sebastian Redlb3298c32010-08-18 23:56:48 +00001265void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001266 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001267 unsigned isObjCMethodParam = Record.readInt();
1268 unsigned scopeDepth = Record.readInt();
1269 unsigned scopeIndex = Record.readInt();
1270 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001271 if (isObjCMethodParam) {
1272 assert(scopeDepth == 0);
1273 PD->setObjCMethodScopeInfo(scopeIndex);
1274 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1275 } else {
1276 PD->setScopeInfo(scopeDepth, scopeIndex);
1277 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001278 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1279 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1280 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001281 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001282
1283 // FIXME: If this is a redeclaration of a function from another module, handle
1284 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001285}
1286
Richard Smith7b76d812016-08-12 02:21:25 +00001287void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1288 VisitVarDecl(DD);
1289 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1290 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001291 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001292}
1293
1294void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1295 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001296 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001297}
1298
Sebastian Redlb3298c32010-08-18 23:56:48 +00001299void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001300 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001301 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001302 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001303}
1304
Sebastian Redlb3298c32010-08-18 23:56:48 +00001305void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001306 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001307 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001308 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001309 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001310 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001311 Params.reserve(NumParams);
1312 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001313 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001314 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001315
David L. Jonesbe1557a2016-12-21 00:17:49 +00001316 BD->setIsVariadic(Record.readInt());
1317 BD->setBlockMissingReturnType(Record.readInt());
1318 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001319
David L. Jonesbe1557a2016-12-21 00:17:49 +00001320 bool capturesCXXThis = Record.readInt();
1321 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001322 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001323 captures.reserve(numCaptures);
1324 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001325 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001326 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001327 bool byRef = (flags & 1);
1328 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001329 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001330
1331 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1332 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001333 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001334}
1335
Ben Langmuirce914fc2013-05-03 19:20:19 +00001336void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1337 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001338 unsigned ContextParamPos = Record.readInt();
1339 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001340 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001341 for (unsigned I = 0; I < CD->NumParams; ++I) {
1342 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001343 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001344 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001345 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001346 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001347}
1348
Sebastian Redlb3298c32010-08-18 23:56:48 +00001349void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001350 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001351 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001352 D->setExternLoc(ReadSourceLocation());
1353 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001354}
1355
Richard Smith8df390f2016-09-08 23:14:54 +00001356void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1357 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001358 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001359}
1360
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001361void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1362 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001363 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001364}
1365
Sebastian Redlb3298c32010-08-18 23:56:48 +00001366void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001367 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001368 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001369 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001370 D->LocStart = ReadSourceLocation();
1371 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001372
Richard Smith15e32fd2015-03-17 02:23:11 +00001373 // Defer loading the anonymous namespace until we've finished merging
1374 // this namespace; loading it might load a later declaration of the
1375 // same namespace, and we have an invariant that older declarations
1376 // get merged before newer ones try to merge.
1377 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001378 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001379 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001380 } else {
1381 // Link this namespace back to the first declaration, which has already
1382 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001383 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001384 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001385
1386 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001387
1388 if (AnonNamespace) {
1389 // Each module has its own anonymous namespace, which is disjoint from
1390 // any other module's anonymous namespaces, so don't attach the anonymous
1391 // namespace at all.
1392 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001393 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001394 D->setAnonymousNamespace(Anon);
1395 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001396}
1397
Sebastian Redlb3298c32010-08-18 23:56:48 +00001398void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001399 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001400 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001401 D->NamespaceLoc = ReadSourceLocation();
1402 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001403 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001404 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001405 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001406}
1407
Sebastian Redlb3298c32010-08-18 23:56:48 +00001408void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001409 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001410 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001411 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001412 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1413 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001414 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001415 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001416 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001417 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001418}
1419
Richard Smith151c4562016-12-20 21:35:28 +00001420void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1421 VisitNamedDecl(D);
1422 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1423 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1424 for (unsigned I = 0; I != D->NumExpansions; ++I)
1425 Expansions[I] = ReadDeclAs<NamedDecl>();
1426 mergeMergeable(D);
1427}
1428
Sebastian Redlb3298c32010-08-18 23:56:48 +00001429void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001430 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001431 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001432 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1433 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1434 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001435 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001436 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001437 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001438}
1439
Richard Smith5179eb72016-06-28 19:03:57 +00001440void ASTDeclReader::VisitConstructorUsingShadowDecl(
1441 ConstructorUsingShadowDecl *D) {
1442 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001443 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1444 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001445 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001446}
1447
Sebastian Redlb3298c32010-08-18 23:56:48 +00001448void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001449 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001450 D->UsingLoc = ReadSourceLocation();
1451 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001452 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001453 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1454 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001455}
1456
Sebastian Redlb3298c32010-08-18 23:56:48 +00001457void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001458 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001459 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001460 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001461 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001462 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001463 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001464}
1465
Sebastian Redlb3298c32010-08-18 23:56:48 +00001466void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001467 UnresolvedUsingTypenameDecl *D) {
1468 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001469 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001470 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001471 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001472 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001473}
1474
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001475void ASTDeclReader::ReadCXXDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001476 struct CXXRecordDecl::DefinitionData &Data) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001477 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001478 Data.UserDeclaredConstructor = Record.readInt();
1479 Data.UserDeclaredSpecialMembers = Record.readInt();
1480 Data.Aggregate = Record.readInt();
1481 Data.PlainOldData = Record.readInt();
1482 Data.Empty = Record.readInt();
1483 Data.Polymorphic = Record.readInt();
1484 Data.Abstract = Record.readInt();
1485 Data.IsStandardLayout = Record.readInt();
1486 Data.HasNoNonEmptyBases = Record.readInt();
1487 Data.HasPrivateFields = Record.readInt();
1488 Data.HasProtectedFields = Record.readInt();
1489 Data.HasPublicFields = Record.readInt();
1490 Data.HasMutableFields = Record.readInt();
1491 Data.HasVariantMembers = Record.readInt();
1492 Data.HasOnlyCMembers = Record.readInt();
1493 Data.HasInClassInitializer = Record.readInt();
1494 Data.HasUninitializedReferenceMember = Record.readInt();
1495 Data.HasUninitializedFields = Record.readInt();
1496 Data.HasInheritedConstructor = Record.readInt();
1497 Data.HasInheritedAssignment = Record.readInt();
1498 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1499 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1500 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1501 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1502 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1503 Data.DefaultedDestructorIsDeleted = Record.readInt();
1504 Data.HasTrivialSpecialMembers = Record.readInt();
1505 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1506 Data.HasIrrelevantDestructor = Record.readInt();
1507 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1508 Data.HasDefaultedDefaultConstructor = Record.readInt();
1509 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1510 Data.HasConstexprDefaultConstructor = Record.readInt();
1511 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1512 Data.ComputedVisibleConversions = Record.readInt();
1513 Data.UserProvidedDefaultConstructor = Record.readInt();
1514 Data.DeclaredSpecialMembers = Record.readInt();
1515 Data.ImplicitCopyConstructorHasConstParam = Record.readInt();
1516 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1517 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1518 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Anders Carlsson703d6e62011-01-22 18:11:02 +00001519
David L. Jonesbe1557a2016-12-21 00:17:49 +00001520 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001521 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001522 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001523 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001524 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001525 Data.VBases = ReadGlobalOffset();
1526
David L. Jonesb6a8f022016-12-21 04:34:52 +00001527 Record.readUnresolvedSet(Data.Conversions);
1528 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001529 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001530 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001531
Douglas Gregor99ae8062012-02-14 17:54:36 +00001532 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001533 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001534 CXXRecordDecl::LambdaDefinitionData &Lambda
1535 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001536 Lambda.Dependent = Record.readInt();
1537 Lambda.IsGenericLambda = Record.readInt();
1538 Lambda.CaptureDefault = Record.readInt();
1539 Lambda.NumCaptures = Record.readInt();
1540 Lambda.NumExplicitCaptures = Record.readInt();
1541 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001542 Lambda.ContextDecl = ReadDeclID();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001543 Lambda.Captures
1544 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1545 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001546 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001547 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001548 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001549 bool IsImplicit = Record.readInt();
1550 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001551 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001552 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001553 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001554 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001555 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001556 break;
1557 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001558 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001559 VarDecl *Var = ReadDeclAs<VarDecl>();
1560 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001561 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1562 break;
1563 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001564 }
1565 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001566}
1567
Richard Smithcd45dbc2014-04-19 03:48:30 +00001568void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001569 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001570 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001571 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001572 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001573
Richard Smith02793752015-03-27 21:16:39 +00001574 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001575 // Track that we merged the definitions.
1576 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1577 DD.Definition));
1578 Reader.PendingDefinitions.erase(MergeDD.Definition);
1579 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001580 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001581 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1582 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001583 }
1584
Richard Smith2a9e5c52015-02-03 03:32:14 +00001585 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1586 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1587 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001588 // We faked up this definition data because we found a class for which we'd
1589 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001590 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001591 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001592
1593 // Don't change which declaration is the definition; that is required
1594 // to be invariant once we select it.
1595 auto *Def = DD.Definition;
1596 DD = std::move(MergeDD);
1597 DD.Definition = Def;
1598 return;
1599 }
1600
Richard Smithcd45dbc2014-04-19 03:48:30 +00001601 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001602 bool DetectedOdrViolation = false;
1603#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1604#define MATCH_FIELD(Field) \
1605 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1606 OR_FIELD(Field)
1607 MATCH_FIELD(UserDeclaredConstructor)
1608 MATCH_FIELD(UserDeclaredSpecialMembers)
1609 MATCH_FIELD(Aggregate)
1610 MATCH_FIELD(PlainOldData)
1611 MATCH_FIELD(Empty)
1612 MATCH_FIELD(Polymorphic)
1613 MATCH_FIELD(Abstract)
1614 MATCH_FIELD(IsStandardLayout)
1615 MATCH_FIELD(HasNoNonEmptyBases)
1616 MATCH_FIELD(HasPrivateFields)
1617 MATCH_FIELD(HasProtectedFields)
1618 MATCH_FIELD(HasPublicFields)
1619 MATCH_FIELD(HasMutableFields)
1620 MATCH_FIELD(HasVariantMembers)
1621 MATCH_FIELD(HasOnlyCMembers)
1622 MATCH_FIELD(HasInClassInitializer)
1623 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001624 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001625 MATCH_FIELD(HasInheritedConstructor)
1626 MATCH_FIELD(HasInheritedAssignment)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001627 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1628 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1629 MATCH_FIELD(NeedOverloadResolutionForDestructor)
1630 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1631 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1632 MATCH_FIELD(DefaultedDestructorIsDeleted)
1633 OR_FIELD(HasTrivialSpecialMembers)
1634 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1635 MATCH_FIELD(HasIrrelevantDestructor)
1636 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001637 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001638 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1639 OR_FIELD(HasConstexprDefaultConstructor)
1640 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1641 // ComputedVisibleConversions is handled below.
1642 MATCH_FIELD(UserProvidedDefaultConstructor)
1643 OR_FIELD(DeclaredSpecialMembers)
1644 MATCH_FIELD(ImplicitCopyConstructorHasConstParam)
1645 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1646 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1647 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1648 MATCH_FIELD(IsLambda)
1649#undef OR_FIELD
1650#undef MATCH_FIELD
1651
1652 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1653 DetectedOdrViolation = true;
1654 // FIXME: Issue a diagnostic if the base classes don't match when we come
1655 // to lazily load them.
1656
1657 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1658 // match when we come to lazily load them.
1659 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1660 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1661 DD.ComputedVisibleConversions = true;
1662 }
1663
1664 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1665 // lazily load it.
1666
1667 if (DD.IsLambda) {
1668 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1669 // when they occur within the body of a function template specialization).
1670 }
1671
1672 if (DetectedOdrViolation)
1673 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1674}
1675
Richard Smith2a9e5c52015-02-03 03:32:14 +00001676void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001677 struct CXXRecordDecl::DefinitionData *DD;
1678 ASTContext &C = Reader.getContext();
1679
1680 // Determine whether this is a lambda closure type, so that we can
1681 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001682 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001683 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001684 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001685 LCD_None);
1686 else
1687 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1688
David L. Jonesbe1557a2016-12-21 00:17:49 +00001689 ReadCXXDefinitionData(*DD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001690
Richard Smith5156c382015-01-22 01:41:56 +00001691 // We might already have a definition for this record. This can happen either
1692 // because we're reading an update record, or because we've already done some
1693 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001694 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001695 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001696 MergeDefinitionData(Canon, std::move(*DD));
1697 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001698 return;
1699 }
1700
Richard Smith97100e32015-06-20 00:22:34 +00001701 // Mark this declaration as being a definition.
1702 D->IsCompleteDefinition = true;
1703 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001704
Richard Smith97100e32015-06-20 00:22:34 +00001705 // If this is not the first declaration or is an update record, we can have
1706 // other redeclarations already. Make a note that we need to propagate the
1707 // DefinitionData pointer onto them.
1708 if (Update || Canon != D) {
1709 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001710 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001711 }
1712}
1713
Richard Smith1d209d02013-05-23 01:49:11 +00001714ASTDeclReader::RedeclarableResult
1715ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1716 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001717
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001718 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001719
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001720 enum CXXRecKind {
1721 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1722 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001723 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001724 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001725 // Merged when we merge the folding set entry in the primary template.
1726 if (!isa<ClassTemplateSpecializationDecl>(D))
1727 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001728 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001729 case CXXRecTemplate: {
1730 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001731 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001732 D->TemplateOrInstantiation = Template;
1733 if (!Template->getTemplatedDecl()) {
1734 // We've not actually loaded the ClassTemplateDecl yet, because we're
1735 // currently being loaded as its pattern. Rely on it to set up our
1736 // TypeForDecl (see VisitClassTemplateDecl).
1737 //
1738 // Beware: we do not yet know our canonical declaration, and may still
1739 // get merged once the surrounding class template has got off the ground.
1740 TypeIDForTypeDecl = 0;
1741 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001742 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001743 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001744 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001745 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001746 TemplateSpecializationKind TSK =
1747 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001748 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001749 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1750 MSI->setPointOfInstantiation(POI);
1751 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001752 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001753 break;
1754 }
1755 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001756
David L. Jonesbe1557a2016-12-21 00:17:49 +00001757 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001758 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001759 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001760 else
1761 // Propagate DefinitionData pointer from the canonical declaration.
1762 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1763
Richard Smith676c4042013-08-29 23:59:27 +00001764 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001765 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001766 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001767 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001768 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001769 // FIXME: This is wrong for the ARM ABI, where some other module may have
1770 // made this function no longer be a key function. We need an update
1771 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001772 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001773 }
Richard Smith1d209d02013-05-23 01:49:11 +00001774
1775 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001776}
1777
Sebastian Redlb3298c32010-08-18 23:56:48 +00001778void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001779 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001780
David L. Jonesbe1557a2016-12-21 00:17:49 +00001781 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001782 if (D->isCanonicalDecl()) {
1783 while (NumOverridenMethods--) {
1784 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1785 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001786 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001787 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1788 }
1789 } else {
1790 // We don't care about which declarations this used to override; we get
1791 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001792 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001793 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001794}
1795
Sebastian Redlb3298c32010-08-18 23:56:48 +00001796void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001797 // We need the inherited constructor information to merge the declaration,
1798 // so we have to read it before we call VisitCXXMethodDecl.
1799 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001800 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1801 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001802 *D->getTrailingObjects<InheritedConstructor>() =
1803 InheritedConstructor(Shadow, Ctor);
1804 }
1805
Chris Lattnerca025db2010-05-07 21:43:38 +00001806 VisitCXXMethodDecl(D);
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00001807
David L. Jonesbe1557a2016-12-21 00:17:49 +00001808 D->IsExplicitSpecified = Record.readInt();
Chris Lattnerca025db2010-05-07 21:43:38 +00001809}
1810
Sebastian Redlb3298c32010-08-18 23:56:48 +00001811void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001812 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001813
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001814 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001815 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1816 // FIXME: Check consistency if we have an old and new operator delete.
1817 if (!Canon->OperatorDelete)
1818 Canon->OperatorDelete = OperatorDelete;
1819 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001820}
1821
Sebastian Redlb3298c32010-08-18 23:56:48 +00001822void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001823 VisitCXXMethodDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001824 D->IsExplicitSpecified = Record.readInt();
Chris Lattnerca025db2010-05-07 21:43:38 +00001825}
1826
Douglas Gregorba345522011-12-02 23:23:56 +00001827void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1828 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001829 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001830 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001831 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001832 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001833 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001834 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001835}
1836
Sebastian Redlb3298c32010-08-18 23:56:48 +00001837void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001838 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001839 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001840}
1841
Sebastian Redlb3298c32010-08-18 23:56:48 +00001842void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001843 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001844 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001845 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001846 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001847 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001848 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001849 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001850 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001851 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001852 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001853 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001854}
1855
Sebastian Redlb3298c32010-08-18 23:56:48 +00001856void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001857 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001858 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001859 D->NumParams = NumParams;
1860 D->Params = new TemplateParameterList*[NumParams];
1861 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001862 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001863 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001864 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001865 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001866 D->Friend = GetTypeSourceInfo();
1867 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001868}
1869
Richard Smithf17fdbd2014-04-24 02:25:27 +00001870DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001871 VisitNamedDecl(D);
1872
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001873 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001874 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001875 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001876 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001877
Richard Smithf17fdbd2014-04-24 02:25:27 +00001878 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001879}
1880
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001881ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001882ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001883 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001884
Douglas Gregor68444de2012-01-14 15:13:49 +00001885 // Make sure we've allocated the Common pointer first. We do this before
1886 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1887 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1888 if (!CanonD->Common) {
1889 CanonD->Common = CanonD->newCommon(Reader.getContext());
1890 Reader.PendingDefinitions.insert(CanonD);
1891 }
1892 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001893
Douglas Gregor68444de2012-01-14 15:13:49 +00001894 // If this is the first declaration of the template, fill in the information
1895 // for the 'common' pointer.
1896 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001897 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001898 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001899 assert(RTD->getKind() == D->getKind() &&
1900 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001901 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001902 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001903 D->setMemberSpecialization();
1904 }
1905 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001906
Richard Smithf17fdbd2014-04-24 02:25:27 +00001907 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001908 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001909
Richard Smithf17fdbd2014-04-24 02:25:27 +00001910 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001911
Richard Smithd46d6de2013-10-13 23:50:45 +00001912 // If we merged the template with a prior declaration chain, merge the common
1913 // pointer.
1914 // FIXME: Actually merge here, don't just overwrite.
1915 D->Common = D->getCanonicalDecl()->Common;
1916
Douglas Gregor68444de2012-01-14 15:13:49 +00001917 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001918}
1919
Richard Smith0b884372015-02-27 00:25:58 +00001920static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1921 SmallVectorImpl<DeclID> &IDs) {
1922 assert(!IDs.empty() && "no IDs to add to list");
Richard Smith36befce2015-02-28 01:45:19 +00001923 if (Old) {
1924 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1925 std::sort(IDs.begin(), IDs.end());
1926 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1927 }
Richard Smith0b884372015-02-27 00:25:58 +00001928
Richard Smith36befce2015-02-28 01:45:19 +00001929 auto *Result = new (Context) DeclID[1 + IDs.size()];
1930 *Result = IDs.size();
1931 std::copy(IDs.begin(), IDs.end(), Result + 1);
Richard Smith0b884372015-02-27 00:25:58 +00001932 return Result;
1933}
1934
Sebastian Redlb3298c32010-08-18 23:56:48 +00001935void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001936 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001937
Douglas Gregor68444de2012-01-14 15:13:49 +00001938 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001939 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1940 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001941 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001942 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001943
Richard Smith0b884372015-02-27 00:25:58 +00001944 if (!SpecIDs.empty()) {
1945 auto *CommonPtr = D->getCommonPtr();
1946 CommonPtr->LazySpecializations = newDeclIDList(
1947 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001948 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001949 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00001950
1951 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1952 // We were loaded before our templated declaration was. We've not set up
1953 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1954 // it now.
1955 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00001956 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00001957 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001958}
1959
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001960void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
1961 llvm_unreachable("BuiltinTemplates are not serialized");
1962}
1963
Larisse Voufo30616382013-08-23 22:21:36 +00001964/// TODO: Unify with ClassTemplateDecl version?
1965/// May require unifying ClassTemplateDecl and
1966/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00001967void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
1968 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
1969
1970 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001971 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00001972 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001973 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001974 ReadDeclIDList(SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001975
Richard Smith0b884372015-02-27 00:25:58 +00001976 if (!SpecIDs.empty()) {
1977 auto *CommonPtr = D->getCommonPtr();
1978 CommonPtr->LazySpecializations = newDeclIDList(
1979 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001980 }
1981 }
1982}
1983
Richard Smith1d209d02013-05-23 01:49:11 +00001984ASTDeclReader::RedeclarableResult
1985ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
1986 ClassTemplateSpecializationDecl *D) {
1987 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001988
Douglas Gregor4163aca2011-09-09 21:34:22 +00001989 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001990 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001991 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00001992 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001993 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001994 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00001995 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00001996 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00001997 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00001998 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
1999 = new (C) ClassTemplateSpecializationDecl::
2000 SpecializedPartialSpecialization();
2001 PS->PartialSpecialization
2002 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2003 PS->TemplateArgs = ArgList;
2004 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002005 }
2006 }
2007
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002008 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002009 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002010 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002011 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002012 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002013
David L. Jonesbe1557a2016-12-21 00:17:49 +00002014 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002015 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002016 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002017 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002018 // Set this as, or find, the canonical declaration for this specialization
2019 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002020 if (ClassTemplatePartialSpecializationDecl *Partial =
2021 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002022 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002023 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002024 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002025 CanonSpec =
2026 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2027 }
2028 // If there was already a canonical specialization, merge into it.
2029 if (CanonSpec != D) {
2030 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2031
2032 // This declaration might be a definition. Merge with any existing
2033 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002034 if (auto *DDD = D->DefinitionData) {
2035 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002036 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002037 else
Richard Smith053f6c62014-05-16 23:01:30 +00002038 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002039 }
Richard Smith547864d2014-07-11 18:22:58 +00002040 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002041 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002042 }
2043 }
Richard Smith1d209d02013-05-23 01:49:11 +00002044
Richard Smithd55889a2013-09-09 16:55:27 +00002045 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002046 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002047 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2048 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2049 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002050 ExplicitInfo->ExternLoc = ReadSourceLocation();
2051 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002052 D->ExplicitInfo = ExplicitInfo;
2053 }
2054
Richard Smith1d209d02013-05-23 01:49:11 +00002055 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002056}
2057
Sebastian Redlb3298c32010-08-18 23:56:48 +00002058void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002059 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002060 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002061
David L. Jonesb6a8f022016-12-21 04:34:52 +00002062 D->TemplateParams = Record.readTemplateParameterList();
2063 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002064
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002065 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002066 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002067 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002068 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002069 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002070 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002071}
2072
Sebastian Redlb7448632011-08-31 13:59:56 +00002073void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2074 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002075 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002076 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002077}
2078
Sebastian Redlb3298c32010-08-18 23:56:48 +00002079void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002080 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002081
Douglas Gregor68444de2012-01-14 15:13:49 +00002082 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002083 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002084 SmallVector<serialization::DeclID, 32> SpecIDs;
2085 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002086
Richard Smith0b884372015-02-27 00:25:58 +00002087 if (!SpecIDs.empty()) {
2088 auto *CommonPtr = D->getCommonPtr();
2089 CommonPtr->LazySpecializations = newDeclIDList(
2090 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Richard Smithfeb3e1a2013-06-28 04:37:53 +00002091 }
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002092 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002093}
2094
Larisse Voufo30616382013-08-23 22:21:36 +00002095/// TODO: Unify with ClassTemplateSpecializationDecl version?
2096/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2097/// VarTemplate(Partial)SpecializationDecl with a new data
2098/// structure Template(Partial)SpecializationDecl, and
2099/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002100ASTDeclReader::RedeclarableResult
2101ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2102 VarTemplateSpecializationDecl *D) {
2103 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2104
2105 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002106 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002107 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2108 D->SpecializedTemplate = VTD;
2109 } else {
2110 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002111 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002112 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002113 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002114 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2115 new (C)
2116 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2117 PS->PartialSpecialization =
2118 cast<VarTemplatePartialSpecializationDecl>(InstD);
2119 PS->TemplateArgs = ArgList;
2120 D->SpecializedTemplate = PS;
2121 }
2122 }
2123
2124 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002125 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002126 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2127 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2128 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002129 ExplicitInfo->ExternLoc = ReadSourceLocation();
2130 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002131 D->ExplicitInfo = ExplicitInfo;
2132 }
2133
2134 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002135 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002136 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002137 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002138 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002139
David L. Jonesbe1557a2016-12-21 00:17:49 +00002140 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002141 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002142 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002143 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002144 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002145 if (VarTemplatePartialSpecializationDecl *Partial =
2146 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002147 CanonPattern->getCommonPtr()->PartialSpecializations
2148 .GetOrInsertNode(Partial);
2149 } else {
2150 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2151 }
2152 }
2153 }
2154
2155 return Redecl;
2156}
2157
Larisse Voufo30616382013-08-23 22:21:36 +00002158/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2159/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2160/// VarTemplate(Partial)SpecializationDecl with a new data
2161/// structure Template(Partial)SpecializationDecl, and
2162/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002163void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2164 VarTemplatePartialSpecializationDecl *D) {
2165 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2166
David L. Jonesb6a8f022016-12-21 04:34:52 +00002167 D->TemplateParams = Record.readTemplateParameterList();
2168 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002169
2170 // These are read/set from/to the first declaration.
2171 if (ThisDeclID == Redecl.getFirstID()) {
2172 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002173 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002174 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002175 }
2176}
2177
Sebastian Redlb3298c32010-08-18 23:56:48 +00002178void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002179 VisitTypeDecl(D);
2180
David L. Jonesbe1557a2016-12-21 00:17:49 +00002181 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002182
David L. Jonesbe1557a2016-12-21 00:17:49 +00002183 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002184 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002185}
2186
Sebastian Redlb3298c32010-08-18 23:56:48 +00002187void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002188 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002189 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002190 D->setDepth(Record.readInt());
2191 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002192 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002193 auto TypesAndInfos =
2194 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002195 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002196 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002197 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002198 }
2199 } else {
2200 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002201 D->ParameterPack = Record.readInt();
2202 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002203 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002204 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002205}
2206
Sebastian Redlb3298c32010-08-18 23:56:48 +00002207void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002208 VisitTemplateDecl(D);
2209 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002210 D->setDepth(Record.readInt());
2211 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002212 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002213 TemplateParameterList **Data =
2214 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002215 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2216 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002217 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002218 } else {
2219 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002220 D->ParameterPack = Record.readInt();
2221 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002222 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002223 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002224 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002225}
2226
Richard Smith3f1b5d02011-05-05 21:57:07 +00002227void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2228 VisitRedeclarableTemplateDecl(D);
2229}
2230
Sebastian Redlb3298c32010-08-18 23:56:48 +00002231void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002232 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002233 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002234 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002235 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002236 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002237}
2238
Michael Han84324352013-02-22 17:15:32 +00002239void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2240 VisitDecl(D);
2241}
2242
Mike Stump11289f42009-09-09 15:08:12 +00002243std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002244ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002245 uint64_t LexicalOffset = ReadLocalOffset();
2246 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002247 return std::make_pair(LexicalOffset, VisibleOffset);
2248}
2249
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002250template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002251ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002252ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002253 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002254 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002255
Richard Smith5fc18a92015-07-12 23:43:21 +00002256 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002257 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002258
Richard Smithd61d4ac2015-08-22 20:13:39 +00002259 uint64_t RedeclOffset = 0;
2260
Douglas Gregor358cd442012-01-15 16:58:34 +00002261 // 0 indicates that this declaration was the only declaration of its entity,
2262 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002263 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002264 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002265 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002266 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002267 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002268 // This declaration was the first local declaration, but may have imported
2269 // other declarations.
2270 IsKeyDecl = N == 1;
2271 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002272
Richard Smithfe620d22015-03-05 23:24:12 +00002273 // We have some declarations that must be before us in our redeclaration
2274 // chain. Read them now, and remember that we ought to merge with one of
2275 // them.
2276 // FIXME: Provide a known merge target to the second and subsequent such
2277 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002278 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002279 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002280
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002281 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002282 } else {
2283 // This declaration was not the first local declaration. Read the first
2284 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002285 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002286 }
2287
Douglas Gregor358cd442012-01-15 16:58:34 +00002288 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2289 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002290 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2291 // We temporarily set the first (canonical) declaration as the previous one
2292 // which is the one that matters and mark the real previous DeclID to be
2293 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002294 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002295 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002296 }
Richard Smithd8a83712015-08-22 01:47:18 +00002297
Richard Smithd8a83712015-08-22 01:47:18 +00002298 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002299
2300 // Note that we need to load local redeclarations of this decl and build a
2301 // decl chain for them. This must happen *after* we perform the preloading
2302 // above; this ensures that the redeclaration chain is built in the correct
2303 // order.
2304 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002305 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002306
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002307 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002308}
2309
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002310/// \brief Attempts to merge the given declaration (D) with another declaration
2311/// of the same entity.
2312template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002313void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002314 RedeclarableResult &Redecl,
2315 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002316 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002317 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002318 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002319
Richard Smith202850a2015-03-10 02:57:50 +00002320 // If we're not the canonical declaration, we don't need to merge.
2321 if (!DBase->isFirstDecl())
2322 return;
2323
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002324 T *D = static_cast<T*>(DBase);
2325
Richard Smith5a4737c2015-02-06 02:42:59 +00002326 if (auto *Existing = Redecl.getKnownMergeTarget())
2327 // We already know of an existing declaration we should merge with.
2328 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2329 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002330 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002331 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2332}
2333
2334/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2335/// We use this to put code in a template that will only be valid for certain
2336/// instantiations.
2337template<typename T> static T assert_cast(T t) { return t; }
2338template<typename T> static T assert_cast(...) {
2339 llvm_unreachable("bad assert_cast");
2340}
2341
2342/// \brief Merge together the pattern declarations from two template
2343/// declarations.
2344void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2345 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002346 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002347 auto *DPattern = D->getTemplatedDecl();
2348 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002349 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2350 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002351 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002352
Richard Smith547864d2014-07-11 18:22:58 +00002353 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2354 // Merge with any existing definition.
2355 // FIXME: This is duplicated in several places. Refactor.
2356 auto *ExistingClass =
2357 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002358 if (auto *DDD = DClass->DefinitionData) {
2359 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002360 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002361 } else {
2362 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002363 // We may have skipped this before because we thought that DClass
2364 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002365 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002366 }
2367 }
2368 DClass->DefinitionData = ExistingClass->DefinitionData;
2369
Richard Smithf17fdbd2014-04-24 02:25:27 +00002370 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2371 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002372 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002373 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2374 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2375 Result);
2376 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2377 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002378 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2379 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2380 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002381 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002382}
2383
2384/// \brief Attempts to merge the given declaration (D) with another declaration
2385/// of the same entity.
2386template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002387void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2388 RedeclarableResult &Redecl,
2389 DeclID TemplatePatternID) {
2390 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002391 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002392 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002393 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002394 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2395 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002396
Richard Smithd55889a2013-09-09 16:55:27 +00002397 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002398 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002399 // appropriate canonical declaration.
2400 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002401 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002402 ExistingCanon->Used |= D->Used;
2403 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002404
2405 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002406 // We cannot have loaded any redeclarations of this declaration yet, so
2407 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002408 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002409 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002410 assert_cast<NamespaceDecl*>(ExistingCanon));
2411
2412 // When we merge a template, merge its pattern.
2413 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2414 mergeTemplatePattern(
2415 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002416 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002417
Richard Smith5fc18a92015-07-12 23:43:21 +00002418 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002419 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002420 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002421 }
2422}
2423
Richard Smith0b87e072013-10-07 08:02:11 +00002424/// \brief Attempts to merge the given declaration (D) with another declaration
2425/// of the same entity, for the case where the entity is not actually
2426/// redeclarable. This happens, for instance, when merging the fields of
2427/// identical class definitions from two different modules.
2428template<typename T>
2429void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2430 // If modules are not available, there is no reason to perform this merge.
2431 if (!Reader.getContext().getLangOpts().Modules)
2432 return;
2433
Richard Smith01a73372013-10-15 22:02:41 +00002434 // ODR-based merging is only performed in C++. In C, identically-named things
2435 // in different translation units are not redeclarations (but may still have
2436 // compatible types).
2437 if (!Reader.getContext().getLangOpts().CPlusPlus)
2438 return;
2439
Richard Smith0b87e072013-10-07 08:02:11 +00002440 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2441 if (T *Existing = ExistingRes)
2442 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2443 Existing->getCanonicalDecl());
2444}
2445
Alexey Bataeva769e072013-03-22 06:34:35 +00002446void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2447 VisitDecl(D);
2448 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002449 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002450 Vars.reserve(NumVars);
2451 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002452 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002453 }
2454 D->setVars(Vars);
2455}
2456
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002457void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002458 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002459 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002460 D->setCombiner(Record.readExpr());
2461 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002462 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002463}
2464
Alexey Bataev4244be22016-02-11 05:35:55 +00002465void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002466 VisitVarDecl(D);
2467}
2468
Chris Lattner487412d2009-04-27 05:27:42 +00002469//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002470// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002471//===----------------------------------------------------------------------===//
2472
Chris Lattner8f63ab52009-04-27 06:01:06 +00002473/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002474void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2475 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002476 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002477 attr::Kind Kind = (attr::Kind)Record.readInt();
2478 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002479
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002480#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002481
2482 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002483 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002484 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002485}
2486
2487//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002488// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002489//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002490
2491/// \brief Note that we have loaded the declaration with the given
2492/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002493///
Chris Lattner487412d2009-04-27 05:27:42 +00002494/// This routine notes that this declaration has already been loaded,
2495/// so that future GetDecl calls will return this declaration rather
2496/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002497inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002498 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2499 DeclsLoaded[Index] = D;
2500}
2501
2502
2503/// \brief Determine whether the consumer will be interested in seeing
2504/// this declaration (via HandleTopLevelDecl).
2505///
2506/// This routine should return true for anything that might affect
2507/// code generation, e.g., inline function definitions, Objective-C
2508/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002509static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002510 // An ObjCMethodDecl is never considered as "interesting" because its
2511 // implementation container always is.
2512
Richard Smithdc1f0422016-07-20 19:10:16 +00002513 // An ImportDecl or VarDecl imported from a module will get emitted when
2514 // we import the relevant module.
2515 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && Ctx.DeclMustBeEmitted(D) &&
2516 D->getImportedOwningModule())
2517 return false;
2518
Douglas Gregor87d81242011-09-10 00:22:34 +00002519 if (isa<FileScopeAsmDecl>(D) ||
2520 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002521 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002522 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002523 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002524 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002525 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002526 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2527 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002528 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002529 return Var->isFileVarDecl() &&
2530 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002531 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002532 return Func->doesThisDeclarationHaveABody() || HasBody;
Douglas Gregor87d81242011-09-10 00:22:34 +00002533
2534 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002535}
2536
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002537/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002538ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002539ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002540 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2541 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002542 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002543 const DeclOffset &DOffs =
2544 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002545 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002546 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002547}
2548
Douglas Gregord32f0352011-07-22 06:10:01 +00002549ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002550 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002551 = GlobalBitOffsetsMap.find(GlobalOffset);
2552
2553 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2554 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2555}
2556
Douglas Gregorde3ef502011-11-30 23:21:26 +00002557uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002558 return LocalOffset + M.GlobalBitOffset;
2559}
2560
Richard Smithbf78e642013-06-24 22:51:00 +00002561static bool isSameTemplateParameterList(const TemplateParameterList *X,
2562 const TemplateParameterList *Y);
2563
2564/// \brief Determine whether two template parameters are similar enough
2565/// that they may be used in declarations of the same template.
2566static bool isSameTemplateParameter(const NamedDecl *X,
2567 const NamedDecl *Y) {
2568 if (X->getKind() != Y->getKind())
2569 return false;
2570
2571 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2572 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2573 return TX->isParameterPack() == TY->isParameterPack();
2574 }
2575
2576 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2577 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2578 return TX->isParameterPack() == TY->isParameterPack() &&
2579 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2580 }
2581
2582 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2583 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2584 return TX->isParameterPack() == TY->isParameterPack() &&
2585 isSameTemplateParameterList(TX->getTemplateParameters(),
2586 TY->getTemplateParameters());
2587}
2588
Richard Smith32952e12014-10-14 02:00:47 +00002589static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2590 if (auto *NS = X->getAsNamespace())
2591 return NS;
2592 if (auto *NAS = X->getAsNamespaceAlias())
2593 return NAS->getNamespace();
2594 return nullptr;
2595}
2596
2597static bool isSameQualifier(const NestedNameSpecifier *X,
2598 const NestedNameSpecifier *Y) {
2599 if (auto *NSX = getNamespace(X)) {
2600 auto *NSY = getNamespace(Y);
2601 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2602 return false;
2603 } else if (X->getKind() != Y->getKind())
2604 return false;
2605
2606 // FIXME: For namespaces and types, we're permitted to check that the entity
2607 // is named via the same tokens. We should probably do so.
2608 switch (X->getKind()) {
2609 case NestedNameSpecifier::Identifier:
2610 if (X->getAsIdentifier() != Y->getAsIdentifier())
2611 return false;
2612 break;
2613 case NestedNameSpecifier::Namespace:
2614 case NestedNameSpecifier::NamespaceAlias:
2615 // We've already checked that we named the same namespace.
2616 break;
2617 case NestedNameSpecifier::TypeSpec:
2618 case NestedNameSpecifier::TypeSpecWithTemplate:
2619 if (X->getAsType()->getCanonicalTypeInternal() !=
2620 Y->getAsType()->getCanonicalTypeInternal())
2621 return false;
2622 break;
2623 case NestedNameSpecifier::Global:
2624 case NestedNameSpecifier::Super:
2625 return true;
2626 }
2627
2628 // Recurse into earlier portion of NNS, if any.
2629 auto *PX = X->getPrefix();
2630 auto *PY = Y->getPrefix();
2631 if (PX && PY)
2632 return isSameQualifier(PX, PY);
2633 return !PX && !PY;
2634}
2635
Richard Smithbf78e642013-06-24 22:51:00 +00002636/// \brief Determine whether two template parameter lists are similar enough
2637/// that they may be used in declarations of the same template.
2638static bool isSameTemplateParameterList(const TemplateParameterList *X,
2639 const TemplateParameterList *Y) {
2640 if (X->size() != Y->size())
2641 return false;
2642
2643 for (unsigned I = 0, N = X->size(); I != N; ++I)
2644 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2645 return false;
2646
2647 return true;
2648}
2649
Douglas Gregor022857e2011-12-22 01:48:48 +00002650/// \brief Determine whether the two declarations refer to the same entity.
2651static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2652 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002653
Douglas Gregor022857e2011-12-22 01:48:48 +00002654 if (X == Y)
2655 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002656
Douglas Gregor022857e2011-12-22 01:48:48 +00002657 // Must be in the same context.
2658 if (!X->getDeclContext()->getRedeclContext()->Equals(
2659 Y->getDeclContext()->getRedeclContext()))
2660 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002661
2662 // Two typedefs refer to the same entity if they have the same underlying
2663 // type.
2664 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2665 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2666 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2667 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002668
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002669 // Must have the same kind.
2670 if (X->getKind() != Y->getKind())
2671 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002672
Douglas Gregorda389302012-01-01 21:47:52 +00002673 // Objective-C classes and protocols with the same name always match.
2674 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002675 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002676
2677 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002678 // No need to handle these here: we merge them when adding them to the
2679 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002680 return false;
2681 }
Richard Smithd55889a2013-09-09 16:55:27 +00002682
Douglas Gregor2009cee2012-01-03 22:46:00 +00002683 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002684 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2685 TagDecl *TagY = cast<TagDecl>(Y);
2686 return (TagX->getTagKind() == TagY->getTagKind()) ||
2687 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2688 TagX->getTagKind() == TTK_Interface) &&
2689 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2690 TagY->getTagKind() == TTK_Interface));
2691 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002692
Joao Matose9a3ed42012-08-31 22:18:20 +00002693 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002694 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2695 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002696 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2697 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002698 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2699 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2700 if (CtorX->getInheritedConstructor() &&
2701 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2702 CtorY->getInheritedConstructor().getConstructor()))
2703 return false;
2704 }
Rafael Espindola3ae00052013-05-13 00:12:11 +00002705 return (FuncX->getLinkageInternal() == FuncY->getLinkageInternal()) &&
Douglas Gregorb2585692012-01-04 17:13:46 +00002706 FuncX->getASTContext().hasSameType(FuncX->getType(), FuncY->getType());
2707 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002708
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002709 // Variables with the same type and linkage match.
2710 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2711 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002712 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2713 ASTContext &C = VarX->getASTContext();
2714 if (C.hasSameType(VarX->getType(), VarY->getType()))
2715 return true;
2716
2717 // We can get decls with different types on the redecl chain. Eg.
2718 // template <typename T> struct S { static T Var[]; }; // #1
2719 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2720 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002721 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002722 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2723 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2724 if (!VarXTy || !VarYTy)
2725 return false;
2726 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2727 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2728 }
2729 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002730 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002731
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002732 // Namespaces with the same name and inlinedness match.
2733 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2734 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2735 return NamespaceX->isInline() == NamespaceY->isInline();
2736 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002737
Richard Smith8f8f05c2013-06-24 04:45:28 +00002738 // Identical template names and kinds match if their template parameter lists
2739 // and patterns match.
2740 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002741 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002742 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002743 TemplateY->getTemplatedDecl()) &&
2744 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2745 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002746 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002747
Richard Smith0b87e072013-10-07 08:02:11 +00002748 // Fields with the same name and the same type match.
2749 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2750 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002751 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002752 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2753 }
2754
Richard Smith8cbd8952015-08-04 02:05:09 +00002755 // Indirect fields with the same target field match.
2756 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2757 auto *IFDY = cast<IndirectFieldDecl>(Y);
2758 return IFDX->getAnonField()->getCanonicalDecl() ==
2759 IFDY->getAnonField()->getCanonicalDecl();
2760 }
2761
Richard Smith01a73372013-10-15 22:02:41 +00002762 // Enumerators with the same name match.
2763 if (isa<EnumConstantDecl>(X))
2764 // FIXME: Also check the value is odr-equivalent.
2765 return true;
2766
Richard Smithfd8634a2013-10-23 02:17:46 +00002767 // Using shadow declarations with the same target match.
2768 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2769 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2770 return USX->getTargetDecl() == USY->getTargetDecl();
2771 }
2772
Richard Smith32952e12014-10-14 02:00:47 +00002773 // Using declarations with the same qualifier match. (We already know that
2774 // the name matches.)
2775 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2776 auto *UY = cast<UsingDecl>(Y);
2777 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2778 UX->hasTypename() == UY->hasTypename() &&
2779 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2780 }
2781 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2782 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2783 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2784 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2785 }
2786 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2787 return isSameQualifier(
2788 UX->getQualifier(),
2789 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2790
Richard Smithf4634362014-09-03 23:11:22 +00002791 // Namespace alias definitions with the same target match.
2792 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2793 auto *NAY = cast<NamespaceAliasDecl>(Y);
2794 return NAX->getNamespace()->Equals(NAY->getNamespace());
2795 }
2796
Douglas Gregor022857e2011-12-22 01:48:48 +00002797 return false;
2798}
2799
Richard Smithd55889a2013-09-09 16:55:27 +00002800/// Find the context in which we should search for previous declarations when
2801/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002802DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2803 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002804 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2805 return ND->getOriginalNamespace();
2806
Richard Smith8a639892015-01-24 01:07:20 +00002807 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2808 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002809 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002810 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002811 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002812
2813 // If there's no definition yet, then DC's definition is added by an update
2814 // record, but we've not yet loaded that update record. In this case, we
2815 // commit to DC being the canonical definition now, and will fix this when
2816 // we load the update record.
2817 if (!DD) {
2818 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2819 RD->IsCompleteDefinition = true;
2820 RD->DefinitionData = DD;
2821 RD->getCanonicalDecl()->DefinitionData = DD;
2822
2823 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002824 Reader.PendingFakeDefinitionData.insert(
2825 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002826 }
2827
2828 return DD->Definition;
2829 }
Richard Smithd55889a2013-09-09 16:55:27 +00002830
Richard Smith01a73372013-10-15 22:02:41 +00002831 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002832 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2833 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002834
Richard Smith4eca9b92015-02-04 23:37:59 +00002835 // We can see the TU here only if we have no Sema object. In that case,
2836 // there's no TU scope to look in, so using the DC alone is sufficient.
2837 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2838 return TU;
2839
Craig Toppera13603a2014-05-22 05:54:18 +00002840 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002841}
2842
Douglas Gregor022857e2011-12-22 01:48:48 +00002843ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002844 // Record that we had a typedef name for linkage whether or not we merge
2845 // with that declaration.
2846 if (TypedefNameForLinkage) {
2847 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2848 Reader.ImportedTypedefNamesForLinkage.insert(
2849 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2850 return;
2851 }
2852
Douglas Gregor9b47f942012-01-09 17:38:47 +00002853 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002854 return;
Richard Smith95d99302013-07-13 02:00:19 +00002855
Richard Smith70d58502014-08-30 00:04:23 +00002856 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002857 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002858 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002859 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2860 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002861 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002862 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002863 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002864 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2865 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002866 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002867 // Add the declaration to its redeclaration context so later merging
2868 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002869 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002870 }
2871}
2872
Richard Smith88ebade2014-08-23 01:45:27 +00002873/// Find the declaration that should be merged into, given the declaration found
2874/// by name lookup. If we're merging an anonymous declaration within a typedef,
2875/// we need a matching typedef, and we merge with the type inside it.
2876static NamedDecl *getDeclForMerging(NamedDecl *Found,
2877 bool IsTypedefNameForLinkage) {
2878 if (!IsTypedefNameForLinkage)
2879 return Found;
2880
2881 // If we found a typedef declaration that gives a name to some other
2882 // declaration, then we want that inner declaration. Declarations from
2883 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002884 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002885 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002886
2887 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00002888 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00002889
Hans Wennborgdcfba332015-10-06 23:40:43 +00002890 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00002891}
2892
Richard Smithd08aeb62014-08-28 01:33:39 +00002893NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2894 DeclContext *DC,
2895 unsigned Index) {
2896 // If the lexical context has been merged, look into the now-canonical
2897 // definition.
2898 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2899 DC = Merged;
2900
2901 // If we've seen this before, return the canonical declaration.
2902 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2903 if (Index < Previous.size() && Previous[Index])
2904 return Previous[Index];
2905
2906 // If this is the first time, but we have parsed a declaration of the context,
2907 // build the anonymous declaration list from the parsed declaration.
2908 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00002909 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
2910 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00002911 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
2912 else
Richard Smith2b560572015-02-07 03:11:11 +00002913 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
2914 });
Richard Smithd08aeb62014-08-28 01:33:39 +00002915 }
2916
2917 return Index < Previous.size() ? Previous[Index] : nullptr;
2918}
2919
2920void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
2921 DeclContext *DC, unsigned Index,
2922 NamedDecl *D) {
2923 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2924 DC = Merged;
2925
2926 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2927 if (Index >= Previous.size())
2928 Previous.resize(Index + 1);
2929 if (!Previous[Index])
2930 Previous[Index] = D;
2931}
2932
Douglas Gregor022857e2011-12-22 01:48:48 +00002933ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00002934 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
2935 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00002936
Richard Smithd08aeb62014-08-28 01:33:39 +00002937 if (!Name && !needsAnonymousDeclarationNumber(D)) {
2938 // Don't bother trying to find unnamed declarations that are in
2939 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00002940 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
2941 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00002942 Result.suppress();
2943 return Result;
2944 }
Richard Smith95d99302013-07-13 02:00:19 +00002945
Douglas Gregor022857e2011-12-22 01:48:48 +00002946 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00002947 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00002948 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00002949 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00002950 if (It != Reader.ImportedTypedefNamesForLinkage.end())
2951 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00002952 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
2953 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00002954 // Go on to check in other places in case an existing typedef name
2955 // was not imported.
2956 }
Richard Smithd08aeb62014-08-28 01:33:39 +00002957
Richard Smith2b560572015-02-07 03:11:11 +00002958 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002959 // This is an anonymous declaration that we may need to merge. Look it up
2960 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00002961 if (auto *Existing = getAnonymousDeclForMerging(
2962 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
2963 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00002964 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
2965 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00002966 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002967 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002968 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00002969
2970 // Temporarily consider the identifier to be up-to-date. We don't want to
2971 // cause additional lookups here.
2972 class UpToDateIdentifierRAII {
2973 IdentifierInfo *II;
2974 bool WasOutToDate;
2975
2976 public:
2977 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
2978 : II(II), WasOutToDate(false)
2979 {
2980 if (II) {
2981 WasOutToDate = II->isOutOfDate();
2982 if (WasOutToDate)
2983 II->setOutOfDate(false);
2984 }
2985 }
2986
2987 ~UpToDateIdentifierRAII() {
2988 if (WasOutToDate)
2989 II->setOutOfDate(true);
2990 }
2991 } UpToDate(Name.getAsIdentifierInfo());
2992
Douglas Gregor2009cee2012-01-03 22:46:00 +00002993 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00002994 IEnd = IdResolver.end();
2995 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00002996 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00002997 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00002998 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
2999 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003000 }
Richard Smith8a639892015-01-24 01:07:20 +00003001 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003002 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003003 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003004 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003005 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003006 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3007 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003008 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003009 } else {
3010 // Not in a mergeable context.
3011 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003012 }
Richard Smithd55889a2013-09-09 16:55:27 +00003013
Richard Smith2b9e3e32013-10-18 06:05:18 +00003014 // If this declaration is from a merged context, make a note that we need to
3015 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003016 //
3017 // FIXME: We should do something similar if we merge two definitions of the
3018 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003019 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3020 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3021 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003022 Reader.PendingOdrMergeChecks.push_back(D);
3023
Richard Smithd08aeb62014-08-28 01:33:39 +00003024 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003025 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003026}
3027
Richard Smithb321ecb2014-05-13 01:15:00 +00003028template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003029Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3030 return D->RedeclLink.getLatestNotUpdated();
3031}
3032Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3033 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3034}
3035
3036Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3037 assert(D);
3038
3039 switch (D->getKind()) {
3040#define ABSTRACT_DECL(TYPE)
3041#define DECL(TYPE, BASE) \
3042 case Decl::TYPE: \
3043 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3044#include "clang/AST/DeclNodes.inc"
3045 }
3046 llvm_unreachable("unknown decl kind");
3047}
3048
Richard Smith8ce51082015-03-11 01:44:51 +00003049Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3050 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3051}
3052
Richard Smithc3a53252015-02-28 05:57:02 +00003053template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003054void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3055 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003056 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003057 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003058 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003059}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003060
Richard Smith24d166c2014-07-31 23:52:38 +00003061namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003062template<>
3063void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003064 Redeclarable<VarDecl> *D,
3065 Decl *Previous, Decl *Canon) {
3066 VarDecl *VD = static_cast<VarDecl*>(D);
3067 VarDecl *PrevVD = cast<VarDecl>(Previous);
3068 D->RedeclLink.setPrevious(PrevVD);
3069 D->First = PrevVD->First;
3070
3071 // We should keep at most one definition on the chain.
3072 // FIXME: Cache the definition once we've found it. Building a chain with
3073 // N definitions currently takes O(N^2) time here.
3074 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3075 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3076 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3077 Reader.mergeDefinitionVisibility(CurD, VD);
3078 VD->demoteThisDefinitionToDeclaration();
3079 break;
3080 }
3081 }
3082 }
3083}
3084
3085template<>
3086void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003087 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003088 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003089 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3090 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3091
3092 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003093 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003094
3095 // If the previous declaration is an inline function declaration, then this
3096 // declaration is too.
3097 if (PrevFD->IsInline != FD->IsInline) {
3098 // FIXME: [dcl.fct.spec]p4:
3099 // If a function with external linkage is declared inline in one
3100 // translation unit, it shall be declared inline in all translation
3101 // units in which it appears.
3102 //
3103 // Be careful of this case:
3104 //
3105 // module A:
3106 // template<typename T> struct X { void f(); };
3107 // template<typename T> inline void X<T>::f() {}
3108 //
3109 // module B instantiates the declaration of X<int>::f
3110 // module C instantiates the definition of X<int>::f
3111 //
3112 // If module B and C are merged, we do not have a violation of this rule.
3113 FD->IsInline = true;
3114 }
3115
Richard Smith9e2341d2015-03-23 03:25:59 +00003116 // If we need to propagate an exception specification along the redecl
3117 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003118 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3119 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003120 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003121 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3122 bool WasUnresolved =
3123 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3124 if (IsUnresolved != WasUnresolved)
3125 Reader.PendingExceptionSpecUpdates.insert(
3126 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003127 }
3128}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003129} // end namespace clang
3130
Richard Smith6de7a242014-07-31 23:46:44 +00003131void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003132 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3133}
3134
Richard Smith8346e522015-06-10 01:47:58 +00003135/// Inherit the default template argument from \p From to \p To. Returns
3136/// \c false if there is no default template for \p From.
3137template <typename ParmDecl>
3138static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3139 Decl *ToD) {
3140 auto *To = cast<ParmDecl>(ToD);
3141 if (!From->hasDefaultArgument())
3142 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003143 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003144 return true;
3145}
3146
3147static void inheritDefaultTemplateArguments(ASTContext &Context,
3148 TemplateDecl *From,
3149 TemplateDecl *To) {
3150 auto *FromTP = From->getTemplateParameters();
3151 auto *ToTP = To->getTemplateParameters();
3152 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3153
3154 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3155 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003156 if (FromParam->isParameterPack())
3157 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003158 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3159
3160 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003161 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003162 break;
3163 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003164 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003165 break;
3166 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003167 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003168 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3169 break;
3170 }
3171 }
3172}
3173
Richard Smith6de7a242014-07-31 23:46:44 +00003174void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003175 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003176 assert(D && Previous);
3177
3178 switch (D->getKind()) {
3179#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003180#define DECL(TYPE, BASE) \
3181 case Decl::TYPE: \
3182 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003183 break;
3184#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003185 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003186
3187 // If the declaration was visible in one module, a redeclaration of it in
3188 // another module remains visible even if it wouldn't be visible by itself.
3189 //
3190 // FIXME: In this case, the declaration should only be visible if a module
3191 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003192 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003193 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003194 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003195
Richard Smith8346e522015-06-10 01:47:58 +00003196 // If the declaration declares a template, it may inherit default arguments
3197 // from the previous declaration.
3198 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3199 inheritDefaultTemplateArguments(Reader.getContext(),
3200 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003201}
3202
Richard Smithb321ecb2014-05-13 01:15:00 +00003203template<typename DeclT>
3204void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003205 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003206}
3207void ASTDeclReader::attachLatestDeclImpl(...) {
3208 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3209}
3210
Douglas Gregor05f10352011-12-17 23:38:30 +00003211void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3212 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003213
3214 switch (D->getKind()) {
3215#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003216#define DECL(TYPE, BASE) \
3217 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003218 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3219 break;
3220#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003221 }
3222}
3223
Richard Smith851072e2014-05-19 20:59:20 +00003224template<typename DeclT>
3225void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3226 D->RedeclLink.markIncomplete();
3227}
3228void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3229 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3230}
3231
3232void ASTReader::markIncompleteDeclChain(Decl *D) {
3233 switch (D->getKind()) {
3234#define ABSTRACT_DECL(TYPE)
3235#define DECL(TYPE, BASE) \
3236 case Decl::TYPE: \
3237 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3238 break;
3239#include "clang/AST/DeclNodes.inc"
3240 }
3241}
3242
Sebastian Redlb3298c32010-08-18 23:56:48 +00003243/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003244Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003245 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003246 SourceLocation DeclLoc;
3247 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003248 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003249 // Keep track of where we are in the stream, then jump back there
3250 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003251 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003252
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003253 ReadingKindTracker ReadingKind(Read_Decl, *this);
3254
Douglas Gregor1342e842009-07-06 18:54:52 +00003255 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003256 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003257
Sebastian Redl2c373b92010-10-05 15:59:54 +00003258 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003259 ASTRecordReader Record(*this, *Loc.F);
3260 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003261 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003262
Craig Toppera13603a2014-05-22 05:54:18 +00003263 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003264 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003265 case DECL_CONTEXT_LEXICAL:
3266 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003267 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003268 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003269 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003270 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003271 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003272 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003273 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003274 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003275 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003276 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003277 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003278 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003279 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003280 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003281 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003282 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003283 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003284 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003285 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003286 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003287 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003288 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003289 case DECL_EXPORT:
3290 D = ExportDecl::CreateDeserialized(Context, ID);
3291 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003292 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003293 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003294 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003295 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003296 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003297 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003298 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003299 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003300 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003301 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003302 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003303 break;
Richard Smith151c4562016-12-20 21:35:28 +00003304 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003305 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003306 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003307 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003308 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003309 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003310 case DECL_CONSTRUCTOR_USING_SHADOW:
3311 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3312 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003313 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003314 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003315 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003316 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003317 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003318 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003319 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003320 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003321 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003322 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003323 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003324 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003325 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003326 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003327 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003328 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003329 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3330 break;
3331 case DECL_CXX_INHERITED_CONSTRUCTOR:
3332 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003333 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003334 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003335 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003336 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003337 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003338 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003339 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003340 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003341 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003342 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003343 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003344 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003345 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003346 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003347 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003348 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003349 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003350 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003351 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003353 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003354 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003355 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003356 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003357 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003358 case DECL_VAR_TEMPLATE:
3359 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3360 break;
3361 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3362 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3363 break;
3364 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3365 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3366 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003367 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003368 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003369 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003370 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003371 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003372 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003373 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003374 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003375 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003376 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003377 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003378 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003379 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003380 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3381 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003382 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003383 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003384 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003385 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003386 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3387 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003388 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003389 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003390 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003391 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003392 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003393 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003394 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003395 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003396 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003397 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003398 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003399 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003400 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003401 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003402 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003403 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003404 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003405 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003406 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003407 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003408 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003409 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003410 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003411 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003412 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003413 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003414 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003415 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003416 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003417 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003418 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003419 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003420 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003421 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003422 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003423 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003424 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003425 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003426 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003427 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003428 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003429 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003430 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003431 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003432 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003433 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003434 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003435 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003436 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003437 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003438 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003439 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003440 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003441 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003442 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003443 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003444 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003445 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003446 break;
3447 case DECL_BINDING:
3448 D = BindingDecl::CreateDeserialized(Context, ID);
3449 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003450 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003451 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003452 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003453 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003454 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003455 break;
John McCall5e77d762013-04-16 07:28:30 +00003456 case DECL_MS_PROPERTY:
3457 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3458 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003459 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003460 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003461 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003462 case DECL_CXX_BASE_SPECIFIERS:
3463 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003464 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003465 case DECL_CXX_CTOR_INITIALIZERS:
3466 Error("attempt to read a C++ ctor initializer record as a declaration");
3467 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003468 case DECL_IMPORT:
3469 // Note: last entry of the ImportDecl record is the number of stored source
3470 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003471 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003472 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003473 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003474 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003475 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003476 case DECL_OMP_DECLARE_REDUCTION:
3477 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3478 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003479 case DECL_OMP_CAPTUREDEXPR:
3480 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003481 break;
Nico Weber66220292016-03-02 17:28:48 +00003482 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003483 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003484 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003485 case DECL_PRAGMA_DETECT_MISMATCH:
3486 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003487 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003488 break;
Michael Han84324352013-02-22 17:15:32 +00003489 case DECL_EMPTY:
3490 D = EmptyDecl::CreateDeserialized(Context, ID);
3491 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003492 case DECL_OBJC_TYPE_PARAM:
3493 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3494 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003495 }
Chris Lattner487412d2009-04-27 05:27:42 +00003496
Sebastian Redlb3298c32010-08-18 23:56:48 +00003497 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003498 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003499 // Set the DeclContext before doing any deserialization, to make sure internal
3500 // calls to Decl::getASTContext() by Decl's methods will find the
3501 // TranslationUnitDecl without crashing.
3502 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003503 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003504
3505 // If this declaration is also a declaration context, get the
3506 // offsets for its tables of lexical and visible declarations.
3507 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3508 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003509 if (Offsets.first &&
3510 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3511 return nullptr;
3512 if (Offsets.second &&
3513 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3514 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003515 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003516 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003517
Douglas Gregordab42432011-08-12 00:15:20 +00003518 // Load any relevant update records.
Richard Smithd1c46742014-04-30 02:24:17 +00003519 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003520
Douglas Gregor404cdde2012-01-27 01:47:08 +00003521 // Load the categories after recursive loading is finished.
3522 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003523 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3524 // we put the Decl in PendingDefinitions so we can pull the categories here.
3525 if (Class->isThisDeclarationADefinition() ||
3526 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003527 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003528
Richard Smith13fb8602016-07-15 21:33:46 +00003529 // If we have deserialized a declaration that has a definition the
3530 // AST consumer might need to know about, queue it.
3531 // We don't pass it to the consumer immediately because we may be in recursive
3532 // loading, and some declarations may still be initializing.
Richard Smithdc1f0422016-07-20 19:10:16 +00003533 if (isConsumerInterestedIn(Context, D, Reader.hasPendingBody()))
Richard Smith13fb8602016-07-15 21:33:46 +00003534 InterestingDecls.push_back(D);
3535
Douglas Gregordab42432011-08-12 00:15:20 +00003536 return D;
3537}
3538
3539void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003540 // The declaration may have been modified by files later in the chain.
3541 // If this is the case, read the record containing the updates from each file
3542 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003543 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003544 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3545 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003546 auto UpdateOffsets = std::move(UpdI->second);
3547 DeclUpdateOffsets.erase(UpdI);
3548
Richard Smithdc1f0422016-07-20 19:10:16 +00003549 bool WasInteresting = isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003550 for (auto &FileAndOffset : UpdateOffsets) {
3551 ModuleFile *F = FileAndOffset.first;
3552 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003553 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3554 SavedStreamPosition SavedPosition(Cursor);
3555 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003556 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003557 ASTRecordReader Record(*this, *F);
3558 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003559 (void)RecCode;
3560 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003561
David L. Jonesbe1557a2016-12-21 00:17:49 +00003562 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3563 SourceLocation());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003564 Reader.UpdateDecl(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003565
3566 // We might have made this declaration interesting. If so, remember that
3567 // we need to hand it off to the consumer.
3568 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003569 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Richard Smith13fb8602016-07-15 21:33:46 +00003570 InterestingDecls.push_back(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003571 WasInteresting = true;
3572 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003573 }
3574 }
Richard Smith1e8e91b2016-04-08 20:53:26 +00003575
3576 // Load the pending visible updates for this decl context, if it has any.
3577 auto I = PendingVisibleUpdates.find(ID);
3578 if (I != PendingVisibleUpdates.end()) {
3579 auto VisibleUpdates = std::move(I->second);
3580 PendingVisibleUpdates.erase(I);
3581
3582 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3583 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3584 Lookups[DC].Table.add(
3585 Update.Mod, Update.Data,
3586 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3587 DC->setHasExternalVisibleStorage(true);
3588 }
Chris Lattner487412d2009-04-27 05:27:42 +00003589}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003590
Richard Smithd61d4ac2015-08-22 20:13:39 +00003591void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3592 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003593 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003594 if (FirstLocal != CanonDecl) {
3595 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3596 ASTDeclReader::attachPreviousDecl(
3597 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3598 CanonDecl);
3599 }
3600
3601 if (!LocalOffset) {
3602 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3603 return;
3604 }
3605
3606 // Load the list of other redeclarations from this module file.
3607 ModuleFile *M = getOwningModuleFile(FirstLocal);
3608 assert(M && "imported decl from no module file");
3609
3610 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3611 SavedStreamPosition SavedPosition(Cursor);
3612 Cursor.JumpToBit(LocalOffset);
3613
3614 RecordData Record;
3615 unsigned Code = Cursor.ReadCode();
3616 unsigned RecCode = Cursor.readRecord(Code, Record);
3617 (void)RecCode;
3618 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3619
3620 // FIXME: We have several different dispatches on decl kind here; maybe
3621 // we should instead generate one loop per kind and dispatch up-front?
3622 Decl *MostRecent = FirstLocal;
3623 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3624 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003625 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3626 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003627 }
Richard Smithc3a53252015-02-28 05:57:02 +00003628 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003629}
3630
3631namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003632 /// \brief Given an ObjC interface, goes through the modules and links to the
3633 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003634 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003635 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003636 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003637 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003638 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003639 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003640 serialization::GlobalDeclID InterfaceID;
3641 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003642
3643 void add(ObjCCategoryDecl *Cat) {
3644 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003645 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003646 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003647
3648 // Check for duplicate categories.
3649 if (Cat->getDeclName()) {
3650 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3651 if (Existing &&
3652 Reader.getOwningModuleFile(Existing)
3653 != Reader.getOwningModuleFile(Cat)) {
3654 // FIXME: We should not warn for duplicates in diamond:
3655 //
3656 // MT //
3657 // / \ //
3658 // ML MR //
3659 // \ / //
3660 // MB //
3661 //
3662 // If there are duplicates in ML/MR, there will be warning when
3663 // creating MB *and* when importing MB. We should not warn when
3664 // importing.
3665 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3666 << Interface->getDeclName() << Cat->getDeclName();
3667 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3668 } else if (!Existing) {
3669 // Record this category.
3670 Existing = Cat;
3671 }
3672 }
3673
3674 // Add this category to the end of the chain.
3675 if (Tail)
3676 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3677 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003678 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003679 Tail = Cat;
3680 }
3681
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003682 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003683 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003684 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003685 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3686 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003687 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003688 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3689 Tail(nullptr), InterfaceID(InterfaceID),
3690 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003691 {
3692 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003693 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003694 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003695 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003696
3697 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003698 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003699 }
3700 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003701
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003702 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003703 // If we've loaded all of the category information we care about from
3704 // this module file, we're done.
3705 if (M.Generation <= PreviousGeneration)
3706 return true;
3707
3708 // Map global ID of the definition down to the local ID used in this
3709 // module file. If there is no such mapping, we'll find nothing here
3710 // (or in any module it imports).
3711 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3712 if (!LocalID)
3713 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003714
Douglas Gregor404cdde2012-01-27 01:47:08 +00003715 // Perform a binary search to find the local redeclarations for this
3716 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003717 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003718 const ObjCCategoriesInfo *Result
3719 = std::lower_bound(M.ObjCCategoriesMap,
3720 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003721 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003722 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3723 Result->DefinitionID != LocalID) {
3724 // We didn't find anything. If the class definition is in this module
3725 // file, then the module files it depends on cannot have any categories,
3726 // so suppress further lookup.
3727 return Reader.isDeclIDFromModule(InterfaceID, M);
3728 }
3729
3730 // We found something. Dig out all of the categories.
3731 unsigned Offset = Result->Offset;
3732 unsigned N = M.ObjCCategories[Offset];
3733 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3734 for (unsigned I = 0; I != N; ++I)
3735 add(cast_or_null<ObjCCategoryDecl>(
3736 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3737 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003738 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003739 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003740} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003741
Douglas Gregor404cdde2012-01-27 01:47:08 +00003742void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3743 ObjCInterfaceDecl *D,
3744 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003745 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003746 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003747 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003748}
Douglas Gregordab42432011-08-12 00:15:20 +00003749
Richard Smithd6db68c2014-08-07 20:58:41 +00003750template<typename DeclT, typename Fn>
3751static void forAllLaterRedecls(DeclT *D, Fn F) {
3752 F(D);
3753
3754 // Check whether we've already merged D into its redeclaration chain.
3755 // MostRecent may or may not be nullptr if D has not been merged. If
3756 // not, walk the merged redecl chain and see if it's there.
3757 auto *MostRecent = D->getMostRecentDecl();
3758 bool Found = false;
3759 for (auto *Redecl = MostRecent; Redecl && !Found;
3760 Redecl = Redecl->getPreviousDecl())
3761 Found = (Redecl == D);
3762
3763 // If this declaration is merged, apply the functor to all later decls.
3764 if (Found) {
3765 for (auto *Redecl = MostRecent; Redecl != D;
3766 Redecl = Redecl->getPreviousDecl())
3767 F(Redecl);
3768 }
3769}
3770
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003771void ASTDeclReader::UpdateDecl(Decl *D) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003772 while (Record.getIdx() < Record.size()) {
3773 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003774 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003775 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003776 // FIXME: If we also have an update record for instantiating the
3777 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003778 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003779 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003780 // FIXME: We should call addHiddenDecl instead, to add the member
3781 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003782 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003783 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003784 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003785
3786 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3787 // It will be added to the template's specializations set when loaded.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003788 (void)Record.readDecl();
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003789 break;
3790
3791 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003792 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3793
Douglas Gregor540fd812012-01-09 18:07:24 +00003794 // Each module has its own anonymous namespace, which is disjoint from
3795 // any other module's anonymous namespaces, so don't attach the anonymous
3796 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003797 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003798 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3799 TU->setAnonymousNamespace(Anon);
3800 else
3801 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3802 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003803 break;
3804 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003805
3806 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3807 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003808 ReadSourceLocation());
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003809 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00003810
John McCall32791cc2016-01-06 22:34:54 +00003811 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3812 auto Param = cast<ParmVarDecl>(D);
3813
3814 // We have to read the default argument regardless of whether we use it
3815 // so that hypothetical further update records aren't messed up.
3816 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003817 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003818
3819 // Only apply the update if the parameter still has an uninstantiated
3820 // default argument.
3821 if (Param->hasUninstantiatedDefaultArg())
3822 Param->setDefaultArg(DefaultArg);
3823 break;
3824 }
3825
Richard Smith4b054b22016-08-24 21:25:37 +00003826 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3827 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003828 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003829
3830 // Only apply the update if the field still has an uninstantiated
3831 // default member initializer.
3832 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3833 if (DefaultInit)
3834 FD->setInClassInitializer(DefaultInit);
3835 else
3836 // Instantiation failed. We can get here if we serialized an AST for
3837 // an invalid program.
3838 FD->removeInClassInitializer();
3839 }
3840 break;
3841 }
3842
Richard Smith4d235792014-08-07 18:53:08 +00003843 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003844 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003845 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003846 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003847 // It's safe to stop now because this update record is always last.
3848 return;
3849 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003850
David L. Jonesbe1557a2016-12-21 00:17:49 +00003851 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00003852 // Maintain AST consistency: any later redeclarations of this function
3853 // are inline if this one is. (We might have merged another declaration
3854 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00003855 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3856 FD->setImplicitlyInline();
3857 });
Richard Smith195d8ef2014-05-29 03:15:31 +00003858 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003859 FD->setInnerLocStart(ReadSourceLocation());
Richard Smithc2bb8182015-03-24 06:36:48 +00003860 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003861 CD->NumCtorInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00003862 if (CD->NumCtorInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003863 CD->CtorInitializers = ReadGlobalOffset();
Richard Smithc2bb8182015-03-24 06:36:48 +00003864 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003865 // Store the offset of the body so we can lazily load it later.
3866 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
Richard Smith04d05b52014-03-23 00:27:18 +00003867 HasPendingBody = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003868 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00003869 break;
3870 }
3871
Richard Smithcd45dbc2014-04-19 03:48:30 +00003872 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
3873 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00003874 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00003875 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00003876 OldDD && (OldDD->Definition != RD ||
3877 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00003878 ReadCXXRecordDefinition(RD, /*Update*/true);
3879
Richard Smithcd45dbc2014-04-19 03:48:30 +00003880 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003881 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00003882 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00003883 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00003884 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003885 }
3886
David L. Jonesbe1557a2016-12-21 00:17:49 +00003887 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003888 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003889 if (MemberSpecializationInfo *MSInfo =
3890 RD->getMemberSpecializationInfo()) {
3891 MSInfo->setTemplateSpecializationKind(TSK);
3892 MSInfo->setPointOfInstantiation(POI);
3893 } else {
3894 ClassTemplateSpecializationDecl *Spec =
3895 cast<ClassTemplateSpecializationDecl>(RD);
3896 Spec->setTemplateSpecializationKind(TSK);
3897 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00003898
David L. Jonesbe1557a2016-12-21 00:17:49 +00003899 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00003900 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003901 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00003902 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003903 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00003904 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00003905 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00003906
3907 // FIXME: If we already have a partial specialization set,
3908 // check that it matches.
3909 if (!Spec->getSpecializedTemplateOrPartial()
3910 .is<ClassTemplatePartialSpecializationDecl *>())
3911 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00003912 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003913 }
3914
David L. Jonesbe1557a2016-12-21 00:17:49 +00003915 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003916 RD->setLocation(ReadSourceLocation());
3917 RD->setLocStart(ReadSourceLocation());
3918 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00003919
David L. Jonesbe1557a2016-12-21 00:17:49 +00003920 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003921 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003922 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00003923 // If the declaration already has attributes, we assume that some other
3924 // AST file already loaded them.
3925 if (!D->hasAttrs())
3926 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00003927 }
3928 break;
3929 }
3930
Richard Smithf8134002015-03-10 01:41:22 +00003931 case UPD_CXX_RESOLVED_DTOR_DELETE: {
3932 // Set the 'operator delete' directly to avoid emitting another update
3933 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003934 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00003935 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
3936 // FIXME: Check consistency if we have an old and new operator delete.
3937 if (!First->OperatorDelete)
3938 First->OperatorDelete = Del;
3939 break;
3940 }
3941
Richard Smith564417a2014-03-20 21:47:22 +00003942 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00003943 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00003944 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003945 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00003946
3947 // Update this declaration's exception specification, if needed.
3948 auto *FD = cast<FunctionDecl>(D);
3949 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
3950 // FIXME: If the exception specification is already present, check that it
3951 // matches.
3952 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00003953 FD->setType(Reader.Context.getFunctionType(
3954 FPT->getReturnType(), FPT->getParamTypes(),
3955 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00003956
3957 // When we get to the end of deserializing, see if there are other decls
3958 // that we need to propagate this exception specification onto.
3959 Reader.PendingExceptionSpecUpdates.insert(
3960 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003961 }
Richard Smith564417a2014-03-20 21:47:22 +00003962 break;
3963 }
3964
Richard Smith1fa5d642013-05-11 05:45:24 +00003965 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00003966 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00003967 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00003968 for (auto *Redecl : merged_redecls(D)) {
3969 // FIXME: If the return type is already deduced, check that it matches.
3970 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
3971 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
3972 }
Richard Smith1fa5d642013-05-11 05:45:24 +00003973 break;
3974 }
Eli Friedman276dd182013-09-05 00:02:25 +00003975
3976 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00003977 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003978 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00003979 break;
3980 }
Richard Smith5652c0f2014-03-21 01:48:23 +00003981
3982 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003983 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00003984 break;
3985
3986 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003987 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00003988 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00003989
Alexey Bataev97720002014-11-11 04:05:39 +00003990 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
3991 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003992 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00003993 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00003994
Alex Denisovfde64952015-06-26 05:28:36 +00003995 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003996 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00003997 auto *Exported = cast<NamedDecl>(D);
3998 if (auto *TD = dyn_cast<TagDecl>(Exported))
3999 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004000 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004001 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004002 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4003 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004004 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4005 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004006 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004007 // If Owner is made visible at some later point, make this declaration
4008 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004009 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004010 } else {
4011 // The declaration is now visible.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004012 Exported->Hidden = false;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004013 }
4014 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004015 }
Alex Denisovfde64952015-06-26 05:28:36 +00004016
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004017 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004018 case UPD_ADDED_ATTR_TO_RECORD:
4019 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004020 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004021 assert(Attrs.size() == 1);
4022 D->addAttr(Attrs[0]);
4023 break;
4024 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004025 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004026}