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