ASTWriter.cpp revision acec34bda4a765fd05ed10e5aa83bc93dac072c1
1//===--- ASTWriter.cpp - AST File Writer ----------------------------------===// 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 defines the ASTWriter class, which writes AST files. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Serialization/ASTWriter.h" 15#include "ASTCommon.h" 16#include "clang/Sema/Sema.h" 17#include "clang/Sema/IdentifierResolver.h" 18#include "clang/AST/ASTContext.h" 19#include "clang/AST/Decl.h" 20#include "clang/AST/DeclContextInternals.h" 21#include "clang/AST/DeclTemplate.h" 22#include "clang/AST/DeclFriend.h" 23#include "clang/AST/Expr.h" 24#include "clang/AST/ExprCXX.h" 25#include "clang/AST/Type.h" 26#include "clang/AST/TypeLocVisitor.h" 27#include "clang/Serialization/ASTReader.h" 28#include "clang/Lex/MacroInfo.h" 29#include "clang/Lex/PreprocessingRecord.h" 30#include "clang/Lex/Preprocessor.h" 31#include "clang/Lex/HeaderSearch.h" 32#include "clang/Basic/FileManager.h" 33#include "clang/Basic/OnDiskHashTable.h" 34#include "clang/Basic/SourceManager.h" 35#include "clang/Basic/SourceManagerInternals.h" 36#include "clang/Basic/TargetInfo.h" 37#include "clang/Basic/Version.h" 38#include "llvm/ADT/APFloat.h" 39#include "llvm/ADT/APInt.h" 40#include "llvm/ADT/StringExtras.h" 41#include "llvm/Bitcode/BitstreamWriter.h" 42#include "llvm/Support/MemoryBuffer.h" 43#include "llvm/System/Path.h" 44#include <cstdio> 45using namespace clang; 46using namespace clang::serialization; 47 48template <typename T, typename Allocator> 49T *data(std::vector<T, Allocator> &v) { 50 return v.empty() ? 0 : &v.front(); 51} 52template <typename T, typename Allocator> 53const T *data(const std::vector<T, Allocator> &v) { 54 return v.empty() ? 0 : &v.front(); 55} 56 57//===----------------------------------------------------------------------===// 58// Type serialization 59//===----------------------------------------------------------------------===// 60 61namespace { 62 class ASTTypeWriter { 63 ASTWriter &Writer; 64 ASTWriter::RecordDataImpl &Record; 65 66 public: 67 /// \brief Type code that corresponds to the record generated. 68 TypeCode Code; 69 70 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 71 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } 72 73 void VisitArrayType(const ArrayType *T); 74 void VisitFunctionType(const FunctionType *T); 75 void VisitTagType(const TagType *T); 76 77#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); 78#define ABSTRACT_TYPE(Class, Base) 79#include "clang/AST/TypeNodes.def" 80 }; 81} 82 83void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { 84 assert(false && "Built-in types are never serialized"); 85} 86 87void ASTTypeWriter::VisitComplexType(const ComplexType *T) { 88 Writer.AddTypeRef(T->getElementType(), Record); 89 Code = TYPE_COMPLEX; 90} 91 92void ASTTypeWriter::VisitPointerType(const PointerType *T) { 93 Writer.AddTypeRef(T->getPointeeType(), Record); 94 Code = TYPE_POINTER; 95} 96 97void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { 98 Writer.AddTypeRef(T->getPointeeType(), Record); 99 Code = TYPE_BLOCK_POINTER; 100} 101 102void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { 103 Writer.AddTypeRef(T->getPointeeType(), Record); 104 Code = TYPE_LVALUE_REFERENCE; 105} 106 107void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { 108 Writer.AddTypeRef(T->getPointeeType(), Record); 109 Code = TYPE_RVALUE_REFERENCE; 110} 111 112void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { 113 Writer.AddTypeRef(T->getPointeeType(), Record); 114 Writer.AddTypeRef(QualType(T->getClass(), 0), Record); 115 Code = TYPE_MEMBER_POINTER; 116} 117 118void ASTTypeWriter::VisitArrayType(const ArrayType *T) { 119 Writer.AddTypeRef(T->getElementType(), Record); 120 Record.push_back(T->getSizeModifier()); // FIXME: stable values 121 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values 122} 123 124void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { 125 VisitArrayType(T); 126 Writer.AddAPInt(T->getSize(), Record); 127 Code = TYPE_CONSTANT_ARRAY; 128} 129 130void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 131 VisitArrayType(T); 132 Code = TYPE_INCOMPLETE_ARRAY; 133} 134 135void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { 136 VisitArrayType(T); 137 Writer.AddSourceLocation(T->getLBracketLoc(), Record); 138 Writer.AddSourceLocation(T->getRBracketLoc(), Record); 139 Writer.AddStmt(T->getSizeExpr()); 140 Code = TYPE_VARIABLE_ARRAY; 141} 142 143void ASTTypeWriter::VisitVectorType(const VectorType *T) { 144 Writer.AddTypeRef(T->getElementType(), Record); 145 Record.push_back(T->getNumElements()); 146 Record.push_back(T->getAltiVecSpecific()); 147 Code = TYPE_VECTOR; 148} 149 150void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { 151 VisitVectorType(T); 152 Code = TYPE_EXT_VECTOR; 153} 154 155void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { 156 Writer.AddTypeRef(T->getResultType(), Record); 157 FunctionType::ExtInfo C = T->getExtInfo(); 158 Record.push_back(C.getNoReturn()); 159 Record.push_back(C.getRegParm()); 160 // FIXME: need to stabilize encoding of calling convention... 161 Record.push_back(C.getCC()); 162} 163 164void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 165 VisitFunctionType(T); 166 Code = TYPE_FUNCTION_NO_PROTO; 167} 168 169void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { 170 VisitFunctionType(T); 171 Record.push_back(T->getNumArgs()); 172 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) 173 Writer.AddTypeRef(T->getArgType(I), Record); 174 Record.push_back(T->isVariadic()); 175 Record.push_back(T->getTypeQuals()); 176 Record.push_back(T->hasExceptionSpec()); 177 Record.push_back(T->hasAnyExceptionSpec()); 178 Record.push_back(T->getNumExceptions()); 179 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) 180 Writer.AddTypeRef(T->getExceptionType(I), Record); 181 Code = TYPE_FUNCTION_PROTO; 182} 183 184void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 185 Writer.AddDeclRef(T->getDecl(), Record); 186 Code = TYPE_UNRESOLVED_USING; 187} 188 189void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { 190 Writer.AddDeclRef(T->getDecl(), Record); 191 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); 192 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); 193 Code = TYPE_TYPEDEF; 194} 195 196void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { 197 Writer.AddStmt(T->getUnderlyingExpr()); 198 Code = TYPE_TYPEOF_EXPR; 199} 200 201void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { 202 Writer.AddTypeRef(T->getUnderlyingType(), Record); 203 Code = TYPE_TYPEOF; 204} 205 206void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { 207 Writer.AddStmt(T->getUnderlyingExpr()); 208 Code = TYPE_DECLTYPE; 209} 210 211void ASTTypeWriter::VisitTagType(const TagType *T) { 212 Record.push_back(T->isDependentType()); 213 Writer.AddDeclRef(T->getDecl(), Record); 214 assert(!T->isBeingDefined() && 215 "Cannot serialize in the middle of a type definition"); 216} 217 218void ASTTypeWriter::VisitRecordType(const RecordType *T) { 219 VisitTagType(T); 220 Code = TYPE_RECORD; 221} 222 223void ASTTypeWriter::VisitEnumType(const EnumType *T) { 224 VisitTagType(T); 225 Code = TYPE_ENUM; 226} 227 228void 229ASTTypeWriter::VisitSubstTemplateTypeParmType( 230 const SubstTemplateTypeParmType *T) { 231 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 232 Writer.AddTypeRef(T->getReplacementType(), Record); 233 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; 234} 235 236void 237ASTTypeWriter::VisitTemplateSpecializationType( 238 const TemplateSpecializationType *T) { 239 Record.push_back(T->isDependentType()); 240 Writer.AddTemplateName(T->getTemplateName(), Record); 241 Record.push_back(T->getNumArgs()); 242 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); 243 ArgI != ArgE; ++ArgI) 244 Writer.AddTemplateArgument(*ArgI, Record); 245 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 246 : T->getCanonicalTypeInternal(), 247 Record); 248 Code = TYPE_TEMPLATE_SPECIALIZATION; 249} 250 251void 252ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { 253 VisitArrayType(T); 254 Writer.AddStmt(T->getSizeExpr()); 255 Writer.AddSourceRange(T->getBracketsRange(), Record); 256 Code = TYPE_DEPENDENT_SIZED_ARRAY; 257} 258 259void 260ASTTypeWriter::VisitDependentSizedExtVectorType( 261 const DependentSizedExtVectorType *T) { 262 // FIXME: Serialize this type (C++ only) 263 assert(false && "Cannot serialize dependent sized extended vector types"); 264} 265 266void 267ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 268 Record.push_back(T->getDepth()); 269 Record.push_back(T->getIndex()); 270 Record.push_back(T->isParameterPack()); 271 Writer.AddIdentifierRef(T->getName(), Record); 272 Code = TYPE_TEMPLATE_TYPE_PARM; 273} 274 275void 276ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { 277 Record.push_back(T->getKeyword()); 278 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 279 Writer.AddIdentifierRef(T->getIdentifier(), Record); 280 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 281 : T->getCanonicalTypeInternal(), 282 Record); 283 Code = TYPE_DEPENDENT_NAME; 284} 285 286void 287ASTTypeWriter::VisitDependentTemplateSpecializationType( 288 const DependentTemplateSpecializationType *T) { 289 Record.push_back(T->getKeyword()); 290 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 291 Writer.AddIdentifierRef(T->getIdentifier(), Record); 292 Record.push_back(T->getNumArgs()); 293 for (DependentTemplateSpecializationType::iterator 294 I = T->begin(), E = T->end(); I != E; ++I) 295 Writer.AddTemplateArgument(*I, Record); 296 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; 297} 298 299void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { 300 Record.push_back(T->getKeyword()); 301 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 302 Writer.AddTypeRef(T->getNamedType(), Record); 303 Code = TYPE_ELABORATED; 304} 305 306void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { 307 Writer.AddDeclRef(T->getDecl(), Record); 308 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); 309 Code = TYPE_INJECTED_CLASS_NAME; 310} 311 312void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 313 Writer.AddDeclRef(T->getDecl(), Record); 314 Code = TYPE_OBJC_INTERFACE; 315} 316 317void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { 318 Writer.AddTypeRef(T->getBaseType(), Record); 319 Record.push_back(T->getNumProtocols()); 320 for (ObjCObjectType::qual_iterator I = T->qual_begin(), 321 E = T->qual_end(); I != E; ++I) 322 Writer.AddDeclRef(*I, Record); 323 Code = TYPE_OBJC_OBJECT; 324} 325 326void 327ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 328 Writer.AddTypeRef(T->getPointeeType(), Record); 329 Code = TYPE_OBJC_OBJECT_POINTER; 330} 331 332namespace { 333 334class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { 335 ASTWriter &Writer; 336 ASTWriter::RecordDataImpl &Record; 337 338public: 339 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 340 : Writer(Writer), Record(Record) { } 341 342#define ABSTRACT_TYPELOC(CLASS, PARENT) 343#define TYPELOC(CLASS, PARENT) \ 344 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 345#include "clang/AST/TypeLocNodes.def" 346 347 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); 348 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); 349}; 350 351} 352 353void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 354 // nothing to do 355} 356void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 357 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); 358 if (TL.needsExtraLocalData()) { 359 Record.push_back(TL.getWrittenTypeSpec()); 360 Record.push_back(TL.getWrittenSignSpec()); 361 Record.push_back(TL.getWrittenWidthSpec()); 362 Record.push_back(TL.hasModeAttr()); 363 } 364} 365void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { 366 Writer.AddSourceLocation(TL.getNameLoc(), Record); 367} 368void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { 369 Writer.AddSourceLocation(TL.getStarLoc(), Record); 370} 371void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 372 Writer.AddSourceLocation(TL.getCaretLoc(), Record); 373} 374void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 375 Writer.AddSourceLocation(TL.getAmpLoc(), Record); 376} 377void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 378 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); 379} 380void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 381 Writer.AddSourceLocation(TL.getStarLoc(), Record); 382} 383void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { 384 Writer.AddSourceLocation(TL.getLBracketLoc(), Record); 385 Writer.AddSourceLocation(TL.getRBracketLoc(), Record); 386 Record.push_back(TL.getSizeExpr() ? 1 : 0); 387 if (TL.getSizeExpr()) 388 Writer.AddStmt(TL.getSizeExpr()); 389} 390void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 391 VisitArrayTypeLoc(TL); 392} 393void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 394 VisitArrayTypeLoc(TL); 395} 396void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 397 VisitArrayTypeLoc(TL); 398} 399void TypeLocWriter::VisitDependentSizedArrayTypeLoc( 400 DependentSizedArrayTypeLoc TL) { 401 VisitArrayTypeLoc(TL); 402} 403void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( 404 DependentSizedExtVectorTypeLoc TL) { 405 Writer.AddSourceLocation(TL.getNameLoc(), Record); 406} 407void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { 408 Writer.AddSourceLocation(TL.getNameLoc(), Record); 409} 410void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 411 Writer.AddSourceLocation(TL.getNameLoc(), Record); 412} 413void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 414 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 415 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 416 Record.push_back(TL.getTrailingReturn()); 417 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 418 Writer.AddDeclRef(TL.getArg(i), Record); 419} 420void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 421 VisitFunctionTypeLoc(TL); 422} 423void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 424 VisitFunctionTypeLoc(TL); 425} 426void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 427 Writer.AddSourceLocation(TL.getNameLoc(), Record); 428} 429void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 430 Writer.AddSourceLocation(TL.getNameLoc(), Record); 431} 432void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 433 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 434 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 435 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 436} 437void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 438 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 439 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 440 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 441 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 442} 443void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 444 Writer.AddSourceLocation(TL.getNameLoc(), Record); 445} 446void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { 447 Writer.AddSourceLocation(TL.getNameLoc(), Record); 448} 449void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { 450 Writer.AddSourceLocation(TL.getNameLoc(), Record); 451} 452void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 453 Writer.AddSourceLocation(TL.getNameLoc(), Record); 454} 455void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( 456 SubstTemplateTypeParmTypeLoc TL) { 457 Writer.AddSourceLocation(TL.getNameLoc(), Record); 458} 459void TypeLocWriter::VisitTemplateSpecializationTypeLoc( 460 TemplateSpecializationTypeLoc TL) { 461 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 462 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 463 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 464 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 465 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), 466 TL.getArgLoc(i).getLocInfo(), Record); 467} 468void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 469 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 470 Writer.AddSourceRange(TL.getQualifierRange(), Record); 471} 472void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 473 Writer.AddSourceLocation(TL.getNameLoc(), Record); 474} 475void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 476 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 477 Writer.AddSourceRange(TL.getQualifierRange(), Record); 478 Writer.AddSourceLocation(TL.getNameLoc(), Record); 479} 480void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( 481 DependentTemplateSpecializationTypeLoc TL) { 482 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 483 Writer.AddSourceRange(TL.getQualifierRange(), Record); 484 Writer.AddSourceLocation(TL.getNameLoc(), Record); 485 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 486 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 487 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 488 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), 489 TL.getArgLoc(I).getLocInfo(), Record); 490} 491void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 492 Writer.AddSourceLocation(TL.getNameLoc(), Record); 493} 494void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 495 Record.push_back(TL.hasBaseTypeAsWritten()); 496 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 497 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 498 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 499 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); 500} 501void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 502 Writer.AddSourceLocation(TL.getStarLoc(), Record); 503} 504 505//===----------------------------------------------------------------------===// 506// ASTWriter Implementation 507//===----------------------------------------------------------------------===// 508 509static void EmitBlockID(unsigned ID, const char *Name, 510 llvm::BitstreamWriter &Stream, 511 ASTWriter::RecordDataImpl &Record) { 512 Record.clear(); 513 Record.push_back(ID); 514 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 515 516 // Emit the block name if present. 517 if (Name == 0 || Name[0] == 0) return; 518 Record.clear(); 519 while (*Name) 520 Record.push_back(*Name++); 521 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 522} 523 524static void EmitRecordID(unsigned ID, const char *Name, 525 llvm::BitstreamWriter &Stream, 526 ASTWriter::RecordDataImpl &Record) { 527 Record.clear(); 528 Record.push_back(ID); 529 while (*Name) 530 Record.push_back(*Name++); 531 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 532} 533 534static void AddStmtsExprs(llvm::BitstreamWriter &Stream, 535 ASTWriter::RecordDataImpl &Record) { 536#define RECORD(X) EmitRecordID(X, #X, Stream, Record) 537 RECORD(STMT_STOP); 538 RECORD(STMT_NULL_PTR); 539 RECORD(STMT_NULL); 540 RECORD(STMT_COMPOUND); 541 RECORD(STMT_CASE); 542 RECORD(STMT_DEFAULT); 543 RECORD(STMT_LABEL); 544 RECORD(STMT_IF); 545 RECORD(STMT_SWITCH); 546 RECORD(STMT_WHILE); 547 RECORD(STMT_DO); 548 RECORD(STMT_FOR); 549 RECORD(STMT_GOTO); 550 RECORD(STMT_INDIRECT_GOTO); 551 RECORD(STMT_CONTINUE); 552 RECORD(STMT_BREAK); 553 RECORD(STMT_RETURN); 554 RECORD(STMT_DECL); 555 RECORD(STMT_ASM); 556 RECORD(EXPR_PREDEFINED); 557 RECORD(EXPR_DECL_REF); 558 RECORD(EXPR_INTEGER_LITERAL); 559 RECORD(EXPR_FLOATING_LITERAL); 560 RECORD(EXPR_IMAGINARY_LITERAL); 561 RECORD(EXPR_STRING_LITERAL); 562 RECORD(EXPR_CHARACTER_LITERAL); 563 RECORD(EXPR_PAREN); 564 RECORD(EXPR_UNARY_OPERATOR); 565 RECORD(EXPR_SIZEOF_ALIGN_OF); 566 RECORD(EXPR_ARRAY_SUBSCRIPT); 567 RECORD(EXPR_CALL); 568 RECORD(EXPR_MEMBER); 569 RECORD(EXPR_BINARY_OPERATOR); 570 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); 571 RECORD(EXPR_CONDITIONAL_OPERATOR); 572 RECORD(EXPR_IMPLICIT_CAST); 573 RECORD(EXPR_CSTYLE_CAST); 574 RECORD(EXPR_COMPOUND_LITERAL); 575 RECORD(EXPR_EXT_VECTOR_ELEMENT); 576 RECORD(EXPR_INIT_LIST); 577 RECORD(EXPR_DESIGNATED_INIT); 578 RECORD(EXPR_IMPLICIT_VALUE_INIT); 579 RECORD(EXPR_VA_ARG); 580 RECORD(EXPR_ADDR_LABEL); 581 RECORD(EXPR_STMT); 582 RECORD(EXPR_TYPES_COMPATIBLE); 583 RECORD(EXPR_CHOOSE); 584 RECORD(EXPR_GNU_NULL); 585 RECORD(EXPR_SHUFFLE_VECTOR); 586 RECORD(EXPR_BLOCK); 587 RECORD(EXPR_BLOCK_DECL_REF); 588 RECORD(EXPR_OBJC_STRING_LITERAL); 589 RECORD(EXPR_OBJC_ENCODE); 590 RECORD(EXPR_OBJC_SELECTOR_EXPR); 591 RECORD(EXPR_OBJC_PROTOCOL_EXPR); 592 RECORD(EXPR_OBJC_IVAR_REF_EXPR); 593 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); 594 RECORD(EXPR_OBJC_KVC_REF_EXPR); 595 RECORD(EXPR_OBJC_MESSAGE_EXPR); 596 RECORD(STMT_OBJC_FOR_COLLECTION); 597 RECORD(STMT_OBJC_CATCH); 598 RECORD(STMT_OBJC_FINALLY); 599 RECORD(STMT_OBJC_AT_TRY); 600 RECORD(STMT_OBJC_AT_SYNCHRONIZED); 601 RECORD(STMT_OBJC_AT_THROW); 602 RECORD(EXPR_CXX_OPERATOR_CALL); 603 RECORD(EXPR_CXX_CONSTRUCT); 604 RECORD(EXPR_CXX_STATIC_CAST); 605 RECORD(EXPR_CXX_DYNAMIC_CAST); 606 RECORD(EXPR_CXX_REINTERPRET_CAST); 607 RECORD(EXPR_CXX_CONST_CAST); 608 RECORD(EXPR_CXX_FUNCTIONAL_CAST); 609 RECORD(EXPR_CXX_BOOL_LITERAL); 610 RECORD(EXPR_CXX_NULL_PTR_LITERAL); 611#undef RECORD 612} 613 614void ASTWriter::WriteBlockInfoBlock() { 615 RecordData Record; 616 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); 617 618#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) 619#define RECORD(X) EmitRecordID(X, #X, Stream, Record) 620 621 // AST Top-Level Block. 622 BLOCK(AST_BLOCK); 623 RECORD(ORIGINAL_FILE_NAME); 624 RECORD(TYPE_OFFSET); 625 RECORD(DECL_OFFSET); 626 RECORD(LANGUAGE_OPTIONS); 627 RECORD(METADATA); 628 RECORD(IDENTIFIER_OFFSET); 629 RECORD(IDENTIFIER_TABLE); 630 RECORD(EXTERNAL_DEFINITIONS); 631 RECORD(SPECIAL_TYPES); 632 RECORD(STATISTICS); 633 RECORD(TENTATIVE_DEFINITIONS); 634 RECORD(UNUSED_FILESCOPED_DECLS); 635 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); 636 RECORD(SELECTOR_OFFSETS); 637 RECORD(METHOD_POOL); 638 RECORD(PP_COUNTER_VALUE); 639 RECORD(SOURCE_LOCATION_OFFSETS); 640 RECORD(SOURCE_LOCATION_PRELOADS); 641 RECORD(STAT_CACHE); 642 RECORD(EXT_VECTOR_DECLS); 643 RECORD(VERSION_CONTROL_BRANCH_REVISION); 644 RECORD(MACRO_DEFINITION_OFFSETS); 645 RECORD(CHAINED_METADATA); 646 RECORD(REFERENCED_SELECTOR_POOL); 647 648 // SourceManager Block. 649 BLOCK(SOURCE_MANAGER_BLOCK); 650 RECORD(SM_SLOC_FILE_ENTRY); 651 RECORD(SM_SLOC_BUFFER_ENTRY); 652 RECORD(SM_SLOC_BUFFER_BLOB); 653 RECORD(SM_SLOC_INSTANTIATION_ENTRY); 654 RECORD(SM_LINE_TABLE); 655 656 // Preprocessor Block. 657 BLOCK(PREPROCESSOR_BLOCK); 658 RECORD(PP_MACRO_OBJECT_LIKE); 659 RECORD(PP_MACRO_FUNCTION_LIKE); 660 RECORD(PP_TOKEN); 661 RECORD(PP_MACRO_INSTANTIATION); 662 RECORD(PP_MACRO_DEFINITION); 663 664 // Decls and Types block. 665 BLOCK(DECLTYPES_BLOCK); 666 RECORD(TYPE_EXT_QUAL); 667 RECORD(TYPE_COMPLEX); 668 RECORD(TYPE_POINTER); 669 RECORD(TYPE_BLOCK_POINTER); 670 RECORD(TYPE_LVALUE_REFERENCE); 671 RECORD(TYPE_RVALUE_REFERENCE); 672 RECORD(TYPE_MEMBER_POINTER); 673 RECORD(TYPE_CONSTANT_ARRAY); 674 RECORD(TYPE_INCOMPLETE_ARRAY); 675 RECORD(TYPE_VARIABLE_ARRAY); 676 RECORD(TYPE_VECTOR); 677 RECORD(TYPE_EXT_VECTOR); 678 RECORD(TYPE_FUNCTION_PROTO); 679 RECORD(TYPE_FUNCTION_NO_PROTO); 680 RECORD(TYPE_TYPEDEF); 681 RECORD(TYPE_TYPEOF_EXPR); 682 RECORD(TYPE_TYPEOF); 683 RECORD(TYPE_RECORD); 684 RECORD(TYPE_ENUM); 685 RECORD(TYPE_OBJC_INTERFACE); 686 RECORD(TYPE_OBJC_OBJECT); 687 RECORD(TYPE_OBJC_OBJECT_POINTER); 688 RECORD(DECL_TRANSLATION_UNIT); 689 RECORD(DECL_TYPEDEF); 690 RECORD(DECL_ENUM); 691 RECORD(DECL_RECORD); 692 RECORD(DECL_ENUM_CONSTANT); 693 RECORD(DECL_FUNCTION); 694 RECORD(DECL_OBJC_METHOD); 695 RECORD(DECL_OBJC_INTERFACE); 696 RECORD(DECL_OBJC_PROTOCOL); 697 RECORD(DECL_OBJC_IVAR); 698 RECORD(DECL_OBJC_AT_DEFS_FIELD); 699 RECORD(DECL_OBJC_CLASS); 700 RECORD(DECL_OBJC_FORWARD_PROTOCOL); 701 RECORD(DECL_OBJC_CATEGORY); 702 RECORD(DECL_OBJC_CATEGORY_IMPL); 703 RECORD(DECL_OBJC_IMPLEMENTATION); 704 RECORD(DECL_OBJC_COMPATIBLE_ALIAS); 705 RECORD(DECL_OBJC_PROPERTY); 706 RECORD(DECL_OBJC_PROPERTY_IMPL); 707 RECORD(DECL_FIELD); 708 RECORD(DECL_VAR); 709 RECORD(DECL_IMPLICIT_PARAM); 710 RECORD(DECL_PARM_VAR); 711 RECORD(DECL_FILE_SCOPE_ASM); 712 RECORD(DECL_BLOCK); 713 RECORD(DECL_CONTEXT_LEXICAL); 714 RECORD(DECL_CONTEXT_VISIBLE); 715 // Statements and Exprs can occur in the Decls and Types block. 716 AddStmtsExprs(Stream, Record); 717#undef RECORD 718#undef BLOCK 719 Stream.ExitBlock(); 720} 721 722/// \brief Adjusts the given filename to only write out the portion of the 723/// filename that is not part of the system root directory. 724/// 725/// \param Filename the file name to adjust. 726/// 727/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and 728/// the returned filename will be adjusted by this system root. 729/// 730/// \returns either the original filename (if it needs no adjustment) or the 731/// adjusted filename (which points into the @p Filename parameter). 732static const char * 733adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) { 734 assert(Filename && "No file name to adjust?"); 735 736 if (!isysroot) 737 return Filename; 738 739 // Verify that the filename and the system root have the same prefix. 740 unsigned Pos = 0; 741 for (; Filename[Pos] && isysroot[Pos]; ++Pos) 742 if (Filename[Pos] != isysroot[Pos]) 743 return Filename; // Prefixes don't match. 744 745 // We hit the end of the filename before we hit the end of the system root. 746 if (!Filename[Pos]) 747 return Filename; 748 749 // If the file name has a '/' at the current position, skip over the '/'. 750 // We distinguish sysroot-based includes from absolute includes by the 751 // absence of '/' at the beginning of sysroot-based includes. 752 if (Filename[Pos] == '/') 753 ++Pos; 754 755 return Filename + Pos; 756} 757 758/// \brief Write the AST metadata (e.g., i686-apple-darwin9). 759void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) { 760 using namespace llvm; 761 762 // Metadata 763 const TargetInfo &Target = Context.Target; 764 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); 765 MetaAbbrev->Add(BitCodeAbbrevOp( 766 Chain ? CHAINED_METADATA : METADATA)); 767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major 768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor 769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major 770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor 771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable 772 // Target triple or chained PCH name 773 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 774 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); 775 776 RecordData Record; 777 Record.push_back(Chain ? CHAINED_METADATA : METADATA); 778 Record.push_back(VERSION_MAJOR); 779 Record.push_back(VERSION_MINOR); 780 Record.push_back(CLANG_VERSION_MAJOR); 781 Record.push_back(CLANG_VERSION_MINOR); 782 Record.push_back(isysroot != 0); 783 // FIXME: This writes the absolute path for chained headers. 784 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple(); 785 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr); 786 787 // Original file name 788 SourceManager &SM = Context.getSourceManager(); 789 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 790 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); 791 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME)); 792 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 793 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); 794 795 llvm::sys::Path MainFilePath(MainFile->getName()); 796 797 MainFilePath.makeAbsolute(); 798 799 const char *MainFileNameStr = MainFilePath.c_str(); 800 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, 801 isysroot); 802 RecordData Record; 803 Record.push_back(ORIGINAL_FILE_NAME); 804 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); 805 } 806 807 // Repository branch/version information. 808 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev(); 809 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION)); 810 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag 811 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev); 812 Record.clear(); 813 Record.push_back(VERSION_CONTROL_BRANCH_REVISION); 814 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record, 815 getClangFullRepositoryVersion()); 816} 817 818/// \brief Write the LangOptions structure. 819void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) { 820 RecordData Record; 821 Record.push_back(LangOpts.Trigraphs); 822 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. 823 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. 824 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. 825 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) 826 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords 827 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. 828 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ 829 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. 830 Record.push_back(LangOpts.C99); // C99 Support 831 Record.push_back(LangOpts.Microsoft); // Microsoft extensions. 832 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is 833 // already saved elsewhere. 834 Record.push_back(LangOpts.CPlusPlus); // C++ Support 835 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support 836 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. 837 838 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. 839 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. 840 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C 841 // modern abi enabled. 842 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced 843 // modern abi enabled. 844 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled.. 845 846 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings 847 Record.push_back(LangOpts.WritableStrings); // Allow writable strings 848 Record.push_back(LangOpts.LaxVectorConversions); 849 Record.push_back(LangOpts.AltiVec); 850 Record.push_back(LangOpts.Exceptions); // Support exception handling. 851 Record.push_back(LangOpts.SjLjExceptions); 852 853 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. 854 Record.push_back(LangOpts.Freestanding); // Freestanding implementation 855 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) 856 857 // Whether static initializers are protected by locks. 858 Record.push_back(LangOpts.ThreadsafeStatics); 859 Record.push_back(LangOpts.POSIXThreads); 860 Record.push_back(LangOpts.Blocks); // block extension to C 861 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if 862 // they are unused. 863 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno 864 // (modulo the platform support). 865 866 Record.push_back(LangOpts.getSignedOverflowBehavior()); 867 Record.push_back(LangOpts.HeinousExtensions); 868 869 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. 870 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be 871 // defined. 872 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as 873 // opposed to __DYNAMIC__). 874 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. 875 876 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be 877 // used (instead of C99 semantics). 878 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. 879 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should 880 // be enabled. 881 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or 882 // unsigned type 883 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short 884 Record.push_back(LangOpts.getGCMode()); 885 Record.push_back(LangOpts.getVisibilityMode()); 886 Record.push_back(LangOpts.getStackProtectorMode()); 887 Record.push_back(LangOpts.InstantiationDepth); 888 Record.push_back(LangOpts.OpenCL); 889 Record.push_back(LangOpts.CatchUndefined); 890 Record.push_back(LangOpts.ElideConstructors); 891 Record.push_back(LangOpts.SpellChecking); 892 Stream.EmitRecord(LANGUAGE_OPTIONS, Record); 893} 894 895//===----------------------------------------------------------------------===// 896// stat cache Serialization 897//===----------------------------------------------------------------------===// 898 899namespace { 900// Trait used for the on-disk hash table of stat cache results. 901class ASTStatCacheTrait { 902public: 903 typedef const char * key_type; 904 typedef key_type key_type_ref; 905 906 typedef std::pair<int, struct stat> data_type; 907 typedef const data_type& data_type_ref; 908 909 static unsigned ComputeHash(const char *path) { 910 return llvm::HashString(path); 911 } 912 913 std::pair<unsigned,unsigned> 914 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path, 915 data_type_ref Data) { 916 unsigned StrLen = strlen(path); 917 clang::io::Emit16(Out, StrLen); 918 unsigned DataLen = 1; // result value 919 if (Data.first == 0) 920 DataLen += 4 + 4 + 2 + 8 + 8; 921 clang::io::Emit8(Out, DataLen); 922 return std::make_pair(StrLen + 1, DataLen); 923 } 924 925 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) { 926 Out.write(path, KeyLen); 927 } 928 929 void EmitData(llvm::raw_ostream& Out, key_type_ref, 930 data_type_ref Data, unsigned DataLen) { 931 using namespace clang::io; 932 uint64_t Start = Out.tell(); (void)Start; 933 934 // Result of stat() 935 Emit8(Out, Data.first? 1 : 0); 936 937 if (Data.first == 0) { 938 Emit32(Out, (uint32_t) Data.second.st_ino); 939 Emit32(Out, (uint32_t) Data.second.st_dev); 940 Emit16(Out, (uint16_t) Data.second.st_mode); 941 Emit64(Out, (uint64_t) Data.second.st_mtime); 942 Emit64(Out, (uint64_t) Data.second.st_size); 943 } 944 945 assert(Out.tell() - Start == DataLen && "Wrong data length"); 946 } 947}; 948} // end anonymous namespace 949 950/// \brief Write the stat() system call cache to the AST file. 951void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { 952 // Build the on-disk hash table containing information about every 953 // stat() call. 954 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator; 955 unsigned NumStatEntries = 0; 956 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), 957 StatEnd = StatCalls.end(); 958 Stat != StatEnd; ++Stat, ++NumStatEntries) { 959 const char *Filename = Stat->first(); 960 Generator.insert(Filename, Stat->second); 961 } 962 963 // Create the on-disk hash table in a buffer. 964 llvm::SmallString<4096> StatCacheData; 965 uint32_t BucketOffset; 966 { 967 llvm::raw_svector_ostream Out(StatCacheData); 968 // Make sure that no bucket is at offset 0 969 clang::io::Emit32(Out, 0); 970 BucketOffset = Generator.Emit(Out); 971 } 972 973 // Create a blob abbreviation 974 using namespace llvm; 975 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 976 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE)); 977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 980 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); 981 982 // Write the stat cache 983 RecordData Record; 984 Record.push_back(STAT_CACHE); 985 Record.push_back(BucketOffset); 986 Record.push_back(NumStatEntries); 987 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str()); 988} 989 990//===----------------------------------------------------------------------===// 991// Source Manager Serialization 992//===----------------------------------------------------------------------===// 993 994/// \brief Create an abbreviation for the SLocEntry that refers to a 995/// file. 996static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { 997 using namespace llvm; 998 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 999 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); 1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1004 // FileEntry fields. 1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size 1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time 1007 // HeaderFileInfo fields. 1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport 1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo 1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes 1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro 1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1013 return Stream.EmitAbbrev(Abbrev); 1014} 1015 1016/// \brief Create an abbreviation for the SLocEntry that refers to a 1017/// buffer. 1018static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { 1019 using namespace llvm; 1020 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1021 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); 1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob 1027 return Stream.EmitAbbrev(Abbrev); 1028} 1029 1030/// \brief Create an abbreviation for the SLocEntry that refers to a 1031/// buffer's blob. 1032static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { 1033 using namespace llvm; 1034 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1035 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); 1036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob 1037 return Stream.EmitAbbrev(Abbrev); 1038} 1039 1040/// \brief Create an abbreviation for the SLocEntry that refers to an 1041/// buffer. 1042static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) { 1043 using namespace llvm; 1044 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1045 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY)); 1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location 1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location 1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location 1050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length 1051 return Stream.EmitAbbrev(Abbrev); 1052} 1053 1054/// \brief Writes the block containing the serialized form of the 1055/// source manager. 1056/// 1057/// TODO: We should probably use an on-disk hash table (stored in a 1058/// blob), indexed based on the file name, so that we only create 1059/// entries for files that we actually need. In the common case (no 1060/// errors), we probably won't have to create file entries for any of 1061/// the files in the AST. 1062void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, 1063 const Preprocessor &PP, 1064 const char *isysroot) { 1065 RecordData Record; 1066 1067 // Enter the source manager block. 1068 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); 1069 1070 // Abbreviations for the various kinds of source-location entries. 1071 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); 1072 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); 1073 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); 1074 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream); 1075 1076 // Write the line table. 1077 if (SourceMgr.hasLineTable()) { 1078 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1079 1080 // Emit the file names 1081 Record.push_back(LineTable.getNumFilenames()); 1082 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { 1083 // Emit the file name 1084 const char *Filename = LineTable.getFilename(I); 1085 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1086 unsigned FilenameLen = Filename? strlen(Filename) : 0; 1087 Record.push_back(FilenameLen); 1088 if (FilenameLen) 1089 Record.insert(Record.end(), Filename, Filename + FilenameLen); 1090 } 1091 1092 // Emit the line entries 1093 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); 1094 L != LEnd; ++L) { 1095 // Emit the file ID 1096 Record.push_back(L->first); 1097 1098 // Emit the line entries 1099 Record.push_back(L->second.size()); 1100 for (std::vector<LineEntry>::iterator LE = L->second.begin(), 1101 LEEnd = L->second.end(); 1102 LE != LEEnd; ++LE) { 1103 Record.push_back(LE->FileOffset); 1104 Record.push_back(LE->LineNo); 1105 Record.push_back(LE->FilenameID); 1106 Record.push_back((unsigned)LE->FileKind); 1107 Record.push_back(LE->IncludeOffset); 1108 } 1109 } 1110 Stream.EmitRecord(SM_LINE_TABLE, Record); 1111 } 1112 1113 // Write out the source location entry table. We skip the first 1114 // entry, which is always the same dummy entry. 1115 std::vector<uint32_t> SLocEntryOffsets; 1116 RecordData PreloadSLocs; 1117 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0; 1118 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID); 1119 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size(); 1120 I != N; ++I) { 1121 // Get this source location entry. 1122 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I); 1123 1124 // Record the offset of this source-location entry. 1125 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1126 1127 // Figure out which record code to use. 1128 unsigned Code; 1129 if (SLoc->isFile()) { 1130 if (SLoc->getFile().getContentCache()->Entry) 1131 Code = SM_SLOC_FILE_ENTRY; 1132 else 1133 Code = SM_SLOC_BUFFER_ENTRY; 1134 } else 1135 Code = SM_SLOC_INSTANTIATION_ENTRY; 1136 Record.clear(); 1137 Record.push_back(Code); 1138 1139 Record.push_back(SLoc->getOffset()); 1140 if (SLoc->isFile()) { 1141 const SrcMgr::FileInfo &File = SLoc->getFile(); 1142 Record.push_back(File.getIncludeLoc().getRawEncoding()); 1143 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding 1144 Record.push_back(File.hasLineDirectives()); 1145 1146 const SrcMgr::ContentCache *Content = File.getContentCache(); 1147 if (Content->Entry) { 1148 // The source location entry is a file. The blob associated 1149 // with this entry is the file name. 1150 1151 // Emit size/modification time for this file. 1152 Record.push_back(Content->Entry->getSize()); 1153 Record.push_back(Content->Entry->getModificationTime()); 1154 1155 // Emit header-search information associated with this file. 1156 HeaderFileInfo HFI; 1157 HeaderSearch &HS = PP.getHeaderSearchInfo(); 1158 if (Content->Entry->getUID() < HS.header_file_size()) 1159 HFI = HS.header_file_begin()[Content->Entry->getUID()]; 1160 Record.push_back(HFI.isImport); 1161 Record.push_back(HFI.DirInfo); 1162 Record.push_back(HFI.NumIncludes); 1163 AddIdentifierRef(HFI.ControllingMacro, Record); 1164 1165 // Turn the file name into an absolute path, if it isn't already. 1166 const char *Filename = Content->Entry->getName(); 1167 llvm::sys::Path FilePath(Filename, strlen(Filename)); 1168 FilePath.makeAbsolute(); 1169 Filename = FilePath.c_str(); 1170 1171 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1172 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename); 1173 1174 // FIXME: For now, preload all file source locations, so that 1175 // we get the appropriate File entries in the reader. This is 1176 // a temporary measure. 1177 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size()); 1178 } else { 1179 // The source location entry is a buffer. The blob associated 1180 // with this entry contains the contents of the buffer. 1181 1182 // We add one to the size so that we capture the trailing NULL 1183 // that is required by llvm::MemoryBuffer::getMemBuffer (on 1184 // the reader side). 1185 const llvm::MemoryBuffer *Buffer 1186 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1187 const char *Name = Buffer->getBufferIdentifier(); 1188 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, 1189 llvm::StringRef(Name, strlen(Name) + 1)); 1190 Record.clear(); 1191 Record.push_back(SM_SLOC_BUFFER_BLOB); 1192 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1193 llvm::StringRef(Buffer->getBufferStart(), 1194 Buffer->getBufferSize() + 1)); 1195 1196 if (strcmp(Name, "<built-in>") == 0) 1197 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size()); 1198 } 1199 } else { 1200 // The source location entry is an instantiation. 1201 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); 1202 Record.push_back(Inst.getSpellingLoc().getRawEncoding()); 1203 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); 1204 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); 1205 1206 // Compute the token length for this macro expansion. 1207 unsigned NextOffset = SourceMgr.getNextOffset(); 1208 if (I + 1 != N) 1209 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset(); 1210 Record.push_back(NextOffset - SLoc->getOffset() - 1); 1211 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); 1212 } 1213 } 1214 1215 Stream.ExitBlock(); 1216 1217 if (SLocEntryOffsets.empty()) 1218 return; 1219 1220 // Write the source-location offsets table into the AST block. This 1221 // table is used for lazily loading source-location information. 1222 using namespace llvm; 1223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1224 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); 1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset 1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1228 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1229 1230 Record.clear(); 1231 Record.push_back(SOURCE_LOCATION_OFFSETS); 1232 Record.push_back(SLocEntryOffsets.size()); 1233 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0; 1234 Record.push_back(SourceMgr.getNextOffset() - BaseOffset); 1235 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, 1236 (const char *)data(SLocEntryOffsets), 1237 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0])); 1238 1239 // Write the source location entry preloads array, telling the AST 1240 // reader which source locations entries it should load eagerly. 1241 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); 1242} 1243 1244//===----------------------------------------------------------------------===// 1245// Preprocessor Serialization 1246//===----------------------------------------------------------------------===// 1247 1248/// \brief Writes the block containing the serialized form of the 1249/// preprocessor. 1250/// 1251void ASTWriter::WritePreprocessor(const Preprocessor &PP) { 1252 RecordData Record; 1253 1254 // If the preprocessor __COUNTER__ value has been bumped, remember it. 1255 if (PP.getCounterValue() != 0) { 1256 Record.push_back(PP.getCounterValue()); 1257 Stream.EmitRecord(PP_COUNTER_VALUE, Record); 1258 Record.clear(); 1259 } 1260 1261 // Enter the preprocessor block. 1262 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); 1263 1264 // If the AST file contains __DATE__ or __TIME__ emit a warning about this. 1265 // FIXME: use diagnostics subsystem for localization etc. 1266 if (PP.SawDateOrTime()) 1267 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); 1268 1269 1270 // Loop over all the macro definitions that are live at the end of the file, 1271 // emitting each to the PP section. 1272 PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); 1273 unsigned InclusionAbbrev = 0; 1274 if (PPRec) { 1275 using namespace llvm; 1276 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1277 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE)); 1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index 1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location 1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location 1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length 1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes 1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind 1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1285 InclusionAbbrev = Stream.EmitAbbrev(Abbrev); 1286 } 1287 1288 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 1289 I != E; ++I) { 1290 // FIXME: This emits macros in hash table order, we should do it in a stable 1291 // order so that output is reproducible. 1292 MacroInfo *MI = I->second; 1293 1294 // Don't emit builtin macros like __LINE__ to the AST file unless they have 1295 // been redefined by the header (in which case they are not isBuiltinMacro). 1296 // Also skip macros from a AST file if we're chaining. 1297 1298 // FIXME: There is a (probably minor) optimization we could do here, if 1299 // the macro comes from the original PCH but the identifier comes from a 1300 // chained PCH, by storing the offset into the original PCH rather than 1301 // writing the macro definition a second time. 1302 if (MI->isBuiltinMacro() || 1303 (Chain && I->first->isFromAST() && MI->isFromAST())) 1304 continue; 1305 1306 AddIdentifierRef(I->first, Record); 1307 MacroOffsets[I->first] = Stream.GetCurrentBitNo(); 1308 Record.push_back(MI->getDefinitionLoc().getRawEncoding()); 1309 Record.push_back(MI->isUsed()); 1310 1311 unsigned Code; 1312 if (MI->isObjectLike()) { 1313 Code = PP_MACRO_OBJECT_LIKE; 1314 } else { 1315 Code = PP_MACRO_FUNCTION_LIKE; 1316 1317 Record.push_back(MI->isC99Varargs()); 1318 Record.push_back(MI->isGNUVarargs()); 1319 Record.push_back(MI->getNumArgs()); 1320 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 1321 I != E; ++I) 1322 AddIdentifierRef(*I, Record); 1323 } 1324 1325 // If we have a detailed preprocessing record, record the macro definition 1326 // ID that corresponds to this macro. 1327 if (PPRec) 1328 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI))); 1329 1330 Stream.EmitRecord(Code, Record); 1331 Record.clear(); 1332 1333 // Emit the tokens array. 1334 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { 1335 // Note that we know that the preprocessor does not have any annotation 1336 // tokens in it because they are created by the parser, and thus can't be 1337 // in a macro definition. 1338 const Token &Tok = MI->getReplacementToken(TokNo); 1339 1340 Record.push_back(Tok.getLocation().getRawEncoding()); 1341 Record.push_back(Tok.getLength()); 1342 1343 // FIXME: When reading literal tokens, reconstruct the literal pointer if 1344 // it is needed. 1345 AddIdentifierRef(Tok.getIdentifierInfo(), Record); 1346 1347 // FIXME: Should translate token kind to a stable encoding. 1348 Record.push_back(Tok.getKind()); 1349 // FIXME: Should translate token flags to a stable encoding. 1350 Record.push_back(Tok.getFlags()); 1351 1352 Stream.EmitRecord(PP_TOKEN, Record); 1353 Record.clear(); 1354 } 1355 ++NumMacros; 1356 } 1357 1358 // If the preprocessor has a preprocessing record, emit it. 1359 unsigned NumPreprocessingRecords = 0; 1360 if (PPRec) { 1361 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0; 1362 for (PreprocessingRecord::iterator E = PPRec->begin(Chain), 1363 EEnd = PPRec->end(Chain); 1364 E != EEnd; ++E) { 1365 Record.clear(); 1366 1367 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) { 1368 Record.push_back(IndexBase + NumPreprocessingRecords++); 1369 AddSourceLocation(MI->getSourceRange().getBegin(), Record); 1370 AddSourceLocation(MI->getSourceRange().getEnd(), Record); 1371 AddIdentifierRef(MI->getName(), Record); 1372 Record.push_back(getMacroDefinitionID(MI->getDefinition())); 1373 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record); 1374 continue; 1375 } 1376 1377 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { 1378 // Record this macro definition's location. 1379 MacroID ID = getMacroDefinitionID(MD); 1380 1381 // Don't write the macro definition if it is from another AST file. 1382 if (ID < FirstMacroID) 1383 continue; 1384 1385 unsigned Position = ID - FirstMacroID; 1386 if (Position != MacroDefinitionOffsets.size()) { 1387 if (Position > MacroDefinitionOffsets.size()) 1388 MacroDefinitionOffsets.resize(Position + 1); 1389 1390 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo(); 1391 } else 1392 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo()); 1393 1394 Record.push_back(IndexBase + NumPreprocessingRecords++); 1395 Record.push_back(ID); 1396 AddSourceLocation(MD->getSourceRange().getBegin(), Record); 1397 AddSourceLocation(MD->getSourceRange().getEnd(), Record); 1398 AddIdentifierRef(MD->getName(), Record); 1399 AddSourceLocation(MD->getLocation(), Record); 1400 Stream.EmitRecord(PP_MACRO_DEFINITION, Record); 1401 continue; 1402 } 1403 1404 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { 1405 Record.push_back(PP_INCLUSION_DIRECTIVE); 1406 Record.push_back(IndexBase + NumPreprocessingRecords++); 1407 AddSourceLocation(ID->getSourceRange().getBegin(), Record); 1408 AddSourceLocation(ID->getSourceRange().getEnd(), Record); 1409 Record.push_back(ID->getFileName().size()); 1410 Record.push_back(ID->wasInQuotes()); 1411 Record.push_back(static_cast<unsigned>(ID->getKind())); 1412 llvm::SmallString<64> Buffer; 1413 Buffer += ID->getFileName(); 1414 Buffer += ID->getFile()->getName(); 1415 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); 1416 continue; 1417 } 1418 } 1419 } 1420 1421 Stream.ExitBlock(); 1422 1423 // Write the offsets table for the preprocessing record. 1424 if (NumPreprocessingRecords > 0) { 1425 // Write the offsets table for identifier IDs. 1426 using namespace llvm; 1427 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1428 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS)); 1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records 1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs 1431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1432 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1433 1434 Record.clear(); 1435 Record.push_back(MACRO_DEFINITION_OFFSETS); 1436 Record.push_back(NumPreprocessingRecords); 1437 Record.push_back(MacroDefinitionOffsets.size()); 1438 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record, 1439 (const char *)data(MacroDefinitionOffsets), 1440 MacroDefinitionOffsets.size() * sizeof(uint32_t)); 1441 } 1442} 1443 1444//===----------------------------------------------------------------------===// 1445// Type Serialization 1446//===----------------------------------------------------------------------===// 1447 1448/// \brief Write the representation of a type to the AST stream. 1449void ASTWriter::WriteType(QualType T) { 1450 TypeIdx &Idx = TypeIdxs[T]; 1451 if (Idx.getIndex() == 0) // we haven't seen this type before. 1452 Idx = TypeIdx(NextTypeID++); 1453 1454 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); 1455 1456 // Record the offset for this type. 1457 unsigned Index = Idx.getIndex() - FirstTypeID; 1458 if (TypeOffsets.size() == Index) 1459 TypeOffsets.push_back(Stream.GetCurrentBitNo()); 1460 else if (TypeOffsets.size() < Index) { 1461 TypeOffsets.resize(Index + 1); 1462 TypeOffsets[Index] = Stream.GetCurrentBitNo(); 1463 } 1464 1465 RecordData Record; 1466 1467 // Emit the type's representation. 1468 ASTTypeWriter W(*this, Record); 1469 1470 if (T.hasLocalNonFastQualifiers()) { 1471 Qualifiers Qs = T.getLocalQualifiers(); 1472 AddTypeRef(T.getLocalUnqualifiedType(), Record); 1473 Record.push_back(Qs.getAsOpaqueValue()); 1474 W.Code = TYPE_EXT_QUAL; 1475 } else { 1476 switch (T->getTypeClass()) { 1477 // For all of the concrete, non-dependent types, call the 1478 // appropriate visitor function. 1479#define TYPE(Class, Base) \ 1480 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; 1481#define ABSTRACT_TYPE(Class, Base) 1482#include "clang/AST/TypeNodes.def" 1483 } 1484 } 1485 1486 // Emit the serialized record. 1487 Stream.EmitRecord(W.Code, Record); 1488 1489 // Flush any expressions that were written as part of this type. 1490 FlushStmts(); 1491} 1492 1493//===----------------------------------------------------------------------===// 1494// Declaration Serialization 1495//===----------------------------------------------------------------------===// 1496 1497/// \brief Write the block containing all of the declaration IDs 1498/// lexically declared within the given DeclContext. 1499/// 1500/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the 1501/// bistream, or 0 if no block was written. 1502uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, 1503 DeclContext *DC) { 1504 if (DC->decls_empty()) 1505 return 0; 1506 1507 uint64_t Offset = Stream.GetCurrentBitNo(); 1508 RecordData Record; 1509 Record.push_back(DECL_CONTEXT_LEXICAL); 1510 llvm::SmallVector<KindDeclIDPair, 64> Decls; 1511 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 1512 D != DEnd; ++D) 1513 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D))); 1514 1515 ++NumLexicalDeclContexts; 1516 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, 1517 reinterpret_cast<char*>(Decls.data()), 1518 Decls.size() * sizeof(KindDeclIDPair)); 1519 return Offset; 1520} 1521 1522void ASTWriter::WriteTypeDeclOffsets() { 1523 using namespace llvm; 1524 RecordData Record; 1525 1526 // Write the type offsets array 1527 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1528 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); 1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types 1530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block 1531 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1532 Record.clear(); 1533 Record.push_back(TYPE_OFFSET); 1534 Record.push_back(TypeOffsets.size()); 1535 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, 1536 (const char *)data(TypeOffsets), 1537 TypeOffsets.size() * sizeof(TypeOffsets[0])); 1538 1539 // Write the declaration offsets array 1540 Abbrev = new BitCodeAbbrev(); 1541 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); 1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations 1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block 1544 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1545 Record.clear(); 1546 Record.push_back(DECL_OFFSET); 1547 Record.push_back(DeclOffsets.size()); 1548 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, 1549 (const char *)data(DeclOffsets), 1550 DeclOffsets.size() * sizeof(DeclOffsets[0])); 1551} 1552 1553//===----------------------------------------------------------------------===// 1554// Global Method Pool and Selector Serialization 1555//===----------------------------------------------------------------------===// 1556 1557namespace { 1558// Trait used for the on-disk hash table used in the method pool. 1559class ASTMethodPoolTrait { 1560 ASTWriter &Writer; 1561 1562public: 1563 typedef Selector key_type; 1564 typedef key_type key_type_ref; 1565 1566 struct data_type { 1567 SelectorID ID; 1568 ObjCMethodList Instance, Factory; 1569 }; 1570 typedef const data_type& data_type_ref; 1571 1572 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } 1573 1574 static unsigned ComputeHash(Selector Sel) { 1575 return serialization::ComputeHash(Sel); 1576 } 1577 1578 std::pair<unsigned,unsigned> 1579 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel, 1580 data_type_ref Methods) { 1581 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); 1582 clang::io::Emit16(Out, KeyLen); 1583 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts 1584 for (const ObjCMethodList *Method = &Methods.Instance; Method; 1585 Method = Method->Next) 1586 if (Method->Method) 1587 DataLen += 4; 1588 for (const ObjCMethodList *Method = &Methods.Factory; Method; 1589 Method = Method->Next) 1590 if (Method->Method) 1591 DataLen += 4; 1592 clang::io::Emit16(Out, DataLen); 1593 return std::make_pair(KeyLen, DataLen); 1594 } 1595 1596 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) { 1597 uint64_t Start = Out.tell(); 1598 assert((Start >> 32) == 0 && "Selector key offset too large"); 1599 Writer.SetSelectorOffset(Sel, Start); 1600 unsigned N = Sel.getNumArgs(); 1601 clang::io::Emit16(Out, N); 1602 if (N == 0) 1603 N = 1; 1604 for (unsigned I = 0; I != N; ++I) 1605 clang::io::Emit32(Out, 1606 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); 1607 } 1608 1609 void EmitData(llvm::raw_ostream& Out, key_type_ref, 1610 data_type_ref Methods, unsigned DataLen) { 1611 uint64_t Start = Out.tell(); (void)Start; 1612 clang::io::Emit32(Out, Methods.ID); 1613 unsigned NumInstanceMethods = 0; 1614 for (const ObjCMethodList *Method = &Methods.Instance; Method; 1615 Method = Method->Next) 1616 if (Method->Method) 1617 ++NumInstanceMethods; 1618 1619 unsigned NumFactoryMethods = 0; 1620 for (const ObjCMethodList *Method = &Methods.Factory; Method; 1621 Method = Method->Next) 1622 if (Method->Method) 1623 ++NumFactoryMethods; 1624 1625 clang::io::Emit16(Out, NumInstanceMethods); 1626 clang::io::Emit16(Out, NumFactoryMethods); 1627 for (const ObjCMethodList *Method = &Methods.Instance; Method; 1628 Method = Method->Next) 1629 if (Method->Method) 1630 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 1631 for (const ObjCMethodList *Method = &Methods.Factory; Method; 1632 Method = Method->Next) 1633 if (Method->Method) 1634 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 1635 1636 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 1637 } 1638}; 1639} // end anonymous namespace 1640 1641/// \brief Write ObjC data: selectors and the method pool. 1642/// 1643/// The method pool contains both instance and factory methods, stored 1644/// in an on-disk hash table indexed by the selector. The hash table also 1645/// contains an empty entry for every other selector known to Sema. 1646void ASTWriter::WriteSelectors(Sema &SemaRef) { 1647 using namespace llvm; 1648 1649 // Do we have to do anything at all? 1650 if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) 1651 return; 1652 unsigned NumTableEntries = 0; 1653 // Create and write out the blob that contains selectors and the method pool. 1654 { 1655 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; 1656 ASTMethodPoolTrait Trait(*this); 1657 1658 // Create the on-disk hash table representation. We walk through every 1659 // selector we've seen and look it up in the method pool. 1660 SelectorOffsets.resize(NextSelectorID - FirstSelectorID); 1661 for (llvm::DenseMap<Selector, SelectorID>::iterator 1662 I = SelectorIDs.begin(), E = SelectorIDs.end(); 1663 I != E; ++I) { 1664 Selector S = I->first; 1665 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); 1666 ASTMethodPoolTrait::data_type Data = { 1667 I->second, 1668 ObjCMethodList(), 1669 ObjCMethodList() 1670 }; 1671 if (F != SemaRef.MethodPool.end()) { 1672 Data.Instance = F->second.first; 1673 Data.Factory = F->second.second; 1674 } 1675 // Only write this selector if it's not in an existing AST or something 1676 // changed. 1677 if (Chain && I->second < FirstSelectorID) { 1678 // Selector already exists. Did it change? 1679 bool changed = false; 1680 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method; 1681 M = M->Next) { 1682 if (M->Method->getPCHLevel() == 0) 1683 changed = true; 1684 } 1685 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method; 1686 M = M->Next) { 1687 if (M->Method->getPCHLevel() == 0) 1688 changed = true; 1689 } 1690 if (!changed) 1691 continue; 1692 } else if (Data.Instance.Method || Data.Factory.Method) { 1693 // A new method pool entry. 1694 ++NumTableEntries; 1695 } 1696 Generator.insert(S, Data, Trait); 1697 } 1698 1699 // Create the on-disk hash table in a buffer. 1700 llvm::SmallString<4096> MethodPool; 1701 uint32_t BucketOffset; 1702 { 1703 ASTMethodPoolTrait Trait(*this); 1704 llvm::raw_svector_ostream Out(MethodPool); 1705 // Make sure that no bucket is at offset 0 1706 clang::io::Emit32(Out, 0); 1707 BucketOffset = Generator.Emit(Out, Trait); 1708 } 1709 1710 // Create a blob abbreviation 1711 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1712 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); 1713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1715 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1716 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); 1717 1718 // Write the method pool 1719 RecordData Record; 1720 Record.push_back(METHOD_POOL); 1721 Record.push_back(BucketOffset); 1722 Record.push_back(NumTableEntries); 1723 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str()); 1724 1725 // Create a blob abbreviation for the selector table offsets. 1726 Abbrev = new BitCodeAbbrev(); 1727 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); 1728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 1729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1730 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1731 1732 // Write the selector offsets table. 1733 Record.clear(); 1734 Record.push_back(SELECTOR_OFFSETS); 1735 Record.push_back(SelectorOffsets.size()); 1736 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, 1737 (const char *)data(SelectorOffsets), 1738 SelectorOffsets.size() * 4); 1739 } 1740} 1741 1742/// \brief Write the selectors referenced in @selector expression into AST file. 1743void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { 1744 using namespace llvm; 1745 if (SemaRef.ReferencedSelectors.empty()) 1746 return; 1747 1748 RecordData Record; 1749 1750 // Note: this writes out all references even for a dependent AST. But it is 1751 // very tricky to fix, and given that @selector shouldn't really appear in 1752 // headers, probably not worth it. It's not a correctness issue. 1753 for (DenseMap<Selector, SourceLocation>::iterator S = 1754 SemaRef.ReferencedSelectors.begin(), 1755 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) { 1756 Selector Sel = (*S).first; 1757 SourceLocation Loc = (*S).second; 1758 AddSelectorRef(Sel, Record); 1759 AddSourceLocation(Loc, Record); 1760 } 1761 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); 1762} 1763 1764//===----------------------------------------------------------------------===// 1765// Identifier Table Serialization 1766//===----------------------------------------------------------------------===// 1767 1768namespace { 1769class ASTIdentifierTableTrait { 1770 ASTWriter &Writer; 1771 Preprocessor &PP; 1772 1773 /// \brief Determines whether this is an "interesting" identifier 1774 /// that needs a full IdentifierInfo structure written into the hash 1775 /// table. 1776 static bool isInterestingIdentifier(const IdentifierInfo *II) { 1777 return II->isPoisoned() || 1778 II->isExtensionToken() || 1779 II->hasMacroDefinition() || 1780 II->getObjCOrBuiltinID() || 1781 II->getFETokenInfo<void>(); 1782 } 1783 1784public: 1785 typedef const IdentifierInfo* key_type; 1786 typedef key_type key_type_ref; 1787 1788 typedef IdentID data_type; 1789 typedef data_type data_type_ref; 1790 1791 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP) 1792 : Writer(Writer), PP(PP) { } 1793 1794 static unsigned ComputeHash(const IdentifierInfo* II) { 1795 return llvm::HashString(II->getName()); 1796 } 1797 1798 std::pair<unsigned,unsigned> 1799 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II, 1800 IdentID ID) { 1801 unsigned KeyLen = II->getLength() + 1; 1802 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 1803 if (isInterestingIdentifier(II)) { 1804 DataLen += 2; // 2 bytes for builtin ID, flags 1805 if (II->hasMacroDefinition() && 1806 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro()) 1807 DataLen += 4; 1808 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II), 1809 DEnd = IdentifierResolver::end(); 1810 D != DEnd; ++D) 1811 DataLen += sizeof(DeclID); 1812 } 1813 clang::io::Emit16(Out, DataLen); 1814 // We emit the key length after the data length so that every 1815 // string is preceded by a 16-bit length. This matches the PTH 1816 // format for storing identifiers. 1817 clang::io::Emit16(Out, KeyLen); 1818 return std::make_pair(KeyLen, DataLen); 1819 } 1820 1821 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II, 1822 unsigned KeyLen) { 1823 // Record the location of the key data. This is used when generating 1824 // the mapping from persistent IDs to strings. 1825 Writer.SetIdentifierOffset(II, Out.tell()); 1826 Out.write(II->getNameStart(), KeyLen); 1827 } 1828 1829 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II, 1830 IdentID ID, unsigned) { 1831 if (!isInterestingIdentifier(II)) { 1832 clang::io::Emit32(Out, ID << 1); 1833 return; 1834 } 1835 1836 clang::io::Emit32(Out, (ID << 1) | 0x01); 1837 uint32_t Bits = 0; 1838 bool hasMacroDefinition = 1839 II->hasMacroDefinition() && 1840 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro(); 1841 Bits = (uint32_t)II->getObjCOrBuiltinID(); 1842 Bits = (Bits << 1) | unsigned(hasMacroDefinition); 1843 Bits = (Bits << 1) | unsigned(II->isExtensionToken()); 1844 Bits = (Bits << 1) | unsigned(II->isPoisoned()); 1845 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); 1846 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); 1847 clang::io::Emit16(Out, Bits); 1848 1849 if (hasMacroDefinition) 1850 clang::io::Emit32(Out, Writer.getMacroOffset(II)); 1851 1852 // Emit the declaration IDs in reverse order, because the 1853 // IdentifierResolver provides the declarations as they would be 1854 // visible (e.g., the function "stat" would come before the struct 1855 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain() 1856 // adds declarations to the end of the list (so we need to see the 1857 // struct "status" before the function "status"). 1858 // Only emit declarations that aren't from a chained PCH, though. 1859 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II), 1860 IdentifierResolver::end()); 1861 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), 1862 DEnd = Decls.rend(); 1863 D != DEnd; ++D) 1864 clang::io::Emit32(Out, Writer.getDeclID(*D)); 1865 } 1866}; 1867} // end anonymous namespace 1868 1869/// \brief Write the identifier table into the AST file. 1870/// 1871/// The identifier table consists of a blob containing string data 1872/// (the actual identifiers themselves) and a separate "offsets" index 1873/// that maps identifier IDs to locations within the blob. 1874void ASTWriter::WriteIdentifierTable(Preprocessor &PP) { 1875 using namespace llvm; 1876 1877 // Create and write out the blob that contains the identifier 1878 // strings. 1879 { 1880 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; 1881 ASTIdentifierTableTrait Trait(*this, PP); 1882 1883 // Look for any identifiers that were named while processing the 1884 // headers, but are otherwise not needed. We add these to the hash 1885 // table to enable checking of the predefines buffer in the case 1886 // where the user adds new macro definitions when building the AST 1887 // file. 1888 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 1889 IDEnd = PP.getIdentifierTable().end(); 1890 ID != IDEnd; ++ID) 1891 getIdentifierRef(ID->second); 1892 1893 // Create the on-disk hash table representation. We only store offsets 1894 // for identifiers that appear here for the first time. 1895 IdentifierOffsets.resize(NextIdentID - FirstIdentID); 1896 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator 1897 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); 1898 ID != IDEnd; ++ID) { 1899 assert(ID->first && "NULL identifier in identifier table"); 1900 if (!Chain || !ID->first->isFromAST()) 1901 Generator.insert(ID->first, ID->second, Trait); 1902 } 1903 1904 // Create the on-disk hash table in a buffer. 1905 llvm::SmallString<4096> IdentifierTable; 1906 uint32_t BucketOffset; 1907 { 1908 ASTIdentifierTableTrait Trait(*this, PP); 1909 llvm::raw_svector_ostream Out(IdentifierTable); 1910 // Make sure that no bucket is at offset 0 1911 clang::io::Emit32(Out, 0); 1912 BucketOffset = Generator.Emit(Out, Trait); 1913 } 1914 1915 // Create a blob abbreviation 1916 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1917 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); 1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1920 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); 1921 1922 // Write the identifier table 1923 RecordData Record; 1924 Record.push_back(IDENTIFIER_TABLE); 1925 Record.push_back(BucketOffset); 1926 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str()); 1927 } 1928 1929 // Write the offsets table for identifier IDs. 1930 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1931 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); 1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers 1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1934 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1935 1936 RecordData Record; 1937 Record.push_back(IDENTIFIER_OFFSET); 1938 Record.push_back(IdentifierOffsets.size()); 1939 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, 1940 (const char *)data(IdentifierOffsets), 1941 IdentifierOffsets.size() * sizeof(uint32_t)); 1942} 1943 1944//===----------------------------------------------------------------------===// 1945// DeclContext's Name Lookup Table Serialization 1946//===----------------------------------------------------------------------===// 1947 1948namespace { 1949// Trait used for the on-disk hash table used in the method pool. 1950class ASTDeclContextNameLookupTrait { 1951 ASTWriter &Writer; 1952 1953public: 1954 typedef DeclarationName key_type; 1955 typedef key_type key_type_ref; 1956 1957 typedef DeclContext::lookup_result data_type; 1958 typedef const data_type& data_type_ref; 1959 1960 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } 1961 1962 unsigned ComputeHash(DeclarationName Name) { 1963 llvm::FoldingSetNodeID ID; 1964 ID.AddInteger(Name.getNameKind()); 1965 1966 switch (Name.getNameKind()) { 1967 case DeclarationName::Identifier: 1968 ID.AddString(Name.getAsIdentifierInfo()->getName()); 1969 break; 1970 case DeclarationName::ObjCZeroArgSelector: 1971 case DeclarationName::ObjCOneArgSelector: 1972 case DeclarationName::ObjCMultiArgSelector: 1973 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); 1974 break; 1975 case DeclarationName::CXXConstructorName: 1976 case DeclarationName::CXXDestructorName: 1977 case DeclarationName::CXXConversionFunctionName: 1978 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType())); 1979 break; 1980 case DeclarationName::CXXOperatorName: 1981 ID.AddInteger(Name.getCXXOverloadedOperator()); 1982 break; 1983 case DeclarationName::CXXLiteralOperatorName: 1984 ID.AddString(Name.getCXXLiteralIdentifier()->getName()); 1985 case DeclarationName::CXXUsingDirective: 1986 break; 1987 } 1988 1989 return ID.ComputeHash(); 1990 } 1991 1992 std::pair<unsigned,unsigned> 1993 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name, 1994 data_type_ref Lookup) { 1995 unsigned KeyLen = 1; 1996 switch (Name.getNameKind()) { 1997 case DeclarationName::Identifier: 1998 case DeclarationName::ObjCZeroArgSelector: 1999 case DeclarationName::ObjCOneArgSelector: 2000 case DeclarationName::ObjCMultiArgSelector: 2001 case DeclarationName::CXXConstructorName: 2002 case DeclarationName::CXXDestructorName: 2003 case DeclarationName::CXXConversionFunctionName: 2004 case DeclarationName::CXXLiteralOperatorName: 2005 KeyLen += 4; 2006 break; 2007 case DeclarationName::CXXOperatorName: 2008 KeyLen += 1; 2009 break; 2010 case DeclarationName::CXXUsingDirective: 2011 break; 2012 } 2013 clang::io::Emit16(Out, KeyLen); 2014 2015 // 2 bytes for num of decls and 4 for each DeclID. 2016 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first); 2017 clang::io::Emit16(Out, DataLen); 2018 2019 return std::make_pair(KeyLen, DataLen); 2020 } 2021 2022 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) { 2023 using namespace clang::io; 2024 2025 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?"); 2026 Emit8(Out, Name.getNameKind()); 2027 switch (Name.getNameKind()) { 2028 case DeclarationName::Identifier: 2029 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo())); 2030 break; 2031 case DeclarationName::ObjCZeroArgSelector: 2032 case DeclarationName::ObjCOneArgSelector: 2033 case DeclarationName::ObjCMultiArgSelector: 2034 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector())); 2035 break; 2036 case DeclarationName::CXXConstructorName: 2037 case DeclarationName::CXXDestructorName: 2038 case DeclarationName::CXXConversionFunctionName: 2039 Emit32(Out, Writer.getTypeID(Name.getCXXNameType())); 2040 break; 2041 case DeclarationName::CXXOperatorName: 2042 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?"); 2043 Emit8(Out, Name.getCXXOverloadedOperator()); 2044 break; 2045 case DeclarationName::CXXLiteralOperatorName: 2046 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); 2047 break; 2048 case DeclarationName::CXXUsingDirective: 2049 break; 2050 } 2051 } 2052 2053 void EmitData(llvm::raw_ostream& Out, key_type_ref, 2054 data_type Lookup, unsigned DataLen) { 2055 uint64_t Start = Out.tell(); (void)Start; 2056 clang::io::Emit16(Out, Lookup.second - Lookup.first); 2057 for (; Lookup.first != Lookup.second; ++Lookup.first) 2058 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first)); 2059 2060 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2061 } 2062}; 2063} // end anonymous namespace 2064 2065/// \brief Write the block containing all of the declaration IDs 2066/// visible from the given DeclContext. 2067/// 2068/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the 2069/// bitstream, or 0 if no block was written. 2070uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, 2071 DeclContext *DC) { 2072 if (DC->getPrimaryContext() != DC) 2073 return 0; 2074 2075 // Since there is no name lookup into functions or methods, don't bother to 2076 // build a visible-declarations table for these entities. 2077 if (DC->isFunctionOrMethod()) 2078 return 0; 2079 2080 // If not in C++, we perform name lookup for the translation unit via the 2081 // IdentifierInfo chains, don't bother to build a visible-declarations table. 2082 // FIXME: In C++ we need the visible declarations in order to "see" the 2083 // friend declarations, is there a way to do this without writing the table ? 2084 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus) 2085 return 0; 2086 2087 // Force the DeclContext to build a its name-lookup table. 2088 if (DC->hasExternalVisibleStorage()) 2089 DC->MaterializeVisibleDeclsFromExternalStorage(); 2090 else 2091 DC->lookup(DeclarationName()); 2092 2093 // Serialize the contents of the mapping used for lookup. Note that, 2094 // although we have two very different code paths, the serialized 2095 // representation is the same for both cases: a declaration name, 2096 // followed by a size, followed by references to the visible 2097 // declarations that have that name. 2098 uint64_t Offset = Stream.GetCurrentBitNo(); 2099 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2100 if (!Map || Map->empty()) 2101 return 0; 2102 2103 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2104 ASTDeclContextNameLookupTrait Trait(*this); 2105 2106 // Create the on-disk hash table representation. 2107 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2108 D != DEnd; ++D) { 2109 DeclarationName Name = D->first; 2110 DeclContext::lookup_result Result = D->second.getLookupResult(); 2111 Generator.insert(Name, Result, Trait); 2112 } 2113 2114 // Create the on-disk hash table in a buffer. 2115 llvm::SmallString<4096> LookupTable; 2116 uint32_t BucketOffset; 2117 { 2118 llvm::raw_svector_ostream Out(LookupTable); 2119 // Make sure that no bucket is at offset 0 2120 clang::io::Emit32(Out, 0); 2121 BucketOffset = Generator.Emit(Out, Trait); 2122 } 2123 2124 // Write the lookup table 2125 RecordData Record; 2126 Record.push_back(DECL_CONTEXT_VISIBLE); 2127 Record.push_back(BucketOffset); 2128 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, 2129 LookupTable.str()); 2130 2131 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record); 2132 ++NumVisibleDeclContexts; 2133 return Offset; 2134} 2135 2136/// \brief Write an UPDATE_VISIBLE block for the given context. 2137/// 2138/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing 2139/// DeclContext in a dependent AST file. As such, they only exist for the TU 2140/// (in C++) and for namespaces. 2141void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { 2142 // Make the context build its lookup table, but don't make it load external 2143 // decls. 2144 DC->lookup(DeclarationName()); 2145 2146 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2147 if (!Map || Map->empty()) 2148 return; 2149 2150 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2151 ASTDeclContextNameLookupTrait Trait(*this); 2152 2153 // Create the hash table. 2154 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2155 D != DEnd; ++D) { 2156 DeclarationName Name = D->first; 2157 DeclContext::lookup_result Result = D->second.getLookupResult(); 2158 // For any name that appears in this table, the results are complete, i.e. 2159 // they overwrite results from previous PCHs. Merging is always a mess. 2160 Generator.insert(Name, Result, Trait); 2161 } 2162 2163 // Create the on-disk hash table in a buffer. 2164 llvm::SmallString<4096> LookupTable; 2165 uint32_t BucketOffset; 2166 { 2167 llvm::raw_svector_ostream Out(LookupTable); 2168 // Make sure that no bucket is at offset 0 2169 clang::io::Emit32(Out, 0); 2170 BucketOffset = Generator.Emit(Out, Trait); 2171 } 2172 2173 // Write the lookup table 2174 RecordData Record; 2175 Record.push_back(UPDATE_VISIBLE); 2176 Record.push_back(getDeclID(cast<Decl>(DC))); 2177 Record.push_back(BucketOffset); 2178 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); 2179} 2180 2181//===----------------------------------------------------------------------===// 2182// General Serialization Routines 2183//===----------------------------------------------------------------------===// 2184 2185/// \brief Write a record containing the given attributes. 2186void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) { 2187 Record.push_back(Attrs.size()); 2188 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){ 2189 const Attr * A = *i; 2190 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs 2191 AddSourceLocation(A->getLocation(), Record); 2192 Record.push_back(A->isInherited()); 2193 2194#include "clang/Serialization/AttrPCHWrite.inc" 2195 2196 } 2197} 2198 2199void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) { 2200 Record.push_back(Str.size()); 2201 Record.insert(Record.end(), Str.begin(), Str.end()); 2202} 2203 2204/// \brief Note that the identifier II occurs at the given offset 2205/// within the identifier table. 2206void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { 2207 IdentID ID = IdentifierIDs[II]; 2208 // Only store offsets new to this AST file. Other identifier names are looked 2209 // up earlier in the chain and thus don't need an offset. 2210 if (ID >= FirstIdentID) 2211 IdentifierOffsets[ID - FirstIdentID] = Offset; 2212} 2213 2214/// \brief Note that the selector Sel occurs at the given offset 2215/// within the method pool/selector table. 2216void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { 2217 unsigned ID = SelectorIDs[Sel]; 2218 assert(ID && "Unknown selector"); 2219 // Don't record offsets for selectors that are also available in a different 2220 // file. 2221 if (ID < FirstSelectorID) 2222 return; 2223 SelectorOffsets[ID - FirstSelectorID] = Offset; 2224} 2225 2226ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) 2227 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID), 2228 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), 2229 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1), 2230 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID), 2231 CollectedStmts(&StmtsToEmit), 2232 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), 2233 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1), 2234 NextCXXBaseSpecifiersID(1) 2235{ 2236} 2237 2238void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2239 const char *isysroot) { 2240 // Emit the file header. 2241 Stream.Emit((unsigned)'C', 8); 2242 Stream.Emit((unsigned)'P', 8); 2243 Stream.Emit((unsigned)'C', 8); 2244 Stream.Emit((unsigned)'H', 8); 2245 2246 WriteBlockInfoBlock(); 2247 2248 if (Chain) 2249 WriteASTChain(SemaRef, StatCalls, isysroot); 2250 else 2251 WriteASTCore(SemaRef, StatCalls, isysroot); 2252} 2253 2254void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2255 const char *isysroot) { 2256 using namespace llvm; 2257 2258 ASTContext &Context = SemaRef.Context; 2259 Preprocessor &PP = SemaRef.PP; 2260 2261 // The translation unit is the first declaration we'll emit. 2262 DeclIDs[Context.getTranslationUnitDecl()] = 1; 2263 ++NextDeclID; 2264 DeclTypesToEmit.push(Context.getTranslationUnitDecl()); 2265 2266 // Make sure that we emit IdentifierInfos (and any attached 2267 // declarations) for builtins. 2268 { 2269 IdentifierTable &Table = PP.getIdentifierTable(); 2270 llvm::SmallVector<const char *, 32> BuiltinNames; 2271 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, 2272 Context.getLangOptions().NoBuiltin); 2273 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) 2274 getIdentifierRef(&Table.get(BuiltinNames[I])); 2275 } 2276 2277 // Build a record containing all of the tentative definitions in this file, in 2278 // TentativeDefinitions order. Generally, this record will be empty for 2279 // headers. 2280 RecordData TentativeDefinitions; 2281 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) { 2282 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions); 2283 } 2284 2285 // Build a record containing all of the file scoped decls in this file. 2286 RecordData UnusedFileScopedDecls; 2287 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) 2288 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls); 2289 2290 RecordData WeakUndeclaredIdentifiers; 2291 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { 2292 WeakUndeclaredIdentifiers.push_back( 2293 SemaRef.WeakUndeclaredIdentifiers.size()); 2294 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator 2295 I = SemaRef.WeakUndeclaredIdentifiers.begin(), 2296 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { 2297 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); 2298 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); 2299 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); 2300 WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); 2301 } 2302 } 2303 2304 // Build a record containing all of the locally-scoped external 2305 // declarations in this header file. Generally, this record will be 2306 // empty. 2307 RecordData LocallyScopedExternalDecls; 2308 // FIXME: This is filling in the AST file in densemap order which is 2309 // nondeterminstic! 2310 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator 2311 TD = SemaRef.LocallyScopedExternalDecls.begin(), 2312 TDEnd = SemaRef.LocallyScopedExternalDecls.end(); 2313 TD != TDEnd; ++TD) 2314 AddDeclRef(TD->second, LocallyScopedExternalDecls); 2315 2316 // Build a record containing all of the ext_vector declarations. 2317 RecordData ExtVectorDecls; 2318 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) 2319 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); 2320 2321 // Build a record containing all of the VTable uses information. 2322 RecordData VTableUses; 2323 if (!SemaRef.VTableUses.empty()) { 2324 VTableUses.push_back(SemaRef.VTableUses.size()); 2325 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 2326 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 2327 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 2328 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 2329 } 2330 } 2331 2332 // Build a record containing all of dynamic classes declarations. 2333 RecordData DynamicClasses; 2334 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) 2335 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); 2336 2337 // Build a record containing all of pending implicit instantiations. 2338 RecordData PendingInstantiations; 2339 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 2340 I = SemaRef.PendingInstantiations.begin(), 2341 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 2342 AddDeclRef(I->first, PendingInstantiations); 2343 AddSourceLocation(I->second, PendingInstantiations); 2344 } 2345 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 2346 "There are local ones at end of translation unit!"); 2347 2348 // Build a record containing some declaration references. 2349 RecordData SemaDeclRefs; 2350 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 2351 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 2352 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 2353 } 2354 2355 // Write the remaining AST contents. 2356 RecordData Record; 2357 Stream.EnterSubblock(AST_BLOCK_ID, 5); 2358 WriteMetadata(Context, isysroot); 2359 WriteLanguageOptions(Context.getLangOptions()); 2360 if (StatCalls && !isysroot) 2361 WriteStatCache(*StatCalls); 2362 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); 2363 // Write the record of special types. 2364 Record.clear(); 2365 2366 AddTypeRef(Context.getBuiltinVaListType(), Record); 2367 AddTypeRef(Context.getObjCIdType(), Record); 2368 AddTypeRef(Context.getObjCSelType(), Record); 2369 AddTypeRef(Context.getObjCProtoType(), Record); 2370 AddTypeRef(Context.getObjCClassType(), Record); 2371 AddTypeRef(Context.getRawCFConstantStringType(), Record); 2372 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record); 2373 AddTypeRef(Context.getFILEType(), Record); 2374 AddTypeRef(Context.getjmp_bufType(), Record); 2375 AddTypeRef(Context.getsigjmp_bufType(), Record); 2376 AddTypeRef(Context.ObjCIdRedefinitionType, Record); 2377 AddTypeRef(Context.ObjCClassRedefinitionType, Record); 2378 AddTypeRef(Context.getRawBlockdescriptorType(), Record); 2379 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record); 2380 AddTypeRef(Context.ObjCSelRedefinitionType, Record); 2381 AddTypeRef(Context.getRawNSConstantStringType(), Record); 2382 Record.push_back(Context.isInt128Installed()); 2383 Stream.EmitRecord(SPECIAL_TYPES, Record); 2384 2385 // Keep writing types and declarations until all types and 2386 // declarations have been written. 2387 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3); 2388 WriteDeclsBlockAbbrevs(); 2389 while (!DeclTypesToEmit.empty()) { 2390 DeclOrType DOT = DeclTypesToEmit.front(); 2391 DeclTypesToEmit.pop(); 2392 if (DOT.isType()) 2393 WriteType(DOT.getType()); 2394 else 2395 WriteDecl(Context, DOT.getDecl()); 2396 } 2397 Stream.ExitBlock(); 2398 2399 WritePreprocessor(PP); 2400 WriteSelectors(SemaRef); 2401 WriteReferencedSelectorsPool(SemaRef); 2402 WriteIdentifierTable(PP); 2403 2404 WriteTypeDeclOffsets(); 2405 2406 // Write the C++ base-specifier set offsets. 2407 if (!CXXBaseSpecifiersOffsets.empty()) { 2408 // Create a blob abbreviation for the C++ base specifiers offsets. 2409 using namespace llvm; 2410 2411 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2412 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); 2413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 2414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2415 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2416 2417 // Write the selector offsets table. 2418 Record.clear(); 2419 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS); 2420 Record.push_back(CXXBaseSpecifiersOffsets.size()); 2421 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, 2422 (const char *)CXXBaseSpecifiersOffsets.data(), 2423 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t)); 2424 } 2425 2426 // Write the record containing external, unnamed definitions. 2427 if (!ExternalDefinitions.empty()) 2428 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); 2429 2430 // Write the record containing tentative definitions. 2431 if (!TentativeDefinitions.empty()) 2432 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 2433 2434 // Write the record containing unused file scoped decls. 2435 if (!UnusedFileScopedDecls.empty()) 2436 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 2437 2438 // Write the record containing weak undeclared identifiers. 2439 if (!WeakUndeclaredIdentifiers.empty()) 2440 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 2441 WeakUndeclaredIdentifiers); 2442 2443 // Write the record containing locally-scoped external definitions. 2444 if (!LocallyScopedExternalDecls.empty()) 2445 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, 2446 LocallyScopedExternalDecls); 2447 2448 // Write the record containing ext_vector type names. 2449 if (!ExtVectorDecls.empty()) 2450 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 2451 2452 // Write the record containing VTable uses information. 2453 if (!VTableUses.empty()) 2454 Stream.EmitRecord(VTABLE_USES, VTableUses); 2455 2456 // Write the record containing dynamic classes declarations. 2457 if (!DynamicClasses.empty()) 2458 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); 2459 2460 // Write the record containing pending implicit instantiations. 2461 if (!PendingInstantiations.empty()) 2462 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 2463 2464 // Write the record containing declaration references of Sema. 2465 if (!SemaDeclRefs.empty()) 2466 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 2467 2468 // Some simple statistics 2469 Record.clear(); 2470 Record.push_back(NumStatements); 2471 Record.push_back(NumMacros); 2472 Record.push_back(NumLexicalDeclContexts); 2473 Record.push_back(NumVisibleDeclContexts); 2474 Stream.EmitRecord(STATISTICS, Record); 2475 Stream.ExitBlock(); 2476} 2477 2478void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2479 const char *isysroot) { 2480 using namespace llvm; 2481 2482 ASTContext &Context = SemaRef.Context; 2483 Preprocessor &PP = SemaRef.PP; 2484 2485 RecordData Record; 2486 Stream.EnterSubblock(AST_BLOCK_ID, 5); 2487 WriteMetadata(Context, isysroot); 2488 if (StatCalls && !isysroot) 2489 WriteStatCache(*StatCalls); 2490 // FIXME: Source manager block should only write new stuff, which could be 2491 // done by tracking the largest ID in the chain 2492 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); 2493 2494 // The special types are in the chained PCH. 2495 2496 // We don't start with the translation unit, but with its decls that 2497 // don't come from the chained PCH. 2498 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); 2499 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls; 2500 for (DeclContext::decl_iterator I = TU->noload_decls_begin(), 2501 E = TU->noload_decls_end(); 2502 I != E; ++I) { 2503 if ((*I)->getPCHLevel() == 0) 2504 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I))); 2505 else if ((*I)->isChangedSinceDeserialization()) 2506 (void)GetDeclRef(*I); // Make sure it's written, but don't record it. 2507 } 2508 // We also need to write a lexical updates block for the TU. 2509 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); 2510 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); 2511 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 2512 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); 2513 Record.clear(); 2514 Record.push_back(TU_UPDATE_LEXICAL); 2515 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, 2516 reinterpret_cast<const char*>(NewGlobalDecls.data()), 2517 NewGlobalDecls.size() * sizeof(KindDeclIDPair)); 2518 // And a visible updates block for the DeclContexts. 2519 Abv = new llvm::BitCodeAbbrev(); 2520 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); 2521 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); 2522 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); 2523 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 2524 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); 2525 WriteDeclContextVisibleUpdate(TU); 2526 2527 // Build a record containing all of the new tentative definitions in this 2528 // file, in TentativeDefinitions order. 2529 RecordData TentativeDefinitions; 2530 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) { 2531 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0) 2532 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions); 2533 } 2534 2535 // Build a record containing all of the file scoped decls in this file. 2536 RecordData UnusedFileScopedDecls; 2537 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) { 2538 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0) 2539 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls); 2540 } 2541 2542 // We write the entire table, overwriting the tables from the chain. 2543 RecordData WeakUndeclaredIdentifiers; 2544 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { 2545 WeakUndeclaredIdentifiers.push_back( 2546 SemaRef.WeakUndeclaredIdentifiers.size()); 2547 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator 2548 I = SemaRef.WeakUndeclaredIdentifiers.begin(), 2549 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { 2550 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); 2551 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); 2552 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); 2553 WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); 2554 } 2555 } 2556 2557 // Build a record containing all of the locally-scoped external 2558 // declarations in this header file. Generally, this record will be 2559 // empty. 2560 RecordData LocallyScopedExternalDecls; 2561 // FIXME: This is filling in the AST file in densemap order which is 2562 // nondeterminstic! 2563 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator 2564 TD = SemaRef.LocallyScopedExternalDecls.begin(), 2565 TDEnd = SemaRef.LocallyScopedExternalDecls.end(); 2566 TD != TDEnd; ++TD) { 2567 if (TD->second->getPCHLevel() == 0) 2568 AddDeclRef(TD->second, LocallyScopedExternalDecls); 2569 } 2570 2571 // Build a record containing all of the ext_vector declarations. 2572 RecordData ExtVectorDecls; 2573 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) { 2574 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0) 2575 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); 2576 } 2577 2578 // Build a record containing all of the VTable uses information. 2579 // We write everything here, because it's too hard to determine whether 2580 // a use is new to this part. 2581 RecordData VTableUses; 2582 if (!SemaRef.VTableUses.empty()) { 2583 VTableUses.push_back(SemaRef.VTableUses.size()); 2584 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 2585 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 2586 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 2587 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 2588 } 2589 } 2590 2591 // Build a record containing all of dynamic classes declarations. 2592 RecordData DynamicClasses; 2593 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) 2594 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0) 2595 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); 2596 2597 // Build a record containing all of pending implicit instantiations. 2598 RecordData PendingInstantiations; 2599 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 2600 I = SemaRef.PendingInstantiations.begin(), 2601 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 2602 if (I->first->getPCHLevel() == 0) { 2603 AddDeclRef(I->first, PendingInstantiations); 2604 AddSourceLocation(I->second, PendingInstantiations); 2605 } 2606 } 2607 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 2608 "There are local ones at end of translation unit!"); 2609 2610 // Build a record containing some declaration references. 2611 // It's not worth the effort to avoid duplication here. 2612 RecordData SemaDeclRefs; 2613 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 2614 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 2615 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 2616 } 2617 2618 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3); 2619 WriteDeclsBlockAbbrevs(); 2620 for (DeclsToRewriteTy::iterator 2621 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I) 2622 DeclTypesToEmit.push(const_cast<Decl*>(*I)); 2623 while (!DeclTypesToEmit.empty()) { 2624 DeclOrType DOT = DeclTypesToEmit.front(); 2625 DeclTypesToEmit.pop(); 2626 if (DOT.isType()) 2627 WriteType(DOT.getType()); 2628 else 2629 WriteDecl(Context, DOT.getDecl()); 2630 } 2631 Stream.ExitBlock(); 2632 2633 WritePreprocessor(PP); 2634 WriteSelectors(SemaRef); 2635 WriteReferencedSelectorsPool(SemaRef); 2636 WriteIdentifierTable(PP); 2637 WriteTypeDeclOffsets(); 2638 2639 /// Build a record containing first declarations from a chained PCH and the 2640 /// most recent declarations in this AST that they point to. 2641 RecordData FirstLatestDeclIDs; 2642 for (FirstLatestDeclMap::iterator 2643 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) { 2644 assert(I->first->getPCHLevel() > I->second->getPCHLevel() && 2645 "Expected first & second to be in different PCHs"); 2646 AddDeclRef(I->first, FirstLatestDeclIDs); 2647 AddDeclRef(I->second, FirstLatestDeclIDs); 2648 } 2649 if (!FirstLatestDeclIDs.empty()) 2650 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs); 2651 2652 // Write the record containing external, unnamed definitions. 2653 if (!ExternalDefinitions.empty()) 2654 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); 2655 2656 // Write the record containing tentative definitions. 2657 if (!TentativeDefinitions.empty()) 2658 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 2659 2660 // Write the record containing unused file scoped decls. 2661 if (!UnusedFileScopedDecls.empty()) 2662 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 2663 2664 // Write the record containing weak undeclared identifiers. 2665 if (!WeakUndeclaredIdentifiers.empty()) 2666 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 2667 WeakUndeclaredIdentifiers); 2668 2669 // Write the record containing locally-scoped external definitions. 2670 if (!LocallyScopedExternalDecls.empty()) 2671 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, 2672 LocallyScopedExternalDecls); 2673 2674 // Write the record containing ext_vector type names. 2675 if (!ExtVectorDecls.empty()) 2676 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 2677 2678 // Write the record containing VTable uses information. 2679 if (!VTableUses.empty()) 2680 Stream.EmitRecord(VTABLE_USES, VTableUses); 2681 2682 // Write the record containing dynamic classes declarations. 2683 if (!DynamicClasses.empty()) 2684 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); 2685 2686 // Write the record containing pending implicit instantiations. 2687 if (!PendingInstantiations.empty()) 2688 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 2689 2690 // Write the record containing declaration references of Sema. 2691 if (!SemaDeclRefs.empty()) 2692 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 2693 2694 // Write the updates to DeclContexts. 2695 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator 2696 I = UpdatedDeclContexts.begin(), 2697 E = UpdatedDeclContexts.end(); 2698 I != E; ++I) 2699 WriteDeclContextVisibleUpdate(*I); 2700 2701 WriteDeclUpdatesBlocks(); 2702 2703 Record.clear(); 2704 Record.push_back(NumStatements); 2705 Record.push_back(NumMacros); 2706 Record.push_back(NumLexicalDeclContexts); 2707 Record.push_back(NumVisibleDeclContexts); 2708 WriteDeclReplacementsBlock(); 2709 Stream.EmitRecord(STATISTICS, Record); 2710 Stream.ExitBlock(); 2711} 2712 2713void ASTWriter::WriteDeclUpdatesBlocks() { 2714 if (DeclUpdates.empty()) 2715 return; 2716 2717 RecordData OffsetsRecord; 2718 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3); 2719 for (DeclUpdateMap::iterator 2720 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { 2721 const Decl *D = I->first; 2722 UpdateRecord &URec = I->second; 2723 2724 if (DeclsToRewrite.count(D)) 2725 continue; // The decl will be written completely,no need to store updates. 2726 2727 uint64_t Offset = Stream.GetCurrentBitNo(); 2728 Stream.EmitRecord(DECL_UPDATES, URec); 2729 2730 OffsetsRecord.push_back(GetDeclRef(D)); 2731 OffsetsRecord.push_back(Offset); 2732 } 2733 Stream.ExitBlock(); 2734 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord); 2735} 2736 2737void ASTWriter::WriteDeclReplacementsBlock() { 2738 if (ReplacedDecls.empty()) 2739 return; 2740 2741 RecordData Record; 2742 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator 2743 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { 2744 Record.push_back(I->first); 2745 Record.push_back(I->second); 2746 } 2747 Stream.EmitRecord(DECL_REPLACEMENTS, Record); 2748} 2749 2750void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { 2751 Record.push_back(Loc.getRawEncoding()); 2752} 2753 2754void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { 2755 AddSourceLocation(Range.getBegin(), Record); 2756 AddSourceLocation(Range.getEnd(), Record); 2757} 2758 2759void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { 2760 Record.push_back(Value.getBitWidth()); 2761 const uint64_t *Words = Value.getRawData(); 2762 Record.append(Words, Words + Value.getNumWords()); 2763} 2764 2765void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { 2766 Record.push_back(Value.isUnsigned()); 2767 AddAPInt(Value, Record); 2768} 2769 2770void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { 2771 AddAPInt(Value.bitcastToAPInt(), Record); 2772} 2773 2774void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { 2775 Record.push_back(getIdentifierRef(II)); 2776} 2777 2778IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { 2779 if (II == 0) 2780 return 0; 2781 2782 IdentID &ID = IdentifierIDs[II]; 2783 if (ID == 0) 2784 ID = NextIdentID++; 2785 return ID; 2786} 2787 2788MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) { 2789 if (MD == 0) 2790 return 0; 2791 2792 MacroID &ID = MacroDefinitions[MD]; 2793 if (ID == 0) 2794 ID = NextMacroID++; 2795 return ID; 2796} 2797 2798void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { 2799 Record.push_back(getSelectorRef(SelRef)); 2800} 2801 2802SelectorID ASTWriter::getSelectorRef(Selector Sel) { 2803 if (Sel.getAsOpaquePtr() == 0) { 2804 return 0; 2805 } 2806 2807 SelectorID &SID = SelectorIDs[Sel]; 2808 if (SID == 0 && Chain) { 2809 // This might trigger a ReadSelector callback, which will set the ID for 2810 // this selector. 2811 Chain->LoadSelector(Sel); 2812 } 2813 if (SID == 0) { 2814 SID = NextSelectorID++; 2815 } 2816 return SID; 2817} 2818 2819void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { 2820 AddDeclRef(Temp->getDestructor(), Record); 2821} 2822 2823void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, 2824 CXXBaseSpecifier const *BasesEnd, 2825 RecordDataImpl &Record) { 2826 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); 2827 CXXBaseSpecifiersToWrite.push_back( 2828 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, 2829 Bases, BasesEnd)); 2830 Record.push_back(NextCXXBaseSpecifiersID++); 2831} 2832 2833void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 2834 const TemplateArgumentLocInfo &Arg, 2835 RecordDataImpl &Record) { 2836 switch (Kind) { 2837 case TemplateArgument::Expression: 2838 AddStmt(Arg.getAsExpr()); 2839 break; 2840 case TemplateArgument::Type: 2841 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); 2842 break; 2843 case TemplateArgument::Template: 2844 AddSourceRange(Arg.getTemplateQualifierRange(), Record); 2845 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 2846 break; 2847 case TemplateArgument::Null: 2848 case TemplateArgument::Integral: 2849 case TemplateArgument::Declaration: 2850 case TemplateArgument::Pack: 2851 break; 2852 } 2853} 2854 2855void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, 2856 RecordDataImpl &Record) { 2857 AddTemplateArgument(Arg.getArgument(), Record); 2858 2859 if (Arg.getArgument().getKind() == TemplateArgument::Expression) { 2860 bool InfoHasSameExpr 2861 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); 2862 Record.push_back(InfoHasSameExpr); 2863 if (InfoHasSameExpr) 2864 return; // Avoid storing the same expr twice. 2865 } 2866 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), 2867 Record); 2868} 2869 2870void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) { 2871 if (TInfo == 0) { 2872 AddTypeRef(QualType(), Record); 2873 return; 2874 } 2875 2876 AddTypeRef(TInfo->getType(), Record); 2877 TypeLocWriter TLW(*this, Record); 2878 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 2879 TLW.Visit(TL); 2880} 2881 2882void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { 2883 Record.push_back(GetOrCreateTypeID(T)); 2884} 2885 2886TypeID ASTWriter::GetOrCreateTypeID(QualType T) { 2887 return MakeTypeID(T, 2888 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this)); 2889} 2890 2891TypeID ASTWriter::getTypeID(QualType T) const { 2892 return MakeTypeID(T, 2893 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this)); 2894} 2895 2896TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) { 2897 if (T.isNull()) 2898 return TypeIdx(); 2899 assert(!T.getLocalFastQualifiers()); 2900 2901 TypeIdx &Idx = TypeIdxs[T]; 2902 if (Idx.getIndex() == 0) { 2903 // We haven't seen this type before. Assign it a new ID and put it 2904 // into the queue of types to emit. 2905 Idx = TypeIdx(NextTypeID++); 2906 DeclTypesToEmit.push(T); 2907 } 2908 return Idx; 2909} 2910 2911TypeIdx ASTWriter::getTypeIdx(QualType T) const { 2912 if (T.isNull()) 2913 return TypeIdx(); 2914 assert(!T.getLocalFastQualifiers()); 2915 2916 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 2917 assert(I != TypeIdxs.end() && "Type not emitted!"); 2918 return I->second; 2919} 2920 2921void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { 2922 Record.push_back(GetDeclRef(D)); 2923} 2924 2925DeclID ASTWriter::GetDeclRef(const Decl *D) { 2926 if (D == 0) { 2927 return 0; 2928 } 2929 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); 2930 DeclID &ID = DeclIDs[D]; 2931 if (ID == 0) { 2932 // We haven't seen this declaration before. Give it a new ID and 2933 // enqueue it in the list of declarations to emit. 2934 ID = NextDeclID++; 2935 DeclTypesToEmit.push(const_cast<Decl *>(D)); 2936 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) { 2937 // We don't add it to the replacement collection here, because we don't 2938 // have the offset yet. 2939 DeclTypesToEmit.push(const_cast<Decl *>(D)); 2940 // Reset the flag, so that we don't add this decl multiple times. 2941 const_cast<Decl *>(D)->setChangedSinceDeserialization(false); 2942 } 2943 2944 return ID; 2945} 2946 2947DeclID ASTWriter::getDeclID(const Decl *D) { 2948 if (D == 0) 2949 return 0; 2950 2951 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); 2952 return DeclIDs[D]; 2953} 2954 2955void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { 2956 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. 2957 Record.push_back(Name.getNameKind()); 2958 switch (Name.getNameKind()) { 2959 case DeclarationName::Identifier: 2960 AddIdentifierRef(Name.getAsIdentifierInfo(), Record); 2961 break; 2962 2963 case DeclarationName::ObjCZeroArgSelector: 2964 case DeclarationName::ObjCOneArgSelector: 2965 case DeclarationName::ObjCMultiArgSelector: 2966 AddSelectorRef(Name.getObjCSelector(), Record); 2967 break; 2968 2969 case DeclarationName::CXXConstructorName: 2970 case DeclarationName::CXXDestructorName: 2971 case DeclarationName::CXXConversionFunctionName: 2972 AddTypeRef(Name.getCXXNameType(), Record); 2973 break; 2974 2975 case DeclarationName::CXXOperatorName: 2976 Record.push_back(Name.getCXXOverloadedOperator()); 2977 break; 2978 2979 case DeclarationName::CXXLiteralOperatorName: 2980 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); 2981 break; 2982 2983 case DeclarationName::CXXUsingDirective: 2984 // No extra data to emit 2985 break; 2986 } 2987} 2988 2989void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 2990 DeclarationName Name, RecordDataImpl &Record) { 2991 switch (Name.getNameKind()) { 2992 case DeclarationName::CXXConstructorName: 2993 case DeclarationName::CXXDestructorName: 2994 case DeclarationName::CXXConversionFunctionName: 2995 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); 2996 break; 2997 2998 case DeclarationName::CXXOperatorName: 2999 AddSourceLocation( 3000 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), 3001 Record); 3002 AddSourceLocation( 3003 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), 3004 Record); 3005 break; 3006 3007 case DeclarationName::CXXLiteralOperatorName: 3008 AddSourceLocation( 3009 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), 3010 Record); 3011 break; 3012 3013 case DeclarationName::Identifier: 3014 case DeclarationName::ObjCZeroArgSelector: 3015 case DeclarationName::ObjCOneArgSelector: 3016 case DeclarationName::ObjCMultiArgSelector: 3017 case DeclarationName::CXXUsingDirective: 3018 break; 3019 } 3020} 3021 3022void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 3023 RecordDataImpl &Record) { 3024 AddDeclarationName(NameInfo.getName(), Record); 3025 AddSourceLocation(NameInfo.getLoc(), Record); 3026 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); 3027} 3028 3029void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, 3030 RecordDataImpl &Record) { 3031 AddNestedNameSpecifier(Info.NNS, Record); 3032 AddSourceRange(Info.NNSRange, Record); 3033 Record.push_back(Info.NumTemplParamLists); 3034 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) 3035 AddTemplateParameterList(Info.TemplParamLists[i], Record); 3036} 3037 3038void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, 3039 RecordDataImpl &Record) { 3040 // Nested name specifiers usually aren't too long. I think that 8 would 3041 // typically accomodate the vast majority. 3042 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames; 3043 3044 // Push each of the NNS's onto a stack for serialization in reverse order. 3045 while (NNS) { 3046 NestedNames.push_back(NNS); 3047 NNS = NNS->getPrefix(); 3048 } 3049 3050 Record.push_back(NestedNames.size()); 3051 while(!NestedNames.empty()) { 3052 NNS = NestedNames.pop_back_val(); 3053 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); 3054 Record.push_back(Kind); 3055 switch (Kind) { 3056 case NestedNameSpecifier::Identifier: 3057 AddIdentifierRef(NNS->getAsIdentifier(), Record); 3058 break; 3059 3060 case NestedNameSpecifier::Namespace: 3061 AddDeclRef(NNS->getAsNamespace(), Record); 3062 break; 3063 3064 case NestedNameSpecifier::TypeSpec: 3065 case NestedNameSpecifier::TypeSpecWithTemplate: 3066 AddTypeRef(QualType(NNS->getAsType(), 0), Record); 3067 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 3068 break; 3069 3070 case NestedNameSpecifier::Global: 3071 // Don't need to write an associated value. 3072 break; 3073 } 3074 } 3075} 3076 3077void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { 3078 TemplateName::NameKind Kind = Name.getKind(); 3079 Record.push_back(Kind); 3080 switch (Kind) { 3081 case TemplateName::Template: 3082 AddDeclRef(Name.getAsTemplateDecl(), Record); 3083 break; 3084 3085 case TemplateName::OverloadedTemplate: { 3086 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); 3087 Record.push_back(OvT->size()); 3088 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); 3089 I != E; ++I) 3090 AddDeclRef(*I, Record); 3091 break; 3092 } 3093 3094 case TemplateName::QualifiedTemplate: { 3095 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); 3096 AddNestedNameSpecifier(QualT->getQualifier(), Record); 3097 Record.push_back(QualT->hasTemplateKeyword()); 3098 AddDeclRef(QualT->getTemplateDecl(), Record); 3099 break; 3100 } 3101 3102 case TemplateName::DependentTemplate: { 3103 DependentTemplateName *DepT = Name.getAsDependentTemplateName(); 3104 AddNestedNameSpecifier(DepT->getQualifier(), Record); 3105 Record.push_back(DepT->isIdentifier()); 3106 if (DepT->isIdentifier()) 3107 AddIdentifierRef(DepT->getIdentifier(), Record); 3108 else 3109 Record.push_back(DepT->getOperator()); 3110 break; 3111 } 3112 } 3113} 3114 3115void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, 3116 RecordDataImpl &Record) { 3117 Record.push_back(Arg.getKind()); 3118 switch (Arg.getKind()) { 3119 case TemplateArgument::Null: 3120 break; 3121 case TemplateArgument::Type: 3122 AddTypeRef(Arg.getAsType(), Record); 3123 break; 3124 case TemplateArgument::Declaration: 3125 AddDeclRef(Arg.getAsDecl(), Record); 3126 break; 3127 case TemplateArgument::Integral: 3128 AddAPSInt(*Arg.getAsIntegral(), Record); 3129 AddTypeRef(Arg.getIntegralType(), Record); 3130 break; 3131 case TemplateArgument::Template: 3132 AddTemplateName(Arg.getAsTemplate(), Record); 3133 break; 3134 case TemplateArgument::Expression: 3135 AddStmt(Arg.getAsExpr()); 3136 break; 3137 case TemplateArgument::Pack: 3138 Record.push_back(Arg.pack_size()); 3139 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); 3140 I != E; ++I) 3141 AddTemplateArgument(*I, Record); 3142 break; 3143 } 3144} 3145 3146void 3147ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, 3148 RecordDataImpl &Record) { 3149 assert(TemplateParams && "No TemplateParams!"); 3150 AddSourceLocation(TemplateParams->getTemplateLoc(), Record); 3151 AddSourceLocation(TemplateParams->getLAngleLoc(), Record); 3152 AddSourceLocation(TemplateParams->getRAngleLoc(), Record); 3153 Record.push_back(TemplateParams->size()); 3154 for (TemplateParameterList::const_iterator 3155 P = TemplateParams->begin(), PEnd = TemplateParams->end(); 3156 P != PEnd; ++P) 3157 AddDeclRef(*P, Record); 3158} 3159 3160/// \brief Emit a template argument list. 3161void 3162ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, 3163 RecordDataImpl &Record) { 3164 assert(TemplateArgs && "No TemplateArgs!"); 3165 Record.push_back(TemplateArgs->flat_size()); 3166 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i) 3167 AddTemplateArgument(TemplateArgs->get(i), Record); 3168} 3169 3170 3171void 3172ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) { 3173 Record.push_back(Set.size()); 3174 for (UnresolvedSetImpl::const_iterator 3175 I = Set.begin(), E = Set.end(); I != E; ++I) { 3176 AddDeclRef(I.getDecl(), Record); 3177 Record.push_back(I.getAccess()); 3178 } 3179} 3180 3181void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, 3182 RecordDataImpl &Record) { 3183 Record.push_back(Base.isVirtual()); 3184 Record.push_back(Base.isBaseOfClass()); 3185 Record.push_back(Base.getAccessSpecifierAsWritten()); 3186 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); 3187 AddSourceRange(Base.getSourceRange(), Record); 3188} 3189 3190void ASTWriter::FlushCXXBaseSpecifiers() { 3191 RecordData Record; 3192 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) { 3193 Record.clear(); 3194 3195 // Record the offset of this base-specifier set. 3196 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID; 3197 if (Index == CXXBaseSpecifiersOffsets.size()) 3198 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); 3199 else { 3200 if (Index > CXXBaseSpecifiersOffsets.size()) 3201 CXXBaseSpecifiersOffsets.resize(Index + 1); 3202 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); 3203 } 3204 3205 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, 3206 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; 3207 Record.push_back(BEnd - B); 3208 for (; B != BEnd; ++B) 3209 AddCXXBaseSpecifier(*B, Record); 3210 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); 3211 3212 // Flush any expressions that were written as part of the base specifiers. 3213 FlushStmts(); 3214 } 3215 3216 CXXBaseSpecifiersToWrite.clear(); 3217} 3218 3219void ASTWriter::AddCXXBaseOrMemberInitializers( 3220 const CXXBaseOrMemberInitializer * const *BaseOrMembers, 3221 unsigned NumBaseOrMembers, RecordDataImpl &Record) { 3222 Record.push_back(NumBaseOrMembers); 3223 for (unsigned i=0; i != NumBaseOrMembers; ++i) { 3224 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i]; 3225 3226 Record.push_back(Init->isBaseInitializer()); 3227 if (Init->isBaseInitializer()) { 3228 AddTypeSourceInfo(Init->getBaseClassInfo(), Record); 3229 Record.push_back(Init->isBaseVirtual()); 3230 } else { 3231 AddDeclRef(Init->getMember(), Record); 3232 } 3233 AddSourceLocation(Init->getMemberLocation(), Record); 3234 AddStmt(Init->getInit()); 3235 AddDeclRef(Init->getAnonUnionMember(), Record); 3236 AddSourceLocation(Init->getLParenLoc(), Record); 3237 AddSourceLocation(Init->getRParenLoc(), Record); 3238 Record.push_back(Init->isWritten()); 3239 if (Init->isWritten()) { 3240 Record.push_back(Init->getSourceOrder()); 3241 } else { 3242 Record.push_back(Init->getNumArrayIndices()); 3243 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) 3244 AddDeclRef(Init->getArrayIndex(i), Record); 3245 } 3246 } 3247} 3248 3249void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { 3250 assert(D->DefinitionData); 3251 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; 3252 Record.push_back(Data.UserDeclaredConstructor); 3253 Record.push_back(Data.UserDeclaredCopyConstructor); 3254 Record.push_back(Data.UserDeclaredCopyAssignment); 3255 Record.push_back(Data.UserDeclaredDestructor); 3256 Record.push_back(Data.Aggregate); 3257 Record.push_back(Data.PlainOldData); 3258 Record.push_back(Data.Empty); 3259 Record.push_back(Data.Polymorphic); 3260 Record.push_back(Data.Abstract); 3261 Record.push_back(Data.HasTrivialConstructor); 3262 Record.push_back(Data.HasTrivialCopyConstructor); 3263 Record.push_back(Data.HasTrivialCopyAssignment); 3264 Record.push_back(Data.HasTrivialDestructor); 3265 Record.push_back(Data.ComputedVisibleConversions); 3266 Record.push_back(Data.DeclaredDefaultConstructor); 3267 Record.push_back(Data.DeclaredCopyConstructor); 3268 Record.push_back(Data.DeclaredCopyAssignment); 3269 Record.push_back(Data.DeclaredDestructor); 3270 3271 Record.push_back(Data.NumBases); 3272 if (Data.NumBases > 0) 3273 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, 3274 Record); 3275 3276 // FIXME: Make VBases lazily computed when needed to avoid storing them. 3277 Record.push_back(Data.NumVBases); 3278 if (Data.NumVBases > 0) 3279 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, 3280 Record); 3281 3282 AddUnresolvedSet(Data.Conversions, Record); 3283 AddUnresolvedSet(Data.VisibleConversions, Record); 3284 // Data.Definition is the owning decl, no need to write it. 3285 AddDeclRef(Data.FirstFriend, Record); 3286} 3287 3288void ASTWriter::ReaderInitialized(ASTReader *Reader) { 3289 assert(Reader && "Cannot remove chain"); 3290 assert(!Chain && "Cannot replace chain"); 3291 assert(FirstDeclID == NextDeclID && 3292 FirstTypeID == NextTypeID && 3293 FirstIdentID == NextIdentID && 3294 FirstSelectorID == NextSelectorID && 3295 FirstMacroID == NextMacroID && 3296 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID && 3297 "Setting chain after writing has started."); 3298 Chain = Reader; 3299 3300 FirstDeclID += Chain->getTotalNumDecls(); 3301 FirstTypeID += Chain->getTotalNumTypes(); 3302 FirstIdentID += Chain->getTotalNumIdentifiers(); 3303 FirstSelectorID += Chain->getTotalNumSelectors(); 3304 FirstMacroID += Chain->getTotalNumMacroDefinitions(); 3305 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers(); 3306 NextDeclID = FirstDeclID; 3307 NextTypeID = FirstTypeID; 3308 NextIdentID = FirstIdentID; 3309 NextSelectorID = FirstSelectorID; 3310 NextMacroID = FirstMacroID; 3311 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID; 3312} 3313 3314void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { 3315 IdentifierIDs[II] = ID; 3316} 3317 3318void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { 3319 // Always take the highest-numbered type index. This copes with an interesting 3320 // case for chained AST writing where we schedule writing the type and then, 3321 // later, deserialize the type from another AST. In this case, we want to 3322 // keep the higher-numbered entry so that we can properly write it out to 3323 // the AST file. 3324 TypeIdx &StoredIdx = TypeIdxs[T]; 3325 if (Idx.getIndex() >= StoredIdx.getIndex()) 3326 StoredIdx = Idx; 3327} 3328 3329void ASTWriter::DeclRead(DeclID ID, const Decl *D) { 3330 DeclIDs[D] = ID; 3331} 3332 3333void ASTWriter::SelectorRead(SelectorID ID, Selector S) { 3334 SelectorIDs[S] = ID; 3335} 3336 3337void ASTWriter::MacroDefinitionRead(serialization::MacroID ID, 3338 MacroDefinition *MD) { 3339 MacroDefinitions[MD] = ID; 3340} 3341 3342void ASTWriter::CompletedTagDefinition(const TagDecl *D) { 3343 assert(D->isDefinition()); 3344 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 3345 // We are interested when a PCH decl is modified. 3346 if (RD->getPCHLevel() > 0) { 3347 // A forward reference was mutated into a definition. Rewrite it. 3348 // FIXME: This happens during template instantiation, should we 3349 // have created a new definition decl instead ? 3350 RewriteDecl(RD); 3351 } 3352 3353 for (CXXRecordDecl::redecl_iterator 3354 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) { 3355 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I); 3356 if (Redecl == RD) 3357 continue; 3358 3359 // We are interested when a PCH decl is modified. 3360 if (Redecl->getPCHLevel() > 0) { 3361 UpdateRecord &Record = DeclUpdates[Redecl]; 3362 Record.push_back(UPD_CXX_SET_DEFINITIONDATA); 3363 assert(Redecl->DefinitionData); 3364 assert(Redecl->DefinitionData->Definition == D); 3365 AddDeclRef(D, Record); // the DefinitionDecl 3366 } 3367 } 3368 } 3369} 3370void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { 3371 // TU and namespaces are handled elsewhere. 3372 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) 3373 return; 3374 3375 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0)) 3376 return; // Not a source decl added to a DeclContext from PCH. 3377 3378 AddUpdatedDeclContext(DC); 3379} 3380 3381void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { 3382 assert(D->isImplicit()); 3383 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0)) 3384 return; // Not a source member added to a class from PCH. 3385 if (!isa<CXXMethodDecl>(D)) 3386 return; // We are interested in lazily declared implicit methods. 3387 3388 // A decl coming from PCH was modified. 3389 assert(RD->isDefinition()); 3390 UpdateRecord &Record = DeclUpdates[RD]; 3391 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER); 3392 AddDeclRef(D, Record); 3393} 3394 3395void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, 3396 const ClassTemplateSpecializationDecl *D) { 3397 // The specializations set is kept in the canonical template. 3398 TD = TD->getCanonicalDecl(); 3399 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0)) 3400 return; // Not a source specialization added to a template from PCH. 3401 3402 UpdateRecord &Record = DeclUpdates[TD]; 3403 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); 3404 AddDeclRef(D, Record); 3405} 3406