ASTWriterDecl.cpp revision e09a275444576deb2c8d9e2255554242f65d7c00
1//===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements serialization for Declarations. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Serialization/ASTWriter.h" 15#include "clang/AST/DeclVisitor.h" 16#include "clang/AST/DeclCXX.h" 17#include "clang/AST/DeclTemplate.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/DeclContextInternals.h" 20#include "llvm/ADT/Twine.h" 21#include "llvm/Bitcode/BitstreamWriter.h" 22#include "llvm/Support/ErrorHandling.h" 23using namespace clang; 24 25//===----------------------------------------------------------------------===// 26// Declaration serialization 27//===----------------------------------------------------------------------===// 28 29namespace clang { 30 class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> { 31 32 ASTWriter &Writer; 33 ASTContext &Context; 34 typedef ASTWriter::RecordData RecordData; 35 RecordData &Record; 36 37 public: 38 serialization::DeclCode Code; 39 unsigned AbbrevToUse; 40 41 ASTDeclWriter(ASTWriter &Writer, ASTContext &Context, RecordData &Record) 42 : Writer(Writer), Context(Context), Record(Record) { 43 } 44 45 void Visit(Decl *D); 46 47 void VisitDecl(Decl *D); 48 void VisitTranslationUnitDecl(TranslationUnitDecl *D); 49 void VisitNamedDecl(NamedDecl *D); 50 void VisitNamespaceDecl(NamespaceDecl *D); 51 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 52 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 53 void VisitTypeDecl(TypeDecl *D); 54 void VisitTypedefDecl(TypedefDecl *D); 55 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 56 void VisitTagDecl(TagDecl *D); 57 void VisitEnumDecl(EnumDecl *D); 58 void VisitRecordDecl(RecordDecl *D); 59 void VisitCXXRecordDecl(CXXRecordDecl *D); 60 void VisitClassTemplateSpecializationDecl( 61 ClassTemplateSpecializationDecl *D); 62 void VisitClassTemplatePartialSpecializationDecl( 63 ClassTemplatePartialSpecializationDecl *D); 64 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 65 void VisitValueDecl(ValueDecl *D); 66 void VisitEnumConstantDecl(EnumConstantDecl *D); 67 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 68 void VisitDeclaratorDecl(DeclaratorDecl *D); 69 void VisitFunctionDecl(FunctionDecl *D); 70 void VisitCXXMethodDecl(CXXMethodDecl *D); 71 void VisitCXXConstructorDecl(CXXConstructorDecl *D); 72 void VisitCXXDestructorDecl(CXXDestructorDecl *D); 73 void VisitCXXConversionDecl(CXXConversionDecl *D); 74 void VisitFieldDecl(FieldDecl *D); 75 void VisitVarDecl(VarDecl *D); 76 void VisitImplicitParamDecl(ImplicitParamDecl *D); 77 void VisitParmVarDecl(ParmVarDecl *D); 78 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 79 void VisitTemplateDecl(TemplateDecl *D); 80 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D); 81 void VisitClassTemplateDecl(ClassTemplateDecl *D); 82 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 83 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 84 void VisitUsingDecl(UsingDecl *D); 85 void VisitUsingShadowDecl(UsingShadowDecl *D); 86 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 87 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 88 void VisitAccessSpecDecl(AccessSpecDecl *D); 89 void VisitFriendDecl(FriendDecl *D); 90 void VisitFriendTemplateDecl(FriendTemplateDecl *D); 91 void VisitStaticAssertDecl(StaticAssertDecl *D); 92 void VisitBlockDecl(BlockDecl *D); 93 94 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 95 uint64_t VisibleOffset); 96 template <typename T> void VisitRedeclarable(Redeclarable<T> *D); 97 98 99 // FIXME: Put in the same order is DeclNodes.td? 100 void VisitObjCMethodDecl(ObjCMethodDecl *D); 101 void VisitObjCContainerDecl(ObjCContainerDecl *D); 102 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 103 void VisitObjCIvarDecl(ObjCIvarDecl *D); 104 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 105 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); 106 void VisitObjCClassDecl(ObjCClassDecl *D); 107 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); 108 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 109 void VisitObjCImplDecl(ObjCImplDecl *D); 110 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 111 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 112 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 113 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 114 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 115 }; 116} 117 118void ASTDeclWriter::Visit(Decl *D) { 119 DeclVisitor<ASTDeclWriter>::Visit(D); 120 121 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs 122 // have been written. We want it last because we will not read it back when 123 // retrieving it from the AST, we'll just lazily set the offset. 124 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 125 Record.push_back(FD->isThisDeclarationADefinition()); 126 if (FD->isThisDeclarationADefinition()) 127 Writer.AddStmt(FD->getBody()); 128 } 129} 130 131void ASTDeclWriter::VisitDecl(Decl *D) { 132 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); 133 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); 134 Writer.AddSourceLocation(D->getLocation(), Record); 135 Record.push_back(D->isInvalidDecl()); 136 Record.push_back(D->hasAttrs()); 137 if (D->hasAttrs()) 138 Writer.WriteAttributes(D->getAttrs(), Record); 139 Record.push_back(D->isImplicit()); 140 Record.push_back(D->isUsed(false)); 141 Record.push_back(D->getAccess()); 142 Record.push_back(D->getPCHLevel()); 143} 144 145void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 146 VisitDecl(D); 147 Writer.AddDeclRef(D->getAnonymousNamespace(), Record); 148 Code = serialization::DECL_TRANSLATION_UNIT; 149} 150 151void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) { 152 VisitDecl(D); 153 Writer.AddDeclarationName(D->getDeclName(), Record); 154} 155 156void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) { 157 VisitNamedDecl(D); 158 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); 159} 160 161void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) { 162 VisitTypeDecl(D); 163 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); 164 Code = serialization::DECL_TYPEDEF; 165} 166 167void ASTDeclWriter::VisitTagDecl(TagDecl *D) { 168 VisitTypeDecl(D); 169 VisitRedeclarable(D); 170 Record.push_back(D->getIdentifierNamespace()); 171 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding 172 Record.push_back(D->isDefinition()); 173 Record.push_back(D->isEmbeddedInDeclarator()); 174 Writer.AddSourceLocation(D->getRBraceLoc(), Record); 175 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record); 176 Record.push_back(D->hasExtInfo()); 177 if (D->hasExtInfo()) 178 Writer.AddQualifierInfo(*D->getExtInfo(), Record); 179 else 180 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record); 181} 182 183void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) { 184 VisitTagDecl(D); 185 Writer.AddTypeSourceInfo(D->getIntegerTypeSourceInfo(), Record); 186 if (!D->getIntegerTypeSourceInfo()) 187 Writer.AddTypeRef(D->getIntegerType(), Record); 188 Writer.AddTypeRef(D->getPromotionType(), Record); 189 Record.push_back(D->getNumPositiveBits()); 190 Record.push_back(D->getNumNegativeBits()); 191 Record.push_back(D->isScoped()); 192 Record.push_back(D->isFixed()); 193 Writer.AddDeclRef(D->getInstantiatedFromMemberEnum(), Record); 194 Code = serialization::DECL_ENUM; 195} 196 197void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) { 198 VisitTagDecl(D); 199 Record.push_back(D->hasFlexibleArrayMember()); 200 Record.push_back(D->isAnonymousStructOrUnion()); 201 Record.push_back(D->hasObjectMember()); 202 Code = serialization::DECL_RECORD; 203} 204 205void ASTDeclWriter::VisitValueDecl(ValueDecl *D) { 206 VisitNamedDecl(D); 207 Writer.AddTypeRef(D->getType(), Record); 208} 209 210void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) { 211 VisitValueDecl(D); 212 Record.push_back(D->getInitExpr()? 1 : 0); 213 if (D->getInitExpr()) 214 Writer.AddStmt(D->getInitExpr()); 215 Writer.AddAPSInt(D->getInitVal(), Record); 216 Code = serialization::DECL_ENUM_CONSTANT; 217} 218 219void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) { 220 VisitValueDecl(D); 221 Record.push_back(D->hasExtInfo()); 222 if (D->hasExtInfo()) 223 Writer.AddQualifierInfo(*D->getExtInfo(), Record); 224 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); 225} 226 227void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) { 228 VisitDeclaratorDecl(D); 229 VisitRedeclarable(D); 230 231 Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record); 232 Record.push_back(D->getIdentifierNamespace()); 233 Record.push_back(D->getTemplatedKind()); 234 switch (D->getTemplatedKind()) { 235 default: assert(false && "Unhandled TemplatedKind!"); 236 break; 237 case FunctionDecl::TK_NonTemplate: 238 break; 239 case FunctionDecl::TK_FunctionTemplate: 240 Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record); 241 break; 242 case FunctionDecl::TK_MemberSpecialization: { 243 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo(); 244 Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record); 245 Record.push_back(MemberInfo->getTemplateSpecializationKind()); 246 Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record); 247 break; 248 } 249 case FunctionDecl::TK_FunctionTemplateSpecialization: { 250 FunctionTemplateSpecializationInfo * 251 FTSInfo = D->getTemplateSpecializationInfo(); 252 Writer.AddDeclRef(FTSInfo->getTemplate(), Record); 253 Record.push_back(FTSInfo->getTemplateSpecializationKind()); 254 255 // Template arguments. 256 Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record); 257 258 // Template args as written. 259 Record.push_back(FTSInfo->TemplateArgumentsAsWritten != 0); 260 if (FTSInfo->TemplateArgumentsAsWritten) { 261 Record.push_back(FTSInfo->TemplateArgumentsAsWritten->size()); 262 for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->size(); i!=e; ++i) 263 Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i], 264 Record); 265 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getLAngleLoc(), 266 Record); 267 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getRAngleLoc(), 268 Record); 269 } 270 271 Writer.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Record); 272 273 if (D->isCanonicalDecl()) { 274 // Write the template that contains the specializations set. We will 275 // add a FunctionTemplateSpecializationInfo to it when reading. 276 Writer.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl(), Record); 277 } 278 break; 279 } 280 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { 281 DependentFunctionTemplateSpecializationInfo * 282 DFTSInfo = D->getDependentSpecializationInfo(); 283 284 // Templates. 285 Record.push_back(DFTSInfo->getNumTemplates()); 286 for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i) 287 Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record); 288 289 // Templates args. 290 Record.push_back(DFTSInfo->getNumTemplateArgs()); 291 for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i) 292 Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record); 293 Writer.AddSourceLocation(DFTSInfo->getLAngleLoc(), Record); 294 Writer.AddSourceLocation(DFTSInfo->getRAngleLoc(), Record); 295 break; 296 } 297 } 298 299 // FunctionDecl's body is handled last at ASTWriterDecl::Visit, 300 // after everything else is written. 301 302 Record.push_back(D->getStorageClass()); // FIXME: stable encoding 303 Record.push_back(D->getStorageClassAsWritten()); 304 Record.push_back(D->isInlineSpecified()); 305 Record.push_back(D->isVirtualAsWritten()); 306 Record.push_back(D->isPure()); 307 Record.push_back(D->hasInheritedPrototype()); 308 Record.push_back(D->hasWrittenPrototype()); 309 Record.push_back(D->isDeleted()); 310 Record.push_back(D->isTrivial()); 311 Record.push_back(D->hasImplicitReturnZero()); 312 Writer.AddSourceLocation(D->getLocEnd(), Record); 313 314 Record.push_back(D->param_size()); 315 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); 316 P != PEnd; ++P) 317 Writer.AddDeclRef(*P, Record); 318 Code = serialization::DECL_FUNCTION; 319} 320 321void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 322 VisitNamedDecl(D); 323 // FIXME: convert to LazyStmtPtr? 324 // Unlike C/C++, method bodies will never be in header files. 325 bool HasBodyStuff = D->getBody() != 0 || 326 D->getSelfDecl() != 0 || D->getCmdDecl() != 0; 327 Record.push_back(HasBodyStuff); 328 if (HasBodyStuff) { 329 Writer.AddStmt(D->getBody()); 330 Writer.AddDeclRef(D->getSelfDecl(), Record); 331 Writer.AddDeclRef(D->getCmdDecl(), Record); 332 } 333 Record.push_back(D->isInstanceMethod()); 334 Record.push_back(D->isVariadic()); 335 Record.push_back(D->isSynthesized()); 336 Record.push_back(D->isDefined()); 337 // FIXME: stable encoding for @required/@optional 338 Record.push_back(D->getImplementationControl()); 339 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway 340 Record.push_back(D->getObjCDeclQualifier()); 341 Record.push_back(D->getNumSelectorArgs()); 342 Writer.AddTypeRef(D->getResultType(), Record); 343 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record); 344 Writer.AddSourceLocation(D->getLocEnd(), Record); 345 Record.push_back(D->param_size()); 346 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 347 PEnd = D->param_end(); P != PEnd; ++P) 348 Writer.AddDeclRef(*P, Record); 349 Code = serialization::DECL_OBJC_METHOD; 350} 351 352void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { 353 VisitNamedDecl(D); 354 Writer.AddSourceRange(D->getAtEndRange(), Record); 355 // Abstract class (no need to define a stable serialization::DECL code). 356} 357 358void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 359 VisitObjCContainerDecl(D); 360 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); 361 Writer.AddDeclRef(D->getSuperClass(), Record); 362 363 // Write out the protocols that are directly referenced by the @interface. 364 Record.push_back(D->ReferencedProtocols.size()); 365 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), 366 PEnd = D->protocol_end(); 367 P != PEnd; ++P) 368 Writer.AddDeclRef(*P, Record); 369 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(), 370 PLEnd = D->protocol_loc_end(); 371 PL != PLEnd; ++PL) 372 Writer.AddSourceLocation(*PL, Record); 373 374 // Write out the protocols that are transitively referenced. 375 Record.push_back(D->AllReferencedProtocols.size()); 376 for (ObjCList<ObjCProtocolDecl>::iterator 377 P = D->AllReferencedProtocols.begin(), 378 PEnd = D->AllReferencedProtocols.end(); 379 P != PEnd; ++P) 380 Writer.AddDeclRef(*P, Record); 381 382 // Write out the ivars. 383 Record.push_back(D->ivar_size()); 384 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), 385 IEnd = D->ivar_end(); I != IEnd; ++I) 386 Writer.AddDeclRef(*I, Record); 387 Writer.AddDeclRef(D->getCategoryList(), Record); 388 Record.push_back(D->isForwardDecl()); 389 Record.push_back(D->isImplicitInterfaceDecl()); 390 Writer.AddSourceLocation(D->getClassLoc(), Record); 391 Writer.AddSourceLocation(D->getSuperClassLoc(), Record); 392 Writer.AddSourceLocation(D->getLocEnd(), Record); 393 Code = serialization::DECL_OBJC_INTERFACE; 394} 395 396void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 397 VisitFieldDecl(D); 398 // FIXME: stable encoding for @public/@private/@protected/@package 399 Record.push_back(D->getAccessControl()); 400 Record.push_back(D->getSynthesize()); 401 Code = serialization::DECL_OBJC_IVAR; 402} 403 404void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 405 VisitObjCContainerDecl(D); 406 Record.push_back(D->isForwardDecl()); 407 Writer.AddSourceLocation(D->getLocEnd(), Record); 408 Record.push_back(D->protocol_size()); 409 for (ObjCProtocolDecl::protocol_iterator 410 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) 411 Writer.AddDeclRef(*I, Record); 412 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(), 413 PLEnd = D->protocol_loc_end(); 414 PL != PLEnd; ++PL) 415 Writer.AddSourceLocation(*PL, Record); 416 Code = serialization::DECL_OBJC_PROTOCOL; 417} 418 419void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { 420 VisitFieldDecl(D); 421 Code = serialization::DECL_OBJC_AT_DEFS_FIELD; 422} 423 424void ASTDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) { 425 VisitDecl(D); 426 Record.push_back(D->size()); 427 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) 428 Writer.AddDeclRef(I->getInterface(), Record); 429 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) 430 Writer.AddSourceLocation(I->getLocation(), Record); 431 Code = serialization::DECL_OBJC_CLASS; 432} 433 434void ASTDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { 435 VisitDecl(D); 436 Record.push_back(D->protocol_size()); 437 for (ObjCForwardProtocolDecl::protocol_iterator 438 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) 439 Writer.AddDeclRef(*I, Record); 440 for (ObjCForwardProtocolDecl::protocol_loc_iterator 441 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end(); 442 PL != PLEnd; ++PL) 443 Writer.AddSourceLocation(*PL, Record); 444 Code = serialization::DECL_OBJC_FORWARD_PROTOCOL; 445} 446 447void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 448 VisitObjCContainerDecl(D); 449 Writer.AddDeclRef(D->getClassInterface(), Record); 450 Record.push_back(D->protocol_size()); 451 for (ObjCCategoryDecl::protocol_iterator 452 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) 453 Writer.AddDeclRef(*I, Record); 454 for (ObjCCategoryDecl::protocol_loc_iterator 455 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end(); 456 PL != PLEnd; ++PL) 457 Writer.AddSourceLocation(*PL, Record); 458 Writer.AddDeclRef(D->getNextClassCategory(), Record); 459 Record.push_back(D->hasSynthBitfield()); 460 Writer.AddSourceLocation(D->getAtLoc(), Record); 461 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record); 462 Code = serialization::DECL_OBJC_CATEGORY; 463} 464 465void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { 466 VisitNamedDecl(D); 467 Writer.AddDeclRef(D->getClassInterface(), Record); 468 Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS; 469} 470 471void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 472 VisitNamedDecl(D); 473 Writer.AddSourceLocation(D->getAtLoc(), Record); 474 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); 475 // FIXME: stable encoding 476 Record.push_back((unsigned)D->getPropertyAttributes()); 477 Record.push_back((unsigned)D->getPropertyAttributesAsWritten()); 478 // FIXME: stable encoding 479 Record.push_back((unsigned)D->getPropertyImplementation()); 480 Writer.AddDeclarationName(D->getGetterName(), Record); 481 Writer.AddDeclarationName(D->getSetterName(), Record); 482 Writer.AddDeclRef(D->getGetterMethodDecl(), Record); 483 Writer.AddDeclRef(D->getSetterMethodDecl(), Record); 484 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); 485 Code = serialization::DECL_OBJC_PROPERTY; 486} 487 488void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { 489 VisitObjCContainerDecl(D); 490 Writer.AddDeclRef(D->getClassInterface(), Record); 491 // Abstract class (no need to define a stable serialization::DECL code). 492} 493 494void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 495 VisitObjCImplDecl(D); 496 Writer.AddIdentifierRef(D->getIdentifier(), Record); 497 Code = serialization::DECL_OBJC_CATEGORY_IMPL; 498} 499 500void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 501 VisitObjCImplDecl(D); 502 Writer.AddDeclRef(D->getSuperClass(), Record); 503 Writer.AddCXXBaseOrMemberInitializers(D->IvarInitializers, 504 D->NumIvarInitializers, Record); 505 Record.push_back(D->hasSynthBitfield()); 506 Code = serialization::DECL_OBJC_IMPLEMENTATION; 507} 508 509void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 510 VisitDecl(D); 511 Writer.AddSourceLocation(D->getLocStart(), Record); 512 Writer.AddDeclRef(D->getPropertyDecl(), Record); 513 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); 514 Writer.AddStmt(D->getGetterCXXConstructor()); 515 Writer.AddStmt(D->getSetterCXXAssignment()); 516 Code = serialization::DECL_OBJC_PROPERTY_IMPL; 517} 518 519void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) { 520 VisitDeclaratorDecl(D); 521 Record.push_back(D->isMutable()); 522 Record.push_back(D->getBitWidth()? 1 : 0); 523 if (D->getBitWidth()) 524 Writer.AddStmt(D->getBitWidth()); 525 if (!D->getDeclName()) 526 Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record); 527 Code = serialization::DECL_FIELD; 528} 529 530void ASTDeclWriter::VisitVarDecl(VarDecl *D) { 531 VisitDeclaratorDecl(D); 532 VisitRedeclarable(D); 533 Record.push_back(D->getStorageClass()); // FIXME: stable encoding 534 Record.push_back(D->getStorageClassAsWritten()); 535 Record.push_back(D->isThreadSpecified()); 536 Record.push_back(D->hasCXXDirectInitializer()); 537 Record.push_back(D->isExceptionVariable()); 538 Record.push_back(D->isNRVOVariable()); 539 Record.push_back(D->getInit() ? 1 : 0); 540 if (D->getInit()) 541 Writer.AddStmt(D->getInit()); 542 543 MemberSpecializationInfo *SpecInfo 544 = D->isStaticDataMember() ? D->getMemberSpecializationInfo() : 0; 545 Record.push_back(SpecInfo != 0); 546 if (SpecInfo) { 547 Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record); 548 Record.push_back(SpecInfo->getTemplateSpecializationKind()); 549 Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record); 550 } 551 552 Code = serialization::DECL_VAR; 553} 554 555void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 556 VisitVarDecl(D); 557 Code = serialization::DECL_IMPLICIT_PARAM; 558} 559 560void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { 561 VisitVarDecl(D); 562 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding 563 Record.push_back(D->hasInheritedDefaultArg()); 564 Record.push_back(D->hasUninstantiatedDefaultArg()); 565 if (D->hasUninstantiatedDefaultArg()) 566 Writer.AddStmt(D->getUninstantiatedDefaultArg()); 567 Code = serialization::DECL_PARM_VAR; 568 569 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here 570 // we dynamically check for the properties that we optimize for, but don't 571 // know are true of all PARM_VAR_DECLs. 572 if (!D->getTypeSourceInfo() && 573 !D->hasAttrs() && 574 !D->isImplicit() && 575 !D->isUsed(false) && 576 D->getAccess() == AS_none && 577 D->getPCHLevel() == 0 && 578 D->getStorageClass() == 0 && 579 !D->hasCXXDirectInitializer() && // Can params have this ever? 580 D->getObjCDeclQualifier() == 0 && 581 !D->hasInheritedDefaultArg() && 582 D->getInit() == 0 && 583 !D->hasUninstantiatedDefaultArg()) // No default expr. 584 AbbrevToUse = Writer.getParmVarDeclAbbrev(); 585 586 // Check things we know are true of *every* PARM_VAR_DECL, which is more than 587 // just us assuming it. 588 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls"); 589 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread"); 590 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private"); 591 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var"); 592 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); 593 assert(!D->isStaticDataMember() && 594 "PARM_VAR_DECL can't be static data member"); 595} 596 597void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 598 VisitDecl(D); 599 Writer.AddStmt(D->getAsmString()); 600 Code = serialization::DECL_FILE_SCOPE_ASM; 601} 602 603void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) { 604 VisitDecl(D); 605 Writer.AddStmt(D->getBody()); 606 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record); 607 Record.push_back(D->param_size()); 608 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); 609 P != PEnd; ++P) 610 Writer.AddDeclRef(*P, Record); 611 Code = serialization::DECL_BLOCK; 612} 613 614void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 615 VisitDecl(D); 616 // FIXME: It might be nice to serialize the brace locations for this 617 // declaration, which don't seem to be readily available in the AST. 618 Record.push_back(D->getLanguage()); 619 Record.push_back(D->hasBraces()); 620 Code = serialization::DECL_LINKAGE_SPEC; 621} 622 623void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) { 624 VisitNamedDecl(D); 625 Record.push_back(D->isInline()); 626 Writer.AddSourceLocation(D->getLBracLoc(), Record); 627 Writer.AddSourceLocation(D->getRBracLoc(), Record); 628 Writer.AddDeclRef(D->getNextNamespace(), Record); 629 630 // Only write one reference--original or anonymous 631 Record.push_back(D->isOriginalNamespace()); 632 if (D->isOriginalNamespace()) 633 Writer.AddDeclRef(D->getAnonymousNamespace(), Record); 634 else 635 Writer.AddDeclRef(D->getOriginalNamespace(), Record); 636 Code = serialization::DECL_NAMESPACE; 637 638 if (Writer.hasChain() && !D->isOriginalNamespace() && 639 D->getOriginalNamespace()->getPCHLevel() > 0) { 640 NamespaceDecl *NS = D->getOriginalNamespace(); 641 Writer.AddUpdatedDeclContext(NS); 642 643 // Make sure all visible decls are written. They will be recorded later. 644 NS->lookup(DeclarationName()); 645 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(NS->getLookupPtr()); 646 if (Map) { 647 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 648 D != DEnd; ++D) { 649 DeclarationName Name = D->first; 650 DeclContext::lookup_result Result = D->second.getLookupResult(); 651 while (Result.first != Result.second) { 652 Writer.GetDeclRef(*Result.first); 653 ++Result.first; 654 } 655 } 656 } 657 } 658} 659 660void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 661 VisitNamedDecl(D); 662 Writer.AddSourceLocation(D->getNamespaceLoc(), Record); 663 Writer.AddSourceRange(D->getQualifierRange(), Record); 664 Writer.AddNestedNameSpecifier(D->getQualifier(), Record); 665 Writer.AddSourceLocation(D->getTargetNameLoc(), Record); 666 Writer.AddDeclRef(D->getNamespace(), Record); 667 Code = serialization::DECL_NAMESPACE_ALIAS; 668} 669 670void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) { 671 VisitNamedDecl(D); 672 Writer.AddSourceRange(D->getNestedNameRange(), Record); 673 Writer.AddSourceLocation(D->getUsingLocation(), Record); 674 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record); 675 Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record); 676 Record.push_back(D->getNumShadowDecls()); 677 for (UsingDecl::shadow_iterator P = D->shadow_begin(), 678 PEnd = D->shadow_end(); P != PEnd; ++P) 679 Writer.AddDeclRef(*P, Record); 680 Record.push_back(D->isTypeName()); 681 Writer.AddDeclRef(Context.getInstantiatedFromUsingDecl(D), Record); 682 Code = serialization::DECL_USING; 683} 684 685void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) { 686 VisitNamedDecl(D); 687 Writer.AddDeclRef(D->getTargetDecl(), Record); 688 Writer.AddDeclRef(D->getUsingDecl(), Record); 689 Writer.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D), Record); 690 Code = serialization::DECL_USING_SHADOW; 691} 692 693void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 694 VisitNamedDecl(D); 695 Writer.AddSourceLocation(D->getUsingLoc(), Record); 696 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record); 697 Writer.AddSourceRange(D->getQualifierRange(), Record); 698 Writer.AddNestedNameSpecifier(D->getQualifier(), Record); 699 Writer.AddDeclRef(D->getNominatedNamespace(), Record); 700 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record); 701 Code = serialization::DECL_USING_DIRECTIVE; 702} 703 704void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 705 VisitValueDecl(D); 706 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record); 707 Writer.AddSourceLocation(D->getUsingLoc(), Record); 708 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record); 709 Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record); 710 Code = serialization::DECL_UNRESOLVED_USING_VALUE; 711} 712 713void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl( 714 UnresolvedUsingTypenameDecl *D) { 715 VisitTypeDecl(D); 716 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record); 717 Writer.AddSourceLocation(D->getUsingLoc(), Record); 718 Writer.AddSourceLocation(D->getTypenameLoc(), Record); 719 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record); 720 Code = serialization::DECL_UNRESOLVED_USING_TYPENAME; 721} 722 723void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) { 724 VisitRecordDecl(D); 725 726 CXXRecordDecl *DefinitionDecl = 0; 727 if (D->DefinitionData) 728 DefinitionDecl = D->DefinitionData->Definition; 729 Writer.AddDeclRef(DefinitionDecl, Record); 730 if (D == DefinitionDecl) 731 Writer.AddCXXDefinitionData(D, Record); 732 733 enum { 734 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization 735 }; 736 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) { 737 Record.push_back(CXXRecTemplate); 738 Writer.AddDeclRef(TemplD, Record); 739 } else if (MemberSpecializationInfo *MSInfo 740 = D->getMemberSpecializationInfo()) { 741 Record.push_back(CXXRecMemberSpecialization); 742 Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record); 743 Record.push_back(MSInfo->getTemplateSpecializationKind()); 744 Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record); 745 } else { 746 Record.push_back(CXXRecNotTemplate); 747 } 748 749 // Store the key function to avoid deserializing every method so we can 750 // compute it. 751 if (D->IsDefinition) 752 Writer.AddDeclRef(Context.getKeyFunction(D), Record); 753 754 Code = serialization::DECL_CXX_RECORD; 755} 756 757void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) { 758 VisitFunctionDecl(D); 759 Record.push_back(D->size_overridden_methods()); 760 for (CXXMethodDecl::method_iterator 761 I = D->begin_overridden_methods(), E = D->end_overridden_methods(); 762 I != E; ++I) 763 Writer.AddDeclRef(*I, Record); 764 Code = serialization::DECL_CXX_METHOD; 765} 766 767void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 768 VisitCXXMethodDecl(D); 769 770 Record.push_back(D->IsExplicitSpecified); 771 Record.push_back(D->ImplicitlyDefined); 772 Writer.AddCXXBaseOrMemberInitializers(D->BaseOrMemberInitializers, 773 D->NumBaseOrMemberInitializers, Record); 774 775 Code = serialization::DECL_CXX_CONSTRUCTOR; 776} 777 778void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 779 VisitCXXMethodDecl(D); 780 781 Record.push_back(D->ImplicitlyDefined); 782 Writer.AddDeclRef(D->OperatorDelete, Record); 783 784 Code = serialization::DECL_CXX_DESTRUCTOR; 785} 786 787void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) { 788 VisitCXXMethodDecl(D); 789 Record.push_back(D->IsExplicitSpecified); 790 Code = serialization::DECL_CXX_CONVERSION; 791} 792 793void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) { 794 VisitDecl(D); 795 Writer.AddSourceLocation(D->getColonLoc(), Record); 796 Code = serialization::DECL_ACCESS_SPEC; 797} 798 799void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) { 800 VisitDecl(D); 801 Record.push_back(D->Friend.is<TypeSourceInfo*>()); 802 if (D->Friend.is<TypeSourceInfo*>()) 803 Writer.AddTypeSourceInfo(D->Friend.get<TypeSourceInfo*>(), Record); 804 else 805 Writer.AddDeclRef(D->Friend.get<NamedDecl*>(), Record); 806 Writer.AddDeclRef(D->getNextFriend(), Record); 807 Record.push_back(D->UnsupportedFriend); 808 Writer.AddSourceLocation(D->FriendLoc, Record); 809 Code = serialization::DECL_FRIEND; 810} 811 812void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) { 813 VisitDecl(D); 814 Record.push_back(D->getNumTemplateParameters()); 815 for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i) 816 Writer.AddTemplateParameterList(D->getTemplateParameterList(i), Record); 817 Record.push_back(D->getFriendDecl() != 0); 818 if (D->getFriendDecl()) 819 Writer.AddDeclRef(D->getFriendDecl(), Record); 820 else 821 Writer.AddTypeSourceInfo(D->getFriendType(), Record); 822 Writer.AddSourceLocation(D->getFriendLoc(), Record); 823 Code = serialization::DECL_FRIEND_TEMPLATE; 824} 825 826void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) { 827 VisitNamedDecl(D); 828 829 Writer.AddDeclRef(D->getTemplatedDecl(), Record); 830 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record); 831} 832 833void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { 834 // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that 835 // getCommonPtr() can be used while this is still initializing. 836 837 Writer.AddDeclRef(D->getPreviousDeclaration(), Record); 838 if (D->getPreviousDeclaration() == 0) { 839 // This TemplateDecl owns the CommonPtr; write it. 840 assert(D->isCanonicalDecl()); 841 842 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record); 843 if (D->getInstantiatedFromMemberTemplate()) 844 Record.push_back(D->isMemberSpecialization()); 845 846 Writer.AddDeclRef(D->getCommonPtr()->Latest, Record); 847 } else { 848 RedeclarableTemplateDecl *First = D->getFirstDeclaration(); 849 assert(First != D); 850 // If this is a most recent redeclaration that is pointed to by a first decl 851 // in a chained PCH, keep track of the association with the map so we can 852 // update the first decl during AST reading. 853 if (First->getMostRecentDeclaration() == D && 854 First->getPCHLevel() > D->getPCHLevel()) { 855 assert(Writer.FirstLatestDecls.find(First)==Writer.FirstLatestDecls.end() 856 && "The latest is already set"); 857 Writer.FirstLatestDecls[First] = D; 858 } 859 } 860 861 VisitTemplateDecl(D); 862 Record.push_back(D->getIdentifierNamespace()); 863} 864 865void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 866 VisitRedeclarableTemplateDecl(D); 867 868 if (D->getPreviousDeclaration() == 0) { 869 typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy; 870 CTSDSetTy &CTSDSet = D->getSpecializations(); 871 Record.push_back(CTSDSet.size()); 872 for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) { 873 assert(I->isCanonicalDecl() && "Expected only canonical decls in set"); 874 Writer.AddDeclRef(&*I, Record); 875 } 876 877 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy; 878 CTPSDSetTy &CTPSDSet = D->getPartialSpecializations(); 879 Record.push_back(CTPSDSet.size()); 880 for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) { 881 assert(I->isCanonicalDecl() && "Expected only canonical decls in set"); 882 Writer.AddDeclRef(&*I, Record); 883 } 884 885 // InjectedClassNameType is computed, no need to write it. 886 } 887 Code = serialization::DECL_CLASS_TEMPLATE; 888} 889 890void ASTDeclWriter::VisitClassTemplateSpecializationDecl( 891 ClassTemplateSpecializationDecl *D) { 892 VisitCXXRecordDecl(D); 893 894 llvm::PointerUnion<ClassTemplateDecl *, 895 ClassTemplatePartialSpecializationDecl *> InstFrom 896 = D->getSpecializedTemplateOrPartial(); 897 Decl *InstFromD; 898 if (InstFrom.is<ClassTemplateDecl *>()) { 899 InstFromD = InstFrom.get<ClassTemplateDecl *>(); 900 Writer.AddDeclRef(InstFromD, Record); 901 } else { 902 InstFromD = InstFrom.get<ClassTemplatePartialSpecializationDecl *>(); 903 Writer.AddDeclRef(InstFromD, Record); 904 Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record); 905 InstFromD = cast<ClassTemplatePartialSpecializationDecl>(InstFromD)-> 906 getSpecializedTemplate(); 907 } 908 909 // Explicit info. 910 Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record); 911 if (D->getTypeAsWritten()) { 912 Writer.AddSourceLocation(D->getExternLoc(), Record); 913 Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record); 914 } 915 916 Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record); 917 Writer.AddSourceLocation(D->getPointOfInstantiation(), Record); 918 Record.push_back(D->getSpecializationKind()); 919 920 if (D->isCanonicalDecl()) { 921 // When reading, we'll add it to the folding set of the following template. 922 Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record); 923 } 924 925 Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION; 926} 927 928void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl( 929 ClassTemplatePartialSpecializationDecl *D) { 930 VisitClassTemplateSpecializationDecl(D); 931 932 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record); 933 934 Record.push_back(D->getNumTemplateArgsAsWritten()); 935 for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i) 936 Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record); 937 938 Record.push_back(D->getSequenceNumber()); 939 940 // These are read/set from/to the first declaration. 941 if (D->getPreviousDeclaration() == 0) { 942 Writer.AddDeclRef(D->getInstantiatedFromMember(), Record); 943 Record.push_back(D->isMemberSpecialization()); 944 } 945 946 Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION; 947} 948 949void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 950 VisitRedeclarableTemplateDecl(D); 951 952 if (D->getPreviousDeclaration() == 0) { 953 // This FunctionTemplateDecl owns the CommonPtr; write it. 954 955 // Write the function specialization declarations. 956 Record.push_back(D->getSpecializations().size()); 957 for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator 958 I = D->getSpecializations().begin(), 959 E = D->getSpecializations().end() ; I != E; ++I) { 960 assert(I->Function->isCanonicalDecl() && 961 "Expected only canonical decls in set"); 962 Writer.AddDeclRef(I->Function, Record); 963 } 964 } 965 Code = serialization::DECL_FUNCTION_TEMPLATE; 966} 967 968void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 969 VisitTypeDecl(D); 970 971 Record.push_back(D->wasDeclaredWithTypename()); 972 Record.push_back(D->isParameterPack()); 973 Record.push_back(D->defaultArgumentWasInherited()); 974 Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record); 975 976 Code = serialization::DECL_TEMPLATE_TYPE_PARM; 977} 978 979void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 980 VisitVarDecl(D); 981 // TemplateParmPosition. 982 Record.push_back(D->getDepth()); 983 Record.push_back(D->getPosition()); 984 // Rest of NonTypeTemplateParmDecl. 985 Record.push_back(D->getDefaultArgument() != 0); 986 if (D->getDefaultArgument()) { 987 Writer.AddStmt(D->getDefaultArgument()); 988 Record.push_back(D->defaultArgumentWasInherited()); 989 } 990 Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM; 991} 992 993void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 994 VisitTemplateDecl(D); 995 // TemplateParmPosition. 996 Record.push_back(D->getDepth()); 997 Record.push_back(D->getPosition()); 998 // Rest of TemplateTemplateParmDecl. 999 Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record); 1000 Record.push_back(D->defaultArgumentWasInherited()); 1001 Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM; 1002} 1003 1004void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) { 1005 VisitDecl(D); 1006 Writer.AddStmt(D->getAssertExpr()); 1007 Writer.AddStmt(D->getMessage()); 1008 Code = serialization::DECL_STATIC_ASSERT; 1009} 1010 1011/// \brief Emit the DeclContext part of a declaration context decl. 1012/// 1013/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL 1014/// block for this declaration context is stored. May be 0 to indicate 1015/// that there are no declarations stored within this context. 1016/// 1017/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE 1018/// block for this declaration context is stored. May be 0 to indicate 1019/// that there are no declarations visible from this context. Note 1020/// that this value will not be emitted for non-primary declaration 1021/// contexts. 1022void ASTDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 1023 uint64_t VisibleOffset) { 1024 Record.push_back(LexicalOffset); 1025 Record.push_back(VisibleOffset); 1026} 1027 1028template <typename T> 1029void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) { 1030 enum { NoRedeclaration = 0, PointsToPrevious, PointsToLatest }; 1031 if (D->RedeclLink.getNext() == D) { 1032 Record.push_back(NoRedeclaration); 1033 } else { 1034 Record.push_back(D->RedeclLink.NextIsPrevious() ? PointsToPrevious 1035 : PointsToLatest); 1036 Writer.AddDeclRef(D->RedeclLink.getPointer(), Record); 1037 } 1038 1039 T *First = D->getFirstDeclaration(); 1040 T *ThisDecl = static_cast<T*>(D); 1041 // If this is a most recent redeclaration that is pointed to by a first decl 1042 // in a chained PCH, keep track of the association with the map so we can 1043 // update the first decl during AST reading. 1044 if (ThisDecl != First && First->getMostRecentDeclaration() == ThisDecl && 1045 First->getPCHLevel() > ThisDecl->getPCHLevel()) { 1046 assert(Writer.FirstLatestDecls.find(First) == Writer.FirstLatestDecls.end() 1047 && "The latest is already set"); 1048 Writer.FirstLatestDecls[First] = ThisDecl; 1049 } 1050} 1051 1052//===----------------------------------------------------------------------===// 1053// ASTWriter Implementation 1054//===----------------------------------------------------------------------===// 1055 1056void ASTWriter::WriteDeclsBlockAbbrevs() { 1057 using namespace llvm; 1058 // Abbreviation for DECL_PARM_VAR. 1059 BitCodeAbbrev *Abv = new BitCodeAbbrev(); 1060 Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR)); 1061 1062 // Decl 1063 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext 1064 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext 1065 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location 1066 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) 1067 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs 1068 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit 1069 Abv->Add(BitCodeAbbrevOp(0)); // isUsed 1070 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier 1071 Abv->Add(BitCodeAbbrevOp(0)); // PCH level 1072 1073 // NamedDecl 1074 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier 1075 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name 1076 // ValueDecl 1077 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type 1078 // DeclaratorDecl 1079 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo 1080 Abv->Add(BitCodeAbbrevOp(serialization::PREDEF_TYPE_NULL_ID)); // InfoType 1081 // VarDecl 1082 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration 1083 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass 1084 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten 1085 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified 1086 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer 1087 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable 1088 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable 1089 Abv->Add(BitCodeAbbrevOp(0)); // HasInit 1090 Abv->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo 1091 // ParmVarDecl 1092 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier 1093 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg 1094 Abv->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg 1095 1096 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); 1097 1098 Abv = new BitCodeAbbrev(); 1099 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL)); 1100 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1101 DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv); 1102 1103 Abv = new BitCodeAbbrev(); 1104 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE)); 1105 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1106 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1107 DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(Abv); 1108} 1109 1110/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by 1111/// consumers of the AST. 1112/// 1113/// Such decls will always be deserialized from the AST file, so we would like 1114/// this to be as restrictive as possible. Currently the predicate is driven by 1115/// code generation requirements, if other clients have a different notion of 1116/// what is "required" then we may have to consider an alternate scheme where 1117/// clients can iterate over the top-level decls and get information on them, 1118/// without necessary deserializing them. We could explicitly require such 1119/// clients to use a separate API call to "realize" the decl. This should be 1120/// relatively painless since they would presumably only do it for top-level 1121/// decls. 1122static bool isRequiredDecl(const Decl *D, ASTContext &Context) { 1123 // File scoped assembly or obj-c implementation must be seen. 1124 if (isa<FileScopeAsmDecl>(D) || isa<ObjCImplementationDecl>(D)) 1125 return true; 1126 1127 return Context.DeclMustBeEmitted(D); 1128} 1129 1130void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) { 1131 // Switch case IDs are per Decl. 1132 ClearSwitchCaseIDs(); 1133 1134 RecordData Record; 1135 ASTDeclWriter W(*this, Context, Record); 1136 1137 // If this declaration is also a DeclContext, write blocks for the 1138 // declarations that lexically stored inside its context and those 1139 // declarations that are visible from its context. These blocks 1140 // are written before the declaration itself so that we can put 1141 // their offsets into the record for the declaration. 1142 uint64_t LexicalOffset = 0; 1143 uint64_t VisibleOffset = 0; 1144 DeclContext *DC = dyn_cast<DeclContext>(D); 1145 if (DC) { 1146 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); 1147 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); 1148 } 1149 1150 // Determine the ID for this declaration 1151 serialization::DeclID &IDR = DeclIDs[D]; 1152 if (IDR == 0) 1153 IDR = NextDeclID++; 1154 serialization::DeclID ID = IDR; 1155 1156 if (ID < FirstDeclID) { 1157 // We're replacing a decl in a previous file. 1158 ReplacedDecls.push_back(std::make_pair(ID, Stream.GetCurrentBitNo())); 1159 } else { 1160 unsigned Index = ID - FirstDeclID; 1161 1162 // Record the offset for this declaration 1163 if (DeclOffsets.size() == Index) 1164 DeclOffsets.push_back(Stream.GetCurrentBitNo()); 1165 else if (DeclOffsets.size() < Index) { 1166 DeclOffsets.resize(Index+1); 1167 DeclOffsets[Index] = Stream.GetCurrentBitNo(); 1168 } 1169 } 1170 1171 // Build and emit a record for this declaration 1172 Record.clear(); 1173 W.Code = (serialization::DeclCode)0; 1174 W.AbbrevToUse = 0; 1175 W.Visit(D); 1176 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); 1177 1178 if (!W.Code) 1179 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") + 1180 D->getDeclKindName() + "'"); 1181 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); 1182 1183 // Flush any expressions that were written as part of this declaration. 1184 FlushStmts(); 1185 1186 // Note "external" declarations so that we can add them to a record in the 1187 // AST file later. 1188 // 1189 // FIXME: This should be renamed, the predicate is much more complicated. 1190 if (isRequiredDecl(D, Context)) 1191 ExternalDefinitions.push_back(ID); 1192} 1193