1 //===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ASTReader::ReadDeclRecord method, which is the
11 // entrypoint for loading a decl.
13 //===----------------------------------------------------------------------===//
15 #include "ASTCommon.h"
16 #include "clang/Serialization/ASTReader.h"
17 #include "clang/Sema/SemaDiagnostic.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/DeclVisitor.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/Expr.h"
25 using namespace clang;
26 using namespace clang::serialization;
28 //===----------------------------------------------------------------------===//
29 // Declaration deserialization
30 //===----------------------------------------------------------------------===//
33 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
36 llvm::BitstreamCursor &Cursor;
37 const DeclID ThisDeclID;
38 typedef ASTReader::RecordData RecordData;
39 const RecordData &Record;
41 TypeID TypeIDForTypeDecl;
43 DeclID DeclContextIDForTemplateParmDecl;
44 DeclID LexicalDeclContextIDForTemplateParmDecl;
46 uint64_t GetCurrentCursorOffset();
48 SourceLocation ReadSourceLocation(const RecordData &R, unsigned &I) {
49 return Reader.ReadSourceLocation(F, R, I);
52 SourceRange ReadSourceRange(const RecordData &R, unsigned &I) {
53 return Reader.ReadSourceRange(F, R, I);
56 TypeSourceInfo *GetTypeSourceInfo(const RecordData &R, unsigned &I) {
57 return Reader.GetTypeSourceInfo(F, R, I);
60 serialization::DeclID ReadDeclID(const RecordData &R, unsigned &I) {
61 return Reader.ReadDeclID(F, R, I);
64 Decl *ReadDecl(const RecordData &R, unsigned &I) {
65 return Reader.ReadDecl(F, R, I);
69 T *ReadDeclAs(const RecordData &R, unsigned &I) {
70 return Reader.ReadDeclAs<T>(F, R, I);
73 void ReadQualifierInfo(QualifierInfo &Info,
74 const RecordData &R, unsigned &I) {
75 Reader.ReadQualifierInfo(F, Info, R, I);
78 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name,
79 const RecordData &R, unsigned &I) {
80 Reader.ReadDeclarationNameLoc(F, DNLoc, Name, R, I);
83 void ReadDeclarationNameInfo(DeclarationNameInfo &NameInfo,
84 const RecordData &R, unsigned &I) {
85 Reader.ReadDeclarationNameInfo(F, NameInfo, R, I);
88 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
89 const RecordData &R, unsigned &I);
91 void InitializeCXXDefinitionData(CXXRecordDecl *D,
92 CXXRecordDecl *DefinitionDecl,
93 const RecordData &Record, unsigned &Idx);
95 ASTDeclReader(ASTReader &Reader, Module &F,
96 llvm::BitstreamCursor &Cursor, DeclID thisDeclID,
97 const RecordData &Record, unsigned &Idx)
98 : Reader(Reader), F(F), Cursor(Cursor), ThisDeclID(thisDeclID),
99 Record(Record), Idx(Idx), TypeIDForTypeDecl(0) { }
101 static void attachPreviousDecl(Decl *D, Decl *previous);
105 void UpdateDecl(Decl *D, Module &Module,
106 const RecordData &Record);
108 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
109 ObjCCategoryDecl *Next) {
110 Cat->NextClassCategory = Next;
113 void VisitDecl(Decl *D);
114 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
115 void VisitNamedDecl(NamedDecl *ND);
116 void VisitLabelDecl(LabelDecl *LD);
117 void VisitNamespaceDecl(NamespaceDecl *D);
118 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
119 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
120 void VisitTypeDecl(TypeDecl *TD);
121 void VisitTypedefDecl(TypedefDecl *TD);
122 void VisitTypeAliasDecl(TypeAliasDecl *TD);
123 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
124 void VisitTagDecl(TagDecl *TD);
125 void VisitEnumDecl(EnumDecl *ED);
126 void VisitRecordDecl(RecordDecl *RD);
127 void VisitCXXRecordDecl(CXXRecordDecl *D);
128 void VisitClassTemplateSpecializationDecl(
129 ClassTemplateSpecializationDecl *D);
130 void VisitClassTemplatePartialSpecializationDecl(
131 ClassTemplatePartialSpecializationDecl *D);
132 void VisitClassScopeFunctionSpecializationDecl(
133 ClassScopeFunctionSpecializationDecl *D);
134 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
135 void VisitValueDecl(ValueDecl *VD);
136 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
137 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
138 void VisitDeclaratorDecl(DeclaratorDecl *DD);
139 void VisitFunctionDecl(FunctionDecl *FD);
140 void VisitCXXMethodDecl(CXXMethodDecl *D);
141 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
142 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
143 void VisitCXXConversionDecl(CXXConversionDecl *D);
144 void VisitFieldDecl(FieldDecl *FD);
145 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
146 void VisitVarDecl(VarDecl *VD);
147 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
148 void VisitParmVarDecl(ParmVarDecl *PD);
149 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
150 void VisitTemplateDecl(TemplateDecl *D);
151 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
152 void VisitClassTemplateDecl(ClassTemplateDecl *D);
153 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
154 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
155 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
156 void VisitUsingDecl(UsingDecl *D);
157 void VisitUsingShadowDecl(UsingShadowDecl *D);
158 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
159 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
160 void VisitAccessSpecDecl(AccessSpecDecl *D);
161 void VisitFriendDecl(FriendDecl *D);
162 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
163 void VisitStaticAssertDecl(StaticAssertDecl *D);
164 void VisitBlockDecl(BlockDecl *BD);
166 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
167 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
169 // FIXME: Reorder according to DeclNodes.td?
170 void VisitObjCMethodDecl(ObjCMethodDecl *D);
171 void VisitObjCContainerDecl(ObjCContainerDecl *D);
172 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
173 void VisitObjCIvarDecl(ObjCIvarDecl *D);
174 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
175 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
176 void VisitObjCClassDecl(ObjCClassDecl *D);
177 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
178 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
179 void VisitObjCImplDecl(ObjCImplDecl *D);
180 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
181 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
182 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
183 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
184 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
188 uint64_t ASTDeclReader::GetCurrentCursorOffset() {
189 return F.DeclsCursor.GetCurrentBitNo() + F.GlobalBitOffset;
192 void ASTDeclReader::Visit(Decl *D) {
193 DeclVisitor<ASTDeclReader, void>::Visit(D);
195 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
197 DeclaratorDecl::ExtInfo *Info =
198 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
200 GetTypeSourceInfo(Record, Idx);
203 DD->DeclInfo = GetTypeSourceInfo(Record, Idx);
207 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
208 // if we have a fully initialized TypeDecl, we can safely read its type now.
209 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
210 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
211 // FunctionDecl's body was written last after all other Stmts/Exprs.
213 FD->setLazyBody(GetCurrentCursorOffset());
214 } else if (D->isTemplateParameter()) {
215 // If we have a fully initialized template parameter, we can now
216 // set its DeclContext.
218 cast_or_null<DeclContext>(
219 Reader.GetDecl(DeclContextIDForTemplateParmDecl)));
220 D->setLexicalDeclContext(
221 cast_or_null<DeclContext>(
222 Reader.GetDecl(LexicalDeclContextIDForTemplateParmDecl)));
226 void ASTDeclReader::VisitDecl(Decl *D) {
227 if (D->isTemplateParameter()) {
228 // We don't want to deserialize the DeclContext of a template
229 // parameter immediately, because the template parameter might be
230 // used in the formulation of its DeclContext. Use the translation
231 // unit DeclContext as a placeholder.
232 DeclContextIDForTemplateParmDecl = ReadDeclID(Record, Idx);
233 LexicalDeclContextIDForTemplateParmDecl = ReadDeclID(Record, Idx);
234 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
236 D->setDeclContext(ReadDeclAs<DeclContext>(Record, Idx));
237 D->setLexicalDeclContext(ReadDeclAs<DeclContext>(Record, Idx));
239 D->setLocation(ReadSourceLocation(Record, Idx));
240 D->setInvalidDecl(Record[Idx++]);
241 if (Record[Idx++]) { // hasAttrs
243 Reader.ReadAttributes(F, Attrs, Record, Idx);
246 D->setImplicit(Record[Idx++]);
247 D->setUsed(Record[Idx++]);
248 D->setReferenced(Record[Idx++]);
249 D->setAccess((AccessSpecifier)Record[Idx++]);
250 D->FromASTFile = true;
251 D->ModulePrivate = Record[Idx++];
254 void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
255 llvm_unreachable("Translation units are not serialized");
258 void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
260 ND->setDeclName(Reader.ReadDeclarationName(F, Record, Idx));
263 void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
265 TD->setLocStart(ReadSourceLocation(Record, Idx));
266 // Delay type reading until after we have fully initialized the decl.
267 TypeIDForTypeDecl = Reader.getGlobalTypeID(F, Record[Idx++]);
270 void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
272 TD->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
275 void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
277 TD->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
280 void ASTDeclReader::VisitTagDecl(TagDecl *TD) {
282 VisitRedeclarable(TD);
283 TD->IdentifierNamespace = Record[Idx++];
284 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
285 TD->setCompleteDefinition(Record[Idx++]);
286 TD->setEmbeddedInDeclarator(Record[Idx++]);
287 TD->setFreeStanding(Record[Idx++]);
288 TD->setRBraceLoc(ReadSourceLocation(Record, Idx));
289 if (Record[Idx++]) { // hasExtInfo
290 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
291 ReadQualifierInfo(*Info, Record, Idx);
292 TD->TypedefNameDeclOrQualifier = Info;
294 TD->setTypedefNameForAnonDecl(ReadDeclAs<TypedefNameDecl>(Record, Idx));
297 void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
299 if (TypeSourceInfo *TI = Reader.GetTypeSourceInfo(F, Record, Idx))
300 ED->setIntegerTypeSourceInfo(TI);
302 ED->setIntegerType(Reader.readType(F, Record, Idx));
303 ED->setPromotionType(Reader.readType(F, Record, Idx));
304 ED->setNumPositiveBits(Record[Idx++]);
305 ED->setNumNegativeBits(Record[Idx++]);
306 ED->IsScoped = Record[Idx++];
307 ED->IsScopedUsingClassTag = Record[Idx++];
308 ED->IsFixed = Record[Idx++];
309 ED->setInstantiationOfMemberEnum(ReadDeclAs<EnumDecl>(Record, Idx));
312 void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
314 RD->setHasFlexibleArrayMember(Record[Idx++]);
315 RD->setAnonymousStructOrUnion(Record[Idx++]);
316 RD->setHasObjectMember(Record[Idx++]);
319 void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
321 VD->setType(Reader.readType(F, Record, Idx));
324 void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
327 ECD->setInitExpr(Reader.ReadExpr(F));
328 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
331 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
333 DD->setInnerLocStart(ReadSourceLocation(Record, Idx));
334 if (Record[Idx++]) { // hasExtInfo
335 DeclaratorDecl::ExtInfo *Info
336 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
337 ReadQualifierInfo(*Info, Record, Idx);
342 void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
343 VisitDeclaratorDecl(FD);
344 VisitRedeclarable(FD);
346 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName(), Record, Idx);
347 FD->IdentifierNamespace = Record[Idx++];
348 switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
349 default: llvm_unreachable("Unhandled TemplatedKind!");
350 case FunctionDecl::TK_NonTemplate:
352 case FunctionDecl::TK_FunctionTemplate:
353 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>(Record,
356 case FunctionDecl::TK_MemberSpecialization: {
357 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>(Record, Idx);
358 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
359 SourceLocation POI = ReadSourceLocation(Record, Idx);
360 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
361 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
364 case FunctionDecl::TK_FunctionTemplateSpecialization: {
365 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>(Record,
367 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
369 // Template arguments.
370 SmallVector<TemplateArgument, 8> TemplArgs;
371 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
373 // Template args as written.
374 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
375 SourceLocation LAngleLoc, RAngleLoc;
376 bool HasTemplateArgumentsAsWritten = Record[Idx++];
377 if (HasTemplateArgumentsAsWritten) {
378 unsigned NumTemplateArgLocs = Record[Idx++];
379 TemplArgLocs.reserve(NumTemplateArgLocs);
380 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
381 TemplArgLocs.push_back(
382 Reader.ReadTemplateArgumentLoc(F, Record, Idx));
384 LAngleLoc = ReadSourceLocation(Record, Idx);
385 RAngleLoc = ReadSourceLocation(Record, Idx);
388 SourceLocation POI = ReadSourceLocation(Record, Idx);
390 ASTContext &C = Reader.getContext();
391 TemplateArgumentList *TemplArgList
392 = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), TemplArgs.size());
393 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
394 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
395 TemplArgsInfo.addArgument(TemplArgLocs[i]);
396 FunctionTemplateSpecializationInfo *FTInfo
397 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
399 HasTemplateArgumentsAsWritten ? &TemplArgsInfo : 0,
401 FD->TemplateOrSpecialization = FTInfo;
403 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
404 // The template that contains the specializations set. It's not safe to
405 // use getCanonicalDecl on Template since it may still be initializing.
406 FunctionTemplateDecl *CanonTemplate
407 = ReadDeclAs<FunctionTemplateDecl>(Record, Idx);
408 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
409 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
410 // FunctionTemplateSpecializationInfo's Profile().
411 // We avoid getASTContext because a decl in the parent hierarchy may
413 llvm::FoldingSetNodeID ID;
414 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs.data(),
415 TemplArgs.size(), C);
417 CanonTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
418 assert(InsertPos && "Another specialization already inserted!");
419 CanonTemplate->getSpecializations().InsertNode(FTInfo, InsertPos);
423 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
425 UnresolvedSet<8> TemplDecls;
426 unsigned NumTemplates = Record[Idx++];
427 while (NumTemplates--)
428 TemplDecls.addDecl(ReadDeclAs<NamedDecl>(Record, Idx));
431 TemplateArgumentListInfo TemplArgs;
432 unsigned NumArgs = Record[Idx++];
434 TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(F, Record, Idx));
435 TemplArgs.setLAngleLoc(ReadSourceLocation(Record, Idx));
436 TemplArgs.setRAngleLoc(ReadSourceLocation(Record, Idx));
438 FD->setDependentTemplateSpecialization(Reader.getContext(),
439 TemplDecls, TemplArgs);
444 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
445 // after everything else is read.
447 FD->SClass = (StorageClass)Record[Idx++];
448 FD->SClassAsWritten = (StorageClass)Record[Idx++];
449 FD->IsInline = Record[Idx++];
450 FD->IsInlineSpecified = Record[Idx++];
451 FD->IsVirtualAsWritten = Record[Idx++];
452 FD->IsPure = Record[Idx++];
453 FD->HasInheritedPrototype = Record[Idx++];
454 FD->HasWrittenPrototype = Record[Idx++];
455 FD->IsDeleted = Record[Idx++];
456 FD->IsTrivial = Record[Idx++];
457 FD->IsDefaulted = Record[Idx++];
458 FD->IsExplicitlyDefaulted = Record[Idx++];
459 FD->HasImplicitReturnZero = Record[Idx++];
460 FD->IsConstexpr = Record[Idx++];
461 FD->EndRangeLoc = ReadSourceLocation(Record, Idx);
463 // Read in the parameters.
464 unsigned NumParams = Record[Idx++];
465 SmallVector<ParmVarDecl *, 16> Params;
466 Params.reserve(NumParams);
467 for (unsigned I = 0; I != NumParams; ++I)
468 Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
469 FD->setParams(Reader.getContext(), Params);
472 void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
475 // In practice, this won't be executed (since method definitions
476 // don't occur in header files).
477 MD->setBody(Reader.ReadStmt(F));
478 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx));
479 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx));
481 MD->setInstanceMethod(Record[Idx++]);
482 MD->setVariadic(Record[Idx++]);
483 MD->setSynthesized(Record[Idx++]);
484 MD->setDefined(Record[Idx++]);
486 MD->IsRedeclaration = Record[Idx++];
487 MD->HasRedeclaration = Record[Idx++];
488 if (MD->HasRedeclaration)
489 Reader.getContext().setObjCMethodRedeclaration(MD,
490 ReadDeclAs<ObjCMethodDecl>(Record, Idx));
492 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
493 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
494 MD->SetRelatedResultType(Record[Idx++]);
495 MD->setResultType(Reader.readType(F, Record, Idx));
496 MD->setResultTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
497 MD->setEndLoc(ReadSourceLocation(Record, Idx));
498 unsigned NumParams = Record[Idx++];
499 SmallVector<ParmVarDecl *, 16> Params;
500 Params.reserve(NumParams);
501 for (unsigned I = 0; I != NumParams; ++I)
502 Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
504 MD->SelLocsKind = Record[Idx++];
505 unsigned NumStoredSelLocs = Record[Idx++];
506 SmallVector<SourceLocation, 16> SelLocs;
507 SelLocs.reserve(NumStoredSelLocs);
508 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
509 SelLocs.push_back(ReadSourceLocation(Record, Idx));
511 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
514 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
516 CD->setAtStartLoc(ReadSourceLocation(Record, Idx));
517 CD->setAtEndRange(ReadSourceRange(Record, Idx));
520 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
521 VisitObjCContainerDecl(ID);
522 ID->setTypeForDecl(Reader.readType(F, Record, Idx).getTypePtrOrNull());
523 ID->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
525 // Read the directly referenced protocols and their SourceLocations.
526 unsigned NumProtocols = Record[Idx++];
527 SmallVector<ObjCProtocolDecl *, 16> Protocols;
528 Protocols.reserve(NumProtocols);
529 for (unsigned I = 0; I != NumProtocols; ++I)
530 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
531 SmallVector<SourceLocation, 16> ProtoLocs;
532 ProtoLocs.reserve(NumProtocols);
533 for (unsigned I = 0; I != NumProtocols; ++I)
534 ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
535 ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
536 Reader.getContext());
538 // Read the transitive closure of protocols referenced by this class.
539 NumProtocols = Record[Idx++];
541 Protocols.reserve(NumProtocols);
542 for (unsigned I = 0; I != NumProtocols; ++I)
543 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
544 ID->AllReferencedProtocols.set(Protocols.data(), NumProtocols,
545 Reader.getContext());
548 unsigned NumIvars = Record[Idx++];
549 SmallVector<ObjCIvarDecl *, 16> IVars;
550 IVars.reserve(NumIvars);
551 for (unsigned I = 0; I != NumIvars; ++I)
552 IVars.push_back(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
553 ID->setCategoryList(ReadDeclAs<ObjCCategoryDecl>(Record, Idx));
555 // We will rebuild this list lazily.
557 ID->setForwardDecl(Record[Idx++]);
558 ID->setImplicitInterfaceDecl(Record[Idx++]);
559 ID->setSuperClassLoc(ReadSourceLocation(Record, Idx));
560 ID->setLocEnd(ReadSourceLocation(Record, Idx));
563 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
565 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
566 // This field will be built lazily.
568 bool synth = Record[Idx++];
569 IVD->setSynthesize(synth);
572 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
573 VisitObjCContainerDecl(PD);
574 PD->setForwardDecl(Record[Idx++]);
575 PD->setLocEnd(ReadSourceLocation(Record, Idx));
576 unsigned NumProtoRefs = Record[Idx++];
577 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
578 ProtoRefs.reserve(NumProtoRefs);
579 for (unsigned I = 0; I != NumProtoRefs; ++I)
580 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
581 SmallVector<SourceLocation, 16> ProtoLocs;
582 ProtoLocs.reserve(NumProtoRefs);
583 for (unsigned I = 0; I != NumProtoRefs; ++I)
584 ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
585 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
586 Reader.getContext());
589 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
593 void ASTDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
595 ObjCInterfaceDecl *ClassRef = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
596 SourceLocation SLoc = ReadSourceLocation(Record, Idx);
597 CD->setClass(Reader.getContext(), ClassRef, SLoc);
600 void ASTDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
602 unsigned NumProtoRefs = Record[Idx++];
603 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
604 ProtoRefs.reserve(NumProtoRefs);
605 for (unsigned I = 0; I != NumProtoRefs; ++I)
606 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
607 SmallVector<SourceLocation, 16> ProtoLocs;
608 ProtoLocs.reserve(NumProtoRefs);
609 for (unsigned I = 0; I != NumProtoRefs; ++I)
610 ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
611 FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
612 Reader.getContext());
615 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
616 VisitObjCContainerDecl(CD);
617 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
618 unsigned NumProtoRefs = Record[Idx++];
619 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
620 ProtoRefs.reserve(NumProtoRefs);
621 for (unsigned I = 0; I != NumProtoRefs; ++I)
622 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
623 SmallVector<SourceLocation, 16> ProtoLocs;
624 ProtoLocs.reserve(NumProtoRefs);
625 for (unsigned I = 0; I != NumProtoRefs; ++I)
626 ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
627 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
628 Reader.getContext());
629 CD->NextClassCategory = ReadDeclAs<ObjCCategoryDecl>(Record, Idx);
630 CD->setHasSynthBitfield(Record[Idx++]);
631 CD->setCategoryNameLoc(ReadSourceLocation(Record, Idx));
634 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
636 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
639 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
641 D->setAtLoc(ReadSourceLocation(Record, Idx));
642 D->setType(GetTypeSourceInfo(Record, Idx));
643 // FIXME: stable encoding
644 D->setPropertyAttributes(
645 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
646 D->setPropertyAttributesAsWritten(
647 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
648 // FIXME: stable encoding
649 D->setPropertyImplementation(
650 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
651 D->setGetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
652 D->setSetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
653 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
654 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
655 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
658 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
659 VisitObjCContainerDecl(D);
660 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
663 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
664 VisitObjCImplDecl(D);
665 D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx));
668 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
669 VisitObjCImplDecl(D);
670 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
671 llvm::tie(D->IvarInitializers, D->NumIvarInitializers)
672 = Reader.ReadCXXCtorInitializers(F, Record, Idx);
673 D->setHasSynthBitfield(Record[Idx++]);
677 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
679 D->setAtLoc(ReadSourceLocation(Record, Idx));
680 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>(Record, Idx));
681 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>(Record, Idx);
682 D->IvarLoc = ReadSourceLocation(Record, Idx);
683 D->setGetterCXXConstructor(Reader.ReadExpr(F));
684 D->setSetterCXXAssignment(Reader.ReadExpr(F));
687 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
688 VisitDeclaratorDecl(FD);
689 FD->setMutable(Record[Idx++]);
690 int BitWidthOrInitializer = Record[Idx++];
691 if (BitWidthOrInitializer == 1)
692 FD->setBitWidth(Reader.ReadExpr(F));
693 else if (BitWidthOrInitializer == 2)
694 FD->setInClassInitializer(Reader.ReadExpr(F));
695 if (!FD->getDeclName()) {
696 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>(Record, Idx))
697 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
701 void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
704 FD->ChainingSize = Record[Idx++];
705 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
706 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
708 for (unsigned I = 0; I != FD->ChainingSize; ++I)
709 FD->Chaining[I] = ReadDeclAs<NamedDecl>(Record, Idx);
712 void ASTDeclReader::VisitVarDecl(VarDecl *VD) {
713 VisitDeclaratorDecl(VD);
714 VisitRedeclarable(VD);
715 VD->VarDeclBits.SClass = (StorageClass)Record[Idx++];
716 VD->VarDeclBits.SClassAsWritten = (StorageClass)Record[Idx++];
717 VD->VarDeclBits.ThreadSpecified = Record[Idx++];
718 VD->VarDeclBits.HasCXXDirectInit = Record[Idx++];
719 VD->VarDeclBits.ExceptionVar = Record[Idx++];
720 VD->VarDeclBits.NRVOVariable = Record[Idx++];
721 VD->VarDeclBits.CXXForRangeDecl = Record[Idx++];
722 VD->VarDeclBits.ARCPseudoStrong = Record[Idx++];
724 VD->setInit(Reader.ReadExpr(F));
726 if (Record[Idx++]) { // HasMemberSpecializationInfo.
727 VarDecl *Tmpl = ReadDeclAs<VarDecl>(Record, Idx);
728 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
729 SourceLocation POI = ReadSourceLocation(Record, Idx);
730 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
734 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
738 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
740 unsigned isObjCMethodParam = Record[Idx++];
741 unsigned scopeDepth = Record[Idx++];
742 unsigned scopeIndex = Record[Idx++];
743 unsigned declQualifier = Record[Idx++];
744 if (isObjCMethodParam) {
745 assert(scopeDepth == 0);
746 PD->setObjCMethodScopeInfo(scopeIndex);
747 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
749 PD->setScopeInfo(scopeDepth, scopeIndex);
751 PD->ParmVarDeclBits.IsKNRPromoted = Record[Idx++];
752 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record[Idx++];
753 if (Record[Idx++]) // hasUninstantiatedDefaultArg.
754 PD->setUninstantiatedDefaultArg(Reader.ReadExpr(F));
757 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
759 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(F)));
760 AD->setRParenLoc(ReadSourceLocation(Record, Idx));
763 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
765 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(F)));
766 BD->setSignatureAsWritten(GetTypeSourceInfo(Record, Idx));
767 unsigned NumParams = Record[Idx++];
768 SmallVector<ParmVarDecl *, 16> Params;
769 Params.reserve(NumParams);
770 for (unsigned I = 0; I != NumParams; ++I)
771 Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
772 BD->setParams(Params);
774 bool capturesCXXThis = Record[Idx++];
775 unsigned numCaptures = Record[Idx++];
776 SmallVector<BlockDecl::Capture, 16> captures;
777 captures.reserve(numCaptures);
778 for (unsigned i = 0; i != numCaptures; ++i) {
779 VarDecl *decl = ReadDeclAs<VarDecl>(Record, Idx);
780 unsigned flags = Record[Idx++];
781 bool byRef = (flags & 1);
782 bool nested = (flags & 2);
783 Expr *copyExpr = ((flags & 4) ? Reader.ReadExpr(F) : 0);
785 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
787 BD->setCaptures(Reader.getContext(), captures.begin(),
788 captures.end(), capturesCXXThis);
791 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
793 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
794 D->setExternLoc(ReadSourceLocation(Record, Idx));
795 D->setRBraceLoc(ReadSourceLocation(Record, Idx));
798 void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
800 D->setLocStart(ReadSourceLocation(Record, Idx));
804 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
806 D->IsInline = Record[Idx++];
807 D->LocStart = ReadSourceLocation(Record, Idx);
808 D->RBraceLoc = ReadSourceLocation(Record, Idx);
809 D->NextNamespace = Record[Idx++];
811 bool IsOriginal = Record[Idx++];
812 // FIXME: Modules will likely have trouble with pointing directly at
813 // the original namespace.
814 D->OrigOrAnonNamespace.setInt(IsOriginal);
815 D->OrigOrAnonNamespace.setPointer(ReadDeclAs<NamespaceDecl>(Record, Idx));
818 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
820 D->NamespaceLoc = ReadSourceLocation(Record, Idx);
821 D->IdentLoc = ReadSourceLocation(Record, Idx);
822 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
823 D->Namespace = ReadDeclAs<NamedDecl>(Record, Idx);
826 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
828 D->setUsingLocation(ReadSourceLocation(Record, Idx));
829 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
830 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
831 D->FirstUsingShadow = ReadDeclAs<UsingShadowDecl>(Record, Idx);
832 D->setTypeName(Record[Idx++]);
833 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>(Record, Idx))
834 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
837 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
839 D->setTargetDecl(ReadDeclAs<NamedDecl>(Record, Idx));
840 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(Record, Idx);
841 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(Record, Idx);
843 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
846 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
848 D->UsingLoc = ReadSourceLocation(Record, Idx);
849 D->NamespaceLoc = ReadSourceLocation(Record, Idx);
850 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
851 D->NominatedNamespace = ReadDeclAs<NamedDecl>(Record, Idx);
852 D->CommonAncestor = ReadDeclAs<DeclContext>(Record, Idx);
855 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
857 D->setUsingLoc(ReadSourceLocation(Record, Idx));
858 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
859 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
862 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
863 UnresolvedUsingTypenameDecl *D) {
865 D->TypenameLocation = ReadSourceLocation(Record, Idx);
866 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
869 void ASTDeclReader::ReadCXXDefinitionData(
870 struct CXXRecordDecl::DefinitionData &Data,
871 const RecordData &Record, unsigned &Idx) {
872 Data.UserDeclaredConstructor = Record[Idx++];
873 Data.UserDeclaredCopyConstructor = Record[Idx++];
874 Data.UserDeclaredMoveConstructor = Record[Idx++];
875 Data.UserDeclaredCopyAssignment = Record[Idx++];
876 Data.UserDeclaredMoveAssignment = Record[Idx++];
877 Data.UserDeclaredDestructor = Record[Idx++];
878 Data.Aggregate = Record[Idx++];
879 Data.PlainOldData = Record[Idx++];
880 Data.Empty = Record[Idx++];
881 Data.Polymorphic = Record[Idx++];
882 Data.Abstract = Record[Idx++];
883 Data.IsStandardLayout = Record[Idx++];
884 Data.HasNoNonEmptyBases = Record[Idx++];
885 Data.HasPrivateFields = Record[Idx++];
886 Data.HasProtectedFields = Record[Idx++];
887 Data.HasPublicFields = Record[Idx++];
888 Data.HasMutableFields = Record[Idx++];
889 Data.HasTrivialDefaultConstructor = Record[Idx++];
890 Data.HasConstexprNonCopyMoveConstructor = Record[Idx++];
891 Data.HasTrivialCopyConstructor = Record[Idx++];
892 Data.HasTrivialMoveConstructor = Record[Idx++];
893 Data.HasTrivialCopyAssignment = Record[Idx++];
894 Data.HasTrivialMoveAssignment = Record[Idx++];
895 Data.HasTrivialDestructor = Record[Idx++];
896 Data.HasNonLiteralTypeFieldsOrBases = Record[Idx++];
897 Data.ComputedVisibleConversions = Record[Idx++];
898 Data.UserProvidedDefaultConstructor = Record[Idx++];
899 Data.DeclaredDefaultConstructor = Record[Idx++];
900 Data.DeclaredCopyConstructor = Record[Idx++];
901 Data.DeclaredMoveConstructor = Record[Idx++];
902 Data.DeclaredCopyAssignment = Record[Idx++];
903 Data.DeclaredMoveAssignment = Record[Idx++];
904 Data.DeclaredDestructor = Record[Idx++];
905 Data.FailedImplicitMoveConstructor = Record[Idx++];
906 Data.FailedImplicitMoveAssignment = Record[Idx++];
908 Data.NumBases = Record[Idx++];
910 Data.Bases = Reader.readCXXBaseSpecifiers(F, Record, Idx);
911 Data.NumVBases = Record[Idx++];
913 Data.VBases = Reader.readCXXBaseSpecifiers(F, Record, Idx);
915 Reader.ReadUnresolvedSet(F, Data.Conversions, Record, Idx);
916 Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx);
917 assert(Data.Definition && "Data.Definition should be already set!");
918 Data.FirstFriend = ReadDeclAs<FriendDecl>(Record, Idx);
921 void ASTDeclReader::InitializeCXXDefinitionData(CXXRecordDecl *D,
922 CXXRecordDecl *DefinitionDecl,
923 const RecordData &Record,
925 ASTContext &C = Reader.getContext();
927 if (D == DefinitionDecl) {
928 D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
929 ReadCXXDefinitionData(*D->DefinitionData, Record, Idx);
930 // We read the definition info. Check if there are pending forward
931 // references that need to point to this DefinitionData pointer.
932 ASTReader::PendingForwardRefsMap::iterator
933 FindI = Reader.PendingForwardRefs.find(D);
934 if (FindI != Reader.PendingForwardRefs.end()) {
935 ASTReader::ForwardRefs &Refs = FindI->second;
936 for (ASTReader::ForwardRefs::iterator
937 I = Refs.begin(), E = Refs.end(); I != E; ++I)
938 (*I)->DefinitionData = D->DefinitionData;
940 // We later check whether PendingForwardRefs is empty to make sure all
941 // pending references were linked.
942 Reader.PendingForwardRefs.erase(D);
945 } else if (DefinitionDecl) {
946 if (DefinitionDecl->DefinitionData) {
947 D->DefinitionData = DefinitionDecl->DefinitionData;
949 // The definition is still initializing.
950 Reader.PendingForwardRefs[DefinitionDecl].push_back(D);
955 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
958 CXXRecordDecl *DefinitionDecl = ReadDeclAs<CXXRecordDecl>(Record, Idx);
959 InitializeCXXDefinitionData(D, DefinitionDecl, Record, Idx);
961 ASTContext &C = Reader.getContext();
964 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
966 switch ((CXXRecKind)Record[Idx++]) {
968 llvm_unreachable("Out of sync with ASTDeclWriter::VisitCXXRecordDecl?");
969 case CXXRecNotTemplate:
972 D->TemplateOrInstantiation = ReadDeclAs<ClassTemplateDecl>(Record, Idx);
974 case CXXRecMemberSpecialization: {
975 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(Record, Idx);
976 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
977 SourceLocation POI = ReadSourceLocation(Record, Idx);
978 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
979 MSI->setPointOfInstantiation(POI);
980 D->TemplateOrInstantiation = MSI;
985 // Load the key function to avoid deserializing every method so we can
987 if (D->IsCompleteDefinition) {
988 if (CXXMethodDecl *Key = ReadDeclAs<CXXMethodDecl>(Record, Idx))
989 C.KeyFunctions[D] = Key;
993 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
994 VisitFunctionDecl(D);
995 unsigned NumOverridenMethods = Record[Idx++];
996 while (NumOverridenMethods--) {
997 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
998 // MD may be initializing.
999 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>(Record, Idx))
1000 Reader.getContext().addOverriddenMethod(D, MD);
1004 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1005 VisitCXXMethodDecl(D);
1007 D->IsExplicitSpecified = Record[Idx++];
1008 D->ImplicitlyDefined = Record[Idx++];
1009 llvm::tie(D->CtorInitializers, D->NumCtorInitializers)
1010 = Reader.ReadCXXCtorInitializers(F, Record, Idx);
1013 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1014 VisitCXXMethodDecl(D);
1016 D->ImplicitlyDefined = Record[Idx++];
1017 D->OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx);
1020 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
1021 VisitCXXMethodDecl(D);
1022 D->IsExplicitSpecified = Record[Idx++];
1025 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
1027 D->setColonLoc(ReadSourceLocation(Record, Idx));
1030 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
1033 D->Friend = GetTypeSourceInfo(Record, Idx);
1035 D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1036 D->NextFriend = Record[Idx++];
1037 D->UnsupportedFriend = (Record[Idx++] != 0);
1038 D->FriendLoc = ReadSourceLocation(Record, Idx);
1041 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1043 unsigned NumParams = Record[Idx++];
1044 D->NumParams = NumParams;
1045 D->Params = new TemplateParameterList*[NumParams];
1046 for (unsigned i = 0; i != NumParams; ++i)
1047 D->Params[i] = Reader.ReadTemplateParameterList(F, Record, Idx);
1048 if (Record[Idx++]) // HasFriendDecl
1049 D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1051 D->Friend = GetTypeSourceInfo(Record, Idx);
1052 D->FriendLoc = ReadSourceLocation(Record, Idx);
1055 void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
1058 NamedDecl *TemplatedDecl = ReadDeclAs<NamedDecl>(Record, Idx);
1059 TemplateParameterList* TemplateParams
1060 = Reader.ReadTemplateParameterList(F, Record, Idx);
1061 D->init(TemplatedDecl, TemplateParams);
1064 void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1065 // Initialize CommonOrPrev before VisitTemplateDecl so that getCommonPtr()
1066 // can be used while this is still initializing.
1068 assert(D->CommonOrPrev.isNull() && "getCommonPtr was called earlier on this");
1069 DeclID PreviousDeclID = ReadDeclID(Record, Idx);
1070 DeclID FirstDeclID = PreviousDeclID ? ReadDeclID(Record, Idx) : 0;
1071 // We delay loading of the redeclaration chain to avoid deeply nested calls.
1072 // We temporarily set the first (canonical) declaration as the previous one
1073 // which is the one that matters and mark the real previous DeclID to be
1074 // loaded & attached later on.
1075 RedeclarableTemplateDecl *FirstDecl =
1076 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(FirstDeclID));
1077 assert((FirstDecl == 0 || FirstDecl->getKind() == D->getKind()) &&
1078 "FirstDecl kind mismatch");
1080 D->CommonOrPrev = FirstDecl;
1081 // Mark the real previous DeclID to be loaded & attached later on.
1082 if (PreviousDeclID != FirstDeclID)
1083 Reader.PendingPreviousDecls.push_back(std::make_pair(D, PreviousDeclID));
1085 D->CommonOrPrev = D->newCommon(Reader.getContext());
1086 if (RedeclarableTemplateDecl *RTD
1087 = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx)) {
1088 assert(RTD->getKind() == D->getKind() &&
1089 "InstantiatedFromMemberTemplate kind mismatch");
1090 D->setInstantiatedFromMemberTemplateImpl(RTD);
1092 D->setMemberSpecialization();
1095 RedeclarableTemplateDecl *LatestDecl
1096 = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx);
1098 // This decl is a first one and the latest declaration that it points to is
1099 // in the same AST file. However, if this actually needs to point to a
1100 // redeclaration in another AST file, we need to update it by checking
1101 // the FirstLatestDeclIDs map which tracks this kind of decls.
1102 assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
1103 ASTReader::FirstLatestDeclIDMap::iterator I
1104 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1105 if (I != Reader.FirstLatestDeclIDs.end()) {
1106 if (Decl *NewLatest = Reader.GetDecl(I->second))
1107 LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
1110 assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
1111 D->getCommonPtr()->Latest = LatestDecl;
1114 VisitTemplateDecl(D);
1115 D->IdentifierNamespace = Record[Idx++];
1118 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1119 VisitRedeclarableTemplateDecl(D);
1121 if (D->getPreviousDeclaration() == 0) {
1122 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1123 // the specializations.
1124 SmallVector<serialization::DeclID, 2> SpecIDs;
1125 SpecIDs.push_back(0);
1128 unsigned Size = Record[Idx++];
1130 for (unsigned I = 0; I != Size; ++I)
1131 SpecIDs.push_back(ReadDeclID(Record, Idx));
1133 // Partial specializations.
1134 Size = Record[Idx++];
1136 for (unsigned I = 0; I != Size; ++I)
1137 SpecIDs.push_back(ReadDeclID(Record, Idx));
1140 typedef serialization::DeclID DeclID;
1142 ClassTemplateDecl::Common *CommonPtr = D->getCommonPtr();
1143 CommonPtr->LazySpecializations
1144 = new (Reader.getContext()) DeclID [SpecIDs.size()];
1145 memcpy(CommonPtr->LazySpecializations, SpecIDs.data(),
1146 SpecIDs.size() * sizeof(DeclID));
1149 // InjectedClassNameType is computed.
1153 void ASTDeclReader::VisitClassTemplateSpecializationDecl(
1154 ClassTemplateSpecializationDecl *D) {
1155 VisitCXXRecordDecl(D);
1157 ASTContext &C = Reader.getContext();
1158 if (Decl *InstD = ReadDecl(Record, Idx)) {
1159 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
1160 D->SpecializedTemplate = CTD;
1162 SmallVector<TemplateArgument, 8> TemplArgs;
1163 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1164 TemplateArgumentList *ArgList
1165 = TemplateArgumentList::CreateCopy(C, TemplArgs.data(),
1167 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
1168 = new (C) ClassTemplateSpecializationDecl::
1169 SpecializedPartialSpecialization();
1170 PS->PartialSpecialization
1171 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
1172 PS->TemplateArgs = ArgList;
1173 D->SpecializedTemplate = PS;
1178 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) {
1179 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
1180 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
1181 ExplicitInfo->TypeAsWritten = TyInfo;
1182 ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx);
1183 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx);
1184 D->ExplicitInfo = ExplicitInfo;
1187 SmallVector<TemplateArgument, 8> TemplArgs;
1188 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1189 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs.data(),
1191 D->PointOfInstantiation = ReadSourceLocation(Record, Idx);
1192 D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++];
1194 if (D->isCanonicalDecl()) { // It's kept in the folding set.
1195 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(Record,Idx);
1196 if (ClassTemplatePartialSpecializationDecl *Partial
1197 = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
1198 CanonPattern->getCommonPtr()->PartialSpecializations.InsertNode(Partial);
1200 CanonPattern->getCommonPtr()->Specializations.InsertNode(D);
1205 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
1206 ClassTemplatePartialSpecializationDecl *D) {
1207 VisitClassTemplateSpecializationDecl(D);
1209 ASTContext &C = Reader.getContext();
1210 D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx);
1212 unsigned NumArgs = Record[Idx++];
1214 D->NumArgsAsWritten = NumArgs;
1215 D->ArgsAsWritten = new (C) TemplateArgumentLoc[NumArgs];
1216 for (unsigned i=0; i != NumArgs; ++i)
1217 D->ArgsAsWritten[i] = Reader.ReadTemplateArgumentLoc(F, Record, Idx);
1220 D->SequenceNumber = Record[Idx++];
1222 // These are read/set from/to the first declaration.
1223 if (D->getPreviousDeclaration() == 0) {
1224 D->InstantiatedFromMember.setPointer(
1225 ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx));
1226 D->InstantiatedFromMember.setInt(Record[Idx++]);
1230 void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
1231 ClassScopeFunctionSpecializationDecl *D) {
1233 D->Specialization = ReadDeclAs<CXXMethodDecl>(Record, Idx);
1236 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1237 VisitRedeclarableTemplateDecl(D);
1239 if (D->getPreviousDeclaration() == 0) {
1240 // This FunctionTemplateDecl owns a CommonPtr; read it.
1242 // Read the function specialization declarations.
1243 // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1244 // when reading the specialized FunctionDecl.
1245 unsigned NumSpecs = Record[Idx++];
1247 (void)ReadDecl(Record, Idx);
1251 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1254 D->setDeclaredWithTypename(Record[Idx++]);
1256 bool Inherited = Record[Idx++];
1257 TypeSourceInfo *DefArg = GetTypeSourceInfo(Record, Idx);
1258 D->setDefaultArgument(DefArg, Inherited);
1261 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1262 VisitDeclaratorDecl(D);
1263 // TemplateParmPosition.
1264 D->setDepth(Record[Idx++]);
1265 D->setPosition(Record[Idx++]);
1266 if (D->isExpandedParameterPack()) {
1267 void **Data = reinterpret_cast<void **>(D + 1);
1268 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1269 Data[2*I] = Reader.readType(F, Record, Idx).getAsOpaquePtr();
1270 Data[2*I + 1] = GetTypeSourceInfo(Record, Idx);
1273 // Rest of NonTypeTemplateParmDecl.
1274 D->ParameterPack = Record[Idx++];
1275 if (Record[Idx++]) {
1276 Expr *DefArg = Reader.ReadExpr(F);
1277 bool Inherited = Record[Idx++];
1278 D->setDefaultArgument(DefArg, Inherited);
1283 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1284 VisitTemplateDecl(D);
1285 // TemplateParmPosition.
1286 D->setDepth(Record[Idx++]);
1287 D->setPosition(Record[Idx++]);
1288 // Rest of TemplateTemplateParmDecl.
1289 TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(F, Record, Idx);
1290 bool IsInherited = Record[Idx++];
1291 D->setDefaultArgument(Arg, IsInherited);
1292 D->ParameterPack = Record[Idx++];
1295 void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1296 VisitRedeclarableTemplateDecl(D);
1299 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
1301 D->AssertExpr = Reader.ReadExpr(F);
1302 D->Message = cast<StringLiteral>(Reader.ReadExpr(F));
1303 D->RParenLoc = ReadSourceLocation(Record, Idx);
1306 std::pair<uint64_t, uint64_t>
1307 ASTDeclReader::VisitDeclContext(DeclContext *DC) {
1308 uint64_t LexicalOffset = Record[Idx++];
1309 uint64_t VisibleOffset = Record[Idx++];
1310 return std::make_pair(LexicalOffset, VisibleOffset);
1313 template <typename T>
1314 void ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1315 enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1316 RedeclKind Kind = (RedeclKind)Record[Idx++];
1319 llvm_unreachable("Out of sync with ASTDeclWriter::VisitRedeclarable or"
1320 " messed up reading");
1321 case NoRedeclaration:
1323 case PointsToPrevious: {
1324 DeclID PreviousDeclID = ReadDeclID(Record, Idx);
1325 DeclID FirstDeclID = ReadDeclID(Record, Idx);
1326 // We delay loading of the redeclaration chain to avoid deeply nested calls.
1327 // We temporarily set the first (canonical) declaration as the previous one
1328 // which is the one that matters and mark the real previous DeclID to be
1329 // loaded & attached later on.
1330 D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1331 cast_or_null<T>(Reader.GetDecl(FirstDeclID)));
1332 if (PreviousDeclID != FirstDeclID)
1333 Reader.PendingPreviousDecls.push_back(std::make_pair(static_cast<T*>(D),
1337 case PointsToLatest:
1338 D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1339 ReadDeclAs<T>(Record, Idx));
1343 assert(!(Kind == PointsToPrevious &&
1344 Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1345 Reader.FirstLatestDeclIDs.end()) &&
1346 "This decl is not first, it should not be in the map");
1347 if (Kind == PointsToPrevious)
1350 // This decl is a first one and the latest declaration that it points to is in
1351 // the same AST file. However, if this actually needs to point to a
1352 // redeclaration in another AST file, we need to update it by checking the
1353 // FirstLatestDeclIDs map which tracks this kind of decls.
1354 assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1355 "Invalid ThisDeclID ?");
1356 ASTReader::FirstLatestDeclIDMap::iterator I
1357 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1358 if (I != Reader.FirstLatestDeclIDs.end()) {
1359 Decl *NewLatest = Reader.GetDecl(I->second);
1361 = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1365 //===----------------------------------------------------------------------===//
1366 // Attribute Reading
1367 //===----------------------------------------------------------------------===//
1369 /// \brief Reads attributes from the current stream position.
1370 void ASTReader::ReadAttributes(Module &F, AttrVec &Attrs,
1371 const RecordData &Record, unsigned &Idx) {
1372 for (unsigned i = 0, e = Record[Idx++]; i != e; ++i) {
1374 attr::Kind Kind = (attr::Kind)Record[Idx++];
1375 SourceRange Range = ReadSourceRange(F, Record, Idx);
1377 #include "clang/Serialization/AttrPCHRead.inc"
1379 assert(New && "Unable to decode attribute?");
1380 Attrs.push_back(New);
1384 //===----------------------------------------------------------------------===//
1385 // ASTReader Implementation
1386 //===----------------------------------------------------------------------===//
1388 /// \brief Note that we have loaded the declaration with the given
1391 /// This routine notes that this declaration has already been loaded,
1392 /// so that future GetDecl calls will return this declaration rather
1393 /// than trying to load a new declaration.
1394 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
1395 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
1396 DeclsLoaded[Index] = D;
1400 /// \brief Determine whether the consumer will be interested in seeing
1401 /// this declaration (via HandleTopLevelDecl).
1403 /// This routine should return true for anything that might affect
1404 /// code generation, e.g., inline function definitions, Objective-C
1405 /// declarations with metadata, etc.
1406 static bool isConsumerInterestedIn(Decl *D) {
1407 // An ObjCMethodDecl is never considered as "interesting" because its
1408 // implementation container always is.
1410 if (isa<FileScopeAsmDecl>(D) ||
1411 isa<ObjCProtocolDecl>(D) ||
1412 isa<ObjCImplDecl>(D))
1414 if (VarDecl *Var = dyn_cast<VarDecl>(D))
1415 return Var->isFileVarDecl() &&
1416 Var->isThisDeclarationADefinition() == VarDecl::Definition;
1417 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1418 return Func->doesThisDeclarationHaveABody();
1423 /// \brief Get the correct cursor and offset for loading a declaration.
1424 ASTReader::RecordLocation
1425 ASTReader::DeclCursorForID(DeclID ID) {
1426 // See if there's an override.
1427 DeclReplacementMap::iterator It = ReplacedDecls.find(ID);
1428 if (It != ReplacedDecls.end())
1429 return RecordLocation(It->second.first, It->second.second);
1431 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
1432 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
1433 Module *M = I->second;
1434 return RecordLocation(M,
1435 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]);
1438 ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
1439 ContinuousRangeMap<uint64_t, Module*, 4>::iterator I
1440 = GlobalBitOffsetsMap.find(GlobalOffset);
1442 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
1443 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
1446 uint64_t ASTReader::getGlobalBitOffset(Module &M, uint32_t LocalOffset) {
1447 return LocalOffset + M.GlobalBitOffset;
1450 void ASTDeclReader::attachPreviousDecl(Decl *D, Decl *previous) {
1451 assert(D && previous);
1452 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1453 TD->RedeclLink.setPointer(cast<TagDecl>(previous));
1454 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1455 FD->RedeclLink.setPointer(cast<FunctionDecl>(previous));
1456 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1457 VD->RedeclLink.setPointer(cast<VarDecl>(previous));
1459 RedeclarableTemplateDecl *TD = cast<RedeclarableTemplateDecl>(D);
1460 TD->CommonOrPrev = cast<RedeclarableTemplateDecl>(previous);
1464 void ASTReader::loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID) {
1465 Decl *previous = GetDecl(ID);
1466 ASTDeclReader::attachPreviousDecl(D, previous);
1469 /// \brief Read the declaration at the given offset from the AST file.
1470 Decl *ASTReader::ReadDeclRecord(DeclID ID) {
1471 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
1472 RecordLocation Loc = DeclCursorForID(ID);
1473 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
1474 // Keep track of where we are in the stream, then jump back there
1475 // after reading this declaration.
1476 SavedStreamPosition SavedPosition(DeclsCursor);
1478 ReadingKindTracker ReadingKind(Read_Decl, *this);
1480 // Note that we are loading a declaration record.
1481 Deserializing ADecl(this);
1483 DeclsCursor.JumpToBit(Loc.Offset);
1485 unsigned Code = DeclsCursor.ReadCode();
1487 ASTDeclReader Reader(*this, *Loc.F, DeclsCursor, ID, Record, Idx);
1490 switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
1491 case DECL_CONTEXT_LEXICAL:
1492 case DECL_CONTEXT_VISIBLE:
1493 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
1495 D = TypedefDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1498 case DECL_TYPEALIAS:
1499 D = TypeAliasDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1503 D = EnumDecl::Create(Context, Decl::EmptyShell());
1506 D = RecordDecl::Create(Context, Decl::EmptyShell());
1508 case DECL_ENUM_CONSTANT:
1509 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1513 D = FunctionDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1514 DeclarationName(), QualType(), 0);
1516 case DECL_LINKAGE_SPEC:
1517 D = LinkageSpecDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1518 (LinkageSpecDecl::LanguageIDs)0,
1522 D = LabelDecl::Create(Context, 0, SourceLocation(), 0);
1524 case DECL_NAMESPACE:
1525 D = NamespaceDecl::Create(Context, 0, SourceLocation(),
1526 SourceLocation(), 0);
1528 case DECL_NAMESPACE_ALIAS:
1529 D = NamespaceAliasDecl::Create(Context, 0, SourceLocation(),
1530 SourceLocation(), 0,
1531 NestedNameSpecifierLoc(),
1532 SourceLocation(), 0);
1535 D = UsingDecl::Create(Context, 0, SourceLocation(),
1536 NestedNameSpecifierLoc(), DeclarationNameInfo(),
1539 case DECL_USING_SHADOW:
1540 D = UsingShadowDecl::Create(Context, 0, SourceLocation(), 0, 0);
1542 case DECL_USING_DIRECTIVE:
1543 D = UsingDirectiveDecl::Create(Context, 0, SourceLocation(),
1544 SourceLocation(), NestedNameSpecifierLoc(),
1545 SourceLocation(), 0, 0);
1547 case DECL_UNRESOLVED_USING_VALUE:
1548 D = UnresolvedUsingValueDecl::Create(Context, 0, SourceLocation(),
1549 NestedNameSpecifierLoc(),
1550 DeclarationNameInfo());
1552 case DECL_UNRESOLVED_USING_TYPENAME:
1553 D = UnresolvedUsingTypenameDecl::Create(Context, 0, SourceLocation(),
1555 NestedNameSpecifierLoc(),
1559 case DECL_CXX_RECORD:
1560 D = CXXRecordDecl::Create(Context, Decl::EmptyShell());
1562 case DECL_CXX_METHOD:
1563 D = CXXMethodDecl::Create(Context, 0, SourceLocation(),
1564 DeclarationNameInfo(), QualType(), 0,
1565 false, SC_None, false, false, SourceLocation());
1567 case DECL_CXX_CONSTRUCTOR:
1568 D = CXXConstructorDecl::Create(Context, Decl::EmptyShell());
1570 case DECL_CXX_DESTRUCTOR:
1571 D = CXXDestructorDecl::Create(Context, Decl::EmptyShell());
1573 case DECL_CXX_CONVERSION:
1574 D = CXXConversionDecl::Create(Context, Decl::EmptyShell());
1576 case DECL_ACCESS_SPEC:
1577 D = AccessSpecDecl::Create(Context, Decl::EmptyShell());
1580 D = FriendDecl::Create(Context, Decl::EmptyShell());
1582 case DECL_FRIEND_TEMPLATE:
1583 D = FriendTemplateDecl::Create(Context, Decl::EmptyShell());
1585 case DECL_CLASS_TEMPLATE:
1586 D = ClassTemplateDecl::Create(Context, Decl::EmptyShell());
1588 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
1589 D = ClassTemplateSpecializationDecl::Create(Context, Decl::EmptyShell());
1591 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
1592 D = ClassTemplatePartialSpecializationDecl::Create(Context,
1593 Decl::EmptyShell());
1595 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
1596 D = ClassScopeFunctionSpecializationDecl::Create(Context,
1597 Decl::EmptyShell());
1599 case DECL_FUNCTION_TEMPLATE:
1600 D = FunctionTemplateDecl::Create(Context, Decl::EmptyShell());
1602 case DECL_TEMPLATE_TYPE_PARM:
1603 D = TemplateTypeParmDecl::Create(Context, Decl::EmptyShell());
1605 case DECL_NON_TYPE_TEMPLATE_PARM:
1606 D = NonTypeTemplateParmDecl::Create(Context, 0, SourceLocation(),
1607 SourceLocation(), 0, 0, 0, QualType(),
1610 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
1611 D = NonTypeTemplateParmDecl::Create(Context, 0, SourceLocation(),
1612 SourceLocation(), 0, 0, 0, QualType(),
1613 0, 0, Record[Idx++], 0);
1615 case DECL_TEMPLATE_TEMPLATE_PARM:
1616 D = TemplateTemplateParmDecl::Create(Context, 0, SourceLocation(), 0, 0,
1619 case DECL_TYPE_ALIAS_TEMPLATE:
1620 D = TypeAliasTemplateDecl::Create(Context, Decl::EmptyShell());
1622 case DECL_STATIC_ASSERT:
1623 D = StaticAssertDecl::Create(Context, 0, SourceLocation(), 0, 0,
1627 case DECL_OBJC_METHOD:
1628 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
1629 Selector(), QualType(), 0, 0);
1631 case DECL_OBJC_INTERFACE:
1632 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
1634 case DECL_OBJC_IVAR:
1635 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1636 0, QualType(), 0, ObjCIvarDecl::None);
1638 case DECL_OBJC_PROTOCOL:
1639 D = ObjCProtocolDecl::Create(Context, 0, 0, SourceLocation(),
1642 case DECL_OBJC_AT_DEFS_FIELD:
1643 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(),
1644 SourceLocation(), 0, QualType(), 0);
1646 case DECL_OBJC_CLASS:
1647 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
1649 case DECL_OBJC_FORWARD_PROTOCOL:
1650 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
1652 case DECL_OBJC_CATEGORY:
1653 D = ObjCCategoryDecl::Create(Context, Decl::EmptyShell());
1655 case DECL_OBJC_CATEGORY_IMPL:
1656 D = ObjCCategoryImplDecl::Create(Context, 0, 0, 0, SourceLocation(),
1659 case DECL_OBJC_IMPLEMENTATION:
1660 D = ObjCImplementationDecl::Create(Context, 0, 0, 0, SourceLocation(),
1663 case DECL_OBJC_COMPATIBLE_ALIAS:
1664 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
1666 case DECL_OBJC_PROPERTY:
1667 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, SourceLocation(),
1670 case DECL_OBJC_PROPERTY_IMPL:
1671 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
1672 SourceLocation(), 0,
1673 ObjCPropertyImplDecl::Dynamic, 0,
1677 D = FieldDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1678 QualType(), 0, 0, false, false);
1680 case DECL_INDIRECTFIELD:
1681 D = IndirectFieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1685 D = VarDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1686 QualType(), 0, SC_None, SC_None);
1689 case DECL_IMPLICIT_PARAM:
1690 D = ImplicitParamDecl::Create(Context, 0, SourceLocation(), 0, QualType());
1694 D = ParmVarDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1695 QualType(), 0, SC_None, SC_None, 0);
1697 case DECL_FILE_SCOPE_ASM:
1698 D = FileScopeAsmDecl::Create(Context, 0, 0, SourceLocation(),
1702 D = BlockDecl::Create(Context, 0, SourceLocation());
1704 case DECL_CXX_BASE_SPECIFIERS:
1705 Error("attempt to read a C++ base-specifier record as a declaration");
1709 assert(D && "Unknown declaration reading AST file");
1710 LoadedDecl(Index, D);
1713 // If this declaration is also a declaration context, get the
1714 // offsets for its tables of lexical and visible declarations.
1715 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1716 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1717 if (Offsets.first || Offsets.second) {
1718 if (Offsets.first != 0)
1719 DC->setHasExternalLexicalStorage(true);
1720 if (Offsets.second != 0)
1721 DC->setHasExternalVisibleStorage(true);
1722 if (ReadDeclContextStorage(*Loc.F, DeclsCursor, Offsets,
1723 Loc.F->DeclContextInfos[DC]))
1727 // Now add the pending visible updates for this decl context, if it has any.
1728 DeclContextVisibleUpdatesPending::iterator I =
1729 PendingVisibleUpdates.find(ID);
1730 if (I != PendingVisibleUpdates.end()) {
1731 // There are updates. This means the context has external visible
1732 // storage, even if the original stored version didn't.
1733 DC->setHasExternalVisibleStorage(true);
1734 DeclContextVisibleUpdates &U = I->second;
1735 for (DeclContextVisibleUpdates::iterator UI = U.begin(), UE = U.end();
1737 UI->second->DeclContextInfos[DC].NameLookupTableData = UI->first;
1739 PendingVisibleUpdates.erase(I);
1742 assert(Idx == Record.size());
1744 // Load any relevant update records.
1745 loadDeclUpdateRecords(ID, D);
1747 if (ObjCChainedCategoriesInterfaces.count(ID))
1748 loadObjCChainedCategories(ID, cast<ObjCInterfaceDecl>(D));
1750 // If we have deserialized a declaration that has a definition the
1751 // AST consumer might need to know about, queue it.
1752 // We don't pass it to the consumer immediately because we may be in recursive
1753 // loading, and some declarations may still be initializing.
1754 if (isConsumerInterestedIn(D))
1755 InterestingDecls.push_back(D);
1760 void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
1761 // The declaration may have been modified by files later in the chain.
1762 // If this is the case, read the record containing the updates from each file
1763 // and pass it to ASTDeclReader to make the modifications.
1764 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
1765 if (UpdI != DeclUpdateOffsets.end()) {
1766 FileOffsetsTy &UpdateOffsets = UpdI->second;
1767 for (FileOffsetsTy::iterator
1768 I = UpdateOffsets.begin(), E = UpdateOffsets.end(); I != E; ++I) {
1769 Module *F = I->first;
1770 uint64_t Offset = I->second;
1771 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
1772 SavedStreamPosition SavedPosition(Cursor);
1773 Cursor.JumpToBit(Offset);
1775 unsigned Code = Cursor.ReadCode();
1776 unsigned RecCode = Cursor.ReadRecord(Code, Record);
1778 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
1781 ASTDeclReader Reader(*this, *F, Cursor, ID, Record, Idx);
1782 Reader.UpdateDecl(D, *F, Record);
1788 /// \brief Given an ObjC interface, goes through the modules and links to the
1789 /// interface all the categories for it.
1790 class ObjCChainedCategoriesVisitor {
1792 serialization::GlobalDeclID InterfaceID;
1793 ObjCInterfaceDecl *Interface;
1794 ObjCCategoryDecl *GlobHeadCat, *GlobTailCat;
1795 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
1798 ObjCChainedCategoriesVisitor(ASTReader &Reader,
1799 serialization::GlobalDeclID InterfaceID,
1800 ObjCInterfaceDecl *Interface)
1801 : Reader(Reader), InterfaceID(InterfaceID), Interface(Interface),
1802 GlobHeadCat(0), GlobTailCat(0) { }
1804 static bool visit(Module &M, void *UserData) {
1805 return static_cast<ObjCChainedCategoriesVisitor *>(UserData)->visit(M);
1808 bool visit(Module &M) {
1809 if (Reader.isDeclIDFromModule(InterfaceID, M))
1810 return true; // We reached the module where the interface originated
1811 // from. Stop traversing the imported modules.
1813 Module::ChainedObjCCategoriesMap::iterator
1814 I = M.ChainedObjCCategories.find(InterfaceID);
1815 if (I == M.ChainedObjCCategories.end())
1819 HeadCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.first);
1821 TailCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.second);
1823 addCategories(HeadCat, TailCat);
1827 void addCategories(ObjCCategoryDecl *HeadCat,
1828 ObjCCategoryDecl *TailCat = 0) {
1836 while (TailCat->getNextClassCategory())
1837 TailCat = TailCat->getNextClassCategory();
1841 GlobHeadCat = HeadCat;
1842 GlobTailCat = TailCat;
1844 ASTDeclReader::setNextObjCCategory(GlobTailCat, HeadCat);
1845 GlobTailCat = TailCat;
1848 llvm::DenseSet<DeclarationName> Checked;
1849 for (ObjCCategoryDecl *Cat = HeadCat,
1850 *CatEnd = TailCat->getNextClassCategory();
1851 Cat != CatEnd; Cat = Cat->getNextClassCategory()) {
1852 if (Checked.count(Cat->getDeclName()))
1854 Checked.insert(Cat->getDeclName());
1855 checkForDuplicate(Cat);
1859 /// \brief Warns for duplicate categories that come from different modules.
1860 void checkForDuplicate(ObjCCategoryDecl *Cat) {
1861 DeclarationName Name = Cat->getDeclName();
1862 // Find the top category with the same name. We do not want to warn for
1863 // duplicates along the established chain because there were already
1864 // warnings for them when the module was created. We only want to warn for
1865 // duplicates between non-dependent modules:
1871 // We want to warn for duplicates between ML and MR,not between ML and MT.
1873 // FIXME: We should not warn for duplicates in diamond:
1881 // If there are duplicates in ML/MR, there will be warning when creating
1882 // MB *and* when importing MB. We should not warn when importing.
1883 for (ObjCCategoryDecl *Next = Cat->getNextClassCategory(); Next;
1884 Next = Next->getNextClassCategory()) {
1885 if (Next->getDeclName() == Name)
1889 ObjCCategoryDecl *&PrevCat = NameCategoryMap[Name];
1893 if (PrevCat != Cat) {
1894 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
1895 << Interface->getDeclName() << Name;
1896 Reader.Diag(PrevCat->getLocation(), diag::note_previous_definition);
1900 ObjCCategoryDecl *getHeadCategory() const { return GlobHeadCat; }
1904 void ASTReader::loadObjCChainedCategories(serialization::GlobalDeclID ID,
1905 ObjCInterfaceDecl *D) {
1906 ObjCChainedCategoriesVisitor Visitor(*this, ID, D);
1907 ModuleMgr.visit(ObjCChainedCategoriesVisitor::visit, &Visitor);
1908 // Also add the categories that the interface already links to.
1909 Visitor.addCategories(D->getCategoryList());
1910 D->setCategoryList(Visitor.getHeadCategory());
1913 void ASTDeclReader::UpdateDecl(Decl *D, Module &Module,
1914 const RecordData &Record) {
1916 while (Idx < Record.size()) {
1917 switch ((DeclUpdateKind)Record[Idx++]) {
1918 case UPD_CXX_SET_DEFINITIONDATA: {
1919 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1920 CXXRecordDecl *DefinitionDecl
1921 = Reader.ReadDeclAs<CXXRecordDecl>(Module, Record, Idx);
1922 assert(!RD->DefinitionData && "DefinitionData is already set!");
1923 InitializeCXXDefinitionData(RD, DefinitionDecl, Record, Idx);
1927 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
1928 cast<CXXRecordDecl>(D)->addedMember(Reader.ReadDecl(Module, Record, Idx));
1931 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
1932 // It will be added to the template's specializations set when loaded.
1933 (void)Reader.ReadDecl(Module, Record, Idx);
1936 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
1938 = Reader.ReadDeclAs<NamespaceDecl>(Module, Record, Idx);
1939 // Guard against these being loaded out of original order. Don't use
1940 // getNextNamespace(), since it tries to access the context and can't in
1941 // the middle of deserialization.
1942 if (!Anon->NextNamespace) {
1943 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
1944 TU->setAnonymousNamespace(Anon);
1946 cast<NamespaceDecl>(D)->OrigOrAnonNamespace.setPointer(Anon);
1951 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
1952 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
1953 Reader.ReadSourceLocation(Module, Record, Idx));