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