ASTWriter.cpp revision ce835df9301d5bee7fc2c70344e05541f21a5dda
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 "clang/Serialization/ASTSerializationListener.h" 16#include "ASTCommon.h" 17#include "clang/Sema/Sema.h" 18#include "clang/Sema/IdentifierResolver.h" 19#include "clang/AST/ASTContext.h" 20#include "clang/AST/Decl.h" 21#include "clang/AST/DeclContextInternals.h" 22#include "clang/AST/DeclTemplate.h" 23#include "clang/AST/DeclFriend.h" 24#include "clang/AST/Expr.h" 25#include "clang/AST/ExprCXX.h" 26#include "clang/AST/Type.h" 27#include "clang/AST/TypeLocVisitor.h" 28#include "clang/Serialization/ASTReader.h" 29#include "clang/Lex/MacroInfo.h" 30#include "clang/Lex/PreprocessingRecord.h" 31#include "clang/Lex/Preprocessor.h" 32#include "clang/Lex/HeaderSearch.h" 33#include "clang/Basic/FileManager.h" 34#include "clang/Basic/FileSystemStatCache.h" 35#include "clang/Basic/OnDiskHashTable.h" 36#include "clang/Basic/SourceManager.h" 37#include "clang/Basic/SourceManagerInternals.h" 38#include "clang/Basic/TargetInfo.h" 39#include "clang/Basic/Version.h" 40#include "clang/Basic/VersionTuple.h" 41#include "llvm/ADT/APFloat.h" 42#include "llvm/ADT/APInt.h" 43#include "llvm/ADT/StringExtras.h" 44#include "llvm/Bitcode/BitstreamWriter.h" 45#include "llvm/Support/FileSystem.h" 46#include "llvm/Support/MemoryBuffer.h" 47#include "llvm/Support/Path.h" 48#include <algorithm> 49#include <cstdio> 50#include <string.h> 51#include <utility> 52using namespace clang; 53using namespace clang::serialization; 54 55template <typename T, typename Allocator> 56static StringRef data(const std::vector<T, Allocator> &v) { 57 if (v.empty()) return StringRef(); 58 return StringRef(reinterpret_cast<const char*>(&v[0]), 59 sizeof(T) * v.size()); 60} 61 62template <typename T> 63static StringRef data(const SmallVectorImpl<T> &v) { 64 return StringRef(reinterpret_cast<const char*>(v.data()), 65 sizeof(T) * v.size()); 66} 67 68//===----------------------------------------------------------------------===// 69// Type serialization 70//===----------------------------------------------------------------------===// 71 72namespace { 73 class ASTTypeWriter { 74 ASTWriter &Writer; 75 ASTWriter::RecordDataImpl &Record; 76 77 public: 78 /// \brief Type code that corresponds to the record generated. 79 TypeCode Code; 80 81 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 82 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } 83 84 void VisitArrayType(const ArrayType *T); 85 void VisitFunctionType(const FunctionType *T); 86 void VisitTagType(const TagType *T); 87 88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); 89#define ABSTRACT_TYPE(Class, Base) 90#include "clang/AST/TypeNodes.def" 91 }; 92} 93 94void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { 95 assert(false && "Built-in types are never serialized"); 96} 97 98void ASTTypeWriter::VisitComplexType(const ComplexType *T) { 99 Writer.AddTypeRef(T->getElementType(), Record); 100 Code = TYPE_COMPLEX; 101} 102 103void ASTTypeWriter::VisitPointerType(const PointerType *T) { 104 Writer.AddTypeRef(T->getPointeeType(), Record); 105 Code = TYPE_POINTER; 106} 107 108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { 109 Writer.AddTypeRef(T->getPointeeType(), Record); 110 Code = TYPE_BLOCK_POINTER; 111} 112 113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { 114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); 115 Record.push_back(T->isSpelledAsLValue()); 116 Code = TYPE_LVALUE_REFERENCE; 117} 118 119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { 120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); 121 Code = TYPE_RVALUE_REFERENCE; 122} 123 124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { 125 Writer.AddTypeRef(T->getPointeeType(), Record); 126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record); 127 Code = TYPE_MEMBER_POINTER; 128} 129 130void ASTTypeWriter::VisitArrayType(const ArrayType *T) { 131 Writer.AddTypeRef(T->getElementType(), Record); 132 Record.push_back(T->getSizeModifier()); // FIXME: stable values 133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values 134} 135 136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { 137 VisitArrayType(T); 138 Writer.AddAPInt(T->getSize(), Record); 139 Code = TYPE_CONSTANT_ARRAY; 140} 141 142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 143 VisitArrayType(T); 144 Code = TYPE_INCOMPLETE_ARRAY; 145} 146 147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { 148 VisitArrayType(T); 149 Writer.AddSourceLocation(T->getLBracketLoc(), Record); 150 Writer.AddSourceLocation(T->getRBracketLoc(), Record); 151 Writer.AddStmt(T->getSizeExpr()); 152 Code = TYPE_VARIABLE_ARRAY; 153} 154 155void ASTTypeWriter::VisitVectorType(const VectorType *T) { 156 Writer.AddTypeRef(T->getElementType(), Record); 157 Record.push_back(T->getNumElements()); 158 Record.push_back(T->getVectorKind()); 159 Code = TYPE_VECTOR; 160} 161 162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { 163 VisitVectorType(T); 164 Code = TYPE_EXT_VECTOR; 165} 166 167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { 168 Writer.AddTypeRef(T->getResultType(), Record); 169 FunctionType::ExtInfo C = T->getExtInfo(); 170 Record.push_back(C.getNoReturn()); 171 Record.push_back(C.getHasRegParm()); 172 Record.push_back(C.getRegParm()); 173 // FIXME: need to stabilize encoding of calling convention... 174 Record.push_back(C.getCC()); 175 Record.push_back(C.getProducesResult()); 176} 177 178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 179 VisitFunctionType(T); 180 Code = TYPE_FUNCTION_NO_PROTO; 181} 182 183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { 184 VisitFunctionType(T); 185 Record.push_back(T->getNumArgs()); 186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) 187 Writer.AddTypeRef(T->getArgType(I), Record); 188 Record.push_back(T->isVariadic()); 189 Record.push_back(T->getTypeQuals()); 190 Record.push_back(static_cast<unsigned>(T->getRefQualifier())); 191 Record.push_back(T->getExceptionSpecType()); 192 if (T->getExceptionSpecType() == EST_Dynamic) { 193 Record.push_back(T->getNumExceptions()); 194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) 195 Writer.AddTypeRef(T->getExceptionType(I), Record); 196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) { 197 Writer.AddStmt(T->getNoexceptExpr()); 198 } 199 Code = TYPE_FUNCTION_PROTO; 200} 201 202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 203 Writer.AddDeclRef(T->getDecl(), Record); 204 Code = TYPE_UNRESOLVED_USING; 205} 206 207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { 208 Writer.AddDeclRef(T->getDecl(), Record); 209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); 210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); 211 Code = TYPE_TYPEDEF; 212} 213 214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { 215 Writer.AddStmt(T->getUnderlyingExpr()); 216 Code = TYPE_TYPEOF_EXPR; 217} 218 219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { 220 Writer.AddTypeRef(T->getUnderlyingType(), Record); 221 Code = TYPE_TYPEOF; 222} 223 224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { 225 Writer.AddStmt(T->getUnderlyingExpr()); 226 Code = TYPE_DECLTYPE; 227} 228 229void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) { 230 Writer.AddTypeRef(T->getBaseType(), Record); 231 Writer.AddTypeRef(T->getUnderlyingType(), Record); 232 Record.push_back(T->getUTTKind()); 233 Code = TYPE_UNARY_TRANSFORM; 234} 235 236void ASTTypeWriter::VisitAutoType(const AutoType *T) { 237 Writer.AddTypeRef(T->getDeducedType(), Record); 238 Code = TYPE_AUTO; 239} 240 241void ASTTypeWriter::VisitTagType(const TagType *T) { 242 Record.push_back(T->isDependentType()); 243 Writer.AddDeclRef(T->getDecl(), Record); 244 assert(!T->isBeingDefined() && 245 "Cannot serialize in the middle of a type definition"); 246} 247 248void ASTTypeWriter::VisitRecordType(const RecordType *T) { 249 VisitTagType(T); 250 Code = TYPE_RECORD; 251} 252 253void ASTTypeWriter::VisitEnumType(const EnumType *T) { 254 VisitTagType(T); 255 Code = TYPE_ENUM; 256} 257 258void ASTTypeWriter::VisitAttributedType(const AttributedType *T) { 259 Writer.AddTypeRef(T->getModifiedType(), Record); 260 Writer.AddTypeRef(T->getEquivalentType(), Record); 261 Record.push_back(T->getAttrKind()); 262 Code = TYPE_ATTRIBUTED; 263} 264 265void 266ASTTypeWriter::VisitSubstTemplateTypeParmType( 267 const SubstTemplateTypeParmType *T) { 268 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 269 Writer.AddTypeRef(T->getReplacementType(), Record); 270 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; 271} 272 273void 274ASTTypeWriter::VisitSubstTemplateTypeParmPackType( 275 const SubstTemplateTypeParmPackType *T) { 276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 277 Writer.AddTemplateArgument(T->getArgumentPack(), Record); 278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK; 279} 280 281void 282ASTTypeWriter::VisitTemplateSpecializationType( 283 const TemplateSpecializationType *T) { 284 Record.push_back(T->isDependentType()); 285 Writer.AddTemplateName(T->getTemplateName(), Record); 286 Record.push_back(T->getNumArgs()); 287 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); 288 ArgI != ArgE; ++ArgI) 289 Writer.AddTemplateArgument(*ArgI, Record); 290 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() : 291 T->isCanonicalUnqualified() ? QualType() 292 : T->getCanonicalTypeInternal(), 293 Record); 294 Code = TYPE_TEMPLATE_SPECIALIZATION; 295} 296 297void 298ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { 299 VisitArrayType(T); 300 Writer.AddStmt(T->getSizeExpr()); 301 Writer.AddSourceRange(T->getBracketsRange(), Record); 302 Code = TYPE_DEPENDENT_SIZED_ARRAY; 303} 304 305void 306ASTTypeWriter::VisitDependentSizedExtVectorType( 307 const DependentSizedExtVectorType *T) { 308 // FIXME: Serialize this type (C++ only) 309 assert(false && "Cannot serialize dependent sized extended vector types"); 310} 311 312void 313ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 314 Record.push_back(T->getDepth()); 315 Record.push_back(T->getIndex()); 316 Record.push_back(T->isParameterPack()); 317 Writer.AddDeclRef(T->getDecl(), Record); 318 Code = TYPE_TEMPLATE_TYPE_PARM; 319} 320 321void 322ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { 323 Record.push_back(T->getKeyword()); 324 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 325 Writer.AddIdentifierRef(T->getIdentifier(), Record); 326 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 327 : T->getCanonicalTypeInternal(), 328 Record); 329 Code = TYPE_DEPENDENT_NAME; 330} 331 332void 333ASTTypeWriter::VisitDependentTemplateSpecializationType( 334 const DependentTemplateSpecializationType *T) { 335 Record.push_back(T->getKeyword()); 336 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 337 Writer.AddIdentifierRef(T->getIdentifier(), Record); 338 Record.push_back(T->getNumArgs()); 339 for (DependentTemplateSpecializationType::iterator 340 I = T->begin(), E = T->end(); I != E; ++I) 341 Writer.AddTemplateArgument(*I, Record); 342 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; 343} 344 345void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) { 346 Writer.AddTypeRef(T->getPattern(), Record); 347 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions()) 348 Record.push_back(*NumExpansions + 1); 349 else 350 Record.push_back(0); 351 Code = TYPE_PACK_EXPANSION; 352} 353 354void ASTTypeWriter::VisitParenType(const ParenType *T) { 355 Writer.AddTypeRef(T->getInnerType(), Record); 356 Code = TYPE_PAREN; 357} 358 359void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { 360 Record.push_back(T->getKeyword()); 361 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 362 Writer.AddTypeRef(T->getNamedType(), Record); 363 Code = TYPE_ELABORATED; 364} 365 366void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { 367 Writer.AddDeclRef(T->getDecl(), Record); 368 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); 369 Code = TYPE_INJECTED_CLASS_NAME; 370} 371 372void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 373 Writer.AddDeclRef(T->getDecl(), Record); 374 Code = TYPE_OBJC_INTERFACE; 375} 376 377void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { 378 Writer.AddTypeRef(T->getBaseType(), Record); 379 Record.push_back(T->getNumProtocols()); 380 for (ObjCObjectType::qual_iterator I = T->qual_begin(), 381 E = T->qual_end(); I != E; ++I) 382 Writer.AddDeclRef(*I, Record); 383 Code = TYPE_OBJC_OBJECT; 384} 385 386void 387ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 388 Writer.AddTypeRef(T->getPointeeType(), Record); 389 Code = TYPE_OBJC_OBJECT_POINTER; 390} 391 392namespace { 393 394class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { 395 ASTWriter &Writer; 396 ASTWriter::RecordDataImpl &Record; 397 398public: 399 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 400 : Writer(Writer), Record(Record) { } 401 402#define ABSTRACT_TYPELOC(CLASS, PARENT) 403#define TYPELOC(CLASS, PARENT) \ 404 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 405#include "clang/AST/TypeLocNodes.def" 406 407 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); 408 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); 409}; 410 411} 412 413void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 414 // nothing to do 415} 416void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 417 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); 418 if (TL.needsExtraLocalData()) { 419 Record.push_back(TL.getWrittenTypeSpec()); 420 Record.push_back(TL.getWrittenSignSpec()); 421 Record.push_back(TL.getWrittenWidthSpec()); 422 Record.push_back(TL.hasModeAttr()); 423 } 424} 425void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { 426 Writer.AddSourceLocation(TL.getNameLoc(), Record); 427} 428void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { 429 Writer.AddSourceLocation(TL.getStarLoc(), Record); 430} 431void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 432 Writer.AddSourceLocation(TL.getCaretLoc(), Record); 433} 434void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 435 Writer.AddSourceLocation(TL.getAmpLoc(), Record); 436} 437void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 438 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); 439} 440void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 441 Writer.AddSourceLocation(TL.getStarLoc(), Record); 442 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record); 443} 444void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { 445 Writer.AddSourceLocation(TL.getLBracketLoc(), Record); 446 Writer.AddSourceLocation(TL.getRBracketLoc(), Record); 447 Record.push_back(TL.getSizeExpr() ? 1 : 0); 448 if (TL.getSizeExpr()) 449 Writer.AddStmt(TL.getSizeExpr()); 450} 451void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 452 VisitArrayTypeLoc(TL); 453} 454void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 455 VisitArrayTypeLoc(TL); 456} 457void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 458 VisitArrayTypeLoc(TL); 459} 460void TypeLocWriter::VisitDependentSizedArrayTypeLoc( 461 DependentSizedArrayTypeLoc TL) { 462 VisitArrayTypeLoc(TL); 463} 464void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( 465 DependentSizedExtVectorTypeLoc TL) { 466 Writer.AddSourceLocation(TL.getNameLoc(), Record); 467} 468void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { 469 Writer.AddSourceLocation(TL.getNameLoc(), Record); 470} 471void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 472 Writer.AddSourceLocation(TL.getNameLoc(), Record); 473} 474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 475 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record); 476 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record); 477 Record.push_back(TL.getTrailingReturn()); 478 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 479 Writer.AddDeclRef(TL.getArg(i), Record); 480} 481void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 482 VisitFunctionTypeLoc(TL); 483} 484void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 485 VisitFunctionTypeLoc(TL); 486} 487void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 488 Writer.AddSourceLocation(TL.getNameLoc(), Record); 489} 490void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 491 Writer.AddSourceLocation(TL.getNameLoc(), Record); 492} 493void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 494 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 495 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 496 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 497} 498void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 500 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 501 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 502 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 503} 504void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 505 Writer.AddSourceLocation(TL.getNameLoc(), Record); 506} 507void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 508 Writer.AddSourceLocation(TL.getKWLoc(), Record); 509 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 510 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 511 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 512} 513void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { 514 Writer.AddSourceLocation(TL.getNameLoc(), Record); 515} 516void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { 517 Writer.AddSourceLocation(TL.getNameLoc(), Record); 518} 519void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { 520 Writer.AddSourceLocation(TL.getNameLoc(), Record); 521} 522void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 523 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record); 524 if (TL.hasAttrOperand()) { 525 SourceRange range = TL.getAttrOperandParensRange(); 526 Writer.AddSourceLocation(range.getBegin(), Record); 527 Writer.AddSourceLocation(range.getEnd(), Record); 528 } 529 if (TL.hasAttrExprOperand()) { 530 Expr *operand = TL.getAttrExprOperand(); 531 Record.push_back(operand ? 1 : 0); 532 if (operand) Writer.AddStmt(operand); 533 } else if (TL.hasAttrEnumOperand()) { 534 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record); 535 } 536} 537void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 538 Writer.AddSourceLocation(TL.getNameLoc(), Record); 539} 540void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( 541 SubstTemplateTypeParmTypeLoc TL) { 542 Writer.AddSourceLocation(TL.getNameLoc(), Record); 543} 544void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( 545 SubstTemplateTypeParmPackTypeLoc TL) { 546 Writer.AddSourceLocation(TL.getNameLoc(), Record); 547} 548void TypeLocWriter::VisitTemplateSpecializationTypeLoc( 549 TemplateSpecializationTypeLoc TL) { 550 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 553 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), 555 TL.getArgLoc(i).getLocInfo(), Record); 556} 557void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { 558 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 559 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 560} 561void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 562 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 563 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 564} 565void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 566 Writer.AddSourceLocation(TL.getNameLoc(), Record); 567} 568void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 569 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 570 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 571 Writer.AddSourceLocation(TL.getNameLoc(), Record); 572} 573void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( 574 DependentTemplateSpecializationTypeLoc TL) { 575 Writer.AddSourceLocation(TL.getKeywordLoc(), Record); 576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 577 Writer.AddSourceLocation(TL.getNameLoc(), Record); 578 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 579 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 580 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 581 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), 582 TL.getArgLoc(I).getLocInfo(), Record); 583} 584void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 585 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record); 586} 587void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 588 Writer.AddSourceLocation(TL.getNameLoc(), Record); 589} 590void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 591 Record.push_back(TL.hasBaseTypeAsWritten()); 592 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 593 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 594 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 595 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); 596} 597void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 598 Writer.AddSourceLocation(TL.getStarLoc(), Record); 599} 600 601//===----------------------------------------------------------------------===// 602// ASTWriter Implementation 603//===----------------------------------------------------------------------===// 604 605static void EmitBlockID(unsigned ID, const char *Name, 606 llvm::BitstreamWriter &Stream, 607 ASTWriter::RecordDataImpl &Record) { 608 Record.clear(); 609 Record.push_back(ID); 610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 611 612 // Emit the block name if present. 613 if (Name == 0 || Name[0] == 0) return; 614 Record.clear(); 615 while (*Name) 616 Record.push_back(*Name++); 617 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 618} 619 620static void EmitRecordID(unsigned ID, const char *Name, 621 llvm::BitstreamWriter &Stream, 622 ASTWriter::RecordDataImpl &Record) { 623 Record.clear(); 624 Record.push_back(ID); 625 while (*Name) 626 Record.push_back(*Name++); 627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 628} 629 630static void AddStmtsExprs(llvm::BitstreamWriter &Stream, 631 ASTWriter::RecordDataImpl &Record) { 632#define RECORD(X) EmitRecordID(X, #X, Stream, Record) 633 RECORD(STMT_STOP); 634 RECORD(STMT_NULL_PTR); 635 RECORD(STMT_NULL); 636 RECORD(STMT_COMPOUND); 637 RECORD(STMT_CASE); 638 RECORD(STMT_DEFAULT); 639 RECORD(STMT_LABEL); 640 RECORD(STMT_IF); 641 RECORD(STMT_SWITCH); 642 RECORD(STMT_WHILE); 643 RECORD(STMT_DO); 644 RECORD(STMT_FOR); 645 RECORD(STMT_GOTO); 646 RECORD(STMT_INDIRECT_GOTO); 647 RECORD(STMT_CONTINUE); 648 RECORD(STMT_BREAK); 649 RECORD(STMT_RETURN); 650 RECORD(STMT_DECL); 651 RECORD(STMT_ASM); 652 RECORD(EXPR_PREDEFINED); 653 RECORD(EXPR_DECL_REF); 654 RECORD(EXPR_INTEGER_LITERAL); 655 RECORD(EXPR_FLOATING_LITERAL); 656 RECORD(EXPR_IMAGINARY_LITERAL); 657 RECORD(EXPR_STRING_LITERAL); 658 RECORD(EXPR_CHARACTER_LITERAL); 659 RECORD(EXPR_PAREN); 660 RECORD(EXPR_UNARY_OPERATOR); 661 RECORD(EXPR_SIZEOF_ALIGN_OF); 662 RECORD(EXPR_ARRAY_SUBSCRIPT); 663 RECORD(EXPR_CALL); 664 RECORD(EXPR_MEMBER); 665 RECORD(EXPR_BINARY_OPERATOR); 666 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); 667 RECORD(EXPR_CONDITIONAL_OPERATOR); 668 RECORD(EXPR_IMPLICIT_CAST); 669 RECORD(EXPR_CSTYLE_CAST); 670 RECORD(EXPR_COMPOUND_LITERAL); 671 RECORD(EXPR_EXT_VECTOR_ELEMENT); 672 RECORD(EXPR_INIT_LIST); 673 RECORD(EXPR_DESIGNATED_INIT); 674 RECORD(EXPR_IMPLICIT_VALUE_INIT); 675 RECORD(EXPR_VA_ARG); 676 RECORD(EXPR_ADDR_LABEL); 677 RECORD(EXPR_STMT); 678 RECORD(EXPR_CHOOSE); 679 RECORD(EXPR_GNU_NULL); 680 RECORD(EXPR_SHUFFLE_VECTOR); 681 RECORD(EXPR_BLOCK); 682 RECORD(EXPR_BLOCK_DECL_REF); 683 RECORD(EXPR_GENERIC_SELECTION); 684 RECORD(EXPR_OBJC_STRING_LITERAL); 685 RECORD(EXPR_OBJC_ENCODE); 686 RECORD(EXPR_OBJC_SELECTOR_EXPR); 687 RECORD(EXPR_OBJC_PROTOCOL_EXPR); 688 RECORD(EXPR_OBJC_IVAR_REF_EXPR); 689 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); 690 RECORD(EXPR_OBJC_KVC_REF_EXPR); 691 RECORD(EXPR_OBJC_MESSAGE_EXPR); 692 RECORD(STMT_OBJC_FOR_COLLECTION); 693 RECORD(STMT_OBJC_CATCH); 694 RECORD(STMT_OBJC_FINALLY); 695 RECORD(STMT_OBJC_AT_TRY); 696 RECORD(STMT_OBJC_AT_SYNCHRONIZED); 697 RECORD(STMT_OBJC_AT_THROW); 698 RECORD(EXPR_CXX_OPERATOR_CALL); 699 RECORD(EXPR_CXX_CONSTRUCT); 700 RECORD(EXPR_CXX_STATIC_CAST); 701 RECORD(EXPR_CXX_DYNAMIC_CAST); 702 RECORD(EXPR_CXX_REINTERPRET_CAST); 703 RECORD(EXPR_CXX_CONST_CAST); 704 RECORD(EXPR_CXX_FUNCTIONAL_CAST); 705 RECORD(EXPR_CXX_BOOL_LITERAL); 706 RECORD(EXPR_CXX_NULL_PTR_LITERAL); 707 RECORD(EXPR_CXX_TYPEID_EXPR); 708 RECORD(EXPR_CXX_TYPEID_TYPE); 709 RECORD(EXPR_CXX_UUIDOF_EXPR); 710 RECORD(EXPR_CXX_UUIDOF_TYPE); 711 RECORD(EXPR_CXX_THIS); 712 RECORD(EXPR_CXX_THROW); 713 RECORD(EXPR_CXX_DEFAULT_ARG); 714 RECORD(EXPR_CXX_BIND_TEMPORARY); 715 RECORD(EXPR_CXX_SCALAR_VALUE_INIT); 716 RECORD(EXPR_CXX_NEW); 717 RECORD(EXPR_CXX_DELETE); 718 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR); 719 RECORD(EXPR_EXPR_WITH_CLEANUPS); 720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER); 721 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF); 722 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT); 723 RECORD(EXPR_CXX_UNRESOLVED_MEMBER); 724 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP); 725 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT); 726 RECORD(EXPR_CXX_NOEXCEPT); 727 RECORD(EXPR_OPAQUE_VALUE); 728 RECORD(EXPR_BINARY_TYPE_TRAIT); 729 RECORD(EXPR_PACK_EXPANSION); 730 RECORD(EXPR_SIZEOF_PACK); 731 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK); 732 RECORD(EXPR_CUDA_KERNEL_CALL); 733#undef RECORD 734} 735 736void ASTWriter::WriteBlockInfoBlock() { 737 RecordData Record; 738 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); 739 740#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) 741#define RECORD(X) EmitRecordID(X, #X, Stream, Record) 742 743 // AST Top-Level Block. 744 BLOCK(AST_BLOCK); 745 RECORD(ORIGINAL_FILE_NAME); 746 RECORD(ORIGINAL_FILE_ID); 747 RECORD(TYPE_OFFSET); 748 RECORD(DECL_OFFSET); 749 RECORD(LANGUAGE_OPTIONS); 750 RECORD(METADATA); 751 RECORD(IDENTIFIER_OFFSET); 752 RECORD(IDENTIFIER_TABLE); 753 RECORD(EXTERNAL_DEFINITIONS); 754 RECORD(SPECIAL_TYPES); 755 RECORD(STATISTICS); 756 RECORD(TENTATIVE_DEFINITIONS); 757 RECORD(UNUSED_FILESCOPED_DECLS); 758 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); 759 RECORD(SELECTOR_OFFSETS); 760 RECORD(METHOD_POOL); 761 RECORD(PP_COUNTER_VALUE); 762 RECORD(SOURCE_LOCATION_OFFSETS); 763 RECORD(SOURCE_LOCATION_PRELOADS); 764 RECORD(STAT_CACHE); 765 RECORD(EXT_VECTOR_DECLS); 766 RECORD(VERSION_CONTROL_BRANCH_REVISION); 767 RECORD(MACRO_DEFINITION_OFFSETS); 768 RECORD(IMPORTS); 769 RECORD(REFERENCED_SELECTOR_POOL); 770 RECORD(TU_UPDATE_LEXICAL); 771 RECORD(REDECLS_UPDATE_LATEST); 772 RECORD(SEMA_DECL_REFS); 773 RECORD(WEAK_UNDECLARED_IDENTIFIERS); 774 RECORD(PENDING_IMPLICIT_INSTANTIATIONS); 775 RECORD(DECL_REPLACEMENTS); 776 RECORD(UPDATE_VISIBLE); 777 RECORD(DECL_UPDATE_OFFSETS); 778 RECORD(DECL_UPDATES); 779 RECORD(CXX_BASE_SPECIFIER_OFFSETS); 780 RECORD(DIAG_PRAGMA_MAPPINGS); 781 RECORD(CUDA_SPECIAL_DECL_REFS); 782 RECORD(HEADER_SEARCH_TABLE); 783 RECORD(ORIGINAL_PCH_DIR); 784 RECORD(FP_PRAGMA_OPTIONS); 785 RECORD(OPENCL_EXTENSIONS); 786 RECORD(DELEGATING_CTORS); 787 RECORD(FILE_SOURCE_LOCATION_OFFSETS); 788 RECORD(KNOWN_NAMESPACES); 789 RECORD(MODULE_OFFSET_MAP); 790 RECORD(SOURCE_MANAGER_LINE_TABLE); 791 792 // SourceManager Block. 793 BLOCK(SOURCE_MANAGER_BLOCK); 794 RECORD(SM_SLOC_FILE_ENTRY); 795 RECORD(SM_SLOC_BUFFER_ENTRY); 796 RECORD(SM_SLOC_BUFFER_BLOB); 797 RECORD(SM_SLOC_EXPANSION_ENTRY); 798 799 // Preprocessor Block. 800 BLOCK(PREPROCESSOR_BLOCK); 801 RECORD(PP_MACRO_OBJECT_LIKE); 802 RECORD(PP_MACRO_FUNCTION_LIKE); 803 RECORD(PP_TOKEN); 804 805 // Decls and Types block. 806 BLOCK(DECLTYPES_BLOCK); 807 RECORD(TYPE_EXT_QUAL); 808 RECORD(TYPE_COMPLEX); 809 RECORD(TYPE_POINTER); 810 RECORD(TYPE_BLOCK_POINTER); 811 RECORD(TYPE_LVALUE_REFERENCE); 812 RECORD(TYPE_RVALUE_REFERENCE); 813 RECORD(TYPE_MEMBER_POINTER); 814 RECORD(TYPE_CONSTANT_ARRAY); 815 RECORD(TYPE_INCOMPLETE_ARRAY); 816 RECORD(TYPE_VARIABLE_ARRAY); 817 RECORD(TYPE_VECTOR); 818 RECORD(TYPE_EXT_VECTOR); 819 RECORD(TYPE_FUNCTION_PROTO); 820 RECORD(TYPE_FUNCTION_NO_PROTO); 821 RECORD(TYPE_TYPEDEF); 822 RECORD(TYPE_TYPEOF_EXPR); 823 RECORD(TYPE_TYPEOF); 824 RECORD(TYPE_RECORD); 825 RECORD(TYPE_ENUM); 826 RECORD(TYPE_OBJC_INTERFACE); 827 RECORD(TYPE_OBJC_OBJECT); 828 RECORD(TYPE_OBJC_OBJECT_POINTER); 829 RECORD(TYPE_DECLTYPE); 830 RECORD(TYPE_ELABORATED); 831 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM); 832 RECORD(TYPE_UNRESOLVED_USING); 833 RECORD(TYPE_INJECTED_CLASS_NAME); 834 RECORD(TYPE_OBJC_OBJECT); 835 RECORD(TYPE_TEMPLATE_TYPE_PARM); 836 RECORD(TYPE_TEMPLATE_SPECIALIZATION); 837 RECORD(TYPE_DEPENDENT_NAME); 838 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); 839 RECORD(TYPE_DEPENDENT_SIZED_ARRAY); 840 RECORD(TYPE_PAREN); 841 RECORD(TYPE_PACK_EXPANSION); 842 RECORD(TYPE_ATTRIBUTED); 843 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); 844 RECORD(DECL_TYPEDEF); 845 RECORD(DECL_ENUM); 846 RECORD(DECL_RECORD); 847 RECORD(DECL_ENUM_CONSTANT); 848 RECORD(DECL_FUNCTION); 849 RECORD(DECL_OBJC_METHOD); 850 RECORD(DECL_OBJC_INTERFACE); 851 RECORD(DECL_OBJC_PROTOCOL); 852 RECORD(DECL_OBJC_IVAR); 853 RECORD(DECL_OBJC_AT_DEFS_FIELD); 854 RECORD(DECL_OBJC_CLASS); 855 RECORD(DECL_OBJC_FORWARD_PROTOCOL); 856 RECORD(DECL_OBJC_CATEGORY); 857 RECORD(DECL_OBJC_CATEGORY_IMPL); 858 RECORD(DECL_OBJC_IMPLEMENTATION); 859 RECORD(DECL_OBJC_COMPATIBLE_ALIAS); 860 RECORD(DECL_OBJC_PROPERTY); 861 RECORD(DECL_OBJC_PROPERTY_IMPL); 862 RECORD(DECL_FIELD); 863 RECORD(DECL_VAR); 864 RECORD(DECL_IMPLICIT_PARAM); 865 RECORD(DECL_PARM_VAR); 866 RECORD(DECL_FILE_SCOPE_ASM); 867 RECORD(DECL_BLOCK); 868 RECORD(DECL_CONTEXT_LEXICAL); 869 RECORD(DECL_CONTEXT_VISIBLE); 870 RECORD(DECL_NAMESPACE); 871 RECORD(DECL_NAMESPACE_ALIAS); 872 RECORD(DECL_USING); 873 RECORD(DECL_USING_SHADOW); 874 RECORD(DECL_USING_DIRECTIVE); 875 RECORD(DECL_UNRESOLVED_USING_VALUE); 876 RECORD(DECL_UNRESOLVED_USING_TYPENAME); 877 RECORD(DECL_LINKAGE_SPEC); 878 RECORD(DECL_CXX_RECORD); 879 RECORD(DECL_CXX_METHOD); 880 RECORD(DECL_CXX_CONSTRUCTOR); 881 RECORD(DECL_CXX_DESTRUCTOR); 882 RECORD(DECL_CXX_CONVERSION); 883 RECORD(DECL_ACCESS_SPEC); 884 RECORD(DECL_FRIEND); 885 RECORD(DECL_FRIEND_TEMPLATE); 886 RECORD(DECL_CLASS_TEMPLATE); 887 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION); 888 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION); 889 RECORD(DECL_FUNCTION_TEMPLATE); 890 RECORD(DECL_TEMPLATE_TYPE_PARM); 891 RECORD(DECL_NON_TYPE_TEMPLATE_PARM); 892 RECORD(DECL_TEMPLATE_TEMPLATE_PARM); 893 RECORD(DECL_STATIC_ASSERT); 894 RECORD(DECL_CXX_BASE_SPECIFIERS); 895 RECORD(DECL_INDIRECTFIELD); 896 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK); 897 898 // Statements and Exprs can occur in the Decls and Types block. 899 AddStmtsExprs(Stream, Record); 900 901 BLOCK(PREPROCESSOR_DETAIL_BLOCK); 902 RECORD(PPD_MACRO_EXPANSION); 903 RECORD(PPD_MACRO_DEFINITION); 904 RECORD(PPD_INCLUSION_DIRECTIVE); 905 906#undef RECORD 907#undef BLOCK 908 Stream.ExitBlock(); 909} 910 911/// \brief Adjusts the given filename to only write out the portion of the 912/// filename that is not part of the system root directory. 913/// 914/// \param Filename the file name to adjust. 915/// 916/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and 917/// the returned filename will be adjusted by this system root. 918/// 919/// \returns either the original filename (if it needs no adjustment) or the 920/// adjusted filename (which points into the @p Filename parameter). 921static const char * 922adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) { 923 assert(Filename && "No file name to adjust?"); 924 925 if (isysroot.empty()) 926 return Filename; 927 928 // Verify that the filename and the system root have the same prefix. 929 unsigned Pos = 0; 930 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos) 931 if (Filename[Pos] != isysroot[Pos]) 932 return Filename; // Prefixes don't match. 933 934 // We hit the end of the filename before we hit the end of the system root. 935 if (!Filename[Pos]) 936 return Filename; 937 938 // If the file name has a '/' at the current position, skip over the '/'. 939 // We distinguish sysroot-based includes from absolute includes by the 940 // absence of '/' at the beginning of sysroot-based includes. 941 if (Filename[Pos] == '/') 942 ++Pos; 943 944 return Filename + Pos; 945} 946 947/// \brief Write the AST metadata (e.g., i686-apple-darwin9). 948void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot, 949 const std::string &OutputFile) { 950 using namespace llvm; 951 952 // Metadata 953 const TargetInfo &Target = Context.getTargetInfo(); 954 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); 955 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA)); 956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major 957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor 958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major 959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor 960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable 961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple 962 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); 963 964 RecordData Record; 965 Record.push_back(METADATA); 966 Record.push_back(VERSION_MAJOR); 967 Record.push_back(VERSION_MINOR); 968 Record.push_back(CLANG_VERSION_MAJOR); 969 Record.push_back(CLANG_VERSION_MINOR); 970 Record.push_back(!isysroot.empty()); 971 const std::string &Triple = Target.getTriple().getTriple(); 972 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple); 973 974 if (Chain) { 975 serialization::ModuleManager &Mgr = Chain->getModuleManager(); 976 llvm::SmallVector<char, 128> ModulePaths; 977 Record.clear(); 978 979 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end(); 980 M != MEnd; ++M) { 981 // Skip modules that weren't directly imported. 982 if (!(*M)->isDirectlyImported()) 983 continue; 984 985 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding 986 // FIXME: Write import location, once it matters. 987 // FIXME: This writes the absolute path for AST files we depend on. 988 const std::string &FileName = (*M)->FileName; 989 Record.push_back(FileName.size()); 990 Record.append(FileName.begin(), FileName.end()); 991 } 992 Stream.EmitRecord(IMPORTS, Record); 993 } 994 995 // Original file name and file ID 996 SourceManager &SM = Context.getSourceManager(); 997 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 998 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); 999 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME)); 1000 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1001 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); 1002 1003 llvm::SmallString<128> MainFilePath(MainFile->getName()); 1004 1005 llvm::sys::fs::make_absolute(MainFilePath); 1006 1007 const char *MainFileNameStr = MainFilePath.c_str(); 1008 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, 1009 isysroot); 1010 RecordData Record; 1011 Record.push_back(ORIGINAL_FILE_NAME); 1012 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); 1013 1014 Record.clear(); 1015 Record.push_back(SM.getMainFileID().getOpaqueValue()); 1016 Stream.EmitRecord(ORIGINAL_FILE_ID, Record); 1017 } 1018 1019 // Original PCH directory 1020 if (!OutputFile.empty() && OutputFile != "-") { 1021 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1022 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR)); 1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1024 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 1025 1026 llvm::SmallString<128> OutputPath(OutputFile); 1027 1028 llvm::sys::fs::make_absolute(OutputPath); 1029 StringRef origDir = llvm::sys::path::parent_path(OutputPath); 1030 1031 RecordData Record; 1032 Record.push_back(ORIGINAL_PCH_DIR); 1033 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); 1034 } 1035 1036 // Repository branch/version information. 1037 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev(); 1038 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION)); 1039 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag 1040 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev); 1041 Record.clear(); 1042 Record.push_back(VERSION_CONTROL_BRANCH_REVISION); 1043 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record, 1044 getClangFullRepositoryVersion()); 1045} 1046 1047/// \brief Write the LangOptions structure. 1048void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) { 1049 RecordData Record; 1050#define LANGOPT(Name, Bits, Default, Description) \ 1051 Record.push_back(LangOpts.Name); 1052#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 1053 Record.push_back(static_cast<unsigned>(LangOpts.get##Name())); 1054#include "clang/Basic/LangOptions.def" 1055 Stream.EmitRecord(LANGUAGE_OPTIONS, Record); 1056} 1057 1058//===----------------------------------------------------------------------===// 1059// stat cache Serialization 1060//===----------------------------------------------------------------------===// 1061 1062namespace { 1063// Trait used for the on-disk hash table of stat cache results. 1064class ASTStatCacheTrait { 1065public: 1066 typedef const char * key_type; 1067 typedef key_type key_type_ref; 1068 1069 typedef struct stat data_type; 1070 typedef const data_type &data_type_ref; 1071 1072 static unsigned ComputeHash(const char *path) { 1073 return llvm::HashString(path); 1074 } 1075 1076 std::pair<unsigned,unsigned> 1077 EmitKeyDataLength(raw_ostream& Out, const char *path, 1078 data_type_ref Data) { 1079 unsigned StrLen = strlen(path); 1080 clang::io::Emit16(Out, StrLen); 1081 unsigned DataLen = 4 + 4 + 2 + 8 + 8; 1082 clang::io::Emit8(Out, DataLen); 1083 return std::make_pair(StrLen + 1, DataLen); 1084 } 1085 1086 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) { 1087 Out.write(path, KeyLen); 1088 } 1089 1090 void EmitData(raw_ostream &Out, key_type_ref, 1091 data_type_ref Data, unsigned DataLen) { 1092 using namespace clang::io; 1093 uint64_t Start = Out.tell(); (void)Start; 1094 1095 Emit32(Out, (uint32_t) Data.st_ino); 1096 Emit32(Out, (uint32_t) Data.st_dev); 1097 Emit16(Out, (uint16_t) Data.st_mode); 1098 Emit64(Out, (uint64_t) Data.st_mtime); 1099 Emit64(Out, (uint64_t) Data.st_size); 1100 1101 assert(Out.tell() - Start == DataLen && "Wrong data length"); 1102 } 1103}; 1104} // end anonymous namespace 1105 1106/// \brief Write the stat() system call cache to the AST file. 1107void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { 1108 // Build the on-disk hash table containing information about every 1109 // stat() call. 1110 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator; 1111 unsigned NumStatEntries = 0; 1112 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), 1113 StatEnd = StatCalls.end(); 1114 Stat != StatEnd; ++Stat, ++NumStatEntries) { 1115 StringRef Filename = Stat->first(); 1116 Generator.insert(Filename.data(), Stat->second); 1117 } 1118 1119 // Create the on-disk hash table in a buffer. 1120 llvm::SmallString<4096> StatCacheData; 1121 uint32_t BucketOffset; 1122 { 1123 llvm::raw_svector_ostream Out(StatCacheData); 1124 // Make sure that no bucket is at offset 0 1125 clang::io::Emit32(Out, 0); 1126 BucketOffset = Generator.Emit(Out); 1127 } 1128 1129 // Create a blob abbreviation 1130 using namespace llvm; 1131 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1132 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE)); 1133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1136 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); 1137 1138 // Write the stat cache 1139 RecordData Record; 1140 Record.push_back(STAT_CACHE); 1141 Record.push_back(BucketOffset); 1142 Record.push_back(NumStatEntries); 1143 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str()); 1144} 1145 1146//===----------------------------------------------------------------------===// 1147// Source Manager Serialization 1148//===----------------------------------------------------------------------===// 1149 1150/// \brief Create an abbreviation for the SLocEntry that refers to a 1151/// file. 1152static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { 1153 using namespace llvm; 1154 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1155 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); 1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1160 // FileEntry fields. 1161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size 1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time 1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs 1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1165 return Stream.EmitAbbrev(Abbrev); 1166} 1167 1168/// \brief Create an abbreviation for the SLocEntry that refers to a 1169/// buffer. 1170static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { 1171 using namespace llvm; 1172 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1173 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); 1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob 1179 return Stream.EmitAbbrev(Abbrev); 1180} 1181 1182/// \brief Create an abbreviation for the SLocEntry that refers to a 1183/// buffer's blob. 1184static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { 1185 using namespace llvm; 1186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1187 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); 1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob 1189 return Stream.EmitAbbrev(Abbrev); 1190} 1191 1192/// \brief Create an abbreviation for the SLocEntry that refers to a macro 1193/// expansion. 1194static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) { 1195 using namespace llvm; 1196 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1197 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY)); 1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location 1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location 1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location 1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length 1203 return Stream.EmitAbbrev(Abbrev); 1204} 1205 1206namespace { 1207 // Trait used for the on-disk hash table of header search information. 1208 class HeaderFileInfoTrait { 1209 ASTWriter &Writer; 1210 HeaderSearch &HS; 1211 1212 // Keep track of the framework names we've used during serialization. 1213 SmallVector<char, 128> FrameworkStringData; 1214 llvm::StringMap<unsigned> FrameworkNameOffset; 1215 1216 public: 1217 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS) 1218 : Writer(Writer), HS(HS) { } 1219 1220 typedef const char *key_type; 1221 typedef key_type key_type_ref; 1222 1223 typedef HeaderFileInfo data_type; 1224 typedef const data_type &data_type_ref; 1225 1226 static unsigned ComputeHash(const char *path) { 1227 // The hash is based only on the filename portion of the key, so that the 1228 // reader can match based on filenames when symlinking or excess path 1229 // elements ("foo/../", "../") change the form of the name. However, 1230 // complete path is still the key. 1231 return llvm::HashString(llvm::sys::path::filename(path)); 1232 } 1233 1234 std::pair<unsigned,unsigned> 1235 EmitKeyDataLength(raw_ostream& Out, const char *path, 1236 data_type_ref Data) { 1237 unsigned StrLen = strlen(path); 1238 clang::io::Emit16(Out, StrLen); 1239 unsigned DataLen = 1 + 2 + 4 + 4; 1240 clang::io::Emit8(Out, DataLen); 1241 return std::make_pair(StrLen + 1, DataLen); 1242 } 1243 1244 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) { 1245 Out.write(path, KeyLen); 1246 } 1247 1248 void EmitData(raw_ostream &Out, key_type_ref, 1249 data_type_ref Data, unsigned DataLen) { 1250 using namespace clang::io; 1251 uint64_t Start = Out.tell(); (void)Start; 1252 1253 unsigned char Flags = (Data.isImport << 5) 1254 | (Data.isPragmaOnce << 4) 1255 | (Data.DirInfo << 2) 1256 | (Data.Resolved << 1) 1257 | Data.IndexHeaderMapHeader; 1258 Emit8(Out, (uint8_t)Flags); 1259 Emit16(Out, (uint16_t) Data.NumIncludes); 1260 1261 if (!Data.ControllingMacro) 1262 Emit32(Out, (uint32_t)Data.ControllingMacroID); 1263 else 1264 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro)); 1265 1266 unsigned Offset = 0; 1267 if (!Data.Framework.empty()) { 1268 // If this header refers into a framework, save the framework name. 1269 llvm::StringMap<unsigned>::iterator Pos 1270 = FrameworkNameOffset.find(Data.Framework); 1271 if (Pos == FrameworkNameOffset.end()) { 1272 Offset = FrameworkStringData.size() + 1; 1273 FrameworkStringData.append(Data.Framework.begin(), 1274 Data.Framework.end()); 1275 FrameworkStringData.push_back(0); 1276 1277 FrameworkNameOffset[Data.Framework] = Offset; 1278 } else 1279 Offset = Pos->second; 1280 } 1281 Emit32(Out, Offset); 1282 1283 assert(Out.tell() - Start == DataLen && "Wrong data length"); 1284 } 1285 1286 const char *strings_begin() const { return FrameworkStringData.begin(); } 1287 const char *strings_end() const { return FrameworkStringData.end(); } 1288 }; 1289} // end anonymous namespace 1290 1291/// \brief Write the header search block for the list of files that 1292/// 1293/// \param HS The header search structure to save. 1294/// 1295/// \param Chain Whether we're creating a chained AST file. 1296void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) { 1297 SmallVector<const FileEntry *, 16> FilesByUID; 1298 HS.getFileMgr().GetUniqueIDMapping(FilesByUID); 1299 1300 if (FilesByUID.size() > HS.header_file_size()) 1301 FilesByUID.resize(HS.header_file_size()); 1302 1303 HeaderFileInfoTrait GeneratorTrait(*this, HS); 1304 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator; 1305 SmallVector<const char *, 4> SavedStrings; 1306 unsigned NumHeaderSearchEntries = 0; 1307 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { 1308 const FileEntry *File = FilesByUID[UID]; 1309 if (!File) 1310 continue; 1311 1312 const HeaderFileInfo &HFI = HS.header_file_begin()[UID]; 1313 if (HFI.External && Chain) 1314 continue; 1315 1316 // Turn the file name into an absolute path, if it isn't already. 1317 const char *Filename = File->getName(); 1318 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1319 1320 // If we performed any translation on the file name at all, we need to 1321 // save this string, since the generator will refer to it later. 1322 if (Filename != File->getName()) { 1323 Filename = strdup(Filename); 1324 SavedStrings.push_back(Filename); 1325 } 1326 1327 Generator.insert(Filename, HFI, GeneratorTrait); 1328 ++NumHeaderSearchEntries; 1329 } 1330 1331 // Create the on-disk hash table in a buffer. 1332 llvm::SmallString<4096> TableData; 1333 uint32_t BucketOffset; 1334 { 1335 llvm::raw_svector_ostream Out(TableData); 1336 // Make sure that no bucket is at offset 0 1337 clang::io::Emit32(Out, 0); 1338 BucketOffset = Generator.Emit(Out, GeneratorTrait); 1339 } 1340 1341 // Create a blob abbreviation 1342 using namespace llvm; 1343 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1344 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE)); 1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1349 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev); 1350 1351 // Write the header search table 1352 RecordData Record; 1353 Record.push_back(HEADER_SEARCH_TABLE); 1354 Record.push_back(BucketOffset); 1355 Record.push_back(NumHeaderSearchEntries); 1356 Record.push_back(TableData.size()); 1357 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end()); 1358 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str()); 1359 1360 // Free all of the strings we had to duplicate. 1361 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I) 1362 free((void*)SavedStrings[I]); 1363} 1364 1365/// \brief Writes the block containing the serialized form of the 1366/// source manager. 1367/// 1368/// TODO: We should probably use an on-disk hash table (stored in a 1369/// blob), indexed based on the file name, so that we only create 1370/// entries for files that we actually need. In the common case (no 1371/// errors), we probably won't have to create file entries for any of 1372/// the files in the AST. 1373void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, 1374 const Preprocessor &PP, 1375 StringRef isysroot) { 1376 RecordData Record; 1377 1378 // Enter the source manager block. 1379 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); 1380 1381 // Abbreviations for the various kinds of source-location entries. 1382 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); 1383 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); 1384 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); 1385 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream); 1386 1387 // Write out the source location entry table. We skip the first 1388 // entry, which is always the same dummy entry. 1389 std::vector<uint32_t> SLocEntryOffsets; 1390 // Write out the offsets of only source location file entries. 1391 // We will go through them in ASTReader::validateFileEntries(). 1392 std::vector<uint32_t> SLocFileEntryOffsets; 1393 RecordData PreloadSLocs; 1394 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1); 1395 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); 1396 I != N; ++I) { 1397 // Get this source location entry. 1398 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); 1399 1400 // Record the offset of this source-location entry. 1401 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1402 1403 // Figure out which record code to use. 1404 unsigned Code; 1405 if (SLoc->isFile()) { 1406 if (SLoc->getFile().getContentCache()->OrigEntry) { 1407 Code = SM_SLOC_FILE_ENTRY; 1408 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1409 } else 1410 Code = SM_SLOC_BUFFER_ENTRY; 1411 } else 1412 Code = SM_SLOC_EXPANSION_ENTRY; 1413 Record.clear(); 1414 Record.push_back(Code); 1415 1416 // Starting offset of this entry within this module, so skip the dummy. 1417 Record.push_back(SLoc->getOffset() - 2); 1418 if (SLoc->isFile()) { 1419 const SrcMgr::FileInfo &File = SLoc->getFile(); 1420 Record.push_back(File.getIncludeLoc().getRawEncoding()); 1421 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding 1422 Record.push_back(File.hasLineDirectives()); 1423 1424 const SrcMgr::ContentCache *Content = File.getContentCache(); 1425 if (Content->OrigEntry) { 1426 assert(Content->OrigEntry == Content->ContentsEntry && 1427 "Writing to AST an overriden file is not supported"); 1428 1429 // The source location entry is a file. The blob associated 1430 // with this entry is the file name. 1431 1432 // Emit size/modification time for this file. 1433 Record.push_back(Content->OrigEntry->getSize()); 1434 Record.push_back(Content->OrigEntry->getModificationTime()); 1435 1436 Record.push_back(File.NumCreatedFIDs); 1437 1438 // Turn the file name into an absolute path, if it isn't already. 1439 const char *Filename = Content->OrigEntry->getName(); 1440 llvm::SmallString<128> FilePath(Filename); 1441 1442 // Ask the file manager to fixup the relative path for us. This will 1443 // honor the working directory. 1444 SourceMgr.getFileManager().FixupRelativePath(FilePath); 1445 1446 // FIXME: This call to make_absolute shouldn't be necessary, the 1447 // call to FixupRelativePath should always return an absolute path. 1448 llvm::sys::fs::make_absolute(FilePath); 1449 Filename = FilePath.c_str(); 1450 1451 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1452 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename); 1453 } else { 1454 // The source location entry is a buffer. The blob associated 1455 // with this entry contains the contents of the buffer. 1456 1457 // We add one to the size so that we capture the trailing NULL 1458 // that is required by llvm::MemoryBuffer::getMemBuffer (on 1459 // the reader side). 1460 const llvm::MemoryBuffer *Buffer 1461 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1462 const char *Name = Buffer->getBufferIdentifier(); 1463 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, 1464 StringRef(Name, strlen(Name) + 1)); 1465 Record.clear(); 1466 Record.push_back(SM_SLOC_BUFFER_BLOB); 1467 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1468 StringRef(Buffer->getBufferStart(), 1469 Buffer->getBufferSize() + 1)); 1470 1471 if (strcmp(Name, "<built-in>") == 0) { 1472 PreloadSLocs.push_back(SLocEntryOffsets.size()); 1473 } 1474 } 1475 } else { 1476 // The source location entry is a macro expansion. 1477 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion(); 1478 Record.push_back(Expansion.getSpellingLoc().getRawEncoding()); 1479 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding()); 1480 Record.push_back(Expansion.isMacroArgExpansion() ? 0 1481 : Expansion.getExpansionLocEnd().getRawEncoding()); 1482 1483 // Compute the token length for this macro expansion. 1484 unsigned NextOffset = SourceMgr.getNextLocalOffset(); 1485 if (I + 1 != N) 1486 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset(); 1487 Record.push_back(NextOffset - SLoc->getOffset() - 1); 1488 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record); 1489 } 1490 } 1491 1492 Stream.ExitBlock(); 1493 1494 if (SLocEntryOffsets.empty()) 1495 return; 1496 1497 // Write the source-location offsets table into the AST block. This 1498 // table is used for lazily loading source-location information. 1499 using namespace llvm; 1500 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1501 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); 1502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size 1504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1505 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1506 1507 Record.clear(); 1508 Record.push_back(SOURCE_LOCATION_OFFSETS); 1509 Record.push_back(SLocEntryOffsets.size()); 1510 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy 1511 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets)); 1512 1513 Abbrev = new BitCodeAbbrev(); 1514 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS)); 1515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1516 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1517 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1518 1519 Record.clear(); 1520 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS); 1521 Record.push_back(SLocFileEntryOffsets.size()); 1522 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record, 1523 data(SLocFileEntryOffsets)); 1524 1525 // Write the source location entry preloads array, telling the AST 1526 // reader which source locations entries it should load eagerly. 1527 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); 1528 1529 // Write the line table. It depends on remapping working, so it must come 1530 // after the source location offsets. 1531 if (SourceMgr.hasLineTable()) { 1532 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1533 1534 Record.clear(); 1535 // Emit the file names 1536 Record.push_back(LineTable.getNumFilenames()); 1537 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { 1538 // Emit the file name 1539 const char *Filename = LineTable.getFilename(I); 1540 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); 1541 unsigned FilenameLen = Filename? strlen(Filename) : 0; 1542 Record.push_back(FilenameLen); 1543 if (FilenameLen) 1544 Record.insert(Record.end(), Filename, Filename + FilenameLen); 1545 } 1546 1547 // Emit the line entries 1548 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); 1549 L != LEnd; ++L) { 1550 // Only emit entries for local files. 1551 if (L->first < 0) 1552 continue; 1553 1554 // Emit the file ID 1555 Record.push_back(L->first); 1556 1557 // Emit the line entries 1558 Record.push_back(L->second.size()); 1559 for (std::vector<LineEntry>::iterator LE = L->second.begin(), 1560 LEEnd = L->second.end(); 1561 LE != LEEnd; ++LE) { 1562 Record.push_back(LE->FileOffset); 1563 Record.push_back(LE->LineNo); 1564 Record.push_back(LE->FilenameID); 1565 Record.push_back((unsigned)LE->FileKind); 1566 Record.push_back(LE->IncludeOffset); 1567 } 1568 } 1569 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record); 1570 } 1571} 1572 1573//===----------------------------------------------------------------------===// 1574// Preprocessor Serialization 1575//===----------------------------------------------------------------------===// 1576 1577static int compareMacroDefinitions(const void *XPtr, const void *YPtr) { 1578 const std::pair<const IdentifierInfo *, MacroInfo *> &X = 1579 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr; 1580 const std::pair<const IdentifierInfo *, MacroInfo *> &Y = 1581 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr; 1582 return X.first->getName().compare(Y.first->getName()); 1583} 1584 1585/// \brief Writes the block containing the serialized form of the 1586/// preprocessor. 1587/// 1588void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { 1589 RecordData Record; 1590 1591 // If the preprocessor __COUNTER__ value has been bumped, remember it. 1592 if (PP.getCounterValue() != 0) { 1593 Record.push_back(PP.getCounterValue()); 1594 Stream.EmitRecord(PP_COUNTER_VALUE, Record); 1595 Record.clear(); 1596 } 1597 1598 // Enter the preprocessor block. 1599 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); 1600 1601 // If the AST file contains __DATE__ or __TIME__ emit a warning about this. 1602 // FIXME: use diagnostics subsystem for localization etc. 1603 if (PP.SawDateOrTime()) 1604 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); 1605 1606 1607 // Loop over all the macro definitions that are live at the end of the file, 1608 // emitting each to the PP section. 1609 PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); 1610 1611 // Construct the list of macro definitions that need to be serialized. 1612 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2> 1613 MacrosToEmit; 1614 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen; 1615 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0), 1616 E = PP.macro_end(Chain == 0); 1617 I != E; ++I) { 1618 if (!IsModule || I->second->isExported()) { 1619 MacroDefinitionsSeen.insert(I->first); 1620 MacrosToEmit.push_back(std::make_pair(I->first, I->second)); 1621 } 1622 } 1623 1624 // Sort the set of macro definitions that need to be serialized by the 1625 // name of the macro, to provide a stable ordering. 1626 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(), 1627 &compareMacroDefinitions); 1628 1629 // Resolve any identifiers that defined macros at the time they were 1630 // deserialized, adding them to the list of macros to emit (if appropriate). 1631 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) { 1632 IdentifierInfo *Name 1633 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]); 1634 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name)) 1635 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name))); 1636 } 1637 1638 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) { 1639 const IdentifierInfo *Name = MacrosToEmit[I].first; 1640 MacroInfo *MI = MacrosToEmit[I].second; 1641 if (!MI) 1642 continue; 1643 1644 // Don't emit builtin macros like __LINE__ to the AST file unless they have 1645 // been redefined by the header (in which case they are not isBuiltinMacro). 1646 // Also skip macros from a AST file if we're chaining. 1647 1648 // FIXME: There is a (probably minor) optimization we could do here, if 1649 // the macro comes from the original PCH but the identifier comes from a 1650 // chained PCH, by storing the offset into the original PCH rather than 1651 // writing the macro definition a second time. 1652 if (MI->isBuiltinMacro() || 1653 (Chain && Name->isFromAST() && MI->isFromAST() && 1654 !MI->hasChangedAfterLoad())) 1655 continue; 1656 1657 AddIdentifierRef(Name, Record); 1658 MacroOffsets[Name] = Stream.GetCurrentBitNo(); 1659 Record.push_back(MI->getDefinitionLoc().getRawEncoding()); 1660 Record.push_back(MI->isUsed()); 1661 AddSourceLocation(MI->getExportLocation(), Record); 1662 unsigned Code; 1663 if (MI->isObjectLike()) { 1664 Code = PP_MACRO_OBJECT_LIKE; 1665 } else { 1666 Code = PP_MACRO_FUNCTION_LIKE; 1667 1668 Record.push_back(MI->isC99Varargs()); 1669 Record.push_back(MI->isGNUVarargs()); 1670 Record.push_back(MI->getNumArgs()); 1671 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 1672 I != E; ++I) 1673 AddIdentifierRef(*I, Record); 1674 } 1675 1676 // If we have a detailed preprocessing record, record the macro definition 1677 // ID that corresponds to this macro. 1678 if (PPRec) 1679 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI))); 1680 1681 Stream.EmitRecord(Code, Record); 1682 Record.clear(); 1683 1684 // Emit the tokens array. 1685 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { 1686 // Note that we know that the preprocessor does not have any annotation 1687 // tokens in it because they are created by the parser, and thus can't be 1688 // in a macro definition. 1689 const Token &Tok = MI->getReplacementToken(TokNo); 1690 1691 Record.push_back(Tok.getLocation().getRawEncoding()); 1692 Record.push_back(Tok.getLength()); 1693 1694 // FIXME: When reading literal tokens, reconstruct the literal pointer if 1695 // it is needed. 1696 AddIdentifierRef(Tok.getIdentifierInfo(), Record); 1697 // FIXME: Should translate token kind to a stable encoding. 1698 Record.push_back(Tok.getKind()); 1699 // FIXME: Should translate token flags to a stable encoding. 1700 Record.push_back(Tok.getFlags()); 1701 1702 Stream.EmitRecord(PP_TOKEN, Record); 1703 Record.clear(); 1704 } 1705 ++NumMacros; 1706 } 1707 Stream.ExitBlock(); 1708 1709 if (PPRec) 1710 WritePreprocessorDetail(*PPRec); 1711} 1712 1713void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) { 1714 if (PPRec.begin(Chain) == PPRec.end(Chain)) 1715 return; 1716 1717 // Enter the preprocessor block. 1718 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3); 1719 1720 // If the preprocessor has a preprocessing record, emit it. 1721 unsigned NumPreprocessingRecords = 0; 1722 using namespace llvm; 1723 1724 // Set up the abbreviation for 1725 unsigned InclusionAbbrev = 0; 1726 { 1727 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1728 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE)); 1729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index 1730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location 1731 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location 1732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length 1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes 1734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind 1735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1736 InclusionAbbrev = Stream.EmitAbbrev(Abbrev); 1737 } 1738 1739 unsigned FirstPreprocessorEntityID 1740 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0) 1741 + NUM_PREDEF_PP_ENTITY_IDS; 1742 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID; 1743 RecordData Record; 1744 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0; 1745 for (PreprocessingRecord::iterator E = PPRec.begin(Chain), 1746 EEnd = PPRec.end(Chain); 1747 E != EEnd; 1748 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) { 1749 Record.clear(); 1750 1751 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { 1752 // Record this macro definition's location. 1753 MacroID ID = getMacroDefinitionID(MD); 1754 1755 // Don't write the macro definition if it is from another AST file. 1756 if (ID < FirstMacroID) 1757 continue; 1758 1759 // Notify the serialization listener that we're serializing this entity. 1760 if (SerializationListener) 1761 SerializationListener->SerializedPreprocessedEntity(*E, 1762 BitsInChain + Stream.GetCurrentBitNo()); 1763 1764 unsigned Position = ID - FirstMacroID; 1765 if (Position != MacroDefinitionOffsets.size()) { 1766 if (Position > MacroDefinitionOffsets.size()) 1767 MacroDefinitionOffsets.resize(Position + 1); 1768 1769 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo(); 1770 } else 1771 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo()); 1772 1773 Record.push_back(NextPreprocessorEntityID); 1774 Record.push_back(ID); 1775 AddSourceLocation(MD->getSourceRange().getBegin(), Record); 1776 AddSourceLocation(MD->getSourceRange().getEnd(), Record); 1777 AddIdentifierRef(MD->getName(), Record); 1778 AddSourceLocation(MD->getLocation(), Record); 1779 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record); 1780 continue; 1781 } 1782 1783 // Notify the serialization listener that we're serializing this entity. 1784 if (SerializationListener) 1785 SerializationListener->SerializedPreprocessedEntity(*E, 1786 BitsInChain + Stream.GetCurrentBitNo()); 1787 1788 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) { 1789 Record.push_back(NextPreprocessorEntityID); 1790 AddSourceLocation(ME->getSourceRange().getBegin(), Record); 1791 AddSourceLocation(ME->getSourceRange().getEnd(), Record); 1792 Record.push_back(ME->isBuiltinMacro()); 1793 if (ME->isBuiltinMacro()) 1794 AddIdentifierRef(ME->getName(), Record); 1795 else 1796 Record.push_back(getMacroDefinitionID(ME->getDefinition())); 1797 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record); 1798 continue; 1799 } 1800 1801 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { 1802 Record.push_back(PPD_INCLUSION_DIRECTIVE); 1803 Record.push_back(NextPreprocessorEntityID); 1804 AddSourceLocation(ID->getSourceRange().getBegin(), Record); 1805 AddSourceLocation(ID->getSourceRange().getEnd(), Record); 1806 Record.push_back(ID->getFileName().size()); 1807 Record.push_back(ID->wasInQuotes()); 1808 Record.push_back(static_cast<unsigned>(ID->getKind())); 1809 llvm::SmallString<64> Buffer; 1810 Buffer += ID->getFileName(); 1811 Buffer += ID->getFile()->getName(); 1812 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); 1813 continue; 1814 } 1815 1816 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter"); 1817 } 1818 Stream.ExitBlock(); 1819 1820 // Write the offsets table for the preprocessing record. 1821 if (NumPreprocessingRecords > 0) { 1822 // Write the offsets table for identifier IDs. 1823 using namespace llvm; 1824 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1825 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS)); 1826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records 1827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity 1828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs 1829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def 1830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1831 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1832 1833 Record.clear(); 1834 Record.push_back(MACRO_DEFINITION_OFFSETS); 1835 Record.push_back(NumPreprocessingRecords); 1836 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS); 1837 Record.push_back(MacroDefinitionOffsets.size()); 1838 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS); 1839 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record, 1840 data(MacroDefinitionOffsets)); 1841 } 1842} 1843 1844void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) { 1845 RecordData Record; 1846 for (Diagnostic::DiagStatePointsTy::const_iterator 1847 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end(); 1848 I != E; ++I) { 1849 const Diagnostic::DiagStatePoint &point = *I; 1850 if (point.Loc.isInvalid()) 1851 continue; 1852 1853 Record.push_back(point.Loc.getRawEncoding()); 1854 for (Diagnostic::DiagState::iterator 1855 I = point.State->begin(), E = point.State->end(); I != E; ++I) { 1856 unsigned diag = I->first, map = I->second; 1857 if (map & 0x10) { // mapping from a diagnostic pragma. 1858 Record.push_back(diag); 1859 Record.push_back(map & 0x7); 1860 } 1861 } 1862 Record.push_back(-1); // mark the end of the diag/map pairs for this 1863 // location. 1864 } 1865 1866 if (!Record.empty()) 1867 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record); 1868} 1869 1870void ASTWriter::WriteCXXBaseSpecifiersOffsets() { 1871 if (CXXBaseSpecifiersOffsets.empty()) 1872 return; 1873 1874 RecordData Record; 1875 1876 // Create a blob abbreviation for the C++ base specifiers offsets. 1877 using namespace llvm; 1878 1879 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1880 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); 1881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 1882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1883 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1884 1885 // Write the base specifier offsets table. 1886 Record.clear(); 1887 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS); 1888 Record.push_back(CXXBaseSpecifiersOffsets.size()); 1889 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, 1890 data(CXXBaseSpecifiersOffsets)); 1891} 1892 1893//===----------------------------------------------------------------------===// 1894// Type Serialization 1895//===----------------------------------------------------------------------===// 1896 1897/// \brief Write the representation of a type to the AST stream. 1898void ASTWriter::WriteType(QualType T) { 1899 TypeIdx &Idx = TypeIdxs[T]; 1900 if (Idx.getIndex() == 0) // we haven't seen this type before. 1901 Idx = TypeIdx(NextTypeID++); 1902 1903 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); 1904 1905 // Record the offset for this type. 1906 unsigned Index = Idx.getIndex() - FirstTypeID; 1907 if (TypeOffsets.size() == Index) 1908 TypeOffsets.push_back(Stream.GetCurrentBitNo()); 1909 else if (TypeOffsets.size() < Index) { 1910 TypeOffsets.resize(Index + 1); 1911 TypeOffsets[Index] = Stream.GetCurrentBitNo(); 1912 } 1913 1914 RecordData Record; 1915 1916 // Emit the type's representation. 1917 ASTTypeWriter W(*this, Record); 1918 1919 if (T.hasLocalNonFastQualifiers()) { 1920 Qualifiers Qs = T.getLocalQualifiers(); 1921 AddTypeRef(T.getLocalUnqualifiedType(), Record); 1922 Record.push_back(Qs.getAsOpaqueValue()); 1923 W.Code = TYPE_EXT_QUAL; 1924 } else { 1925 switch (T->getTypeClass()) { 1926 // For all of the concrete, non-dependent types, call the 1927 // appropriate visitor function. 1928#define TYPE(Class, Base) \ 1929 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; 1930#define ABSTRACT_TYPE(Class, Base) 1931#include "clang/AST/TypeNodes.def" 1932 } 1933 } 1934 1935 // Emit the serialized record. 1936 Stream.EmitRecord(W.Code, Record); 1937 1938 // Flush any expressions that were written as part of this type. 1939 FlushStmts(); 1940} 1941 1942//===----------------------------------------------------------------------===// 1943// Declaration Serialization 1944//===----------------------------------------------------------------------===// 1945 1946/// \brief Write the block containing all of the declaration IDs 1947/// lexically declared within the given DeclContext. 1948/// 1949/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the 1950/// bistream, or 0 if no block was written. 1951uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, 1952 DeclContext *DC) { 1953 if (DC->decls_empty()) 1954 return 0; 1955 1956 uint64_t Offset = Stream.GetCurrentBitNo(); 1957 RecordData Record; 1958 Record.push_back(DECL_CONTEXT_LEXICAL); 1959 SmallVector<KindDeclIDPair, 64> Decls; 1960 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 1961 D != DEnd; ++D) 1962 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D))); 1963 1964 ++NumLexicalDeclContexts; 1965 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls)); 1966 return Offset; 1967} 1968 1969void ASTWriter::WriteTypeDeclOffsets() { 1970 using namespace llvm; 1971 RecordData Record; 1972 1973 // Write the type offsets array 1974 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1975 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); 1976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types 1977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index 1978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block 1979 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1980 Record.clear(); 1981 Record.push_back(TYPE_OFFSET); 1982 Record.push_back(TypeOffsets.size()); 1983 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS); 1984 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets)); 1985 1986 // Write the declaration offsets array 1987 Abbrev = new BitCodeAbbrev(); 1988 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); 1989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations 1990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID 1991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block 1992 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 1993 Record.clear(); 1994 Record.push_back(DECL_OFFSET); 1995 Record.push_back(DeclOffsets.size()); 1996 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS); 1997 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets)); 1998} 1999 2000//===----------------------------------------------------------------------===// 2001// Global Method Pool and Selector Serialization 2002//===----------------------------------------------------------------------===// 2003 2004namespace { 2005// Trait used for the on-disk hash table used in the method pool. 2006class ASTMethodPoolTrait { 2007 ASTWriter &Writer; 2008 2009public: 2010 typedef Selector key_type; 2011 typedef key_type key_type_ref; 2012 2013 struct data_type { 2014 SelectorID ID; 2015 ObjCMethodList Instance, Factory; 2016 }; 2017 typedef const data_type& data_type_ref; 2018 2019 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } 2020 2021 static unsigned ComputeHash(Selector Sel) { 2022 return serialization::ComputeHash(Sel); 2023 } 2024 2025 std::pair<unsigned,unsigned> 2026 EmitKeyDataLength(raw_ostream& Out, Selector Sel, 2027 data_type_ref Methods) { 2028 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); 2029 clang::io::Emit16(Out, KeyLen); 2030 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts 2031 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2032 Method = Method->Next) 2033 if (Method->Method) 2034 DataLen += 4; 2035 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2036 Method = Method->Next) 2037 if (Method->Method) 2038 DataLen += 4; 2039 clang::io::Emit16(Out, DataLen); 2040 return std::make_pair(KeyLen, DataLen); 2041 } 2042 2043 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) { 2044 uint64_t Start = Out.tell(); 2045 assert((Start >> 32) == 0 && "Selector key offset too large"); 2046 Writer.SetSelectorOffset(Sel, Start); 2047 unsigned N = Sel.getNumArgs(); 2048 clang::io::Emit16(Out, N); 2049 if (N == 0) 2050 N = 1; 2051 for (unsigned I = 0; I != N; ++I) 2052 clang::io::Emit32(Out, 2053 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); 2054 } 2055 2056 void EmitData(raw_ostream& Out, key_type_ref, 2057 data_type_ref Methods, unsigned DataLen) { 2058 uint64_t Start = Out.tell(); (void)Start; 2059 clang::io::Emit32(Out, Methods.ID); 2060 unsigned NumInstanceMethods = 0; 2061 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2062 Method = Method->Next) 2063 if (Method->Method) 2064 ++NumInstanceMethods; 2065 2066 unsigned NumFactoryMethods = 0; 2067 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2068 Method = Method->Next) 2069 if (Method->Method) 2070 ++NumFactoryMethods; 2071 2072 clang::io::Emit16(Out, NumInstanceMethods); 2073 clang::io::Emit16(Out, NumFactoryMethods); 2074 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2075 Method = Method->Next) 2076 if (Method->Method) 2077 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 2078 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2079 Method = Method->Next) 2080 if (Method->Method) 2081 clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); 2082 2083 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2084 } 2085}; 2086} // end anonymous namespace 2087 2088/// \brief Write ObjC data: selectors and the method pool. 2089/// 2090/// The method pool contains both instance and factory methods, stored 2091/// in an on-disk hash table indexed by the selector. The hash table also 2092/// contains an empty entry for every other selector known to Sema. 2093void ASTWriter::WriteSelectors(Sema &SemaRef) { 2094 using namespace llvm; 2095 2096 // Do we have to do anything at all? 2097 if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) 2098 return; 2099 unsigned NumTableEntries = 0; 2100 // Create and write out the blob that contains selectors and the method pool. 2101 { 2102 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; 2103 ASTMethodPoolTrait Trait(*this); 2104 2105 // Create the on-disk hash table representation. We walk through every 2106 // selector we've seen and look it up in the method pool. 2107 SelectorOffsets.resize(NextSelectorID - FirstSelectorID); 2108 for (llvm::DenseMap<Selector, SelectorID>::iterator 2109 I = SelectorIDs.begin(), E = SelectorIDs.end(); 2110 I != E; ++I) { 2111 Selector S = I->first; 2112 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); 2113 ASTMethodPoolTrait::data_type Data = { 2114 I->second, 2115 ObjCMethodList(), 2116 ObjCMethodList() 2117 }; 2118 if (F != SemaRef.MethodPool.end()) { 2119 Data.Instance = F->second.first; 2120 Data.Factory = F->second.second; 2121 } 2122 // Only write this selector if it's not in an existing AST or something 2123 // changed. 2124 if (Chain && I->second < FirstSelectorID) { 2125 // Selector already exists. Did it change? 2126 bool changed = false; 2127 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method; 2128 M = M->Next) { 2129 if (!M->Method->isFromASTFile()) 2130 changed = true; 2131 } 2132 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method; 2133 M = M->Next) { 2134 if (!M->Method->isFromASTFile()) 2135 changed = true; 2136 } 2137 if (!changed) 2138 continue; 2139 } else if (Data.Instance.Method || Data.Factory.Method) { 2140 // A new method pool entry. 2141 ++NumTableEntries; 2142 } 2143 Generator.insert(S, Data, Trait); 2144 } 2145 2146 // Create the on-disk hash table in a buffer. 2147 llvm::SmallString<4096> MethodPool; 2148 uint32_t BucketOffset; 2149 { 2150 ASTMethodPoolTrait Trait(*this); 2151 llvm::raw_svector_ostream Out(MethodPool); 2152 // Make sure that no bucket is at offset 0 2153 clang::io::Emit32(Out, 0); 2154 BucketOffset = Generator.Emit(Out, Trait); 2155 } 2156 2157 // Create a blob abbreviation 2158 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2159 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); 2160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2163 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); 2164 2165 // Write the method pool 2166 RecordData Record; 2167 Record.push_back(METHOD_POOL); 2168 Record.push_back(BucketOffset); 2169 Record.push_back(NumTableEntries); 2170 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str()); 2171 2172 // Create a blob abbreviation for the selector table offsets. 2173 Abbrev = new BitCodeAbbrev(); 2174 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); 2175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 2176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 2177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2178 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2179 2180 // Write the selector offsets table. 2181 Record.clear(); 2182 Record.push_back(SELECTOR_OFFSETS); 2183 Record.push_back(SelectorOffsets.size()); 2184 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS); 2185 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, 2186 data(SelectorOffsets)); 2187 } 2188} 2189 2190/// \brief Write the selectors referenced in @selector expression into AST file. 2191void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { 2192 using namespace llvm; 2193 if (SemaRef.ReferencedSelectors.empty()) 2194 return; 2195 2196 RecordData Record; 2197 2198 // Note: this writes out all references even for a dependent AST. But it is 2199 // very tricky to fix, and given that @selector shouldn't really appear in 2200 // headers, probably not worth it. It's not a correctness issue. 2201 for (DenseMap<Selector, SourceLocation>::iterator S = 2202 SemaRef.ReferencedSelectors.begin(), 2203 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) { 2204 Selector Sel = (*S).first; 2205 SourceLocation Loc = (*S).second; 2206 AddSelectorRef(Sel, Record); 2207 AddSourceLocation(Loc, Record); 2208 } 2209 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); 2210} 2211 2212//===----------------------------------------------------------------------===// 2213// Identifier Table Serialization 2214//===----------------------------------------------------------------------===// 2215 2216namespace { 2217class ASTIdentifierTableTrait { 2218 ASTWriter &Writer; 2219 Preprocessor &PP; 2220 bool IsModule; 2221 2222 /// \brief Determines whether this is an "interesting" identifier 2223 /// that needs a full IdentifierInfo structure written into the hash 2224 /// table. 2225 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) { 2226 if (II->isPoisoned() || 2227 II->isExtensionToken() || 2228 II->getObjCOrBuiltinID() || 2229 II->getFETokenInfo<void>()) 2230 return true; 2231 2232 return hasMacroDefinition(II, Macro); 2233 } 2234 2235 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) { 2236 if (!II->hasMacroDefinition()) 2237 return false; 2238 2239 if (Macro || (Macro = PP.getMacroInfo(II))) 2240 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isExported()); 2241 2242 return false; 2243 } 2244 2245public: 2246 typedef IdentifierInfo* key_type; 2247 typedef key_type key_type_ref; 2248 2249 typedef IdentID data_type; 2250 typedef data_type data_type_ref; 2251 2252 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, bool IsModule) 2253 : Writer(Writer), PP(PP), IsModule(IsModule) { } 2254 2255 static unsigned ComputeHash(const IdentifierInfo* II) { 2256 return llvm::HashString(II->getName()); 2257 } 2258 2259 std::pair<unsigned,unsigned> 2260 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) { 2261 unsigned KeyLen = II->getLength() + 1; 2262 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 2263 MacroInfo *Macro = 0; 2264 if (isInterestingIdentifier(II, Macro)) { 2265 DataLen += 2; // 2 bytes for builtin ID, flags 2266 if (hasMacroDefinition(II, Macro)) 2267 DataLen += 4; 2268 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II), 2269 DEnd = IdentifierResolver::end(); 2270 D != DEnd; ++D) 2271 DataLen += sizeof(DeclID); 2272 } 2273 clang::io::Emit16(Out, DataLen); 2274 // We emit the key length after the data length so that every 2275 // string is preceded by a 16-bit length. This matches the PTH 2276 // format for storing identifiers. 2277 clang::io::Emit16(Out, KeyLen); 2278 return std::make_pair(KeyLen, DataLen); 2279 } 2280 2281 void EmitKey(raw_ostream& Out, const IdentifierInfo* II, 2282 unsigned KeyLen) { 2283 // Record the location of the key data. This is used when generating 2284 // the mapping from persistent IDs to strings. 2285 Writer.SetIdentifierOffset(II, Out.tell()); 2286 Out.write(II->getNameStart(), KeyLen); 2287 } 2288 2289 void EmitData(raw_ostream& Out, IdentifierInfo* II, 2290 IdentID ID, unsigned) { 2291 MacroInfo *Macro = 0; 2292 if (!isInterestingIdentifier(II, Macro)) { 2293 clang::io::Emit32(Out, ID << 1); 2294 return; 2295 } 2296 2297 clang::io::Emit32(Out, (ID << 1) | 0x01); 2298 uint32_t Bits = 0; 2299 bool HasMacroDefinition = hasMacroDefinition(II, Macro); 2300 Bits = (uint32_t)II->getObjCOrBuiltinID(); 2301 Bits = (Bits << 1) | unsigned(HasMacroDefinition); 2302 Bits = (Bits << 1) | unsigned(II->isExtensionToken()); 2303 Bits = (Bits << 1) | unsigned(II->isPoisoned()); 2304 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); 2305 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); 2306 clang::io::Emit16(Out, Bits); 2307 2308 if (HasMacroDefinition) 2309 clang::io::Emit32(Out, Writer.getMacroOffset(II)); 2310 2311 // Emit the declaration IDs in reverse order, because the 2312 // IdentifierResolver provides the declarations as they would be 2313 // visible (e.g., the function "stat" would come before the struct 2314 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain() 2315 // adds declarations to the end of the list (so we need to see the 2316 // struct "status" before the function "status"). 2317 // Only emit declarations that aren't from a chained PCH, though. 2318 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II), 2319 IdentifierResolver::end()); 2320 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), 2321 DEnd = Decls.rend(); 2322 D != DEnd; ++D) 2323 clang::io::Emit32(Out, Writer.getDeclID(*D)); 2324 } 2325}; 2326} // end anonymous namespace 2327 2328/// \brief Write the identifier table into the AST file. 2329/// 2330/// The identifier table consists of a blob containing string data 2331/// (the actual identifiers themselves) and a separate "offsets" index 2332/// that maps identifier IDs to locations within the blob. 2333void ASTWriter::WriteIdentifierTable(Preprocessor &PP, bool IsModule) { 2334 using namespace llvm; 2335 2336 // Create and write out the blob that contains the identifier 2337 // strings. 2338 { 2339 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; 2340 ASTIdentifierTableTrait Trait(*this, PP, IsModule); 2341 2342 // Look for any identifiers that were named while processing the 2343 // headers, but are otherwise not needed. We add these to the hash 2344 // table to enable checking of the predefines buffer in the case 2345 // where the user adds new macro definitions when building the AST 2346 // file. 2347 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 2348 IDEnd = PP.getIdentifierTable().end(); 2349 ID != IDEnd; ++ID) 2350 getIdentifierRef(ID->second); 2351 2352 // Create the on-disk hash table representation. We only store offsets 2353 // for identifiers that appear here for the first time. 2354 IdentifierOffsets.resize(NextIdentID - FirstIdentID); 2355 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator 2356 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); 2357 ID != IDEnd; ++ID) { 2358 assert(ID->first && "NULL identifier in identifier table"); 2359 if (!Chain || !ID->first->isFromAST()) 2360 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second, 2361 Trait); 2362 } 2363 2364 // Create the on-disk hash table in a buffer. 2365 llvm::SmallString<4096> IdentifierTable; 2366 uint32_t BucketOffset; 2367 { 2368 ASTIdentifierTableTrait Trait(*this, PP, IsModule); 2369 llvm::raw_svector_ostream Out(IdentifierTable); 2370 // Make sure that no bucket is at offset 0 2371 clang::io::Emit32(Out, 0); 2372 BucketOffset = Generator.Emit(Out, Trait); 2373 } 2374 2375 // Create a blob abbreviation 2376 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2377 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); 2378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2380 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); 2381 2382 // Write the identifier table 2383 RecordData Record; 2384 Record.push_back(IDENTIFIER_TABLE); 2385 Record.push_back(BucketOffset); 2386 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str()); 2387 } 2388 2389 // Write the offsets table for identifier IDs. 2390 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2391 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); 2392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers 2393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2395 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2396 2397 RecordData Record; 2398 Record.push_back(IDENTIFIER_OFFSET); 2399 Record.push_back(IdentifierOffsets.size()); 2400 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS); 2401 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, 2402 data(IdentifierOffsets)); 2403} 2404 2405//===----------------------------------------------------------------------===// 2406// DeclContext's Name Lookup Table Serialization 2407//===----------------------------------------------------------------------===// 2408 2409namespace { 2410// Trait used for the on-disk hash table used in the method pool. 2411class ASTDeclContextNameLookupTrait { 2412 ASTWriter &Writer; 2413 2414public: 2415 typedef DeclarationName key_type; 2416 typedef key_type key_type_ref; 2417 2418 typedef DeclContext::lookup_result data_type; 2419 typedef const data_type& data_type_ref; 2420 2421 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } 2422 2423 unsigned ComputeHash(DeclarationName Name) { 2424 llvm::FoldingSetNodeID ID; 2425 ID.AddInteger(Name.getNameKind()); 2426 2427 switch (Name.getNameKind()) { 2428 case DeclarationName::Identifier: 2429 ID.AddString(Name.getAsIdentifierInfo()->getName()); 2430 break; 2431 case DeclarationName::ObjCZeroArgSelector: 2432 case DeclarationName::ObjCOneArgSelector: 2433 case DeclarationName::ObjCMultiArgSelector: 2434 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); 2435 break; 2436 case DeclarationName::CXXConstructorName: 2437 case DeclarationName::CXXDestructorName: 2438 case DeclarationName::CXXConversionFunctionName: 2439 break; 2440 case DeclarationName::CXXOperatorName: 2441 ID.AddInteger(Name.getCXXOverloadedOperator()); 2442 break; 2443 case DeclarationName::CXXLiteralOperatorName: 2444 ID.AddString(Name.getCXXLiteralIdentifier()->getName()); 2445 case DeclarationName::CXXUsingDirective: 2446 break; 2447 } 2448 2449 return ID.ComputeHash(); 2450 } 2451 2452 std::pair<unsigned,unsigned> 2453 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name, 2454 data_type_ref Lookup) { 2455 unsigned KeyLen = 1; 2456 switch (Name.getNameKind()) { 2457 case DeclarationName::Identifier: 2458 case DeclarationName::ObjCZeroArgSelector: 2459 case DeclarationName::ObjCOneArgSelector: 2460 case DeclarationName::ObjCMultiArgSelector: 2461 case DeclarationName::CXXLiteralOperatorName: 2462 KeyLen += 4; 2463 break; 2464 case DeclarationName::CXXOperatorName: 2465 KeyLen += 1; 2466 break; 2467 case DeclarationName::CXXConstructorName: 2468 case DeclarationName::CXXDestructorName: 2469 case DeclarationName::CXXConversionFunctionName: 2470 case DeclarationName::CXXUsingDirective: 2471 break; 2472 } 2473 clang::io::Emit16(Out, KeyLen); 2474 2475 // 2 bytes for num of decls and 4 for each DeclID. 2476 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first); 2477 clang::io::Emit16(Out, DataLen); 2478 2479 return std::make_pair(KeyLen, DataLen); 2480 } 2481 2482 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) { 2483 using namespace clang::io; 2484 2485 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?"); 2486 Emit8(Out, Name.getNameKind()); 2487 switch (Name.getNameKind()) { 2488 case DeclarationName::Identifier: 2489 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo())); 2490 break; 2491 case DeclarationName::ObjCZeroArgSelector: 2492 case DeclarationName::ObjCOneArgSelector: 2493 case DeclarationName::ObjCMultiArgSelector: 2494 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector())); 2495 break; 2496 case DeclarationName::CXXOperatorName: 2497 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?"); 2498 Emit8(Out, Name.getCXXOverloadedOperator()); 2499 break; 2500 case DeclarationName::CXXLiteralOperatorName: 2501 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); 2502 break; 2503 case DeclarationName::CXXConstructorName: 2504 case DeclarationName::CXXDestructorName: 2505 case DeclarationName::CXXConversionFunctionName: 2506 case DeclarationName::CXXUsingDirective: 2507 break; 2508 } 2509 } 2510 2511 void EmitData(raw_ostream& Out, key_type_ref, 2512 data_type Lookup, unsigned DataLen) { 2513 uint64_t Start = Out.tell(); (void)Start; 2514 clang::io::Emit16(Out, Lookup.second - Lookup.first); 2515 for (; Lookup.first != Lookup.second; ++Lookup.first) 2516 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first)); 2517 2518 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2519 } 2520}; 2521} // end anonymous namespace 2522 2523/// \brief Write the block containing all of the declaration IDs 2524/// visible from the given DeclContext. 2525/// 2526/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the 2527/// bitstream, or 0 if no block was written. 2528uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, 2529 DeclContext *DC) { 2530 if (DC->getPrimaryContext() != DC) 2531 return 0; 2532 2533 // Since there is no name lookup into functions or methods, don't bother to 2534 // build a visible-declarations table for these entities. 2535 if (DC->isFunctionOrMethod()) 2536 return 0; 2537 2538 // If not in C++, we perform name lookup for the translation unit via the 2539 // IdentifierInfo chains, don't bother to build a visible-declarations table. 2540 // FIXME: In C++ we need the visible declarations in order to "see" the 2541 // friend declarations, is there a way to do this without writing the table ? 2542 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus) 2543 return 0; 2544 2545 // Force the DeclContext to build a its name-lookup table. 2546 if (!DC->hasExternalVisibleStorage()) 2547 DC->lookup(DeclarationName()); 2548 2549 // Serialize the contents of the mapping used for lookup. Note that, 2550 // although we have two very different code paths, the serialized 2551 // representation is the same for both cases: a declaration name, 2552 // followed by a size, followed by references to the visible 2553 // declarations that have that name. 2554 uint64_t Offset = Stream.GetCurrentBitNo(); 2555 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2556 if (!Map || Map->empty()) 2557 return 0; 2558 2559 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2560 ASTDeclContextNameLookupTrait Trait(*this); 2561 2562 // Create the on-disk hash table representation. 2563 DeclarationName ConversionName; 2564 llvm::SmallVector<NamedDecl *, 4> ConversionDecls; 2565 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2566 D != DEnd; ++D) { 2567 DeclarationName Name = D->first; 2568 DeclContext::lookup_result Result = D->second.getLookupResult(); 2569 if (Result.first != Result.second) { 2570 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { 2571 // Hash all conversion function names to the same name. The actual 2572 // type information in conversion function name is not used in the 2573 // key (since such type information is not stable across different 2574 // modules), so the intended effect is to coalesce all of the conversion 2575 // functions under a single key. 2576 if (!ConversionName) 2577 ConversionName = Name; 2578 ConversionDecls.append(Result.first, Result.second); 2579 continue; 2580 } 2581 2582 Generator.insert(Name, Result, Trait); 2583 } 2584 } 2585 2586 // Add the conversion functions 2587 if (!ConversionDecls.empty()) { 2588 Generator.insert(ConversionName, 2589 DeclContext::lookup_result(ConversionDecls.begin(), 2590 ConversionDecls.end()), 2591 Trait); 2592 } 2593 2594 // Create the on-disk hash table in a buffer. 2595 llvm::SmallString<4096> LookupTable; 2596 uint32_t BucketOffset; 2597 { 2598 llvm::raw_svector_ostream Out(LookupTable); 2599 // Make sure that no bucket is at offset 0 2600 clang::io::Emit32(Out, 0); 2601 BucketOffset = Generator.Emit(Out, Trait); 2602 } 2603 2604 // Write the lookup table 2605 RecordData Record; 2606 Record.push_back(DECL_CONTEXT_VISIBLE); 2607 Record.push_back(BucketOffset); 2608 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, 2609 LookupTable.str()); 2610 2611 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record); 2612 ++NumVisibleDeclContexts; 2613 return Offset; 2614} 2615 2616/// \brief Write an UPDATE_VISIBLE block for the given context. 2617/// 2618/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing 2619/// DeclContext in a dependent AST file. As such, they only exist for the TU 2620/// (in C++) and for namespaces. 2621void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { 2622 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); 2623 if (!Map || Map->empty()) 2624 return; 2625 2626 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; 2627 ASTDeclContextNameLookupTrait Trait(*this); 2628 2629 // Create the hash table. 2630 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); 2631 D != DEnd; ++D) { 2632 DeclarationName Name = D->first; 2633 DeclContext::lookup_result Result = D->second.getLookupResult(); 2634 // For any name that appears in this table, the results are complete, i.e. 2635 // they overwrite results from previous PCHs. Merging is always a mess. 2636 if (Result.first != Result.second) 2637 Generator.insert(Name, Result, Trait); 2638 } 2639 2640 // Create the on-disk hash table in a buffer. 2641 llvm::SmallString<4096> LookupTable; 2642 uint32_t BucketOffset; 2643 { 2644 llvm::raw_svector_ostream Out(LookupTable); 2645 // Make sure that no bucket is at offset 0 2646 clang::io::Emit32(Out, 0); 2647 BucketOffset = Generator.Emit(Out, Trait); 2648 } 2649 2650 // Write the lookup table 2651 RecordData Record; 2652 Record.push_back(UPDATE_VISIBLE); 2653 Record.push_back(getDeclID(cast<Decl>(DC))); 2654 Record.push_back(BucketOffset); 2655 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); 2656} 2657 2658/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions. 2659void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) { 2660 RecordData Record; 2661 Record.push_back(Opts.fp_contract); 2662 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record); 2663} 2664 2665/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions. 2666void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) { 2667 if (!SemaRef.Context.getLangOptions().OpenCL) 2668 return; 2669 2670 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions(); 2671 RecordData Record; 2672#define OPENCLEXT(nm) Record.push_back(Opts.nm); 2673#include "clang/Basic/OpenCLExtensions.def" 2674 Stream.EmitRecord(OPENCL_EXTENSIONS, Record); 2675} 2676 2677//===----------------------------------------------------------------------===// 2678// General Serialization Routines 2679//===----------------------------------------------------------------------===// 2680 2681/// \brief Write a record containing the given attributes. 2682void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) { 2683 Record.push_back(Attrs.size()); 2684 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){ 2685 const Attr * A = *i; 2686 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs 2687 AddSourceRange(A->getRange(), Record); 2688 2689#include "clang/Serialization/AttrPCHWrite.inc" 2690 2691 } 2692} 2693 2694void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) { 2695 Record.push_back(Str.size()); 2696 Record.insert(Record.end(), Str.begin(), Str.end()); 2697} 2698 2699void ASTWriter::AddVersionTuple(const VersionTuple &Version, 2700 RecordDataImpl &Record) { 2701 Record.push_back(Version.getMajor()); 2702 if (llvm::Optional<unsigned> Minor = Version.getMinor()) 2703 Record.push_back(*Minor + 1); 2704 else 2705 Record.push_back(0); 2706 if (llvm::Optional<unsigned> Subminor = Version.getSubminor()) 2707 Record.push_back(*Subminor + 1); 2708 else 2709 Record.push_back(0); 2710} 2711 2712/// \brief Note that the identifier II occurs at the given offset 2713/// within the identifier table. 2714void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { 2715 IdentID ID = IdentifierIDs[II]; 2716 // Only store offsets new to this AST file. Other identifier names are looked 2717 // up earlier in the chain and thus don't need an offset. 2718 if (ID >= FirstIdentID) 2719 IdentifierOffsets[ID - FirstIdentID] = Offset; 2720} 2721 2722/// \brief Note that the selector Sel occurs at the given offset 2723/// within the method pool/selector table. 2724void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { 2725 unsigned ID = SelectorIDs[Sel]; 2726 assert(ID && "Unknown selector"); 2727 // Don't record offsets for selectors that are also available in a different 2728 // file. 2729 if (ID < FirstSelectorID) 2730 return; 2731 SelectorOffsets[ID - FirstSelectorID] = Offset; 2732} 2733 2734ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) 2735 : Stream(Stream), Context(0), Chain(0), SerializationListener(0), 2736 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID), 2737 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), 2738 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID), 2739 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID), 2740 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID), 2741 CollectedStmts(&StmtsToEmit), 2742 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), 2743 NumVisibleDeclContexts(0), 2744 NextCXXBaseSpecifiersID(1), 2745 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0), 2746 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0), 2747 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0), 2748 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0), 2749 DeclTypedefAbbrev(0), 2750 DeclVarAbbrev(0), DeclFieldAbbrev(0), 2751 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0) 2752{ 2753} 2754 2755void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2756 const std::string &OutputFile, 2757 bool IsModule, StringRef isysroot) { 2758 // Emit the file header. 2759 Stream.Emit((unsigned)'C', 8); 2760 Stream.Emit((unsigned)'P', 8); 2761 Stream.Emit((unsigned)'C', 8); 2762 Stream.Emit((unsigned)'H', 8); 2763 2764 WriteBlockInfoBlock(); 2765 2766 Context = &SemaRef.Context; 2767 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, IsModule); 2768 Context = 0; 2769} 2770 2771template<typename Vector> 2772static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec, 2773 ASTWriter::RecordData &Record) { 2774 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end(); 2775 I != E; ++I) { 2776 Writer.AddDeclRef(*I, Record); 2777 } 2778} 2779 2780void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, 2781 StringRef isysroot, 2782 const std::string &OutputFile, bool IsModule) { 2783 using namespace llvm; 2784 2785 ASTContext &Context = SemaRef.Context; 2786 Preprocessor &PP = SemaRef.PP; 2787 2788 // Set up predefined declaration IDs. 2789 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID; 2790 if (Context.ObjCIdDecl) 2791 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID; 2792 if (Context.ObjCSelDecl) 2793 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID; 2794 if (Context.ObjCClassDecl) 2795 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID; 2796 if (Context.Int128Decl) 2797 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID; 2798 if (Context.UInt128Decl) 2799 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID; 2800 if (Context.ObjCInstanceTypeDecl) 2801 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID; 2802 2803 if (!Chain) { 2804 // Make sure that we emit IdentifierInfos (and any attached 2805 // declarations) for builtins. We don't need to do this when we're 2806 // emitting chained PCH files, because all of the builtins will be 2807 // in the original PCH file. 2808 // FIXME: Modules won't like this at all. 2809 IdentifierTable &Table = PP.getIdentifierTable(); 2810 SmallVector<const char *, 32> BuiltinNames; 2811 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, 2812 Context.getLangOptions().NoBuiltin); 2813 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) 2814 getIdentifierRef(&Table.get(BuiltinNames[I])); 2815 } 2816 2817 // Build a record containing all of the tentative definitions in this file, in 2818 // TentativeDefinitions order. Generally, this record will be empty for 2819 // headers. 2820 RecordData TentativeDefinitions; 2821 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions); 2822 2823 // Build a record containing all of the file scoped decls in this file. 2824 RecordData UnusedFileScopedDecls; 2825 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls, 2826 UnusedFileScopedDecls); 2827 2828 // Build a record containing all of the delegating constructors we still need 2829 // to resolve. 2830 RecordData DelegatingCtorDecls; 2831 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls); 2832 2833 // Write the set of weak, undeclared identifiers. We always write the 2834 // entire table, since later PCH files in a PCH chain are only interested in 2835 // the results at the end of the chain. 2836 RecordData WeakUndeclaredIdentifiers; 2837 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { 2838 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator 2839 I = SemaRef.WeakUndeclaredIdentifiers.begin(), 2840 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { 2841 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); 2842 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); 2843 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); 2844 WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); 2845 } 2846 } 2847 2848 // Build a record containing all of the locally-scoped external 2849 // declarations in this header file. Generally, this record will be 2850 // empty. 2851 RecordData LocallyScopedExternalDecls; 2852 // FIXME: This is filling in the AST file in densemap order which is 2853 // nondeterminstic! 2854 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator 2855 TD = SemaRef.LocallyScopedExternalDecls.begin(), 2856 TDEnd = SemaRef.LocallyScopedExternalDecls.end(); 2857 TD != TDEnd; ++TD) { 2858 if (!TD->second->isFromASTFile()) 2859 AddDeclRef(TD->second, LocallyScopedExternalDecls); 2860 } 2861 2862 // Build a record containing all of the ext_vector declarations. 2863 RecordData ExtVectorDecls; 2864 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls); 2865 2866 // Build a record containing all of the VTable uses information. 2867 RecordData VTableUses; 2868 if (!SemaRef.VTableUses.empty()) { 2869 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 2870 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 2871 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 2872 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 2873 } 2874 } 2875 2876 // Build a record containing all of dynamic classes declarations. 2877 RecordData DynamicClasses; 2878 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses); 2879 2880 // Build a record containing all of pending implicit instantiations. 2881 RecordData PendingInstantiations; 2882 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 2883 I = SemaRef.PendingInstantiations.begin(), 2884 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 2885 AddDeclRef(I->first, PendingInstantiations); 2886 AddSourceLocation(I->second, PendingInstantiations); 2887 } 2888 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 2889 "There are local ones at end of translation unit!"); 2890 2891 // Build a record containing some declaration references. 2892 RecordData SemaDeclRefs; 2893 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 2894 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 2895 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 2896 } 2897 2898 RecordData CUDASpecialDeclRefs; 2899 if (Context.getcudaConfigureCallDecl()) { 2900 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs); 2901 } 2902 2903 // Build a record containing all of the known namespaces. 2904 RecordData KnownNamespaces; 2905 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator 2906 I = SemaRef.KnownNamespaces.begin(), 2907 IEnd = SemaRef.KnownNamespaces.end(); 2908 I != IEnd; ++I) { 2909 if (!I->second) 2910 AddDeclRef(I->first, KnownNamespaces); 2911 } 2912 2913 // Write the remaining AST contents. 2914 RecordData Record; 2915 Stream.EnterSubblock(AST_BLOCK_ID, 5); 2916 WriteMetadata(Context, isysroot, OutputFile); 2917 WriteLanguageOptions(Context.getLangOptions()); 2918 if (StatCalls && isysroot.empty()) 2919 WriteStatCache(*StatCalls); 2920 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); 2921 2922 if (Chain) { 2923 // Write the mapping information describing our module dependencies and how 2924 // each of those modules were mapped into our own offset/ID space, so that 2925 // the reader can build the appropriate mapping to its own offset/ID space. 2926 // The map consists solely of a blob with the following format: 2927 // *(module-name-len:i16 module-name:len*i8 2928 // source-location-offset:i32 2929 // identifier-id:i32 2930 // preprocessed-entity-id:i32 2931 // macro-definition-id:i32 2932 // selector-id:i32 2933 // declaration-id:i32 2934 // c++-base-specifiers-id:i32 2935 // type-id:i32) 2936 // 2937 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2938 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP)); 2939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2940 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev); 2941 llvm::SmallString<2048> Buffer; 2942 { 2943 llvm::raw_svector_ostream Out(Buffer); 2944 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(), 2945 MEnd = Chain->ModuleMgr.end(); 2946 M != MEnd; ++M) { 2947 StringRef FileName = (*M)->FileName; 2948 io::Emit16(Out, FileName.size()); 2949 Out.write(FileName.data(), FileName.size()); 2950 io::Emit32(Out, (*M)->SLocEntryBaseOffset); 2951 io::Emit32(Out, (*M)->BaseIdentifierID); 2952 io::Emit32(Out, (*M)->BasePreprocessedEntityID); 2953 io::Emit32(Out, (*M)->BaseMacroDefinitionID); 2954 io::Emit32(Out, (*M)->BaseSelectorID); 2955 io::Emit32(Out, (*M)->BaseDeclID); 2956 io::Emit32(Out, (*M)->BaseTypeIndex); 2957 } 2958 } 2959 Record.clear(); 2960 Record.push_back(MODULE_OFFSET_MAP); 2961 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record, 2962 Buffer.data(), Buffer.size()); 2963 } 2964 2965 // Create a lexical update block containing all of the declarations in the 2966 // translation unit that do not come from other AST files. 2967 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); 2968 SmallVector<KindDeclIDPair, 64> NewGlobalDecls; 2969 for (DeclContext::decl_iterator I = TU->noload_decls_begin(), 2970 E = TU->noload_decls_end(); 2971 I != E; ++I) { 2972 if (!(*I)->isFromASTFile()) 2973 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I))); 2974 else if ((*I)->isChangedSinceDeserialization()) 2975 (void)GetDeclRef(*I); // Make sure it's written, but don't record it. 2976 } 2977 2978 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); 2979 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); 2980 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 2981 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); 2982 Record.clear(); 2983 Record.push_back(TU_UPDATE_LEXICAL); 2984 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, 2985 data(NewGlobalDecls)); 2986 2987 // And a visible updates block for the translation unit. 2988 Abv = new llvm::BitCodeAbbrev(); 2989 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); 2990 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); 2991 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); 2992 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 2993 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); 2994 WriteDeclContextVisibleUpdate(TU); 2995 2996 // If the translation unit has an anonymous namespace, and we don't already 2997 // have an update block for it, write it as an update block. 2998 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) { 2999 ASTWriter::UpdateRecord &Record = DeclUpdates[TU]; 3000 if (Record.empty()) { 3001 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE); 3002 AddDeclRef(NS, Record); 3003 } 3004 } 3005 3006 // Form the record of special types. 3007 RecordData SpecialTypes; 3008 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes); 3009 AddTypeRef(Context.ObjCProtoType, SpecialTypes); 3010 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); 3011 AddTypeRef(Context.getFILEType(), SpecialTypes); 3012 AddTypeRef(Context.getjmp_bufType(), SpecialTypes); 3013 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); 3014 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); 3015 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); 3016 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); 3017 3018 // Keep writing types and declarations until all types and 3019 // declarations have been written. 3020 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); 3021 WriteDeclsBlockAbbrevs(); 3022 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(), 3023 E = DeclsToRewrite.end(); 3024 I != E; ++I) 3025 DeclTypesToEmit.push(const_cast<Decl*>(*I)); 3026 while (!DeclTypesToEmit.empty()) { 3027 DeclOrType DOT = DeclTypesToEmit.front(); 3028 DeclTypesToEmit.pop(); 3029 if (DOT.isType()) 3030 WriteType(DOT.getType()); 3031 else 3032 WriteDecl(Context, DOT.getDecl()); 3033 } 3034 Stream.ExitBlock(); 3035 3036 WritePreprocessor(PP, IsModule); 3037 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot); 3038 WriteSelectors(SemaRef); 3039 WriteReferencedSelectorsPool(SemaRef); 3040 WriteIdentifierTable(PP, IsModule); 3041 WriteFPPragmaOptions(SemaRef.getFPOptions()); 3042 WriteOpenCLExtensions(SemaRef); 3043 3044 WriteTypeDeclOffsets(); 3045 WritePragmaDiagnosticMappings(Context.getDiagnostics()); 3046 3047 WriteCXXBaseSpecifiersOffsets(); 3048 3049 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes); 3050 3051 /// Build a record containing first declarations from a chained PCH and the 3052 /// most recent declarations in this AST that they point to. 3053 RecordData FirstLatestDeclIDs; 3054 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(), 3055 E = FirstLatestDecls.end(); 3056 I != E; ++I) { 3057 AddDeclRef(I->first, FirstLatestDeclIDs); 3058 AddDeclRef(I->second, FirstLatestDeclIDs); 3059 } 3060 3061 if (!FirstLatestDeclIDs.empty()) 3062 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs); 3063 3064 // Write the record containing external, unnamed definitions. 3065 if (!ExternalDefinitions.empty()) 3066 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); 3067 3068 // Write the record containing tentative definitions. 3069 if (!TentativeDefinitions.empty()) 3070 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 3071 3072 // Write the record containing unused file scoped decls. 3073 if (!UnusedFileScopedDecls.empty()) 3074 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 3075 3076 // Write the record containing weak undeclared identifiers. 3077 if (!WeakUndeclaredIdentifiers.empty()) 3078 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 3079 WeakUndeclaredIdentifiers); 3080 3081 // Write the record containing locally-scoped external definitions. 3082 if (!LocallyScopedExternalDecls.empty()) 3083 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, 3084 LocallyScopedExternalDecls); 3085 3086 // Write the record containing ext_vector type names. 3087 if (!ExtVectorDecls.empty()) 3088 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 3089 3090 // Write the record containing VTable uses information. 3091 if (!VTableUses.empty()) 3092 Stream.EmitRecord(VTABLE_USES, VTableUses); 3093 3094 // Write the record containing dynamic classes declarations. 3095 if (!DynamicClasses.empty()) 3096 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); 3097 3098 // Write the record containing pending implicit instantiations. 3099 if (!PendingInstantiations.empty()) 3100 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 3101 3102 // Write the record containing declaration references of Sema. 3103 if (!SemaDeclRefs.empty()) 3104 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 3105 3106 // Write the record containing CUDA-specific declaration references. 3107 if (!CUDASpecialDeclRefs.empty()) 3108 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs); 3109 3110 // Write the delegating constructors. 3111 if (!DelegatingCtorDecls.empty()) 3112 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls); 3113 3114 // Write the known namespaces. 3115 if (!KnownNamespaces.empty()) 3116 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces); 3117 3118 // Write the visible updates to DeclContexts. 3119 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator 3120 I = UpdatedDeclContexts.begin(), 3121 E = UpdatedDeclContexts.end(); 3122 I != E; ++I) 3123 WriteDeclContextVisibleUpdate(*I); 3124 3125 WriteDeclUpdatesBlocks(); 3126 WriteDeclReplacementsBlock(); 3127 WriteChainedObjCCategories(); 3128 3129 // Some simple statistics 3130 Record.clear(); 3131 Record.push_back(NumStatements); 3132 Record.push_back(NumMacros); 3133 Record.push_back(NumLexicalDeclContexts); 3134 Record.push_back(NumVisibleDeclContexts); 3135 Stream.EmitRecord(STATISTICS, Record); 3136 Stream.ExitBlock(); 3137} 3138 3139void ASTWriter::WriteDeclUpdatesBlocks() { 3140 if (DeclUpdates.empty()) 3141 return; 3142 3143 RecordData OffsetsRecord; 3144 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); 3145 for (DeclUpdateMap::iterator 3146 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { 3147 const Decl *D = I->first; 3148 UpdateRecord &URec = I->second; 3149 3150 if (DeclsToRewrite.count(D)) 3151 continue; // The decl will be written completely,no need to store updates. 3152 3153 uint64_t Offset = Stream.GetCurrentBitNo(); 3154 Stream.EmitRecord(DECL_UPDATES, URec); 3155 3156 OffsetsRecord.push_back(GetDeclRef(D)); 3157 OffsetsRecord.push_back(Offset); 3158 } 3159 Stream.ExitBlock(); 3160 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord); 3161} 3162 3163void ASTWriter::WriteDeclReplacementsBlock() { 3164 if (ReplacedDecls.empty()) 3165 return; 3166 3167 RecordData Record; 3168 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator 3169 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { 3170 Record.push_back(I->first); 3171 Record.push_back(I->second); 3172 } 3173 Stream.EmitRecord(DECL_REPLACEMENTS, Record); 3174} 3175 3176void ASTWriter::WriteChainedObjCCategories() { 3177 if (LocalChainedObjCCategories.empty()) 3178 return; 3179 3180 RecordData Record; 3181 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator 3182 I = LocalChainedObjCCategories.begin(), 3183 E = LocalChainedObjCCategories.end(); I != E; ++I) { 3184 ChainedObjCCategoriesData &Data = *I; 3185 serialization::DeclID 3186 HeadCatID = getDeclID(Data.Interface->getCategoryList()); 3187 assert(HeadCatID != 0 && "Category not written ?"); 3188 3189 Record.push_back(Data.InterfaceID); 3190 Record.push_back(HeadCatID); 3191 Record.push_back(Data.TailCatID); 3192 } 3193 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record); 3194} 3195 3196void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { 3197 Record.push_back(Loc.getRawEncoding()); 3198} 3199 3200void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { 3201 AddSourceLocation(Range.getBegin(), Record); 3202 AddSourceLocation(Range.getEnd(), Record); 3203} 3204 3205void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { 3206 Record.push_back(Value.getBitWidth()); 3207 const uint64_t *Words = Value.getRawData(); 3208 Record.append(Words, Words + Value.getNumWords()); 3209} 3210 3211void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { 3212 Record.push_back(Value.isUnsigned()); 3213 AddAPInt(Value, Record); 3214} 3215 3216void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { 3217 AddAPInt(Value.bitcastToAPInt(), Record); 3218} 3219 3220void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { 3221 Record.push_back(getIdentifierRef(II)); 3222} 3223 3224IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { 3225 if (II == 0) 3226 return 0; 3227 3228 IdentID &ID = IdentifierIDs[II]; 3229 if (ID == 0) 3230 ID = NextIdentID++; 3231 return ID; 3232} 3233 3234MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) { 3235 if (MD == 0) 3236 return 0; 3237 3238 MacroID &ID = MacroDefinitions[MD]; 3239 if (ID == 0) 3240 ID = NextMacroID++; 3241 return ID; 3242} 3243 3244void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { 3245 Record.push_back(getSelectorRef(SelRef)); 3246} 3247 3248SelectorID ASTWriter::getSelectorRef(Selector Sel) { 3249 if (Sel.getAsOpaquePtr() == 0) { 3250 return 0; 3251 } 3252 3253 SelectorID &SID = SelectorIDs[Sel]; 3254 if (SID == 0 && Chain) { 3255 // This might trigger a ReadSelector callback, which will set the ID for 3256 // this selector. 3257 Chain->LoadSelector(Sel); 3258 } 3259 if (SID == 0) { 3260 SID = NextSelectorID++; 3261 } 3262 return SID; 3263} 3264 3265void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { 3266 AddDeclRef(Temp->getDestructor(), Record); 3267} 3268 3269void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, 3270 CXXBaseSpecifier const *BasesEnd, 3271 RecordDataImpl &Record) { 3272 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); 3273 CXXBaseSpecifiersToWrite.push_back( 3274 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, 3275 Bases, BasesEnd)); 3276 Record.push_back(NextCXXBaseSpecifiersID++); 3277} 3278 3279void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 3280 const TemplateArgumentLocInfo &Arg, 3281 RecordDataImpl &Record) { 3282 switch (Kind) { 3283 case TemplateArgument::Expression: 3284 AddStmt(Arg.getAsExpr()); 3285 break; 3286 case TemplateArgument::Type: 3287 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); 3288 break; 3289 case TemplateArgument::Template: 3290 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); 3291 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 3292 break; 3293 case TemplateArgument::TemplateExpansion: 3294 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); 3295 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 3296 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record); 3297 break; 3298 case TemplateArgument::Null: 3299 case TemplateArgument::Integral: 3300 case TemplateArgument::Declaration: 3301 case TemplateArgument::Pack: 3302 break; 3303 } 3304} 3305 3306void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, 3307 RecordDataImpl &Record) { 3308 AddTemplateArgument(Arg.getArgument(), Record); 3309 3310 if (Arg.getArgument().getKind() == TemplateArgument::Expression) { 3311 bool InfoHasSameExpr 3312 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); 3313 Record.push_back(InfoHasSameExpr); 3314 if (InfoHasSameExpr) 3315 return; // Avoid storing the same expr twice. 3316 } 3317 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), 3318 Record); 3319} 3320 3321void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, 3322 RecordDataImpl &Record) { 3323 if (TInfo == 0) { 3324 AddTypeRef(QualType(), Record); 3325 return; 3326 } 3327 3328 AddTypeLoc(TInfo->getTypeLoc(), Record); 3329} 3330 3331void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) { 3332 AddTypeRef(TL.getType(), Record); 3333 3334 TypeLocWriter TLW(*this, Record); 3335 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 3336 TLW.Visit(TL); 3337} 3338 3339void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { 3340 Record.push_back(GetOrCreateTypeID(T)); 3341} 3342 3343TypeID ASTWriter::GetOrCreateTypeID( QualType T) { 3344 return MakeTypeID(*Context, T, 3345 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this)); 3346} 3347 3348TypeID ASTWriter::getTypeID(QualType T) const { 3349 return MakeTypeID(*Context, T, 3350 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this)); 3351} 3352 3353TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) { 3354 if (T.isNull()) 3355 return TypeIdx(); 3356 assert(!T.getLocalFastQualifiers()); 3357 3358 TypeIdx &Idx = TypeIdxs[T]; 3359 if (Idx.getIndex() == 0) { 3360 // We haven't seen this type before. Assign it a new ID and put it 3361 // into the queue of types to emit. 3362 Idx = TypeIdx(NextTypeID++); 3363 DeclTypesToEmit.push(T); 3364 } 3365 return Idx; 3366} 3367 3368TypeIdx ASTWriter::getTypeIdx(QualType T) const { 3369 if (T.isNull()) 3370 return TypeIdx(); 3371 assert(!T.getLocalFastQualifiers()); 3372 3373 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 3374 assert(I != TypeIdxs.end() && "Type not emitted!"); 3375 return I->second; 3376} 3377 3378void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { 3379 Record.push_back(GetDeclRef(D)); 3380} 3381 3382DeclID ASTWriter::GetDeclRef(const Decl *D) { 3383 if (D == 0) { 3384 return 0; 3385 } 3386 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); 3387 DeclID &ID = DeclIDs[D]; 3388 if (ID == 0) { 3389 // We haven't seen this declaration before. Give it a new ID and 3390 // enqueue it in the list of declarations to emit. 3391 ID = NextDeclID++; 3392 DeclTypesToEmit.push(const_cast<Decl *>(D)); 3393 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) { 3394 // We don't add it to the replacement collection here, because we don't 3395 // have the offset yet. 3396 DeclTypesToEmit.push(const_cast<Decl *>(D)); 3397 // Reset the flag, so that we don't add this decl multiple times. 3398 const_cast<Decl *>(D)->setChangedSinceDeserialization(false); 3399 } 3400 3401 return ID; 3402} 3403 3404DeclID ASTWriter::getDeclID(const Decl *D) { 3405 if (D == 0) 3406 return 0; 3407 3408 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); 3409 return DeclIDs[D]; 3410} 3411 3412void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { 3413 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. 3414 Record.push_back(Name.getNameKind()); 3415 switch (Name.getNameKind()) { 3416 case DeclarationName::Identifier: 3417 AddIdentifierRef(Name.getAsIdentifierInfo(), Record); 3418 break; 3419 3420 case DeclarationName::ObjCZeroArgSelector: 3421 case DeclarationName::ObjCOneArgSelector: 3422 case DeclarationName::ObjCMultiArgSelector: 3423 AddSelectorRef(Name.getObjCSelector(), Record); 3424 break; 3425 3426 case DeclarationName::CXXConstructorName: 3427 case DeclarationName::CXXDestructorName: 3428 case DeclarationName::CXXConversionFunctionName: 3429 AddTypeRef(Name.getCXXNameType(), Record); 3430 break; 3431 3432 case DeclarationName::CXXOperatorName: 3433 Record.push_back(Name.getCXXOverloadedOperator()); 3434 break; 3435 3436 case DeclarationName::CXXLiteralOperatorName: 3437 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); 3438 break; 3439 3440 case DeclarationName::CXXUsingDirective: 3441 // No extra data to emit 3442 break; 3443 } 3444} 3445 3446void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 3447 DeclarationName Name, RecordDataImpl &Record) { 3448 switch (Name.getNameKind()) { 3449 case DeclarationName::CXXConstructorName: 3450 case DeclarationName::CXXDestructorName: 3451 case DeclarationName::CXXConversionFunctionName: 3452 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); 3453 break; 3454 3455 case DeclarationName::CXXOperatorName: 3456 AddSourceLocation( 3457 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), 3458 Record); 3459 AddSourceLocation( 3460 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), 3461 Record); 3462 break; 3463 3464 case DeclarationName::CXXLiteralOperatorName: 3465 AddSourceLocation( 3466 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), 3467 Record); 3468 break; 3469 3470 case DeclarationName::Identifier: 3471 case DeclarationName::ObjCZeroArgSelector: 3472 case DeclarationName::ObjCOneArgSelector: 3473 case DeclarationName::ObjCMultiArgSelector: 3474 case DeclarationName::CXXUsingDirective: 3475 break; 3476 } 3477} 3478 3479void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 3480 RecordDataImpl &Record) { 3481 AddDeclarationName(NameInfo.getName(), Record); 3482 AddSourceLocation(NameInfo.getLoc(), Record); 3483 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); 3484} 3485 3486void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, 3487 RecordDataImpl &Record) { 3488 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record); 3489 Record.push_back(Info.NumTemplParamLists); 3490 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) 3491 AddTemplateParameterList(Info.TemplParamLists[i], Record); 3492} 3493 3494void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, 3495 RecordDataImpl &Record) { 3496 // Nested name specifiers usually aren't too long. I think that 8 would 3497 // typically accommodate the vast majority. 3498 SmallVector<NestedNameSpecifier *, 8> NestedNames; 3499 3500 // Push each of the NNS's onto a stack for serialization in reverse order. 3501 while (NNS) { 3502 NestedNames.push_back(NNS); 3503 NNS = NNS->getPrefix(); 3504 } 3505 3506 Record.push_back(NestedNames.size()); 3507 while(!NestedNames.empty()) { 3508 NNS = NestedNames.pop_back_val(); 3509 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); 3510 Record.push_back(Kind); 3511 switch (Kind) { 3512 case NestedNameSpecifier::Identifier: 3513 AddIdentifierRef(NNS->getAsIdentifier(), Record); 3514 break; 3515 3516 case NestedNameSpecifier::Namespace: 3517 AddDeclRef(NNS->getAsNamespace(), Record); 3518 break; 3519 3520 case NestedNameSpecifier::NamespaceAlias: 3521 AddDeclRef(NNS->getAsNamespaceAlias(), Record); 3522 break; 3523 3524 case NestedNameSpecifier::TypeSpec: 3525 case NestedNameSpecifier::TypeSpecWithTemplate: 3526 AddTypeRef(QualType(NNS->getAsType(), 0), Record); 3527 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 3528 break; 3529 3530 case NestedNameSpecifier::Global: 3531 // Don't need to write an associated value. 3532 break; 3533 } 3534 } 3535} 3536 3537void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, 3538 RecordDataImpl &Record) { 3539 // Nested name specifiers usually aren't too long. I think that 8 would 3540 // typically accommodate the vast majority. 3541 SmallVector<NestedNameSpecifierLoc , 8> NestedNames; 3542 3543 // Push each of the nested-name-specifiers's onto a stack for 3544 // serialization in reverse order. 3545 while (NNS) { 3546 NestedNames.push_back(NNS); 3547 NNS = NNS.getPrefix(); 3548 } 3549 3550 Record.push_back(NestedNames.size()); 3551 while(!NestedNames.empty()) { 3552 NNS = NestedNames.pop_back_val(); 3553 NestedNameSpecifier::SpecifierKind Kind 3554 = NNS.getNestedNameSpecifier()->getKind(); 3555 Record.push_back(Kind); 3556 switch (Kind) { 3557 case NestedNameSpecifier::Identifier: 3558 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record); 3559 AddSourceRange(NNS.getLocalSourceRange(), Record); 3560 break; 3561 3562 case NestedNameSpecifier::Namespace: 3563 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record); 3564 AddSourceRange(NNS.getLocalSourceRange(), Record); 3565 break; 3566 3567 case NestedNameSpecifier::NamespaceAlias: 3568 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record); 3569 AddSourceRange(NNS.getLocalSourceRange(), Record); 3570 break; 3571 3572 case NestedNameSpecifier::TypeSpec: 3573 case NestedNameSpecifier::TypeSpecWithTemplate: 3574 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 3575 AddTypeLoc(NNS.getTypeLoc(), Record); 3576 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); 3577 break; 3578 3579 case NestedNameSpecifier::Global: 3580 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); 3581 break; 3582 } 3583 } 3584} 3585 3586void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { 3587 TemplateName::NameKind Kind = Name.getKind(); 3588 Record.push_back(Kind); 3589 switch (Kind) { 3590 case TemplateName::Template: 3591 AddDeclRef(Name.getAsTemplateDecl(), Record); 3592 break; 3593 3594 case TemplateName::OverloadedTemplate: { 3595 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); 3596 Record.push_back(OvT->size()); 3597 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); 3598 I != E; ++I) 3599 AddDeclRef(*I, Record); 3600 break; 3601 } 3602 3603 case TemplateName::QualifiedTemplate: { 3604 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); 3605 AddNestedNameSpecifier(QualT->getQualifier(), Record); 3606 Record.push_back(QualT->hasTemplateKeyword()); 3607 AddDeclRef(QualT->getTemplateDecl(), Record); 3608 break; 3609 } 3610 3611 case TemplateName::DependentTemplate: { 3612 DependentTemplateName *DepT = Name.getAsDependentTemplateName(); 3613 AddNestedNameSpecifier(DepT->getQualifier(), Record); 3614 Record.push_back(DepT->isIdentifier()); 3615 if (DepT->isIdentifier()) 3616 AddIdentifierRef(DepT->getIdentifier(), Record); 3617 else 3618 Record.push_back(DepT->getOperator()); 3619 break; 3620 } 3621 3622 case TemplateName::SubstTemplateTemplateParm: { 3623 SubstTemplateTemplateParmStorage *subst 3624 = Name.getAsSubstTemplateTemplateParm(); 3625 AddDeclRef(subst->getParameter(), Record); 3626 AddTemplateName(subst->getReplacement(), Record); 3627 break; 3628 } 3629 3630 case TemplateName::SubstTemplateTemplateParmPack: { 3631 SubstTemplateTemplateParmPackStorage *SubstPack 3632 = Name.getAsSubstTemplateTemplateParmPack(); 3633 AddDeclRef(SubstPack->getParameterPack(), Record); 3634 AddTemplateArgument(SubstPack->getArgumentPack(), Record); 3635 break; 3636 } 3637 } 3638} 3639 3640void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, 3641 RecordDataImpl &Record) { 3642 Record.push_back(Arg.getKind()); 3643 switch (Arg.getKind()) { 3644 case TemplateArgument::Null: 3645 break; 3646 case TemplateArgument::Type: 3647 AddTypeRef(Arg.getAsType(), Record); 3648 break; 3649 case TemplateArgument::Declaration: 3650 AddDeclRef(Arg.getAsDecl(), Record); 3651 break; 3652 case TemplateArgument::Integral: 3653 AddAPSInt(*Arg.getAsIntegral(), Record); 3654 AddTypeRef(Arg.getIntegralType(), Record); 3655 break; 3656 case TemplateArgument::Template: 3657 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); 3658 break; 3659 case TemplateArgument::TemplateExpansion: 3660 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); 3661 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions()) 3662 Record.push_back(*NumExpansions + 1); 3663 else 3664 Record.push_back(0); 3665 break; 3666 case TemplateArgument::Expression: 3667 AddStmt(Arg.getAsExpr()); 3668 break; 3669 case TemplateArgument::Pack: 3670 Record.push_back(Arg.pack_size()); 3671 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); 3672 I != E; ++I) 3673 AddTemplateArgument(*I, Record); 3674 break; 3675 } 3676} 3677 3678void 3679ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, 3680 RecordDataImpl &Record) { 3681 assert(TemplateParams && "No TemplateParams!"); 3682 AddSourceLocation(TemplateParams->getTemplateLoc(), Record); 3683 AddSourceLocation(TemplateParams->getLAngleLoc(), Record); 3684 AddSourceLocation(TemplateParams->getRAngleLoc(), Record); 3685 Record.push_back(TemplateParams->size()); 3686 for (TemplateParameterList::const_iterator 3687 P = TemplateParams->begin(), PEnd = TemplateParams->end(); 3688 P != PEnd; ++P) 3689 AddDeclRef(*P, Record); 3690} 3691 3692/// \brief Emit a template argument list. 3693void 3694ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, 3695 RecordDataImpl &Record) { 3696 assert(TemplateArgs && "No TemplateArgs!"); 3697 Record.push_back(TemplateArgs->size()); 3698 for (int i=0, e = TemplateArgs->size(); i != e; ++i) 3699 AddTemplateArgument(TemplateArgs->get(i), Record); 3700} 3701 3702 3703void 3704ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) { 3705 Record.push_back(Set.size()); 3706 for (UnresolvedSetImpl::const_iterator 3707 I = Set.begin(), E = Set.end(); I != E; ++I) { 3708 AddDeclRef(I.getDecl(), Record); 3709 Record.push_back(I.getAccess()); 3710 } 3711} 3712 3713void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, 3714 RecordDataImpl &Record) { 3715 Record.push_back(Base.isVirtual()); 3716 Record.push_back(Base.isBaseOfClass()); 3717 Record.push_back(Base.getAccessSpecifierAsWritten()); 3718 Record.push_back(Base.getInheritConstructors()); 3719 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); 3720 AddSourceRange(Base.getSourceRange(), Record); 3721 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() 3722 : SourceLocation(), 3723 Record); 3724} 3725 3726void ASTWriter::FlushCXXBaseSpecifiers() { 3727 RecordData Record; 3728 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) { 3729 Record.clear(); 3730 3731 // Record the offset of this base-specifier set. 3732 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1; 3733 if (Index == CXXBaseSpecifiersOffsets.size()) 3734 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); 3735 else { 3736 if (Index > CXXBaseSpecifiersOffsets.size()) 3737 CXXBaseSpecifiersOffsets.resize(Index + 1); 3738 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); 3739 } 3740 3741 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, 3742 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; 3743 Record.push_back(BEnd - B); 3744 for (; B != BEnd; ++B) 3745 AddCXXBaseSpecifier(*B, Record); 3746 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); 3747 3748 // Flush any expressions that were written as part of the base specifiers. 3749 FlushStmts(); 3750 } 3751 3752 CXXBaseSpecifiersToWrite.clear(); 3753} 3754 3755void ASTWriter::AddCXXCtorInitializers( 3756 const CXXCtorInitializer * const *CtorInitializers, 3757 unsigned NumCtorInitializers, 3758 RecordDataImpl &Record) { 3759 Record.push_back(NumCtorInitializers); 3760 for (unsigned i=0; i != NumCtorInitializers; ++i) { 3761 const CXXCtorInitializer *Init = CtorInitializers[i]; 3762 3763 if (Init->isBaseInitializer()) { 3764 Record.push_back(CTOR_INITIALIZER_BASE); 3765 AddTypeSourceInfo(Init->getBaseClassInfo(), Record); 3766 Record.push_back(Init->isBaseVirtual()); 3767 } else if (Init->isDelegatingInitializer()) { 3768 Record.push_back(CTOR_INITIALIZER_DELEGATING); 3769 AddDeclRef(Init->getTargetConstructor(), Record); 3770 } else if (Init->isMemberInitializer()){ 3771 Record.push_back(CTOR_INITIALIZER_MEMBER); 3772 AddDeclRef(Init->getMember(), Record); 3773 } else { 3774 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER); 3775 AddDeclRef(Init->getIndirectMember(), Record); 3776 } 3777 3778 AddSourceLocation(Init->getMemberLocation(), Record); 3779 AddStmt(Init->getInit()); 3780 AddSourceLocation(Init->getLParenLoc(), Record); 3781 AddSourceLocation(Init->getRParenLoc(), Record); 3782 Record.push_back(Init->isWritten()); 3783 if (Init->isWritten()) { 3784 Record.push_back(Init->getSourceOrder()); 3785 } else { 3786 Record.push_back(Init->getNumArrayIndices()); 3787 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) 3788 AddDeclRef(Init->getArrayIndex(i), Record); 3789 } 3790 } 3791} 3792 3793void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { 3794 assert(D->DefinitionData); 3795 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; 3796 Record.push_back(Data.UserDeclaredConstructor); 3797 Record.push_back(Data.UserDeclaredCopyConstructor); 3798 Record.push_back(Data.UserDeclaredMoveConstructor); 3799 Record.push_back(Data.UserDeclaredCopyAssignment); 3800 Record.push_back(Data.UserDeclaredMoveAssignment); 3801 Record.push_back(Data.UserDeclaredDestructor); 3802 Record.push_back(Data.Aggregate); 3803 Record.push_back(Data.PlainOldData); 3804 Record.push_back(Data.Empty); 3805 Record.push_back(Data.Polymorphic); 3806 Record.push_back(Data.Abstract); 3807 Record.push_back(Data.IsStandardLayout); 3808 Record.push_back(Data.HasNoNonEmptyBases); 3809 Record.push_back(Data.HasPrivateFields); 3810 Record.push_back(Data.HasProtectedFields); 3811 Record.push_back(Data.HasPublicFields); 3812 Record.push_back(Data.HasMutableFields); 3813 Record.push_back(Data.HasTrivialDefaultConstructor); 3814 Record.push_back(Data.HasConstexprNonCopyMoveConstructor); 3815 Record.push_back(Data.HasTrivialCopyConstructor); 3816 Record.push_back(Data.HasTrivialMoveConstructor); 3817 Record.push_back(Data.HasTrivialCopyAssignment); 3818 Record.push_back(Data.HasTrivialMoveAssignment); 3819 Record.push_back(Data.HasTrivialDestructor); 3820 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases); 3821 Record.push_back(Data.ComputedVisibleConversions); 3822 Record.push_back(Data.UserProvidedDefaultConstructor); 3823 Record.push_back(Data.DeclaredDefaultConstructor); 3824 Record.push_back(Data.DeclaredCopyConstructor); 3825 Record.push_back(Data.DeclaredMoveConstructor); 3826 Record.push_back(Data.DeclaredCopyAssignment); 3827 Record.push_back(Data.DeclaredMoveAssignment); 3828 Record.push_back(Data.DeclaredDestructor); 3829 Record.push_back(Data.FailedImplicitMoveConstructor); 3830 Record.push_back(Data.FailedImplicitMoveAssignment); 3831 3832 Record.push_back(Data.NumBases); 3833 if (Data.NumBases > 0) 3834 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, 3835 Record); 3836 3837 // FIXME: Make VBases lazily computed when needed to avoid storing them. 3838 Record.push_back(Data.NumVBases); 3839 if (Data.NumVBases > 0) 3840 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, 3841 Record); 3842 3843 AddUnresolvedSet(Data.Conversions, Record); 3844 AddUnresolvedSet(Data.VisibleConversions, Record); 3845 // Data.Definition is the owning decl, no need to write it. 3846 AddDeclRef(Data.FirstFriend, Record); 3847} 3848 3849void ASTWriter::ReaderInitialized(ASTReader *Reader) { 3850 assert(Reader && "Cannot remove chain"); 3851 assert((!Chain || Chain == Reader) && "Cannot replace chain"); 3852 assert(FirstDeclID == NextDeclID && 3853 FirstTypeID == NextTypeID && 3854 FirstIdentID == NextIdentID && 3855 FirstSelectorID == NextSelectorID && 3856 FirstMacroID == NextMacroID && 3857 "Setting chain after writing has started."); 3858 3859 Chain = Reader; 3860 3861 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls(); 3862 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes(); 3863 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers(); 3864 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors(); 3865 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacroDefinitions(); 3866 NextDeclID = FirstDeclID; 3867 NextTypeID = FirstTypeID; 3868 NextIdentID = FirstIdentID; 3869 NextSelectorID = FirstSelectorID; 3870 NextMacroID = FirstMacroID; 3871} 3872 3873void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { 3874 IdentifierIDs[II] = ID; 3875 if (II->hasMacroDefinition()) 3876 DeserializedMacroNames.push_back(II); 3877} 3878 3879void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { 3880 // Always take the highest-numbered type index. This copes with an interesting 3881 // case for chained AST writing where we schedule writing the type and then, 3882 // later, deserialize the type from another AST. In this case, we want to 3883 // keep the higher-numbered entry so that we can properly write it out to 3884 // the AST file. 3885 TypeIdx &StoredIdx = TypeIdxs[T]; 3886 if (Idx.getIndex() >= StoredIdx.getIndex()) 3887 StoredIdx = Idx; 3888} 3889 3890void ASTWriter::DeclRead(DeclID ID, const Decl *D) { 3891 DeclIDs[D] = ID; 3892} 3893 3894void ASTWriter::SelectorRead(SelectorID ID, Selector S) { 3895 SelectorIDs[S] = ID; 3896} 3897 3898void ASTWriter::MacroDefinitionRead(serialization::MacroID ID, 3899 MacroDefinition *MD) { 3900 MacroDefinitions[MD] = ID; 3901} 3902 3903void ASTWriter::CompletedTagDefinition(const TagDecl *D) { 3904 assert(D->isDefinition()); 3905 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 3906 // We are interested when a PCH decl is modified. 3907 if (RD->isFromASTFile()) { 3908 // A forward reference was mutated into a definition. Rewrite it. 3909 // FIXME: This happens during template instantiation, should we 3910 // have created a new definition decl instead ? 3911 RewriteDecl(RD); 3912 } 3913 3914 for (CXXRecordDecl::redecl_iterator 3915 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) { 3916 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I); 3917 if (Redecl == RD) 3918 continue; 3919 3920 // We are interested when a PCH decl is modified. 3921 if (Redecl->isFromASTFile()) { 3922 UpdateRecord &Record = DeclUpdates[Redecl]; 3923 Record.push_back(UPD_CXX_SET_DEFINITIONDATA); 3924 assert(Redecl->DefinitionData); 3925 assert(Redecl->DefinitionData->Definition == D); 3926 AddDeclRef(D, Record); // the DefinitionDecl 3927 } 3928 } 3929 } 3930} 3931void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { 3932 // TU and namespaces are handled elsewhere. 3933 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) 3934 return; 3935 3936 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile())) 3937 return; // Not a source decl added to a DeclContext from PCH. 3938 3939 AddUpdatedDeclContext(DC); 3940} 3941 3942void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { 3943 assert(D->isImplicit()); 3944 if (!(!D->isFromASTFile() && RD->isFromASTFile())) 3945 return; // Not a source member added to a class from PCH. 3946 if (!isa<CXXMethodDecl>(D)) 3947 return; // We are interested in lazily declared implicit methods. 3948 3949 // A decl coming from PCH was modified. 3950 assert(RD->isDefinition()); 3951 UpdateRecord &Record = DeclUpdates[RD]; 3952 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER); 3953 AddDeclRef(D, Record); 3954} 3955 3956void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, 3957 const ClassTemplateSpecializationDecl *D) { 3958 // The specializations set is kept in the canonical template. 3959 TD = TD->getCanonicalDecl(); 3960 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 3961 return; // Not a source specialization added to a template from PCH. 3962 3963 UpdateRecord &Record = DeclUpdates[TD]; 3964 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); 3965 AddDeclRef(D, Record); 3966} 3967 3968void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, 3969 const FunctionDecl *D) { 3970 // The specializations set is kept in the canonical template. 3971 TD = TD->getCanonicalDecl(); 3972 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 3973 return; // Not a source specialization added to a template from PCH. 3974 3975 UpdateRecord &Record = DeclUpdates[TD]; 3976 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); 3977 AddDeclRef(D, Record); 3978} 3979 3980void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) { 3981 if (!D->isFromASTFile()) 3982 return; // Declaration not imported from PCH. 3983 3984 // Implicit decl from a PCH was defined. 3985 // FIXME: Should implicit definition be a separate FunctionDecl? 3986 RewriteDecl(D); 3987} 3988 3989void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) { 3990 if (!D->isFromASTFile()) 3991 return; 3992 3993 // Since the actual instantiation is delayed, this really means that we need 3994 // to update the instantiation location. 3995 UpdateRecord &Record = DeclUpdates[D]; 3996 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER); 3997 AddSourceLocation( 3998 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record); 3999} 4000 4001void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, 4002 const ObjCInterfaceDecl *IFD) { 4003 if (!IFD->isFromASTFile()) 4004 return; // Declaration not imported from PCH. 4005 if (CatD->getNextClassCategory() && 4006 !CatD->getNextClassCategory()->isFromASTFile()) 4007 return; // We already recorded that the tail of a category chain should be 4008 // attached to an interface. 4009 4010 ChainedObjCCategoriesData Data = { IFD, GetDeclRef(IFD), GetDeclRef(CatD) }; 4011 LocalChainedObjCCategories.push_back(Data); 4012} 4013 4014ASTSerializationListener::~ASTSerializationListener() { } 4015