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