blob: afda1a83c2aac5aa420a023409a1bac265ded1b4 [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 Smithc0ca4c22017-01-26 22:39:55 +0000595 // Read and discard the declaration for which this is a typedef name for
596 // linkage, if it exists. We cannot rely on our type to pull in this decl,
597 // because it might have been merged with a type from another module and
598 // thus might not refer to our version of the declaration.
599 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000600 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000601}
602
603void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000604 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
605 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000606}
607
Richard Smithdda56e42011-04-15 14:24:37 +0000608void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000609 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000610 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000611 // Merged when we merge the template.
612 TD->setDescribedAliasTemplate(Template);
613 else
614 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000615}
616
Richard Smith1d209d02013-05-23 01:49:11 +0000617ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000618 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000619 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000620
David L. Jonesbe1557a2016-12-21 00:17:49 +0000621 TD->IdentifierNamespace = Record.readInt();
622 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000623 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000624 TD->setCompleteDefinition(Record.readInt());
625 TD->setEmbeddedInDeclarator(Record.readInt());
626 TD->setFreeStanding(Record.readInt());
627 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000628 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000629
David L. Jonesbe1557a2016-12-21 00:17:49 +0000630 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000631 case 0:
632 break;
633 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000634 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000635 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000636 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000637 break;
638 }
639 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000640 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000641 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000642 break;
Richard Smith70d58502014-08-30 00:04:23 +0000643 default:
644 llvm_unreachable("unexpected tag info kind");
645 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000646
Richard Smithcd45dbc2014-04-19 03:48:30 +0000647 if (!isa<CXXRecordDecl>(TD))
648 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000649 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000650}
651
Sebastian Redlb3298c32010-08-18 23:56:48 +0000652void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000653 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000654 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000655 ED->setIntegerTypeSourceInfo(TI);
656 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000657 ED->setIntegerType(Record.readType());
658 ED->setPromotionType(Record.readType());
659 ED->setNumPositiveBits(Record.readInt());
660 ED->setNumNegativeBits(Record.readInt());
661 ED->IsScoped = Record.readInt();
662 ED->IsScopedUsingClassTag = Record.readInt();
663 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000664
Richard Smith01a73372013-10-15 22:02:41 +0000665 // If this is a definition subject to the ODR, and we already have a
666 // definition, merge this one into it.
667 if (ED->IsCompleteDefinition &&
668 Reader.getContext().getLangOpts().Modules &&
669 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000670 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
671 if (!OldDef) {
672 // This is the first time we've seen an imported definition. Look for a
673 // local definition before deciding that we are the first definition.
674 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
675 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
676 OldDef = D;
677 break;
678 }
679 }
680 }
681 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000682 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
683 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000684 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000685 } else {
686 OldDef = ED;
687 }
688 }
689
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000690 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000691 TemplateSpecializationKind TSK =
692 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000693 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000694 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
695 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
696 }
Chris Lattner487412d2009-04-27 05:27:42 +0000697}
698
Richard Smith1d209d02013-05-23 01:49:11 +0000699ASTDeclReader::RedeclarableResult
700ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
701 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000702 RD->setHasFlexibleArrayMember(Record.readInt());
703 RD->setAnonymousStructOrUnion(Record.readInt());
704 RD->setHasObjectMember(Record.readInt());
705 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000706 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000707}
708
Sebastian Redlb3298c32010-08-18 23:56:48 +0000709void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000710 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000711 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000712}
713
Sebastian Redlb3298c32010-08-18 23:56:48 +0000714void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000715 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000716 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000717 ECD->setInitExpr(Record.readExpr());
718 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000719 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000720}
721
Sebastian Redlb3298c32010-08-18 23:56:48 +0000722void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000723 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000724 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000725 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000726 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000727 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000728 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000729 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000730 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000731}
732
Sebastian Redlb3298c32010-08-18 23:56:48 +0000733void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000734 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000735 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000736
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000737 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000738 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000739
Douglas Gregorb2585692012-01-04 17:13:46 +0000740 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
741 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000742
David L. Jonesbe1557a2016-12-21 00:17:49 +0000743 FD->SClass = (StorageClass)Record.readInt();
744 FD->IsInline = Record.readInt();
745 FD->IsInlineSpecified = Record.readInt();
746 FD->IsVirtualAsWritten = Record.readInt();
747 FD->IsPure = Record.readInt();
748 FD->HasInheritedPrototype = Record.readInt();
749 FD->HasWrittenPrototype = Record.readInt();
750 FD->IsDeleted = Record.readInt();
751 FD->IsTrivial = Record.readInt();
752 FD->IsDefaulted = Record.readInt();
753 FD->IsExplicitlyDefaulted = Record.readInt();
754 FD->HasImplicitReturnZero = Record.readInt();
755 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000756 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000757 FD->HasSkippedBody = Record.readInt();
758 FD->IsLateTemplateParsed = Record.readInt();
759 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000760 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000761
David L. Jonesbe1557a2016-12-21 00:17:49 +0000762 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000763 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000764 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000765 break;
766 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000767 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000768 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000769 break;
770 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000771 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000772 TemplateSpecializationKind TSK =
773 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000774 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000775 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000776 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000777 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000778 break;
779 }
780 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000781 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000782 TemplateSpecializationKind TSK =
783 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000784
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000785 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000786 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000787 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000788
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000789 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000790 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000791 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000792 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000793 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000794 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000795 TemplArgLocs.reserve(NumTemplateArgLocs);
796 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000797 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000798
799 LAngleLoc = ReadSourceLocation();
800 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000801 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000802
803 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000804
Douglas Gregor4163aca2011-09-09 21:34:22 +0000805 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000806 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000807 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000808 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000809 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000810 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000811 FunctionTemplateSpecializationInfo *FTInfo
812 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
813 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000814 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
815 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000816 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000817 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000818
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000819 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000820 // The template that contains the specializations set. It's not safe to
821 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000822 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000823 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
824 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
825 // FunctionTemplateSpecializationInfo's Profile().
826 // We avoid getASTContext because a decl in the parent hierarchy may
827 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000828 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000829 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000830 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000831 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000832 FunctionTemplateSpecializationInfo *ExistingInfo =
833 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000834 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000835 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
836 else {
837 assert(Reader.getContext().getLangOpts().Modules &&
838 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000839 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000840 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000841 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000842 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000843 }
844 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
845 // Templates.
846 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000847 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000848 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000849 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
850
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000851 // Templates args.
852 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000853 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000854 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000855 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000856 TemplArgs.setLAngleLoc(ReadSourceLocation());
857 TemplArgs.setRAngleLoc(ReadSourceLocation());
858
Douglas Gregor4163aca2011-09-09 21:34:22 +0000859 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000860 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000861 // These are not merged; we don't need to merge redeclarations of dependent
862 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000863 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000864 }
865 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000866
Chris Lattnerca025db2010-05-07 21:43:38 +0000867 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000868 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000869 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000870 Params.reserve(NumParams);
871 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000872 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000873 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000874}
875
Sebastian Redlb3298c32010-08-18 23:56:48 +0000876void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000877 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000878 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000879 // Load the body on-demand. Most clients won't care, because method
880 // definitions rarely show up in headers.
881 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
882 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000883 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
884 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000885 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000886 MD->setInstanceMethod(Record.readInt());
887 MD->setVariadic(Record.readInt());
888 MD->setPropertyAccessor(Record.readInt());
889 MD->setDefined(Record.readInt());
890 MD->IsOverriding = Record.readInt();
891 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000892
David L. Jonesbe1557a2016-12-21 00:17:49 +0000893 MD->IsRedeclaration = Record.readInt();
894 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000895 if (MD->HasRedeclaration)
896 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000897 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000898
David L. Jonesbe1557a2016-12-21 00:17:49 +0000899 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
900 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
901 MD->SetRelatedResultType(Record.readInt());
902 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000903 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
904 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000905 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000906 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000907 Params.reserve(NumParams);
908 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000909 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000910
David L. Jonesbe1557a2016-12-21 00:17:49 +0000911 MD->SelLocsKind = Record.readInt();
912 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000913 SmallVector<SourceLocation, 16> SelLocs;
914 SelLocs.reserve(NumStoredSelLocs);
915 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000916 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000917
918 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000919}
920
Douglas Gregor85f3f952015-07-07 03:57:15 +0000921void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000922 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000923
David L. Jonesbe1557a2016-12-21 00:17:49 +0000924 D->Variance = Record.readInt();
925 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000926 D->VarianceLoc = ReadSourceLocation();
927 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000928}
929
Sebastian Redlb3298c32010-08-18 23:56:48 +0000930void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000931 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000932 CD->setAtStartLoc(ReadSourceLocation());
933 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000934}
935
Douglas Gregor85f3f952015-07-07 03:57:15 +0000936ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000937 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000938 if (numParams == 0)
939 return nullptr;
940
941 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
942 typeParams.reserve(numParams);
943 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000944 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000945 if (!typeParam)
946 return nullptr;
947
948 typeParams.push_back(typeParam);
949 }
950
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000951 SourceLocation lAngleLoc = ReadSourceLocation();
952 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000953
954 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
955 typeParams, rAngleLoc);
956}
957
Manman Renec315f12016-09-09 23:48:27 +0000958void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000959 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000960 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000961 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000962
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000963 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000964 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000965
Manman Renec315f12016-09-09 23:48:27 +0000966 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000967 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000968 SmallVector<ObjCProtocolDecl *, 16> Protocols;
969 Protocols.reserve(NumProtocols);
970 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000971 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000972 SmallVector<SourceLocation, 16> ProtoLocs;
973 ProtoLocs.reserve(NumProtocols);
974 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000975 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +0000976 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
977 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000978
Manman Renec315f12016-09-09 23:48:27 +0000979 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000980 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000981 Protocols.clear();
982 Protocols.reserve(NumProtocols);
983 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000984 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000985 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
986 Reader.getContext());
987}
988
989void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
990 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
991 // FIXME: odr checking?
992}
993
Sebastian Redlb3298c32010-08-18 23:56:48 +0000994void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000995 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +0000996 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000998 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000999
1000 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001001 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001002 // Read the definition.
1003 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001004
David L. Jonesbe1557a2016-12-21 00:17:49 +00001005 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001006 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1007 if (Canon->Data.getPointer()) {
1008 // If we already have a definition, keep the definition invariant and
1009 // merge the data.
1010 MergeDefinitionData(Canon, std::move(ID->data()));
1011 ID->Data = Canon->Data;
1012 } else {
1013 // Set the definition data of the canonical declaration, so other
1014 // redeclarations will see it.
1015 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001016
Manman Renec315f12016-09-09 23:48:27 +00001017 // We will rebuild this list lazily.
1018 ID->setIvarList(nullptr);
1019 }
Craig Toppera13603a2014-05-22 05:54:18 +00001020
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001021 // Note that we have deserialized a definition.
1022 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001023
Douglas Gregor404cdde2012-01-27 01:47:08 +00001024 // Note that we've loaded this Objective-C class.
1025 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001026 } else {
1027 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001028 }
Chris Lattner487412d2009-04-27 05:27:42 +00001029}
1030
Sebastian Redlb3298c32010-08-18 23:56:48 +00001031void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001032 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001033 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001034 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001035 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001036 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001037 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001038}
1039
Sebastian Redlb3298c32010-08-18 23:56:48 +00001040void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
Douglas Gregorda389302012-01-01 21:47:52 +00001041 RedeclarableResult Redecl = VisitRedeclarable(PD);
Chris Lattner487412d2009-04-27 05:27:42 +00001042 VisitObjCContainerDecl(PD);
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001043 mergeRedeclarable(PD, Redecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001044
David L. Jonesbe1557a2016-12-21 00:17:49 +00001045 if (Record.readInt()) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00001046 // Read the definition.
1047 PD->allocateDefinitionData();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001048
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001049 // Set the definition data of the canonical declaration, so other
1050 // redeclarations will see it.
1051 PD->getCanonicalDecl()->Data = PD->Data;
1052
David L. Jonesbe1557a2016-12-21 00:17:49 +00001053 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001054 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1055 ProtoRefs.reserve(NumProtoRefs);
1056 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001057 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001058 SmallVector<SourceLocation, 16> ProtoLocs;
1059 ProtoLocs.reserve(NumProtoRefs);
1060 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001061 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001062 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1063 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001064
Douglas Gregora715bff2012-01-01 19:51:50 +00001065 // Note that we have deserialized a definition.
1066 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001067 } else {
1068 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001069 }
Chris Lattner487412d2009-04-27 05:27:42 +00001070}
1071
Sebastian Redlb3298c32010-08-18 23:56:48 +00001072void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001073 VisitFieldDecl(FD);
1074}
1075
Sebastian Redlb3298c32010-08-18 23:56:48 +00001076void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001077 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001078 CD->setCategoryNameLoc(ReadSourceLocation());
1079 CD->setIvarLBraceLoc(ReadSourceLocation());
1080 CD->setIvarRBraceLoc(ReadSourceLocation());
1081
Douglas Gregor404cdde2012-01-27 01:47:08 +00001082 // Note that this category has been deserialized. We do this before
1083 // deserializing the interface declaration, so that it will consider this
1084 /// category.
1085 Reader.CategoriesDeserialized.insert(CD);
1086
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001087 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001088 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001089 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001090 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001091 ProtoRefs.reserve(NumProtoRefs);
1092 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001093 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001094 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001095 ProtoLocs.reserve(NumProtoRefs);
1096 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001097 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001098 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001099 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001100}
1101
Sebastian Redlb3298c32010-08-18 23:56:48 +00001102void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001103 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001105}
1106
Sebastian Redlb3298c32010-08-18 23:56:48 +00001107void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001108 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001109 D->setAtLoc(ReadSourceLocation());
1110 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001111 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001112 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001113 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001114 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001115 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001116 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001117 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001118 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001119 (ObjCPropertyDecl::PropertyControl)Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001120 D->setGetterName(Record.readDeclarationName().getObjCSelector());
1121 D->setSetterName(Record.readDeclarationName().getObjCSelector());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001122 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1123 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1124 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001125}
1126
Sebastian Redlb3298c32010-08-18 23:56:48 +00001127void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001128 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001129 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001130}
1131
Sebastian Redlb3298c32010-08-18 23:56:48 +00001132void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001133 VisitObjCImplDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001134 D->setIdentifier(Record.getIdentifierInfo());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001135 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001136}
1137
Sebastian Redlb3298c32010-08-18 23:56:48 +00001138void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001139 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001140 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1141 D->SuperLoc = ReadSourceLocation();
1142 D->setIvarLBraceLoc(ReadSourceLocation());
1143 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001144 D->setHasNonZeroConstructors(Record.readInt());
1145 D->setHasDestructors(Record.readInt());
1146 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001147 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001148 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001149}
1150
Sebastian Redlb3298c32010-08-18 23:56:48 +00001151void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001152 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001153 D->setAtLoc(ReadSourceLocation());
1154 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1155 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1156 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001157 D->setGetterCXXConstructor(Record.readExpr());
1158 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001159}
1160
Sebastian Redlb3298c32010-08-18 23:56:48 +00001161void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001162 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001163 FD->Mutable = Record.readInt();
1164 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001165 FD->InitStorage.setInt(
1166 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1167 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001168 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001169 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001170 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001171 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001172 }
Richard Smith2b013182012-06-10 03:12:00 +00001173 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001174 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001175 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001176 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001177 }
Richard Smith0b87e072013-10-07 08:02:11 +00001178 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001179}
1180
John McCall5e77d762013-04-16 07:28:30 +00001181void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1182 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001183 PD->GetterId = Record.getIdentifierInfo();
1184 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001185}
1186
Francois Pichet783dd6e2010-11-21 06:08:52 +00001187void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1188 VisitValueDecl(FD);
1189
David L. Jonesbe1557a2016-12-21 00:17:49 +00001190 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001191 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001192 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001193
1194 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001195 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001196
1197 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001198}
1199
Larisse Voufo39a1e502013-08-06 01:03:05 +00001200ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001201 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001202 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001203
David L. Jonesbe1557a2016-12-21 00:17:49 +00001204 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1205 VD->VarDeclBits.TSCSpec = Record.readInt();
1206 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001207 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001208 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1209 Record.readInt();
1210 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1211 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1212 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1213 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1214 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1215 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1216 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1217 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1218 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001219 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001220 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001221 VD->setCachedLinkage(VarLinkage);
1222
1223 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001224 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001225 VD->getLexicalDeclContext()->isFunctionOrMethod())
1226 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001227
David L. Jonesbe1557a2016-12-21 00:17:49 +00001228 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001229 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001230 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001231 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1232 Eval->CheckedICE = true;
1233 Eval->IsICE = Val == 3;
1234 }
1235 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001236
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001237 enum VarKind {
1238 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1239 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001240 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001241 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001242 // Only true variables (not parameters or implicit parameters) can be
1243 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001244 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1245 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001246 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001247 break;
1248 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001249 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001250 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001251 break;
1252 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001253 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001254 TemplateSpecializationKind TSK =
1255 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001256 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001257 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001258 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001259 break;
1260 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001261 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001262
1263 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001264}
1265
Sebastian Redlb3298c32010-08-18 23:56:48 +00001266void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001267 VisitVarDecl(PD);
1268}
1269
Sebastian Redlb3298c32010-08-18 23:56:48 +00001270void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001271 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001272 unsigned isObjCMethodParam = Record.readInt();
1273 unsigned scopeDepth = Record.readInt();
1274 unsigned scopeIndex = Record.readInt();
1275 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001276 if (isObjCMethodParam) {
1277 assert(scopeDepth == 0);
1278 PD->setObjCMethodScopeInfo(scopeIndex);
1279 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1280 } else {
1281 PD->setScopeInfo(scopeDepth, scopeIndex);
1282 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001283 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1284 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1285 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001286 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001287
1288 // FIXME: If this is a redeclaration of a function from another module, handle
1289 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001290}
1291
Richard Smith7b76d812016-08-12 02:21:25 +00001292void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1293 VisitVarDecl(DD);
1294 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1295 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001296 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001297}
1298
1299void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1300 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001301 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001302}
1303
Sebastian Redlb3298c32010-08-18 23:56:48 +00001304void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001305 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001306 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001307 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001308}
1309
Sebastian Redlb3298c32010-08-18 23:56:48 +00001310void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001311 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001312 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001313 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001314 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001315 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001316 Params.reserve(NumParams);
1317 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001318 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001319 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001320
David L. Jonesbe1557a2016-12-21 00:17:49 +00001321 BD->setIsVariadic(Record.readInt());
1322 BD->setBlockMissingReturnType(Record.readInt());
1323 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001324
David L. Jonesbe1557a2016-12-21 00:17:49 +00001325 bool capturesCXXThis = Record.readInt();
1326 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001327 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001328 captures.reserve(numCaptures);
1329 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001330 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001331 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001332 bool byRef = (flags & 1);
1333 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001334 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001335
1336 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1337 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001338 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001339}
1340
Ben Langmuirce914fc2013-05-03 19:20:19 +00001341void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1342 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001343 unsigned ContextParamPos = Record.readInt();
1344 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001345 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001346 for (unsigned I = 0; I < CD->NumParams; ++I) {
1347 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001348 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001349 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001350 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001351 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001352}
1353
Sebastian Redlb3298c32010-08-18 23:56:48 +00001354void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001355 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001356 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001357 D->setExternLoc(ReadSourceLocation());
1358 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001359}
1360
Richard Smith8df390f2016-09-08 23:14:54 +00001361void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1362 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001363 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001364}
1365
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001366void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1367 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001368 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001369}
1370
Sebastian Redlb3298c32010-08-18 23:56:48 +00001371void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001372 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001373 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001374 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001375 D->LocStart = ReadSourceLocation();
1376 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001377
Richard Smith15e32fd2015-03-17 02:23:11 +00001378 // Defer loading the anonymous namespace until we've finished merging
1379 // this namespace; loading it might load a later declaration of the
1380 // same namespace, and we have an invariant that older declarations
1381 // get merged before newer ones try to merge.
1382 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001383 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001384 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001385 } else {
1386 // Link this namespace back to the first declaration, which has already
1387 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001388 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001389 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001390
1391 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001392
1393 if (AnonNamespace) {
1394 // Each module has its own anonymous namespace, which is disjoint from
1395 // any other module's anonymous namespaces, so don't attach the anonymous
1396 // namespace at all.
1397 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001398 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001399 D->setAnonymousNamespace(Anon);
1400 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001401}
1402
Sebastian Redlb3298c32010-08-18 23:56:48 +00001403void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001404 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001405 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001406 D->NamespaceLoc = ReadSourceLocation();
1407 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001408 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001409 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001410 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001411}
1412
Sebastian Redlb3298c32010-08-18 23:56:48 +00001413void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001414 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001415 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001416 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001417 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1418 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001419 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001420 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001421 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001422 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001423}
1424
Richard Smith151c4562016-12-20 21:35:28 +00001425void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1426 VisitNamedDecl(D);
1427 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1428 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1429 for (unsigned I = 0; I != D->NumExpansions; ++I)
1430 Expansions[I] = ReadDeclAs<NamedDecl>();
1431 mergeMergeable(D);
1432}
1433
Sebastian Redlb3298c32010-08-18 23:56:48 +00001434void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001435 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001436 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001437 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1438 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1439 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001440 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001441 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001442 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001443}
1444
Richard Smith5179eb72016-06-28 19:03:57 +00001445void ASTDeclReader::VisitConstructorUsingShadowDecl(
1446 ConstructorUsingShadowDecl *D) {
1447 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001448 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1449 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001450 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001451}
1452
Sebastian Redlb3298c32010-08-18 23:56:48 +00001453void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001454 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001455 D->UsingLoc = ReadSourceLocation();
1456 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001457 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001458 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1459 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001460}
1461
Sebastian Redlb3298c32010-08-18 23:56:48 +00001462void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001463 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001464 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001465 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001466 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001467 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001468 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001469}
1470
Sebastian Redlb3298c32010-08-18 23:56:48 +00001471void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001472 UnresolvedUsingTypenameDecl *D) {
1473 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001474 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001475 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001476 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001477 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001478}
1479
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001480void ASTDeclReader::ReadCXXDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001481 struct CXXRecordDecl::DefinitionData &Data) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001482 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001483 Data.UserDeclaredConstructor = Record.readInt();
1484 Data.UserDeclaredSpecialMembers = Record.readInt();
1485 Data.Aggregate = Record.readInt();
1486 Data.PlainOldData = Record.readInt();
1487 Data.Empty = Record.readInt();
1488 Data.Polymorphic = Record.readInt();
1489 Data.Abstract = Record.readInt();
1490 Data.IsStandardLayout = Record.readInt();
1491 Data.HasNoNonEmptyBases = Record.readInt();
1492 Data.HasPrivateFields = Record.readInt();
1493 Data.HasProtectedFields = Record.readInt();
1494 Data.HasPublicFields = Record.readInt();
1495 Data.HasMutableFields = Record.readInt();
1496 Data.HasVariantMembers = Record.readInt();
1497 Data.HasOnlyCMembers = Record.readInt();
1498 Data.HasInClassInitializer = Record.readInt();
1499 Data.HasUninitializedReferenceMember = Record.readInt();
1500 Data.HasUninitializedFields = Record.readInt();
1501 Data.HasInheritedConstructor = Record.readInt();
1502 Data.HasInheritedAssignment = Record.readInt();
1503 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1504 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1505 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1506 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1507 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1508 Data.DefaultedDestructorIsDeleted = Record.readInt();
1509 Data.HasTrivialSpecialMembers = Record.readInt();
1510 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1511 Data.HasIrrelevantDestructor = Record.readInt();
1512 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1513 Data.HasDefaultedDefaultConstructor = Record.readInt();
1514 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1515 Data.HasConstexprDefaultConstructor = Record.readInt();
1516 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1517 Data.ComputedVisibleConversions = Record.readInt();
1518 Data.UserProvidedDefaultConstructor = Record.readInt();
1519 Data.DeclaredSpecialMembers = Record.readInt();
1520 Data.ImplicitCopyConstructorHasConstParam = Record.readInt();
1521 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1522 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1523 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Anders Carlsson703d6e62011-01-22 18:11:02 +00001524
David L. Jonesbe1557a2016-12-21 00:17:49 +00001525 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001526 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001527 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001528 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001529 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001530 Data.VBases = ReadGlobalOffset();
1531
David L. Jonesb6a8f022016-12-21 04:34:52 +00001532 Record.readUnresolvedSet(Data.Conversions);
1533 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001534 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001535 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001536
Douglas Gregor99ae8062012-02-14 17:54:36 +00001537 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001538 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001539 CXXRecordDecl::LambdaDefinitionData &Lambda
1540 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001541 Lambda.Dependent = Record.readInt();
1542 Lambda.IsGenericLambda = Record.readInt();
1543 Lambda.CaptureDefault = Record.readInt();
1544 Lambda.NumCaptures = Record.readInt();
1545 Lambda.NumExplicitCaptures = Record.readInt();
1546 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001547 Lambda.ContextDecl = ReadDeclID();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001548 Lambda.Captures
1549 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1550 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001551 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001552 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001553 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001554 bool IsImplicit = Record.readInt();
1555 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001556 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001557 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001558 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001559 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001560 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001561 break;
1562 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001563 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001564 VarDecl *Var = ReadDeclAs<VarDecl>();
1565 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001566 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1567 break;
1568 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001569 }
1570 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001571}
1572
Richard Smithcd45dbc2014-04-19 03:48:30 +00001573void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001574 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001575 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001576 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001577 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001578
Richard Smith02793752015-03-27 21:16:39 +00001579 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001580 // Track that we merged the definitions.
1581 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1582 DD.Definition));
1583 Reader.PendingDefinitions.erase(MergeDD.Definition);
1584 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001585 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001586 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1587 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001588 }
1589
Richard Smith2a9e5c52015-02-03 03:32:14 +00001590 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1591 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1592 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001593 // We faked up this definition data because we found a class for which we'd
1594 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001595 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001596 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001597
1598 // Don't change which declaration is the definition; that is required
1599 // to be invariant once we select it.
1600 auto *Def = DD.Definition;
1601 DD = std::move(MergeDD);
1602 DD.Definition = Def;
1603 return;
1604 }
1605
Richard Smithcd45dbc2014-04-19 03:48:30 +00001606 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001607 bool DetectedOdrViolation = false;
1608#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1609#define MATCH_FIELD(Field) \
1610 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1611 OR_FIELD(Field)
1612 MATCH_FIELD(UserDeclaredConstructor)
1613 MATCH_FIELD(UserDeclaredSpecialMembers)
1614 MATCH_FIELD(Aggregate)
1615 MATCH_FIELD(PlainOldData)
1616 MATCH_FIELD(Empty)
1617 MATCH_FIELD(Polymorphic)
1618 MATCH_FIELD(Abstract)
1619 MATCH_FIELD(IsStandardLayout)
1620 MATCH_FIELD(HasNoNonEmptyBases)
1621 MATCH_FIELD(HasPrivateFields)
1622 MATCH_FIELD(HasProtectedFields)
1623 MATCH_FIELD(HasPublicFields)
1624 MATCH_FIELD(HasMutableFields)
1625 MATCH_FIELD(HasVariantMembers)
1626 MATCH_FIELD(HasOnlyCMembers)
1627 MATCH_FIELD(HasInClassInitializer)
1628 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001629 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001630 MATCH_FIELD(HasInheritedConstructor)
1631 MATCH_FIELD(HasInheritedAssignment)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001632 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1633 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1634 MATCH_FIELD(NeedOverloadResolutionForDestructor)
1635 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1636 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1637 MATCH_FIELD(DefaultedDestructorIsDeleted)
1638 OR_FIELD(HasTrivialSpecialMembers)
1639 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1640 MATCH_FIELD(HasIrrelevantDestructor)
1641 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001642 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001643 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1644 OR_FIELD(HasConstexprDefaultConstructor)
1645 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1646 // ComputedVisibleConversions is handled below.
1647 MATCH_FIELD(UserProvidedDefaultConstructor)
1648 OR_FIELD(DeclaredSpecialMembers)
1649 MATCH_FIELD(ImplicitCopyConstructorHasConstParam)
1650 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1651 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1652 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1653 MATCH_FIELD(IsLambda)
1654#undef OR_FIELD
1655#undef MATCH_FIELD
1656
1657 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1658 DetectedOdrViolation = true;
1659 // FIXME: Issue a diagnostic if the base classes don't match when we come
1660 // to lazily load them.
1661
1662 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1663 // match when we come to lazily load them.
1664 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1665 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1666 DD.ComputedVisibleConversions = true;
1667 }
1668
1669 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1670 // lazily load it.
1671
1672 if (DD.IsLambda) {
1673 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1674 // when they occur within the body of a function template specialization).
1675 }
1676
1677 if (DetectedOdrViolation)
1678 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1679}
1680
Richard Smith2a9e5c52015-02-03 03:32:14 +00001681void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001682 struct CXXRecordDecl::DefinitionData *DD;
1683 ASTContext &C = Reader.getContext();
1684
1685 // Determine whether this is a lambda closure type, so that we can
1686 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001688 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001689 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001690 LCD_None);
1691 else
1692 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1693
David L. Jonesbe1557a2016-12-21 00:17:49 +00001694 ReadCXXDefinitionData(*DD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001695
Richard Smith5156c382015-01-22 01:41:56 +00001696 // We might already have a definition for this record. This can happen either
1697 // because we're reading an update record, or because we've already done some
1698 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001699 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001700 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001701 MergeDefinitionData(Canon, std::move(*DD));
1702 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001703 return;
1704 }
1705
Richard Smith97100e32015-06-20 00:22:34 +00001706 // Mark this declaration as being a definition.
1707 D->IsCompleteDefinition = true;
1708 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001709
Richard Smith97100e32015-06-20 00:22:34 +00001710 // If this is not the first declaration or is an update record, we can have
1711 // other redeclarations already. Make a note that we need to propagate the
1712 // DefinitionData pointer onto them.
1713 if (Update || Canon != D) {
1714 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001715 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001716 }
1717}
1718
Richard Smith1d209d02013-05-23 01:49:11 +00001719ASTDeclReader::RedeclarableResult
1720ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1721 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001722
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001723 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001724
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001725 enum CXXRecKind {
1726 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1727 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001728 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001729 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001730 // Merged when we merge the folding set entry in the primary template.
1731 if (!isa<ClassTemplateSpecializationDecl>(D))
1732 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001733 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001734 case CXXRecTemplate: {
1735 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001736 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001737 D->TemplateOrInstantiation = Template;
1738 if (!Template->getTemplatedDecl()) {
1739 // We've not actually loaded the ClassTemplateDecl yet, because we're
1740 // currently being loaded as its pattern. Rely on it to set up our
1741 // TypeForDecl (see VisitClassTemplateDecl).
1742 //
1743 // Beware: we do not yet know our canonical declaration, and may still
1744 // get merged once the surrounding class template has got off the ground.
1745 TypeIDForTypeDecl = 0;
1746 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001747 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001748 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001749 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001750 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001751 TemplateSpecializationKind TSK =
1752 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001753 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001754 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1755 MSI->setPointOfInstantiation(POI);
1756 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001757 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001758 break;
1759 }
1760 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001761
David L. Jonesbe1557a2016-12-21 00:17:49 +00001762 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001763 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001764 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001765 else
1766 // Propagate DefinitionData pointer from the canonical declaration.
1767 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1768
Richard Smith676c4042013-08-29 23:59:27 +00001769 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001770 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001771 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001772 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001773 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001774 // FIXME: This is wrong for the ARM ABI, where some other module may have
1775 // made this function no longer be a key function. We need an update
1776 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001777 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001778 }
Richard Smith1d209d02013-05-23 01:49:11 +00001779
1780 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001781}
1782
Sebastian Redlb3298c32010-08-18 23:56:48 +00001783void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001784 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001785
David L. Jonesbe1557a2016-12-21 00:17:49 +00001786 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001787 if (D->isCanonicalDecl()) {
1788 while (NumOverridenMethods--) {
1789 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1790 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001791 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001792 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1793 }
1794 } else {
1795 // We don't care about which declarations this used to override; we get
1796 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001797 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001798 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001799}
1800
Sebastian Redlb3298c32010-08-18 23:56:48 +00001801void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001802 // We need the inherited constructor information to merge the declaration,
1803 // so we have to read it before we call VisitCXXMethodDecl.
1804 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001805 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1806 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001807 *D->getTrailingObjects<InheritedConstructor>() =
1808 InheritedConstructor(Shadow, Ctor);
1809 }
1810
Chris Lattnerca025db2010-05-07 21:43:38 +00001811 VisitCXXMethodDecl(D);
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00001812
David L. Jonesbe1557a2016-12-21 00:17:49 +00001813 D->IsExplicitSpecified = Record.readInt();
Chris Lattnerca025db2010-05-07 21:43:38 +00001814}
1815
Sebastian Redlb3298c32010-08-18 23:56:48 +00001816void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001817 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001818
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001819 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001820 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1821 // FIXME: Check consistency if we have an old and new operator delete.
1822 if (!Canon->OperatorDelete)
1823 Canon->OperatorDelete = OperatorDelete;
1824 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001825}
1826
Sebastian Redlb3298c32010-08-18 23:56:48 +00001827void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001828 VisitCXXMethodDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001829 D->IsExplicitSpecified = Record.readInt();
Chris Lattnerca025db2010-05-07 21:43:38 +00001830}
1831
Douglas Gregorba345522011-12-02 23:23:56 +00001832void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1833 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001834 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001835 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001836 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001837 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001838 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001839 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001840}
1841
Sebastian Redlb3298c32010-08-18 23:56:48 +00001842void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001843 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001844 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001845}
1846
Sebastian Redlb3298c32010-08-18 23:56:48 +00001847void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001848 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001849 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001850 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001851 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001852 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001853 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001854 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001855 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001856 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001857 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001858 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001859}
1860
Sebastian Redlb3298c32010-08-18 23:56:48 +00001861void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001862 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001863 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001864 D->NumParams = NumParams;
1865 D->Params = new TemplateParameterList*[NumParams];
1866 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001867 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001868 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001869 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001870 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001871 D->Friend = GetTypeSourceInfo();
1872 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001873}
1874
Richard Smithf17fdbd2014-04-24 02:25:27 +00001875DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001876 VisitNamedDecl(D);
1877
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001878 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001879 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001880 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001881 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001882
Richard Smithf17fdbd2014-04-24 02:25:27 +00001883 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001884}
1885
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001886ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001887ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001888 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001889
Douglas Gregor68444de2012-01-14 15:13:49 +00001890 // Make sure we've allocated the Common pointer first. We do this before
1891 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1892 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1893 if (!CanonD->Common) {
1894 CanonD->Common = CanonD->newCommon(Reader.getContext());
1895 Reader.PendingDefinitions.insert(CanonD);
1896 }
1897 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001898
Douglas Gregor68444de2012-01-14 15:13:49 +00001899 // If this is the first declaration of the template, fill in the information
1900 // for the 'common' pointer.
1901 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001902 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001903 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001904 assert(RTD->getKind() == D->getKind() &&
1905 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001906 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001907 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001908 D->setMemberSpecialization();
1909 }
1910 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001911
Richard Smithf17fdbd2014-04-24 02:25:27 +00001912 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001913 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001914
Richard Smithf17fdbd2014-04-24 02:25:27 +00001915 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001916
Richard Smithd46d6de2013-10-13 23:50:45 +00001917 // If we merged the template with a prior declaration chain, merge the common
1918 // pointer.
1919 // FIXME: Actually merge here, don't just overwrite.
1920 D->Common = D->getCanonicalDecl()->Common;
1921
Douglas Gregor68444de2012-01-14 15:13:49 +00001922 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001923}
1924
Richard Smith0b884372015-02-27 00:25:58 +00001925static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1926 SmallVectorImpl<DeclID> &IDs) {
1927 assert(!IDs.empty() && "no IDs to add to list");
Richard Smith36befce2015-02-28 01:45:19 +00001928 if (Old) {
1929 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1930 std::sort(IDs.begin(), IDs.end());
1931 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1932 }
Richard Smith0b884372015-02-27 00:25:58 +00001933
Richard Smith36befce2015-02-28 01:45:19 +00001934 auto *Result = new (Context) DeclID[1 + IDs.size()];
1935 *Result = IDs.size();
1936 std::copy(IDs.begin(), IDs.end(), Result + 1);
Richard Smith0b884372015-02-27 00:25:58 +00001937 return Result;
1938}
1939
Sebastian Redlb3298c32010-08-18 23:56:48 +00001940void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001941 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001942
Douglas Gregor68444de2012-01-14 15:13:49 +00001943 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001944 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1945 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001946 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001947 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001948
Richard Smith0b884372015-02-27 00:25:58 +00001949 if (!SpecIDs.empty()) {
1950 auto *CommonPtr = D->getCommonPtr();
1951 CommonPtr->LazySpecializations = newDeclIDList(
1952 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001953 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001954 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00001955
1956 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1957 // We were loaded before our templated declaration was. We've not set up
1958 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1959 // it now.
1960 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00001961 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00001962 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001963}
1964
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001965void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
1966 llvm_unreachable("BuiltinTemplates are not serialized");
1967}
1968
Larisse Voufo30616382013-08-23 22:21:36 +00001969/// TODO: Unify with ClassTemplateDecl version?
1970/// May require unifying ClassTemplateDecl and
1971/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00001972void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
1973 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
1974
1975 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001976 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00001977 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001978 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001979 ReadDeclIDList(SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001980
Richard Smith0b884372015-02-27 00:25:58 +00001981 if (!SpecIDs.empty()) {
1982 auto *CommonPtr = D->getCommonPtr();
1983 CommonPtr->LazySpecializations = newDeclIDList(
1984 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001985 }
1986 }
1987}
1988
Richard Smith1d209d02013-05-23 01:49:11 +00001989ASTDeclReader::RedeclarableResult
1990ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
1991 ClassTemplateSpecializationDecl *D) {
1992 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001993
Douglas Gregor4163aca2011-09-09 21:34:22 +00001994 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001995 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001996 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00001997 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001998 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001999 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002000 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002001 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002002 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002003 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2004 = new (C) ClassTemplateSpecializationDecl::
2005 SpecializedPartialSpecialization();
2006 PS->PartialSpecialization
2007 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2008 PS->TemplateArgs = ArgList;
2009 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002010 }
2011 }
2012
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002013 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002014 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002015 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002016 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002017 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002018
David L. Jonesbe1557a2016-12-21 00:17:49 +00002019 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002020 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002021 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002022 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002023 // Set this as, or find, the canonical declaration for this specialization
2024 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002025 if (ClassTemplatePartialSpecializationDecl *Partial =
2026 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002027 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002028 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002029 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002030 CanonSpec =
2031 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2032 }
2033 // If there was already a canonical specialization, merge into it.
2034 if (CanonSpec != D) {
2035 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2036
2037 // This declaration might be a definition. Merge with any existing
2038 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002039 if (auto *DDD = D->DefinitionData) {
2040 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002041 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002042 else
Richard Smith053f6c62014-05-16 23:01:30 +00002043 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002044 }
Richard Smith547864d2014-07-11 18:22:58 +00002045 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002046 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002047 }
2048 }
Richard Smith1d209d02013-05-23 01:49:11 +00002049
Richard Smithd55889a2013-09-09 16:55:27 +00002050 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002051 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002052 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2053 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2054 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002055 ExplicitInfo->ExternLoc = ReadSourceLocation();
2056 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002057 D->ExplicitInfo = ExplicitInfo;
2058 }
2059
Richard Smith1d209d02013-05-23 01:49:11 +00002060 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002061}
2062
Sebastian Redlb3298c32010-08-18 23:56:48 +00002063void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002064 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002065 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002066
David L. Jonesb6a8f022016-12-21 04:34:52 +00002067 D->TemplateParams = Record.readTemplateParameterList();
2068 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002069
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002070 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002071 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002072 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002073 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002074 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002075 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002076}
2077
Sebastian Redlb7448632011-08-31 13:59:56 +00002078void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2079 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002080 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002081 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002082}
2083
Sebastian Redlb3298c32010-08-18 23:56:48 +00002084void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002085 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002086
Douglas Gregor68444de2012-01-14 15:13:49 +00002087 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002088 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002089 SmallVector<serialization::DeclID, 32> SpecIDs;
2090 ReadDeclIDList(SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002091
Richard Smith0b884372015-02-27 00:25:58 +00002092 if (!SpecIDs.empty()) {
2093 auto *CommonPtr = D->getCommonPtr();
2094 CommonPtr->LazySpecializations = newDeclIDList(
2095 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
Richard Smithfeb3e1a2013-06-28 04:37:53 +00002096 }
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002097 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002098}
2099
Larisse Voufo30616382013-08-23 22:21:36 +00002100/// TODO: Unify with ClassTemplateSpecializationDecl version?
2101/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2102/// VarTemplate(Partial)SpecializationDecl with a new data
2103/// structure Template(Partial)SpecializationDecl, and
2104/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002105ASTDeclReader::RedeclarableResult
2106ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2107 VarTemplateSpecializationDecl *D) {
2108 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2109
2110 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002111 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002112 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2113 D->SpecializedTemplate = VTD;
2114 } else {
2115 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002116 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002117 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002118 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002119 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2120 new (C)
2121 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2122 PS->PartialSpecialization =
2123 cast<VarTemplatePartialSpecializationDecl>(InstD);
2124 PS->TemplateArgs = ArgList;
2125 D->SpecializedTemplate = PS;
2126 }
2127 }
2128
2129 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002130 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002131 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2132 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2133 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002134 ExplicitInfo->ExternLoc = ReadSourceLocation();
2135 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002136 D->ExplicitInfo = ExplicitInfo;
2137 }
2138
2139 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002140 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002141 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002142 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002143 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002144
David L. Jonesbe1557a2016-12-21 00:17:49 +00002145 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002146 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002147 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002148 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002149 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002150 if (VarTemplatePartialSpecializationDecl *Partial =
2151 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002152 CanonPattern->getCommonPtr()->PartialSpecializations
2153 .GetOrInsertNode(Partial);
2154 } else {
2155 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2156 }
2157 }
2158 }
2159
2160 return Redecl;
2161}
2162
Larisse Voufo30616382013-08-23 22:21:36 +00002163/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2164/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2165/// VarTemplate(Partial)SpecializationDecl with a new data
2166/// structure Template(Partial)SpecializationDecl, and
2167/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002168void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2169 VarTemplatePartialSpecializationDecl *D) {
2170 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2171
David L. Jonesb6a8f022016-12-21 04:34:52 +00002172 D->TemplateParams = Record.readTemplateParameterList();
2173 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002174
2175 // These are read/set from/to the first declaration.
2176 if (ThisDeclID == Redecl.getFirstID()) {
2177 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002178 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002179 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002180 }
2181}
2182
Sebastian Redlb3298c32010-08-18 23:56:48 +00002183void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002184 VisitTypeDecl(D);
2185
David L. Jonesbe1557a2016-12-21 00:17:49 +00002186 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002187
David L. Jonesbe1557a2016-12-21 00:17:49 +00002188 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002189 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002190}
2191
Sebastian Redlb3298c32010-08-18 23:56:48 +00002192void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002193 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002194 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002195 D->setDepth(Record.readInt());
2196 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002197 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002198 auto TypesAndInfos =
2199 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002200 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002201 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002202 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002203 }
2204 } else {
2205 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002206 D->ParameterPack = Record.readInt();
2207 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002208 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002209 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002210}
2211
Sebastian Redlb3298c32010-08-18 23:56:48 +00002212void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002213 VisitTemplateDecl(D);
2214 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002215 D->setDepth(Record.readInt());
2216 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002217 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002218 TemplateParameterList **Data =
2219 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002220 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2221 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002222 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002223 } else {
2224 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002225 D->ParameterPack = Record.readInt();
2226 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002227 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002228 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002229 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002230}
2231
Richard Smith3f1b5d02011-05-05 21:57:07 +00002232void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2233 VisitRedeclarableTemplateDecl(D);
2234}
2235
Sebastian Redlb3298c32010-08-18 23:56:48 +00002236void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002237 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002238 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002239 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002240 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002241 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002242}
2243
Michael Han84324352013-02-22 17:15:32 +00002244void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2245 VisitDecl(D);
2246}
2247
Mike Stump11289f42009-09-09 15:08:12 +00002248std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002249ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002250 uint64_t LexicalOffset = ReadLocalOffset();
2251 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002252 return std::make_pair(LexicalOffset, VisibleOffset);
2253}
2254
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002255template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002256ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002257ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002258 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002259 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002260
Richard Smith5fc18a92015-07-12 23:43:21 +00002261 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002262 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002263
Richard Smithd61d4ac2015-08-22 20:13:39 +00002264 uint64_t RedeclOffset = 0;
2265
Douglas Gregor358cd442012-01-15 16:58:34 +00002266 // 0 indicates that this declaration was the only declaration of its entity,
2267 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002268 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002269 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002270 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002271 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002272 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002273 // This declaration was the first local declaration, but may have imported
2274 // other declarations.
2275 IsKeyDecl = N == 1;
2276 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002277
Richard Smithfe620d22015-03-05 23:24:12 +00002278 // We have some declarations that must be before us in our redeclaration
2279 // chain. Read them now, and remember that we ought to merge with one of
2280 // them.
2281 // FIXME: Provide a known merge target to the second and subsequent such
2282 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002283 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002284 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002285
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002286 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002287 } else {
2288 // This declaration was not the first local declaration. Read the first
2289 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002290 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002291 }
2292
Douglas Gregor358cd442012-01-15 16:58:34 +00002293 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2294 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002295 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2296 // We temporarily set the first (canonical) declaration as the previous one
2297 // which is the one that matters and mark the real previous DeclID to be
2298 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002299 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002300 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002301 }
Richard Smithd8a83712015-08-22 01:47:18 +00002302
Richard Smithd8a83712015-08-22 01:47:18 +00002303 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002304
2305 // Note that we need to load local redeclarations of this decl and build a
2306 // decl chain for them. This must happen *after* we perform the preloading
2307 // above; this ensures that the redeclaration chain is built in the correct
2308 // order.
2309 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002310 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002311
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002312 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002313}
2314
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002315/// \brief Attempts to merge the given declaration (D) with another declaration
2316/// of the same entity.
2317template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002318void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002319 RedeclarableResult &Redecl,
2320 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002321 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002322 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002323 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002324
Richard Smith202850a2015-03-10 02:57:50 +00002325 // If we're not the canonical declaration, we don't need to merge.
2326 if (!DBase->isFirstDecl())
2327 return;
2328
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002329 T *D = static_cast<T*>(DBase);
2330
Richard Smith5a4737c2015-02-06 02:42:59 +00002331 if (auto *Existing = Redecl.getKnownMergeTarget())
2332 // We already know of an existing declaration we should merge with.
2333 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2334 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002335 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002336 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2337}
2338
2339/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2340/// We use this to put code in a template that will only be valid for certain
2341/// instantiations.
2342template<typename T> static T assert_cast(T t) { return t; }
2343template<typename T> static T assert_cast(...) {
2344 llvm_unreachable("bad assert_cast");
2345}
2346
2347/// \brief Merge together the pattern declarations from two template
2348/// declarations.
2349void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2350 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002351 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002352 auto *DPattern = D->getTemplatedDecl();
2353 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002354 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2355 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002356 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002357
Richard Smith547864d2014-07-11 18:22:58 +00002358 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2359 // Merge with any existing definition.
2360 // FIXME: This is duplicated in several places. Refactor.
2361 auto *ExistingClass =
2362 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002363 if (auto *DDD = DClass->DefinitionData) {
2364 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002365 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002366 } else {
2367 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002368 // We may have skipped this before because we thought that DClass
2369 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002370 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002371 }
2372 }
2373 DClass->DefinitionData = ExistingClass->DefinitionData;
2374
Richard Smithf17fdbd2014-04-24 02:25:27 +00002375 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2376 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002377 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002378 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2379 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2380 Result);
2381 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2382 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002383 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2384 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2385 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002386 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002387}
2388
2389/// \brief Attempts to merge the given declaration (D) with another declaration
2390/// of the same entity.
2391template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002392void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2393 RedeclarableResult &Redecl,
2394 DeclID TemplatePatternID) {
2395 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002396 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002397 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002398 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002399 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2400 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002401
Richard Smithd55889a2013-09-09 16:55:27 +00002402 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002403 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002404 // appropriate canonical declaration.
2405 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002406 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002407 ExistingCanon->Used |= D->Used;
2408 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002409
2410 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002411 // We cannot have loaded any redeclarations of this declaration yet, so
2412 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002413 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002414 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002415 assert_cast<NamespaceDecl*>(ExistingCanon));
2416
2417 // When we merge a template, merge its pattern.
2418 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2419 mergeTemplatePattern(
2420 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002421 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002422
Richard Smith5fc18a92015-07-12 23:43:21 +00002423 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002424 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002425 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002426 }
2427}
2428
Richard Smith0b87e072013-10-07 08:02:11 +00002429/// \brief Attempts to merge the given declaration (D) with another declaration
2430/// of the same entity, for the case where the entity is not actually
2431/// redeclarable. This happens, for instance, when merging the fields of
2432/// identical class definitions from two different modules.
2433template<typename T>
2434void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2435 // If modules are not available, there is no reason to perform this merge.
2436 if (!Reader.getContext().getLangOpts().Modules)
2437 return;
2438
Richard Smith01a73372013-10-15 22:02:41 +00002439 // ODR-based merging is only performed in C++. In C, identically-named things
2440 // in different translation units are not redeclarations (but may still have
2441 // compatible types).
2442 if (!Reader.getContext().getLangOpts().CPlusPlus)
2443 return;
2444
Richard Smith0b87e072013-10-07 08:02:11 +00002445 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2446 if (T *Existing = ExistingRes)
2447 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2448 Existing->getCanonicalDecl());
2449}
2450
Alexey Bataeva769e072013-03-22 06:34:35 +00002451void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2452 VisitDecl(D);
2453 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002454 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002455 Vars.reserve(NumVars);
2456 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002457 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002458 }
2459 D->setVars(Vars);
2460}
2461
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002462void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002463 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002464 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002465 D->setCombiner(Record.readExpr());
2466 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002467 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002468}
2469
Alexey Bataev4244be22016-02-11 05:35:55 +00002470void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002471 VisitVarDecl(D);
2472}
2473
Chris Lattner487412d2009-04-27 05:27:42 +00002474//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002475// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002476//===----------------------------------------------------------------------===//
2477
Chris Lattner8f63ab52009-04-27 06:01:06 +00002478/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002479void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2480 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002481 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002482 attr::Kind Kind = (attr::Kind)Record.readInt();
2483 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002484
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002485#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002486
2487 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002488 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002489 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002490}
2491
2492//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002493// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002494//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002495
2496/// \brief Note that we have loaded the declaration with the given
2497/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002498///
Chris Lattner487412d2009-04-27 05:27:42 +00002499/// This routine notes that this declaration has already been loaded,
2500/// so that future GetDecl calls will return this declaration rather
2501/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002502inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002503 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2504 DeclsLoaded[Index] = D;
2505}
2506
2507
2508/// \brief Determine whether the consumer will be interested in seeing
2509/// this declaration (via HandleTopLevelDecl).
2510///
2511/// This routine should return true for anything that might affect
2512/// code generation, e.g., inline function definitions, Objective-C
2513/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002514static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002515 // An ObjCMethodDecl is never considered as "interesting" because its
2516 // implementation container always is.
2517
Richard Smithdc1f0422016-07-20 19:10:16 +00002518 // An ImportDecl or VarDecl imported from a module will get emitted when
2519 // we import the relevant module.
2520 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && Ctx.DeclMustBeEmitted(D) &&
2521 D->getImportedOwningModule())
2522 return false;
2523
Douglas Gregor87d81242011-09-10 00:22:34 +00002524 if (isa<FileScopeAsmDecl>(D) ||
2525 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002526 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002527 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002528 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002529 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002530 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002531 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2532 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002533 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002534 return Var->isFileVarDecl() &&
2535 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002536 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002537 return Func->doesThisDeclarationHaveABody() || HasBody;
Douglas Gregor87d81242011-09-10 00:22:34 +00002538
2539 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002540}
2541
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002542/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002543ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002544ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002545 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2546 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002547 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002548 const DeclOffset &DOffs =
2549 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002550 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002551 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002552}
2553
Douglas Gregord32f0352011-07-22 06:10:01 +00002554ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002555 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002556 = GlobalBitOffsetsMap.find(GlobalOffset);
2557
2558 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2559 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2560}
2561
Douglas Gregorde3ef502011-11-30 23:21:26 +00002562uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002563 return LocalOffset + M.GlobalBitOffset;
2564}
2565
Richard Smithbf78e642013-06-24 22:51:00 +00002566static bool isSameTemplateParameterList(const TemplateParameterList *X,
2567 const TemplateParameterList *Y);
2568
2569/// \brief Determine whether two template parameters are similar enough
2570/// that they may be used in declarations of the same template.
2571static bool isSameTemplateParameter(const NamedDecl *X,
2572 const NamedDecl *Y) {
2573 if (X->getKind() != Y->getKind())
2574 return false;
2575
2576 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2577 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2578 return TX->isParameterPack() == TY->isParameterPack();
2579 }
2580
2581 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2582 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2583 return TX->isParameterPack() == TY->isParameterPack() &&
2584 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2585 }
2586
2587 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2588 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2589 return TX->isParameterPack() == TY->isParameterPack() &&
2590 isSameTemplateParameterList(TX->getTemplateParameters(),
2591 TY->getTemplateParameters());
2592}
2593
Richard Smith32952e12014-10-14 02:00:47 +00002594static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2595 if (auto *NS = X->getAsNamespace())
2596 return NS;
2597 if (auto *NAS = X->getAsNamespaceAlias())
2598 return NAS->getNamespace();
2599 return nullptr;
2600}
2601
2602static bool isSameQualifier(const NestedNameSpecifier *X,
2603 const NestedNameSpecifier *Y) {
2604 if (auto *NSX = getNamespace(X)) {
2605 auto *NSY = getNamespace(Y);
2606 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2607 return false;
2608 } else if (X->getKind() != Y->getKind())
2609 return false;
2610
2611 // FIXME: For namespaces and types, we're permitted to check that the entity
2612 // is named via the same tokens. We should probably do so.
2613 switch (X->getKind()) {
2614 case NestedNameSpecifier::Identifier:
2615 if (X->getAsIdentifier() != Y->getAsIdentifier())
2616 return false;
2617 break;
2618 case NestedNameSpecifier::Namespace:
2619 case NestedNameSpecifier::NamespaceAlias:
2620 // We've already checked that we named the same namespace.
2621 break;
2622 case NestedNameSpecifier::TypeSpec:
2623 case NestedNameSpecifier::TypeSpecWithTemplate:
2624 if (X->getAsType()->getCanonicalTypeInternal() !=
2625 Y->getAsType()->getCanonicalTypeInternal())
2626 return false;
2627 break;
2628 case NestedNameSpecifier::Global:
2629 case NestedNameSpecifier::Super:
2630 return true;
2631 }
2632
2633 // Recurse into earlier portion of NNS, if any.
2634 auto *PX = X->getPrefix();
2635 auto *PY = Y->getPrefix();
2636 if (PX && PY)
2637 return isSameQualifier(PX, PY);
2638 return !PX && !PY;
2639}
2640
Richard Smithbf78e642013-06-24 22:51:00 +00002641/// \brief Determine whether two template parameter lists are similar enough
2642/// that they may be used in declarations of the same template.
2643static bool isSameTemplateParameterList(const TemplateParameterList *X,
2644 const TemplateParameterList *Y) {
2645 if (X->size() != Y->size())
2646 return false;
2647
2648 for (unsigned I = 0, N = X->size(); I != N; ++I)
2649 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2650 return false;
2651
2652 return true;
2653}
2654
Douglas Gregor022857e2011-12-22 01:48:48 +00002655/// \brief Determine whether the two declarations refer to the same entity.
2656static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2657 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002658
Douglas Gregor022857e2011-12-22 01:48:48 +00002659 if (X == Y)
2660 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002661
Douglas Gregor022857e2011-12-22 01:48:48 +00002662 // Must be in the same context.
2663 if (!X->getDeclContext()->getRedeclContext()->Equals(
2664 Y->getDeclContext()->getRedeclContext()))
2665 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002666
2667 // Two typedefs refer to the same entity if they have the same underlying
2668 // type.
2669 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2670 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2671 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2672 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002673
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002674 // Must have the same kind.
2675 if (X->getKind() != Y->getKind())
2676 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002677
Douglas Gregorda389302012-01-01 21:47:52 +00002678 // Objective-C classes and protocols with the same name always match.
2679 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002680 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002681
2682 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002683 // No need to handle these here: we merge them when adding them to the
2684 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002685 return false;
2686 }
Richard Smithd55889a2013-09-09 16:55:27 +00002687
Douglas Gregor2009cee2012-01-03 22:46:00 +00002688 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002689 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2690 TagDecl *TagY = cast<TagDecl>(Y);
2691 return (TagX->getTagKind() == TagY->getTagKind()) ||
2692 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2693 TagX->getTagKind() == TTK_Interface) &&
2694 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2695 TagY->getTagKind() == TTK_Interface));
2696 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002697
Joao Matose9a3ed42012-08-31 22:18:20 +00002698 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002699 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2700 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002701 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2702 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002703 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2704 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2705 if (CtorX->getInheritedConstructor() &&
2706 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2707 CtorY->getInheritedConstructor().getConstructor()))
2708 return false;
2709 }
Rafael Espindola3ae00052013-05-13 00:12:11 +00002710 return (FuncX->getLinkageInternal() == FuncY->getLinkageInternal()) &&
Douglas Gregorb2585692012-01-04 17:13:46 +00002711 FuncX->getASTContext().hasSameType(FuncX->getType(), FuncY->getType());
2712 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002713
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002714 // Variables with the same type and linkage match.
2715 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2716 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002717 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2718 ASTContext &C = VarX->getASTContext();
2719 if (C.hasSameType(VarX->getType(), VarY->getType()))
2720 return true;
2721
2722 // We can get decls with different types on the redecl chain. Eg.
2723 // template <typename T> struct S { static T Var[]; }; // #1
2724 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2725 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002726 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002727 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2728 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2729 if (!VarXTy || !VarYTy)
2730 return false;
2731 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2732 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2733 }
2734 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002735 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002736
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002737 // Namespaces with the same name and inlinedness match.
2738 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2739 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2740 return NamespaceX->isInline() == NamespaceY->isInline();
2741 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002742
Richard Smith8f8f05c2013-06-24 04:45:28 +00002743 // Identical template names and kinds match if their template parameter lists
2744 // and patterns match.
2745 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002746 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002747 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002748 TemplateY->getTemplatedDecl()) &&
2749 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2750 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002751 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002752
Richard Smith0b87e072013-10-07 08:02:11 +00002753 // Fields with the same name and the same type match.
2754 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2755 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002756 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002757 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2758 }
2759
Richard Smith8cbd8952015-08-04 02:05:09 +00002760 // Indirect fields with the same target field match.
2761 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2762 auto *IFDY = cast<IndirectFieldDecl>(Y);
2763 return IFDX->getAnonField()->getCanonicalDecl() ==
2764 IFDY->getAnonField()->getCanonicalDecl();
2765 }
2766
Richard Smith01a73372013-10-15 22:02:41 +00002767 // Enumerators with the same name match.
2768 if (isa<EnumConstantDecl>(X))
2769 // FIXME: Also check the value is odr-equivalent.
2770 return true;
2771
Richard Smithfd8634a2013-10-23 02:17:46 +00002772 // Using shadow declarations with the same target match.
2773 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2774 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2775 return USX->getTargetDecl() == USY->getTargetDecl();
2776 }
2777
Richard Smith32952e12014-10-14 02:00:47 +00002778 // Using declarations with the same qualifier match. (We already know that
2779 // the name matches.)
2780 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2781 auto *UY = cast<UsingDecl>(Y);
2782 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2783 UX->hasTypename() == UY->hasTypename() &&
2784 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2785 }
2786 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2787 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2788 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2789 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2790 }
2791 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2792 return isSameQualifier(
2793 UX->getQualifier(),
2794 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2795
Richard Smithf4634362014-09-03 23:11:22 +00002796 // Namespace alias definitions with the same target match.
2797 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2798 auto *NAY = cast<NamespaceAliasDecl>(Y);
2799 return NAX->getNamespace()->Equals(NAY->getNamespace());
2800 }
2801
Douglas Gregor022857e2011-12-22 01:48:48 +00002802 return false;
2803}
2804
Richard Smithd55889a2013-09-09 16:55:27 +00002805/// Find the context in which we should search for previous declarations when
2806/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002807DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2808 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002809 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2810 return ND->getOriginalNamespace();
2811
Richard Smith8a639892015-01-24 01:07:20 +00002812 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2813 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002814 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002815 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002816 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002817
2818 // If there's no definition yet, then DC's definition is added by an update
2819 // record, but we've not yet loaded that update record. In this case, we
2820 // commit to DC being the canonical definition now, and will fix this when
2821 // we load the update record.
2822 if (!DD) {
2823 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2824 RD->IsCompleteDefinition = true;
2825 RD->DefinitionData = DD;
2826 RD->getCanonicalDecl()->DefinitionData = DD;
2827
2828 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002829 Reader.PendingFakeDefinitionData.insert(
2830 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002831 }
2832
2833 return DD->Definition;
2834 }
Richard Smithd55889a2013-09-09 16:55:27 +00002835
Richard Smith01a73372013-10-15 22:02:41 +00002836 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002837 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2838 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002839
Richard Smith4eca9b92015-02-04 23:37:59 +00002840 // We can see the TU here only if we have no Sema object. In that case,
2841 // there's no TU scope to look in, so using the DC alone is sufficient.
2842 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2843 return TU;
2844
Craig Toppera13603a2014-05-22 05:54:18 +00002845 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002846}
2847
Douglas Gregor022857e2011-12-22 01:48:48 +00002848ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002849 // Record that we had a typedef name for linkage whether or not we merge
2850 // with that declaration.
2851 if (TypedefNameForLinkage) {
2852 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2853 Reader.ImportedTypedefNamesForLinkage.insert(
2854 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2855 return;
2856 }
2857
Douglas Gregor9b47f942012-01-09 17:38:47 +00002858 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002859 return;
Richard Smith95d99302013-07-13 02:00:19 +00002860
Richard Smith70d58502014-08-30 00:04:23 +00002861 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002862 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002863 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002864 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2865 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002866 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002867 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002868 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002869 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2870 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002871 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002872 // Add the declaration to its redeclaration context so later merging
2873 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002874 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002875 }
2876}
2877
Richard Smith88ebade2014-08-23 01:45:27 +00002878/// Find the declaration that should be merged into, given the declaration found
2879/// by name lookup. If we're merging an anonymous declaration within a typedef,
2880/// we need a matching typedef, and we merge with the type inside it.
2881static NamedDecl *getDeclForMerging(NamedDecl *Found,
2882 bool IsTypedefNameForLinkage) {
2883 if (!IsTypedefNameForLinkage)
2884 return Found;
2885
2886 // If we found a typedef declaration that gives a name to some other
2887 // declaration, then we want that inner declaration. Declarations from
2888 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002889 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002890 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002891
2892 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00002893 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00002894
Hans Wennborgdcfba332015-10-06 23:40:43 +00002895 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00002896}
2897
Richard Smithd08aeb62014-08-28 01:33:39 +00002898NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2899 DeclContext *DC,
2900 unsigned Index) {
2901 // If the lexical context has been merged, look into the now-canonical
2902 // definition.
2903 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2904 DC = Merged;
2905
2906 // If we've seen this before, return the canonical declaration.
2907 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2908 if (Index < Previous.size() && Previous[Index])
2909 return Previous[Index];
2910
2911 // If this is the first time, but we have parsed a declaration of the context,
2912 // build the anonymous declaration list from the parsed declaration.
2913 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00002914 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
2915 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00002916 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
2917 else
Richard Smith2b560572015-02-07 03:11:11 +00002918 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
2919 });
Richard Smithd08aeb62014-08-28 01:33:39 +00002920 }
2921
2922 return Index < Previous.size() ? Previous[Index] : nullptr;
2923}
2924
2925void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
2926 DeclContext *DC, unsigned Index,
2927 NamedDecl *D) {
2928 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2929 DC = Merged;
2930
2931 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2932 if (Index >= Previous.size())
2933 Previous.resize(Index + 1);
2934 if (!Previous[Index])
2935 Previous[Index] = D;
2936}
2937
Douglas Gregor022857e2011-12-22 01:48:48 +00002938ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00002939 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
2940 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00002941
Richard Smithd08aeb62014-08-28 01:33:39 +00002942 if (!Name && !needsAnonymousDeclarationNumber(D)) {
2943 // Don't bother trying to find unnamed declarations that are in
2944 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00002945 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
2946 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00002947 Result.suppress();
2948 return Result;
2949 }
Richard Smith95d99302013-07-13 02:00:19 +00002950
Douglas Gregor022857e2011-12-22 01:48:48 +00002951 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00002952 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00002953 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00002954 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00002955 if (It != Reader.ImportedTypedefNamesForLinkage.end())
2956 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00002957 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
2958 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00002959 // Go on to check in other places in case an existing typedef name
2960 // was not imported.
2961 }
Richard Smithd08aeb62014-08-28 01:33:39 +00002962
Richard Smith2b560572015-02-07 03:11:11 +00002963 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002964 // This is an anonymous declaration that we may need to merge. Look it up
2965 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00002966 if (auto *Existing = getAnonymousDeclForMerging(
2967 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
2968 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00002969 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
2970 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00002971 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002972 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002973 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00002974
2975 // Temporarily consider the identifier to be up-to-date. We don't want to
2976 // cause additional lookups here.
2977 class UpToDateIdentifierRAII {
2978 IdentifierInfo *II;
2979 bool WasOutToDate;
2980
2981 public:
2982 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
2983 : II(II), WasOutToDate(false)
2984 {
2985 if (II) {
2986 WasOutToDate = II->isOutOfDate();
2987 if (WasOutToDate)
2988 II->setOutOfDate(false);
2989 }
2990 }
2991
2992 ~UpToDateIdentifierRAII() {
2993 if (WasOutToDate)
2994 II->setOutOfDate(true);
2995 }
2996 } UpToDate(Name.getAsIdentifierInfo());
2997
Douglas Gregor2009cee2012-01-03 22:46:00 +00002998 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00002999 IEnd = IdResolver.end();
3000 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003001 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003002 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003003 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3004 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003005 }
Richard Smith8a639892015-01-24 01:07:20 +00003006 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003007 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003008 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003009 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003010 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003011 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3012 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003013 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003014 } else {
3015 // Not in a mergeable context.
3016 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003017 }
Richard Smithd55889a2013-09-09 16:55:27 +00003018
Richard Smith2b9e3e32013-10-18 06:05:18 +00003019 // If this declaration is from a merged context, make a note that we need to
3020 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003021 //
3022 // FIXME: We should do something similar if we merge two definitions of the
3023 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003024 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3025 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3026 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003027 Reader.PendingOdrMergeChecks.push_back(D);
3028
Richard Smithd08aeb62014-08-28 01:33:39 +00003029 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003030 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003031}
3032
Richard Smithb321ecb2014-05-13 01:15:00 +00003033template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003034Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3035 return D->RedeclLink.getLatestNotUpdated();
3036}
3037Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3038 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3039}
3040
3041Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3042 assert(D);
3043
3044 switch (D->getKind()) {
3045#define ABSTRACT_DECL(TYPE)
3046#define DECL(TYPE, BASE) \
3047 case Decl::TYPE: \
3048 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3049#include "clang/AST/DeclNodes.inc"
3050 }
3051 llvm_unreachable("unknown decl kind");
3052}
3053
Richard Smith8ce51082015-03-11 01:44:51 +00003054Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3055 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3056}
3057
Richard Smithc3a53252015-02-28 05:57:02 +00003058template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003059void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3060 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003061 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003062 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003063 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003064}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003065
Richard Smith24d166c2014-07-31 23:52:38 +00003066namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003067template<>
3068void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003069 Redeclarable<VarDecl> *D,
3070 Decl *Previous, Decl *Canon) {
3071 VarDecl *VD = static_cast<VarDecl*>(D);
3072 VarDecl *PrevVD = cast<VarDecl>(Previous);
3073 D->RedeclLink.setPrevious(PrevVD);
3074 D->First = PrevVD->First;
3075
3076 // We should keep at most one definition on the chain.
3077 // FIXME: Cache the definition once we've found it. Building a chain with
3078 // N definitions currently takes O(N^2) time here.
3079 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3080 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3081 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3082 Reader.mergeDefinitionVisibility(CurD, VD);
3083 VD->demoteThisDefinitionToDeclaration();
3084 break;
3085 }
3086 }
3087 }
3088}
3089
3090template<>
3091void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003092 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003093 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003094 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3095 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3096
3097 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003098 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003099
3100 // If the previous declaration is an inline function declaration, then this
3101 // declaration is too.
3102 if (PrevFD->IsInline != FD->IsInline) {
3103 // FIXME: [dcl.fct.spec]p4:
3104 // If a function with external linkage is declared inline in one
3105 // translation unit, it shall be declared inline in all translation
3106 // units in which it appears.
3107 //
3108 // Be careful of this case:
3109 //
3110 // module A:
3111 // template<typename T> struct X { void f(); };
3112 // template<typename T> inline void X<T>::f() {}
3113 //
3114 // module B instantiates the declaration of X<int>::f
3115 // module C instantiates the definition of X<int>::f
3116 //
3117 // If module B and C are merged, we do not have a violation of this rule.
3118 FD->IsInline = true;
3119 }
3120
Richard Smith9e2341d2015-03-23 03:25:59 +00003121 // If we need to propagate an exception specification along the redecl
3122 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003123 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3124 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003125 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003126 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3127 bool WasUnresolved =
3128 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3129 if (IsUnresolved != WasUnresolved)
3130 Reader.PendingExceptionSpecUpdates.insert(
3131 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003132 }
3133}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003134} // end namespace clang
3135
Richard Smith6de7a242014-07-31 23:46:44 +00003136void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003137 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3138}
3139
Richard Smith8346e522015-06-10 01:47:58 +00003140/// Inherit the default template argument from \p From to \p To. Returns
3141/// \c false if there is no default template for \p From.
3142template <typename ParmDecl>
3143static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3144 Decl *ToD) {
3145 auto *To = cast<ParmDecl>(ToD);
3146 if (!From->hasDefaultArgument())
3147 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003148 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003149 return true;
3150}
3151
3152static void inheritDefaultTemplateArguments(ASTContext &Context,
3153 TemplateDecl *From,
3154 TemplateDecl *To) {
3155 auto *FromTP = From->getTemplateParameters();
3156 auto *ToTP = To->getTemplateParameters();
3157 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3158
3159 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3160 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003161 if (FromParam->isParameterPack())
3162 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003163 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3164
3165 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003166 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003167 break;
3168 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003169 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003170 break;
3171 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003172 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003173 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3174 break;
3175 }
3176 }
3177}
3178
Richard Smith6de7a242014-07-31 23:46:44 +00003179void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003180 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003181 assert(D && Previous);
3182
3183 switch (D->getKind()) {
3184#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003185#define DECL(TYPE, BASE) \
3186 case Decl::TYPE: \
3187 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003188 break;
3189#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003190 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003191
3192 // If the declaration was visible in one module, a redeclaration of it in
3193 // another module remains visible even if it wouldn't be visible by itself.
3194 //
3195 // FIXME: In this case, the declaration should only be visible if a module
3196 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003197 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003198 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003199 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003200
Richard Smith8346e522015-06-10 01:47:58 +00003201 // If the declaration declares a template, it may inherit default arguments
3202 // from the previous declaration.
3203 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3204 inheritDefaultTemplateArguments(Reader.getContext(),
3205 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003206}
3207
Richard Smithb321ecb2014-05-13 01:15:00 +00003208template<typename DeclT>
3209void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003210 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003211}
3212void ASTDeclReader::attachLatestDeclImpl(...) {
3213 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3214}
3215
Douglas Gregor05f10352011-12-17 23:38:30 +00003216void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3217 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003218
3219 switch (D->getKind()) {
3220#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003221#define DECL(TYPE, BASE) \
3222 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003223 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3224 break;
3225#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003226 }
3227}
3228
Richard Smith851072e2014-05-19 20:59:20 +00003229template<typename DeclT>
3230void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3231 D->RedeclLink.markIncomplete();
3232}
3233void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3234 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3235}
3236
3237void ASTReader::markIncompleteDeclChain(Decl *D) {
3238 switch (D->getKind()) {
3239#define ABSTRACT_DECL(TYPE)
3240#define DECL(TYPE, BASE) \
3241 case Decl::TYPE: \
3242 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3243 break;
3244#include "clang/AST/DeclNodes.inc"
3245 }
3246}
3247
Sebastian Redlb3298c32010-08-18 23:56:48 +00003248/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003249Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003250 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003251 SourceLocation DeclLoc;
3252 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003253 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003254 // Keep track of where we are in the stream, then jump back there
3255 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003256 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003257
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003258 ReadingKindTracker ReadingKind(Read_Decl, *this);
3259
Douglas Gregor1342e842009-07-06 18:54:52 +00003260 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003261 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003262
Sebastian Redl2c373b92010-10-05 15:59:54 +00003263 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003264 ASTRecordReader Record(*this, *Loc.F);
3265 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003266 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003267
Craig Toppera13603a2014-05-22 05:54:18 +00003268 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003269 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003270 case DECL_CONTEXT_LEXICAL:
3271 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003272 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003273 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003274 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003275 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003276 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003277 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003278 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003279 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003280 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003281 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003282 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003283 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003284 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003286 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003287 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003288 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003289 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003290 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003291 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003292 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003293 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003294 case DECL_EXPORT:
3295 D = ExportDecl::CreateDeserialized(Context, ID);
3296 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003297 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003298 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003299 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003300 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003301 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003302 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003303 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003304 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003305 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003306 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003307 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003308 break;
Richard Smith151c4562016-12-20 21:35:28 +00003309 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003310 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003311 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003312 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003313 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003314 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003315 case DECL_CONSTRUCTOR_USING_SHADOW:
3316 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3317 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003318 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003319 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003320 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003321 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003322 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003323 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003324 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003325 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003326 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003327 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003328 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003329 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003330 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003331 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003332 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003333 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003334 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3335 break;
3336 case DECL_CXX_INHERITED_CONSTRUCTOR:
3337 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003338 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003339 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003340 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003341 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003342 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003343 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003344 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003345 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003346 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003347 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003348 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003349 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003350 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003351 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003352 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003353 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003354 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003355 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003356 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003357 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003358 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003359 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003360 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003361 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003362 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003363 case DECL_VAR_TEMPLATE:
3364 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3365 break;
3366 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3367 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3368 break;
3369 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3370 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3371 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003372 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003373 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003374 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003375 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003376 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003377 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003378 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003379 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003380 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003381 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003382 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003383 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003384 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003385 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3386 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003387 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003388 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003389 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003390 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003391 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3392 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003393 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003394 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003395 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003396 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003397 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003398 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003399 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003400 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003401 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003402 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003403 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003404 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003405 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003406 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003407 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003408 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003409 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003410 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003411 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003412 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003413 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003414 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003415 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003416 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003417 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003418 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003419 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003420 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003421 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003422 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003423 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003424 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003425 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003426 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003427 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003428 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003429 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003430 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003431 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003432 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003433 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003434 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003435 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003436 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003437 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003438 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003439 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003440 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003441 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003442 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003443 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003444 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003445 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003446 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003447 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003448 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003449 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003450 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003451 break;
3452 case DECL_BINDING:
3453 D = BindingDecl::CreateDeserialized(Context, ID);
3454 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003455 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003456 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003457 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003458 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003459 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003460 break;
John McCall5e77d762013-04-16 07:28:30 +00003461 case DECL_MS_PROPERTY:
3462 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3463 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003464 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003465 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003466 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003467 case DECL_CXX_BASE_SPECIFIERS:
3468 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003469 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003470 case DECL_CXX_CTOR_INITIALIZERS:
3471 Error("attempt to read a C++ ctor initializer record as a declaration");
3472 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003473 case DECL_IMPORT:
3474 // Note: last entry of the ImportDecl record is the number of stored source
3475 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003476 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003477 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003478 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003479 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003480 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003481 case DECL_OMP_DECLARE_REDUCTION:
3482 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3483 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003484 case DECL_OMP_CAPTUREDEXPR:
3485 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003486 break;
Nico Weber66220292016-03-02 17:28:48 +00003487 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003488 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003489 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003490 case DECL_PRAGMA_DETECT_MISMATCH:
3491 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003492 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003493 break;
Michael Han84324352013-02-22 17:15:32 +00003494 case DECL_EMPTY:
3495 D = EmptyDecl::CreateDeserialized(Context, ID);
3496 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003497 case DECL_OBJC_TYPE_PARAM:
3498 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3499 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003500 }
Chris Lattner487412d2009-04-27 05:27:42 +00003501
Sebastian Redlb3298c32010-08-18 23:56:48 +00003502 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003503 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003504 // Set the DeclContext before doing any deserialization, to make sure internal
3505 // calls to Decl::getASTContext() by Decl's methods will find the
3506 // TranslationUnitDecl without crashing.
3507 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003508 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003509
3510 // If this declaration is also a declaration context, get the
3511 // offsets for its tables of lexical and visible declarations.
3512 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3513 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003514 if (Offsets.first &&
3515 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3516 return nullptr;
3517 if (Offsets.second &&
3518 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3519 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003520 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003521 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003522
Douglas Gregordab42432011-08-12 00:15:20 +00003523 // Load any relevant update records.
Richard Smithd1c46742014-04-30 02:24:17 +00003524 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003525
Douglas Gregor404cdde2012-01-27 01:47:08 +00003526 // Load the categories after recursive loading is finished.
3527 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003528 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3529 // we put the Decl in PendingDefinitions so we can pull the categories here.
3530 if (Class->isThisDeclarationADefinition() ||
3531 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003532 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003533
Richard Smith13fb8602016-07-15 21:33:46 +00003534 // If we have deserialized a declaration that has a definition the
3535 // AST consumer might need to know about, queue it.
3536 // We don't pass it to the consumer immediately because we may be in recursive
3537 // loading, and some declarations may still be initializing.
Richard Smithdc1f0422016-07-20 19:10:16 +00003538 if (isConsumerInterestedIn(Context, D, Reader.hasPendingBody()))
Richard Smith13fb8602016-07-15 21:33:46 +00003539 InterestingDecls.push_back(D);
3540
Douglas Gregordab42432011-08-12 00:15:20 +00003541 return D;
3542}
3543
3544void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003545 // The declaration may have been modified by files later in the chain.
3546 // If this is the case, read the record containing the updates from each file
3547 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003548 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003549 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3550 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003551 auto UpdateOffsets = std::move(UpdI->second);
3552 DeclUpdateOffsets.erase(UpdI);
3553
Richard Smithdc1f0422016-07-20 19:10:16 +00003554 bool WasInteresting = isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003555 for (auto &FileAndOffset : UpdateOffsets) {
3556 ModuleFile *F = FileAndOffset.first;
3557 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003558 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3559 SavedStreamPosition SavedPosition(Cursor);
3560 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003561 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003562 ASTRecordReader Record(*this, *F);
3563 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003564 (void)RecCode;
3565 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003566
David L. Jonesbe1557a2016-12-21 00:17:49 +00003567 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3568 SourceLocation());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003569 Reader.UpdateDecl(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003570
3571 // We might have made this declaration interesting. If so, remember that
3572 // we need to hand it off to the consumer.
3573 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003574 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Richard Smith13fb8602016-07-15 21:33:46 +00003575 InterestingDecls.push_back(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003576 WasInteresting = true;
3577 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003578 }
3579 }
Richard Smith1e8e91b2016-04-08 20:53:26 +00003580
3581 // Load the pending visible updates for this decl context, if it has any.
3582 auto I = PendingVisibleUpdates.find(ID);
3583 if (I != PendingVisibleUpdates.end()) {
3584 auto VisibleUpdates = std::move(I->second);
3585 PendingVisibleUpdates.erase(I);
3586
3587 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3588 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3589 Lookups[DC].Table.add(
3590 Update.Mod, Update.Data,
3591 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3592 DC->setHasExternalVisibleStorage(true);
3593 }
Chris Lattner487412d2009-04-27 05:27:42 +00003594}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003595
Richard Smithd61d4ac2015-08-22 20:13:39 +00003596void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3597 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003598 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003599 if (FirstLocal != CanonDecl) {
3600 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3601 ASTDeclReader::attachPreviousDecl(
3602 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3603 CanonDecl);
3604 }
3605
3606 if (!LocalOffset) {
3607 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3608 return;
3609 }
3610
3611 // Load the list of other redeclarations from this module file.
3612 ModuleFile *M = getOwningModuleFile(FirstLocal);
3613 assert(M && "imported decl from no module file");
3614
3615 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3616 SavedStreamPosition SavedPosition(Cursor);
3617 Cursor.JumpToBit(LocalOffset);
3618
3619 RecordData Record;
3620 unsigned Code = Cursor.ReadCode();
3621 unsigned RecCode = Cursor.readRecord(Code, Record);
3622 (void)RecCode;
3623 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3624
3625 // FIXME: We have several different dispatches on decl kind here; maybe
3626 // we should instead generate one loop per kind and dispatch up-front?
3627 Decl *MostRecent = FirstLocal;
3628 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3629 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003630 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3631 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003632 }
Richard Smithc3a53252015-02-28 05:57:02 +00003633 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003634}
3635
3636namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003637 /// \brief Given an ObjC interface, goes through the modules and links to the
3638 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003639 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003640 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003641 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003642 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003643 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003644 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003645 serialization::GlobalDeclID InterfaceID;
3646 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003647
3648 void add(ObjCCategoryDecl *Cat) {
3649 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003650 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003651 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003652
3653 // Check for duplicate categories.
3654 if (Cat->getDeclName()) {
3655 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3656 if (Existing &&
3657 Reader.getOwningModuleFile(Existing)
3658 != Reader.getOwningModuleFile(Cat)) {
3659 // FIXME: We should not warn for duplicates in diamond:
3660 //
3661 // MT //
3662 // / \ //
3663 // ML MR //
3664 // \ / //
3665 // MB //
3666 //
3667 // If there are duplicates in ML/MR, there will be warning when
3668 // creating MB *and* when importing MB. We should not warn when
3669 // importing.
3670 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3671 << Interface->getDeclName() << Cat->getDeclName();
3672 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3673 } else if (!Existing) {
3674 // Record this category.
3675 Existing = Cat;
3676 }
3677 }
3678
3679 // Add this category to the end of the chain.
3680 if (Tail)
3681 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3682 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003683 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003684 Tail = Cat;
3685 }
3686
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003687 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003688 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003689 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003690 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3691 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003692 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003693 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3694 Tail(nullptr), InterfaceID(InterfaceID),
3695 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003696 {
3697 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003698 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003699 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003700 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003701
3702 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003703 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003704 }
3705 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003706
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003707 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003708 // If we've loaded all of the category information we care about from
3709 // this module file, we're done.
3710 if (M.Generation <= PreviousGeneration)
3711 return true;
3712
3713 // Map global ID of the definition down to the local ID used in this
3714 // module file. If there is no such mapping, we'll find nothing here
3715 // (or in any module it imports).
3716 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3717 if (!LocalID)
3718 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003719
Douglas Gregor404cdde2012-01-27 01:47:08 +00003720 // Perform a binary search to find the local redeclarations for this
3721 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003722 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003723 const ObjCCategoriesInfo *Result
3724 = std::lower_bound(M.ObjCCategoriesMap,
3725 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003726 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003727 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3728 Result->DefinitionID != LocalID) {
3729 // We didn't find anything. If the class definition is in this module
3730 // file, then the module files it depends on cannot have any categories,
3731 // so suppress further lookup.
3732 return Reader.isDeclIDFromModule(InterfaceID, M);
3733 }
3734
3735 // We found something. Dig out all of the categories.
3736 unsigned Offset = Result->Offset;
3737 unsigned N = M.ObjCCategories[Offset];
3738 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3739 for (unsigned I = 0; I != N; ++I)
3740 add(cast_or_null<ObjCCategoryDecl>(
3741 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3742 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003743 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003744 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003745} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003746
Douglas Gregor404cdde2012-01-27 01:47:08 +00003747void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3748 ObjCInterfaceDecl *D,
3749 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003750 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003751 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003752 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003753}
Douglas Gregordab42432011-08-12 00:15:20 +00003754
Richard Smithd6db68c2014-08-07 20:58:41 +00003755template<typename DeclT, typename Fn>
3756static void forAllLaterRedecls(DeclT *D, Fn F) {
3757 F(D);
3758
3759 // Check whether we've already merged D into its redeclaration chain.
3760 // MostRecent may or may not be nullptr if D has not been merged. If
3761 // not, walk the merged redecl chain and see if it's there.
3762 auto *MostRecent = D->getMostRecentDecl();
3763 bool Found = false;
3764 for (auto *Redecl = MostRecent; Redecl && !Found;
3765 Redecl = Redecl->getPreviousDecl())
3766 Found = (Redecl == D);
3767
3768 // If this declaration is merged, apply the functor to all later decls.
3769 if (Found) {
3770 for (auto *Redecl = MostRecent; Redecl != D;
3771 Redecl = Redecl->getPreviousDecl())
3772 F(Redecl);
3773 }
3774}
3775
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003776void ASTDeclReader::UpdateDecl(Decl *D) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003777 while (Record.getIdx() < Record.size()) {
3778 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003779 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003780 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003781 // FIXME: If we also have an update record for instantiating the
3782 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003783 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003784 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003785 // FIXME: We should call addHiddenDecl instead, to add the member
3786 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003787 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003788 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003789 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003790
3791 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3792 // It will be added to the template's specializations set when loaded.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003793 (void)Record.readDecl();
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003794 break;
3795
3796 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003797 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3798
Douglas Gregor540fd812012-01-09 18:07:24 +00003799 // Each module has its own anonymous namespace, which is disjoint from
3800 // any other module's anonymous namespaces, so don't attach the anonymous
3801 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003802 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003803 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3804 TU->setAnonymousNamespace(Anon);
3805 else
3806 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3807 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003808 break;
3809 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003810
3811 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3812 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003813 ReadSourceLocation());
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003814 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00003815
John McCall32791cc2016-01-06 22:34:54 +00003816 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3817 auto Param = cast<ParmVarDecl>(D);
3818
3819 // We have to read the default argument regardless of whether we use it
3820 // so that hypothetical further update records aren't messed up.
3821 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003822 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003823
3824 // Only apply the update if the parameter still has an uninstantiated
3825 // default argument.
3826 if (Param->hasUninstantiatedDefaultArg())
3827 Param->setDefaultArg(DefaultArg);
3828 break;
3829 }
3830
Richard Smith4b054b22016-08-24 21:25:37 +00003831 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3832 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003833 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003834
3835 // Only apply the update if the field still has an uninstantiated
3836 // default member initializer.
3837 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3838 if (DefaultInit)
3839 FD->setInClassInitializer(DefaultInit);
3840 else
3841 // Instantiation failed. We can get here if we serialized an AST for
3842 // an invalid program.
3843 FD->removeInClassInitializer();
3844 }
3845 break;
3846 }
3847
Richard Smith4d235792014-08-07 18:53:08 +00003848 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003849 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003850 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003851 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003852 // It's safe to stop now because this update record is always last.
3853 return;
3854 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003855
David L. Jonesbe1557a2016-12-21 00:17:49 +00003856 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00003857 // Maintain AST consistency: any later redeclarations of this function
3858 // are inline if this one is. (We might have merged another declaration
3859 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00003860 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3861 FD->setImplicitlyInline();
3862 });
Richard Smith195d8ef2014-05-29 03:15:31 +00003863 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003864 FD->setInnerLocStart(ReadSourceLocation());
Richard Smithc2bb8182015-03-24 06:36:48 +00003865 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003866 CD->NumCtorInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00003867 if (CD->NumCtorInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003868 CD->CtorInitializers = ReadGlobalOffset();
Richard Smithc2bb8182015-03-24 06:36:48 +00003869 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003870 // Store the offset of the body so we can lazily load it later.
3871 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
Richard Smith04d05b52014-03-23 00:27:18 +00003872 HasPendingBody = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003873 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00003874 break;
3875 }
3876
Richard Smithcd45dbc2014-04-19 03:48:30 +00003877 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
3878 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00003879 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00003880 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00003881 OldDD && (OldDD->Definition != RD ||
3882 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00003883 ReadCXXRecordDefinition(RD, /*Update*/true);
3884
Richard Smithcd45dbc2014-04-19 03:48:30 +00003885 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003886 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00003887 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00003888 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00003889 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003890 }
3891
David L. Jonesbe1557a2016-12-21 00:17:49 +00003892 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003893 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003894 if (MemberSpecializationInfo *MSInfo =
3895 RD->getMemberSpecializationInfo()) {
3896 MSInfo->setTemplateSpecializationKind(TSK);
3897 MSInfo->setPointOfInstantiation(POI);
3898 } else {
3899 ClassTemplateSpecializationDecl *Spec =
3900 cast<ClassTemplateSpecializationDecl>(RD);
3901 Spec->setTemplateSpecializationKind(TSK);
3902 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00003903
David L. Jonesbe1557a2016-12-21 00:17:49 +00003904 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00003905 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003906 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00003907 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003908 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00003909 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00003910 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00003911
3912 // FIXME: If we already have a partial specialization set,
3913 // check that it matches.
3914 if (!Spec->getSpecializedTemplateOrPartial()
3915 .is<ClassTemplatePartialSpecializationDecl *>())
3916 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00003917 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003918 }
3919
David L. Jonesbe1557a2016-12-21 00:17:49 +00003920 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003921 RD->setLocation(ReadSourceLocation());
3922 RD->setLocStart(ReadSourceLocation());
3923 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00003924
David L. Jonesbe1557a2016-12-21 00:17:49 +00003925 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003926 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00003927 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00003928 // If the declaration already has attributes, we assume that some other
3929 // AST file already loaded them.
3930 if (!D->hasAttrs())
3931 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00003932 }
3933 break;
3934 }
3935
Richard Smithf8134002015-03-10 01:41:22 +00003936 case UPD_CXX_RESOLVED_DTOR_DELETE: {
3937 // Set the 'operator delete' directly to avoid emitting another update
3938 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003939 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00003940 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
3941 // FIXME: Check consistency if we have an old and new operator delete.
3942 if (!First->OperatorDelete)
3943 First->OperatorDelete = Del;
3944 break;
3945 }
3946
Richard Smith564417a2014-03-20 21:47:22 +00003947 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00003948 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00003949 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003950 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00003951
3952 // Update this declaration's exception specification, if needed.
3953 auto *FD = cast<FunctionDecl>(D);
3954 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
3955 // FIXME: If the exception specification is already present, check that it
3956 // matches.
3957 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00003958 FD->setType(Reader.Context.getFunctionType(
3959 FPT->getReturnType(), FPT->getParamTypes(),
3960 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00003961
3962 // When we get to the end of deserializing, see if there are other decls
3963 // that we need to propagate this exception specification onto.
3964 Reader.PendingExceptionSpecUpdates.insert(
3965 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003966 }
Richard Smith564417a2014-03-20 21:47:22 +00003967 break;
3968 }
3969
Richard Smith1fa5d642013-05-11 05:45:24 +00003970 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00003971 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00003972 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00003973 for (auto *Redecl : merged_redecls(D)) {
3974 // FIXME: If the return type is already deduced, check that it matches.
3975 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
3976 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
3977 }
Richard Smith1fa5d642013-05-11 05:45:24 +00003978 break;
3979 }
Eli Friedman276dd182013-09-05 00:02:25 +00003980
3981 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00003982 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003983 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00003984 break;
3985 }
Richard Smith5652c0f2014-03-21 01:48:23 +00003986
3987 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003988 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00003989 break;
3990
3991 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003992 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00003993 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00003994
Alexey Bataev97720002014-11-11 04:05:39 +00003995 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
3996 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003997 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00003998 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00003999
Alex Denisovfde64952015-06-26 05:28:36 +00004000 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004001 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004002 auto *Exported = cast<NamedDecl>(D);
4003 if (auto *TD = dyn_cast<TagDecl>(Exported))
4004 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004005 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004006 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004007 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4008 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004009 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4010 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004011 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004012 // If Owner is made visible at some later point, make this declaration
4013 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004014 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004015 } else {
4016 // The declaration is now visible.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004017 Exported->Hidden = false;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004018 }
4019 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004020 }
Alex Denisovfde64952015-06-26 05:28:36 +00004021
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004022 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004023 case UPD_ADDED_ATTR_TO_RECORD:
4024 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004025 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004026 assert(Attrs.size() == 1);
4027 D->addAttr(Attrs[0]);
4028 break;
4029 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004030 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004031}