blob: e1b1abba46fb3ed1b4ce948dec04b6cf3a078af3 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000011// entrypoint for loading a decl.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000015#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000016#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclGroup.h"
20#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/DeclVisitor.h"
22#include "clang/AST/Expr.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000023#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000024#include "clang/Sema/SemaDiagnostic.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000025#include "clang/Serialization/ASTReader.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000026#include "llvm/Support/SaveAndRestore.h"
Hans Wennborgdcfba332015-10-06 23:40:43 +000027
Chris Lattner487412d2009-04-27 05:27:42 +000028using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000029using namespace clang::serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000030
31//===----------------------------------------------------------------------===//
32// Declaration deserialization
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000035namespace clang {
Sebastian Redlb3298c32010-08-18 23:56:48 +000036 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000037 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000038 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000039 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000040 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000041 const SourceLocation ThisDeclLoc;
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +000042 typedef ASTReader::RecordData RecordData;
Sebastian Redl539c5062010-08-18 23:57:32 +000043 TypeID TypeIDForTypeDecl;
Richard Smithd08aeb62014-08-28 01:33:39 +000044 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +000045 GlobalDeclID NamedDeclForTagDecl;
46 IdentifierInfo *TypedefNameForLinkage;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000047
Douglas Gregora6017bb2012-10-09 17:21:28 +000048 bool HasPendingBody;
49
Vassil Vassilev928c8252016-04-28 14:13:28 +000050 ///\brief A flag to carry the information for a decl from the entity is
51 /// used. We use it to delay the marking of the canonical decl as used until
52 /// the entire declaration is deserialized and merged.
53 bool IsDeclMarkedUsed;
54
Sebastian Redlc67764e2010-07-22 22:43:28 +000055 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000056
David L. Jonesc4808b9e2016-12-15 20:53:26 +000057 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +000058 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +000059 assert(LocalOffset < Loc.Offset && "offset point after current record");
60 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +000061 }
62
David L. Jonesc4808b9e2016-12-15 20:53:26 +000063 uint64_t ReadGlobalOffset() {
64 uint64_t Local = ReadLocalOffset();
65 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +000066 }
67
David L. Jonesc4808b9e2016-12-15 20:53:26 +000068 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000069 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +000070 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000071
David L. Jonesc4808b9e2016-12-15 20:53:26 +000072 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000073 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +000074 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000075
David L. Jonesc4808b9e2016-12-15 20:53:26 +000076 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000077 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +000078 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000079
David L. Jonesc4808b9e2016-12-15 20:53:26 +000080 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000081 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +000082 }
Richard Smith0b884372015-02-27 00:25:58 +000083
David L. Jonesc4808b9e2016-12-15 20:53:26 +000084 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000085 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +000086 }
87
Richard Smith0b884372015-02-27 00:25:58 +000088 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +000089 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +000090 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +000091 }
92
David L. Jonesc4808b9e2016-12-15 20:53:26 +000093 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000094 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +000095 }
96
97 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +000098 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000099 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000100 }
101
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000102 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000103 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000104 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000105
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000106 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000107 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000108 }
109
110 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000111 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000112 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000113
David L. Jonesbe1557a2016-12-21 00:17:49 +0000114 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000115 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000116
117 Module *readModule() {
118 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000119 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000120
Richard Smith2a9e5c52015-02-03 03:32:14 +0000121 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000122 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
123 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000124 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000125 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000126 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000127 void MergeDefinitionData(ObjCInterfaceDecl *D,
128 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000129 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
130 void MergeDefinitionData(ObjCProtocolDecl *D,
131 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000132
Richard Smithd08aeb62014-08-28 01:33:39 +0000133 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
134 DeclContext *DC,
135 unsigned Index);
136 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
137 unsigned Index, NamedDecl *D);
138
Richard Smith0144f512015-08-22 02:09:38 +0000139 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000140 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000141 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000142 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000143 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000144
Douglas Gregor022857e2011-12-22 01:48:48 +0000145 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000146 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
147 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000148
Douglas Gregor022857e2011-12-22 01:48:48 +0000149 /// \brief Retrieve the first ID.
150 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000151
Richard Smith0144f512015-08-22 02:09:38 +0000152 /// \brief Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000153 bool isKeyDecl() const { return IsKeyDecl; }
154
Richard Smith5a4737c2015-02-06 02:42:59 +0000155 /// \brief Get a known declaration that this should be merged with, if
156 /// any.
157 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000158 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000159
Douglas Gregor022857e2011-12-22 01:48:48 +0000160 /// \brief Class used to capture the result of searching for an existing
161 /// declaration of a specific kind and name, along with the ability
162 /// to update the place where this result was found (the declaration
163 /// chain hanging off an identifier or the DeclContext we searched in)
164 /// if requested.
165 class FindExistingResult {
166 ASTReader &Reader;
167 NamedDecl *New;
168 NamedDecl *Existing;
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000169 bool AddResult;
Richard Smith70d58502014-08-30 00:04:23 +0000170
Richard Smithd08aeb62014-08-28 01:33:39 +0000171 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +0000172 IdentifierInfo *TypedefNameForLinkage;
Richard Smithd08aeb62014-08-28 01:33:39 +0000173
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000174 void operator=(FindExistingResult &&) = delete;
Richard Smithd08aeb62014-08-28 01:33:39 +0000175
Douglas Gregor022857e2011-12-22 01:48:48 +0000176 public:
177 FindExistingResult(ASTReader &Reader)
Richard Smithd08aeb62014-08-28 01:33:39 +0000178 : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false),
Hans Wennborgdcfba332015-10-06 23:40:43 +0000179 AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000180
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000182 unsigned AnonymousDeclNumber,
183 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000184 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000185 AnonymousDeclNumber(AnonymousDeclNumber),
186 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000187
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000188 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000189 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
190 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000191 AnonymousDeclNumber(Other.AnonymousDeclNumber),
192 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 Other.AddResult = false;
194 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000195
Douglas Gregor022857e2011-12-22 01:48:48 +0000196 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000197
Douglas Gregor2009cee2012-01-03 22:46:00 +0000198 /// \brief Suppress the addition of this result into the known set of
199 /// names.
200 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000201
Douglas Gregor022857e2011-12-22 01:48:48 +0000202 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000203
Douglas Gregor022857e2011-12-22 01:48:48 +0000204 template<typename T>
205 operator T*() const { return dyn_cast_or_null<T>(Existing); }
206 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000207
Richard Smith8a639892015-01-24 01:07:20 +0000208 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
209 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000210 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000211
Chris Lattner487412d2009-04-27 05:27:42 +0000212 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000213 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
214 ASTReader::RecordLocation Loc,
215 DeclID thisDeclID, SourceLocation ThisDeclLoc)
216 : Reader(Reader), Record(Record), Loc(Loc),
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000217 ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc),
David L. Jonesbe1557a2016-12-21 00:17:49 +0000218 TypeIDForTypeDecl(0), NamedDeclForTagDecl(0),
Vassil Vassilev928c8252016-04-28 14:13:28 +0000219 TypedefNameForLinkage(nullptr), HasPendingBody(false),
220 IsDeclMarkedUsed(false) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000221
Richard Smithb321ecb2014-05-13 01:15:00 +0000222 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000223 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
224 static Decl *getMostRecentDeclImpl(...);
225 static Decl *getMostRecentDecl(Decl *D);
226
227 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000228 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000229 Redeclarable<DeclT> *D, Decl *Previous,
230 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000231 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000232 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
233 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000234
235 template <typename DeclT>
236 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
237 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000238 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000239
Richard Smith851072e2014-05-19 20:59:20 +0000240 template <typename DeclT>
241 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
242 static void markIncompleteDeclChainImpl(...);
243
Douglas Gregora6017bb2012-10-09 17:21:28 +0000244 /// \brief Determine whether this declaration has a pending body.
245 bool hasPendingBody() const { return HasPendingBody; }
246
David Blaikieac4345c2017-02-12 18:45:31 +0000247 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000248 void Visit(Decl *D);
249
Vassil Vassileva91cbdc2017-06-15 11:05:32 +0000250 void UpdateDecl(Decl *D);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000251
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000252 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
253 ObjCCategoryDecl *Next) {
254 Cat->NextClassCategory = Next;
255 }
256
Chris Lattner487412d2009-04-27 05:27:42 +0000257 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000258 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000259 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000260 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
261 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000262 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000263 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000264 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
265 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000266 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000267 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000268 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000269 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000270 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000271 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000272 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000273 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
274 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
275 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
276 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
277 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000278 ClassTemplateSpecializationDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000279 void VisitClassTemplateSpecializationDecl(
280 ClassTemplateSpecializationDecl *D) {
281 VisitClassTemplateSpecializationDeclImpl(D);
282 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000283 void VisitClassTemplatePartialSpecializationDecl(
284 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000285 void VisitClassScopeFunctionSpecializationDecl(
286 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000287 RedeclarableResult
288 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
289 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
290 VisitVarTemplateSpecializationDeclImpl(D);
291 }
292 void VisitVarTemplatePartialSpecializationDecl(
293 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000294 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000295 void VisitValueDecl(ValueDecl *VD);
296 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000297 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000298 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000299 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000300 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000301 void VisitCXXMethodDecl(CXXMethodDecl *D);
302 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
303 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
304 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000305 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000306 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000307 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000308 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
309 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000310 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
311 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000312 void VisitDecompositionDecl(DecompositionDecl *DD);
313 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000314 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000315 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000316 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000317 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000318 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000319 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000320 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000321 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000322 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000323 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000324 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000325 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000326 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000327 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000328 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000329 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000330 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000331 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000332 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000333 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
334 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000335 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000336 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000337 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000338
Chris Lattner487412d2009-04-27 05:27:42 +0000339 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000340
341 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000342 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000343
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000344 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000345 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
346 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000347
348 template<typename T>
349 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000350 RedeclarableResult &Redecl,
351 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000352
Richard Smith0b87e072013-10-07 08:02:11 +0000353 template<typename T>
354 void mergeMergeable(Mergeable<T> *D);
355
Richard Smithf17fdbd2014-04-24 02:25:27 +0000356 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
357 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000358 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000359
Douglas Gregor85f3f952015-07-07 03:57:15 +0000360 ObjCTypeParamList *ReadObjCTypeParamList();
361
Alexis Hunted053252010-05-30 07:21:58 +0000362 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000363 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000364 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000365 void VisitObjCContainerDecl(ObjCContainerDecl *D);
366 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
367 void VisitObjCIvarDecl(ObjCIvarDecl *D);
368 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
369 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000370 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
371 void VisitObjCImplDecl(ObjCImplDecl *D);
372 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
373 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
374 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
375 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
376 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000377 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000378 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000379 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000380 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000381} // end namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000382
Richard Smith86dfc1e2015-06-18 22:07:00 +0000383namespace {
384/// Iterator over the redeclarations of a declaration that have already
385/// been merged into the same redeclaration chain.
386template<typename DeclT>
387class MergedRedeclIterator {
388 DeclT *Start, *Canonical, *Current;
389public:
390 MergedRedeclIterator() : Current(nullptr) {}
391 MergedRedeclIterator(DeclT *Start)
392 : Start(Start), Canonical(nullptr), Current(Start) {}
393
394 DeclT *operator*() { return Current; }
395
396 MergedRedeclIterator &operator++() {
397 if (Current->isFirstDecl()) {
398 Canonical = Current;
399 Current = Current->getMostRecentDecl();
400 } else
401 Current = Current->getPreviousDecl();
402
403 // If we started in the merged portion, we'll reach our start position
404 // eventually. Otherwise, we'll never reach it, but the second declaration
405 // we reached was the canonical declaration, so stop when we see that one
406 // again.
407 if (Current == Start || Current == Canonical)
408 Current = nullptr;
409 return *this;
410 }
411
412 friend bool operator!=(const MergedRedeclIterator &A,
413 const MergedRedeclIterator &B) {
414 return A.Current != B.Current;
415 }
416};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000417} // end anonymous namespace
418
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000419template <typename DeclT>
420static llvm::iterator_range<MergedRedeclIterator<DeclT>>
421merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000422 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
423 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000424}
425
Sebastian Redlb3298c32010-08-18 23:56:48 +0000426uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000427 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000428}
429
David Blaikieac4345c2017-02-12 18:45:31 +0000430void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
David Blaikiee6b7c282017-04-11 20:46:34 +0000431 if (Record.readInt())
432 Reader.BodySource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000433 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
434 CD->NumCtorInitializers = Record.readInt();
435 if (CD->NumCtorInitializers)
436 CD->CtorInitializers = ReadGlobalOffset();
437 }
438 // Store the offset of the body so we can lazily load it later.
439 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
440 HasPendingBody = true;
441}
442
Sebastian Redlb3298c32010-08-18 23:56:48 +0000443void ASTDeclReader::Visit(Decl *D) {
444 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000445
Vassil Vassilev928c8252016-04-28 14:13:28 +0000446 // At this point we have deserialized and merged the decl and it is safe to
447 // update its canonical decl to signal that the entire entity is used.
448 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
449 IsDeclMarkedUsed = false;
450
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000451 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
452 if (DD->DeclInfo) {
453 DeclaratorDecl::ExtInfo *Info =
454 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000455 Info->TInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000456 }
457 else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000458 DD->DeclInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000459 }
460 }
461
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000462 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000463 // We have a fully initialized TypeDecl. Read its type now.
Douglas Gregor0cdc8322010-12-10 17:03:06 +0000464 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000465
466 // If this is a tag declaration with a typedef name for linkage, it's safe
467 // to load that typedef now.
468 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000469 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
470 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000471 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
472 // if we have a fully initialized TypeDecl, we can safely read its type now.
473 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000474 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
475 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000476 // We only read it if FD doesn't already have a body (e.g., from another
477 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000478 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000479 if (Record.readInt())
480 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000481 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000482}
483
Sebastian Redlb3298c32010-08-18 23:56:48 +0000484void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000485 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
486 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000487 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000488 // parameter or of a parameter of a function template immediately. These
489 // entities might be used in the formulation of its DeclContext (for
490 // example, a function parameter can be used in decltype() in trailing
491 // return type of the function). Use the translation unit DeclContext as a
492 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000493 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
494 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000495 if (!LexicalDCIDForTemplateParmDecl)
496 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000497 Reader.addPendingDeclContextInfo(D,
498 SemaDCIDForTemplateParmDecl,
499 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000500 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000501 } else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000502 DeclContext *SemaDC = ReadDeclAs<DeclContext>();
503 DeclContext *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000504 if (!LexicalDC)
505 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000506 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000507 // Avoid calling setLexicalDeclContext() directly because it uses
508 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000509 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
510 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000511 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000512 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000513 D->setInvalidDecl(Record.readInt());
514 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000515 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000516 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000517 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
518 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000519 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000520 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000521 D->setImplicit(Record.readInt());
522 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000523 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000524 D->setReferenced(Record.readInt());
525 D->setTopLevelDeclInObjCContainer(Record.readInt());
526 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000527 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000528 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000529
Douglas Gregorcf68c582011-12-01 22:20:10 +0000530 // Determine whether this declaration is part of a (sub)module. If so, it
531 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000532 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000533 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000534 D->setModuleOwnershipKind(
535 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
536 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000537 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000538
Richard Smith90dc5252017-06-23 01:04:34 +0000539 if (ModulePrivate) {
540 // Module-private declarations are never visible, so there is no work to
541 // do.
Richard Smith42413142015-05-15 20:05:43 +0000542 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
543 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000544 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000545 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000546 // Mark the declaration as visible when its owning module becomes visible.
547 if (Owner->NameVisibility == Module::AllVisible)
548 D->setVisibleDespiteOwningModule();
549 else
Richard Smith42413142015-05-15 20:05:43 +0000550 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000551 }
552 }
Chris Lattner487412d2009-04-27 05:27:42 +0000553}
554
Nico Weber66220292016-03-02 17:28:48 +0000555void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
556 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000557 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000558 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000559 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000560 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
561 D->getTrailingObjects<char>()[Arg.size()] = '\0';
562}
563
Nico Webercbbaeb12016-03-02 19:28:54 +0000564void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
565 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000566 D->setLocation(ReadSourceLocation());
567 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000568 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
569 D->getTrailingObjects<char>()[Name.size()] = '\0';
570
571 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000572 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000573 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
574 Value.size());
575 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
576}
577
Sebastian Redlb3298c32010-08-18 23:56:48 +0000578void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000579 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000580}
581
Sebastian Redlb3298c32010-08-18 23:56:48 +0000582void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000583 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000584 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000585 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000586}
587
Sebastian Redlb3298c32010-08-18 23:56:48 +0000588void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000589 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000590 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000591 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000592 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000593}
594
Richard Smith43ccec8e2014-08-26 03:52:16 +0000595ASTDeclReader::RedeclarableResult
596ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000597 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000598 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000599 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000600 if (Record.readInt()) { // isModed
601 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000602 TD->setModedTypeSourceInfo(TInfo, modedT);
603 } else
604 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000605 // Read and discard the declaration for which this is a typedef name for
606 // linkage, if it exists. We cannot rely on our type to pull in this decl,
607 // because it might have been merged with a type from another module and
608 // thus might not refer to our version of the declaration.
609 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000610 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000611}
612
613void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000614 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
615 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000616}
617
Richard Smithdda56e42011-04-15 14:24:37 +0000618void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000619 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000620 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000621 // Merged when we merge the template.
622 TD->setDescribedAliasTemplate(Template);
623 else
624 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000625}
626
Richard Smith1d209d02013-05-23 01:49:11 +0000627ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000628 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000629 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000630
David L. Jonesbe1557a2016-12-21 00:17:49 +0000631 TD->IdentifierNamespace = Record.readInt();
632 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000633 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000634 TD->setCompleteDefinition(Record.readInt());
635 TD->setEmbeddedInDeclarator(Record.readInt());
636 TD->setFreeStanding(Record.readInt());
637 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000638 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000639
David L. Jonesbe1557a2016-12-21 00:17:49 +0000640 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000641 case 0:
642 break;
643 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000644 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000645 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000646 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000647 break;
648 }
649 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000650 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000651 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000652 break;
Richard Smith70d58502014-08-30 00:04:23 +0000653 default:
654 llvm_unreachable("unexpected tag info kind");
655 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000656
Richard Smithcd45dbc2014-04-19 03:48:30 +0000657 if (!isa<CXXRecordDecl>(TD))
658 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000659 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000660}
661
Sebastian Redlb3298c32010-08-18 23:56:48 +0000662void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000663 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000664 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000665 ED->setIntegerTypeSourceInfo(TI);
666 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000667 ED->setIntegerType(Record.readType());
668 ED->setPromotionType(Record.readType());
669 ED->setNumPositiveBits(Record.readInt());
670 ED->setNumNegativeBits(Record.readInt());
671 ED->IsScoped = Record.readInt();
672 ED->IsScopedUsingClassTag = Record.readInt();
673 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000674
Richard Smith01a73372013-10-15 22:02:41 +0000675 // If this is a definition subject to the ODR, and we already have a
676 // definition, merge this one into it.
677 if (ED->IsCompleteDefinition &&
678 Reader.getContext().getLangOpts().Modules &&
679 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000680 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
681 if (!OldDef) {
682 // This is the first time we've seen an imported definition. Look for a
683 // local definition before deciding that we are the first definition.
684 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
685 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
686 OldDef = D;
687 break;
688 }
689 }
690 }
691 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000692 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
693 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000694 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000695 } else {
696 OldDef = ED;
697 }
698 }
699
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000700 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000701 TemplateSpecializationKind TSK =
702 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000703 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000704 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
705 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
706 }
Chris Lattner487412d2009-04-27 05:27:42 +0000707}
708
Richard Smith1d209d02013-05-23 01:49:11 +0000709ASTDeclReader::RedeclarableResult
710ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
711 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000712 RD->setHasFlexibleArrayMember(Record.readInt());
713 RD->setAnonymousStructOrUnion(Record.readInt());
714 RD->setHasObjectMember(Record.readInt());
715 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000716 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000717}
718
Sebastian Redlb3298c32010-08-18 23:56:48 +0000719void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000720 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000721 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000722}
723
Sebastian Redlb3298c32010-08-18 23:56:48 +0000724void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000725 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000726 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000727 ECD->setInitExpr(Record.readExpr());
728 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000729 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000730}
731
Sebastian Redlb3298c32010-08-18 23:56:48 +0000732void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000733 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000734 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000735 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000736 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000737 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000738 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000739 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000740 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000741}
742
Sebastian Redlb3298c32010-08-18 23:56:48 +0000743void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000744 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000745 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000746
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000747 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000748 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000749
Douglas Gregorb2585692012-01-04 17:13:46 +0000750 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
751 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000752
David L. Jonesbe1557a2016-12-21 00:17:49 +0000753 FD->SClass = (StorageClass)Record.readInt();
754 FD->IsInline = Record.readInt();
755 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000756 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000757 FD->IsVirtualAsWritten = Record.readInt();
758 FD->IsPure = Record.readInt();
759 FD->HasInheritedPrototype = Record.readInt();
760 FD->HasWrittenPrototype = Record.readInt();
761 FD->IsDeleted = Record.readInt();
762 FD->IsTrivial = Record.readInt();
763 FD->IsDefaulted = Record.readInt();
764 FD->IsExplicitlyDefaulted = Record.readInt();
765 FD->HasImplicitReturnZero = Record.readInt();
766 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000767 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000768 FD->HasSkippedBody = Record.readInt();
769 FD->IsLateTemplateParsed = Record.readInt();
770 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000771 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000772
David L. Jonesbe1557a2016-12-21 00:17:49 +0000773 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000774 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000775 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000776 break;
777 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000778 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000779 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000780 break;
781 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000782 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000783 TemplateSpecializationKind TSK =
784 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000785 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000786 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000787 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000788 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000789 break;
790 }
791 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000792 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000793 TemplateSpecializationKind TSK =
794 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000795
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000796 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000797 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000798 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000799
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000800 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000801 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000802 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000803 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000804 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000805 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000806 TemplArgLocs.reserve(NumTemplateArgLocs);
807 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000808 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000809
810 LAngleLoc = ReadSourceLocation();
811 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000812 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000813
814 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000815
Douglas Gregor4163aca2011-09-09 21:34:22 +0000816 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000817 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000818 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000819 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000820 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000821 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000822 FunctionTemplateSpecializationInfo *FTInfo
823 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
824 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000825 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
826 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000827 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000828 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000829
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000830 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000831 // The template that contains the specializations set. It's not safe to
832 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000833 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000834 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
835 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
836 // FunctionTemplateSpecializationInfo's Profile().
837 // We avoid getASTContext because a decl in the parent hierarchy may
838 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000839 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000840 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000841 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000842 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000843 FunctionTemplateSpecializationInfo *ExistingInfo =
844 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000845 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000846 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
847 else {
848 assert(Reader.getContext().getLangOpts().Modules &&
849 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000850 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000851 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000852 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000853 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000854 }
855 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
856 // Templates.
857 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000858 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000859 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000860 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
861
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000862 // Templates args.
863 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000864 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000865 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000866 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000867 TemplArgs.setLAngleLoc(ReadSourceLocation());
868 TemplArgs.setRAngleLoc(ReadSourceLocation());
869
Douglas Gregor4163aca2011-09-09 21:34:22 +0000870 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000871 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000872 // These are not merged; we don't need to merge redeclarations of dependent
873 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000874 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000875 }
876 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000877
Chris Lattnerca025db2010-05-07 21:43:38 +0000878 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000879 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000880 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000881 Params.reserve(NumParams);
882 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000883 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000884 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000885}
886
Sebastian Redlb3298c32010-08-18 23:56:48 +0000887void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000888 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000889 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000890 // Load the body on-demand. Most clients won't care, because method
891 // definitions rarely show up in headers.
892 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
893 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000894 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
895 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000896 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000897 MD->setInstanceMethod(Record.readInt());
898 MD->setVariadic(Record.readInt());
899 MD->setPropertyAccessor(Record.readInt());
900 MD->setDefined(Record.readInt());
901 MD->IsOverriding = Record.readInt();
902 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000903
David L. Jonesbe1557a2016-12-21 00:17:49 +0000904 MD->IsRedeclaration = Record.readInt();
905 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000906 if (MD->HasRedeclaration)
907 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000908 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000909
David L. Jonesbe1557a2016-12-21 00:17:49 +0000910 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
911 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
912 MD->SetRelatedResultType(Record.readInt());
913 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000914 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
915 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000916 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000917 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000918 Params.reserve(NumParams);
919 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000920 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000921
David L. Jonesbe1557a2016-12-21 00:17:49 +0000922 MD->SelLocsKind = Record.readInt();
923 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000924 SmallVector<SourceLocation, 16> SelLocs;
925 SelLocs.reserve(NumStoredSelLocs);
926 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000927 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000928
929 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000930}
931
Douglas Gregor85f3f952015-07-07 03:57:15 +0000932void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000933 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000934
David L. Jonesbe1557a2016-12-21 00:17:49 +0000935 D->Variance = Record.readInt();
936 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000937 D->VarianceLoc = ReadSourceLocation();
938 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000939}
940
Sebastian Redlb3298c32010-08-18 23:56:48 +0000941void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000942 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000943 CD->setAtStartLoc(ReadSourceLocation());
944 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000945}
946
Douglas Gregor85f3f952015-07-07 03:57:15 +0000947ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000948 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000949 if (numParams == 0)
950 return nullptr;
951
952 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
953 typeParams.reserve(numParams);
954 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000955 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000956 if (!typeParam)
957 return nullptr;
958
959 typeParams.push_back(typeParam);
960 }
961
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000962 SourceLocation lAngleLoc = ReadSourceLocation();
963 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000964
965 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
966 typeParams, rAngleLoc);
967}
968
Manman Renec315f12016-09-09 23:48:27 +0000969void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000970 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000971 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000972 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000973
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000974 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000975 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000976
Manman Renec315f12016-09-09 23:48:27 +0000977 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000978 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000979 SmallVector<ObjCProtocolDecl *, 16> Protocols;
980 Protocols.reserve(NumProtocols);
981 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000982 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000983 SmallVector<SourceLocation, 16> ProtoLocs;
984 ProtoLocs.reserve(NumProtocols);
985 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000986 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +0000987 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
988 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000989
Manman Renec315f12016-09-09 23:48:27 +0000990 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000991 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +0000992 Protocols.clear();
993 Protocols.reserve(NumProtocols);
994 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000995 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +0000996 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
997 Reader.getContext());
998}
999
1000void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1001 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1002 // FIXME: odr checking?
1003}
1004
Sebastian Redlb3298c32010-08-18 23:56:48 +00001005void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001006 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001007 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001008 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001009 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001010
1011 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001012 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001013 // Read the definition.
1014 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001015
David L. Jonesbe1557a2016-12-21 00:17:49 +00001016 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001017 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1018 if (Canon->Data.getPointer()) {
1019 // If we already have a definition, keep the definition invariant and
1020 // merge the data.
1021 MergeDefinitionData(Canon, std::move(ID->data()));
1022 ID->Data = Canon->Data;
1023 } else {
1024 // Set the definition data of the canonical declaration, so other
1025 // redeclarations will see it.
1026 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001027
Manman Renec315f12016-09-09 23:48:27 +00001028 // We will rebuild this list lazily.
1029 ID->setIvarList(nullptr);
1030 }
Craig Toppera13603a2014-05-22 05:54:18 +00001031
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001032 // Note that we have deserialized a definition.
1033 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001034
Douglas Gregor404cdde2012-01-27 01:47:08 +00001035 // Note that we've loaded this Objective-C class.
1036 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001037 } else {
1038 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001039 }
Chris Lattner487412d2009-04-27 05:27:42 +00001040}
1041
Sebastian Redlb3298c32010-08-18 23:56:48 +00001042void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001043 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001044 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001045 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001046 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001047 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001048 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001049}
1050
Graydon Hoaree0a68352017-06-28 18:36:27 +00001051void ASTDeclReader::ReadObjCDefinitionData(
1052 struct ObjCProtocolDecl::DefinitionData &Data) {
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001053
David L. Jonesbe1557a2016-12-21 00:17:49 +00001054 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001055 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1056 ProtoRefs.reserve(NumProtoRefs);
1057 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001058 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001059 SmallVector<SourceLocation, 16> ProtoLocs;
1060 ProtoLocs.reserve(NumProtoRefs);
1061 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001062 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001063 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1064 ProtoLocs.data(), Reader.getContext());
1065}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001066
Graydon Hoaree0a68352017-06-28 18:36:27 +00001067void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1068 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1069 // FIXME: odr checking?
1070}
1071
1072void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1073 RedeclarableResult Redecl = VisitRedeclarable(PD);
1074 VisitObjCContainerDecl(PD);
1075 mergeRedeclarable(PD, Redecl);
1076
1077 if (Record.readInt()) {
1078 // Read the definition.
1079 PD->allocateDefinitionData();
1080
1081 ReadObjCDefinitionData(PD->data());
1082
1083 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1084 if (Canon->Data.getPointer()) {
1085 // If we already have a definition, keep the definition invariant and
1086 // merge the data.
1087 MergeDefinitionData(Canon, std::move(PD->data()));
1088 PD->Data = Canon->Data;
1089 } else {
1090 // Set the definition data of the canonical declaration, so other
1091 // redeclarations will see it.
1092 PD->getCanonicalDecl()->Data = PD->Data;
1093 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001094 // Note that we have deserialized a definition.
1095 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001096 } else {
1097 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001098 }
Chris Lattner487412d2009-04-27 05:27:42 +00001099}
1100
Sebastian Redlb3298c32010-08-18 23:56:48 +00001101void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001102 VisitFieldDecl(FD);
1103}
1104
Sebastian Redlb3298c32010-08-18 23:56:48 +00001105void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001106 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001107 CD->setCategoryNameLoc(ReadSourceLocation());
1108 CD->setIvarLBraceLoc(ReadSourceLocation());
1109 CD->setIvarRBraceLoc(ReadSourceLocation());
1110
Douglas Gregor404cdde2012-01-27 01:47:08 +00001111 // Note that this category has been deserialized. We do this before
1112 // deserializing the interface declaration, so that it will consider this
1113 /// category.
1114 Reader.CategoriesDeserialized.insert(CD);
1115
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001116 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001117 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001118 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001119 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001120 ProtoRefs.reserve(NumProtoRefs);
1121 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001122 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001123 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001124 ProtoLocs.reserve(NumProtoRefs);
1125 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001126 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001127 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001128 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001129}
1130
Sebastian Redlb3298c32010-08-18 23:56:48 +00001131void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001132 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001133 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001134}
1135
Sebastian Redlb3298c32010-08-18 23:56:48 +00001136void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001137 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001138 D->setAtLoc(ReadSourceLocation());
1139 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001140 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001141 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001142 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001143 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001144 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001145 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001146 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001147 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001148 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001149 DeclarationName GetterName = Record.readDeclarationName();
1150 SourceLocation GetterLoc = ReadSourceLocation();
1151 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1152 DeclarationName SetterName = Record.readDeclarationName();
1153 SourceLocation SetterLoc = ReadSourceLocation();
1154 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001155 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1156 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1157 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001158}
1159
Sebastian Redlb3298c32010-08-18 23:56:48 +00001160void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001161 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001162 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001163}
1164
Sebastian Redlb3298c32010-08-18 23:56:48 +00001165void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001166 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001167 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001168}
1169
Sebastian Redlb3298c32010-08-18 23:56:48 +00001170void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001171 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001172 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1173 D->SuperLoc = ReadSourceLocation();
1174 D->setIvarLBraceLoc(ReadSourceLocation());
1175 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001176 D->setHasNonZeroConstructors(Record.readInt());
1177 D->setHasDestructors(Record.readInt());
1178 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001179 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001180 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001181}
1182
Sebastian Redlb3298c32010-08-18 23:56:48 +00001183void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001184 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001185 D->setAtLoc(ReadSourceLocation());
1186 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1187 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1188 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001189 D->setGetterCXXConstructor(Record.readExpr());
1190 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001191}
1192
Sebastian Redlb3298c32010-08-18 23:56:48 +00001193void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001194 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001195 FD->Mutable = Record.readInt();
1196 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001197 FD->InitStorage.setInt(
1198 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1199 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001200 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001201 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001202 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001203 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001204 }
Richard Smith2b013182012-06-10 03:12:00 +00001205 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001206 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001207 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001208 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001209 }
Richard Smith0b87e072013-10-07 08:02:11 +00001210 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001211}
1212
John McCall5e77d762013-04-16 07:28:30 +00001213void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1214 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001215 PD->GetterId = Record.getIdentifierInfo();
1216 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001217}
1218
Francois Pichet783dd6e2010-11-21 06:08:52 +00001219void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1220 VisitValueDecl(FD);
1221
David L. Jonesbe1557a2016-12-21 00:17:49 +00001222 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001223 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001224 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001225
1226 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001227 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001228
1229 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001230}
1231
Larisse Voufo39a1e502013-08-06 01:03:05 +00001232ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001233 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001234 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001235
David L. Jonesbe1557a2016-12-21 00:17:49 +00001236 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1237 VD->VarDeclBits.TSCSpec = Record.readInt();
1238 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001239 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001240 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1241 Record.readInt();
1242 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1243 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1244 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1245 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1246 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1247 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1248 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1249 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1250 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001251 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001252 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001253 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001254 VD->setCachedLinkage(VarLinkage);
1255
1256 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001257 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001258 VD->getLexicalDeclContext()->isFunctionOrMethod())
1259 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001260
David L. Jonesbe1557a2016-12-21 00:17:49 +00001261 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001262 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001263 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001264 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1265 Eval->CheckedICE = true;
1266 Eval->IsICE = Val == 3;
1267 }
1268 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001269
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001270 enum VarKind {
1271 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1272 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001273 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001274 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001275 // Only true variables (not parameters or implicit parameters) can be
1276 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001277 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1278 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001279 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001280 break;
1281 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001282 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001283 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001284 break;
1285 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001286 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001287 TemplateSpecializationKind TSK =
1288 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001289 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001290 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001291 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001292 break;
1293 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001294 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001295
1296 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001297}
1298
Sebastian Redlb3298c32010-08-18 23:56:48 +00001299void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001300 VisitVarDecl(PD);
1301}
1302
Sebastian Redlb3298c32010-08-18 23:56:48 +00001303void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001304 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001305 unsigned isObjCMethodParam = Record.readInt();
1306 unsigned scopeDepth = Record.readInt();
1307 unsigned scopeIndex = Record.readInt();
1308 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001309 if (isObjCMethodParam) {
1310 assert(scopeDepth == 0);
1311 PD->setObjCMethodScopeInfo(scopeIndex);
1312 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1313 } else {
1314 PD->setScopeInfo(scopeDepth, scopeIndex);
1315 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001316 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1317 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1318 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001319 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001320
1321 // FIXME: If this is a redeclaration of a function from another module, handle
1322 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001323}
1324
Richard Smith7b76d812016-08-12 02:21:25 +00001325void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1326 VisitVarDecl(DD);
1327 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1328 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001329 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001330}
1331
1332void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1333 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001334 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001335}
1336
Sebastian Redlb3298c32010-08-18 23:56:48 +00001337void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001338 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001339 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001340 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001341}
1342
Sebastian Redlb3298c32010-08-18 23:56:48 +00001343void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001344 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001345 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001346 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001347 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001348 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001349 Params.reserve(NumParams);
1350 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001351 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001352 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001353
David L. Jonesbe1557a2016-12-21 00:17:49 +00001354 BD->setIsVariadic(Record.readInt());
1355 BD->setBlockMissingReturnType(Record.readInt());
1356 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001357
David L. Jonesbe1557a2016-12-21 00:17:49 +00001358 bool capturesCXXThis = Record.readInt();
1359 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001360 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001361 captures.reserve(numCaptures);
1362 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001363 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001364 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001365 bool byRef = (flags & 1);
1366 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001367 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001368
1369 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1370 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001371 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001372}
1373
Ben Langmuirce914fc2013-05-03 19:20:19 +00001374void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1375 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001376 unsigned ContextParamPos = Record.readInt();
1377 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001378 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001379 for (unsigned I = 0; I < CD->NumParams; ++I) {
1380 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001381 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001382 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001383 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001384 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001385}
1386
Sebastian Redlb3298c32010-08-18 23:56:48 +00001387void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001388 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001389 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001390 D->setExternLoc(ReadSourceLocation());
1391 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001392}
1393
Richard Smith8df390f2016-09-08 23:14:54 +00001394void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1395 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001396 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001397}
1398
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001399void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1400 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001401 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001402}
1403
Sebastian Redlb3298c32010-08-18 23:56:48 +00001404void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001405 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001406 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001407 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001408 D->LocStart = ReadSourceLocation();
1409 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001410
Richard Smith15e32fd2015-03-17 02:23:11 +00001411 // Defer loading the anonymous namespace until we've finished merging
1412 // this namespace; loading it might load a later declaration of the
1413 // same namespace, and we have an invariant that older declarations
1414 // get merged before newer ones try to merge.
1415 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001416 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001417 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001418 } else {
1419 // Link this namespace back to the first declaration, which has already
1420 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001421 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001422 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001423
1424 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001425
1426 if (AnonNamespace) {
1427 // Each module has its own anonymous namespace, which is disjoint from
1428 // any other module's anonymous namespaces, so don't attach the anonymous
1429 // namespace at all.
1430 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001431 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001432 D->setAnonymousNamespace(Anon);
1433 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001434}
1435
Sebastian Redlb3298c32010-08-18 23:56:48 +00001436void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001437 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001438 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001439 D->NamespaceLoc = ReadSourceLocation();
1440 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001441 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001442 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001443 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001444}
1445
Sebastian Redlb3298c32010-08-18 23:56:48 +00001446void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001447 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001448 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001449 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001450 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1451 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001452 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001453 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001454 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001455 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001456}
1457
Richard Smith151c4562016-12-20 21:35:28 +00001458void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1459 VisitNamedDecl(D);
1460 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1461 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1462 for (unsigned I = 0; I != D->NumExpansions; ++I)
1463 Expansions[I] = ReadDeclAs<NamedDecl>();
1464 mergeMergeable(D);
1465}
1466
Sebastian Redlb3298c32010-08-18 23:56:48 +00001467void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001468 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001469 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001470 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1471 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1472 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001473 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001474 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001475 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001476}
1477
Richard Smith5179eb72016-06-28 19:03:57 +00001478void ASTDeclReader::VisitConstructorUsingShadowDecl(
1479 ConstructorUsingShadowDecl *D) {
1480 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001481 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1482 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001483 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001484}
1485
Sebastian Redlb3298c32010-08-18 23:56:48 +00001486void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001487 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001488 D->UsingLoc = ReadSourceLocation();
1489 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001490 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001491 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1492 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001493}
1494
Sebastian Redlb3298c32010-08-18 23:56:48 +00001495void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001496 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001497 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001498 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001499 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001500 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001501 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001502}
1503
Sebastian Redlb3298c32010-08-18 23:56:48 +00001504void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001505 UnresolvedUsingTypenameDecl *D) {
1506 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001507 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001508 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001509 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001510 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001511}
1512
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001513void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001514 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001515 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001516 Data.UserDeclaredConstructor = Record.readInt();
1517 Data.UserDeclaredSpecialMembers = Record.readInt();
1518 Data.Aggregate = Record.readInt();
1519 Data.PlainOldData = Record.readInt();
1520 Data.Empty = Record.readInt();
1521 Data.Polymorphic = Record.readInt();
1522 Data.Abstract = Record.readInt();
1523 Data.IsStandardLayout = Record.readInt();
1524 Data.HasNoNonEmptyBases = Record.readInt();
1525 Data.HasPrivateFields = Record.readInt();
1526 Data.HasProtectedFields = Record.readInt();
1527 Data.HasPublicFields = Record.readInt();
1528 Data.HasMutableFields = Record.readInt();
1529 Data.HasVariantMembers = Record.readInt();
1530 Data.HasOnlyCMembers = Record.readInt();
1531 Data.HasInClassInitializer = Record.readInt();
1532 Data.HasUninitializedReferenceMember = Record.readInt();
1533 Data.HasUninitializedFields = Record.readInt();
1534 Data.HasInheritedConstructor = Record.readInt();
1535 Data.HasInheritedAssignment = Record.readInt();
1536 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1537 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1538 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1539 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1540 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1541 Data.DefaultedDestructorIsDeleted = Record.readInt();
1542 Data.HasTrivialSpecialMembers = Record.readInt();
1543 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1544 Data.HasIrrelevantDestructor = Record.readInt();
1545 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1546 Data.HasDefaultedDefaultConstructor = Record.readInt();
1547 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1548 Data.HasConstexprDefaultConstructor = Record.readInt();
1549 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1550 Data.ComputedVisibleConversions = Record.readInt();
1551 Data.UserProvidedDefaultConstructor = Record.readInt();
1552 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001553 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1554 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001555 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1556 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1557 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001558 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001559 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001560
David Blaikie1ac9c982017-04-11 21:13:37 +00001561 if (Record.readInt()) {
1562 Reader.BodySource[D] = Loc.F->Kind == ModuleKind::MK_MainFile
1563 ? ExternalASTSource::EK_Never
1564 : ExternalASTSource::EK_Always;
1565 }
1566
David L. Jonesbe1557a2016-12-21 00:17:49 +00001567 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001568 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001569 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001570 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001571 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001572 Data.VBases = ReadGlobalOffset();
1573
David L. Jonesb6a8f022016-12-21 04:34:52 +00001574 Record.readUnresolvedSet(Data.Conversions);
1575 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001576 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001577 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001578
Douglas Gregor99ae8062012-02-14 17:54:36 +00001579 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001580 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001581 CXXRecordDecl::LambdaDefinitionData &Lambda
1582 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001583 Lambda.Dependent = Record.readInt();
1584 Lambda.IsGenericLambda = Record.readInt();
1585 Lambda.CaptureDefault = Record.readInt();
1586 Lambda.NumCaptures = Record.readInt();
1587 Lambda.NumExplicitCaptures = Record.readInt();
1588 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001589 Lambda.ContextDecl = ReadDeclID();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001590 Lambda.Captures
1591 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1592 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001593 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001594 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001595 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001596 bool IsImplicit = Record.readInt();
1597 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001598 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001599 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001600 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001601 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001602 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001603 break;
1604 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001605 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001606 VarDecl *Var = ReadDeclAs<VarDecl>();
1607 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001608 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1609 break;
1610 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001611 }
1612 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001613}
1614
Richard Smithcd45dbc2014-04-19 03:48:30 +00001615void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001616 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001617 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001618 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001619 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001620
Richard Smith02793752015-03-27 21:16:39 +00001621 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001622 // Track that we merged the definitions.
1623 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1624 DD.Definition));
1625 Reader.PendingDefinitions.erase(MergeDD.Definition);
1626 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001627 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001628 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1629 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001630 }
1631
Richard Smith2a9e5c52015-02-03 03:32:14 +00001632 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1633 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1634 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001635 // We faked up this definition data because we found a class for which we'd
1636 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001637 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001638 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001639
1640 // Don't change which declaration is the definition; that is required
1641 // to be invariant once we select it.
1642 auto *Def = DD.Definition;
1643 DD = std::move(MergeDD);
1644 DD.Definition = Def;
1645 return;
1646 }
1647
Richard Smithcd45dbc2014-04-19 03:48:30 +00001648 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001649 bool DetectedOdrViolation = false;
1650#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1651#define MATCH_FIELD(Field) \
1652 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1653 OR_FIELD(Field)
1654 MATCH_FIELD(UserDeclaredConstructor)
1655 MATCH_FIELD(UserDeclaredSpecialMembers)
1656 MATCH_FIELD(Aggregate)
1657 MATCH_FIELD(PlainOldData)
1658 MATCH_FIELD(Empty)
1659 MATCH_FIELD(Polymorphic)
1660 MATCH_FIELD(Abstract)
1661 MATCH_FIELD(IsStandardLayout)
1662 MATCH_FIELD(HasNoNonEmptyBases)
1663 MATCH_FIELD(HasPrivateFields)
1664 MATCH_FIELD(HasProtectedFields)
1665 MATCH_FIELD(HasPublicFields)
1666 MATCH_FIELD(HasMutableFields)
1667 MATCH_FIELD(HasVariantMembers)
1668 MATCH_FIELD(HasOnlyCMembers)
1669 MATCH_FIELD(HasInClassInitializer)
1670 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001671 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001672 MATCH_FIELD(HasInheritedConstructor)
1673 MATCH_FIELD(HasInheritedAssignment)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001674 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1675 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1676 MATCH_FIELD(NeedOverloadResolutionForDestructor)
1677 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1678 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1679 MATCH_FIELD(DefaultedDestructorIsDeleted)
1680 OR_FIELD(HasTrivialSpecialMembers)
1681 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1682 MATCH_FIELD(HasIrrelevantDestructor)
1683 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001684 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001685 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1686 OR_FIELD(HasConstexprDefaultConstructor)
1687 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1688 // ComputedVisibleConversions is handled below.
1689 MATCH_FIELD(UserProvidedDefaultConstructor)
1690 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001691 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1692 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001693 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1694 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1695 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1696 MATCH_FIELD(IsLambda)
1697#undef OR_FIELD
1698#undef MATCH_FIELD
1699
1700 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1701 DetectedOdrViolation = true;
1702 // FIXME: Issue a diagnostic if the base classes don't match when we come
1703 // to lazily load them.
1704
1705 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1706 // match when we come to lazily load them.
1707 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1708 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1709 DD.ComputedVisibleConversions = true;
1710 }
1711
1712 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1713 // lazily load it.
1714
1715 if (DD.IsLambda) {
1716 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1717 // when they occur within the body of a function template specialization).
1718 }
1719
Richard Trieufd1acbb2017-04-11 21:31:00 +00001720 if (D->getODRHash() != MergeDD.ODRHash) {
1721 DetectedOdrViolation = true;
1722 }
1723
Richard Smithcd45dbc2014-04-19 03:48:30 +00001724 if (DetectedOdrViolation)
1725 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1726}
1727
Richard Smith2a9e5c52015-02-03 03:32:14 +00001728void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001729 struct CXXRecordDecl::DefinitionData *DD;
1730 ASTContext &C = Reader.getContext();
1731
1732 // Determine whether this is a lambda closure type, so that we can
1733 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001734 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001735 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001736 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001737 LCD_None);
1738 else
1739 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1740
David Blaikie1ac9c982017-04-11 21:13:37 +00001741 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001742
Richard Smith5156c382015-01-22 01:41:56 +00001743 // We might already have a definition for this record. This can happen either
1744 // because we're reading an update record, or because we've already done some
1745 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001746 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001747 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001748 MergeDefinitionData(Canon, std::move(*DD));
1749 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001750 return;
1751 }
1752
Richard Smith97100e32015-06-20 00:22:34 +00001753 // Mark this declaration as being a definition.
1754 D->IsCompleteDefinition = true;
1755 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001756
Richard Smith97100e32015-06-20 00:22:34 +00001757 // If this is not the first declaration or is an update record, we can have
1758 // other redeclarations already. Make a note that we need to propagate the
1759 // DefinitionData pointer onto them.
1760 if (Update || Canon != D) {
1761 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001762 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001763 }
1764}
1765
Richard Smith1d209d02013-05-23 01:49:11 +00001766ASTDeclReader::RedeclarableResult
1767ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1768 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001769
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001770 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001771
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001772 enum CXXRecKind {
1773 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1774 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001775 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001776 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001777 // Merged when we merge the folding set entry in the primary template.
1778 if (!isa<ClassTemplateSpecializationDecl>(D))
1779 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001780 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001781 case CXXRecTemplate: {
1782 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001783 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001784 D->TemplateOrInstantiation = Template;
1785 if (!Template->getTemplatedDecl()) {
1786 // We've not actually loaded the ClassTemplateDecl yet, because we're
1787 // currently being loaded as its pattern. Rely on it to set up our
1788 // TypeForDecl (see VisitClassTemplateDecl).
1789 //
1790 // Beware: we do not yet know our canonical declaration, and may still
1791 // get merged once the surrounding class template has got off the ground.
1792 TypeIDForTypeDecl = 0;
1793 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001794 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001795 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001796 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001797 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001798 TemplateSpecializationKind TSK =
1799 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001800 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001801 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1802 MSI->setPointOfInstantiation(POI);
1803 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001804 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001805 break;
1806 }
1807 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001808
David L. Jonesbe1557a2016-12-21 00:17:49 +00001809 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001810 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001811 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001812 else
1813 // Propagate DefinitionData pointer from the canonical declaration.
1814 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1815
Richard Smith676c4042013-08-29 23:59:27 +00001816 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001817 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001818 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001819 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001820 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001821 // FIXME: This is wrong for the ARM ABI, where some other module may have
1822 // made this function no longer be a key function. We need an update
1823 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001824 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001825 }
Richard Smith1d209d02013-05-23 01:49:11 +00001826
1827 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001828}
1829
Richard Smithbc491202017-02-17 20:05:37 +00001830void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1831 VisitFunctionDecl(D);
1832}
1833
Sebastian Redlb3298c32010-08-18 23:56:48 +00001834void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001835 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001836
David L. Jonesbe1557a2016-12-21 00:17:49 +00001837 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001838 if (D->isCanonicalDecl()) {
1839 while (NumOverridenMethods--) {
1840 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1841 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001842 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001843 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1844 }
1845 } else {
1846 // We don't care about which declarations this used to override; we get
1847 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001848 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001849 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001850}
1851
Sebastian Redlb3298c32010-08-18 23:56:48 +00001852void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001853 // We need the inherited constructor information to merge the declaration,
1854 // so we have to read it before we call VisitCXXMethodDecl.
1855 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001856 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1857 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001858 *D->getTrailingObjects<InheritedConstructor>() =
1859 InheritedConstructor(Shadow, Ctor);
1860 }
1861
Chris Lattnerca025db2010-05-07 21:43:38 +00001862 VisitCXXMethodDecl(D);
1863}
1864
Sebastian Redlb3298c32010-08-18 23:56:48 +00001865void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001866 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001867
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001868 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001869 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1870 // FIXME: Check consistency if we have an old and new operator delete.
1871 if (!Canon->OperatorDelete)
1872 Canon->OperatorDelete = OperatorDelete;
1873 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001874}
1875
Sebastian Redlb3298c32010-08-18 23:56:48 +00001876void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001877 VisitCXXMethodDecl(D);
1878}
1879
Douglas Gregorba345522011-12-02 23:23:56 +00001880void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1881 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001882 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001883 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001884 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001885 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001886 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001887 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001888}
1889
Sebastian Redlb3298c32010-08-18 23:56:48 +00001890void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001891 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001892 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001893}
1894
Sebastian Redlb3298c32010-08-18 23:56:48 +00001895void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001896 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001897 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001898 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001899 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001900 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001901 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001902 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001903 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001904 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001905 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001906 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001907}
1908
Sebastian Redlb3298c32010-08-18 23:56:48 +00001909void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001910 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001911 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001912 D->NumParams = NumParams;
1913 D->Params = new TemplateParameterList*[NumParams];
1914 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001915 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001916 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001917 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001918 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001919 D->Friend = GetTypeSourceInfo();
1920 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001921}
1922
Richard Smithf17fdbd2014-04-24 02:25:27 +00001923DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001924 VisitNamedDecl(D);
1925
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001926 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001927 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001928 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001929 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001930 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001931
Richard Smithf17fdbd2014-04-24 02:25:27 +00001932 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001933}
1934
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001935ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001936ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001937 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001938
Douglas Gregor68444de2012-01-14 15:13:49 +00001939 // Make sure we've allocated the Common pointer first. We do this before
1940 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1941 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1942 if (!CanonD->Common) {
1943 CanonD->Common = CanonD->newCommon(Reader.getContext());
1944 Reader.PendingDefinitions.insert(CanonD);
1945 }
1946 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001947
Douglas Gregor68444de2012-01-14 15:13:49 +00001948 // If this is the first declaration of the template, fill in the information
1949 // for the 'common' pointer.
1950 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001951 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001952 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001953 assert(RTD->getKind() == D->getKind() &&
1954 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001955 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001956 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001957 D->setMemberSpecialization();
1958 }
1959 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001960
Richard Smithf17fdbd2014-04-24 02:25:27 +00001961 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001962 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001963
Richard Smithf17fdbd2014-04-24 02:25:27 +00001964 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001965
Richard Smithd46d6de2013-10-13 23:50:45 +00001966 // If we merged the template with a prior declaration chain, merge the common
1967 // pointer.
1968 // FIXME: Actually merge here, don't just overwrite.
1969 D->Common = D->getCanonicalDecl()->Common;
1970
Douglas Gregor68444de2012-01-14 15:13:49 +00001971 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001972}
1973
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00001974static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1975 SmallVectorImpl<DeclID> &IDs) {
1976 assert(!IDs.empty() && "no IDs to add to list");
1977 if (Old) {
1978 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1979 std::sort(IDs.begin(), IDs.end());
1980 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1981 }
1982
1983 auto *Result = new (Context) DeclID[1 + IDs.size()];
1984 *Result = IDs.size();
1985 std::copy(IDs.begin(), IDs.end(), Result + 1);
1986 return Result;
1987}
1988
Sebastian Redlb3298c32010-08-18 23:56:48 +00001989void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001990 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001991
Douglas Gregor68444de2012-01-14 15:13:49 +00001992 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001993 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1994 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001995 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001996 ReadDeclIDList(SpecIDs);
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00001997
1998 if (!SpecIDs.empty()) {
1999 auto *CommonPtr = D->getCommonPtr();
2000 CommonPtr->LazySpecializations = newDeclIDList(
2001 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
2002 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002003 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002004
2005 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2006 // We were loaded before our templated declaration was. We've not set up
2007 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2008 // it now.
2009 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002010 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002011 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002012}
2013
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002014void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2015 llvm_unreachable("BuiltinTemplates are not serialized");
2016}
2017
Larisse Voufo30616382013-08-23 22:21:36 +00002018/// TODO: Unify with ClassTemplateDecl version?
2019/// May require unifying ClassTemplateDecl and
2020/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002021void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2022 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2023
2024 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002025 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002026 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002027 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002028 ReadDeclIDList(SpecIDs);
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00002029
2030 if (!SpecIDs.empty()) {
2031 auto *CommonPtr = D->getCommonPtr();
2032 CommonPtr->LazySpecializations = newDeclIDList(
2033 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
2034 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00002035 }
2036}
2037
Richard Smith1d209d02013-05-23 01:49:11 +00002038ASTDeclReader::RedeclarableResult
2039ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2040 ClassTemplateSpecializationDecl *D) {
2041 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002042
Douglas Gregor4163aca2011-09-09 21:34:22 +00002043 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002044 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002045 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002046 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002047 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002048 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002049 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002050 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002051 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002052 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2053 = new (C) ClassTemplateSpecializationDecl::
2054 SpecializedPartialSpecialization();
2055 PS->PartialSpecialization
2056 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2057 PS->TemplateArgs = ArgList;
2058 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002059 }
2060 }
2061
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002062 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002063 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002064 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002065 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002066 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002067
David L. Jonesbe1557a2016-12-21 00:17:49 +00002068 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002069 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002070 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002071 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002072 // Set this as, or find, the canonical declaration for this specialization
2073 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002074 if (ClassTemplatePartialSpecializationDecl *Partial =
2075 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002076 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002077 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002078 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002079 CanonSpec =
2080 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2081 }
2082 // If there was already a canonical specialization, merge into it.
2083 if (CanonSpec != D) {
2084 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2085
2086 // This declaration might be a definition. Merge with any existing
2087 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002088 if (auto *DDD = D->DefinitionData) {
2089 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002090 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002091 else
Richard Smith053f6c62014-05-16 23:01:30 +00002092 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002093 }
Richard Smith547864d2014-07-11 18:22:58 +00002094 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002095 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002096 }
2097 }
Richard Smith1d209d02013-05-23 01:49:11 +00002098
Richard Smithd55889a2013-09-09 16:55:27 +00002099 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002100 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002101 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2102 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2103 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002104 ExplicitInfo->ExternLoc = ReadSourceLocation();
2105 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002106 D->ExplicitInfo = ExplicitInfo;
2107 }
2108
Richard Smith1d209d02013-05-23 01:49:11 +00002109 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002110}
2111
Sebastian Redlb3298c32010-08-18 23:56:48 +00002112void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002113 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002114 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002115
David L. Jonesb6a8f022016-12-21 04:34:52 +00002116 D->TemplateParams = Record.readTemplateParameterList();
2117 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002118
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002119 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002120 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002121 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002122 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002123 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002124 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002125}
2126
Sebastian Redlb7448632011-08-31 13:59:56 +00002127void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2128 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002129 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002130 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002131}
2132
Sebastian Redlb3298c32010-08-18 23:56:48 +00002133void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002134 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002135
Douglas Gregor68444de2012-01-14 15:13:49 +00002136 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002137 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002138 SmallVector<serialization::DeclID, 32> SpecIDs;
2139 ReadDeclIDList(SpecIDs);
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00002140
2141 if (!SpecIDs.empty()) {
2142 auto *CommonPtr = D->getCommonPtr();
2143 CommonPtr->LazySpecializations = newDeclIDList(
2144 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
2145 }
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002146 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002147}
2148
Larisse Voufo30616382013-08-23 22:21:36 +00002149/// TODO: Unify with ClassTemplateSpecializationDecl version?
2150/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2151/// VarTemplate(Partial)SpecializationDecl with a new data
2152/// structure Template(Partial)SpecializationDecl, and
2153/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002154ASTDeclReader::RedeclarableResult
2155ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2156 VarTemplateSpecializationDecl *D) {
2157 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2158
2159 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002160 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002161 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2162 D->SpecializedTemplate = VTD;
2163 } else {
2164 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002165 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002166 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002167 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002168 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2169 new (C)
2170 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2171 PS->PartialSpecialization =
2172 cast<VarTemplatePartialSpecializationDecl>(InstD);
2173 PS->TemplateArgs = ArgList;
2174 D->SpecializedTemplate = PS;
2175 }
2176 }
2177
2178 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002179 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002180 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2181 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2182 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002183 ExplicitInfo->ExternLoc = ReadSourceLocation();
2184 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002185 D->ExplicitInfo = ExplicitInfo;
2186 }
2187
2188 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002189 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002190 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002191 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002192 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002193
David L. Jonesbe1557a2016-12-21 00:17:49 +00002194 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002195 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002196 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002197 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002198 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002199 if (VarTemplatePartialSpecializationDecl *Partial =
2200 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002201 CanonPattern->getCommonPtr()->PartialSpecializations
2202 .GetOrInsertNode(Partial);
2203 } else {
2204 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2205 }
2206 }
2207 }
2208
2209 return Redecl;
2210}
2211
Larisse Voufo30616382013-08-23 22:21:36 +00002212/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2213/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2214/// VarTemplate(Partial)SpecializationDecl with a new data
2215/// structure Template(Partial)SpecializationDecl, and
2216/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002217void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2218 VarTemplatePartialSpecializationDecl *D) {
2219 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2220
David L. Jonesb6a8f022016-12-21 04:34:52 +00002221 D->TemplateParams = Record.readTemplateParameterList();
2222 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002223
2224 // These are read/set from/to the first declaration.
2225 if (ThisDeclID == Redecl.getFirstID()) {
2226 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002227 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002228 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002229 }
2230}
2231
Sebastian Redlb3298c32010-08-18 23:56:48 +00002232void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002233 VisitTypeDecl(D);
2234
David L. Jonesbe1557a2016-12-21 00:17:49 +00002235 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002236
David L. Jonesbe1557a2016-12-21 00:17:49 +00002237 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002238 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002239}
2240
Sebastian Redlb3298c32010-08-18 23:56:48 +00002241void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002242 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002243 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002244 D->setDepth(Record.readInt());
2245 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002246 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002247 auto TypesAndInfos =
2248 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002249 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002250 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002251 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002252 }
2253 } else {
2254 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002255 D->ParameterPack = Record.readInt();
2256 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002257 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002258 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002259}
2260
Sebastian Redlb3298c32010-08-18 23:56:48 +00002261void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002262 VisitTemplateDecl(D);
2263 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002264 D->setDepth(Record.readInt());
2265 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002266 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002267 TemplateParameterList **Data =
2268 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002269 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2270 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002271 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002272 } else {
2273 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002274 D->ParameterPack = Record.readInt();
2275 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002276 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002277 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002278 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002279}
2280
Richard Smith3f1b5d02011-05-05 21:57:07 +00002281void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2282 VisitRedeclarableTemplateDecl(D);
2283}
2284
Sebastian Redlb3298c32010-08-18 23:56:48 +00002285void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002286 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002287 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002288 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002289 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002290 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002291}
2292
Michael Han84324352013-02-22 17:15:32 +00002293void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2294 VisitDecl(D);
2295}
2296
Mike Stump11289f42009-09-09 15:08:12 +00002297std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002298ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002299 uint64_t LexicalOffset = ReadLocalOffset();
2300 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002301 return std::make_pair(LexicalOffset, VisibleOffset);
2302}
2303
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002304template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002305ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002306ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002307 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002308 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002309
Richard Smith5fc18a92015-07-12 23:43:21 +00002310 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002311 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002312
Richard Smithd61d4ac2015-08-22 20:13:39 +00002313 uint64_t RedeclOffset = 0;
2314
Douglas Gregor358cd442012-01-15 16:58:34 +00002315 // 0 indicates that this declaration was the only declaration of its entity,
2316 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002317 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002318 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002319 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002320 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002321 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002322 // This declaration was the first local declaration, but may have imported
2323 // other declarations.
2324 IsKeyDecl = N == 1;
2325 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002326
Richard Smithfe620d22015-03-05 23:24:12 +00002327 // We have some declarations that must be before us in our redeclaration
2328 // chain. Read them now, and remember that we ought to merge with one of
2329 // them.
2330 // FIXME: Provide a known merge target to the second and subsequent such
2331 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002332 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002333 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002334
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002335 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002336 } else {
2337 // This declaration was not the first local declaration. Read the first
2338 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002339 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002340 }
2341
Douglas Gregor358cd442012-01-15 16:58:34 +00002342 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2343 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002344 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2345 // We temporarily set the first (canonical) declaration as the previous one
2346 // which is the one that matters and mark the real previous DeclID to be
2347 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002348 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002349 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002350 }
Richard Smithd8a83712015-08-22 01:47:18 +00002351
Richard Smithd8a83712015-08-22 01:47:18 +00002352 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002353
2354 // Note that we need to load local redeclarations of this decl and build a
2355 // decl chain for them. This must happen *after* we perform the preloading
2356 // above; this ensures that the redeclaration chain is built in the correct
2357 // order.
2358 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002359 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002360
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002361 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002362}
2363
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002364/// \brief Attempts to merge the given declaration (D) with another declaration
2365/// of the same entity.
2366template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002367void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002368 RedeclarableResult &Redecl,
2369 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002370 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002371 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002372 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002373
Richard Smith202850a2015-03-10 02:57:50 +00002374 // If we're not the canonical declaration, we don't need to merge.
2375 if (!DBase->isFirstDecl())
2376 return;
2377
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002378 T *D = static_cast<T*>(DBase);
2379
Richard Smith5a4737c2015-02-06 02:42:59 +00002380 if (auto *Existing = Redecl.getKnownMergeTarget())
2381 // We already know of an existing declaration we should merge with.
2382 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2383 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002384 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002385 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2386}
2387
2388/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2389/// We use this to put code in a template that will only be valid for certain
2390/// instantiations.
2391template<typename T> static T assert_cast(T t) { return t; }
2392template<typename T> static T assert_cast(...) {
2393 llvm_unreachable("bad assert_cast");
2394}
2395
2396/// \brief Merge together the pattern declarations from two template
2397/// declarations.
2398void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2399 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002400 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002401 auto *DPattern = D->getTemplatedDecl();
2402 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002403 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2404 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002405 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002406
Richard Smith547864d2014-07-11 18:22:58 +00002407 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2408 // Merge with any existing definition.
2409 // FIXME: This is duplicated in several places. Refactor.
2410 auto *ExistingClass =
2411 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002412 if (auto *DDD = DClass->DefinitionData) {
2413 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002414 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002415 } else {
2416 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002417 // We may have skipped this before because we thought that DClass
2418 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002419 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002420 }
2421 }
2422 DClass->DefinitionData = ExistingClass->DefinitionData;
2423
Richard Smithf17fdbd2014-04-24 02:25:27 +00002424 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2425 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002426 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002427 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2428 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2429 Result);
2430 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2431 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002432 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2433 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2434 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002435 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002436}
2437
2438/// \brief Attempts to merge the given declaration (D) with another declaration
2439/// of the same entity.
2440template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002441void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2442 RedeclarableResult &Redecl,
2443 DeclID TemplatePatternID) {
2444 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002445 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002446 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002447 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002448 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2449 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002450
Richard Smithd55889a2013-09-09 16:55:27 +00002451 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002452 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002453 // appropriate canonical declaration.
2454 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002455 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002456 ExistingCanon->Used |= D->Used;
2457 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002458
2459 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002460 // We cannot have loaded any redeclarations of this declaration yet, so
2461 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002462 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002463 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002464 assert_cast<NamespaceDecl*>(ExistingCanon));
2465
2466 // When we merge a template, merge its pattern.
2467 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2468 mergeTemplatePattern(
2469 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002470 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002471
Richard Smith5fc18a92015-07-12 23:43:21 +00002472 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002473 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002474 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002475 }
2476}
2477
Richard Smith0b87e072013-10-07 08:02:11 +00002478/// \brief Attempts to merge the given declaration (D) with another declaration
2479/// of the same entity, for the case where the entity is not actually
2480/// redeclarable. This happens, for instance, when merging the fields of
2481/// identical class definitions from two different modules.
2482template<typename T>
2483void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2484 // If modules are not available, there is no reason to perform this merge.
2485 if (!Reader.getContext().getLangOpts().Modules)
2486 return;
2487
Richard Smith01a73372013-10-15 22:02:41 +00002488 // ODR-based merging is only performed in C++. In C, identically-named things
2489 // in different translation units are not redeclarations (but may still have
2490 // compatible types).
2491 if (!Reader.getContext().getLangOpts().CPlusPlus)
2492 return;
2493
Richard Smith0b87e072013-10-07 08:02:11 +00002494 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2495 if (T *Existing = ExistingRes)
2496 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2497 Existing->getCanonicalDecl());
2498}
2499
Alexey Bataeva769e072013-03-22 06:34:35 +00002500void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2501 VisitDecl(D);
2502 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002503 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002504 Vars.reserve(NumVars);
2505 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002506 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002507 }
2508 D->setVars(Vars);
2509}
2510
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002511void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002512 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002513 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002514 D->setCombiner(Record.readExpr());
2515 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002516 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002517}
2518
Alexey Bataev4244be22016-02-11 05:35:55 +00002519void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002520 VisitVarDecl(D);
2521}
2522
Chris Lattner487412d2009-04-27 05:27:42 +00002523//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002524// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002525//===----------------------------------------------------------------------===//
2526
Chris Lattner8f63ab52009-04-27 06:01:06 +00002527/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002528void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2529 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002530 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002531 attr::Kind Kind = (attr::Kind)Record.readInt();
2532 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002533
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002534#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002535
2536 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002537 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002538 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002539}
2540
2541//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002542// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002543//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002544
2545/// \brief Note that we have loaded the declaration with the given
2546/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002547///
Chris Lattner487412d2009-04-27 05:27:42 +00002548/// This routine notes that this declaration has already been loaded,
2549/// so that future GetDecl calls will return this declaration rather
2550/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002551inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002552 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2553 DeclsLoaded[Index] = D;
2554}
2555
2556
2557/// \brief Determine whether the consumer will be interested in seeing
2558/// this declaration (via HandleTopLevelDecl).
2559///
2560/// This routine should return true for anything that might affect
2561/// code generation, e.g., inline function definitions, Objective-C
2562/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002563static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002564 // An ObjCMethodDecl is never considered as "interesting" because its
2565 // implementation container always is.
2566
Richard Smithdc1f0422016-07-20 19:10:16 +00002567 // An ImportDecl or VarDecl imported from a module will get emitted when
2568 // we import the relevant module.
Bruno Cardoso Lopes84df6e99b2017-03-01 19:18:42 +00002569 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && D->getImportedOwningModule() &&
2570 Ctx.DeclMustBeEmitted(D))
Richard Smithdc1f0422016-07-20 19:10:16 +00002571 return false;
2572
Douglas Gregor87d81242011-09-10 00:22:34 +00002573 if (isa<FileScopeAsmDecl>(D) ||
2574 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002575 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002576 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002577 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002578 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002579 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002580 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2581 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002582 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002583 return Var->isFileVarDecl() &&
2584 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002585 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002586 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002587
2588 if (auto *ES = D->getASTContext().getExternalSource())
2589 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2590 return true;
2591
Douglas Gregor87d81242011-09-10 00:22:34 +00002592 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002593}
2594
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002595/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002596ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002597ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002598 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2599 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002600 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002601 const DeclOffset &DOffs =
2602 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002603 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002604 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002605}
2606
Douglas Gregord32f0352011-07-22 06:10:01 +00002607ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002608 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002609 = GlobalBitOffsetsMap.find(GlobalOffset);
2610
2611 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2612 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2613}
2614
Douglas Gregorde3ef502011-11-30 23:21:26 +00002615uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002616 return LocalOffset + M.GlobalBitOffset;
2617}
2618
Richard Smithbf78e642013-06-24 22:51:00 +00002619static bool isSameTemplateParameterList(const TemplateParameterList *X,
2620 const TemplateParameterList *Y);
2621
2622/// \brief Determine whether two template parameters are similar enough
2623/// that they may be used in declarations of the same template.
2624static bool isSameTemplateParameter(const NamedDecl *X,
2625 const NamedDecl *Y) {
2626 if (X->getKind() != Y->getKind())
2627 return false;
2628
2629 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2630 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2631 return TX->isParameterPack() == TY->isParameterPack();
2632 }
2633
2634 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2635 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2636 return TX->isParameterPack() == TY->isParameterPack() &&
2637 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2638 }
2639
2640 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2641 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2642 return TX->isParameterPack() == TY->isParameterPack() &&
2643 isSameTemplateParameterList(TX->getTemplateParameters(),
2644 TY->getTemplateParameters());
2645}
2646
Richard Smith32952e12014-10-14 02:00:47 +00002647static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2648 if (auto *NS = X->getAsNamespace())
2649 return NS;
2650 if (auto *NAS = X->getAsNamespaceAlias())
2651 return NAS->getNamespace();
2652 return nullptr;
2653}
2654
2655static bool isSameQualifier(const NestedNameSpecifier *X,
2656 const NestedNameSpecifier *Y) {
2657 if (auto *NSX = getNamespace(X)) {
2658 auto *NSY = getNamespace(Y);
2659 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2660 return false;
2661 } else if (X->getKind() != Y->getKind())
2662 return false;
2663
2664 // FIXME: For namespaces and types, we're permitted to check that the entity
2665 // is named via the same tokens. We should probably do so.
2666 switch (X->getKind()) {
2667 case NestedNameSpecifier::Identifier:
2668 if (X->getAsIdentifier() != Y->getAsIdentifier())
2669 return false;
2670 break;
2671 case NestedNameSpecifier::Namespace:
2672 case NestedNameSpecifier::NamespaceAlias:
2673 // We've already checked that we named the same namespace.
2674 break;
2675 case NestedNameSpecifier::TypeSpec:
2676 case NestedNameSpecifier::TypeSpecWithTemplate:
2677 if (X->getAsType()->getCanonicalTypeInternal() !=
2678 Y->getAsType()->getCanonicalTypeInternal())
2679 return false;
2680 break;
2681 case NestedNameSpecifier::Global:
2682 case NestedNameSpecifier::Super:
2683 return true;
2684 }
2685
2686 // Recurse into earlier portion of NNS, if any.
2687 auto *PX = X->getPrefix();
2688 auto *PY = Y->getPrefix();
2689 if (PX && PY)
2690 return isSameQualifier(PX, PY);
2691 return !PX && !PY;
2692}
2693
Richard Smithbf78e642013-06-24 22:51:00 +00002694/// \brief Determine whether two template parameter lists are similar enough
2695/// that they may be used in declarations of the same template.
2696static bool isSameTemplateParameterList(const TemplateParameterList *X,
2697 const TemplateParameterList *Y) {
2698 if (X->size() != Y->size())
2699 return false;
2700
2701 for (unsigned I = 0, N = X->size(); I != N; ++I)
2702 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2703 return false;
2704
2705 return true;
2706}
2707
George Burgess IV95845082017-02-15 22:43:27 +00002708/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002709/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002710static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2711 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002712 // Note that pass_object_size attributes are represented in the function's
2713 // ExtParameterInfo, so we don't need to check them here.
2714
George Burgess IV95845082017-02-15 22:43:27 +00002715 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2716 // Since this is an equality check, we can ignore that enable_if attrs show up
2717 // in reverse order.
2718 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2719 AEnableIfs.push_back(EIA);
2720
2721 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2722 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2723 BEnableIfs.push_back(EIA);
2724
2725 // Two very common cases: either we have 0 enable_if attrs, or we have an
2726 // unequal number of enable_if attrs.
2727 if (AEnableIfs.empty() && BEnableIfs.empty())
2728 return true;
2729
2730 if (AEnableIfs.size() != BEnableIfs.size())
2731 return false;
2732
2733 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2734 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2735 Cand1ID.clear();
2736 Cand2ID.clear();
2737
2738 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2739 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2740 if (Cand1ID != Cand2ID)
2741 return false;
2742 }
2743
George Burgess IV95845082017-02-15 22:43:27 +00002744 return true;
2745}
2746
Douglas Gregor022857e2011-12-22 01:48:48 +00002747/// \brief Determine whether the two declarations refer to the same entity.
2748static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2749 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002750
Douglas Gregor022857e2011-12-22 01:48:48 +00002751 if (X == Y)
2752 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002753
Douglas Gregor022857e2011-12-22 01:48:48 +00002754 // Must be in the same context.
2755 if (!X->getDeclContext()->getRedeclContext()->Equals(
2756 Y->getDeclContext()->getRedeclContext()))
2757 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002758
2759 // Two typedefs refer to the same entity if they have the same underlying
2760 // type.
2761 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2762 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2763 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2764 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002765
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002766 // Must have the same kind.
2767 if (X->getKind() != Y->getKind())
2768 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002769
Douglas Gregorda389302012-01-01 21:47:52 +00002770 // Objective-C classes and protocols with the same name always match.
2771 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002772 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002773
2774 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002775 // No need to handle these here: we merge them when adding them to the
2776 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002777 return false;
2778 }
Richard Smithd55889a2013-09-09 16:55:27 +00002779
Douglas Gregor2009cee2012-01-03 22:46:00 +00002780 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002781 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2782 TagDecl *TagY = cast<TagDecl>(Y);
2783 return (TagX->getTagKind() == TagY->getTagKind()) ||
2784 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2785 TagX->getTagKind() == TTK_Interface) &&
2786 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2787 TagY->getTagKind() == TTK_Interface));
2788 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002789
Joao Matose9a3ed42012-08-31 22:18:20 +00002790 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002791 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2792 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002793 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2794 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002795 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2796 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2797 if (CtorX->getInheritedConstructor() &&
2798 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2799 CtorY->getInheritedConstructor().getConstructor()))
2800 return false;
2801 }
Richard Smitha54d3242017-03-08 23:00:26 +00002802 ASTContext &C = FuncX->getASTContext();
2803 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) {
2804 // We can get functions with different types on the redecl chain in C++17
2805 // if they have differing exception specifications and at least one of
2806 // the excpetion specs is unresolved.
2807 // FIXME: Do we need to check for C++14 deduced return types here too?
2808 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>();
2809 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>();
2810 if (C.getLangOpts().CPlusPlus1z && XFPT && YFPT &&
2811 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2812 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
2813 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(),
2814 FuncY->getType()))
2815 return true;
2816 return false;
2817 }
George Burgess IV95845082017-02-15 22:43:27 +00002818 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002819 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002820 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002821
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002822 // Variables with the same type and linkage match.
2823 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2824 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002825 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2826 ASTContext &C = VarX->getASTContext();
2827 if (C.hasSameType(VarX->getType(), VarY->getType()))
2828 return true;
2829
2830 // We can get decls with different types on the redecl chain. Eg.
2831 // template <typename T> struct S { static T Var[]; }; // #1
2832 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2833 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002834 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002835 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2836 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2837 if (!VarXTy || !VarYTy)
2838 return false;
2839 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2840 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2841 }
2842 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002843 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002844
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002845 // Namespaces with the same name and inlinedness match.
2846 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2847 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2848 return NamespaceX->isInline() == NamespaceY->isInline();
2849 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002850
Richard Smith8f8f05c2013-06-24 04:45:28 +00002851 // Identical template names and kinds match if their template parameter lists
2852 // and patterns match.
2853 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002854 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002855 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002856 TemplateY->getTemplatedDecl()) &&
2857 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2858 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002859 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002860
Richard Smith0b87e072013-10-07 08:02:11 +00002861 // Fields with the same name and the same type match.
2862 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2863 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002864 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002865 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2866 }
2867
Richard Smith8cbd8952015-08-04 02:05:09 +00002868 // Indirect fields with the same target field match.
2869 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2870 auto *IFDY = cast<IndirectFieldDecl>(Y);
2871 return IFDX->getAnonField()->getCanonicalDecl() ==
2872 IFDY->getAnonField()->getCanonicalDecl();
2873 }
2874
Richard Smith01a73372013-10-15 22:02:41 +00002875 // Enumerators with the same name match.
2876 if (isa<EnumConstantDecl>(X))
2877 // FIXME: Also check the value is odr-equivalent.
2878 return true;
2879
Richard Smithfd8634a2013-10-23 02:17:46 +00002880 // Using shadow declarations with the same target match.
2881 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2882 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2883 return USX->getTargetDecl() == USY->getTargetDecl();
2884 }
2885
Richard Smith32952e12014-10-14 02:00:47 +00002886 // Using declarations with the same qualifier match. (We already know that
2887 // the name matches.)
2888 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2889 auto *UY = cast<UsingDecl>(Y);
2890 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2891 UX->hasTypename() == UY->hasTypename() &&
2892 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2893 }
2894 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2895 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2896 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2897 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2898 }
2899 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2900 return isSameQualifier(
2901 UX->getQualifier(),
2902 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2903
Richard Smithf4634362014-09-03 23:11:22 +00002904 // Namespace alias definitions with the same target match.
2905 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2906 auto *NAY = cast<NamespaceAliasDecl>(Y);
2907 return NAX->getNamespace()->Equals(NAY->getNamespace());
2908 }
2909
Douglas Gregor022857e2011-12-22 01:48:48 +00002910 return false;
2911}
2912
Richard Smithd55889a2013-09-09 16:55:27 +00002913/// Find the context in which we should search for previous declarations when
2914/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002915DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2916 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002917 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2918 return ND->getOriginalNamespace();
2919
Richard Smith8a639892015-01-24 01:07:20 +00002920 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2921 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002922 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002923 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002924 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002925
2926 // If there's no definition yet, then DC's definition is added by an update
2927 // record, but we've not yet loaded that update record. In this case, we
2928 // commit to DC being the canonical definition now, and will fix this when
2929 // we load the update record.
2930 if (!DD) {
2931 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2932 RD->IsCompleteDefinition = true;
2933 RD->DefinitionData = DD;
2934 RD->getCanonicalDecl()->DefinitionData = DD;
2935
2936 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002937 Reader.PendingFakeDefinitionData.insert(
2938 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002939 }
2940
2941 return DD->Definition;
2942 }
Richard Smithd55889a2013-09-09 16:55:27 +00002943
Richard Smith01a73372013-10-15 22:02:41 +00002944 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002945 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2946 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002947
Richard Smith4eca9b92015-02-04 23:37:59 +00002948 // We can see the TU here only if we have no Sema object. In that case,
2949 // there's no TU scope to look in, so using the DC alone is sufficient.
2950 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2951 return TU;
2952
Craig Toppera13603a2014-05-22 05:54:18 +00002953 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002954}
2955
Douglas Gregor022857e2011-12-22 01:48:48 +00002956ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002957 // Record that we had a typedef name for linkage whether or not we merge
2958 // with that declaration.
2959 if (TypedefNameForLinkage) {
2960 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2961 Reader.ImportedTypedefNamesForLinkage.insert(
2962 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2963 return;
2964 }
2965
Douglas Gregor9b47f942012-01-09 17:38:47 +00002966 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002967 return;
Richard Smith95d99302013-07-13 02:00:19 +00002968
Richard Smith70d58502014-08-30 00:04:23 +00002969 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002970 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002971 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002972 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2973 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002974 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002975 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002976 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002977 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2978 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002979 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002980 // Add the declaration to its redeclaration context so later merging
2981 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002982 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002983 }
2984}
2985
Richard Smith88ebade2014-08-23 01:45:27 +00002986/// Find the declaration that should be merged into, given the declaration found
2987/// by name lookup. If we're merging an anonymous declaration within a typedef,
2988/// we need a matching typedef, and we merge with the type inside it.
2989static NamedDecl *getDeclForMerging(NamedDecl *Found,
2990 bool IsTypedefNameForLinkage) {
2991 if (!IsTypedefNameForLinkage)
2992 return Found;
2993
2994 // If we found a typedef declaration that gives a name to some other
2995 // declaration, then we want that inner declaration. Declarations from
2996 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002997 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002998 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002999
3000 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003001 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003002
Hans Wennborgdcfba332015-10-06 23:40:43 +00003003 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003004}
3005
Richard Smithd08aeb62014-08-28 01:33:39 +00003006NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3007 DeclContext *DC,
3008 unsigned Index) {
3009 // If the lexical context has been merged, look into the now-canonical
3010 // definition.
3011 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3012 DC = Merged;
3013
3014 // If we've seen this before, return the canonical declaration.
3015 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3016 if (Index < Previous.size() && Previous[Index])
3017 return Previous[Index];
3018
3019 // If this is the first time, but we have parsed a declaration of the context,
3020 // build the anonymous declaration list from the parsed declaration.
3021 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00003022 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
3023 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003024 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3025 else
Richard Smith2b560572015-02-07 03:11:11 +00003026 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3027 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003028 }
3029
3030 return Index < Previous.size() ? Previous[Index] : nullptr;
3031}
3032
3033void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3034 DeclContext *DC, unsigned Index,
3035 NamedDecl *D) {
3036 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3037 DC = Merged;
3038
3039 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3040 if (Index >= Previous.size())
3041 Previous.resize(Index + 1);
3042 if (!Previous[Index])
3043 Previous[Index] = D;
3044}
3045
Douglas Gregor022857e2011-12-22 01:48:48 +00003046ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003047 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3048 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003049
Richard Smithd08aeb62014-08-28 01:33:39 +00003050 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3051 // Don't bother trying to find unnamed declarations that are in
3052 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003053 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3054 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003055 Result.suppress();
3056 return Result;
3057 }
Richard Smith95d99302013-07-13 02:00:19 +00003058
Douglas Gregor022857e2011-12-22 01:48:48 +00003059 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003060 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003061 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003062 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003063 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3064 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003065 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3066 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003067 // Go on to check in other places in case an existing typedef name
3068 // was not imported.
3069 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003070
Richard Smith2b560572015-02-07 03:11:11 +00003071 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003072 // This is an anonymous declaration that we may need to merge. Look it up
3073 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003074 if (auto *Existing = getAnonymousDeclForMerging(
3075 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3076 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003077 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3078 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003079 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003080 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003081 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003082
3083 // Temporarily consider the identifier to be up-to-date. We don't want to
3084 // cause additional lookups here.
3085 class UpToDateIdentifierRAII {
3086 IdentifierInfo *II;
3087 bool WasOutToDate;
3088
3089 public:
3090 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3091 : II(II), WasOutToDate(false)
3092 {
3093 if (II) {
3094 WasOutToDate = II->isOutOfDate();
3095 if (WasOutToDate)
3096 II->setOutOfDate(false);
3097 }
3098 }
3099
3100 ~UpToDateIdentifierRAII() {
3101 if (WasOutToDate)
3102 II->setOutOfDate(true);
3103 }
3104 } UpToDate(Name.getAsIdentifierInfo());
3105
Douglas Gregor2009cee2012-01-03 22:46:00 +00003106 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003107 IEnd = IdResolver.end();
3108 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003109 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003110 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003111 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3112 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003113 }
Richard Smith8a639892015-01-24 01:07:20 +00003114 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003115 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003116 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003117 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003118 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003119 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3120 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003121 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003122 } else {
3123 // Not in a mergeable context.
3124 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003125 }
Richard Smithd55889a2013-09-09 16:55:27 +00003126
Richard Smith2b9e3e32013-10-18 06:05:18 +00003127 // If this declaration is from a merged context, make a note that we need to
3128 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003129 //
3130 // FIXME: We should do something similar if we merge two definitions of the
3131 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003132 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3133 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3134 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003135 Reader.PendingOdrMergeChecks.push_back(D);
3136
Richard Smithd08aeb62014-08-28 01:33:39 +00003137 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003138 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003139}
3140
Richard Smithb321ecb2014-05-13 01:15:00 +00003141template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003142Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3143 return D->RedeclLink.getLatestNotUpdated();
3144}
3145Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3146 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3147}
3148
3149Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3150 assert(D);
3151
3152 switch (D->getKind()) {
3153#define ABSTRACT_DECL(TYPE)
3154#define DECL(TYPE, BASE) \
3155 case Decl::TYPE: \
3156 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3157#include "clang/AST/DeclNodes.inc"
3158 }
3159 llvm_unreachable("unknown decl kind");
3160}
3161
Richard Smith8ce51082015-03-11 01:44:51 +00003162Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3163 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3164}
3165
Richard Smithc3a53252015-02-28 05:57:02 +00003166template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003167void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3168 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003169 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003170 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003171 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003172}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003173
Richard Smith24d166c2014-07-31 23:52:38 +00003174namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003175template<>
3176void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003177 Redeclarable<VarDecl> *D,
3178 Decl *Previous, Decl *Canon) {
3179 VarDecl *VD = static_cast<VarDecl*>(D);
3180 VarDecl *PrevVD = cast<VarDecl>(Previous);
3181 D->RedeclLink.setPrevious(PrevVD);
3182 D->First = PrevVD->First;
3183
3184 // We should keep at most one definition on the chain.
3185 // FIXME: Cache the definition once we've found it. Building a chain with
3186 // N definitions currently takes O(N^2) time here.
3187 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3188 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3189 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3190 Reader.mergeDefinitionVisibility(CurD, VD);
3191 VD->demoteThisDefinitionToDeclaration();
3192 break;
3193 }
3194 }
3195 }
3196}
3197
3198template<>
3199void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003200 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003201 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003202 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3203 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3204
3205 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003206 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003207
3208 // If the previous declaration is an inline function declaration, then this
3209 // declaration is too.
3210 if (PrevFD->IsInline != FD->IsInline) {
3211 // FIXME: [dcl.fct.spec]p4:
3212 // If a function with external linkage is declared inline in one
3213 // translation unit, it shall be declared inline in all translation
3214 // units in which it appears.
3215 //
3216 // Be careful of this case:
3217 //
3218 // module A:
3219 // template<typename T> struct X { void f(); };
3220 // template<typename T> inline void X<T>::f() {}
3221 //
3222 // module B instantiates the declaration of X<int>::f
3223 // module C instantiates the definition of X<int>::f
3224 //
3225 // If module B and C are merged, we do not have a violation of this rule.
3226 FD->IsInline = true;
3227 }
3228
Richard Smith9e2341d2015-03-23 03:25:59 +00003229 // If we need to propagate an exception specification along the redecl
3230 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003231 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3232 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003233 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003234 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3235 bool WasUnresolved =
3236 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3237 if (IsUnresolved != WasUnresolved)
3238 Reader.PendingExceptionSpecUpdates.insert(
3239 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003240 }
3241}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003242} // end namespace clang
3243
Richard Smith6de7a242014-07-31 23:46:44 +00003244void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003245 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3246}
3247
Richard Smith8346e522015-06-10 01:47:58 +00003248/// Inherit the default template argument from \p From to \p To. Returns
3249/// \c false if there is no default template for \p From.
3250template <typename ParmDecl>
3251static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3252 Decl *ToD) {
3253 auto *To = cast<ParmDecl>(ToD);
3254 if (!From->hasDefaultArgument())
3255 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003256 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003257 return true;
3258}
3259
3260static void inheritDefaultTemplateArguments(ASTContext &Context,
3261 TemplateDecl *From,
3262 TemplateDecl *To) {
3263 auto *FromTP = From->getTemplateParameters();
3264 auto *ToTP = To->getTemplateParameters();
3265 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3266
3267 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3268 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003269 if (FromParam->isParameterPack())
3270 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003271 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3272
3273 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003274 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003275 break;
3276 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003277 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003278 break;
3279 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003280 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003281 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3282 break;
3283 }
3284 }
3285}
3286
Richard Smith6de7a242014-07-31 23:46:44 +00003287void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003288 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003289 assert(D && Previous);
3290
3291 switch (D->getKind()) {
3292#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003293#define DECL(TYPE, BASE) \
3294 case Decl::TYPE: \
3295 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003296 break;
3297#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003298 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003299
3300 // If the declaration was visible in one module, a redeclaration of it in
3301 // another module remains visible even if it wouldn't be visible by itself.
3302 //
3303 // FIXME: In this case, the declaration should only be visible if a module
3304 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003305 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003306 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003307 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003308
Richard Smith8346e522015-06-10 01:47:58 +00003309 // If the declaration declares a template, it may inherit default arguments
3310 // from the previous declaration.
3311 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3312 inheritDefaultTemplateArguments(Reader.getContext(),
3313 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003314}
3315
Richard Smithb321ecb2014-05-13 01:15:00 +00003316template<typename DeclT>
3317void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003318 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003319}
3320void ASTDeclReader::attachLatestDeclImpl(...) {
3321 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3322}
3323
Douglas Gregor05f10352011-12-17 23:38:30 +00003324void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3325 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003326
3327 switch (D->getKind()) {
3328#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003329#define DECL(TYPE, BASE) \
3330 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003331 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3332 break;
3333#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003334 }
3335}
3336
Richard Smith851072e2014-05-19 20:59:20 +00003337template<typename DeclT>
3338void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3339 D->RedeclLink.markIncomplete();
3340}
3341void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3342 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3343}
3344
3345void ASTReader::markIncompleteDeclChain(Decl *D) {
3346 switch (D->getKind()) {
3347#define ABSTRACT_DECL(TYPE)
3348#define DECL(TYPE, BASE) \
3349 case Decl::TYPE: \
3350 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3351 break;
3352#include "clang/AST/DeclNodes.inc"
3353 }
3354}
3355
Sebastian Redlb3298c32010-08-18 23:56:48 +00003356/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003357Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003358 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003359 SourceLocation DeclLoc;
3360 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003361 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003362 // Keep track of where we are in the stream, then jump back there
3363 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003364 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003365
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003366 ReadingKindTracker ReadingKind(Read_Decl, *this);
3367
Douglas Gregor1342e842009-07-06 18:54:52 +00003368 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003369 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003370
Sebastian Redl2c373b92010-10-05 15:59:54 +00003371 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003372 ASTRecordReader Record(*this, *Loc.F);
3373 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003374 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003375
Craig Toppera13603a2014-05-22 05:54:18 +00003376 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003377 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003378 case DECL_CONTEXT_LEXICAL:
3379 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003380 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003381 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003382 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003383 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003384 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003385 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003386 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003387 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003388 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003389 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003390 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003391 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003392 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003393 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003394 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003395 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003396 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003397 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003398 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003399 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003400 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003401 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003402 case DECL_EXPORT:
3403 D = ExportDecl::CreateDeserialized(Context, ID);
3404 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003405 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003406 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003407 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003408 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003409 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003410 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003411 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003412 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003413 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003414 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003415 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003416 break;
Richard Smith151c4562016-12-20 21:35:28 +00003417 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003418 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003419 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003420 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003421 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003422 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003423 case DECL_CONSTRUCTOR_USING_SHADOW:
3424 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3425 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003426 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003427 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003428 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003429 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003430 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003431 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003432 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003433 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003434 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003435 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003436 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003437 break;
Richard Smithbc491202017-02-17 20:05:37 +00003438 case DECL_CXX_DEDUCTION_GUIDE:
3439 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3440 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003441 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003442 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003443 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003445 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3446 break;
3447 case DECL_CXX_INHERITED_CONSTRUCTOR:
3448 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003449 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003450 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003451 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003452 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003453 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003454 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003455 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003456 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003457 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003458 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003459 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003460 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003461 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003462 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003463 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003464 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003465 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003466 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003467 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003468 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003469 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003470 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003471 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003472 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003473 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003474 case DECL_VAR_TEMPLATE:
3475 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3476 break;
3477 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3478 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3479 break;
3480 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3481 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3482 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003483 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003484 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003485 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003486 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003487 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003488 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003489 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003490 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003491 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003492 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003493 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003494 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003495 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003496 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3497 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003498 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003499 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003500 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003501 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003502 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3503 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003504 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003505 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003506 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003507 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003508 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003510 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003511 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003512 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003513 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003514 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003516 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003517 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003518 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003519 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003520 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003521 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003522 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003523 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003524 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003525 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003526 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003527 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003528 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003529 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003530 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003531 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003532 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003533 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003534 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003535 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003536 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003537 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003538 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003539 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003540 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003541 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003542 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003543 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003544 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003545 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003546 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003547 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003548 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003549 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003550 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003551 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003552 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003553 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003554 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003555 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003556 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003557 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003558 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003559 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003560 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003561 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003562 break;
3563 case DECL_BINDING:
3564 D = BindingDecl::CreateDeserialized(Context, ID);
3565 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003566 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003567 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003568 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003569 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003570 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003571 break;
John McCall5e77d762013-04-16 07:28:30 +00003572 case DECL_MS_PROPERTY:
3573 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3574 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003575 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003576 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003577 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003578 case DECL_CXX_BASE_SPECIFIERS:
3579 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003580 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003581 case DECL_CXX_CTOR_INITIALIZERS:
3582 Error("attempt to read a C++ ctor initializer record as a declaration");
3583 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003584 case DECL_IMPORT:
3585 // Note: last entry of the ImportDecl record is the number of stored source
3586 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003587 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003588 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003589 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003590 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003591 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003592 case DECL_OMP_DECLARE_REDUCTION:
3593 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3594 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003595 case DECL_OMP_CAPTUREDEXPR:
3596 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003597 break;
Nico Weber66220292016-03-02 17:28:48 +00003598 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003599 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003600 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003601 case DECL_PRAGMA_DETECT_MISMATCH:
3602 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003603 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003604 break;
Michael Han84324352013-02-22 17:15:32 +00003605 case DECL_EMPTY:
3606 D = EmptyDecl::CreateDeserialized(Context, ID);
3607 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003608 case DECL_OBJC_TYPE_PARAM:
3609 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3610 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003611 }
Chris Lattner487412d2009-04-27 05:27:42 +00003612
Sebastian Redlb3298c32010-08-18 23:56:48 +00003613 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003614 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003615 // Set the DeclContext before doing any deserialization, to make sure internal
3616 // calls to Decl::getASTContext() by Decl's methods will find the
3617 // TranslationUnitDecl without crashing.
3618 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003619 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003620
3621 // If this declaration is also a declaration context, get the
3622 // offsets for its tables of lexical and visible declarations.
3623 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3624 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003625 if (Offsets.first &&
3626 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3627 return nullptr;
3628 if (Offsets.second &&
3629 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3630 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003631 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003632 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003633
Douglas Gregordab42432011-08-12 00:15:20 +00003634 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003635 PendingUpdateRecords.push_back(
3636 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003637
Douglas Gregor404cdde2012-01-27 01:47:08 +00003638 // Load the categories after recursive loading is finished.
3639 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003640 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3641 // we put the Decl in PendingDefinitions so we can pull the categories here.
3642 if (Class->isThisDeclarationADefinition() ||
3643 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003644 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003645
Richard Smith13fb8602016-07-15 21:33:46 +00003646 // If we have deserialized a declaration that has a definition the
3647 // AST consumer might need to know about, queue it.
3648 // We don't pass it to the consumer immediately because we may be in recursive
3649 // loading, and some declarations may still be initializing.
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003650 PotentiallyInterestingDecls.push_back(
3651 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003652
Douglas Gregordab42432011-08-12 00:15:20 +00003653 return D;
3654}
3655
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003656void ASTReader::PassInterestingDeclsToConsumer() {
3657 assert(Consumer);
3658
3659 if (PassingDeclsToConsumer)
3660 return;
3661
3662 // Guard variable to avoid recursively redoing the process of passing
3663 // decls to consumer.
3664 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3665 true);
3666
3667 // Ensure that we've loaded all potentially-interesting declarations
3668 // that need to be eagerly loaded.
3669 for (auto ID : EagerlyDeserializedDecls)
3670 GetDecl(ID);
3671 EagerlyDeserializedDecls.clear();
3672
3673 while (!PotentiallyInterestingDecls.empty()) {
3674 InterestingDecl D = PotentiallyInterestingDecls.front();
3675 PotentiallyInterestingDecls.pop_front();
3676 if (isConsumerInterestedIn(Context, D.getDecl(), D.hasPendingBody()))
3677 PassInterestingDeclToConsumer(D.getDecl());
3678 }
3679}
3680
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003681void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003682 // The declaration may have been modified by files later in the chain.
3683 // If this is the case, read the record containing the updates from each file
3684 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003685 serialization::GlobalDeclID ID = Record.ID;
3686 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003687 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003688 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3689 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003690 auto UpdateOffsets = std::move(UpdI->second);
3691 DeclUpdateOffsets.erase(UpdI);
3692
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003693 // Check if this decl was interesting to the consumer. If we just loaded
3694 // the declaration, then we know it was interesting and we skip the call
3695 // to isConsumerInterestedIn because it is unsafe to call in the
3696 // current ASTReader state.
3697 bool WasInteresting =
3698 Record.JustLoaded || isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003699 for (auto &FileAndOffset : UpdateOffsets) {
3700 ModuleFile *F = FileAndOffset.first;
3701 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003702 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3703 SavedStreamPosition SavedPosition(Cursor);
3704 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003705 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003706 ASTRecordReader Record(*this, *F);
3707 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003708 (void)RecCode;
3709 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003710
David L. Jonesbe1557a2016-12-21 00:17:49 +00003711 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3712 SourceLocation());
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00003713 Reader.UpdateDecl(D);
Richard Smith04d05b52014-03-23 00:27:18 +00003714
3715 // We might have made this declaration interesting. If so, remember that
3716 // we need to hand it off to the consumer.
3717 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003718 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003719 PotentiallyInterestingDecls.push_back(
3720 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003721 WasInteresting = true;
3722 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003723 }
3724 }
Richard Smith1e8e91b2016-04-08 20:53:26 +00003725
3726 // Load the pending visible updates for this decl context, if it has any.
3727 auto I = PendingVisibleUpdates.find(ID);
3728 if (I != PendingVisibleUpdates.end()) {
3729 auto VisibleUpdates = std::move(I->second);
3730 PendingVisibleUpdates.erase(I);
3731
3732 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3733 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3734 Lookups[DC].Table.add(
3735 Update.Mod, Update.Data,
3736 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3737 DC->setHasExternalVisibleStorage(true);
3738 }
Chris Lattner487412d2009-04-27 05:27:42 +00003739}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003740
Richard Smithd61d4ac2015-08-22 20:13:39 +00003741void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3742 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003743 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003744 if (FirstLocal != CanonDecl) {
3745 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3746 ASTDeclReader::attachPreviousDecl(
3747 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3748 CanonDecl);
3749 }
3750
3751 if (!LocalOffset) {
3752 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3753 return;
3754 }
3755
3756 // Load the list of other redeclarations from this module file.
3757 ModuleFile *M = getOwningModuleFile(FirstLocal);
3758 assert(M && "imported decl from no module file");
3759
3760 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3761 SavedStreamPosition SavedPosition(Cursor);
3762 Cursor.JumpToBit(LocalOffset);
3763
3764 RecordData Record;
3765 unsigned Code = Cursor.ReadCode();
3766 unsigned RecCode = Cursor.readRecord(Code, Record);
3767 (void)RecCode;
3768 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3769
3770 // FIXME: We have several different dispatches on decl kind here; maybe
3771 // we should instead generate one loop per kind and dispatch up-front?
3772 Decl *MostRecent = FirstLocal;
3773 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3774 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003775 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3776 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003777 }
Richard Smithc3a53252015-02-28 05:57:02 +00003778 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003779}
3780
3781namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003782 /// \brief Given an ObjC interface, goes through the modules and links to the
3783 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003784 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003785 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003786 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003787 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003788 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003789 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003790 serialization::GlobalDeclID InterfaceID;
3791 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003792
3793 void add(ObjCCategoryDecl *Cat) {
3794 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003795 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003796 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003797
3798 // Check for duplicate categories.
3799 if (Cat->getDeclName()) {
3800 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3801 if (Existing &&
3802 Reader.getOwningModuleFile(Existing)
3803 != Reader.getOwningModuleFile(Cat)) {
3804 // FIXME: We should not warn for duplicates in diamond:
3805 //
3806 // MT //
3807 // / \ //
3808 // ML MR //
3809 // \ / //
3810 // MB //
3811 //
3812 // If there are duplicates in ML/MR, there will be warning when
3813 // creating MB *and* when importing MB. We should not warn when
3814 // importing.
3815 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3816 << Interface->getDeclName() << Cat->getDeclName();
3817 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3818 } else if (!Existing) {
3819 // Record this category.
3820 Existing = Cat;
3821 }
3822 }
3823
3824 // Add this category to the end of the chain.
3825 if (Tail)
3826 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3827 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003828 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003829 Tail = Cat;
3830 }
3831
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003832 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003833 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003834 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003835 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3836 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003837 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003838 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3839 Tail(nullptr), InterfaceID(InterfaceID),
3840 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003841 {
3842 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003843 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003844 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003845 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003846
3847 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003848 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003849 }
3850 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003851
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003852 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003853 // If we've loaded all of the category information we care about from
3854 // this module file, we're done.
3855 if (M.Generation <= PreviousGeneration)
3856 return true;
3857
3858 // Map global ID of the definition down to the local ID used in this
3859 // module file. If there is no such mapping, we'll find nothing here
3860 // (or in any module it imports).
3861 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3862 if (!LocalID)
3863 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003864
Douglas Gregor404cdde2012-01-27 01:47:08 +00003865 // Perform a binary search to find the local redeclarations for this
3866 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003867 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003868 const ObjCCategoriesInfo *Result
3869 = std::lower_bound(M.ObjCCategoriesMap,
3870 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003871 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003872 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3873 Result->DefinitionID != LocalID) {
3874 // We didn't find anything. If the class definition is in this module
3875 // file, then the module files it depends on cannot have any categories,
3876 // so suppress further lookup.
3877 return Reader.isDeclIDFromModule(InterfaceID, M);
3878 }
3879
3880 // We found something. Dig out all of the categories.
3881 unsigned Offset = Result->Offset;
3882 unsigned N = M.ObjCCategories[Offset];
3883 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3884 for (unsigned I = 0; I != N; ++I)
3885 add(cast_or_null<ObjCCategoryDecl>(
3886 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3887 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003888 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003889 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003890} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003891
Douglas Gregor404cdde2012-01-27 01:47:08 +00003892void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3893 ObjCInterfaceDecl *D,
3894 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003895 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003896 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003897 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003898}
Douglas Gregordab42432011-08-12 00:15:20 +00003899
Richard Smithd6db68c2014-08-07 20:58:41 +00003900template<typename DeclT, typename Fn>
3901static void forAllLaterRedecls(DeclT *D, Fn F) {
3902 F(D);
3903
3904 // Check whether we've already merged D into its redeclaration chain.
3905 // MostRecent may or may not be nullptr if D has not been merged. If
3906 // not, walk the merged redecl chain and see if it's there.
3907 auto *MostRecent = D->getMostRecentDecl();
3908 bool Found = false;
3909 for (auto *Redecl = MostRecent; Redecl && !Found;
3910 Redecl = Redecl->getPreviousDecl())
3911 Found = (Redecl == D);
3912
3913 // If this declaration is merged, apply the functor to all later decls.
3914 if (Found) {
3915 for (auto *Redecl = MostRecent; Redecl != D;
3916 Redecl = Redecl->getPreviousDecl())
3917 F(Redecl);
3918 }
3919}
3920
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00003921void ASTDeclReader::UpdateDecl(Decl *D) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003922 while (Record.getIdx() < Record.size()) {
3923 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003924 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003925 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003926 // FIXME: If we also have an update record for instantiating the
3927 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003928 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003929 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003930 // FIXME: We should call addHiddenDecl instead, to add the member
3931 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003932 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003933 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003934 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003935
3936 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassileva91cbdc2017-06-15 11:05:32 +00003937 // It will be added to the template's specializations set when loaded.
3938 (void)Record.readDecl();
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003939 break;
3940
3941 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003942 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3943
Douglas Gregor540fd812012-01-09 18:07:24 +00003944 // Each module has its own anonymous namespace, which is disjoint from
3945 // any other module's anonymous namespaces, so don't attach the anonymous
3946 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003947 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003948 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3949 TU->setAnonymousNamespace(Anon);
3950 else
3951 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3952 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003953 break;
3954 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003955
Richard Smith05a21352017-06-22 22:18:46 +00003956 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: {
3957 VarDecl *VD = cast<VarDecl>(D);
3958 VD->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003959 ReadSourceLocation());
Richard Smith05a21352017-06-22 22:18:46 +00003960 uint64_t Val = Record.readInt();
3961 if (Val && !VD->getInit()) {
3962 VD->setInit(Record.readExpr());
3963 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
3964 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
3965 Eval->CheckedICE = true;
3966 Eval->IsICE = Val == 3;
3967 }
3968 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003969 break;
Richard Smith05a21352017-06-22 22:18:46 +00003970 }
Richard Smith1fa5d642013-05-11 05:45:24 +00003971
John McCall32791cc2016-01-06 22:34:54 +00003972 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3973 auto Param = cast<ParmVarDecl>(D);
3974
3975 // We have to read the default argument regardless of whether we use it
3976 // so that hypothetical further update records aren't messed up.
3977 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003978 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003979
3980 // Only apply the update if the parameter still has an uninstantiated
3981 // default argument.
3982 if (Param->hasUninstantiatedDefaultArg())
3983 Param->setDefaultArg(DefaultArg);
3984 break;
3985 }
3986
Richard Smith4b054b22016-08-24 21:25:37 +00003987 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3988 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003989 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003990
3991 // Only apply the update if the field still has an uninstantiated
3992 // default member initializer.
3993 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3994 if (DefaultInit)
3995 FD->setInClassInitializer(DefaultInit);
3996 else
3997 // Instantiation failed. We can get here if we serialized an AST for
3998 // an invalid program.
3999 FD->removeInClassInitializer();
4000 }
4001 break;
4002 }
4003
Richard Smith4d235792014-08-07 18:53:08 +00004004 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00004005 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004006 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00004007 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004008 // It's safe to stop now because this update record is always last.
4009 return;
4010 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00004011
David L. Jonesbe1557a2016-12-21 00:17:49 +00004012 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004013 // Maintain AST consistency: any later redeclarations of this function
4014 // are inline if this one is. (We might have merged another declaration
4015 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004016 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4017 FD->setImplicitlyInline();
4018 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004019 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004020 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004021 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004022 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004023 break;
4024 }
4025
Richard Smithcd45dbc2014-04-19 03:48:30 +00004026 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4027 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004028 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004029 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004030 OldDD && (OldDD->Definition != RD ||
4031 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00004032 ReadCXXRecordDefinition(RD, /*Update*/true);
4033
Richard Smithcd45dbc2014-04-19 03:48:30 +00004034 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004035 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004036 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004037 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004038 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004039 }
4040
David L. Jonesbe1557a2016-12-21 00:17:49 +00004041 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004042 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004043 if (MemberSpecializationInfo *MSInfo =
4044 RD->getMemberSpecializationInfo()) {
4045 MSInfo->setTemplateSpecializationKind(TSK);
4046 MSInfo->setPointOfInstantiation(POI);
4047 } else {
4048 ClassTemplateSpecializationDecl *Spec =
4049 cast<ClassTemplateSpecializationDecl>(RD);
4050 Spec->setTemplateSpecializationKind(TSK);
4051 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004052
David L. Jonesbe1557a2016-12-21 00:17:49 +00004053 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00004054 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004055 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004056 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004057 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004058 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004059 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004060
4061 // FIXME: If we already have a partial specialization set,
4062 // check that it matches.
4063 if (!Spec->getSpecializedTemplateOrPartial()
4064 .is<ClassTemplatePartialSpecializationDecl *>())
4065 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004066 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004067 }
4068
David L. Jonesbe1557a2016-12-21 00:17:49 +00004069 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004070 RD->setLocation(ReadSourceLocation());
4071 RD->setLocStart(ReadSourceLocation());
4072 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004073
David L. Jonesbe1557a2016-12-21 00:17:49 +00004074 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004075 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004076 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004077 // If the declaration already has attributes, we assume that some other
4078 // AST file already loaded them.
4079 if (!D->hasAttrs())
4080 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004081 }
4082 break;
4083 }
4084
Richard Smithf8134002015-03-10 01:41:22 +00004085 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4086 // Set the 'operator delete' directly to avoid emitting another update
4087 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004088 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004089 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
4090 // FIXME: Check consistency if we have an old and new operator delete.
4091 if (!First->OperatorDelete)
4092 First->OperatorDelete = Del;
4093 break;
4094 }
4095
Richard Smith564417a2014-03-20 21:47:22 +00004096 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004097 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004098 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004099 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004100
4101 // Update this declaration's exception specification, if needed.
4102 auto *FD = cast<FunctionDecl>(D);
4103 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4104 // FIXME: If the exception specification is already present, check that it
4105 // matches.
4106 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00004107 FD->setType(Reader.Context.getFunctionType(
4108 FPT->getReturnType(), FPT->getParamTypes(),
4109 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004110
4111 // When we get to the end of deserializing, see if there are other decls
4112 // that we need to propagate this exception specification onto.
4113 Reader.PendingExceptionSpecUpdates.insert(
4114 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004115 }
Richard Smith564417a2014-03-20 21:47:22 +00004116 break;
4117 }
4118
Richard Smith1fa5d642013-05-11 05:45:24 +00004119 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004120 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004121 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004122 for (auto *Redecl : merged_redecls(D)) {
4123 // FIXME: If the return type is already deduced, check that it matches.
4124 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
4125 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
4126 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004127 break;
4128 }
Eli Friedman276dd182013-09-05 00:02:25 +00004129
4130 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004131 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004132 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00004133 break;
4134 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004135
4136 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004137 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004138 break;
4139
4140 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004141 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004142 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004143
Alexey Bataev97720002014-11-11 04:05:39 +00004144 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4145 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004146 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004147 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004148
Alex Denisovfde64952015-06-26 05:28:36 +00004149 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004150 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004151 auto *Exported = cast<NamedDecl>(D);
4152 if (auto *TD = dyn_cast<TagDecl>(Exported))
4153 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004154 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004155 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004156 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4157 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004158 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4159 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004160 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004161 // If Owner is made visible at some later point, make this declaration
4162 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004163 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004164 } else {
4165 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004166 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004167 }
4168 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004169 }
Alex Denisovfde64952015-06-26 05:28:36 +00004170
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004171 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004172 case UPD_ADDED_ATTR_TO_RECORD:
4173 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004174 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004175 assert(Attrs.size() == 1);
4176 D->addAttr(Attrs[0]);
4177 break;
4178 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004179 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004180}