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" 23 using namespace clang; 24 25 //===----------------------------------------------------------------------===// 26 // Declaration serialization 27 //===----------------------------------------------------------------------===// 28 29 namespace 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 119 void 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 132 void 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 146 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 147 VisitDecl(D); 148 Writer.AddDeclRef(D->getAnonymousNamespace(), Record); 149 Code = serialization::DECL_TRANSLATION_UNIT; 150 } 151 152 void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) { 153 VisitDecl(D); 154 Writer.AddDeclarationName(D->getDeclName(), Record); 155 } 156 157 void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) { 158 VisitNamedDecl(D); 159 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); 160 } 161 162 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) { 163 VisitTypeDecl(D); 164 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); 165 Code = serialization::DECL_TYPEDEF; 166 } 167 168 void 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 184 void 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 199 void 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 207 void ASTDeclWriter::VisitValueDecl(ValueDecl *D) { 208 VisitNamedDecl(D); 209 Writer.AddTypeRef(D->getType(), Record); 210 } 211 212 void 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 221 void 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 229 void 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->IsInline); 307 Record.push_back(D->isInlineSpecified()); 308 Record.push_back(D->isVirtualAsWritten()); 309 Record.push_back(D->isPure()); 310 Record.push_back(D->hasInheritedPrototype()); 311 Record.push_back(D->hasWrittenPrototype()); 312 Record.push_back(D->isDeleted()); 313 Record.push_back(D->isTrivial()); 314 Record.push_back(D->hasImplicitReturnZero()); 315 Writer.AddSourceLocation(D->getLocEnd(), Record); 316 317 Record.push_back(D->param_size()); 318 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); 319 P != PEnd; ++P) 320 Writer.AddDeclRef(*P, Record); 321 Code = serialization::DECL_FUNCTION; 322 } 323 324 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 325 VisitNamedDecl(D); 326 // FIXME: convert to LazyStmtPtr? 327 // Unlike C/C++, method bodies will never be in header files. 328 bool HasBodyStuff = D->getBody() != 0 || 329 D->getSelfDecl() != 0 || D->getCmdDecl() != 0; 330 Record.push_back(HasBodyStuff); 331 if (HasBodyStuff) { 332 Writer.AddStmt(D->getBody()); 333 Writer.AddDeclRef(D->getSelfDecl(), Record); 334 Writer.AddDeclRef(D->getCmdDecl(), Record); 335 } 336 Record.push_back(D->isInstanceMethod()); 337 Record.push_back(D->isVariadic()); 338 Record.push_back(D->isSynthesized()); 339 Record.push_back(D->isDefined()); 340 // FIXME: stable encoding for @required/@optional 341 Record.push_back(D->getImplementationControl()); 342 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway 343 Record.push_back(D->getObjCDeclQualifier()); 344 Record.push_back(D->getNumSelectorArgs()); 345 Writer.AddTypeRef(D->getResultType(), Record); 346 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record); 347 Writer.AddSourceLocation(D->getLocEnd(), Record); 348 Record.push_back(D->param_size()); 349 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 350 PEnd = D->param_end(); P != PEnd; ++P) 351 Writer.AddDeclRef(*P, Record); 352 Code = serialization::DECL_OBJC_METHOD; 353 } 354 355 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { 356 VisitNamedDecl(D); 357 Writer.AddSourceRange(D->getAtEndRange(), Record); 358 // Abstract class (no need to define a stable serialization::DECL code). 359 } 360 361 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 362 VisitObjCContainerDecl(D); 363 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record); 364 Writer.AddDeclRef(D->getSuperClass(), Record); 365 366 // Write out the protocols that are directly referenced by the @interface. 367 Record.push_back(D->ReferencedProtocols.size()); 368 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), 369 PEnd = D->protocol_end(); 370 P != PEnd; ++P) 371 Writer.AddDeclRef(*P, Record); 372 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(), 373 PLEnd = D->protocol_loc_end(); 374 PL != PLEnd; ++PL) 375 Writer.AddSourceLocation(*PL, Record); 376 377 // Write out the protocols that are transitively referenced. 378 Record.push_back(D->AllReferencedProtocols.size()); 379 for (ObjCList<ObjCProtocolDecl>::iterator 380 P = D->AllReferencedProtocols.begin(), 381 PEnd = D->AllReferencedProtocols.end(); 382 P != PEnd; ++P) 383 Writer.AddDeclRef(*P, Record); 384 385 // Write out the ivars. 386 Record.push_back(D->ivar_size()); 387 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), 388 IEnd = D->ivar_end(); I != IEnd; ++I) 389 Writer.AddDeclRef(*I, Record); 390 Writer.AddDeclRef(D->getCategoryList(), Record); 391 Record.push_back(D->isForwardDecl()); 392 Record.push_back(D->isImplicitInterfaceDecl()); 393 Writer.AddSourceLocation(D->getClassLoc(), Record); 394 Writer.AddSourceLocation(D->getSuperClassLoc(), Record); 395 Writer.AddSourceLocation(D->getLocEnd(), Record); 396 Code = serialization::DECL_OBJC_INTERFACE; 397 } 398 399 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 400 VisitFieldDecl(D); 401 // FIXME: stable encoding for @public/@private/@protected/@package 402 Record.push_back(D->getAccessControl()); 403 Record.push_back(D->getSynthesize()); 404 Code = serialization::DECL_OBJC_IVAR; 405 } 406 407 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 408 VisitObjCContainerDecl(D); 409 Record.push_back(D->isForwardDecl()); 410 Writer.AddSourceLocation(D->getLocEnd(), Record); 411 Record.push_back(D->protocol_size()); 412 for (ObjCProtocolDecl::protocol_iterator 413 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) 414 Writer.AddDeclRef(*I, Record); 415 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(), 416 PLEnd = D->protocol_loc_end(); 417 PL != PLEnd; ++PL) 418 Writer.AddSourceLocation(*PL, Record); 419 Code = serialization::DECL_OBJC_PROTOCOL; 420 } 421 422 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { 423 VisitFieldDecl(D); 424 Code = serialization::DECL_OBJC_AT_DEFS_FIELD; 425 } 426 427 void ASTDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) { 428 VisitDecl(D); 429 Record.push_back(D->size()); 430 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) 431 Writer.AddDeclRef(I->getInterface(), Record); 432 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I) 433 Writer.AddSourceLocation(I->getLocation(), Record); 434 Code = serialization::DECL_OBJC_CLASS; 435 } 436 437 void ASTDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { 438 VisitDecl(D); 439 Record.push_back(D->protocol_size()); 440 for (ObjCForwardProtocolDecl::protocol_iterator 441 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) 442 Writer.AddDeclRef(*I, Record); 443 for (ObjCForwardProtocolDecl::protocol_loc_iterator 444 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end(); 445 PL != PLEnd; ++PL) 446 Writer.AddSourceLocation(*PL, Record); 447 Code = serialization::DECL_OBJC_FORWARD_PROTOCOL; 448 } 449 450 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 451 VisitObjCContainerDecl(D); 452 Writer.AddDeclRef(D->getClassInterface(), Record); 453 Record.push_back(D->protocol_size()); 454 for (ObjCCategoryDecl::protocol_iterator 455 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I) 456 Writer.AddDeclRef(*I, Record); 457 for (ObjCCategoryDecl::protocol_loc_iterator 458 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end(); 459 PL != PLEnd; ++PL) 460 Writer.AddSourceLocation(*PL, Record); 461 Writer.AddDeclRef(D->getNextClassCategory(), Record); 462 Record.push_back(D->hasSynthBitfield()); 463 Writer.AddSourceLocation(D->getAtLoc(), Record); 464 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record); 465 Code = serialization::DECL_OBJC_CATEGORY; 466 } 467 468 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { 469 VisitNamedDecl(D); 470 Writer.AddDeclRef(D->getClassInterface(), Record); 471 Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS; 472 } 473 474 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 475 VisitNamedDecl(D); 476 Writer.AddSourceLocation(D->getAtLoc(), Record); 477 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); 478 // FIXME: stable encoding 479 Record.push_back((unsigned)D->getPropertyAttributes()); 480 Record.push_back((unsigned)D->getPropertyAttributesAsWritten()); 481 // FIXME: stable encoding 482 Record.push_back((unsigned)D->getPropertyImplementation()); 483 Writer.AddDeclarationName(D->getGetterName(), Record); 484 Writer.AddDeclarationName(D->getSetterName(), Record); 485 Writer.AddDeclRef(D->getGetterMethodDecl(), Record); 486 Writer.AddDeclRef(D->getSetterMethodDecl(), Record); 487 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); 488 Code = serialization::DECL_OBJC_PROPERTY; 489 } 490 491 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { 492 VisitObjCContainerDecl(D); 493 Writer.AddDeclRef(D->getClassInterface(), Record); 494 // Abstract class (no need to define a stable serialization::DECL code). 495 } 496 497 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 498 VisitObjCImplDecl(D); 499 Writer.AddIdentifierRef(D->getIdentifier(), Record); 500 Code = serialization::DECL_OBJC_CATEGORY_IMPL; 501 } 502 503 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 504 VisitObjCImplDecl(D); 505 Writer.AddDeclRef(D->getSuperClass(), Record); 506 Writer.AddCXXCtorInitializers(D->IvarInitializers, D->NumIvarInitializers, 507 Record); 508 Record.push_back(D->hasSynthBitfield()); 509 Code = serialization::DECL_OBJC_IMPLEMENTATION; 510 } 511 512 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 513 VisitDecl(D); 514 Writer.AddSourceLocation(D->getLocStart(), Record); 515 Writer.AddDeclRef(D->getPropertyDecl(), Record); 516 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record); 517 Writer.AddSourceLocation(D->getPropertyIvarDeclLoc(), Record); 518 Writer.AddStmt(D->getGetterCXXConstructor()); 519 Writer.AddStmt(D->getSetterCXXAssignment()); 520 Code = serialization::DECL_OBJC_PROPERTY_IMPL; 521 } 522 523 void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) { 524 VisitDeclaratorDecl(D); 525 Record.push_back(D->isMutable()); 526 Record.push_back(D->getBitWidth()? 1 : 0); 527 if (D->getBitWidth()) 528 Writer.AddStmt(D->getBitWidth()); 529 if (!D->getDeclName()) 530 Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record); 531 Code = serialization::DECL_FIELD; 532 } 533 534 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { 535 VisitValueDecl(D); 536 Record.push_back(D->getChainingSize()); 537 538 for (IndirectFieldDecl::chain_iterator 539 P = D->chain_begin(), 540 PEnd = D->chain_end(); P != PEnd; ++P) 541 Writer.AddDeclRef(*P, Record); 542 Code = serialization::DECL_INDIRECTFIELD; 543 } 544 545 void ASTDeclWriter::VisitVarDecl(VarDecl *D) { 546 VisitDeclaratorDecl(D); 547 VisitRedeclarable(D); 548 Record.push_back(D->getStorageClass()); // FIXME: stable encoding 549 Record.push_back(D->getStorageClassAsWritten()); 550 Record.push_back(D->isThreadSpecified()); 551 Record.push_back(D->hasCXXDirectInitializer()); 552 Record.push_back(D->isExceptionVariable()); 553 Record.push_back(D->isNRVOVariable()); 554 Record.push_back(D->getInit() ? 1 : 0); 555 if (D->getInit()) 556 Writer.AddStmt(D->getInit()); 557 558 MemberSpecializationInfo *SpecInfo 559 = D->isStaticDataMember() ? D->getMemberSpecializationInfo() : 0; 560 Record.push_back(SpecInfo != 0); 561 if (SpecInfo) { 562 Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record); 563 Record.push_back(SpecInfo->getTemplateSpecializationKind()); 564 Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record); 565 } 566 567 Code = serialization::DECL_VAR; 568 } 569 570 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 571 VisitVarDecl(D); 572 Code = serialization::DECL_IMPLICIT_PARAM; 573 } 574 575 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { 576 VisitVarDecl(D); 577 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding 578 Record.push_back(D->hasInheritedDefaultArg()); 579 Record.push_back(D->hasUninstantiatedDefaultArg()); 580 if (D->hasUninstantiatedDefaultArg()) 581 Writer.AddStmt(D->getUninstantiatedDefaultArg()); 582 Code = serialization::DECL_PARM_VAR; 583 584 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here 585 // we dynamically check for the properties that we optimize for, but don't 586 // know are true of all PARM_VAR_DECLs. 587 if (!D->getTypeSourceInfo() && 588 !D->hasAttrs() && 589 !D->isImplicit() && 590 !D->isUsed(false) && 591 D->getAccess() == AS_none && 592 D->getPCHLevel() == 0 && 593 D->getStorageClass() == 0 && 594 !D->hasCXXDirectInitializer() && // Can params have this ever? 595 D->getObjCDeclQualifier() == 0 && 596 !D->hasInheritedDefaultArg() && 597 D->getInit() == 0 && 598 !D->hasUninstantiatedDefaultArg()) // No default expr. 599 AbbrevToUse = Writer.getParmVarDeclAbbrev(); 600 601 // Check things we know are true of *every* PARM_VAR_DECL, which is more than 602 // just us assuming it. 603 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls"); 604 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread"); 605 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private"); 606 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var"); 607 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); 608 assert(!D->isStaticDataMember() && 609 "PARM_VAR_DECL can't be static data member"); 610 } 611 612 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 613 VisitDecl(D); 614 Writer.AddStmt(D->getAsmString()); 615 Code = serialization::DECL_FILE_SCOPE_ASM; 616 } 617 618 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) { 619 VisitDecl(D); 620 Writer.AddStmt(D->getBody()); 621 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record); 622 Record.push_back(D->param_size()); 623 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); 624 P != PEnd; ++P) 625 Writer.AddDeclRef(*P, Record); 626 Code = serialization::DECL_BLOCK; 627 } 628 629 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 630 VisitDecl(D); 631 // FIXME: It might be nice to serialize the brace locations for this 632 // declaration, which don't seem to be readily available in the AST. 633 Record.push_back(D->getLanguage()); 634 Record.push_back(D->hasBraces()); 635 Code = serialization::DECL_LINKAGE_SPEC; 636 } 637 638 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) { 639 VisitNamedDecl(D); 640 Record.push_back(D->isInline()); 641 Writer.AddSourceLocation(D->getLBracLoc(), Record); 642 Writer.AddSourceLocation(D->getRBracLoc(), Record); 643 Writer.AddDeclRef(D->getNextNamespace(), Record); 644 645 // Only write one reference--original or anonymous 646 Record.push_back(D->isOriginalNamespace()); 647 if (D->isOriginalNamespace()) 648 Writer.AddDeclRef(D->getAnonymousNamespace(), Record); 649 else 650 Writer.AddDeclRef(D->getOriginalNamespace(), Record); 651 Code = serialization::DECL_NAMESPACE; 652 653 if (Writer.hasChain() && !D->isOriginalNamespace() && 654 D->getOriginalNamespace()->getPCHLevel() > 0) { 655 NamespaceDecl *NS = D->getOriginalNamespace(); 656 Writer.AddUpdatedDeclContext(NS); 657 658 // Make sure all visible decls are written. They will be recorded later. 659 NS->lookup(DeclarationName()); 660 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(NS->getLookupPtr()); 661 if (Map) { 662 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 663 D != DEnd; ++D) { 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 674 void 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 684 void 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 696 void 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 704 void 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 715 void 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 724 void 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 734 void 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 768 void 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 778 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 779 VisitCXXMethodDecl(D); 780 781 Record.push_back(D->IsExplicitSpecified); 782 Record.push_back(D->ImplicitlyDefined); 783 Writer.AddCXXCtorInitializers(D->CtorInitializers, D->NumCtorInitializers, 784 Record); 785 786 Code = serialization::DECL_CXX_CONSTRUCTOR; 787 } 788 789 void 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 798 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) { 799 VisitCXXMethodDecl(D); 800 Record.push_back(D->IsExplicitSpecified); 801 Code = serialization::DECL_CXX_CONVERSION; 802 } 803 804 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) { 805 VisitDecl(D); 806 Writer.AddSourceLocation(D->getColonLoc(), Record); 807 Code = serialization::DECL_ACCESS_SPEC; 808 } 809 810 void 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 823 void 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 837 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) { 838 VisitNamedDecl(D); 839 840 Writer.AddDeclRef(D->getTemplatedDecl(), Record); 841 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record); 842 } 843 844 void 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 876 void 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 901 void 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 939 void 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 960 void 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 979 void 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 990 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 991 // For an expanded parameter pack, record the number of expansion types here 992 // so that it's easier for 993 if (D->isExpandedParameterPack()) 994 Record.push_back(D->getNumExpansionTypes()); 995 996 VisitVarDecl(D); 997 // TemplateParmPosition. 998 Record.push_back(D->getDepth()); 999 Record.push_back(D->getPosition()); 1000 1001 if (D->isExpandedParameterPack()) { 1002 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { 1003 Writer.AddTypeRef(D->getExpansionType(I), Record); 1004 Writer.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I), Record); 1005 } 1006 1007 Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK; 1008 } else { 1009 // Rest of NonTypeTemplateParmDecl. 1010 Record.push_back(D->isParameterPack()); 1011 Record.push_back(D->getDefaultArgument() != 0); 1012 if (D->getDefaultArgument()) { 1013 Writer.AddStmt(D->getDefaultArgument()); 1014 Record.push_back(D->defaultArgumentWasInherited()); 1015 } 1016 Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM; 1017 } 1018 } 1019 1020 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 1021 VisitTemplateDecl(D); 1022 // TemplateParmPosition. 1023 Record.push_back(D->getDepth()); 1024 Record.push_back(D->getPosition()); 1025 // Rest of TemplateTemplateParmDecl. 1026 Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record); 1027 Record.push_back(D->defaultArgumentWasInherited()); 1028 Record.push_back(D->isParameterPack()); 1029 Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM; 1030 } 1031 1032 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) { 1033 VisitDecl(D); 1034 Writer.AddStmt(D->getAssertExpr()); 1035 Writer.AddStmt(D->getMessage()); 1036 Code = serialization::DECL_STATIC_ASSERT; 1037 } 1038 1039 /// \brief Emit the DeclContext part of a declaration context decl. 1040 /// 1041 /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL 1042 /// block for this declaration context is stored. May be 0 to indicate 1043 /// that there are no declarations stored within this context. 1044 /// 1045 /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE 1046 /// block for this declaration context is stored. May be 0 to indicate 1047 /// that there are no declarations visible from this context. Note 1048 /// that this value will not be emitted for non-primary declaration 1049 /// contexts. 1050 void ASTDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 1051 uint64_t VisibleOffset) { 1052 Record.push_back(LexicalOffset); 1053 Record.push_back(VisibleOffset); 1054 } 1055 1056 template <typename T> 1057 void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) { 1058 enum { NoRedeclaration = 0, PointsToPrevious, PointsToLatest }; 1059 if (D->RedeclLink.getNext() == D) { 1060 Record.push_back(NoRedeclaration); 1061 } else { 1062 Record.push_back(D->RedeclLink.NextIsPrevious() ? PointsToPrevious 1063 : PointsToLatest); 1064 Writer.AddDeclRef(D->RedeclLink.getPointer(), Record); 1065 } 1066 1067 T *First = D->getFirstDeclaration(); 1068 T *ThisDecl = static_cast<T*>(D); 1069 // If this is a most recent redeclaration that is pointed to by a first decl 1070 // in a chained PCH, keep track of the association with the map so we can 1071 // update the first decl during AST reading. 1072 if (ThisDecl != First && First->getMostRecentDeclaration() == ThisDecl && 1073 First->getPCHLevel() > ThisDecl->getPCHLevel()) { 1074 assert(Writer.FirstLatestDecls.find(First) == Writer.FirstLatestDecls.end() 1075 && "The latest is already set"); 1076 Writer.FirstLatestDecls[First] = ThisDecl; 1077 } 1078 } 1079 1080 //===----------------------------------------------------------------------===// 1081 // ASTWriter Implementation 1082 //===----------------------------------------------------------------------===// 1083 1084 void ASTWriter::WriteDeclsBlockAbbrevs() { 1085 using namespace llvm; 1086 // Abbreviation for DECL_PARM_VAR. 1087 BitCodeAbbrev *Abv = new BitCodeAbbrev(); 1088 Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR)); 1089 1090 // Decl 1091 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext 1092 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext 1093 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location 1094 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) 1095 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs 1096 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit 1097 Abv->Add(BitCodeAbbrevOp(0)); // isUsed 1098 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier 1099 Abv->Add(BitCodeAbbrevOp(0)); // PCH level 1100 1101 // NamedDecl 1102 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier 1103 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name 1104 // ValueDecl 1105 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type 1106 // DeclaratorDecl 1107 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo 1108 Abv->Add(BitCodeAbbrevOp(serialization::PREDEF_TYPE_NULL_ID)); // InfoType 1109 // VarDecl 1110 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration 1111 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass 1112 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten 1113 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified 1114 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer 1115 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable 1116 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable 1117 Abv->Add(BitCodeAbbrevOp(0)); // HasInit 1118 Abv->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo 1119 // ParmVarDecl 1120 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier 1121 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg 1122 Abv->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg 1123 1124 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); 1125 1126 Abv = new BitCodeAbbrev(); 1127 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL)); 1128 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1129 DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv); 1130 1131 Abv = new BitCodeAbbrev(); 1132 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE)); 1133 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1134 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1135 DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(Abv); 1136 } 1137 1138 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by 1139 /// consumers of the AST. 1140 /// 1141 /// Such decls will always be deserialized from the AST file, so we would like 1142 /// this to be as restrictive as possible. Currently the predicate is driven by 1143 /// code generation requirements, if other clients have a different notion of 1144 /// what is "required" then we may have to consider an alternate scheme where 1145 /// clients can iterate over the top-level decls and get information on them, 1146 /// without necessary deserializing them. We could explicitly require such 1147 /// clients to use a separate API call to "realize" the decl. This should be 1148 /// relatively painless since they would presumably only do it for top-level 1149 /// decls. 1150 static bool isRequiredDecl(const Decl *D, ASTContext &Context) { 1151 // File scoped assembly or obj-c implementation must be seen. 1152 if (isa<FileScopeAsmDecl>(D) || isa<ObjCImplementationDecl>(D)) 1153 return true; 1154 1155 return Context.DeclMustBeEmitted(D); 1156 } 1157 1158 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) { 1159 // Switch case IDs are per Decl. 1160 ClearSwitchCaseIDs(); 1161 1162 RecordData Record; 1163 ASTDeclWriter W(*this, Context, Record); 1164 1165 // If this declaration is also a DeclContext, write blocks for the 1166 // declarations that lexically stored inside its context and those 1167 // declarations that are visible from its context. These blocks 1168 // are written before the declaration itself so that we can put 1169 // their offsets into the record for the declaration. 1170 uint64_t LexicalOffset = 0; 1171 uint64_t VisibleOffset = 0; 1172 DeclContext *DC = dyn_cast<DeclContext>(D); 1173 if (DC) { 1174 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC); 1175 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC); 1176 } 1177 1178 // Determine the ID for this declaration 1179 serialization::DeclID &IDR = DeclIDs[D]; 1180 if (IDR == 0) 1181 IDR = NextDeclID++; 1182 serialization::DeclID ID = IDR; 1183 1184 if (ID < FirstDeclID) { 1185 // We're replacing a decl in a previous file. 1186 ReplacedDecls.push_back(std::make_pair(ID, Stream.GetCurrentBitNo())); 1187 } else { 1188 unsigned Index = ID - FirstDeclID; 1189 1190 // Record the offset for this declaration 1191 if (DeclOffsets.size() == Index) 1192 DeclOffsets.push_back(Stream.GetCurrentBitNo()); 1193 else if (DeclOffsets.size() < Index) { 1194 DeclOffsets.resize(Index+1); 1195 DeclOffsets[Index] = Stream.GetCurrentBitNo(); 1196 } 1197 } 1198 1199 // Build and emit a record for this declaration 1200 Record.clear(); 1201 W.Code = (serialization::DeclCode)0; 1202 W.AbbrevToUse = 0; 1203 W.Visit(D); 1204 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset); 1205 1206 if (!W.Code) 1207 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") + 1208 D->getDeclKindName() + "'"); 1209 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); 1210 1211 // Flush any expressions that were written as part of this declaration. 1212 FlushStmts(); 1213 1214 // Flush C++ base specifiers, if there are any. 1215 FlushCXXBaseSpecifiers(); 1216 1217 // Note "external" declarations so that we can add them to a record in the 1218 // AST file later. 1219 // 1220 // FIXME: This should be renamed, the predicate is much more complicated. 1221 if (isRequiredDecl(D, Context)) 1222 ExternalDefinitions.push_back(ID); 1223 } 1224