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 "clang/Serialization/ASTReader.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/AST/DeclGroup.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 using namespace clang;
24 using namespace clang::serialization;
26 //===----------------------------------------------------------------------===//
27 // Declaration deserialization
28 //===----------------------------------------------------------------------===//
31 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
33 llvm::BitstreamCursor &Cursor;
34 const DeclID ThisDeclID;
35 const ASTReader::RecordData &Record;
37 TypeID TypeIDForTypeDecl;
39 uint64_t GetCurrentCursorOffset();
42 ASTDeclReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
43 DeclID thisDeclID, const ASTReader::RecordData &Record,
45 : Reader(Reader), Cursor(Cursor), ThisDeclID(thisDeclID), Record(Record),
46 Idx(Idx), TypeIDForTypeDecl(0) { }
50 void VisitDecl(Decl *D);
51 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
52 void VisitNamedDecl(NamedDecl *ND);
53 void VisitNamespaceDecl(NamespaceDecl *D);
54 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
55 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
56 void VisitTypeDecl(TypeDecl *TD);
57 void VisitTypedefDecl(TypedefDecl *TD);
58 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
59 void VisitTagDecl(TagDecl *TD);
60 void VisitEnumDecl(EnumDecl *ED);
61 void VisitRecordDecl(RecordDecl *RD);
62 void VisitCXXRecordDecl(CXXRecordDecl *D);
63 void VisitClassTemplateSpecializationDecl(
64 ClassTemplateSpecializationDecl *D);
65 void VisitClassTemplatePartialSpecializationDecl(
66 ClassTemplatePartialSpecializationDecl *D);
67 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
68 void VisitValueDecl(ValueDecl *VD);
69 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
70 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
71 void VisitDeclaratorDecl(DeclaratorDecl *DD);
72 void VisitFunctionDecl(FunctionDecl *FD);
73 void VisitCXXMethodDecl(CXXMethodDecl *D);
74 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
75 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
76 void VisitCXXConversionDecl(CXXConversionDecl *D);
77 void VisitFieldDecl(FieldDecl *FD);
78 void VisitVarDecl(VarDecl *VD);
79 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
80 void VisitParmVarDecl(ParmVarDecl *PD);
81 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
82 void VisitTemplateDecl(TemplateDecl *D);
83 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
84 void VisitClassTemplateDecl(ClassTemplateDecl *D);
85 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
86 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
87 void VisitUsingDecl(UsingDecl *D);
88 void VisitUsingShadowDecl(UsingShadowDecl *D);
89 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
90 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
91 void VisitAccessSpecDecl(AccessSpecDecl *D);
92 void VisitFriendDecl(FriendDecl *D);
93 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
94 void VisitStaticAssertDecl(StaticAssertDecl *D);
95 void VisitBlockDecl(BlockDecl *BD);
97 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
98 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
100 // FIXME: Reorder according to DeclNodes.td?
101 void VisitObjCMethodDecl(ObjCMethodDecl *D);
102 void VisitObjCContainerDecl(ObjCContainerDecl *D);
103 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
104 void VisitObjCIvarDecl(ObjCIvarDecl *D);
105 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
106 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
107 void VisitObjCClassDecl(ObjCClassDecl *D);
108 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
109 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
110 void VisitObjCImplDecl(ObjCImplDecl *D);
111 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
112 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
113 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
114 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
115 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
119 uint64_t ASTDeclReader::GetCurrentCursorOffset() {
121 for (unsigned I = 0, N = Reader.Chain.size(); I != N; ++I) {
122 ASTReader::PerFileData &F = *Reader.Chain[N - I - 1];
123 if (&Cursor == &F.DeclsCursor) {
124 Off += F.DeclsCursor.GetCurrentBitNo();
132 void ASTDeclReader::Visit(Decl *D) {
133 DeclVisitor<ASTDeclReader, void>::Visit(D);
135 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
136 // if we have a fully initialized TypeDecl, we can safely read its type now.
137 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtr());
138 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
139 // FunctionDecl's body was written last after all other Stmts/Exprs.
141 FD->setLazyBody(GetCurrentCursorOffset());
145 void ASTDeclReader::VisitDecl(Decl *D) {
146 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
147 D->setLexicalDeclContext(
148 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
149 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
150 D->setInvalidDecl(Record[Idx++]);
153 Reader.ReadAttributes(Cursor, Attrs);
156 D->setImplicit(Record[Idx++]);
157 D->setUsed(Record[Idx++]);
158 D->setAccess((AccessSpecifier)Record[Idx++]);
159 D->setPCHLevel(Record[Idx++] + 1);
162 void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
164 TU->setAnonymousNamespace(
165 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
168 void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
170 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
173 void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
175 // Delay type reading until after we have fully initialized the decl.
176 TypeIDForTypeDecl = Record[Idx++];
179 void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
181 TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
184 void ASTDeclReader::VisitTagDecl(TagDecl *TD) {
186 TD->IdentifierNamespace = Record[Idx++];
187 VisitRedeclarable(TD);
188 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
189 TD->setDefinition(Record[Idx++]);
190 TD->setEmbeddedInDeclarator(Record[Idx++]);
191 TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
192 TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
193 // FIXME: maybe read optional qualifier and its range.
194 TD->setTypedefForAnonDecl(
195 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
198 void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
200 ED->setIntegerType(Reader.GetType(Record[Idx++]));
201 ED->setPromotionType(Reader.GetType(Record[Idx++]));
202 ED->setNumPositiveBits(Record[Idx++]);
203 ED->setNumNegativeBits(Record[Idx++]);
204 ED->setInstantiationOfMemberEnum(
205 cast_or_null<EnumDecl>(Reader.GetDecl(Record[Idx++])));
208 void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
210 RD->setHasFlexibleArrayMember(Record[Idx++]);
211 RD->setAnonymousStructOrUnion(Record[Idx++]);
212 RD->setHasObjectMember(Record[Idx++]);
215 void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
217 VD->setType(Reader.GetType(Record[Idx++]));
220 void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
223 ECD->setInitExpr(Reader.ReadExpr(Cursor));
224 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
227 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
229 TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
231 DD->setTypeSourceInfo(TInfo);
232 // FIXME: read optional qualifier and its range.
235 void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
236 VisitDeclaratorDecl(FD);
237 // FIXME: read DeclarationNameLoc.
239 FD->IdentifierNamespace = Record[Idx++];
240 switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
241 default: assert(false && "Unhandled TemplatedKind!");
243 case FunctionDecl::TK_NonTemplate:
245 case FunctionDecl::TK_FunctionTemplate:
246 FD->setDescribedFunctionTemplate(
247 cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++])));
249 case FunctionDecl::TK_MemberSpecialization: {
250 FunctionDecl *InstFD = cast<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
251 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
252 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
253 FD->setInstantiationOfMemberFunction(InstFD, TSK);
254 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
257 case FunctionDecl::TK_FunctionTemplateSpecialization: {
258 FunctionTemplateDecl *Template
259 = cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++]));
260 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
262 // Template arguments.
263 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
264 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
266 // Template args as written.
267 llvm::SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
268 SourceLocation LAngleLoc, RAngleLoc;
269 if (Record[Idx++]) { // TemplateArgumentsAsWritten != 0
270 unsigned NumTemplateArgLocs = Record[Idx++];
271 TemplArgLocs.reserve(NumTemplateArgLocs);
272 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
273 TemplArgLocs.push_back(
274 Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
276 LAngleLoc = Reader.ReadSourceLocation(Record, Idx);
277 RAngleLoc = Reader.ReadSourceLocation(Record, Idx);
280 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
282 if (FD->isCanonicalDecl()) // if canonical add to template's set.
283 FD->setFunctionTemplateSpecialization(Template, TemplArgs.size(),
284 TemplArgs.data(), TSK,
287 LAngleLoc, RAngleLoc, POI);
290 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
292 UnresolvedSet<8> TemplDecls;
293 unsigned NumTemplates = Record[Idx++];
294 while (NumTemplates--)
295 TemplDecls.addDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
298 TemplateArgumentListInfo TemplArgs;
299 unsigned NumArgs = Record[Idx++];
301 TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Cursor,Record, Idx));
302 TemplArgs.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
303 TemplArgs.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
305 FD->setDependentTemplateSpecialization(*Reader.getContext(),
306 TemplDecls, TemplArgs);
311 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
312 // after everything else is read.
314 VisitRedeclarable(FD);
315 FD->setStorageClass((StorageClass)Record[Idx++]);
316 FD->setStorageClassAsWritten((StorageClass)Record[Idx++]);
317 FD->setInlineSpecified(Record[Idx++]);
318 FD->setVirtualAsWritten(Record[Idx++]);
319 FD->setPure(Record[Idx++]);
320 FD->setHasInheritedPrototype(Record[Idx++]);
321 FD->setHasWrittenPrototype(Record[Idx++]);
322 FD->setDeleted(Record[Idx++]);
323 FD->setTrivial(Record[Idx++]);
324 FD->setCopyAssignment(Record[Idx++]);
325 FD->setHasImplicitReturnZero(Record[Idx++]);
326 FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
328 // Read in the parameters.
329 unsigned NumParams = Record[Idx++];
330 llvm::SmallVector<ParmVarDecl *, 16> Params;
331 Params.reserve(NumParams);
332 for (unsigned I = 0; I != NumParams; ++I)
333 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
334 FD->setParams(Params.data(), NumParams);
337 void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
340 // In practice, this won't be executed (since method definitions
341 // don't occur in header files).
342 MD->setBody(Reader.ReadStmt(Cursor));
343 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
344 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
346 MD->setInstanceMethod(Record[Idx++]);
347 MD->setVariadic(Record[Idx++]);
348 MD->setSynthesized(Record[Idx++]);
349 MD->setDefined(Record[Idx++]);
350 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
351 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
352 MD->setNumSelectorArgs(unsigned(Record[Idx++]));
353 MD->setResultType(Reader.GetType(Record[Idx++]));
354 MD->setResultTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
355 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
356 unsigned NumParams = Record[Idx++];
357 llvm::SmallVector<ParmVarDecl *, 16> Params;
358 Params.reserve(NumParams);
359 for (unsigned I = 0; I != NumParams; ++I)
360 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
361 MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams,
365 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
367 SourceLocation A = SourceLocation::getFromRawEncoding(Record[Idx++]);
368 SourceLocation B = SourceLocation::getFromRawEncoding(Record[Idx++]);
369 CD->setAtEndRange(SourceRange(A, B));
372 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
373 VisitObjCContainerDecl(ID);
374 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
375 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
376 (Reader.GetDecl(Record[Idx++])));
378 // Read the directly referenced protocols and their SourceLocations.
379 unsigned NumProtocols = Record[Idx++];
380 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
381 Protocols.reserve(NumProtocols);
382 for (unsigned I = 0; I != NumProtocols; ++I)
383 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
384 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
385 ProtoLocs.reserve(NumProtocols);
386 for (unsigned I = 0; I != NumProtocols; ++I)
387 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
388 ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
389 *Reader.getContext());
391 // Read the transitive closure of protocols referenced by this class.
392 NumProtocols = Record[Idx++];
394 Protocols.reserve(NumProtocols);
395 for (unsigned I = 0; I != NumProtocols; ++I)
396 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
397 ID->AllReferencedProtocols.set(Protocols.data(), NumProtocols,
398 *Reader.getContext());
401 unsigned NumIvars = Record[Idx++];
402 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
403 IVars.reserve(NumIvars);
404 for (unsigned I = 0; I != NumIvars; ++I)
405 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
407 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
408 // We will rebuild this list lazily.
410 ID->setForwardDecl(Record[Idx++]);
411 ID->setImplicitInterfaceDecl(Record[Idx++]);
412 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
413 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
414 ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
417 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
419 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
420 // This field will be built lazily.
422 bool synth = Record[Idx++];
423 IVD->setSynthesize(synth);
426 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
427 VisitObjCContainerDecl(PD);
428 PD->setForwardDecl(Record[Idx++]);
429 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
430 unsigned NumProtoRefs = Record[Idx++];
431 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
432 ProtoRefs.reserve(NumProtoRefs);
433 for (unsigned I = 0; I != NumProtoRefs; ++I)
434 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
435 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
436 ProtoLocs.reserve(NumProtoRefs);
437 for (unsigned I = 0; I != NumProtoRefs; ++I)
438 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
439 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
440 *Reader.getContext());
443 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
447 void ASTDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
449 unsigned NumClassRefs = Record[Idx++];
450 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
451 ClassRefs.reserve(NumClassRefs);
452 for (unsigned I = 0; I != NumClassRefs; ++I)
453 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
454 llvm::SmallVector<SourceLocation, 16> SLocs;
455 SLocs.reserve(NumClassRefs);
456 for (unsigned I = 0; I != NumClassRefs; ++I)
457 SLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
458 CD->setClassList(*Reader.getContext(), ClassRefs.data(), SLocs.data(),
462 void ASTDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
464 unsigned NumProtoRefs = Record[Idx++];
465 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
466 ProtoRefs.reserve(NumProtoRefs);
467 for (unsigned I = 0; I != NumProtoRefs; ++I)
468 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
469 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
470 ProtoLocs.reserve(NumProtoRefs);
471 for (unsigned I = 0; I != NumProtoRefs; ++I)
472 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
473 FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
474 *Reader.getContext());
477 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
478 VisitObjCContainerDecl(CD);
479 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
480 unsigned NumProtoRefs = Record[Idx++];
481 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
482 ProtoRefs.reserve(NumProtoRefs);
483 for (unsigned I = 0; I != NumProtoRefs; ++I)
484 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
485 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
486 ProtoLocs.reserve(NumProtoRefs);
487 for (unsigned I = 0; I != NumProtoRefs; ++I)
488 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
489 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
490 *Reader.getContext());
491 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
492 CD->setHasSynthBitfield(Record[Idx++]);
493 CD->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
494 CD->setCategoryNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
497 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
499 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
502 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
504 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
505 D->setType(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
506 // FIXME: stable encoding
507 D->setPropertyAttributes(
508 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
509 D->setPropertyAttributesAsWritten(
510 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
511 // FIXME: stable encoding
512 D->setPropertyImplementation(
513 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
514 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
515 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
516 D->setGetterMethodDecl(
517 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
518 D->setSetterMethodDecl(
519 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
520 D->setPropertyIvarDecl(
521 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
524 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
525 VisitObjCContainerDecl(D);
526 D->setClassInterface(
527 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
530 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
531 VisitObjCImplDecl(D);
532 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
535 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
536 VisitObjCImplDecl(D);
538 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
539 llvm::tie(D->IvarInitializers, D->NumIvarInitializers)
540 = Reader.ReadCXXBaseOrMemberInitializers(Cursor, Record, Idx);
541 D->setHasSynthBitfield(Record[Idx++]);
545 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
547 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
549 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
550 D->setPropertyIvarDecl(
551 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
552 D->setGetterCXXConstructor(Reader.ReadExpr(Cursor));
553 D->setSetterCXXAssignment(Reader.ReadExpr(Cursor));
556 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
557 VisitDeclaratorDecl(FD);
558 FD->setMutable(Record[Idx++]);
560 FD->setBitWidth(Reader.ReadExpr(Cursor));
561 if (!FD->getDeclName()) {
562 FieldDecl *Tmpl = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++]));
564 Reader.getContext()->setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
568 void ASTDeclReader::VisitVarDecl(VarDecl *VD) {
569 VisitDeclaratorDecl(VD);
570 VD->setStorageClass((StorageClass)Record[Idx++]);
571 VD->setStorageClassAsWritten((StorageClass)Record[Idx++]);
572 VD->setThreadSpecified(Record[Idx++]);
573 VD->setCXXDirectInitializer(Record[Idx++]);
574 VD->setExceptionVariable(Record[Idx++]);
575 VD->setNRVOVariable(Record[Idx++]);
576 VisitRedeclarable(VD);
578 VD->setInit(Reader.ReadExpr(Cursor));
580 if (Record[Idx++]) { // HasMemberSpecializationInfo.
581 VarDecl *Tmpl = cast<VarDecl>(Reader.GetDecl(Record[Idx++]));
582 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
583 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
584 Reader.getContext()->setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
588 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
592 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
594 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
595 PD->setHasInheritedDefaultArg(Record[Idx++]);
596 if (Record[Idx++]) // hasUninstantiatedDefaultArg.
597 PD->setUninstantiatedDefaultArg(Reader.ReadExpr(Cursor));
600 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
602 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(Cursor)));
605 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
607 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(Cursor)));
608 BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
609 unsigned NumParams = Record[Idx++];
610 llvm::SmallVector<ParmVarDecl *, 16> Params;
611 Params.reserve(NumParams);
612 for (unsigned I = 0; I != NumParams; ++I)
613 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
614 BD->setParams(Params.data(), NumParams);
617 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
619 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
620 D->setHasBraces(Record[Idx++]);
623 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
625 D->setLBracLoc(Reader.ReadSourceLocation(Record, Idx));
626 D->setRBracLoc(Reader.ReadSourceLocation(Record, Idx));
628 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
630 bool IsOriginal = Record[Idx++];
631 D->OrigOrAnonNamespace.setInt(IsOriginal);
632 D->OrigOrAnonNamespace.setPointer(
633 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
636 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
638 D->NamespaceLoc = Reader.ReadSourceLocation(Record, Idx);
639 D->setQualifierRange(Reader.ReadSourceRange(Record, Idx));
640 D->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx));
641 D->IdentLoc = Reader.ReadSourceLocation(Record, Idx);
642 D->Namespace = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
645 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
647 D->setUsingLocation(Reader.ReadSourceLocation(Record, Idx));
648 D->setNestedNameRange(Reader.ReadSourceRange(Record, Idx));
649 D->setTargetNestedNameDecl(Reader.ReadNestedNameSpecifier(Record, Idx));
650 // FIXME: read the DNLoc component.
652 // FIXME: It would probably be more efficient to read these into a vector
653 // and then re-cosntruct the shadow decl set over that vector since it
654 // would avoid existence checks.
655 unsigned NumShadows = Record[Idx++];
656 for(unsigned I = 0; I != NumShadows; ++I) {
657 // Avoid invariant checking of UsingDecl::addShadowDecl, the decl may still
659 D->Shadows.insert(cast<UsingShadowDecl>(Reader.GetDecl(Record[Idx++])));
661 D->setTypeName(Record[Idx++]);
662 NamedDecl *Pattern = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
664 Reader.getContext()->setInstantiatedFromUsingDecl(D, Pattern);
667 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
669 D->setTargetDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
670 D->setUsingDecl(cast<UsingDecl>(Reader.GetDecl(Record[Idx++])));
671 UsingShadowDecl *Pattern
672 = cast_or_null<UsingShadowDecl>(Reader.GetDecl(Record[Idx++]));
674 Reader.getContext()->setInstantiatedFromUsingShadowDecl(D, Pattern);
677 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
679 D->UsingLoc = Reader.ReadSourceLocation(Record, Idx);
680 D->NamespaceLoc = Reader.ReadSourceLocation(Record, Idx);
681 D->QualifierRange = Reader.ReadSourceRange(Record, Idx);
682 D->Qualifier = Reader.ReadNestedNameSpecifier(Record, Idx);
683 D->NominatedNamespace = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
684 D->CommonAncestor = cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]));
687 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
689 D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
690 D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
691 D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
692 // FIXME: read the DNLoc component.
695 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
696 UnresolvedUsingTypenameDecl *D) {
698 D->TargetNestedNameRange = Reader.ReadSourceRange(Record, Idx);
699 D->UsingLocation = Reader.ReadSourceLocation(Record, Idx);
700 D->TypenameLocation = Reader.ReadSourceLocation(Record, Idx);
701 D->TargetNestedNameSpecifier = Reader.ReadNestedNameSpecifier(Record, Idx);
704 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
705 ASTContext &C = *Reader.getContext();
707 // We need to allocate the DefinitionData struct ahead of VisitRecordDecl
708 // so that the other CXXRecordDecls can get a pointer even when the owner
709 // is still initializing.
710 bool OwnsDefinitionData = false;
711 enum DataOwnership { Data_NoDefData, Data_Owner, Data_NotOwner };
712 switch ((DataOwnership)Record[Idx++]) {
714 assert(0 && "Out of sync with ASTDeclWriter or messed up reading");
718 OwnsDefinitionData = true;
719 D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
723 = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]))->DefinitionData;
729 if (OwnsDefinitionData) {
730 assert(D->DefinitionData);
731 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
733 Data.UserDeclaredConstructor = Record[Idx++];
734 Data.UserDeclaredCopyConstructor = Record[Idx++];
735 Data.UserDeclaredCopyAssignment = Record[Idx++];
736 Data.UserDeclaredDestructor = Record[Idx++];
737 Data.Aggregate = Record[Idx++];
738 Data.PlainOldData = Record[Idx++];
739 Data.Empty = Record[Idx++];
740 Data.Polymorphic = Record[Idx++];
741 Data.Abstract = Record[Idx++];
742 Data.HasTrivialConstructor = Record[Idx++];
743 Data.HasTrivialCopyConstructor = Record[Idx++];
744 Data.HasTrivialCopyAssignment = Record[Idx++];
745 Data.HasTrivialDestructor = Record[Idx++];
746 Data.ComputedVisibleConversions = Record[Idx++];
747 Data.DeclaredDefaultConstructor = Record[Idx++];
748 Data.DeclaredCopyConstructor = Record[Idx++];
749 Data.DeclaredCopyAssignment = Record[Idx++];
750 Data.DeclaredDestructor = Record[Idx++];
752 // setBases() is unsuitable since it may try to iterate the bases of an
753 // uninitialized base.
754 Data.NumBases = Record[Idx++];
755 Data.Bases = new(C) CXXBaseSpecifier [Data.NumBases];
756 for (unsigned i = 0; i != Data.NumBases; ++i)
757 Data.Bases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
759 // FIXME: Make VBases lazily computed when needed to avoid storing them.
760 Data.NumVBases = Record[Idx++];
761 Data.VBases = new(C) CXXBaseSpecifier [Data.NumVBases];
762 for (unsigned i = 0; i != Data.NumVBases; ++i)
763 Data.VBases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
765 Reader.ReadUnresolvedSet(Data.Conversions, Record, Idx);
766 Reader.ReadUnresolvedSet(Data.VisibleConversions, Record, Idx);
767 assert(Data.Definition && "Data.Definition should be already set!");
769 = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
773 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
775 switch ((CXXRecKind)Record[Idx++]) {
777 assert(false && "Out of sync with ASTDeclWriter::VisitCXXRecordDecl?");
778 case CXXRecNotTemplate:
781 D->setDescribedClassTemplate(
782 cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++])));
784 case CXXRecMemberSpecialization: {
785 CXXRecordDecl *RD = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]));
786 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
787 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
788 D->setInstantiationOfMemberClass(RD, TSK);
789 D->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
795 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
796 VisitFunctionDecl(D);
797 unsigned NumOverridenMethods = Record[Idx++];
798 while (NumOverridenMethods--) {
799 CXXMethodDecl *MD = cast<CXXMethodDecl>(Reader.GetDecl(Record[Idx++]));
800 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
801 // MD may be initializing.
802 Reader.getContext()->addOverriddenMethod(D, MD);
806 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
807 VisitCXXMethodDecl(D);
809 D->IsExplicitSpecified = Record[Idx++];
810 D->ImplicitlyDefined = Record[Idx++];
811 llvm::tie(D->BaseOrMemberInitializers, D->NumBaseOrMemberInitializers)
812 = Reader.ReadCXXBaseOrMemberInitializers(Cursor, Record, Idx);
815 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
816 VisitCXXMethodDecl(D);
818 D->ImplicitlyDefined = Record[Idx++];
819 D->OperatorDelete = cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
822 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
823 VisitCXXMethodDecl(D);
824 D->IsExplicitSpecified = Record[Idx++];
827 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
829 D->setColonLoc(Reader.ReadSourceLocation(Record, Idx));
832 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
835 D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
837 D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
838 D->NextFriend = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
839 D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
842 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
844 unsigned NumParams = Record[Idx++];
845 D->NumParams = NumParams;
846 D->Params = new TemplateParameterList*[NumParams];
847 for (unsigned i = 0; i != NumParams; ++i)
848 D->Params[i] = Reader.ReadTemplateParameterList(Record, Idx);
849 if (Record[Idx++]) // HasFriendDecl
850 D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
852 D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
853 D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
856 void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
859 NamedDecl *TemplatedDecl
860 = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
861 TemplateParameterList* TemplateParams
862 = Reader.ReadTemplateParameterList(Record, Idx);
863 D->init(TemplatedDecl, TemplateParams);
866 void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
867 VisitTemplateDecl(D);
869 D->IdentifierNamespace = Record[Idx++];
870 RedeclarableTemplateDecl *PrevDecl =
871 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
872 assert((PrevDecl == 0 || PrevDecl->getKind() == D->getKind()) &&
873 "PrevDecl kind mismatch");
875 D->CommonOrPrev = PrevDecl;
877 if (RedeclarableTemplateDecl *RTD
878 = cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]))) {
879 assert(RTD->getKind() == D->getKind() &&
880 "InstantiatedFromMemberTemplate kind mismatch");
881 D->setInstantiatedFromMemberTemplateImpl(RTD);
883 D->setMemberSpecialization();
886 RedeclarableTemplateDecl *LatestDecl =
887 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
889 // This decl is a first one and the latest declaration that it points to is
890 // in the same AST file. However, if this actually needs to point to a
891 // redeclaration in another AST file, we need to update it by checking
892 // the FirstLatestDeclIDs map which tracks this kind of decls.
893 assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
894 ASTReader::FirstLatestDeclIDMap::iterator I
895 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
896 if (I != Reader.FirstLatestDeclIDs.end()) {
897 Decl *NewLatest = Reader.GetDecl(I->second);
898 assert((LatestDecl->getLocation().isInvalid() ||
899 NewLatest->getLocation().isInvalid() ||
900 Reader.SourceMgr.isBeforeInTranslationUnit(
901 LatestDecl->getLocation(),
902 NewLatest->getLocation())) &&
903 "The new latest is supposed to come after the previous latest");
904 LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
907 assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
908 D->getCommonPtr()->Latest = LatestDecl;
912 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
913 VisitRedeclarableTemplateDecl(D);
915 if (D->getPreviousDeclaration() == 0) {
916 // This ClassTemplateDecl owns a CommonPtr; read it.
918 // FoldingSets are filled in VisitClassTemplateSpecializationDecl.
919 unsigned size = Record[Idx++];
921 cast<ClassTemplateSpecializationDecl>(Reader.GetDecl(Record[Idx++]));
923 size = Record[Idx++];
925 cast<ClassTemplatePartialSpecializationDecl>(
926 Reader.GetDecl(Record[Idx++]));
928 // InjectedClassNameType is computed.
932 void ASTDeclReader::VisitClassTemplateSpecializationDecl(
933 ClassTemplateSpecializationDecl *D) {
934 VisitCXXRecordDecl(D);
936 if (Decl *InstD = Reader.GetDecl(Record[Idx++])) {
937 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
938 D->setInstantiationOf(CTD);
940 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
941 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
942 D->setInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(InstD),
943 TemplArgs.data(), TemplArgs.size());
948 if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx)) {
949 D->setTypeAsWritten(TyInfo);
950 D->setExternLoc(Reader.ReadSourceLocation(Record, Idx));
951 D->setTemplateKeywordLoc(Reader.ReadSourceLocation(Record, Idx));
954 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
955 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
956 D->initTemplateArgs(TemplArgs.data(), TemplArgs.size());
957 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
959 D->setPointOfInstantiation(POI);
960 D->setSpecializationKind((TemplateSpecializationKind)Record[Idx++]);
962 if (D->isCanonicalDecl()) { // It's kept in the folding set.
963 ClassTemplateDecl *CanonPattern
964 = cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++]));
965 if (ClassTemplatePartialSpecializationDecl *Partial
966 = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
967 CanonPattern->getPartialSpecializations().InsertNode(Partial);
969 CanonPattern->getSpecializations().InsertNode(D);
974 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
975 ClassTemplatePartialSpecializationDecl *D) {
976 VisitClassTemplateSpecializationDecl(D);
978 D->initTemplateParameters(Reader.ReadTemplateParameterList(Record, Idx));
980 TemplateArgumentListInfo ArgInfos;
981 unsigned NumArgs = Record[Idx++];
983 ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
984 D->initTemplateArgsAsWritten(ArgInfos);
986 D->setSequenceNumber(Record[Idx++]);
988 // These are read/set from/to the first declaration.
989 if (D->getPreviousDeclaration() == 0) {
990 D->setInstantiatedFromMember(
991 cast_or_null<ClassTemplatePartialSpecializationDecl>(
992 Reader.GetDecl(Record[Idx++])));
994 D->setMemberSpecialization();
998 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
999 VisitRedeclarableTemplateDecl(D);
1001 if (D->getPreviousDeclaration() == 0) {
1002 // This FunctionTemplateDecl owns a CommonPtr; read it.
1004 // Read the function specialization declarations.
1005 // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1006 // through the specialized FunctionDecl's setFunctionTemplateSpecialization.
1007 unsigned NumSpecs = Record[Idx++];
1009 Reader.GetDecl(Record[Idx++]);
1013 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1016 D->setDeclaredWithTypename(Record[Idx++]);
1017 D->setParameterPack(Record[Idx++]);
1019 bool Inherited = Record[Idx++];
1020 TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
1021 D->setDefaultArgument(DefArg, Inherited);
1024 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1026 // TemplateParmPosition.
1027 D->setDepth(Record[Idx++]);
1028 D->setPosition(Record[Idx++]);
1029 // Rest of NonTypeTemplateParmDecl.
1030 if (Record[Idx++]) {
1031 Expr *DefArg = Reader.ReadExpr(Cursor);
1032 bool Inherited = Record[Idx++];
1033 D->setDefaultArgument(DefArg, Inherited);
1037 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1038 VisitTemplateDecl(D);
1039 // TemplateParmPosition.
1040 D->setDepth(Record[Idx++]);
1041 D->setPosition(Record[Idx++]);
1042 // Rest of TemplateTemplateParmDecl.
1043 TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx);
1044 bool IsInherited = Record[Idx++];
1045 D->setDefaultArgument(Arg, IsInherited);
1048 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
1050 D->AssertExpr = Reader.ReadExpr(Cursor);
1051 D->Message = cast<StringLiteral>(Reader.ReadExpr(Cursor));
1054 std::pair<uint64_t, uint64_t>
1055 ASTDeclReader::VisitDeclContext(DeclContext *DC) {
1056 uint64_t LexicalOffset = Record[Idx++];
1057 uint64_t VisibleOffset = Record[Idx++];
1058 return std::make_pair(LexicalOffset, VisibleOffset);
1061 template <typename T>
1062 void ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1063 enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1064 RedeclKind Kind = (RedeclKind)Record[Idx++];
1067 assert(0 && "Out of sync with ASTDeclWriter::VisitRedeclarable or messed up"
1069 case NoRedeclaration:
1071 case PointsToPrevious:
1072 D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1073 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1075 case PointsToLatest:
1076 D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1077 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1081 assert(!(Kind == PointsToPrevious &&
1082 Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1083 Reader.FirstLatestDeclIDs.end()) &&
1084 "This decl is not first, it should not be in the map");
1085 if (Kind == PointsToPrevious)
1088 // This decl is a first one and the latest declaration that it points to is in
1089 // the same AST file. However, if this actually needs to point to a
1090 // redeclaration in another AST file, we need to update it by checking the
1091 // FirstLatestDeclIDs map which tracks this kind of decls.
1092 assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1093 "Invalid ThisDeclID ?");
1094 ASTReader::FirstLatestDeclIDMap::iterator I
1095 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1096 if (I != Reader.FirstLatestDeclIDs.end()) {
1097 Decl *NewLatest = Reader.GetDecl(I->second);
1098 assert((D->getMostRecentDeclaration()->getLocation().isInvalid() ||
1099 NewLatest->getLocation().isInvalid() ||
1100 Reader.SourceMgr.isBeforeInTranslationUnit(
1101 D->getMostRecentDeclaration()->getLocation(),
1102 NewLatest->getLocation())) &&
1103 "The new latest is supposed to come after the previous latest");
1105 = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1109 //===----------------------------------------------------------------------===//
1110 // Attribute Reading
1111 //===----------------------------------------------------------------------===//
1113 /// \brief Reads attributes from the current stream position.
1114 void ASTReader::ReadAttributes(llvm::BitstreamCursor &DeclsCursor,
1116 unsigned Code = DeclsCursor.ReadCode();
1117 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1118 "Expected unabbreviated record"); (void)Code;
1122 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
1123 assert(RecCode == DECL_ATTR && "Expected attribute record");
1126 while (Idx < Record.size()) {
1128 attr::Kind Kind = (attr::Kind)Record[Idx++];
1129 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[Idx++]);
1130 bool isInherited = Record[Idx++];
1132 #include "clang/Serialization/AttrPCHRead.inc"
1134 assert(New && "Unable to decode attribute?");
1135 New->setInherited(isInherited);
1136 Attrs.push_back(New);
1140 //===----------------------------------------------------------------------===//
1141 // ASTReader Implementation
1142 //===----------------------------------------------------------------------===//
1144 /// \brief Note that we have loaded the declaration with the given
1147 /// This routine notes that this declaration has already been loaded,
1148 /// so that future GetDecl calls will return this declaration rather
1149 /// than trying to load a new declaration.
1150 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
1151 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
1152 DeclsLoaded[Index] = D;
1156 /// \brief Determine whether the consumer will be interested in seeing
1157 /// this declaration (via HandleTopLevelDecl).
1159 /// This routine should return true for anything that might affect
1160 /// code generation, e.g., inline function definitions, Objective-C
1161 /// declarations with metadata, etc.
1162 static bool isConsumerInterestedIn(Decl *D) {
1163 if (isa<FileScopeAsmDecl>(D))
1165 if (VarDecl *Var = dyn_cast<VarDecl>(D))
1166 return Var->isFileVarDecl() &&
1167 Var->isThisDeclarationADefinition() == VarDecl::Definition;
1168 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1169 return Func->isThisDeclarationADefinition();
1170 return isa<ObjCProtocolDecl>(D) || isa<ObjCImplementationDecl>(D);
1173 /// \brief Get the correct cursor and offset for loading a type.
1174 ASTReader::RecordLocation
1175 ASTReader::DeclCursorForIndex(unsigned Index, DeclID ID) {
1176 // See if there's an override.
1177 DeclReplacementMap::iterator It = ReplacedDecls.find(ID);
1178 if (It != ReplacedDecls.end())
1179 return RecordLocation(&It->second.first->DeclsCursor, It->second.second);
1182 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1183 F = Chain[N - I - 1];
1184 if (Index < F->LocalNumDecls)
1186 Index -= F->LocalNumDecls;
1188 assert(F && F->LocalNumDecls > Index && "Broken chain");
1189 return RecordLocation(&F->DeclsCursor, F->DeclOffsets[Index]);
1192 /// \brief Read the declaration at the given offset from the AST file.
1193 Decl *ASTReader::ReadDeclRecord(unsigned Index, DeclID ID) {
1194 RecordLocation Loc = DeclCursorForIndex(Index, ID);
1195 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
1196 // Keep track of where we are in the stream, then jump back there
1197 // after reading this declaration.
1198 SavedStreamPosition SavedPosition(DeclsCursor);
1200 ReadingKindTracker ReadingKind(Read_Decl, *this);
1202 // Note that we are loading a declaration record.
1203 Deserializing ADecl(this);
1205 DeclsCursor.JumpToBit(Loc.second);
1207 unsigned Code = DeclsCursor.ReadCode();
1209 ASTDeclReader Reader(*this, DeclsCursor, ID, Record, Idx);
1212 switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
1214 case DECL_CONTEXT_LEXICAL:
1215 case DECL_CONTEXT_VISIBLE:
1216 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1218 case DECL_TRANSLATION_UNIT:
1219 assert(Index == 0 && "Translation unit must be at index 0");
1220 D = Context->getTranslationUnitDecl();
1223 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1226 D = EnumDecl::Create(*Context, Decl::EmptyShell());
1229 D = RecordDecl::Create(*Context, Decl::EmptyShell());
1231 case DECL_ENUM_CONSTANT:
1232 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
1236 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
1239 case DECL_LINKAGE_SPEC:
1240 D = LinkageSpecDecl::Create(*Context, 0, SourceLocation(),
1241 (LinkageSpecDecl::LanguageIDs)0,
1244 case DECL_NAMESPACE:
1245 D = NamespaceDecl::Create(*Context, 0, SourceLocation(), 0);
1247 case DECL_NAMESPACE_ALIAS:
1248 D = NamespaceAliasDecl::Create(*Context, 0, SourceLocation(),
1249 SourceLocation(), 0, SourceRange(), 0,
1250 SourceLocation(), 0);
1253 D = UsingDecl::Create(*Context, 0, SourceRange(), SourceLocation(),
1254 0, DeclarationNameInfo(), false);
1256 case DECL_USING_SHADOW:
1257 D = UsingShadowDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1259 case DECL_USING_DIRECTIVE:
1260 D = UsingDirectiveDecl::Create(*Context, 0, SourceLocation(),
1261 SourceLocation(), SourceRange(), 0,
1262 SourceLocation(), 0, 0);
1264 case DECL_UNRESOLVED_USING_VALUE:
1265 D = UnresolvedUsingValueDecl::Create(*Context, 0, SourceLocation(),
1267 DeclarationNameInfo());
1269 case DECL_UNRESOLVED_USING_TYPENAME:
1270 D = UnresolvedUsingTypenameDecl::Create(*Context, 0, SourceLocation(),
1271 SourceLocation(), SourceRange(),
1272 0, SourceLocation(),
1275 case DECL_CXX_RECORD:
1276 D = CXXRecordDecl::Create(*Context, Decl::EmptyShell());
1278 case DECL_CXX_METHOD:
1279 D = CXXMethodDecl::Create(*Context, 0, DeclarationNameInfo(),
1282 case DECL_CXX_CONSTRUCTOR:
1283 D = CXXConstructorDecl::Create(*Context, Decl::EmptyShell());
1285 case DECL_CXX_DESTRUCTOR:
1286 D = CXXDestructorDecl::Create(*Context, Decl::EmptyShell());
1288 case DECL_CXX_CONVERSION:
1289 D = CXXConversionDecl::Create(*Context, Decl::EmptyShell());
1291 case DECL_ACCESS_SPEC:
1292 D = AccessSpecDecl::Create(*Context, AS_none, 0, SourceLocation(),
1296 D = FriendDecl::Create(*Context, Decl::EmptyShell());
1298 case DECL_FRIEND_TEMPLATE:
1299 D = FriendTemplateDecl::Create(*Context, Decl::EmptyShell());
1301 case DECL_CLASS_TEMPLATE:
1302 D = ClassTemplateDecl::Create(*Context, 0, SourceLocation(),
1303 DeclarationName(), 0, 0, 0);
1305 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
1306 D = ClassTemplateSpecializationDecl::Create(*Context, Decl::EmptyShell());
1308 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
1309 D = ClassTemplatePartialSpecializationDecl::Create(*Context,
1310 Decl::EmptyShell());
1312 case DECL_FUNCTION_TEMPLATE:
1313 D = FunctionTemplateDecl::Create(*Context, 0, SourceLocation(),
1314 DeclarationName(), 0, 0);
1316 case DECL_TEMPLATE_TYPE_PARM:
1317 D = TemplateTypeParmDecl::Create(*Context, Decl::EmptyShell());
1319 case DECL_NON_TYPE_TEMPLATE_PARM:
1320 D = NonTypeTemplateParmDecl::Create(*Context, 0, SourceLocation(), 0,0,0,
1323 case DECL_TEMPLATE_TEMPLATE_PARM:
1324 D = TemplateTemplateParmDecl::Create(*Context, 0, SourceLocation(),0,0,0,0);
1326 case DECL_STATIC_ASSERT:
1327 D = StaticAssertDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1330 case DECL_OBJC_METHOD:
1331 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
1332 Selector(), QualType(), 0, 0);
1334 case DECL_OBJC_INTERFACE:
1335 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
1337 case DECL_OBJC_IVAR:
1338 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
1339 ObjCIvarDecl::None);
1341 case DECL_OBJC_PROTOCOL:
1342 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
1344 case DECL_OBJC_AT_DEFS_FIELD:
1345 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
1348 case DECL_OBJC_CLASS:
1349 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
1351 case DECL_OBJC_FORWARD_PROTOCOL:
1352 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
1354 case DECL_OBJC_CATEGORY:
1355 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(),
1356 SourceLocation(), SourceLocation(), 0);
1358 case DECL_OBJC_CATEGORY_IMPL:
1359 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1361 case DECL_OBJC_IMPLEMENTATION:
1362 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1364 case DECL_OBJC_COMPATIBLE_ALIAS:
1365 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1367 case DECL_OBJC_PROPERTY:
1368 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(),
1371 case DECL_OBJC_PROPERTY_IMPL:
1372 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
1373 SourceLocation(), 0,
1374 ObjCPropertyImplDecl::Dynamic, 0);
1377 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0, 0,
1381 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
1385 case DECL_IMPLICIT_PARAM:
1386 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
1390 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
1391 SC_None, SC_None, 0);
1393 case DECL_FILE_SCOPE_ASM:
1394 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
1397 D = BlockDecl::Create(*Context, 0, SourceLocation());
1401 assert(D && "Unknown declaration reading AST file");
1402 LoadedDecl(Index, D);
1405 // If this declaration is also a declaration context, get the
1406 // offsets for its tables of lexical and visible declarations.
1407 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1408 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1409 if (Offsets.first || Offsets.second) {
1410 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1411 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1412 DeclContextInfo Info;
1413 if (ReadDeclContextStorage(DeclsCursor, Offsets, Info))
1415 DeclContextInfos &Infos = DeclContextOffsets[DC];
1416 // Reading the TU will happen after reading its lexical update blocks,
1417 // so we need to make sure we insert in front. For all other contexts,
1418 // the vector is empty here anyway, so there's no loss in efficiency.
1419 Infos.insert(Infos.begin(), Info);
1421 // Now add the pending visible updates for this decl context, if it has
1423 DeclContextVisibleUpdatesPending::iterator I =
1424 PendingVisibleUpdates.find(ID);
1425 if (I != PendingVisibleUpdates.end()) {
1426 DeclContextVisibleUpdates &U = I->second;
1427 Info.LexicalDecls = 0;
1428 Info.NumLexicalDecls = 0;
1429 for (DeclContextVisibleUpdates::iterator UI = U.begin(), UE = U.end();
1431 Info.NameLookupTableData = *UI;
1432 Infos.push_back(Info);
1434 PendingVisibleUpdates.erase(I);
1439 // If this is a template, read additional specializations that may be in a
1440 // different part of the chain.
1441 if (isa<RedeclarableTemplateDecl>(D)) {
1442 AdditionalTemplateSpecializationsMap::iterator F =
1443 AdditionalTemplateSpecializationsPending.find(ID);
1444 if (F != AdditionalTemplateSpecializationsPending.end()) {
1445 for (AdditionalTemplateSpecializations::iterator I = F->second.begin(),
1446 E = F->second.end();
1449 AdditionalTemplateSpecializationsPending.erase(F);
1452 assert(Idx == Record.size());
1454 // If we have deserialized a declaration that has a definition the
1455 // AST consumer might need to know about, queue it.
1456 // We don't pass it to the consumer immediately because we may be in recursive
1457 // loading, and some declarations may still be initializing.
1458 if (isConsumerInterestedIn(D))
1459 InterestingDecls.push_back(D);