1 //===--- ASTWriter.cpp - AST File Writer ----------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ASTWriter class, which writes AST files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Serialization/ASTWriter.h" 15 #include "ASTCommon.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclContextInternals.h" 19 #include "clang/AST/DeclFriend.h" 20 #include "clang/AST/DeclLookups.h" 21 #include "clang/AST/DeclTemplate.h" 22 #include "clang/AST/Expr.h" 23 #include "clang/AST/ExprCXX.h" 24 #include "clang/AST/Type.h" 25 #include "clang/AST/TypeLocVisitor.h" 26 #include "clang/Basic/DiagnosticOptions.h" 27 #include "clang/Basic/FileManager.h" 28 #include "clang/Basic/FileSystemStatCache.h" 29 #include "clang/Basic/SourceManager.h" 30 #include "clang/Basic/SourceManagerInternals.h" 31 #include "clang/Basic/TargetInfo.h" 32 #include "clang/Basic/TargetOptions.h" 33 #include "clang/Basic/Version.h" 34 #include "clang/Basic/VersionTuple.h" 35 #include "clang/Lex/HeaderSearch.h" 36 #include "clang/Lex/HeaderSearchOptions.h" 37 #include "clang/Lex/MacroInfo.h" 38 #include "clang/Lex/PreprocessingRecord.h" 39 #include "clang/Lex/Preprocessor.h" 40 #include "clang/Lex/PreprocessorOptions.h" 41 #include "clang/Sema/IdentifierResolver.h" 42 #include "clang/Sema/Sema.h" 43 #include "clang/Serialization/ASTReader.h" 44 #include "llvm/ADT/APFloat.h" 45 #include "llvm/ADT/APInt.h" 46 #include "llvm/ADT/Hashing.h" 47 #include "llvm/ADT/StringExtras.h" 48 #include "llvm/Bitcode/BitstreamWriter.h" 49 #include "llvm/Support/EndianStream.h" 50 #include "llvm/Support/FileSystem.h" 51 #include "llvm/Support/MemoryBuffer.h" 52 #include "llvm/Support/OnDiskHashTable.h" 53 #include "llvm/Support/Path.h" 54 #include "llvm/Support/Process.h" 55 #include <algorithm> 56 #include <cstdio> 57 #include <string.h> 58 #include <utility> 59 using namespace clang; 60 using namespace clang::serialization; 61 62 template <typename T, typename Allocator> 63 static StringRef bytes(const std::vector<T, Allocator> &v) { 64 if (v.empty()) return StringRef(); 65 return StringRef(reinterpret_cast<const char*>(&v[0]), 66 sizeof(T) * v.size()); 67 } 68 69 template <typename T> 70 static StringRef bytes(const SmallVectorImpl<T> &v) { 71 return StringRef(reinterpret_cast<const char*>(v.data()), 72 sizeof(T) * v.size()); 73 } 74 75 //===----------------------------------------------------------------------===// 76 // Type serialization 77 //===----------------------------------------------------------------------===// 78 79 namespace { 80 class ASTTypeWriter { 81 ASTWriter &Writer; 82 ASTWriter::RecordDataImpl &Record; 83 84 public: 85 /// \brief Type code that corresponds to the record generated. 86 TypeCode Code; 87 /// \brief Abbreviation to use for the record, if any. 88 unsigned AbbrevToUse; 89 90 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 91 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } 92 93 void VisitArrayType(const ArrayType *T); 94 void VisitFunctionType(const FunctionType *T); 95 void VisitTagType(const TagType *T); 96 97 #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); 98 #define ABSTRACT_TYPE(Class, Base) 99 #include "clang/AST/TypeNodes.def" 100 }; 101 } 102 103 void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { 104 llvm_unreachable("Built-in types are never serialized"); 105 } 106 107 void ASTTypeWriter::VisitComplexType(const ComplexType *T) { 108 Writer.AddTypeRef(T->getElementType(), Record); 109 Code = TYPE_COMPLEX; 110 } 111 112 void ASTTypeWriter::VisitPointerType(const PointerType *T) { 113 Writer.AddTypeRef(T->getPointeeType(), Record); 114 Code = TYPE_POINTER; 115 } 116 117 void ASTTypeWriter::VisitDecayedType(const DecayedType *T) { 118 Writer.AddTypeRef(T->getOriginalType(), Record); 119 Code = TYPE_DECAYED; 120 } 121 122 void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) { 123 Writer.AddTypeRef(T->getOriginalType(), Record); 124 Writer.AddTypeRef(T->getAdjustedType(), Record); 125 Code = TYPE_ADJUSTED; 126 } 127 128 void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { 129 Writer.AddTypeRef(T->getPointeeType(), Record); 130 Code = TYPE_BLOCK_POINTER; 131 } 132 133 void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { 134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); 135 Record.push_back(T->isSpelledAsLValue()); 136 Code = TYPE_LVALUE_REFERENCE; 137 } 138 139 void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { 140 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); 141 Code = TYPE_RVALUE_REFERENCE; 142 } 143 144 void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { 145 Writer.AddTypeRef(T->getPointeeType(), Record); 146 Writer.AddTypeRef(QualType(T->getClass(), 0), Record); 147 Code = TYPE_MEMBER_POINTER; 148 } 149 150 void ASTTypeWriter::VisitArrayType(const ArrayType *T) { 151 Writer.AddTypeRef(T->getElementType(), Record); 152 Record.push_back(T->getSizeModifier()); // FIXME: stable values 153 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values 154 } 155 156 void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { 157 VisitArrayType(T); 158 Writer.AddAPInt(T->getSize(), Record); 159 Code = TYPE_CONSTANT_ARRAY; 160 } 161 162 void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 163 VisitArrayType(T); 164 Code = TYPE_INCOMPLETE_ARRAY; 165 } 166 167 void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { 168 VisitArrayType(T); 169 Writer.AddSourceLocation(T->getLBracketLoc(), Record); 170 Writer.AddSourceLocation(T->getRBracketLoc(), Record); 171 Writer.AddStmt(T->getSizeExpr()); 172 Code = TYPE_VARIABLE_ARRAY; 173 } 174 175 void ASTTypeWriter::VisitVectorType(const VectorType *T) { 176 Writer.AddTypeRef(T->getElementType(), Record); 177 Record.push_back(T->getNumElements()); 178 Record.push_back(T->getVectorKind()); 179 Code = TYPE_VECTOR; 180 } 181 182 void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { 183 VisitVectorType(T); 184 Code = TYPE_EXT_VECTOR; 185 } 186 187 void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { 188 Writer.AddTypeRef(T->getReturnType(), Record); 189 FunctionType::ExtInfo C = T->getExtInfo(); 190 Record.push_back(C.getNoReturn()); 191 Record.push_back(C.getHasRegParm()); 192 Record.push_back(C.getRegParm()); 193 // FIXME: need to stabilize encoding of calling convention... 194 Record.push_back(C.getCC()); 195 Record.push_back(C.getProducesResult()); 196 197 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult()) 198 AbbrevToUse = 0; 199 } 200 201 void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 202 VisitFunctionType(T); 203 Code = TYPE_FUNCTION_NO_PROTO; 204 } 205 206 static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T, 207 ASTWriter::RecordDataImpl &Record) { 208 Record.push_back(T->getExceptionSpecType()); 209 if (T->getExceptionSpecType() == EST_Dynamic) { 210 Record.push_back(T->getNumExceptions()); 211 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) 212 Writer.AddTypeRef(T->getExceptionType(I), Record); 213 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) { 214 Writer.AddStmt(T->getNoexceptExpr()); 215 } else if (T->getExceptionSpecType() == EST_Uninstantiated) { 216 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record); 217 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record); 218 } else if (T->getExceptionSpecType() == EST_Unevaluated) { 219 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record); 220 } 221 } 222 223 void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { 224 VisitFunctionType(T); 225 226 Record.push_back(T->isVariadic()); 227 Record.push_back(T->hasTrailingReturn()); 228 Record.push_back(T->getTypeQuals()); 229 Record.push_back(static_cast<unsigned>(T->getRefQualifier())); 230 addExceptionSpec(Writer, T, Record); 231 232 Record.push_back(T->getNumParams()); 233 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I) 234 Writer.AddTypeRef(T->getParamType(I), Record); 235 236 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() || 237 T->getRefQualifier() || T->getExceptionSpecType() != EST_None) 238 AbbrevToUse = 0; 239 240 Code = TYPE_FUNCTION_PROTO; 241 } 242 243 void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 244 Writer.AddDeclRef(T->getDecl(), Record); 245 Code = TYPE_UNRESOLVED_USING; 246 } 247 248 void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { 249 Writer.AddDeclRef(T->getDecl(), Record); 250 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); 251 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); 252 Code = TYPE_TYPEDEF; 253 } 254 255 void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { 256 Writer.AddStmt(T->getUnderlyingExpr()); 257 Code = TYPE_TYPEOF_EXPR; 258 } 259 260 void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { 261 Writer.AddTypeRef(T->getUnderlyingType(), Record); 262 Code = TYPE_TYPEOF; 263 } 264 265 void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { 266 Writer.AddTypeRef(T->getUnderlyingType(), Record); 267 Writer.AddStmt(T->getUnderlyingExpr()); 268 Code = TYPE_DECLTYPE; 269 } 270 271 void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) { 272 Writer.AddTypeRef(T->getBaseType(), Record); 273 Writer.AddTypeRef(T->getUnderlyingType(), Record); 274 Record.push_back(T->getUTTKind()); 275 Code = TYPE_UNARY_TRANSFORM; 276 } 277 278 void ASTTypeWriter::VisitAutoType(const AutoType *T) { 279 Writer.AddTypeRef(T->getDeducedType(), Record); 280 Record.push_back(T->isDecltypeAuto()); 281 if (T->getDeducedType().isNull()) 282 Record.push_back(T->isDependentType()); 283 Code = TYPE_AUTO; 284 } 285 286 void ASTTypeWriter::VisitTagType(const TagType *T) { 287 Record.push_back(T->isDependentType()); 288 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); 289 assert(!T->isBeingDefined() && 290 "Cannot serialize in the middle of a type definition"); 291 } 292 293 void ASTTypeWriter::VisitRecordType(const RecordType *T) { 294 VisitTagType(T); 295 Code = TYPE_RECORD; 296 } 297 298 void ASTTypeWriter::VisitEnumType(const EnumType *T) { 299 VisitTagType(T); 300 Code = TYPE_ENUM; 301 } 302 303 void ASTTypeWriter::VisitAttributedType(const AttributedType *T) { 304 Writer.AddTypeRef(T->getModifiedType(), Record); 305 Writer.AddTypeRef(T->getEquivalentType(), Record); 306 Record.push_back(T->getAttrKind()); 307 Code = TYPE_ATTRIBUTED; 308 } 309 310 void 311 ASTTypeWriter::VisitSubstTemplateTypeParmType( 312 const SubstTemplateTypeParmType *T) { 313 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 314 Writer.AddTypeRef(T->getReplacementType(), Record); 315 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; 316 } 317 318 void 319 ASTTypeWriter::VisitSubstTemplateTypeParmPackType( 320 const SubstTemplateTypeParmPackType *T) { 321 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); 322 Writer.AddTemplateArgument(T->getArgumentPack(), Record); 323 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK; 324 } 325 326 void 327 ASTTypeWriter::VisitTemplateSpecializationType( 328 const TemplateSpecializationType *T) { 329 Record.push_back(T->isDependentType()); 330 Writer.AddTemplateName(T->getTemplateName(), Record); 331 Record.push_back(T->getNumArgs()); 332 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); 333 ArgI != ArgE; ++ArgI) 334 Writer.AddTemplateArgument(*ArgI, Record); 335 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() : 336 T->isCanonicalUnqualified() ? QualType() 337 : T->getCanonicalTypeInternal(), 338 Record); 339 Code = TYPE_TEMPLATE_SPECIALIZATION; 340 } 341 342 void 343 ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { 344 VisitArrayType(T); 345 Writer.AddStmt(T->getSizeExpr()); 346 Writer.AddSourceRange(T->getBracketsRange(), Record); 347 Code = TYPE_DEPENDENT_SIZED_ARRAY; 348 } 349 350 void 351 ASTTypeWriter::VisitDependentSizedExtVectorType( 352 const DependentSizedExtVectorType *T) { 353 // FIXME: Serialize this type (C++ only) 354 llvm_unreachable("Cannot serialize dependent sized extended vector types"); 355 } 356 357 void 358 ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 359 Record.push_back(T->getDepth()); 360 Record.push_back(T->getIndex()); 361 Record.push_back(T->isParameterPack()); 362 Writer.AddDeclRef(T->getDecl(), Record); 363 Code = TYPE_TEMPLATE_TYPE_PARM; 364 } 365 366 void 367 ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { 368 Record.push_back(T->getKeyword()); 369 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 370 Writer.AddIdentifierRef(T->getIdentifier(), Record); 371 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() 372 : T->getCanonicalTypeInternal(), 373 Record); 374 Code = TYPE_DEPENDENT_NAME; 375 } 376 377 void 378 ASTTypeWriter::VisitDependentTemplateSpecializationType( 379 const DependentTemplateSpecializationType *T) { 380 Record.push_back(T->getKeyword()); 381 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 382 Writer.AddIdentifierRef(T->getIdentifier(), Record); 383 Record.push_back(T->getNumArgs()); 384 for (DependentTemplateSpecializationType::iterator 385 I = T->begin(), E = T->end(); I != E; ++I) 386 Writer.AddTemplateArgument(*I, Record); 387 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; 388 } 389 390 void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) { 391 Writer.AddTypeRef(T->getPattern(), Record); 392 if (Optional<unsigned> NumExpansions = T->getNumExpansions()) 393 Record.push_back(*NumExpansions + 1); 394 else 395 Record.push_back(0); 396 Code = TYPE_PACK_EXPANSION; 397 } 398 399 void ASTTypeWriter::VisitParenType(const ParenType *T) { 400 Writer.AddTypeRef(T->getInnerType(), Record); 401 Code = TYPE_PAREN; 402 } 403 404 void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { 405 Record.push_back(T->getKeyword()); 406 Writer.AddNestedNameSpecifier(T->getQualifier(), Record); 407 Writer.AddTypeRef(T->getNamedType(), Record); 408 Code = TYPE_ELABORATED; 409 } 410 411 void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { 412 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); 413 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); 414 Code = TYPE_INJECTED_CLASS_NAME; 415 } 416 417 void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 418 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); 419 Code = TYPE_OBJC_INTERFACE; 420 } 421 422 void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { 423 Writer.AddTypeRef(T->getBaseType(), Record); 424 Record.push_back(T->getTypeArgsAsWritten().size()); 425 for (auto TypeArg : T->getTypeArgsAsWritten()) 426 Writer.AddTypeRef(TypeArg, Record); 427 Record.push_back(T->getNumProtocols()); 428 for (const auto *I : T->quals()) 429 Writer.AddDeclRef(I, Record); 430 Record.push_back(T->isKindOfTypeAsWritten()); 431 Code = TYPE_OBJC_OBJECT; 432 } 433 434 void 435 ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 436 Writer.AddTypeRef(T->getPointeeType(), Record); 437 Code = TYPE_OBJC_OBJECT_POINTER; 438 } 439 440 void 441 ASTTypeWriter::VisitAtomicType(const AtomicType *T) { 442 Writer.AddTypeRef(T->getValueType(), Record); 443 Code = TYPE_ATOMIC; 444 } 445 446 namespace { 447 448 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { 449 ASTWriter &Writer; 450 ASTWriter::RecordDataImpl &Record; 451 452 public: 453 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 454 : Writer(Writer), Record(Record) { } 455 456 #define ABSTRACT_TYPELOC(CLASS, PARENT) 457 #define TYPELOC(CLASS, PARENT) \ 458 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 459 #include "clang/AST/TypeLocNodes.def" 460 461 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); 462 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); 463 }; 464 465 } 466 467 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 468 // nothing to do 469 } 470 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 471 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); 472 if (TL.needsExtraLocalData()) { 473 Record.push_back(TL.getWrittenTypeSpec()); 474 Record.push_back(TL.getWrittenSignSpec()); 475 Record.push_back(TL.getWrittenWidthSpec()); 476 Record.push_back(TL.hasModeAttr()); 477 } 478 } 479 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { 480 Writer.AddSourceLocation(TL.getNameLoc(), Record); 481 } 482 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { 483 Writer.AddSourceLocation(TL.getStarLoc(), Record); 484 } 485 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 486 // nothing to do 487 } 488 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 489 // nothing to do 490 } 491 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 492 Writer.AddSourceLocation(TL.getCaretLoc(), Record); 493 } 494 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 495 Writer.AddSourceLocation(TL.getAmpLoc(), Record); 496 } 497 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 498 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); 499 } 500 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 501 Writer.AddSourceLocation(TL.getStarLoc(), Record); 502 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record); 503 } 504 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { 505 Writer.AddSourceLocation(TL.getLBracketLoc(), Record); 506 Writer.AddSourceLocation(TL.getRBracketLoc(), Record); 507 Record.push_back(TL.getSizeExpr() ? 1 : 0); 508 if (TL.getSizeExpr()) 509 Writer.AddStmt(TL.getSizeExpr()); 510 } 511 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 512 VisitArrayTypeLoc(TL); 513 } 514 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 515 VisitArrayTypeLoc(TL); 516 } 517 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 518 VisitArrayTypeLoc(TL); 519 } 520 void TypeLocWriter::VisitDependentSizedArrayTypeLoc( 521 DependentSizedArrayTypeLoc TL) { 522 VisitArrayTypeLoc(TL); 523 } 524 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( 525 DependentSizedExtVectorTypeLoc TL) { 526 Writer.AddSourceLocation(TL.getNameLoc(), Record); 527 } 528 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { 529 Writer.AddSourceLocation(TL.getNameLoc(), Record); 530 } 531 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 532 Writer.AddSourceLocation(TL.getNameLoc(), Record); 533 } 534 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 535 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record); 536 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 537 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 538 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record); 539 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) 540 Writer.AddDeclRef(TL.getParam(i), Record); 541 } 542 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 543 VisitFunctionTypeLoc(TL); 544 } 545 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 546 VisitFunctionTypeLoc(TL); 547 } 548 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 549 Writer.AddSourceLocation(TL.getNameLoc(), Record); 550 } 551 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 552 Writer.AddSourceLocation(TL.getNameLoc(), Record); 553 } 554 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 555 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 556 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 557 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 558 } 559 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 560 Writer.AddSourceLocation(TL.getTypeofLoc(), Record); 561 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 562 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 563 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 564 } 565 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 566 Writer.AddSourceLocation(TL.getNameLoc(), Record); 567 } 568 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 569 Writer.AddSourceLocation(TL.getKWLoc(), Record); 570 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 571 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 572 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); 573 } 574 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { 575 Writer.AddSourceLocation(TL.getNameLoc(), Record); 576 } 577 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { 578 Writer.AddSourceLocation(TL.getNameLoc(), Record); 579 } 580 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { 581 Writer.AddSourceLocation(TL.getNameLoc(), Record); 582 } 583 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 584 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record); 585 if (TL.hasAttrOperand()) { 586 SourceRange range = TL.getAttrOperandParensRange(); 587 Writer.AddSourceLocation(range.getBegin(), Record); 588 Writer.AddSourceLocation(range.getEnd(), Record); 589 } 590 if (TL.hasAttrExprOperand()) { 591 Expr *operand = TL.getAttrExprOperand(); 592 Record.push_back(operand ? 1 : 0); 593 if (operand) Writer.AddStmt(operand); 594 } else if (TL.hasAttrEnumOperand()) { 595 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record); 596 } 597 } 598 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 599 Writer.AddSourceLocation(TL.getNameLoc(), Record); 600 } 601 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( 602 SubstTemplateTypeParmTypeLoc TL) { 603 Writer.AddSourceLocation(TL.getNameLoc(), Record); 604 } 605 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( 606 SubstTemplateTypeParmPackTypeLoc TL) { 607 Writer.AddSourceLocation(TL.getNameLoc(), Record); 608 } 609 void TypeLocWriter::VisitTemplateSpecializationTypeLoc( 610 TemplateSpecializationTypeLoc TL) { 611 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); 612 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 613 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 614 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 615 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 616 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), 617 TL.getArgLoc(i).getLocInfo(), Record); 618 } 619 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { 620 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 621 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 622 } 623 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 624 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); 625 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 626 } 627 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 628 Writer.AddSourceLocation(TL.getNameLoc(), Record); 629 } 630 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 631 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); 632 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 633 Writer.AddSourceLocation(TL.getNameLoc(), Record); 634 } 635 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( 636 DependentTemplateSpecializationTypeLoc TL) { 637 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); 638 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); 639 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); 640 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); 641 Writer.AddSourceLocation(TL.getLAngleLoc(), Record); 642 Writer.AddSourceLocation(TL.getRAngleLoc(), Record); 643 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 644 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), 645 TL.getArgLoc(I).getLocInfo(), Record); 646 } 647 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 648 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record); 649 } 650 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 651 Writer.AddSourceLocation(TL.getNameLoc(), Record); 652 } 653 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 654 Record.push_back(TL.hasBaseTypeAsWritten()); 655 Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record); 656 Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record); 657 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 658 Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record); 659 Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record); 660 Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record); 661 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 662 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); 663 } 664 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 665 Writer.AddSourceLocation(TL.getStarLoc(), Record); 666 } 667 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 668 Writer.AddSourceLocation(TL.getKWLoc(), Record); 669 Writer.AddSourceLocation(TL.getLParenLoc(), Record); 670 Writer.AddSourceLocation(TL.getRParenLoc(), Record); 671 } 672 673 void ASTWriter::WriteTypeAbbrevs() { 674 using namespace llvm; 675 676 BitCodeAbbrev *Abv; 677 678 // Abbreviation for TYPE_EXT_QUAL 679 Abv = new BitCodeAbbrev(); 680 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL)); 681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type 682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals 683 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv); 684 685 // Abbreviation for TYPE_FUNCTION_PROTO 686 Abv = new BitCodeAbbrev(); 687 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO)); 688 // FunctionType 689 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType 690 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn 691 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm 692 Abv->Add(BitCodeAbbrevOp(0)); // RegParm 693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC 694 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult 695 // FunctionProtoType 696 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic 697 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn 698 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals 699 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier 700 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec 701 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams 702 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 703 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params 704 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv); 705 } 706 707 //===----------------------------------------------------------------------===// 708 // ASTWriter Implementation 709 //===----------------------------------------------------------------------===// 710 711 static void EmitBlockID(unsigned ID, const char *Name, 712 llvm::BitstreamWriter &Stream, 713 ASTWriter::RecordDataImpl &Record) { 714 Record.clear(); 715 Record.push_back(ID); 716 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 717 718 // Emit the block name if present. 719 if (!Name || Name[0] == 0) 720 return; 721 Record.clear(); 722 while (*Name) 723 Record.push_back(*Name++); 724 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 725 } 726 727 static void EmitRecordID(unsigned ID, const char *Name, 728 llvm::BitstreamWriter &Stream, 729 ASTWriter::RecordDataImpl &Record) { 730 Record.clear(); 731 Record.push_back(ID); 732 while (*Name) 733 Record.push_back(*Name++); 734 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 735 } 736 737 static void AddStmtsExprs(llvm::BitstreamWriter &Stream, 738 ASTWriter::RecordDataImpl &Record) { 739 #define RECORD(X) EmitRecordID(X, #X, Stream, Record) 740 RECORD(STMT_STOP); 741 RECORD(STMT_NULL_PTR); 742 RECORD(STMT_REF_PTR); 743 RECORD(STMT_NULL); 744 RECORD(STMT_COMPOUND); 745 RECORD(STMT_CASE); 746 RECORD(STMT_DEFAULT); 747 RECORD(STMT_LABEL); 748 RECORD(STMT_ATTRIBUTED); 749 RECORD(STMT_IF); 750 RECORD(STMT_SWITCH); 751 RECORD(STMT_WHILE); 752 RECORD(STMT_DO); 753 RECORD(STMT_FOR); 754 RECORD(STMT_GOTO); 755 RECORD(STMT_INDIRECT_GOTO); 756 RECORD(STMT_CONTINUE); 757 RECORD(STMT_BREAK); 758 RECORD(STMT_RETURN); 759 RECORD(STMT_DECL); 760 RECORD(STMT_GCCASM); 761 RECORD(STMT_MSASM); 762 RECORD(EXPR_PREDEFINED); 763 RECORD(EXPR_DECL_REF); 764 RECORD(EXPR_INTEGER_LITERAL); 765 RECORD(EXPR_FLOATING_LITERAL); 766 RECORD(EXPR_IMAGINARY_LITERAL); 767 RECORD(EXPR_STRING_LITERAL); 768 RECORD(EXPR_CHARACTER_LITERAL); 769 RECORD(EXPR_PAREN); 770 RECORD(EXPR_PAREN_LIST); 771 RECORD(EXPR_UNARY_OPERATOR); 772 RECORD(EXPR_SIZEOF_ALIGN_OF); 773 RECORD(EXPR_ARRAY_SUBSCRIPT); 774 RECORD(EXPR_CALL); 775 RECORD(EXPR_MEMBER); 776 RECORD(EXPR_BINARY_OPERATOR); 777 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); 778 RECORD(EXPR_CONDITIONAL_OPERATOR); 779 RECORD(EXPR_IMPLICIT_CAST); 780 RECORD(EXPR_CSTYLE_CAST); 781 RECORD(EXPR_COMPOUND_LITERAL); 782 RECORD(EXPR_EXT_VECTOR_ELEMENT); 783 RECORD(EXPR_INIT_LIST); 784 RECORD(EXPR_DESIGNATED_INIT); 785 RECORD(EXPR_DESIGNATED_INIT_UPDATE); 786 RECORD(EXPR_IMPLICIT_VALUE_INIT); 787 RECORD(EXPR_NO_INIT); 788 RECORD(EXPR_VA_ARG); 789 RECORD(EXPR_ADDR_LABEL); 790 RECORD(EXPR_STMT); 791 RECORD(EXPR_CHOOSE); 792 RECORD(EXPR_GNU_NULL); 793 RECORD(EXPR_SHUFFLE_VECTOR); 794 RECORD(EXPR_BLOCK); 795 RECORD(EXPR_GENERIC_SELECTION); 796 RECORD(EXPR_OBJC_STRING_LITERAL); 797 RECORD(EXPR_OBJC_BOXED_EXPRESSION); 798 RECORD(EXPR_OBJC_ARRAY_LITERAL); 799 RECORD(EXPR_OBJC_DICTIONARY_LITERAL); 800 RECORD(EXPR_OBJC_ENCODE); 801 RECORD(EXPR_OBJC_SELECTOR_EXPR); 802 RECORD(EXPR_OBJC_PROTOCOL_EXPR); 803 RECORD(EXPR_OBJC_IVAR_REF_EXPR); 804 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); 805 RECORD(EXPR_OBJC_KVC_REF_EXPR); 806 RECORD(EXPR_OBJC_MESSAGE_EXPR); 807 RECORD(STMT_OBJC_FOR_COLLECTION); 808 RECORD(STMT_OBJC_CATCH); 809 RECORD(STMT_OBJC_FINALLY); 810 RECORD(STMT_OBJC_AT_TRY); 811 RECORD(STMT_OBJC_AT_SYNCHRONIZED); 812 RECORD(STMT_OBJC_AT_THROW); 813 RECORD(EXPR_OBJC_BOOL_LITERAL); 814 RECORD(STMT_CXX_CATCH); 815 RECORD(STMT_CXX_TRY); 816 RECORD(STMT_CXX_FOR_RANGE); 817 RECORD(EXPR_CXX_OPERATOR_CALL); 818 RECORD(EXPR_CXX_MEMBER_CALL); 819 RECORD(EXPR_CXX_CONSTRUCT); 820 RECORD(EXPR_CXX_TEMPORARY_OBJECT); 821 RECORD(EXPR_CXX_STATIC_CAST); 822 RECORD(EXPR_CXX_DYNAMIC_CAST); 823 RECORD(EXPR_CXX_REINTERPRET_CAST); 824 RECORD(EXPR_CXX_CONST_CAST); 825 RECORD(EXPR_CXX_FUNCTIONAL_CAST); 826 RECORD(EXPR_USER_DEFINED_LITERAL); 827 RECORD(EXPR_CXX_STD_INITIALIZER_LIST); 828 RECORD(EXPR_CXX_BOOL_LITERAL); 829 RECORD(EXPR_CXX_NULL_PTR_LITERAL); 830 RECORD(EXPR_CXX_TYPEID_EXPR); 831 RECORD(EXPR_CXX_TYPEID_TYPE); 832 RECORD(EXPR_CXX_THIS); 833 RECORD(EXPR_CXX_THROW); 834 RECORD(EXPR_CXX_DEFAULT_ARG); 835 RECORD(EXPR_CXX_DEFAULT_INIT); 836 RECORD(EXPR_CXX_BIND_TEMPORARY); 837 RECORD(EXPR_CXX_SCALAR_VALUE_INIT); 838 RECORD(EXPR_CXX_NEW); 839 RECORD(EXPR_CXX_DELETE); 840 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR); 841 RECORD(EXPR_EXPR_WITH_CLEANUPS); 842 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER); 843 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF); 844 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT); 845 RECORD(EXPR_CXX_UNRESOLVED_MEMBER); 846 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP); 847 RECORD(EXPR_CXX_EXPRESSION_TRAIT); 848 RECORD(EXPR_CXX_NOEXCEPT); 849 RECORD(EXPR_OPAQUE_VALUE); 850 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR); 851 RECORD(EXPR_TYPE_TRAIT); 852 RECORD(EXPR_ARRAY_TYPE_TRAIT); 853 RECORD(EXPR_PACK_EXPANSION); 854 RECORD(EXPR_SIZEOF_PACK); 855 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM); 856 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK); 857 RECORD(EXPR_FUNCTION_PARM_PACK); 858 RECORD(EXPR_MATERIALIZE_TEMPORARY); 859 RECORD(EXPR_CUDA_KERNEL_CALL); 860 RECORD(EXPR_CXX_UUIDOF_EXPR); 861 RECORD(EXPR_CXX_UUIDOF_TYPE); 862 RECORD(EXPR_LAMBDA); 863 #undef RECORD 864 } 865 866 void ASTWriter::WriteBlockInfoBlock() { 867 RecordData Record; 868 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); 869 870 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) 871 #define RECORD(X) EmitRecordID(X, #X, Stream, Record) 872 873 // Control Block. 874 BLOCK(CONTROL_BLOCK); 875 RECORD(METADATA); 876 RECORD(SIGNATURE); 877 RECORD(MODULE_NAME); 878 RECORD(MODULE_MAP_FILE); 879 RECORD(IMPORTS); 880 RECORD(LANGUAGE_OPTIONS); 881 RECORD(TARGET_OPTIONS); 882 RECORD(ORIGINAL_FILE); 883 RECORD(ORIGINAL_PCH_DIR); 884 RECORD(ORIGINAL_FILE_ID); 885 RECORD(INPUT_FILE_OFFSETS); 886 RECORD(DIAGNOSTIC_OPTIONS); 887 RECORD(FILE_SYSTEM_OPTIONS); 888 RECORD(HEADER_SEARCH_OPTIONS); 889 RECORD(PREPROCESSOR_OPTIONS); 890 891 BLOCK(INPUT_FILES_BLOCK); 892 RECORD(INPUT_FILE); 893 894 // AST Top-Level Block. 895 BLOCK(AST_BLOCK); 896 RECORD(TYPE_OFFSET); 897 RECORD(DECL_OFFSET); 898 RECORD(IDENTIFIER_OFFSET); 899 RECORD(IDENTIFIER_TABLE); 900 RECORD(EAGERLY_DESERIALIZED_DECLS); 901 RECORD(SPECIAL_TYPES); 902 RECORD(STATISTICS); 903 RECORD(TENTATIVE_DEFINITIONS); 904 RECORD(UNUSED_FILESCOPED_DECLS); 905 RECORD(SELECTOR_OFFSETS); 906 RECORD(METHOD_POOL); 907 RECORD(PP_COUNTER_VALUE); 908 RECORD(SOURCE_LOCATION_OFFSETS); 909 RECORD(SOURCE_LOCATION_PRELOADS); 910 RECORD(EXT_VECTOR_DECLS); 911 RECORD(PPD_ENTITIES_OFFSETS); 912 RECORD(REFERENCED_SELECTOR_POOL); 913 RECORD(TU_UPDATE_LEXICAL); 914 RECORD(LOCAL_REDECLARATIONS_MAP); 915 RECORD(SEMA_DECL_REFS); 916 RECORD(WEAK_UNDECLARED_IDENTIFIERS); 917 RECORD(PENDING_IMPLICIT_INSTANTIATIONS); 918 RECORD(DECL_REPLACEMENTS); 919 RECORD(UPDATE_VISIBLE); 920 RECORD(DECL_UPDATE_OFFSETS); 921 RECORD(DECL_UPDATES); 922 RECORD(CXX_BASE_SPECIFIER_OFFSETS); 923 RECORD(DIAG_PRAGMA_MAPPINGS); 924 RECORD(CUDA_SPECIAL_DECL_REFS); 925 RECORD(HEADER_SEARCH_TABLE); 926 RECORD(FP_PRAGMA_OPTIONS); 927 RECORD(OPENCL_EXTENSIONS); 928 RECORD(DELEGATING_CTORS); 929 RECORD(KNOWN_NAMESPACES); 930 RECORD(UNDEFINED_BUT_USED); 931 RECORD(MODULE_OFFSET_MAP); 932 RECORD(SOURCE_MANAGER_LINE_TABLE); 933 RECORD(OBJC_CATEGORIES_MAP); 934 RECORD(FILE_SORTED_DECLS); 935 RECORD(IMPORTED_MODULES); 936 RECORD(LOCAL_REDECLARATIONS); 937 RECORD(OBJC_CATEGORIES); 938 RECORD(MACRO_OFFSET); 939 RECORD(LATE_PARSED_TEMPLATE); 940 RECORD(OPTIMIZE_PRAGMA_OPTIONS); 941 942 // SourceManager Block. 943 BLOCK(SOURCE_MANAGER_BLOCK); 944 RECORD(SM_SLOC_FILE_ENTRY); 945 RECORD(SM_SLOC_BUFFER_ENTRY); 946 RECORD(SM_SLOC_BUFFER_BLOB); 947 RECORD(SM_SLOC_EXPANSION_ENTRY); 948 949 // Preprocessor Block. 950 BLOCK(PREPROCESSOR_BLOCK); 951 RECORD(PP_MACRO_DIRECTIVE_HISTORY); 952 RECORD(PP_MACRO_FUNCTION_LIKE); 953 RECORD(PP_MACRO_OBJECT_LIKE); 954 RECORD(PP_MODULE_MACRO); 955 RECORD(PP_TOKEN); 956 957 // Decls and Types block. 958 BLOCK(DECLTYPES_BLOCK); 959 RECORD(TYPE_EXT_QUAL); 960 RECORD(TYPE_COMPLEX); 961 RECORD(TYPE_POINTER); 962 RECORD(TYPE_BLOCK_POINTER); 963 RECORD(TYPE_LVALUE_REFERENCE); 964 RECORD(TYPE_RVALUE_REFERENCE); 965 RECORD(TYPE_MEMBER_POINTER); 966 RECORD(TYPE_CONSTANT_ARRAY); 967 RECORD(TYPE_INCOMPLETE_ARRAY); 968 RECORD(TYPE_VARIABLE_ARRAY); 969 RECORD(TYPE_VECTOR); 970 RECORD(TYPE_EXT_VECTOR); 971 RECORD(TYPE_FUNCTION_NO_PROTO); 972 RECORD(TYPE_FUNCTION_PROTO); 973 RECORD(TYPE_TYPEDEF); 974 RECORD(TYPE_TYPEOF_EXPR); 975 RECORD(TYPE_TYPEOF); 976 RECORD(TYPE_RECORD); 977 RECORD(TYPE_ENUM); 978 RECORD(TYPE_OBJC_INTERFACE); 979 RECORD(TYPE_OBJC_OBJECT_POINTER); 980 RECORD(TYPE_DECLTYPE); 981 RECORD(TYPE_ELABORATED); 982 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM); 983 RECORD(TYPE_UNRESOLVED_USING); 984 RECORD(TYPE_INJECTED_CLASS_NAME); 985 RECORD(TYPE_OBJC_OBJECT); 986 RECORD(TYPE_TEMPLATE_TYPE_PARM); 987 RECORD(TYPE_TEMPLATE_SPECIALIZATION); 988 RECORD(TYPE_DEPENDENT_NAME); 989 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); 990 RECORD(TYPE_DEPENDENT_SIZED_ARRAY); 991 RECORD(TYPE_PAREN); 992 RECORD(TYPE_PACK_EXPANSION); 993 RECORD(TYPE_ATTRIBUTED); 994 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); 995 RECORD(TYPE_AUTO); 996 RECORD(TYPE_UNARY_TRANSFORM); 997 RECORD(TYPE_ATOMIC); 998 RECORD(TYPE_DECAYED); 999 RECORD(TYPE_ADJUSTED); 1000 RECORD(DECL_TYPEDEF); 1001 RECORD(DECL_TYPEALIAS); 1002 RECORD(DECL_ENUM); 1003 RECORD(DECL_RECORD); 1004 RECORD(DECL_ENUM_CONSTANT); 1005 RECORD(DECL_FUNCTION); 1006 RECORD(DECL_OBJC_METHOD); 1007 RECORD(DECL_OBJC_INTERFACE); 1008 RECORD(DECL_OBJC_PROTOCOL); 1009 RECORD(DECL_OBJC_IVAR); 1010 RECORD(DECL_OBJC_AT_DEFS_FIELD); 1011 RECORD(DECL_OBJC_CATEGORY); 1012 RECORD(DECL_OBJC_CATEGORY_IMPL); 1013 RECORD(DECL_OBJC_IMPLEMENTATION); 1014 RECORD(DECL_OBJC_COMPATIBLE_ALIAS); 1015 RECORD(DECL_OBJC_PROPERTY); 1016 RECORD(DECL_OBJC_PROPERTY_IMPL); 1017 RECORD(DECL_FIELD); 1018 RECORD(DECL_MS_PROPERTY); 1019 RECORD(DECL_VAR); 1020 RECORD(DECL_IMPLICIT_PARAM); 1021 RECORD(DECL_PARM_VAR); 1022 RECORD(DECL_FILE_SCOPE_ASM); 1023 RECORD(DECL_BLOCK); 1024 RECORD(DECL_CONTEXT_LEXICAL); 1025 RECORD(DECL_CONTEXT_VISIBLE); 1026 RECORD(DECL_NAMESPACE); 1027 RECORD(DECL_NAMESPACE_ALIAS); 1028 RECORD(DECL_USING); 1029 RECORD(DECL_USING_SHADOW); 1030 RECORD(DECL_USING_DIRECTIVE); 1031 RECORD(DECL_UNRESOLVED_USING_VALUE); 1032 RECORD(DECL_UNRESOLVED_USING_TYPENAME); 1033 RECORD(DECL_LINKAGE_SPEC); 1034 RECORD(DECL_CXX_RECORD); 1035 RECORD(DECL_CXX_METHOD); 1036 RECORD(DECL_CXX_CONSTRUCTOR); 1037 RECORD(DECL_CXX_DESTRUCTOR); 1038 RECORD(DECL_CXX_CONVERSION); 1039 RECORD(DECL_ACCESS_SPEC); 1040 RECORD(DECL_FRIEND); 1041 RECORD(DECL_FRIEND_TEMPLATE); 1042 RECORD(DECL_CLASS_TEMPLATE); 1043 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION); 1044 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION); 1045 RECORD(DECL_VAR_TEMPLATE); 1046 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION); 1047 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION); 1048 RECORD(DECL_FUNCTION_TEMPLATE); 1049 RECORD(DECL_TEMPLATE_TYPE_PARM); 1050 RECORD(DECL_NON_TYPE_TEMPLATE_PARM); 1051 RECORD(DECL_TEMPLATE_TEMPLATE_PARM); 1052 RECORD(DECL_STATIC_ASSERT); 1053 RECORD(DECL_CXX_BASE_SPECIFIERS); 1054 RECORD(DECL_INDIRECTFIELD); 1055 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK); 1056 1057 // Statements and Exprs can occur in the Decls and Types block. 1058 AddStmtsExprs(Stream, Record); 1059 1060 BLOCK(PREPROCESSOR_DETAIL_BLOCK); 1061 RECORD(PPD_MACRO_EXPANSION); 1062 RECORD(PPD_MACRO_DEFINITION); 1063 RECORD(PPD_INCLUSION_DIRECTIVE); 1064 1065 #undef RECORD 1066 #undef BLOCK 1067 Stream.ExitBlock(); 1068 } 1069 1070 /// \brief Prepares a path for being written to an AST file by converting it 1071 /// to an absolute path and removing nested './'s. 1072 /// 1073 /// \return \c true if the path was changed. 1074 static bool cleanPathForOutput(FileManager &FileMgr, 1075 SmallVectorImpl<char> &Path) { 1076 bool Changed = FileMgr.makeAbsolutePath(Path); 1077 return Changed | FileMgr.removeDotPaths(Path); 1078 } 1079 1080 /// \brief Adjusts the given filename to only write out the portion of the 1081 /// filename that is not part of the system root directory. 1082 /// 1083 /// \param Filename the file name to adjust. 1084 /// 1085 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and 1086 /// the returned filename will be adjusted by this root directory. 1087 /// 1088 /// \returns either the original filename (if it needs no adjustment) or the 1089 /// adjusted filename (which points into the @p Filename parameter). 1090 static const char * 1091 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) { 1092 assert(Filename && "No file name to adjust?"); 1093 1094 if (BaseDir.empty()) 1095 return Filename; 1096 1097 // Verify that the filename and the system root have the same prefix. 1098 unsigned Pos = 0; 1099 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos) 1100 if (Filename[Pos] != BaseDir[Pos]) 1101 return Filename; // Prefixes don't match. 1102 1103 // We hit the end of the filename before we hit the end of the system root. 1104 if (!Filename[Pos]) 1105 return Filename; 1106 1107 // If there's not a path separator at the end of the base directory nor 1108 // immediately after it, then this isn't within the base directory. 1109 if (!llvm::sys::path::is_separator(Filename[Pos])) { 1110 if (!llvm::sys::path::is_separator(BaseDir.back())) 1111 return Filename; 1112 } else { 1113 // If the file name has a '/' at the current position, skip over the '/'. 1114 // We distinguish relative paths from absolute paths by the 1115 // absence of '/' at the beginning of relative paths. 1116 // 1117 // FIXME: This is wrong. We distinguish them by asking if the path is 1118 // absolute, which isn't the same thing. And there might be multiple '/'s 1119 // in a row. Use a better mechanism to indicate whether we have emitted an 1120 // absolute or relative path. 1121 ++Pos; 1122 } 1123 1124 return Filename + Pos; 1125 } 1126 1127 static ASTFileSignature getSignature() { 1128 while (1) { 1129 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber()) 1130 return S; 1131 // Rely on GetRandomNumber to eventually return non-zero... 1132 } 1133 } 1134 1135 /// \brief Write the control block. 1136 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context, 1137 StringRef isysroot, 1138 const std::string &OutputFile) { 1139 using namespace llvm; 1140 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5); 1141 RecordData Record; 1142 1143 // Metadata 1144 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev(); 1145 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA)); 1146 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major 1147 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor 1148 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj. 1149 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min. 1150 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable 1151 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors 1152 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag 1153 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev); 1154 Record.push_back(METADATA); 1155 Record.push_back(VERSION_MAJOR); 1156 Record.push_back(VERSION_MINOR); 1157 Record.push_back(CLANG_VERSION_MAJOR); 1158 Record.push_back(CLANG_VERSION_MINOR); 1159 assert((!WritingModule || isysroot.empty()) && 1160 "writing module as a relocatable PCH?"); 1161 Record.push_back(!isysroot.empty()); 1162 Record.push_back(ASTHasCompilerErrors); 1163 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record, 1164 getClangFullRepositoryVersion()); 1165 1166 if (WritingModule) { 1167 // For implicit modules we output a signature that we can use to ensure 1168 // duplicate module builds don't collide in the cache as their output order 1169 // is non-deterministic. 1170 // FIXME: Remove this when output is deterministic. 1171 if (Context.getLangOpts().ImplicitModules) { 1172 Record.clear(); 1173 Record.push_back(getSignature()); 1174 Stream.EmitRecord(SIGNATURE, Record); 1175 } 1176 1177 // Module name 1178 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1179 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME)); 1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 1181 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 1182 RecordData Record; 1183 Record.push_back(MODULE_NAME); 1184 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name); 1185 } 1186 1187 if (WritingModule && WritingModule->Directory) { 1188 // Module directory. 1189 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1190 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY)); 1191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory 1192 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 1193 RecordData Record; 1194 Record.push_back(MODULE_DIRECTORY); 1195 1196 SmallString<128> BaseDir(WritingModule->Directory->getName()); 1197 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir); 1198 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir); 1199 1200 // Write out all other paths relative to the base directory if possible. 1201 BaseDirectory.assign(BaseDir.begin(), BaseDir.end()); 1202 } else if (!isysroot.empty()) { 1203 // Write out paths relative to the sysroot if possible. 1204 BaseDirectory = isysroot; 1205 } 1206 1207 // Module map file 1208 if (WritingModule) { 1209 Record.clear(); 1210 1211 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 1212 1213 // Primary module map file. 1214 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record); 1215 1216 // Additional module map files. 1217 if (auto *AdditionalModMaps = 1218 Map.getAdditionalModuleMapFiles(WritingModule)) { 1219 Record.push_back(AdditionalModMaps->size()); 1220 for (const FileEntry *F : *AdditionalModMaps) 1221 AddPath(F->getName(), Record); 1222 } else { 1223 Record.push_back(0); 1224 } 1225 1226 Stream.EmitRecord(MODULE_MAP_FILE, Record); 1227 } 1228 1229 // Imports 1230 if (Chain) { 1231 serialization::ModuleManager &Mgr = Chain->getModuleManager(); 1232 Record.clear(); 1233 1234 for (auto *M : Mgr) { 1235 // Skip modules that weren't directly imported. 1236 if (!M->isDirectlyImported()) 1237 continue; 1238 1239 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding 1240 AddSourceLocation(M->ImportLoc, Record); 1241 Record.push_back(M->File->getSize()); 1242 Record.push_back(M->File->getModificationTime()); 1243 Record.push_back(M->Signature); 1244 AddPath(M->FileName, Record); 1245 } 1246 Stream.EmitRecord(IMPORTS, Record); 1247 } 1248 1249 // Language options. 1250 Record.clear(); 1251 const LangOptions &LangOpts = Context.getLangOpts(); 1252 #define LANGOPT(Name, Bits, Default, Description) \ 1253 Record.push_back(LangOpts.Name); 1254 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 1255 Record.push_back(static_cast<unsigned>(LangOpts.get##Name())); 1256 #include "clang/Basic/LangOptions.def" 1257 #define SANITIZER(NAME, ID) \ 1258 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID)); 1259 #include "clang/Basic/Sanitizers.def" 1260 1261 Record.push_back(LangOpts.ModuleFeatures.size()); 1262 for (StringRef Feature : LangOpts.ModuleFeatures) 1263 AddString(Feature, Record); 1264 1265 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind()); 1266 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record); 1267 1268 AddString(LangOpts.CurrentModule, Record); 1269 1270 // Comment options. 1271 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size()); 1272 for (CommentOptions::BlockCommandNamesTy::const_iterator 1273 I = LangOpts.CommentOpts.BlockCommandNames.begin(), 1274 IEnd = LangOpts.CommentOpts.BlockCommandNames.end(); 1275 I != IEnd; ++I) { 1276 AddString(*I, Record); 1277 } 1278 Record.push_back(LangOpts.CommentOpts.ParseAllComments); 1279 1280 Stream.EmitRecord(LANGUAGE_OPTIONS, Record); 1281 1282 // Target options. 1283 Record.clear(); 1284 const TargetInfo &Target = Context.getTargetInfo(); 1285 const TargetOptions &TargetOpts = Target.getTargetOpts(); 1286 AddString(TargetOpts.Triple, Record); 1287 AddString(TargetOpts.CPU, Record); 1288 AddString(TargetOpts.ABI, Record); 1289 Record.push_back(TargetOpts.FeaturesAsWritten.size()); 1290 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) { 1291 AddString(TargetOpts.FeaturesAsWritten[I], Record); 1292 } 1293 Record.push_back(TargetOpts.Features.size()); 1294 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) { 1295 AddString(TargetOpts.Features[I], Record); 1296 } 1297 Stream.EmitRecord(TARGET_OPTIONS, Record); 1298 1299 // Diagnostic options. 1300 Record.clear(); 1301 const DiagnosticOptions &DiagOpts 1302 = Context.getDiagnostics().getDiagnosticOptions(); 1303 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name); 1304 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 1305 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name())); 1306 #include "clang/Basic/DiagnosticOptions.def" 1307 Record.push_back(DiagOpts.Warnings.size()); 1308 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I) 1309 AddString(DiagOpts.Warnings[I], Record); 1310 Record.push_back(DiagOpts.Remarks.size()); 1311 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I) 1312 AddString(DiagOpts.Remarks[I], Record); 1313 // Note: we don't serialize the log or serialization file names, because they 1314 // are generally transient files and will almost always be overridden. 1315 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record); 1316 1317 // File system options. 1318 Record.clear(); 1319 const FileSystemOptions &FSOpts 1320 = Context.getSourceManager().getFileManager().getFileSystemOptions(); 1321 AddString(FSOpts.WorkingDir, Record); 1322 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record); 1323 1324 // Header search options. 1325 Record.clear(); 1326 const HeaderSearchOptions &HSOpts 1327 = PP.getHeaderSearchInfo().getHeaderSearchOpts(); 1328 AddString(HSOpts.Sysroot, Record); 1329 1330 // Include entries. 1331 Record.push_back(HSOpts.UserEntries.size()); 1332 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) { 1333 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I]; 1334 AddString(Entry.Path, Record); 1335 Record.push_back(static_cast<unsigned>(Entry.Group)); 1336 Record.push_back(Entry.IsFramework); 1337 Record.push_back(Entry.IgnoreSysRoot); 1338 } 1339 1340 // System header prefixes. 1341 Record.push_back(HSOpts.SystemHeaderPrefixes.size()); 1342 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) { 1343 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record); 1344 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader); 1345 } 1346 1347 AddString(HSOpts.ResourceDir, Record); 1348 AddString(HSOpts.ModuleCachePath, Record); 1349 AddString(HSOpts.ModuleUserBuildPath, Record); 1350 Record.push_back(HSOpts.DisableModuleHash); 1351 Record.push_back(HSOpts.UseBuiltinIncludes); 1352 Record.push_back(HSOpts.UseStandardSystemIncludes); 1353 Record.push_back(HSOpts.UseStandardCXXIncludes); 1354 Record.push_back(HSOpts.UseLibcxx); 1355 // Write out the specific module cache path that contains the module files. 1356 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record); 1357 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record); 1358 1359 // Preprocessor options. 1360 Record.clear(); 1361 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts(); 1362 1363 // Macro definitions. 1364 Record.push_back(PPOpts.Macros.size()); 1365 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 1366 AddString(PPOpts.Macros[I].first, Record); 1367 Record.push_back(PPOpts.Macros[I].second); 1368 } 1369 1370 // Includes 1371 Record.push_back(PPOpts.Includes.size()); 1372 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) 1373 AddString(PPOpts.Includes[I], Record); 1374 1375 // Macro includes 1376 Record.push_back(PPOpts.MacroIncludes.size()); 1377 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I) 1378 AddString(PPOpts.MacroIncludes[I], Record); 1379 1380 Record.push_back(PPOpts.UsePredefines); 1381 // Detailed record is important since it is used for the module cache hash. 1382 Record.push_back(PPOpts.DetailedRecord); 1383 AddString(PPOpts.ImplicitPCHInclude, Record); 1384 AddString(PPOpts.ImplicitPTHInclude, Record); 1385 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary)); 1386 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record); 1387 1388 // Original file name and file ID 1389 SourceManager &SM = Context.getSourceManager(); 1390 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { 1391 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); 1392 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE)); 1393 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID 1394 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1395 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); 1396 1397 Record.clear(); 1398 Record.push_back(ORIGINAL_FILE); 1399 Record.push_back(SM.getMainFileID().getOpaqueValue()); 1400 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName()); 1401 } 1402 1403 Record.clear(); 1404 Record.push_back(SM.getMainFileID().getOpaqueValue()); 1405 Stream.EmitRecord(ORIGINAL_FILE_ID, Record); 1406 1407 // Original PCH directory 1408 if (!OutputFile.empty() && OutputFile != "-") { 1409 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1410 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR)); 1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1412 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 1413 1414 SmallString<128> OutputPath(OutputFile); 1415 1416 SM.getFileManager().makeAbsolutePath(OutputPath); 1417 StringRef origDir = llvm::sys::path::parent_path(OutputPath); 1418 1419 RecordData Record; 1420 Record.push_back(ORIGINAL_PCH_DIR); 1421 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); 1422 } 1423 1424 WriteInputFiles(Context.SourceMgr, 1425 PP.getHeaderSearchInfo().getHeaderSearchOpts(), 1426 PP.getLangOpts().Modules); 1427 Stream.ExitBlock(); 1428 } 1429 1430 namespace { 1431 /// \brief An input file. 1432 struct InputFileEntry { 1433 const FileEntry *File; 1434 bool IsSystemFile; 1435 bool BufferOverridden; 1436 }; 1437 } 1438 1439 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, 1440 HeaderSearchOptions &HSOpts, 1441 bool Modules) { 1442 using namespace llvm; 1443 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4); 1444 RecordData Record; 1445 1446 // Create input-file abbreviation. 1447 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev(); 1448 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE)); 1449 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID 1450 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size 1451 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time 1452 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden 1453 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name 1454 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev); 1455 1456 // Get all ContentCache objects for files, sorted by whether the file is a 1457 // system one or not. System files go at the back, users files at the front. 1458 std::deque<InputFileEntry> SortedFiles; 1459 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) { 1460 // Get this source location entry. 1461 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); 1462 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc); 1463 1464 // We only care about file entries that were not overridden. 1465 if (!SLoc->isFile()) 1466 continue; 1467 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); 1468 if (!Cache->OrigEntry) 1469 continue; 1470 1471 InputFileEntry Entry; 1472 Entry.File = Cache->OrigEntry; 1473 Entry.IsSystemFile = Cache->IsSystemFile; 1474 Entry.BufferOverridden = Cache->BufferOverridden; 1475 if (Cache->IsSystemFile) 1476 SortedFiles.push_back(Entry); 1477 else 1478 SortedFiles.push_front(Entry); 1479 } 1480 1481 unsigned UserFilesNum = 0; 1482 // Write out all of the input files. 1483 std::vector<uint64_t> InputFileOffsets; 1484 for (std::deque<InputFileEntry>::iterator 1485 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) { 1486 const InputFileEntry &Entry = *I; 1487 1488 uint32_t &InputFileID = InputFileIDs[Entry.File]; 1489 if (InputFileID != 0) 1490 continue; // already recorded this file. 1491 1492 // Record this entry's offset. 1493 InputFileOffsets.push_back(Stream.GetCurrentBitNo()); 1494 1495 InputFileID = InputFileOffsets.size(); 1496 1497 if (!Entry.IsSystemFile) 1498 ++UserFilesNum; 1499 1500 Record.clear(); 1501 Record.push_back(INPUT_FILE); 1502 Record.push_back(InputFileOffsets.size()); 1503 1504 // Emit size/modification time for this file. 1505 Record.push_back(Entry.File->getSize()); 1506 Record.push_back(Entry.File->getModificationTime()); 1507 1508 // Whether this file was overridden. 1509 Record.push_back(Entry.BufferOverridden); 1510 1511 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName()); 1512 } 1513 1514 Stream.ExitBlock(); 1515 1516 // Create input file offsets abbreviation. 1517 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev(); 1518 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS)); 1519 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files 1520 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system 1521 // input files 1522 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array 1523 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev); 1524 1525 // Write input file offsets. 1526 Record.clear(); 1527 Record.push_back(INPUT_FILE_OFFSETS); 1528 Record.push_back(InputFileOffsets.size()); 1529 Record.push_back(UserFilesNum); 1530 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets)); 1531 } 1532 1533 //===----------------------------------------------------------------------===// 1534 // Source Manager Serialization 1535 //===----------------------------------------------------------------------===// 1536 1537 /// \brief Create an abbreviation for the SLocEntry that refers to a 1538 /// file. 1539 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { 1540 using namespace llvm; 1541 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1542 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); 1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1547 // FileEntry fields. 1548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID 1549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs 1550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex 1551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls 1552 return Stream.EmitAbbrev(Abbrev); 1553 } 1554 1555 /// \brief Create an abbreviation for the SLocEntry that refers to a 1556 /// buffer. 1557 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { 1558 using namespace llvm; 1559 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1560 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); 1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location 1563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic 1564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives 1565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob 1566 return Stream.EmitAbbrev(Abbrev); 1567 } 1568 1569 /// \brief Create an abbreviation for the SLocEntry that refers to a 1570 /// buffer's blob. 1571 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { 1572 using namespace llvm; 1573 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1574 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); 1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob 1576 return Stream.EmitAbbrev(Abbrev); 1577 } 1578 1579 /// \brief Create an abbreviation for the SLocEntry that refers to a macro 1580 /// expansion. 1581 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) { 1582 using namespace llvm; 1583 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1584 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY)); 1585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset 1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location 1587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location 1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location 1589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length 1590 return Stream.EmitAbbrev(Abbrev); 1591 } 1592 1593 namespace { 1594 // Trait used for the on-disk hash table of header search information. 1595 class HeaderFileInfoTrait { 1596 ASTWriter &Writer; 1597 const HeaderSearch &HS; 1598 1599 // Keep track of the framework names we've used during serialization. 1600 SmallVector<char, 128> FrameworkStringData; 1601 llvm::StringMap<unsigned> FrameworkNameOffset; 1602 1603 public: 1604 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS) 1605 : Writer(Writer), HS(HS) { } 1606 1607 struct key_type { 1608 const FileEntry *FE; 1609 const char *Filename; 1610 }; 1611 typedef const key_type &key_type_ref; 1612 1613 typedef HeaderFileInfo data_type; 1614 typedef const data_type &data_type_ref; 1615 typedef unsigned hash_value_type; 1616 typedef unsigned offset_type; 1617 1618 static hash_value_type ComputeHash(key_type_ref key) { 1619 // The hash is based only on size/time of the file, so that the reader can 1620 // match even when symlinking or excess path elements ("foo/../", "../") 1621 // change the form of the name. However, complete path is still the key. 1622 // 1623 // FIXME: Using the mtime here will cause problems for explicit module 1624 // imports. 1625 return llvm::hash_combine(key.FE->getSize(), 1626 key.FE->getModificationTime()); 1627 } 1628 1629 std::pair<unsigned,unsigned> 1630 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) { 1631 using namespace llvm::support; 1632 endian::Writer<little> Writer(Out); 1633 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8; 1634 Writer.write<uint16_t>(KeyLen); 1635 unsigned DataLen = 1 + 2 + 4 + 4; 1636 if (Data.isModuleHeader) 1637 DataLen += 4; 1638 Writer.write<uint8_t>(DataLen); 1639 return std::make_pair(KeyLen, DataLen); 1640 } 1641 1642 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) { 1643 using namespace llvm::support; 1644 endian::Writer<little> LE(Out); 1645 LE.write<uint64_t>(key.FE->getSize()); 1646 KeyLen -= 8; 1647 LE.write<uint64_t>(key.FE->getModificationTime()); 1648 KeyLen -= 8; 1649 Out.write(key.Filename, KeyLen); 1650 } 1651 1652 void EmitData(raw_ostream &Out, key_type_ref key, 1653 data_type_ref Data, unsigned DataLen) { 1654 using namespace llvm::support; 1655 endian::Writer<little> LE(Out); 1656 uint64_t Start = Out.tell(); (void)Start; 1657 1658 unsigned char Flags = (Data.HeaderRole << 6) 1659 | (Data.isImport << 5) 1660 | (Data.isPragmaOnce << 4) 1661 | (Data.DirInfo << 2) 1662 | (Data.Resolved << 1) 1663 | Data.IndexHeaderMapHeader; 1664 LE.write<uint8_t>(Flags); 1665 LE.write<uint16_t>(Data.NumIncludes); 1666 1667 if (!Data.ControllingMacro) 1668 LE.write<uint32_t>(Data.ControllingMacroID); 1669 else 1670 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro)); 1671 1672 unsigned Offset = 0; 1673 if (!Data.Framework.empty()) { 1674 // If this header refers into a framework, save the framework name. 1675 llvm::StringMap<unsigned>::iterator Pos 1676 = FrameworkNameOffset.find(Data.Framework); 1677 if (Pos == FrameworkNameOffset.end()) { 1678 Offset = FrameworkStringData.size() + 1; 1679 FrameworkStringData.append(Data.Framework.begin(), 1680 Data.Framework.end()); 1681 FrameworkStringData.push_back(0); 1682 1683 FrameworkNameOffset[Data.Framework] = Offset; 1684 } else 1685 Offset = Pos->second; 1686 } 1687 LE.write<uint32_t>(Offset); 1688 1689 if (Data.isModuleHeader) { 1690 Module *Mod = HS.findModuleForHeader(key.FE).getModule(); 1691 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod)); 1692 } 1693 1694 assert(Out.tell() - Start == DataLen && "Wrong data length"); 1695 } 1696 1697 const char *strings_begin() const { return FrameworkStringData.begin(); } 1698 const char *strings_end() const { return FrameworkStringData.end(); } 1699 }; 1700 } // end anonymous namespace 1701 1702 /// \brief Write the header search block for the list of files that 1703 /// 1704 /// \param HS The header search structure to save. 1705 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) { 1706 SmallVector<const FileEntry *, 16> FilesByUID; 1707 HS.getFileMgr().GetUniqueIDMapping(FilesByUID); 1708 1709 if (FilesByUID.size() > HS.header_file_size()) 1710 FilesByUID.resize(HS.header_file_size()); 1711 1712 HeaderFileInfoTrait GeneratorTrait(*this, HS); 1713 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator; 1714 SmallVector<const char *, 4> SavedStrings; 1715 unsigned NumHeaderSearchEntries = 0; 1716 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { 1717 const FileEntry *File = FilesByUID[UID]; 1718 if (!File) 1719 continue; 1720 1721 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo 1722 // from the external source if it was not provided already. 1723 HeaderFileInfo HFI; 1724 if (!HS.tryGetFileInfo(File, HFI) || 1725 (HFI.External && Chain) || 1726 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)) 1727 continue; 1728 1729 // Massage the file path into an appropriate form. 1730 const char *Filename = File->getName(); 1731 SmallString<128> FilenameTmp(Filename); 1732 if (PreparePathForOutput(FilenameTmp)) { 1733 // If we performed any translation on the file name at all, we need to 1734 // save this string, since the generator will refer to it later. 1735 Filename = strdup(FilenameTmp.c_str()); 1736 SavedStrings.push_back(Filename); 1737 } 1738 1739 HeaderFileInfoTrait::key_type key = { File, Filename }; 1740 Generator.insert(key, HFI, GeneratorTrait); 1741 ++NumHeaderSearchEntries; 1742 } 1743 1744 // Create the on-disk hash table in a buffer. 1745 SmallString<4096> TableData; 1746 uint32_t BucketOffset; 1747 { 1748 using namespace llvm::support; 1749 llvm::raw_svector_ostream Out(TableData); 1750 // Make sure that no bucket is at offset 0 1751 endian::Writer<little>(Out).write<uint32_t>(0); 1752 BucketOffset = Generator.Emit(Out, GeneratorTrait); 1753 } 1754 1755 // Create a blob abbreviation 1756 using namespace llvm; 1757 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1758 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE)); 1759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 1763 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev); 1764 1765 // Write the header search table 1766 RecordData Record; 1767 Record.push_back(HEADER_SEARCH_TABLE); 1768 Record.push_back(BucketOffset); 1769 Record.push_back(NumHeaderSearchEntries); 1770 Record.push_back(TableData.size()); 1771 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end()); 1772 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData); 1773 1774 // Free all of the strings we had to duplicate. 1775 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I) 1776 free(const_cast<char *>(SavedStrings[I])); 1777 } 1778 1779 /// \brief Writes the block containing the serialized form of the 1780 /// source manager. 1781 /// 1782 /// TODO: We should probably use an on-disk hash table (stored in a 1783 /// blob), indexed based on the file name, so that we only create 1784 /// entries for files that we actually need. In the common case (no 1785 /// errors), we probably won't have to create file entries for any of 1786 /// the files in the AST. 1787 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, 1788 const Preprocessor &PP) { 1789 RecordData Record; 1790 1791 // Enter the source manager block. 1792 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); 1793 1794 // Abbreviations for the various kinds of source-location entries. 1795 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); 1796 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); 1797 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); 1798 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream); 1799 1800 // Write out the source location entry table. We skip the first 1801 // entry, which is always the same dummy entry. 1802 std::vector<uint32_t> SLocEntryOffsets; 1803 RecordData PreloadSLocs; 1804 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1); 1805 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); 1806 I != N; ++I) { 1807 // Get this source location entry. 1808 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); 1809 FileID FID = FileID::get(I); 1810 assert(&SourceMgr.getSLocEntry(FID) == SLoc); 1811 1812 // Record the offset of this source-location entry. 1813 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); 1814 1815 // Figure out which record code to use. 1816 unsigned Code; 1817 if (SLoc->isFile()) { 1818 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); 1819 if (Cache->OrigEntry) { 1820 Code = SM_SLOC_FILE_ENTRY; 1821 } else 1822 Code = SM_SLOC_BUFFER_ENTRY; 1823 } else 1824 Code = SM_SLOC_EXPANSION_ENTRY; 1825 Record.clear(); 1826 Record.push_back(Code); 1827 1828 // Starting offset of this entry within this module, so skip the dummy. 1829 Record.push_back(SLoc->getOffset() - 2); 1830 if (SLoc->isFile()) { 1831 const SrcMgr::FileInfo &File = SLoc->getFile(); 1832 Record.push_back(File.getIncludeLoc().getRawEncoding()); 1833 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding 1834 Record.push_back(File.hasLineDirectives()); 1835 1836 const SrcMgr::ContentCache *Content = File.getContentCache(); 1837 if (Content->OrigEntry) { 1838 assert(Content->OrigEntry == Content->ContentsEntry && 1839 "Writing to AST an overridden file is not supported"); 1840 1841 // The source location entry is a file. Emit input file ID. 1842 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry"); 1843 Record.push_back(InputFileIDs[Content->OrigEntry]); 1844 1845 Record.push_back(File.NumCreatedFIDs); 1846 1847 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID); 1848 if (FDI != FileDeclIDs.end()) { 1849 Record.push_back(FDI->second->FirstDeclIndex); 1850 Record.push_back(FDI->second->DeclIDs.size()); 1851 } else { 1852 Record.push_back(0); 1853 Record.push_back(0); 1854 } 1855 1856 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record); 1857 1858 if (Content->BufferOverridden) { 1859 Record.clear(); 1860 Record.push_back(SM_SLOC_BUFFER_BLOB); 1861 const llvm::MemoryBuffer *Buffer 1862 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1863 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1864 StringRef(Buffer->getBufferStart(), 1865 Buffer->getBufferSize() + 1)); 1866 } 1867 } else { 1868 // The source location entry is a buffer. The blob associated 1869 // with this entry contains the contents of the buffer. 1870 1871 // We add one to the size so that we capture the trailing NULL 1872 // that is required by llvm::MemoryBuffer::getMemBuffer (on 1873 // the reader side). 1874 const llvm::MemoryBuffer *Buffer 1875 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); 1876 const char *Name = Buffer->getBufferIdentifier(); 1877 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, 1878 StringRef(Name, strlen(Name) + 1)); 1879 Record.clear(); 1880 Record.push_back(SM_SLOC_BUFFER_BLOB); 1881 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, 1882 StringRef(Buffer->getBufferStart(), 1883 Buffer->getBufferSize() + 1)); 1884 1885 if (strcmp(Name, "<built-in>") == 0) { 1886 PreloadSLocs.push_back(SLocEntryOffsets.size()); 1887 } 1888 } 1889 } else { 1890 // The source location entry is a macro expansion. 1891 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion(); 1892 Record.push_back(Expansion.getSpellingLoc().getRawEncoding()); 1893 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding()); 1894 Record.push_back(Expansion.isMacroArgExpansion() ? 0 1895 : Expansion.getExpansionLocEnd().getRawEncoding()); 1896 1897 // Compute the token length for this macro expansion. 1898 unsigned NextOffset = SourceMgr.getNextLocalOffset(); 1899 if (I + 1 != N) 1900 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset(); 1901 Record.push_back(NextOffset - SLoc->getOffset() - 1); 1902 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record); 1903 } 1904 } 1905 1906 Stream.ExitBlock(); 1907 1908 if (SLocEntryOffsets.empty()) 1909 return; 1910 1911 // Write the source-location offsets table into the AST block. This 1912 // table is used for lazily loading source-location information. 1913 using namespace llvm; 1914 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 1915 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); 1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs 1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size 1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets 1919 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); 1920 1921 Record.clear(); 1922 Record.push_back(SOURCE_LOCATION_OFFSETS); 1923 Record.push_back(SLocEntryOffsets.size()); 1924 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy 1925 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, bytes(SLocEntryOffsets)); 1926 1927 // Write the source location entry preloads array, telling the AST 1928 // reader which source locations entries it should load eagerly. 1929 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); 1930 1931 // Write the line table. It depends on remapping working, so it must come 1932 // after the source location offsets. 1933 if (SourceMgr.hasLineTable()) { 1934 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1935 1936 Record.clear(); 1937 // Emit the file names. 1938 Record.push_back(LineTable.getNumFilenames()); 1939 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) 1940 AddPath(LineTable.getFilename(I), Record); 1941 1942 // Emit the line entries 1943 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); 1944 L != LEnd; ++L) { 1945 // Only emit entries for local files. 1946 if (L->first.ID < 0) 1947 continue; 1948 1949 // Emit the file ID 1950 Record.push_back(L->first.ID); 1951 1952 // Emit the line entries 1953 Record.push_back(L->second.size()); 1954 for (std::vector<LineEntry>::iterator LE = L->second.begin(), 1955 LEEnd = L->second.end(); 1956 LE != LEEnd; ++LE) { 1957 Record.push_back(LE->FileOffset); 1958 Record.push_back(LE->LineNo); 1959 Record.push_back(LE->FilenameID); 1960 Record.push_back((unsigned)LE->FileKind); 1961 Record.push_back(LE->IncludeOffset); 1962 } 1963 } 1964 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record); 1965 } 1966 } 1967 1968 //===----------------------------------------------------------------------===// 1969 // Preprocessor Serialization 1970 //===----------------------------------------------------------------------===// 1971 1972 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule, 1973 const Preprocessor &PP) { 1974 if (MacroInfo *MI = MD->getMacroInfo()) 1975 if (MI->isBuiltinMacro()) 1976 return true; 1977 1978 if (IsModule) { 1979 SourceLocation Loc = MD->getLocation(); 1980 if (Loc.isInvalid()) 1981 return true; 1982 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID()) 1983 return true; 1984 } 1985 1986 return false; 1987 } 1988 1989 /// \brief Writes the block containing the serialized form of the 1990 /// preprocessor. 1991 /// 1992 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { 1993 PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); 1994 if (PPRec) 1995 WritePreprocessorDetail(*PPRec); 1996 1997 RecordData Record; 1998 RecordData ModuleMacroRecord; 1999 2000 // If the preprocessor __COUNTER__ value has been bumped, remember it. 2001 if (PP.getCounterValue() != 0) { 2002 Record.push_back(PP.getCounterValue()); 2003 Stream.EmitRecord(PP_COUNTER_VALUE, Record); 2004 Record.clear(); 2005 } 2006 2007 // Enter the preprocessor block. 2008 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); 2009 2010 // If the AST file contains __DATE__ or __TIME__ emit a warning about this. 2011 // FIXME: use diagnostics subsystem for localization etc. 2012 if (PP.SawDateOrTime()) 2013 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); 2014 2015 2016 // Loop over all the macro directives that are live at the end of the file, 2017 // emitting each to the PP section. 2018 2019 // Construct the list of identifiers with macro directives that need to be 2020 // serialized. 2021 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers; 2022 for (auto &Id : PP.getIdentifierTable()) 2023 if (Id.second->hadMacroDefinition() && 2024 (!Id.second->isFromAST() || 2025 Id.second->hasChangedSinceDeserialization())) 2026 MacroIdentifiers.push_back(Id.second); 2027 // Sort the set of macro definitions that need to be serialized by the 2028 // name of the macro, to provide a stable ordering. 2029 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(), 2030 llvm::less_ptr<IdentifierInfo>()); 2031 2032 // Emit the macro directives as a list and associate the offset with the 2033 // identifier they belong to. 2034 for (const IdentifierInfo *Name : MacroIdentifiers) { 2035 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name); 2036 auto StartOffset = Stream.GetCurrentBitNo(); 2037 2038 // Emit the macro directives in reverse source order. 2039 for (; MD; MD = MD->getPrevious()) { 2040 // Once we hit an ignored macro, we're done: the rest of the chain 2041 // will all be ignored macros. 2042 if (shouldIgnoreMacro(MD, IsModule, PP)) 2043 break; 2044 2045 AddSourceLocation(MD->getLocation(), Record); 2046 Record.push_back(MD->getKind()); 2047 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) { 2048 Record.push_back(getMacroRef(DefMD->getInfo(), Name)); 2049 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) { 2050 Record.push_back(VisMD->isPublic()); 2051 } 2052 } 2053 2054 // Write out any exported module macros. 2055 bool EmittedModuleMacros = false; 2056 if (IsModule) { 2057 auto Leafs = PP.getLeafModuleMacros(Name); 2058 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end()); 2059 llvm::DenseMap<ModuleMacro*, unsigned> Visits; 2060 while (!Worklist.empty()) { 2061 auto *Macro = Worklist.pop_back_val(); 2062 2063 // Emit a record indicating this submodule exports this macro. 2064 ModuleMacroRecord.push_back( 2065 getSubmoduleID(Macro->getOwningModule())); 2066 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name)); 2067 for (auto *M : Macro->overrides()) 2068 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule())); 2069 2070 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord); 2071 ModuleMacroRecord.clear(); 2072 2073 // Enqueue overridden macros once we've visited all their ancestors. 2074 for (auto *M : Macro->overrides()) 2075 if (++Visits[M] == M->getNumOverridingMacros()) 2076 Worklist.push_back(M); 2077 2078 EmittedModuleMacros = true; 2079 } 2080 } 2081 2082 if (Record.empty() && !EmittedModuleMacros) 2083 continue; 2084 2085 IdentMacroDirectivesOffsetMap[Name] = StartOffset; 2086 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record); 2087 Record.clear(); 2088 } 2089 2090 /// \brief Offsets of each of the macros into the bitstream, indexed by 2091 /// the local macro ID 2092 /// 2093 /// For each identifier that is associated with a macro, this map 2094 /// provides the offset into the bitstream where that macro is 2095 /// defined. 2096 std::vector<uint32_t> MacroOffsets; 2097 2098 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) { 2099 const IdentifierInfo *Name = MacroInfosToEmit[I].Name; 2100 MacroInfo *MI = MacroInfosToEmit[I].MI; 2101 MacroID ID = MacroInfosToEmit[I].ID; 2102 2103 if (ID < FirstMacroID) { 2104 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?"); 2105 continue; 2106 } 2107 2108 // Record the local offset of this macro. 2109 unsigned Index = ID - FirstMacroID; 2110 if (Index == MacroOffsets.size()) 2111 MacroOffsets.push_back(Stream.GetCurrentBitNo()); 2112 else { 2113 if (Index > MacroOffsets.size()) 2114 MacroOffsets.resize(Index + 1); 2115 2116 MacroOffsets[Index] = Stream.GetCurrentBitNo(); 2117 } 2118 2119 AddIdentifierRef(Name, Record); 2120 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc())); 2121 AddSourceLocation(MI->getDefinitionLoc(), Record); 2122 AddSourceLocation(MI->getDefinitionEndLoc(), Record); 2123 Record.push_back(MI->isUsed()); 2124 Record.push_back(MI->isUsedForHeaderGuard()); 2125 unsigned Code; 2126 if (MI->isObjectLike()) { 2127 Code = PP_MACRO_OBJECT_LIKE; 2128 } else { 2129 Code = PP_MACRO_FUNCTION_LIKE; 2130 2131 Record.push_back(MI->isC99Varargs()); 2132 Record.push_back(MI->isGNUVarargs()); 2133 Record.push_back(MI->hasCommaPasting()); 2134 Record.push_back(MI->getNumArgs()); 2135 for (const IdentifierInfo *Arg : MI->args()) 2136 AddIdentifierRef(Arg, Record); 2137 } 2138 2139 // If we have a detailed preprocessing record, record the macro definition 2140 // ID that corresponds to this macro. 2141 if (PPRec) 2142 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]); 2143 2144 Stream.EmitRecord(Code, Record); 2145 Record.clear(); 2146 2147 // Emit the tokens array. 2148 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { 2149 // Note that we know that the preprocessor does not have any annotation 2150 // tokens in it because they are created by the parser, and thus can't 2151 // be in a macro definition. 2152 const Token &Tok = MI->getReplacementToken(TokNo); 2153 AddToken(Tok, Record); 2154 Stream.EmitRecord(PP_TOKEN, Record); 2155 Record.clear(); 2156 } 2157 ++NumMacros; 2158 } 2159 2160 Stream.ExitBlock(); 2161 2162 // Write the offsets table for macro IDs. 2163 using namespace llvm; 2164 auto *Abbrev = new BitCodeAbbrev(); 2165 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET)); 2166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros 2167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 2168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2169 2170 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2171 Record.clear(); 2172 Record.push_back(MACRO_OFFSET); 2173 Record.push_back(MacroOffsets.size()); 2174 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS); 2175 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, 2176 bytes(MacroOffsets)); 2177 } 2178 2179 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) { 2180 if (PPRec.local_begin() == PPRec.local_end()) 2181 return; 2182 2183 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets; 2184 2185 // Enter the preprocessor block. 2186 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3); 2187 2188 // If the preprocessor has a preprocessing record, emit it. 2189 unsigned NumPreprocessingRecords = 0; 2190 using namespace llvm; 2191 2192 // Set up the abbreviation for 2193 unsigned InclusionAbbrev = 0; 2194 { 2195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2196 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE)); 2197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length 2198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes 2199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind 2200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module 2201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2202 InclusionAbbrev = Stream.EmitAbbrev(Abbrev); 2203 } 2204 2205 unsigned FirstPreprocessorEntityID 2206 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0) 2207 + NUM_PREDEF_PP_ENTITY_IDS; 2208 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID; 2209 RecordData Record; 2210 for (PreprocessingRecord::iterator E = PPRec.local_begin(), 2211 EEnd = PPRec.local_end(); 2212 E != EEnd; 2213 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) { 2214 Record.clear(); 2215 2216 PreprocessedEntityOffsets.push_back( 2217 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo())); 2218 2219 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(*E)) { 2220 // Record this macro definition's ID. 2221 MacroDefinitions[MD] = NextPreprocessorEntityID; 2222 2223 AddIdentifierRef(MD->getName(), Record); 2224 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record); 2225 continue; 2226 } 2227 2228 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) { 2229 Record.push_back(ME->isBuiltinMacro()); 2230 if (ME->isBuiltinMacro()) 2231 AddIdentifierRef(ME->getName(), Record); 2232 else 2233 Record.push_back(MacroDefinitions[ME->getDefinition()]); 2234 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record); 2235 continue; 2236 } 2237 2238 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { 2239 Record.push_back(PPD_INCLUSION_DIRECTIVE); 2240 Record.push_back(ID->getFileName().size()); 2241 Record.push_back(ID->wasInQuotes()); 2242 Record.push_back(static_cast<unsigned>(ID->getKind())); 2243 Record.push_back(ID->importedModule()); 2244 SmallString<64> Buffer; 2245 Buffer += ID->getFileName(); 2246 // Check that the FileEntry is not null because it was not resolved and 2247 // we create a PCH even with compiler errors. 2248 if (ID->getFile()) 2249 Buffer += ID->getFile()->getName(); 2250 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); 2251 continue; 2252 } 2253 2254 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter"); 2255 } 2256 Stream.ExitBlock(); 2257 2258 // Write the offsets table for the preprocessing record. 2259 if (NumPreprocessingRecords > 0) { 2260 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords); 2261 2262 // Write the offsets table for identifier IDs. 2263 using namespace llvm; 2264 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2265 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS)); 2266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity 2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2268 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2269 2270 Record.clear(); 2271 Record.push_back(PPD_ENTITIES_OFFSETS); 2272 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS); 2273 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record, 2274 bytes(PreprocessedEntityOffsets)); 2275 } 2276 } 2277 2278 unsigned ASTWriter::getSubmoduleID(Module *Mod) { 2279 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod); 2280 if (Known != SubmoduleIDs.end()) 2281 return Known->second; 2282 2283 return SubmoduleIDs[Mod] = NextSubmoduleID++; 2284 } 2285 2286 unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const { 2287 if (!Mod) 2288 return 0; 2289 2290 llvm::DenseMap<Module *, unsigned>::const_iterator 2291 Known = SubmoduleIDs.find(Mod); 2292 if (Known != SubmoduleIDs.end()) 2293 return Known->second; 2294 2295 return 0; 2296 } 2297 2298 /// \brief Compute the number of modules within the given tree (including the 2299 /// given module). 2300 static unsigned getNumberOfModules(Module *Mod) { 2301 unsigned ChildModules = 0; 2302 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2303 SubEnd = Mod->submodule_end(); 2304 Sub != SubEnd; ++Sub) 2305 ChildModules += getNumberOfModules(*Sub); 2306 2307 return ChildModules + 1; 2308 } 2309 2310 void ASTWriter::WriteSubmodules(Module *WritingModule) { 2311 // Enter the submodule description block. 2312 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5); 2313 2314 // Write the abbreviations needed for the submodules block. 2315 using namespace llvm; 2316 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2317 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION)); 2318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID 2319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent 2320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework 2321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit 2322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem 2323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC 2324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules... 2325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit... 2326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild... 2327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh... 2328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2329 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev); 2330 2331 Abbrev = new BitCodeAbbrev(); 2332 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER)); 2333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2334 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev); 2335 2336 Abbrev = new BitCodeAbbrev(); 2337 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER)); 2338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2339 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev); 2340 2341 Abbrev = new BitCodeAbbrev(); 2342 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER)); 2343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2344 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev); 2345 2346 Abbrev = new BitCodeAbbrev(); 2347 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR)); 2348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2349 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev); 2350 2351 Abbrev = new BitCodeAbbrev(); 2352 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES)); 2353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State 2354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature 2355 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev); 2356 2357 Abbrev = new BitCodeAbbrev(); 2358 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER)); 2359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2360 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev); 2361 2362 Abbrev = new BitCodeAbbrev(); 2363 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER)); 2364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2365 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev); 2366 2367 Abbrev = new BitCodeAbbrev(); 2368 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER)); 2369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2370 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev); 2371 2372 Abbrev = new BitCodeAbbrev(); 2373 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER)); 2374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2375 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev); 2376 2377 Abbrev = new BitCodeAbbrev(); 2378 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY)); 2379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework 2380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name 2381 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev); 2382 2383 Abbrev = new BitCodeAbbrev(); 2384 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO)); 2385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name 2386 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev); 2387 2388 Abbrev = new BitCodeAbbrev(); 2389 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT)); 2390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module 2391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message 2392 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev); 2393 2394 // Write the submodule metadata block. 2395 RecordData Record; 2396 Record.push_back(getNumberOfModules(WritingModule)); 2397 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS); 2398 Stream.EmitRecord(SUBMODULE_METADATA, Record); 2399 2400 // Write all of the submodules. 2401 std::queue<Module *> Q; 2402 Q.push(WritingModule); 2403 while (!Q.empty()) { 2404 Module *Mod = Q.front(); 2405 Q.pop(); 2406 unsigned ID = getSubmoduleID(Mod); 2407 2408 // Emit the definition of the block. 2409 Record.clear(); 2410 Record.push_back(SUBMODULE_DEFINITION); 2411 Record.push_back(ID); 2412 if (Mod->Parent) { 2413 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?"); 2414 Record.push_back(SubmoduleIDs[Mod->Parent]); 2415 } else { 2416 Record.push_back(0); 2417 } 2418 Record.push_back(Mod->IsFramework); 2419 Record.push_back(Mod->IsExplicit); 2420 Record.push_back(Mod->IsSystem); 2421 Record.push_back(Mod->IsExternC); 2422 Record.push_back(Mod->InferSubmodules); 2423 Record.push_back(Mod->InferExplicitSubmodules); 2424 Record.push_back(Mod->InferExportWildcard); 2425 Record.push_back(Mod->ConfigMacrosExhaustive); 2426 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name); 2427 2428 // Emit the requirements. 2429 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) { 2430 Record.clear(); 2431 Record.push_back(SUBMODULE_REQUIRES); 2432 Record.push_back(Mod->Requirements[I].second); 2433 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, 2434 Mod->Requirements[I].first); 2435 } 2436 2437 // Emit the umbrella header, if there is one. 2438 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) { 2439 Record.clear(); 2440 Record.push_back(SUBMODULE_UMBRELLA_HEADER); 2441 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record, 2442 UmbrellaHeader.NameAsWritten); 2443 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) { 2444 Record.clear(); 2445 Record.push_back(SUBMODULE_UMBRELLA_DIR); 2446 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record, 2447 UmbrellaDir.NameAsWritten); 2448 } 2449 2450 // Emit the headers. 2451 struct { 2452 unsigned RecordKind; 2453 unsigned Abbrev; 2454 Module::HeaderKind HeaderKind; 2455 } HeaderLists[] = { 2456 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal}, 2457 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual}, 2458 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private}, 2459 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev, 2460 Module::HK_PrivateTextual}, 2461 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded} 2462 }; 2463 for (auto &HL : HeaderLists) { 2464 Record.clear(); 2465 Record.push_back(HL.RecordKind); 2466 for (auto &H : Mod->Headers[HL.HeaderKind]) 2467 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten); 2468 } 2469 2470 // Emit the top headers. 2471 { 2472 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager()); 2473 Record.clear(); 2474 Record.push_back(SUBMODULE_TOPHEADER); 2475 for (auto *H : TopHeaders) 2476 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName()); 2477 } 2478 2479 // Emit the imports. 2480 if (!Mod->Imports.empty()) { 2481 Record.clear(); 2482 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) { 2483 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]); 2484 assert(ImportedID && "Unknown submodule!"); 2485 Record.push_back(ImportedID); 2486 } 2487 Stream.EmitRecord(SUBMODULE_IMPORTS, Record); 2488 } 2489 2490 // Emit the exports. 2491 if (!Mod->Exports.empty()) { 2492 Record.clear(); 2493 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) { 2494 if (Module *Exported = Mod->Exports[I].getPointer()) { 2495 unsigned ExportedID = getSubmoduleID(Exported); 2496 Record.push_back(ExportedID); 2497 } else { 2498 Record.push_back(0); 2499 } 2500 2501 Record.push_back(Mod->Exports[I].getInt()); 2502 } 2503 Stream.EmitRecord(SUBMODULE_EXPORTS, Record); 2504 } 2505 2506 //FIXME: How do we emit the 'use'd modules? They may not be submodules. 2507 // Might be unnecessary as use declarations are only used to build the 2508 // module itself. 2509 2510 // Emit the link libraries. 2511 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) { 2512 Record.clear(); 2513 Record.push_back(SUBMODULE_LINK_LIBRARY); 2514 Record.push_back(Mod->LinkLibraries[I].IsFramework); 2515 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, 2516 Mod->LinkLibraries[I].Library); 2517 } 2518 2519 // Emit the conflicts. 2520 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) { 2521 Record.clear(); 2522 Record.push_back(SUBMODULE_CONFLICT); 2523 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other); 2524 assert(OtherID && "Unknown submodule!"); 2525 Record.push_back(OtherID); 2526 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, 2527 Mod->Conflicts[I].Message); 2528 } 2529 2530 // Emit the configuration macros. 2531 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) { 2532 Record.clear(); 2533 Record.push_back(SUBMODULE_CONFIG_MACRO); 2534 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, 2535 Mod->ConfigMacros[I]); 2536 } 2537 2538 // Queue up the submodules of this module. 2539 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2540 SubEnd = Mod->submodule_end(); 2541 Sub != SubEnd; ++Sub) 2542 Q.push(*Sub); 2543 } 2544 2545 Stream.ExitBlock(); 2546 2547 // FIXME: This can easily happen, if we have a reference to a submodule that 2548 // did not result in us loading a module file for that submodule. For 2549 // instance, a cross-top-level-module 'conflict' declaration will hit this. 2550 assert((NextSubmoduleID - FirstSubmoduleID == 2551 getNumberOfModules(WritingModule)) && 2552 "Wrong # of submodules; found a reference to a non-local, " 2553 "non-imported submodule?"); 2554 } 2555 2556 serialization::SubmoduleID 2557 ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) { 2558 if (Loc.isInvalid() || !WritingModule) 2559 return 0; // No submodule 2560 2561 // Find the module that owns this location. 2562 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap(); 2563 Module *OwningMod 2564 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager())); 2565 if (!OwningMod) 2566 return 0; 2567 2568 // Check whether this submodule is part of our own module. 2569 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule)) 2570 return 0; 2571 2572 return getSubmoduleID(OwningMod); 2573 } 2574 2575 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag, 2576 bool isModule) { 2577 // Make sure set diagnostic pragmas don't affect the translation unit that 2578 // imports the module. 2579 // FIXME: Make diagnostic pragma sections work properly with modules. 2580 if (isModule) 2581 return; 2582 2583 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64> 2584 DiagStateIDMap; 2585 unsigned CurrID = 0; 2586 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one. 2587 RecordData Record; 2588 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator 2589 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end(); 2590 I != E; ++I) { 2591 const DiagnosticsEngine::DiagStatePoint &point = *I; 2592 if (point.Loc.isInvalid()) 2593 continue; 2594 2595 Record.push_back(point.Loc.getRawEncoding()); 2596 unsigned &DiagStateID = DiagStateIDMap[point.State]; 2597 Record.push_back(DiagStateID); 2598 2599 if (DiagStateID == 0) { 2600 DiagStateID = ++CurrID; 2601 for (DiagnosticsEngine::DiagState::const_iterator 2602 I = point.State->begin(), E = point.State->end(); I != E; ++I) { 2603 if (I->second.isPragma()) { 2604 Record.push_back(I->first); 2605 Record.push_back((unsigned)I->second.getSeverity()); 2606 } 2607 } 2608 Record.push_back(-1); // mark the end of the diag/map pairs for this 2609 // location. 2610 } 2611 } 2612 2613 if (!Record.empty()) 2614 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record); 2615 } 2616 2617 void ASTWriter::WriteCXXCtorInitializersOffsets() { 2618 if (CXXCtorInitializersOffsets.empty()) 2619 return; 2620 2621 RecordData Record; 2622 2623 // Create a blob abbreviation for the C++ ctor initializer offsets. 2624 using namespace llvm; 2625 2626 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2627 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS)); 2628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 2629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2630 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2631 2632 // Write the base specifier offsets table. 2633 Record.clear(); 2634 Record.push_back(CXX_CTOR_INITIALIZERS_OFFSETS); 2635 Record.push_back(CXXCtorInitializersOffsets.size()); 2636 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record, 2637 bytes(CXXCtorInitializersOffsets)); 2638 } 2639 2640 void ASTWriter::WriteCXXBaseSpecifiersOffsets() { 2641 if (CXXBaseSpecifiersOffsets.empty()) 2642 return; 2643 2644 RecordData Record; 2645 2646 // Create a blob abbreviation for the C++ base specifiers offsets. 2647 using namespace llvm; 2648 2649 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2650 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); 2651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 2652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2653 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2654 2655 // Write the base specifier offsets table. 2656 Record.clear(); 2657 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS); 2658 Record.push_back(CXXBaseSpecifiersOffsets.size()); 2659 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, 2660 bytes(CXXBaseSpecifiersOffsets)); 2661 } 2662 2663 //===----------------------------------------------------------------------===// 2664 // Type Serialization 2665 //===----------------------------------------------------------------------===// 2666 2667 /// \brief Write the representation of a type to the AST stream. 2668 void ASTWriter::WriteType(QualType T) { 2669 TypeIdx &Idx = TypeIdxs[T]; 2670 if (Idx.getIndex() == 0) // we haven't seen this type before. 2671 Idx = TypeIdx(NextTypeID++); 2672 2673 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); 2674 2675 // Record the offset for this type. 2676 unsigned Index = Idx.getIndex() - FirstTypeID; 2677 if (TypeOffsets.size() == Index) 2678 TypeOffsets.push_back(Stream.GetCurrentBitNo()); 2679 else if (TypeOffsets.size() < Index) { 2680 TypeOffsets.resize(Index + 1); 2681 TypeOffsets[Index] = Stream.GetCurrentBitNo(); 2682 } 2683 2684 RecordData Record; 2685 2686 // Emit the type's representation. 2687 ASTTypeWriter W(*this, Record); 2688 W.AbbrevToUse = 0; 2689 2690 if (T.hasLocalNonFastQualifiers()) { 2691 Qualifiers Qs = T.getLocalQualifiers(); 2692 AddTypeRef(T.getLocalUnqualifiedType(), Record); 2693 Record.push_back(Qs.getAsOpaqueValue()); 2694 W.Code = TYPE_EXT_QUAL; 2695 W.AbbrevToUse = TypeExtQualAbbrev; 2696 } else { 2697 switch (T->getTypeClass()) { 2698 // For all of the concrete, non-dependent types, call the 2699 // appropriate visitor function. 2700 #define TYPE(Class, Base) \ 2701 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; 2702 #define ABSTRACT_TYPE(Class, Base) 2703 #include "clang/AST/TypeNodes.def" 2704 } 2705 } 2706 2707 // Emit the serialized record. 2708 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); 2709 2710 // Flush any expressions that were written as part of this type. 2711 FlushStmts(); 2712 } 2713 2714 //===----------------------------------------------------------------------===// 2715 // Declaration Serialization 2716 //===----------------------------------------------------------------------===// 2717 2718 /// \brief Write the block containing all of the declaration IDs 2719 /// lexically declared within the given DeclContext. 2720 /// 2721 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the 2722 /// bistream, or 0 if no block was written. 2723 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, 2724 DeclContext *DC) { 2725 if (DC->decls_empty()) 2726 return 0; 2727 2728 uint64_t Offset = Stream.GetCurrentBitNo(); 2729 RecordData Record; 2730 Record.push_back(DECL_CONTEXT_LEXICAL); 2731 SmallVector<uint32_t, 128> KindDeclPairs; 2732 for (const auto *D : DC->decls()) { 2733 KindDeclPairs.push_back(D->getKind()); 2734 KindDeclPairs.push_back(GetDeclRef(D)); 2735 } 2736 2737 ++NumLexicalDeclContexts; 2738 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, 2739 bytes(KindDeclPairs)); 2740 return Offset; 2741 } 2742 2743 void ASTWriter::WriteTypeDeclOffsets() { 2744 using namespace llvm; 2745 RecordData Record; 2746 2747 // Write the type offsets array 2748 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2749 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); 2750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types 2751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index 2752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block 2753 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2754 Record.clear(); 2755 Record.push_back(TYPE_OFFSET); 2756 Record.push_back(TypeOffsets.size()); 2757 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS); 2758 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets)); 2759 2760 // Write the declaration offsets array 2761 Abbrev = new BitCodeAbbrev(); 2762 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); 2763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations 2764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID 2765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block 2766 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 2767 Record.clear(); 2768 Record.push_back(DECL_OFFSET); 2769 Record.push_back(DeclOffsets.size()); 2770 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS); 2771 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets)); 2772 } 2773 2774 void ASTWriter::WriteFileDeclIDsMap() { 2775 using namespace llvm; 2776 RecordData Record; 2777 2778 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs( 2779 FileDeclIDs.begin(), FileDeclIDs.end()); 2780 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(), 2781 llvm::less_first()); 2782 2783 // Join the vectors of DeclIDs from all files. 2784 SmallVector<DeclID, 256> FileGroupedDeclIDs; 2785 for (auto &FileDeclEntry : SortedFileDeclIDs) { 2786 DeclIDInFileInfo &Info = *FileDeclEntry.second; 2787 Info.FirstDeclIndex = FileGroupedDeclIDs.size(); 2788 for (auto &LocDeclEntry : Info.DeclIDs) 2789 FileGroupedDeclIDs.push_back(LocDeclEntry.second); 2790 } 2791 2792 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 2793 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS)); 2794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2796 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); 2797 Record.push_back(FILE_SORTED_DECLS); 2798 Record.push_back(FileGroupedDeclIDs.size()); 2799 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs)); 2800 } 2801 2802 void ASTWriter::WriteComments() { 2803 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3); 2804 ArrayRef<RawComment *> RawComments = Context->Comments.getComments(); 2805 RecordData Record; 2806 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(), 2807 E = RawComments.end(); 2808 I != E; ++I) { 2809 Record.clear(); 2810 AddSourceRange((*I)->getSourceRange(), Record); 2811 Record.push_back((*I)->getKind()); 2812 Record.push_back((*I)->isTrailingComment()); 2813 Record.push_back((*I)->isAlmostTrailingComment()); 2814 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record); 2815 } 2816 Stream.ExitBlock(); 2817 } 2818 2819 //===----------------------------------------------------------------------===// 2820 // Global Method Pool and Selector Serialization 2821 //===----------------------------------------------------------------------===// 2822 2823 namespace { 2824 // Trait used for the on-disk hash table used in the method pool. 2825 class ASTMethodPoolTrait { 2826 ASTWriter &Writer; 2827 2828 public: 2829 typedef Selector key_type; 2830 typedef key_type key_type_ref; 2831 2832 struct data_type { 2833 SelectorID ID; 2834 ObjCMethodList Instance, Factory; 2835 }; 2836 typedef const data_type& data_type_ref; 2837 2838 typedef unsigned hash_value_type; 2839 typedef unsigned offset_type; 2840 2841 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } 2842 2843 static hash_value_type ComputeHash(Selector Sel) { 2844 return serialization::ComputeHash(Sel); 2845 } 2846 2847 std::pair<unsigned,unsigned> 2848 EmitKeyDataLength(raw_ostream& Out, Selector Sel, 2849 data_type_ref Methods) { 2850 using namespace llvm::support; 2851 endian::Writer<little> LE(Out); 2852 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); 2853 LE.write<uint16_t>(KeyLen); 2854 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts 2855 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2856 Method = Method->getNext()) 2857 if (Method->getMethod()) 2858 DataLen += 4; 2859 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2860 Method = Method->getNext()) 2861 if (Method->getMethod()) 2862 DataLen += 4; 2863 LE.write<uint16_t>(DataLen); 2864 return std::make_pair(KeyLen, DataLen); 2865 } 2866 2867 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) { 2868 using namespace llvm::support; 2869 endian::Writer<little> LE(Out); 2870 uint64_t Start = Out.tell(); 2871 assert((Start >> 32) == 0 && "Selector key offset too large"); 2872 Writer.SetSelectorOffset(Sel, Start); 2873 unsigned N = Sel.getNumArgs(); 2874 LE.write<uint16_t>(N); 2875 if (N == 0) 2876 N = 1; 2877 for (unsigned I = 0; I != N; ++I) 2878 LE.write<uint32_t>( 2879 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); 2880 } 2881 2882 void EmitData(raw_ostream& Out, key_type_ref, 2883 data_type_ref Methods, unsigned DataLen) { 2884 using namespace llvm::support; 2885 endian::Writer<little> LE(Out); 2886 uint64_t Start = Out.tell(); (void)Start; 2887 LE.write<uint32_t>(Methods.ID); 2888 unsigned NumInstanceMethods = 0; 2889 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2890 Method = Method->getNext()) 2891 if (Method->getMethod()) 2892 ++NumInstanceMethods; 2893 2894 unsigned NumFactoryMethods = 0; 2895 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2896 Method = Method->getNext()) 2897 if (Method->getMethod()) 2898 ++NumFactoryMethods; 2899 2900 unsigned InstanceBits = Methods.Instance.getBits(); 2901 assert(InstanceBits < 4); 2902 unsigned InstanceHasMoreThanOneDeclBit = 2903 Methods.Instance.hasMoreThanOneDecl(); 2904 unsigned FullInstanceBits = (NumInstanceMethods << 3) | 2905 (InstanceHasMoreThanOneDeclBit << 2) | 2906 InstanceBits; 2907 unsigned FactoryBits = Methods.Factory.getBits(); 2908 assert(FactoryBits < 4); 2909 unsigned FactoryHasMoreThanOneDeclBit = 2910 Methods.Factory.hasMoreThanOneDecl(); 2911 unsigned FullFactoryBits = (NumFactoryMethods << 3) | 2912 (FactoryHasMoreThanOneDeclBit << 2) | 2913 FactoryBits; 2914 LE.write<uint16_t>(FullInstanceBits); 2915 LE.write<uint16_t>(FullFactoryBits); 2916 for (const ObjCMethodList *Method = &Methods.Instance; Method; 2917 Method = Method->getNext()) 2918 if (Method->getMethod()) 2919 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod())); 2920 for (const ObjCMethodList *Method = &Methods.Factory; Method; 2921 Method = Method->getNext()) 2922 if (Method->getMethod()) 2923 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod())); 2924 2925 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 2926 } 2927 }; 2928 } // end anonymous namespace 2929 2930 /// \brief Write ObjC data: selectors and the method pool. 2931 /// 2932 /// The method pool contains both instance and factory methods, stored 2933 /// in an on-disk hash table indexed by the selector. The hash table also 2934 /// contains an empty entry for every other selector known to Sema. 2935 void ASTWriter::WriteSelectors(Sema &SemaRef) { 2936 using namespace llvm; 2937 2938 // Do we have to do anything at all? 2939 if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) 2940 return; 2941 unsigned NumTableEntries = 0; 2942 // Create and write out the blob that contains selectors and the method pool. 2943 { 2944 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; 2945 ASTMethodPoolTrait Trait(*this); 2946 2947 // Create the on-disk hash table representation. We walk through every 2948 // selector we've seen and look it up in the method pool. 2949 SelectorOffsets.resize(NextSelectorID - FirstSelectorID); 2950 for (auto &SelectorAndID : SelectorIDs) { 2951 Selector S = SelectorAndID.first; 2952 SelectorID ID = SelectorAndID.second; 2953 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); 2954 ASTMethodPoolTrait::data_type Data = { 2955 ID, 2956 ObjCMethodList(), 2957 ObjCMethodList() 2958 }; 2959 if (F != SemaRef.MethodPool.end()) { 2960 Data.Instance = F->second.first; 2961 Data.Factory = F->second.second; 2962 } 2963 // Only write this selector if it's not in an existing AST or something 2964 // changed. 2965 if (Chain && ID < FirstSelectorID) { 2966 // Selector already exists. Did it change? 2967 bool changed = false; 2968 for (ObjCMethodList *M = &Data.Instance; 2969 !changed && M && M->getMethod(); M = M->getNext()) { 2970 if (!M->getMethod()->isFromASTFile()) 2971 changed = true; 2972 } 2973 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod(); 2974 M = M->getNext()) { 2975 if (!M->getMethod()->isFromASTFile()) 2976 changed = true; 2977 } 2978 if (!changed) 2979 continue; 2980 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) { 2981 // A new method pool entry. 2982 ++NumTableEntries; 2983 } 2984 Generator.insert(S, Data, Trait); 2985 } 2986 2987 // Create the on-disk hash table in a buffer. 2988 SmallString<4096> MethodPool; 2989 uint32_t BucketOffset; 2990 { 2991 using namespace llvm::support; 2992 ASTMethodPoolTrait Trait(*this); 2993 llvm::raw_svector_ostream Out(MethodPool); 2994 // Make sure that no bucket is at offset 0 2995 endian::Writer<little>(Out).write<uint32_t>(0); 2996 BucketOffset = Generator.Emit(Out, Trait); 2997 } 2998 2999 // Create a blob abbreviation 3000 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3001 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); 3002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 3003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 3004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3005 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); 3006 3007 // Write the method pool 3008 RecordData Record; 3009 Record.push_back(METHOD_POOL); 3010 Record.push_back(BucketOffset); 3011 Record.push_back(NumTableEntries); 3012 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool); 3013 3014 // Create a blob abbreviation for the selector table offsets. 3015 Abbrev = new BitCodeAbbrev(); 3016 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); 3017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size 3018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 3019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3020 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 3021 3022 // Write the selector offsets table. 3023 Record.clear(); 3024 Record.push_back(SELECTOR_OFFSETS); 3025 Record.push_back(SelectorOffsets.size()); 3026 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS); 3027 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, 3028 bytes(SelectorOffsets)); 3029 } 3030 } 3031 3032 /// \brief Write the selectors referenced in @selector expression into AST file. 3033 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { 3034 using namespace llvm; 3035 if (SemaRef.ReferencedSelectors.empty()) 3036 return; 3037 3038 RecordData Record; 3039 3040 // Note: this writes out all references even for a dependent AST. But it is 3041 // very tricky to fix, and given that @selector shouldn't really appear in 3042 // headers, probably not worth it. It's not a correctness issue. 3043 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) { 3044 Selector Sel = SelectorAndLocation.first; 3045 SourceLocation Loc = SelectorAndLocation.second; 3046 AddSelectorRef(Sel, Record); 3047 AddSourceLocation(Loc, Record); 3048 } 3049 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); 3050 } 3051 3052 //===----------------------------------------------------------------------===// 3053 // Identifier Table Serialization 3054 //===----------------------------------------------------------------------===// 3055 3056 /// Determine the declaration that should be put into the name lookup table to 3057 /// represent the given declaration in this module. This is usually D itself, 3058 /// but if D was imported and merged into a local declaration, we want the most 3059 /// recent local declaration instead. The chosen declaration will be the most 3060 /// recent declaration in any module that imports this one. 3061 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts, 3062 NamedDecl *D) { 3063 if (!LangOpts.Modules || !D->isFromASTFile()) 3064 return D; 3065 3066 if (Decl *Redecl = D->getPreviousDecl()) { 3067 // For Redeclarable decls, a prior declaration might be local. 3068 for (; Redecl; Redecl = Redecl->getPreviousDecl()) { 3069 if (!Redecl->isFromASTFile()) 3070 return cast<NamedDecl>(Redecl); 3071 // If we find a decl from a (chained-)PCH stop since we won't find a 3072 // local one. 3073 if (D->getOwningModuleID() == 0) 3074 break; 3075 } 3076 } else if (Decl *First = D->getCanonicalDecl()) { 3077 // For Mergeable decls, the first decl might be local. 3078 if (!First->isFromASTFile()) 3079 return cast<NamedDecl>(First); 3080 } 3081 3082 // All declarations are imported. Our most recent declaration will also be 3083 // the most recent one in anyone who imports us. 3084 return D; 3085 } 3086 3087 namespace { 3088 class ASTIdentifierTableTrait { 3089 ASTWriter &Writer; 3090 Preprocessor &PP; 3091 IdentifierResolver &IdResolver; 3092 bool IsModule; 3093 bool NeedDecls; 3094 ASTWriter::RecordData *InterestingIdentifierOffsets; 3095 3096 /// \brief Determines whether this is an "interesting" identifier that needs a 3097 /// full IdentifierInfo structure written into the hash table. Notably, this 3098 /// doesn't check whether the name has macros defined; use PublicMacroIterator 3099 /// to check that. 3100 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) { 3101 if (MacroOffset || 3102 II->isPoisoned() || 3103 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) || 3104 II->hasRevertedTokenIDToIdentifier() || 3105 (NeedDecls && II->getFETokenInfo<void>())) 3106 return true; 3107 3108 return false; 3109 } 3110 3111 public: 3112 typedef IdentifierInfo* key_type; 3113 typedef key_type key_type_ref; 3114 3115 typedef IdentID data_type; 3116 typedef data_type data_type_ref; 3117 3118 typedef unsigned hash_value_type; 3119 typedef unsigned offset_type; 3120 3121 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, 3122 IdentifierResolver &IdResolver, bool IsModule, 3123 ASTWriter::RecordData *InterestingIdentifierOffsets) 3124 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule), 3125 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus), 3126 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {} 3127 3128 static hash_value_type ComputeHash(const IdentifierInfo* II) { 3129 return llvm::HashString(II->getName()); 3130 } 3131 3132 bool isInterestingIdentifier(const IdentifierInfo *II) { 3133 auto MacroOffset = Writer.getMacroDirectivesOffset(II); 3134 return isInterestingIdentifier(II, MacroOffset); 3135 } 3136 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) { 3137 return isInterestingIdentifier(II, 0); 3138 } 3139 3140 std::pair<unsigned,unsigned> 3141 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) { 3142 unsigned KeyLen = II->getLength() + 1; 3143 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 3144 auto MacroOffset = Writer.getMacroDirectivesOffset(II); 3145 if (isInterestingIdentifier(II, MacroOffset)) { 3146 DataLen += 2; // 2 bytes for builtin ID 3147 DataLen += 2; // 2 bytes for flags 3148 if (MacroOffset) 3149 DataLen += 4; // MacroDirectives offset. 3150 3151 if (NeedDecls) { 3152 for (IdentifierResolver::iterator D = IdResolver.begin(II), 3153 DEnd = IdResolver.end(); 3154 D != DEnd; ++D) 3155 DataLen += 4; 3156 } 3157 } 3158 using namespace llvm::support; 3159 endian::Writer<little> LE(Out); 3160 3161 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen); 3162 LE.write<uint16_t>(DataLen); 3163 // We emit the key length after the data length so that every 3164 // string is preceded by a 16-bit length. This matches the PTH 3165 // format for storing identifiers. 3166 LE.write<uint16_t>(KeyLen); 3167 return std::make_pair(KeyLen, DataLen); 3168 } 3169 3170 void EmitKey(raw_ostream& Out, const IdentifierInfo* II, 3171 unsigned KeyLen) { 3172 // Record the location of the key data. This is used when generating 3173 // the mapping from persistent IDs to strings. 3174 Writer.SetIdentifierOffset(II, Out.tell()); 3175 3176 // Emit the offset of the key/data length information to the interesting 3177 // identifiers table if necessary. 3178 if (InterestingIdentifierOffsets && isInterestingIdentifier(II)) 3179 InterestingIdentifierOffsets->push_back(Out.tell() - 4); 3180 3181 Out.write(II->getNameStart(), KeyLen); 3182 } 3183 3184 void EmitData(raw_ostream& Out, IdentifierInfo* II, 3185 IdentID ID, unsigned) { 3186 using namespace llvm::support; 3187 endian::Writer<little> LE(Out); 3188 3189 auto MacroOffset = Writer.getMacroDirectivesOffset(II); 3190 if (!isInterestingIdentifier(II, MacroOffset)) { 3191 LE.write<uint32_t>(ID << 1); 3192 return; 3193 } 3194 3195 LE.write<uint32_t>((ID << 1) | 0x01); 3196 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID(); 3197 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader."); 3198 LE.write<uint16_t>(Bits); 3199 Bits = 0; 3200 bool HadMacroDefinition = MacroOffset != 0; 3201 Bits = (Bits << 1) | unsigned(HadMacroDefinition); 3202 Bits = (Bits << 1) | unsigned(II->isExtensionToken()); 3203 Bits = (Bits << 1) | unsigned(II->isPoisoned()); 3204 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin()); 3205 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); 3206 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); 3207 LE.write<uint16_t>(Bits); 3208 3209 if (HadMacroDefinition) 3210 LE.write<uint32_t>(MacroOffset); 3211 3212 if (NeedDecls) { 3213 // Emit the declaration IDs in reverse order, because the 3214 // IdentifierResolver provides the declarations as they would be 3215 // visible (e.g., the function "stat" would come before the struct 3216 // "stat"), but the ASTReader adds declarations to the end of the list 3217 // (so we need to see the struct "stat" before the function "stat"). 3218 // Only emit declarations that aren't from a chained PCH, though. 3219 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), 3220 IdResolver.end()); 3221 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(), 3222 DEnd = Decls.rend(); 3223 D != DEnd; ++D) 3224 LE.write<uint32_t>( 3225 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D))); 3226 } 3227 } 3228 }; 3229 } // end anonymous namespace 3230 3231 /// \brief Write the identifier table into the AST file. 3232 /// 3233 /// The identifier table consists of a blob containing string data 3234 /// (the actual identifiers themselves) and a separate "offsets" index 3235 /// that maps identifier IDs to locations within the blob. 3236 void ASTWriter::WriteIdentifierTable(Preprocessor &PP, 3237 IdentifierResolver &IdResolver, 3238 bool IsModule) { 3239 using namespace llvm; 3240 3241 RecordData InterestingIdents; 3242 3243 // Create and write out the blob that contains the identifier 3244 // strings. 3245 { 3246 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; 3247 ASTIdentifierTableTrait Trait( 3248 *this, PP, IdResolver, IsModule, 3249 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr); 3250 3251 // Look for any identifiers that were named while processing the 3252 // headers, but are otherwise not needed. We add these to the hash 3253 // table to enable checking of the predefines buffer in the case 3254 // where the user adds new macro definitions when building the AST 3255 // file. 3256 SmallVector<const IdentifierInfo *, 128> IIs; 3257 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 3258 IDEnd = PP.getIdentifierTable().end(); 3259 ID != IDEnd; ++ID) 3260 IIs.push_back(ID->second); 3261 // Sort the identifiers lexicographically before getting them references so 3262 // that their order is stable. 3263 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>()); 3264 for (const IdentifierInfo *II : IIs) 3265 if (Trait.isInterestingNonMacroIdentifier(II)) 3266 getIdentifierRef(II); 3267 3268 // Create the on-disk hash table representation. We only store offsets 3269 // for identifiers that appear here for the first time. 3270 IdentifierOffsets.resize(NextIdentID - FirstIdentID); 3271 for (auto IdentIDPair : IdentifierIDs) { 3272 IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first); 3273 IdentID ID = IdentIDPair.second; 3274 assert(II && "NULL identifier in identifier table"); 3275 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) 3276 Generator.insert(II, ID, Trait); 3277 } 3278 3279 // Create the on-disk hash table in a buffer. 3280 SmallString<4096> IdentifierTable; 3281 uint32_t BucketOffset; 3282 { 3283 using namespace llvm::support; 3284 llvm::raw_svector_ostream Out(IdentifierTable); 3285 // Make sure that no bucket is at offset 0 3286 endian::Writer<little>(Out).write<uint32_t>(0); 3287 BucketOffset = Generator.Emit(Out, Trait); 3288 } 3289 3290 // Create a blob abbreviation 3291 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3292 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); 3293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 3294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3295 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); 3296 3297 // Write the identifier table 3298 RecordData Record; 3299 Record.push_back(IDENTIFIER_TABLE); 3300 Record.push_back(BucketOffset); 3301 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable); 3302 } 3303 3304 // Write the offsets table for identifier IDs. 3305 BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3306 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); 3307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers 3308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID 3309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3310 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); 3311 3312 #ifndef NDEBUG 3313 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I) 3314 assert(IdentifierOffsets[I] && "Missing identifier offset?"); 3315 #endif 3316 3317 RecordData Record; 3318 Record.push_back(IDENTIFIER_OFFSET); 3319 Record.push_back(IdentifierOffsets.size()); 3320 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS); 3321 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, 3322 bytes(IdentifierOffsets)); 3323 3324 // In C++, write the list of interesting identifiers (those that are 3325 // defined as macros, poisoned, or similar unusual things). 3326 if (!InterestingIdents.empty()) 3327 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents); 3328 } 3329 3330 //===----------------------------------------------------------------------===// 3331 // DeclContext's Name Lookup Table Serialization 3332 //===----------------------------------------------------------------------===// 3333 3334 namespace { 3335 // Trait used for the on-disk hash table used in the method pool. 3336 class ASTDeclContextNameLookupTrait { 3337 ASTWriter &Writer; 3338 3339 public: 3340 typedef DeclarationName key_type; 3341 typedef key_type key_type_ref; 3342 3343 typedef DeclContext::lookup_result data_type; 3344 typedef const data_type& data_type_ref; 3345 3346 typedef unsigned hash_value_type; 3347 typedef unsigned offset_type; 3348 3349 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } 3350 3351 hash_value_type ComputeHash(DeclarationName Name) { 3352 llvm::FoldingSetNodeID ID; 3353 ID.AddInteger(Name.getNameKind()); 3354 3355 switch (Name.getNameKind()) { 3356 case DeclarationName::Identifier: 3357 ID.AddString(Name.getAsIdentifierInfo()->getName()); 3358 break; 3359 case DeclarationName::ObjCZeroArgSelector: 3360 case DeclarationName::ObjCOneArgSelector: 3361 case DeclarationName::ObjCMultiArgSelector: 3362 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); 3363 break; 3364 case DeclarationName::CXXConstructorName: 3365 case DeclarationName::CXXDestructorName: 3366 case DeclarationName::CXXConversionFunctionName: 3367 break; 3368 case DeclarationName::CXXOperatorName: 3369 ID.AddInteger(Name.getCXXOverloadedOperator()); 3370 break; 3371 case DeclarationName::CXXLiteralOperatorName: 3372 ID.AddString(Name.getCXXLiteralIdentifier()->getName()); 3373 case DeclarationName::CXXUsingDirective: 3374 break; 3375 } 3376 3377 return ID.ComputeHash(); 3378 } 3379 3380 std::pair<unsigned,unsigned> 3381 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name, 3382 data_type_ref Lookup) { 3383 using namespace llvm::support; 3384 endian::Writer<little> LE(Out); 3385 unsigned KeyLen = 1; 3386 switch (Name.getNameKind()) { 3387 case DeclarationName::Identifier: 3388 case DeclarationName::ObjCZeroArgSelector: 3389 case DeclarationName::ObjCOneArgSelector: 3390 case DeclarationName::ObjCMultiArgSelector: 3391 case DeclarationName::CXXLiteralOperatorName: 3392 KeyLen += 4; 3393 break; 3394 case DeclarationName::CXXOperatorName: 3395 KeyLen += 1; 3396 break; 3397 case DeclarationName::CXXConstructorName: 3398 case DeclarationName::CXXDestructorName: 3399 case DeclarationName::CXXConversionFunctionName: 3400 case DeclarationName::CXXUsingDirective: 3401 break; 3402 } 3403 LE.write<uint16_t>(KeyLen); 3404 3405 // 4 bytes for each DeclID. 3406 unsigned DataLen = 4 * Lookup.size(); 3407 LE.write<uint16_t>(DataLen); 3408 3409 return std::make_pair(KeyLen, DataLen); 3410 } 3411 3412 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) { 3413 using namespace llvm::support; 3414 endian::Writer<little> LE(Out); 3415 LE.write<uint8_t>(Name.getNameKind()); 3416 switch (Name.getNameKind()) { 3417 case DeclarationName::Identifier: 3418 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo())); 3419 return; 3420 case DeclarationName::ObjCZeroArgSelector: 3421 case DeclarationName::ObjCOneArgSelector: 3422 case DeclarationName::ObjCMultiArgSelector: 3423 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector())); 3424 return; 3425 case DeclarationName::CXXOperatorName: 3426 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS && 3427 "Invalid operator?"); 3428 LE.write<uint8_t>(Name.getCXXOverloadedOperator()); 3429 return; 3430 case DeclarationName::CXXLiteralOperatorName: 3431 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); 3432 return; 3433 case DeclarationName::CXXConstructorName: 3434 case DeclarationName::CXXDestructorName: 3435 case DeclarationName::CXXConversionFunctionName: 3436 case DeclarationName::CXXUsingDirective: 3437 return; 3438 } 3439 3440 llvm_unreachable("Invalid name kind?"); 3441 } 3442 3443 void EmitData(raw_ostream& Out, key_type_ref, 3444 data_type Lookup, unsigned DataLen) { 3445 using namespace llvm::support; 3446 endian::Writer<little> LE(Out); 3447 uint64_t Start = Out.tell(); (void)Start; 3448 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end(); 3449 I != E; ++I) 3450 LE.write<uint32_t>( 3451 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I))); 3452 3453 assert(Out.tell() - Start == DataLen && "Data length is wrong"); 3454 } 3455 }; 3456 } // end anonymous namespace 3457 3458 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result, 3459 DeclContext *DC) { 3460 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage; 3461 } 3462 3463 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result, 3464 DeclContext *DC) { 3465 for (auto *D : Result.getLookupResult()) 3466 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile()) 3467 return false; 3468 3469 return true; 3470 } 3471 3472 uint32_t 3473 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC, 3474 llvm::SmallVectorImpl<char> &LookupTable) { 3475 assert(!ConstDC->HasLazyLocalLexicalLookups && 3476 !ConstDC->HasLazyExternalLexicalLookups && 3477 "must call buildLookups first"); 3478 3479 // FIXME: We need to build the lookups table, which is logically const. 3480 DeclContext *DC = const_cast<DeclContext*>(ConstDC); 3481 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table"); 3482 3483 // Create the on-disk hash table representation. 3484 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> 3485 Generator; 3486 ASTDeclContextNameLookupTrait Trait(*this); 3487 3488 // The first step is to collect the declaration names which we need to 3489 // serialize into the name lookup table, and to collect them in a stable 3490 // order. 3491 SmallVector<DeclarationName, 16> Names; 3492 3493 // We also build up small sets of the constructor and conversion function 3494 // names which are visible. 3495 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet; 3496 3497 for (auto &Lookup : *DC->buildLookup()) { 3498 auto &Name = Lookup.first; 3499 auto &Result = Lookup.second; 3500 3501 // If there are no local declarations in our lookup result, we don't 3502 // need to write an entry for the name at all unless we're rewriting 3503 // the decl context. If we can't write out a lookup set without 3504 // performing more deserialization, just skip this entry. 3505 if (isLookupResultExternal(Result, DC) && !isRewritten(cast<Decl>(DC)) && 3506 isLookupResultEntirelyExternal(Result, DC)) 3507 continue; 3508 3509 // We also skip empty results. If any of the results could be external and 3510 // the currently available results are empty, then all of the results are 3511 // external and we skip it above. So the only way we get here with an empty 3512 // results is when no results could have been external *and* we have 3513 // external results. 3514 // 3515 // FIXME: While we might want to start emitting on-disk entries for negative 3516 // lookups into a decl context as an optimization, today we *have* to skip 3517 // them because there are names with empty lookup results in decl contexts 3518 // which we can't emit in any stable ordering: we lookup constructors and 3519 // conversion functions in the enclosing namespace scope creating empty 3520 // results for them. This in almost certainly a bug in Clang's name lookup, 3521 // but that is likely to be hard or impossible to fix and so we tolerate it 3522 // here by omitting lookups with empty results. 3523 if (Lookup.second.getLookupResult().empty()) 3524 continue; 3525 3526 switch (Lookup.first.getNameKind()) { 3527 default: 3528 Names.push_back(Lookup.first); 3529 break; 3530 3531 case DeclarationName::CXXConstructorName: 3532 assert(isa<CXXRecordDecl>(DC) && 3533 "Cannot have a constructor name outside of a class!"); 3534 ConstructorNameSet.insert(Name); 3535 break; 3536 3537 case DeclarationName::CXXConversionFunctionName: 3538 assert(isa<CXXRecordDecl>(DC) && 3539 "Cannot have a conversion function name outside of a class!"); 3540 ConversionNameSet.insert(Name); 3541 break; 3542 } 3543 } 3544 3545 // Sort the names into a stable order. 3546 std::sort(Names.begin(), Names.end()); 3547 3548 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) { 3549 // We need to establish an ordering of constructor and conversion function 3550 // names, and they don't have an intrinsic ordering. 3551 3552 // First we try the easy case by forming the current context's constructor 3553 // name and adding that name first. This is a very useful optimization to 3554 // avoid walking the lexical declarations in many cases, and it also 3555 // handles the only case where a constructor name can come from some other 3556 // lexical context -- when that name is an implicit constructor merged from 3557 // another declaration in the redecl chain. Any non-implicit constructor or 3558 // conversion function which doesn't occur in all the lexical contexts 3559 // would be an ODR violation. 3560 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName( 3561 Context->getCanonicalType(Context->getRecordType(D))); 3562 if (ConstructorNameSet.erase(ImplicitCtorName)) 3563 Names.push_back(ImplicitCtorName); 3564 3565 // If we still have constructors or conversion functions, we walk all the 3566 // names in the decl and add the constructors and conversion functions 3567 // which are visible in the order they lexically occur within the context. 3568 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty()) 3569 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls()) 3570 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) { 3571 auto Name = ChildND->getDeclName(); 3572 switch (Name.getNameKind()) { 3573 default: 3574 continue; 3575 3576 case DeclarationName::CXXConstructorName: 3577 if (ConstructorNameSet.erase(Name)) 3578 Names.push_back(Name); 3579 break; 3580 3581 case DeclarationName::CXXConversionFunctionName: 3582 if (ConversionNameSet.erase(Name)) 3583 Names.push_back(Name); 3584 break; 3585 } 3586 3587 if (ConstructorNameSet.empty() && ConversionNameSet.empty()) 3588 break; 3589 } 3590 3591 assert(ConstructorNameSet.empty() && "Failed to find all of the visible " 3592 "constructors by walking all the " 3593 "lexical members of the context."); 3594 assert(ConversionNameSet.empty() && "Failed to find all of the visible " 3595 "conversion functions by walking all " 3596 "the lexical members of the context."); 3597 } 3598 3599 // Next we need to do a lookup with each name into this decl context to fully 3600 // populate any results from external sources. We don't actually use the 3601 // results of these lookups because we only want to use the results after all 3602 // results have been loaded and the pointers into them will be stable. 3603 for (auto &Name : Names) 3604 DC->lookup(Name); 3605 3606 // Now we need to insert the results for each name into the hash table. For 3607 // constructor names and conversion function names, we actually need to merge 3608 // all of the results for them into one list of results each and insert 3609 // those. 3610 SmallVector<NamedDecl *, 8> ConstructorDecls; 3611 SmallVector<NamedDecl *, 8> ConversionDecls; 3612 3613 // Now loop over the names, either inserting them or appending for the two 3614 // special cases. 3615 for (auto &Name : Names) { 3616 DeclContext::lookup_result Result = DC->noload_lookup(Name); 3617 3618 switch (Name.getNameKind()) { 3619 default: 3620 Generator.insert(Name, Result, Trait); 3621 break; 3622 3623 case DeclarationName::CXXConstructorName: 3624 ConstructorDecls.append(Result.begin(), Result.end()); 3625 break; 3626 3627 case DeclarationName::CXXConversionFunctionName: 3628 ConversionDecls.append(Result.begin(), Result.end()); 3629 break; 3630 } 3631 } 3632 3633 // Handle our two special cases if we ended up having any. We arbitrarily use 3634 // the first declaration's name here because the name itself isn't part of 3635 // the key, only the kind of name is used. 3636 if (!ConstructorDecls.empty()) 3637 Generator.insert(ConstructorDecls.front()->getDeclName(), 3638 DeclContext::lookup_result(ConstructorDecls), Trait); 3639 if (!ConversionDecls.empty()) 3640 Generator.insert(ConversionDecls.front()->getDeclName(), 3641 DeclContext::lookup_result(ConversionDecls), Trait); 3642 3643 // Create the on-disk hash table in a buffer. 3644 llvm::raw_svector_ostream Out(LookupTable); 3645 // Make sure that no bucket is at offset 0 3646 using namespace llvm::support; 3647 endian::Writer<little>(Out).write<uint32_t>(0); 3648 return Generator.Emit(Out, Trait); 3649 } 3650 3651 /// \brief Write the block containing all of the declaration IDs 3652 /// visible from the given DeclContext. 3653 /// 3654 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the 3655 /// bitstream, or 0 if no block was written. 3656 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, 3657 DeclContext *DC) { 3658 // If we imported a key declaration of this namespace, write the visible 3659 // lookup results as an update record for it rather than including them 3660 // on this declaration. We will only look at key declarations on reload. 3661 if (isa<NamespaceDecl>(DC) && Chain && 3662 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) { 3663 // Only do this once, for the first local declaration of the namespace. 3664 for (NamespaceDecl *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev; 3665 Prev = Prev->getPreviousDecl()) 3666 if (!Prev->isFromASTFile()) 3667 return 0; 3668 3669 // Note that we need to emit an update record for the primary context. 3670 UpdatedDeclContexts.insert(DC->getPrimaryContext()); 3671 3672 // Make sure all visible decls are written. They will be recorded later. We 3673 // do this using a side data structure so we can sort the names into 3674 // a deterministic order. 3675 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup(); 3676 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16> 3677 LookupResults; 3678 if (Map) { 3679 LookupResults.reserve(Map->size()); 3680 for (auto &Entry : *Map) 3681 LookupResults.push_back( 3682 std::make_pair(Entry.first, Entry.second.getLookupResult())); 3683 } 3684 3685 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first()); 3686 for (auto &NameAndResult : LookupResults) { 3687 DeclarationName Name = NameAndResult.first; 3688 DeclContext::lookup_result Result = NameAndResult.second; 3689 if (Name.getNameKind() == DeclarationName::CXXConstructorName || 3690 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { 3691 // We have to work around a name lookup bug here where negative lookup 3692 // results for these names get cached in namespace lookup tables (these 3693 // names should never be looked up in a namespace). 3694 assert(Result.empty() && "Cannot have a constructor or conversion " 3695 "function name in a namespace!"); 3696 continue; 3697 } 3698 3699 for (NamedDecl *ND : Result) 3700 if (!ND->isFromASTFile()) 3701 GetDeclRef(ND); 3702 } 3703 3704 return 0; 3705 } 3706 3707 if (DC->getPrimaryContext() != DC) 3708 return 0; 3709 3710 // Skip contexts which don't support name lookup. 3711 if (!DC->isLookupContext()) 3712 return 0; 3713 3714 // If not in C++, we perform name lookup for the translation unit via the 3715 // IdentifierInfo chains, don't bother to build a visible-declarations table. 3716 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus) 3717 return 0; 3718 3719 // Serialize the contents of the mapping used for lookup. Note that, 3720 // although we have two very different code paths, the serialized 3721 // representation is the same for both cases: a declaration name, 3722 // followed by a size, followed by references to the visible 3723 // declarations that have that name. 3724 uint64_t Offset = Stream.GetCurrentBitNo(); 3725 StoredDeclsMap *Map = DC->buildLookup(); 3726 if (!Map || Map->empty()) 3727 return 0; 3728 3729 // Create the on-disk hash table in a buffer. 3730 SmallString<4096> LookupTable; 3731 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable); 3732 3733 // Write the lookup table 3734 RecordData Record; 3735 Record.push_back(DECL_CONTEXT_VISIBLE); 3736 Record.push_back(BucketOffset); 3737 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, 3738 LookupTable); 3739 ++NumVisibleDeclContexts; 3740 return Offset; 3741 } 3742 3743 /// \brief Write an UPDATE_VISIBLE block for the given context. 3744 /// 3745 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing 3746 /// DeclContext in a dependent AST file. As such, they only exist for the TU 3747 /// (in C++), for namespaces, and for classes with forward-declared unscoped 3748 /// enumeration members (in C++11). 3749 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { 3750 if (isRewritten(cast<Decl>(DC))) 3751 return; 3752 3753 StoredDeclsMap *Map = DC->getLookupPtr(); 3754 if (!Map || Map->empty()) 3755 return; 3756 3757 // Create the on-disk hash table in a buffer. 3758 SmallString<4096> LookupTable; 3759 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable); 3760 3761 // If we're updating a namespace, select a key declaration as the key for the 3762 // update record; those are the only ones that will be checked on reload. 3763 if (isa<NamespaceDecl>(DC)) 3764 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC))); 3765 3766 // Write the lookup table 3767 RecordData Record; 3768 Record.push_back(UPDATE_VISIBLE); 3769 Record.push_back(getDeclID(cast<Decl>(DC))); 3770 Record.push_back(BucketOffset); 3771 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable); 3772 } 3773 3774 /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions. 3775 void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) { 3776 RecordData Record; 3777 Record.push_back(Opts.fp_contract); 3778 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record); 3779 } 3780 3781 /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions. 3782 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) { 3783 if (!SemaRef.Context.getLangOpts().OpenCL) 3784 return; 3785 3786 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions(); 3787 RecordData Record; 3788 #define OPENCLEXT(nm) Record.push_back(Opts.nm); 3789 #include "clang/Basic/OpenCLExtensions.def" 3790 Stream.EmitRecord(OPENCL_EXTENSIONS, Record); 3791 } 3792 3793 void ASTWriter::WriteRedeclarations() { 3794 RecordData LocalRedeclChains; 3795 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap; 3796 3797 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) { 3798 const Decl *Key = Redeclarations[I]; 3799 assert((Chain ? Chain->getKeyDeclaration(Key) == Key 3800 : Key->isFirstDecl()) && 3801 "not the key declaration"); 3802 3803 const Decl *First = Key->getCanonicalDecl(); 3804 const Decl *MostRecent = First->getMostRecentDecl(); 3805 3806 assert((getDeclID(First) >= NUM_PREDEF_DECL_IDS || First == Key) && 3807 "should not have imported key decls for predefined decl"); 3808 3809 // If we only have a single declaration, there is no point in storing 3810 // a redeclaration chain. 3811 if (First == MostRecent) 3812 continue; 3813 3814 unsigned Offset = LocalRedeclChains.size(); 3815 unsigned Size = 0; 3816 LocalRedeclChains.push_back(0); // Placeholder for the size. 3817 3818 // Collect the set of local redeclarations of this declaration, from newest 3819 // to oldest. 3820 for (const Decl *Prev = MostRecent; Prev; 3821 Prev = Prev->getPreviousDecl()) { 3822 if (!Prev->isFromASTFile() && Prev != Key) { 3823 AddDeclRef(Prev, LocalRedeclChains); 3824 ++Size; 3825 } 3826 } 3827 3828 LocalRedeclChains[Offset] = Size; 3829 3830 // Add the mapping from the first ID from the AST to the set of local 3831 // declarations. 3832 LocalRedeclarationsInfo Info = { getDeclID(Key), Offset }; 3833 LocalRedeclsMap.push_back(Info); 3834 3835 assert(N == Redeclarations.size() && 3836 "Deserialized a declaration we shouldn't have"); 3837 } 3838 3839 if (LocalRedeclChains.empty()) 3840 return; 3841 3842 // Sort the local redeclarations map by the first declaration ID, 3843 // since the reader will be performing binary searches on this information. 3844 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end()); 3845 3846 // Emit the local redeclarations map. 3847 using namespace llvm; 3848 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3849 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP)); 3850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries 3851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3852 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); 3853 3854 RecordData Record; 3855 Record.push_back(LOCAL_REDECLARATIONS_MAP); 3856 Record.push_back(LocalRedeclsMap.size()); 3857 Stream.EmitRecordWithBlob(AbbrevID, Record, 3858 reinterpret_cast<char*>(LocalRedeclsMap.data()), 3859 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo)); 3860 3861 // Emit the redeclaration chains. 3862 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains); 3863 } 3864 3865 void ASTWriter::WriteObjCCategories() { 3866 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap; 3867 RecordData Categories; 3868 3869 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) { 3870 unsigned Size = 0; 3871 unsigned StartIndex = Categories.size(); 3872 3873 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I]; 3874 3875 // Allocate space for the size. 3876 Categories.push_back(0); 3877 3878 // Add the categories. 3879 for (ObjCInterfaceDecl::known_categories_iterator 3880 Cat = Class->known_categories_begin(), 3881 CatEnd = Class->known_categories_end(); 3882 Cat != CatEnd; ++Cat, ++Size) { 3883 assert(getDeclID(*Cat) != 0 && "Bogus category"); 3884 AddDeclRef(*Cat, Categories); 3885 } 3886 3887 // Update the size. 3888 Categories[StartIndex] = Size; 3889 3890 // Record this interface -> category map. 3891 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex }; 3892 CategoriesMap.push_back(CatInfo); 3893 } 3894 3895 // Sort the categories map by the definition ID, since the reader will be 3896 // performing binary searches on this information. 3897 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end()); 3898 3899 // Emit the categories map. 3900 using namespace llvm; 3901 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 3902 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP)); 3903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries 3904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 3905 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); 3906 3907 RecordData Record; 3908 Record.push_back(OBJC_CATEGORIES_MAP); 3909 Record.push_back(CategoriesMap.size()); 3910 Stream.EmitRecordWithBlob(AbbrevID, Record, 3911 reinterpret_cast<char*>(CategoriesMap.data()), 3912 CategoriesMap.size() * sizeof(ObjCCategoriesInfo)); 3913 3914 // Emit the category lists. 3915 Stream.EmitRecord(OBJC_CATEGORIES, Categories); 3916 } 3917 3918 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) { 3919 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap; 3920 3921 if (LPTMap.empty()) 3922 return; 3923 3924 RecordData Record; 3925 for (auto LPTMapEntry : LPTMap) { 3926 const FunctionDecl *FD = LPTMapEntry.first; 3927 LateParsedTemplate *LPT = LPTMapEntry.second; 3928 AddDeclRef(FD, Record); 3929 AddDeclRef(LPT->D, Record); 3930 Record.push_back(LPT->Toks.size()); 3931 3932 for (CachedTokens::iterator TokIt = LPT->Toks.begin(), 3933 TokEnd = LPT->Toks.end(); 3934 TokIt != TokEnd; ++TokIt) { 3935 AddToken(*TokIt, Record); 3936 } 3937 } 3938 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record); 3939 } 3940 3941 /// \brief Write the state of 'pragma clang optimize' at the end of the module. 3942 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) { 3943 RecordData Record; 3944 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation(); 3945 AddSourceLocation(PragmaLoc, Record); 3946 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record); 3947 } 3948 3949 //===----------------------------------------------------------------------===// 3950 // General Serialization Routines 3951 //===----------------------------------------------------------------------===// 3952 3953 /// \brief Write a record containing the given attributes. 3954 void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs, 3955 RecordDataImpl &Record) { 3956 Record.push_back(Attrs.size()); 3957 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(), 3958 e = Attrs.end(); i != e; ++i){ 3959 const Attr *A = *i; 3960 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs 3961 AddSourceRange(A->getRange(), Record); 3962 3963 #include "clang/Serialization/AttrPCHWrite.inc" 3964 3965 } 3966 } 3967 3968 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) { 3969 AddSourceLocation(Tok.getLocation(), Record); 3970 Record.push_back(Tok.getLength()); 3971 3972 // FIXME: When reading literal tokens, reconstruct the literal pointer 3973 // if it is needed. 3974 AddIdentifierRef(Tok.getIdentifierInfo(), Record); 3975 // FIXME: Should translate token kind to a stable encoding. 3976 Record.push_back(Tok.getKind()); 3977 // FIXME: Should translate token flags to a stable encoding. 3978 Record.push_back(Tok.getFlags()); 3979 } 3980 3981 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) { 3982 Record.push_back(Str.size()); 3983 Record.insert(Record.end(), Str.begin(), Str.end()); 3984 } 3985 3986 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) { 3987 assert(Context && "should have context when outputting path"); 3988 3989 bool Changed = 3990 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path); 3991 3992 // Remove a prefix to make the path relative, if relevant. 3993 const char *PathBegin = Path.data(); 3994 const char *PathPtr = 3995 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory); 3996 if (PathPtr != PathBegin) { 3997 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin)); 3998 Changed = true; 3999 } 4000 4001 return Changed; 4002 } 4003 4004 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) { 4005 SmallString<128> FilePath(Path); 4006 PreparePathForOutput(FilePath); 4007 AddString(FilePath, Record); 4008 } 4009 4010 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record, 4011 StringRef Path) { 4012 SmallString<128> FilePath(Path); 4013 PreparePathForOutput(FilePath); 4014 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath); 4015 } 4016 4017 void ASTWriter::AddVersionTuple(const VersionTuple &Version, 4018 RecordDataImpl &Record) { 4019 Record.push_back(Version.getMajor()); 4020 if (Optional<unsigned> Minor = Version.getMinor()) 4021 Record.push_back(*Minor + 1); 4022 else 4023 Record.push_back(0); 4024 if (Optional<unsigned> Subminor = Version.getSubminor()) 4025 Record.push_back(*Subminor + 1); 4026 else 4027 Record.push_back(0); 4028 } 4029 4030 /// \brief Note that the identifier II occurs at the given offset 4031 /// within the identifier table. 4032 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { 4033 IdentID ID = IdentifierIDs[II]; 4034 // Only store offsets new to this AST file. Other identifier names are looked 4035 // up earlier in the chain and thus don't need an offset. 4036 if (ID >= FirstIdentID) 4037 IdentifierOffsets[ID - FirstIdentID] = Offset; 4038 } 4039 4040 /// \brief Note that the selector Sel occurs at the given offset 4041 /// within the method pool/selector table. 4042 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { 4043 unsigned ID = SelectorIDs[Sel]; 4044 assert(ID && "Unknown selector"); 4045 // Don't record offsets for selectors that are also available in a different 4046 // file. 4047 if (ID < FirstSelectorID) 4048 return; 4049 SelectorOffsets[ID - FirstSelectorID] = Offset; 4050 } 4051 4052 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) 4053 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr), 4054 WritingModule(nullptr), WritingAST(false), 4055 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false), 4056 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID), 4057 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), 4058 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID), 4059 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID), 4060 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS), 4061 NextSubmoduleID(FirstSubmoduleID), 4062 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID), 4063 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0), 4064 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0), 4065 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1), 4066 TypeExtQualAbbrev(0), 4067 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0), 4068 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0), 4069 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0), 4070 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0), 4071 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0), 4072 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0), 4073 ExprImplicitCastAbbrev(0) {} 4074 4075 ASTWriter::~ASTWriter() { 4076 llvm::DeleteContainerSeconds(FileDeclIDs); 4077 } 4078 4079 const LangOptions &ASTWriter::getLangOpts() const { 4080 assert(WritingAST && "can't determine lang opts when not writing AST"); 4081 return Context->getLangOpts(); 4082 } 4083 4084 void ASTWriter::WriteAST(Sema &SemaRef, 4085 const std::string &OutputFile, 4086 Module *WritingModule, StringRef isysroot, 4087 bool hasErrors) { 4088 WritingAST = true; 4089 4090 ASTHasCompilerErrors = hasErrors; 4091 4092 // Emit the file header. 4093 Stream.Emit((unsigned)'C', 8); 4094 Stream.Emit((unsigned)'P', 8); 4095 Stream.Emit((unsigned)'C', 8); 4096 Stream.Emit((unsigned)'H', 8); 4097 4098 WriteBlockInfoBlock(); 4099 4100 Context = &SemaRef.Context; 4101 PP = &SemaRef.PP; 4102 this->WritingModule = WritingModule; 4103 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule); 4104 Context = nullptr; 4105 PP = nullptr; 4106 this->WritingModule = nullptr; 4107 this->BaseDirectory.clear(); 4108 4109 WritingAST = false; 4110 } 4111 4112 template<typename Vector> 4113 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec, 4114 ASTWriter::RecordData &Record) { 4115 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end(); 4116 I != E; ++I) { 4117 Writer.AddDeclRef(*I, Record); 4118 } 4119 } 4120 4121 void ASTWriter::WriteASTCore(Sema &SemaRef, 4122 StringRef isysroot, 4123 const std::string &OutputFile, 4124 Module *WritingModule) { 4125 using namespace llvm; 4126 4127 bool isModule = WritingModule != nullptr; 4128 4129 // Make sure that the AST reader knows to finalize itself. 4130 if (Chain) 4131 Chain->finalizeForWriting(); 4132 4133 ASTContext &Context = SemaRef.Context; 4134 Preprocessor &PP = SemaRef.PP; 4135 4136 // Set up predefined declaration IDs. 4137 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) { 4138 if (D) { 4139 assert(D->isCanonicalDecl() && "predefined decl is not canonical"); 4140 DeclIDs[D] = ID; 4141 if (D->getMostRecentDecl() != D) 4142 Redeclarations.push_back(D); 4143 } 4144 }; 4145 RegisterPredefDecl(Context.getTranslationUnitDecl(), 4146 PREDEF_DECL_TRANSLATION_UNIT_ID); 4147 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID); 4148 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID); 4149 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID); 4150 RegisterPredefDecl(Context.ObjCProtocolClassDecl, 4151 PREDEF_DECL_OBJC_PROTOCOL_ID); 4152 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID); 4153 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID); 4154 RegisterPredefDecl(Context.ObjCInstanceTypeDecl, 4155 PREDEF_DECL_OBJC_INSTANCETYPE_ID); 4156 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID); 4157 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG); 4158 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID); 4159 4160 // Build a record containing all of the tentative definitions in this file, in 4161 // TentativeDefinitions order. Generally, this record will be empty for 4162 // headers. 4163 RecordData TentativeDefinitions; 4164 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions); 4165 4166 // Build a record containing all of the file scoped decls in this file. 4167 RecordData UnusedFileScopedDecls; 4168 if (!isModule) 4169 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls, 4170 UnusedFileScopedDecls); 4171 4172 // Build a record containing all of the delegating constructors we still need 4173 // to resolve. 4174 RecordData DelegatingCtorDecls; 4175 if (!isModule) 4176 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls); 4177 4178 // Write the set of weak, undeclared identifiers. We always write the 4179 // entire table, since later PCH files in a PCH chain are only interested in 4180 // the results at the end of the chain. 4181 RecordData WeakUndeclaredIdentifiers; 4182 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) { 4183 IdentifierInfo *II = WeakUndeclaredIdentifier.first; 4184 WeakInfo &WI = WeakUndeclaredIdentifier.second; 4185 AddIdentifierRef(II, WeakUndeclaredIdentifiers); 4186 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers); 4187 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers); 4188 WeakUndeclaredIdentifiers.push_back(WI.getUsed()); 4189 } 4190 4191 // Build a record containing all of the ext_vector declarations. 4192 RecordData ExtVectorDecls; 4193 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls); 4194 4195 // Build a record containing all of the VTable uses information. 4196 RecordData VTableUses; 4197 if (!SemaRef.VTableUses.empty()) { 4198 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { 4199 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); 4200 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); 4201 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); 4202 } 4203 } 4204 4205 // Build a record containing all of the UnusedLocalTypedefNameCandidates. 4206 RecordData UnusedLocalTypedefNameCandidates; 4207 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates) 4208 AddDeclRef(TD, UnusedLocalTypedefNameCandidates); 4209 4210 // Build a record containing all of pending implicit instantiations. 4211 RecordData PendingInstantiations; 4212 for (std::deque<Sema::PendingImplicitInstantiation>::iterator 4213 I = SemaRef.PendingInstantiations.begin(), 4214 N = SemaRef.PendingInstantiations.end(); I != N; ++I) { 4215 AddDeclRef(I->first, PendingInstantiations); 4216 AddSourceLocation(I->second, PendingInstantiations); 4217 } 4218 assert(SemaRef.PendingLocalImplicitInstantiations.empty() && 4219 "There are local ones at end of translation unit!"); 4220 4221 // Build a record containing some declaration references. 4222 RecordData SemaDeclRefs; 4223 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { 4224 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); 4225 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); 4226 } 4227 4228 RecordData CUDASpecialDeclRefs; 4229 if (Context.getcudaConfigureCallDecl()) { 4230 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs); 4231 } 4232 4233 // Build a record containing all of the known namespaces. 4234 RecordData KnownNamespaces; 4235 for (llvm::MapVector<NamespaceDecl*, bool>::iterator 4236 I = SemaRef.KnownNamespaces.begin(), 4237 IEnd = SemaRef.KnownNamespaces.end(); 4238 I != IEnd; ++I) { 4239 if (!I->second) 4240 AddDeclRef(I->first, KnownNamespaces); 4241 } 4242 4243 // Build a record of all used, undefined objects that require definitions. 4244 RecordData UndefinedButUsed; 4245 4246 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined; 4247 SemaRef.getUndefinedButUsed(Undefined); 4248 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator 4249 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) { 4250 AddDeclRef(I->first, UndefinedButUsed); 4251 AddSourceLocation(I->second, UndefinedButUsed); 4252 } 4253 4254 // Build a record containing all delete-expressions that we would like to 4255 // analyze later in AST. 4256 RecordData DeleteExprsToAnalyze; 4257 4258 for (const auto &DeleteExprsInfo : 4259 SemaRef.getMismatchingDeleteExpressions()) { 4260 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze); 4261 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size()); 4262 for (const auto &DeleteLoc : DeleteExprsInfo.second) { 4263 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze); 4264 DeleteExprsToAnalyze.push_back(DeleteLoc.second); 4265 } 4266 } 4267 4268 // Write the control block 4269 WriteControlBlock(PP, Context, isysroot, OutputFile); 4270 4271 // Write the remaining AST contents. 4272 RecordData Record; 4273 Stream.EnterSubblock(AST_BLOCK_ID, 5); 4274 4275 // This is so that older clang versions, before the introduction 4276 // of the control block, can read and reject the newer PCH format. 4277 Record.clear(); 4278 Record.push_back(VERSION_MAJOR); 4279 Stream.EmitRecord(METADATA_OLD_FORMAT, Record); 4280 4281 // Create a lexical update block containing all of the declarations in the 4282 // translation unit that do not come from other AST files. 4283 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); 4284 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs; 4285 for (const auto *D : TU->noload_decls()) { 4286 if (!D->isFromASTFile()) { 4287 NewGlobalKindDeclPairs.push_back(D->getKind()); 4288 NewGlobalKindDeclPairs.push_back(GetDeclRef(D)); 4289 } 4290 } 4291 4292 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); 4293 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); 4294 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 4295 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); 4296 Record.clear(); 4297 Record.push_back(TU_UPDATE_LEXICAL); 4298 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, 4299 bytes(NewGlobalKindDeclPairs)); 4300 4301 // And a visible updates block for the translation unit. 4302 Abv = new llvm::BitCodeAbbrev(); 4303 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); 4304 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); 4305 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); 4306 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); 4307 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); 4308 WriteDeclContextVisibleUpdate(TU); 4309 4310 // If we have any extern "C" names, write out a visible update for them. 4311 if (Context.ExternCContext) 4312 WriteDeclContextVisibleUpdate(Context.ExternCContext); 4313 4314 // If the translation unit has an anonymous namespace, and we don't already 4315 // have an update block for it, write it as an update block. 4316 // FIXME: Why do we not do this if there's already an update block? 4317 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) { 4318 ASTWriter::UpdateRecord &Record = DeclUpdates[TU]; 4319 if (Record.empty()) 4320 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS)); 4321 } 4322 4323 // Add update records for all mangling numbers and static local numbers. 4324 // These aren't really update records, but this is a convenient way of 4325 // tagging this rare extra data onto the declarations. 4326 for (const auto &Number : Context.MangleNumbers) 4327 if (!Number.first->isFromASTFile()) 4328 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER, 4329 Number.second)); 4330 for (const auto &Number : Context.StaticLocalNumbers) 4331 if (!Number.first->isFromASTFile()) 4332 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER, 4333 Number.second)); 4334 4335 // Make sure visible decls, added to DeclContexts previously loaded from 4336 // an AST file, are registered for serialization. 4337 for (SmallVectorImpl<const Decl *>::iterator 4338 I = UpdatingVisibleDecls.begin(), 4339 E = UpdatingVisibleDecls.end(); I != E; ++I) { 4340 GetDeclRef(*I); 4341 } 4342 4343 // Make sure all decls associated with an identifier are registered for 4344 // serialization. 4345 llvm::SmallVector<const IdentifierInfo*, 256> IIs; 4346 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), 4347 IDEnd = PP.getIdentifierTable().end(); 4348 ID != IDEnd; ++ID) { 4349 const IdentifierInfo *II = ID->second; 4350 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) 4351 IIs.push_back(II); 4352 } 4353 // Sort the identifiers to visit based on their name. 4354 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>()); 4355 for (const IdentifierInfo *II : IIs) { 4356 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II), 4357 DEnd = SemaRef.IdResolver.end(); 4358 D != DEnd; ++D) { 4359 GetDeclRef(*D); 4360 } 4361 } 4362 4363 // Form the record of special types. 4364 RecordData SpecialTypes; 4365 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); 4366 AddTypeRef(Context.getFILEType(), SpecialTypes); 4367 AddTypeRef(Context.getjmp_bufType(), SpecialTypes); 4368 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); 4369 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); 4370 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); 4371 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); 4372 AddTypeRef(Context.getucontext_tType(), SpecialTypes); 4373 4374 if (Chain) { 4375 // Write the mapping information describing our module dependencies and how 4376 // each of those modules were mapped into our own offset/ID space, so that 4377 // the reader can build the appropriate mapping to its own offset/ID space. 4378 // The map consists solely of a blob with the following format: 4379 // *(module-name-len:i16 module-name:len*i8 4380 // source-location-offset:i32 4381 // identifier-id:i32 4382 // preprocessed-entity-id:i32 4383 // macro-definition-id:i32 4384 // submodule-id:i32 4385 // selector-id:i32 4386 // declaration-id:i32 4387 // c++-base-specifiers-id:i32 4388 // type-id:i32) 4389 // 4390 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); 4391 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP)); 4392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 4393 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev); 4394 SmallString<2048> Buffer; 4395 { 4396 llvm::raw_svector_ostream Out(Buffer); 4397 for (ModuleFile *M : Chain->ModuleMgr) { 4398 using namespace llvm::support; 4399 endian::Writer<little> LE(Out); 4400 StringRef FileName = M->FileName; 4401 LE.write<uint16_t>(FileName.size()); 4402 Out.write(FileName.data(), FileName.size()); 4403 4404 // Note: if a base ID was uint max, it would not be possible to load 4405 // another module after it or have more than one entity inside it. 4406 uint32_t None = std::numeric_limits<uint32_t>::max(); 4407 4408 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) { 4409 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high"); 4410 if (ShouldWrite) 4411 LE.write<uint32_t>(BaseID); 4412 else 4413 LE.write<uint32_t>(None); 4414 }; 4415 4416 // These values should be unique within a chain, since they will be read 4417 // as keys into ContinuousRangeMaps. 4418 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries); 4419 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers); 4420 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros); 4421 writeBaseIDOrNone(M->BasePreprocessedEntityID, 4422 M->NumPreprocessedEntities); 4423 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules); 4424 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors); 4425 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls); 4426 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes); 4427 } 4428 } 4429 Record.clear(); 4430 Record.push_back(MODULE_OFFSET_MAP); 4431 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record, 4432 Buffer.data(), Buffer.size()); 4433 } 4434 4435 RecordData DeclUpdatesOffsetsRecord; 4436 4437 // Keep writing types, declarations, and declaration update records 4438 // until we've emitted all of them. 4439 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5); 4440 WriteTypeAbbrevs(); 4441 WriteDeclAbbrevs(); 4442 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(), 4443 E = DeclsToRewrite.end(); 4444 I != E; ++I) 4445 DeclTypesToEmit.push(const_cast<Decl*>(*I)); 4446 do { 4447 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord); 4448 while (!DeclTypesToEmit.empty()) { 4449 DeclOrType DOT = DeclTypesToEmit.front(); 4450 DeclTypesToEmit.pop(); 4451 if (DOT.isType()) 4452 WriteType(DOT.getType()); 4453 else 4454 WriteDecl(Context, DOT.getDecl()); 4455 } 4456 } while (!DeclUpdates.empty()); 4457 Stream.ExitBlock(); 4458 4459 DoneWritingDeclsAndTypes = true; 4460 4461 // These things can only be done once we've written out decls and types. 4462 WriteTypeDeclOffsets(); 4463 if (!DeclUpdatesOffsetsRecord.empty()) 4464 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord); 4465 WriteCXXBaseSpecifiersOffsets(); 4466 WriteCXXCtorInitializersOffsets(); 4467 WriteFileDeclIDsMap(); 4468 WriteSourceManagerBlock(Context.getSourceManager(), PP); 4469 4470 WriteComments(); 4471 WritePreprocessor(PP, isModule); 4472 WriteHeaderSearch(PP.getHeaderSearchInfo()); 4473 WriteSelectors(SemaRef); 4474 WriteReferencedSelectorsPool(SemaRef); 4475 WriteLateParsedTemplates(SemaRef); 4476 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule); 4477 WriteFPPragmaOptions(SemaRef.getFPOptions()); 4478 WriteOpenCLExtensions(SemaRef); 4479 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule); 4480 4481 // If we're emitting a module, write out the submodule information. 4482 if (WritingModule) 4483 WriteSubmodules(WritingModule); 4484 4485 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes); 4486 4487 // Write the record containing external, unnamed definitions. 4488 if (!EagerlyDeserializedDecls.empty()) 4489 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls); 4490 4491 // Write the record containing tentative definitions. 4492 if (!TentativeDefinitions.empty()) 4493 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); 4494 4495 // Write the record containing unused file scoped decls. 4496 if (!UnusedFileScopedDecls.empty()) 4497 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); 4498 4499 // Write the record containing weak undeclared identifiers. 4500 if (!WeakUndeclaredIdentifiers.empty()) 4501 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, 4502 WeakUndeclaredIdentifiers); 4503 4504 // Write the record containing ext_vector type names. 4505 if (!ExtVectorDecls.empty()) 4506 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); 4507 4508 // Write the record containing VTable uses information. 4509 if (!VTableUses.empty()) 4510 Stream.EmitRecord(VTABLE_USES, VTableUses); 4511 4512 // Write the record containing potentially unused local typedefs. 4513 if (!UnusedLocalTypedefNameCandidates.empty()) 4514 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES, 4515 UnusedLocalTypedefNameCandidates); 4516 4517 // Write the record containing pending implicit instantiations. 4518 if (!PendingInstantiations.empty()) 4519 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); 4520 4521 // Write the record containing declaration references of Sema. 4522 if (!SemaDeclRefs.empty()) 4523 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); 4524 4525 // Write the record containing CUDA-specific declaration references. 4526 if (!CUDASpecialDeclRefs.empty()) 4527 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs); 4528 4529 // Write the delegating constructors. 4530 if (!DelegatingCtorDecls.empty()) 4531 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls); 4532 4533 // Write the known namespaces. 4534 if (!KnownNamespaces.empty()) 4535 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces); 4536 4537 // Write the undefined internal functions and variables, and inline functions. 4538 if (!UndefinedButUsed.empty()) 4539 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed); 4540 4541 if (!DeleteExprsToAnalyze.empty()) 4542 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze); 4543 4544 // Write the visible updates to DeclContexts. 4545 for (auto *DC : UpdatedDeclContexts) 4546 WriteDeclContextVisibleUpdate(DC); 4547 4548 if (!WritingModule) { 4549 // Write the submodules that were imported, if any. 4550 struct ModuleInfo { 4551 uint64_t ID; 4552 Module *M; 4553 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {} 4554 }; 4555 llvm::SmallVector<ModuleInfo, 64> Imports; 4556 for (const auto *I : Context.local_imports()) { 4557 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end()); 4558 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()], 4559 I->getImportedModule())); 4560 } 4561 4562 if (!Imports.empty()) { 4563 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) { 4564 return A.ID < B.ID; 4565 }; 4566 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) { 4567 return A.ID == B.ID; 4568 }; 4569 4570 // Sort and deduplicate module IDs. 4571 std::sort(Imports.begin(), Imports.end(), Cmp); 4572 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq), 4573 Imports.end()); 4574 4575 RecordData ImportedModules; 4576 for (const auto &Import : Imports) { 4577 ImportedModules.push_back(Import.ID); 4578 // FIXME: If the module has macros imported then later has declarations 4579 // imported, this location won't be the right one as a location for the 4580 // declaration imports. 4581 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules); 4582 } 4583 4584 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules); 4585 } 4586 } 4587 4588 WriteDeclReplacementsBlock(); 4589 WriteRedeclarations(); 4590 WriteObjCCategories(); 4591 if(!WritingModule) 4592 WriteOptimizePragmaOptions(SemaRef); 4593 4594 // Some simple statistics 4595 Record.clear(); 4596 Record.push_back(NumStatements); 4597 Record.push_back(NumMacros); 4598 Record.push_back(NumLexicalDeclContexts); 4599 Record.push_back(NumVisibleDeclContexts); 4600 Stream.EmitRecord(STATISTICS, Record); 4601 Stream.ExitBlock(); 4602 } 4603 4604 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { 4605 if (DeclUpdates.empty()) 4606 return; 4607 4608 DeclUpdateMap LocalUpdates; 4609 LocalUpdates.swap(DeclUpdates); 4610 4611 for (auto &DeclUpdate : LocalUpdates) { 4612 const Decl *D = DeclUpdate.first; 4613 if (isRewritten(D)) 4614 continue; // The decl will be written completely,no need to store updates. 4615 4616 bool HasUpdatedBody = false; 4617 RecordData Record; 4618 for (auto &Update : DeclUpdate.second) { 4619 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind(); 4620 4621 Record.push_back(Kind); 4622 switch (Kind) { 4623 case UPD_CXX_ADDED_IMPLICIT_MEMBER: 4624 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: 4625 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: 4626 assert(Update.getDecl() && "no decl to add?"); 4627 Record.push_back(GetDeclRef(Update.getDecl())); 4628 break; 4629 4630 case UPD_CXX_ADDED_FUNCTION_DEFINITION: 4631 // An updated body is emitted last, so that the reader doesn't need 4632 // to skip over the lazy body to reach statements for other records. 4633 Record.pop_back(); 4634 HasUpdatedBody = true; 4635 break; 4636 4637 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: 4638 AddSourceLocation(Update.getLoc(), Record); 4639 break; 4640 4641 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: { 4642 auto *RD = cast<CXXRecordDecl>(D); 4643 UpdatedDeclContexts.insert(RD->getPrimaryContext()); 4644 AddCXXDefinitionData(RD, Record); 4645 Record.push_back(WriteDeclContextLexicalBlock( 4646 *Context, const_cast<CXXRecordDecl *>(RD))); 4647 4648 // This state is sometimes updated by template instantiation, when we 4649 // switch from the specialization referring to the template declaration 4650 // to it referring to the template definition. 4651 if (auto *MSInfo = RD->getMemberSpecializationInfo()) { 4652 Record.push_back(MSInfo->getTemplateSpecializationKind()); 4653 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record); 4654 } else { 4655 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD); 4656 Record.push_back(Spec->getTemplateSpecializationKind()); 4657 AddSourceLocation(Spec->getPointOfInstantiation(), Record); 4658 4659 // The instantiation might have been resolved to a partial 4660 // specialization. If so, record which one. 4661 auto From = Spec->getInstantiatedFrom(); 4662 if (auto PartialSpec = 4663 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) { 4664 Record.push_back(true); 4665 AddDeclRef(PartialSpec, Record); 4666 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(), 4667 Record); 4668 } else { 4669 Record.push_back(false); 4670 } 4671 } 4672 Record.push_back(RD->getTagKind()); 4673 AddSourceLocation(RD->getLocation(), Record); 4674 AddSourceLocation(RD->getLocStart(), Record); 4675 AddSourceLocation(RD->getRBraceLoc(), Record); 4676 4677 // Instantiation may change attributes; write them all out afresh. 4678 Record.push_back(D->hasAttrs()); 4679 if (Record.back()) 4680 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(), 4681 D->getAttrs().size()), Record); 4682 4683 // FIXME: Ensure we don't get here for explicit instantiations. 4684 break; 4685 } 4686 4687 case UPD_CXX_RESOLVED_DTOR_DELETE: 4688 AddDeclRef(Update.getDecl(), Record); 4689 break; 4690 4691 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: 4692 addExceptionSpec( 4693 *this, 4694 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(), 4695 Record); 4696 break; 4697 4698 case UPD_CXX_DEDUCED_RETURN_TYPE: 4699 Record.push_back(GetOrCreateTypeID(Update.getType())); 4700 break; 4701 4702 case UPD_DECL_MARKED_USED: 4703 break; 4704 4705 case UPD_MANGLING_NUMBER: 4706 case UPD_STATIC_LOCAL_NUMBER: 4707 Record.push_back(Update.getNumber()); 4708 break; 4709 4710 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE: 4711 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(), 4712 Record); 4713 break; 4714 4715 case UPD_DECL_EXPORTED: 4716 Record.push_back(getSubmoduleID(Update.getModule())); 4717 break; 4718 4719 case UPD_ADDED_ATTR_TO_RECORD: 4720 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record); 4721 break; 4722 } 4723 } 4724 4725 if (HasUpdatedBody) { 4726 const FunctionDecl *Def = cast<FunctionDecl>(D); 4727 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION); 4728 Record.push_back(Def->isInlined()); 4729 AddSourceLocation(Def->getInnerLocStart(), Record); 4730 AddFunctionDefinition(Def, Record); 4731 } 4732 4733 OffsetsRecord.push_back(GetDeclRef(D)); 4734 OffsetsRecord.push_back(Stream.GetCurrentBitNo()); 4735 4736 Stream.EmitRecord(DECL_UPDATES, Record); 4737 4738 FlushPendingAfterDecl(); 4739 } 4740 } 4741 4742 void ASTWriter::WriteDeclReplacementsBlock() { 4743 if (ReplacedDecls.empty()) 4744 return; 4745 4746 RecordData Record; 4747 for (SmallVectorImpl<ReplacedDeclInfo>::iterator 4748 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { 4749 Record.push_back(I->ID); 4750 Record.push_back(I->Offset); 4751 Record.push_back(I->Loc); 4752 } 4753 Stream.EmitRecord(DECL_REPLACEMENTS, Record); 4754 } 4755 4756 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { 4757 Record.push_back(Loc.getRawEncoding()); 4758 } 4759 4760 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { 4761 AddSourceLocation(Range.getBegin(), Record); 4762 AddSourceLocation(Range.getEnd(), Record); 4763 } 4764 4765 void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { 4766 Record.push_back(Value.getBitWidth()); 4767 const uint64_t *Words = Value.getRawData(); 4768 Record.append(Words, Words + Value.getNumWords()); 4769 } 4770 4771 void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { 4772 Record.push_back(Value.isUnsigned()); 4773 AddAPInt(Value, Record); 4774 } 4775 4776 void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { 4777 AddAPInt(Value.bitcastToAPInt(), Record); 4778 } 4779 4780 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { 4781 Record.push_back(getIdentifierRef(II)); 4782 } 4783 4784 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { 4785 if (!II) 4786 return 0; 4787 4788 IdentID &ID = IdentifierIDs[II]; 4789 if (ID == 0) 4790 ID = NextIdentID++; 4791 return ID; 4792 } 4793 4794 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) { 4795 // Don't emit builtin macros like __LINE__ to the AST file unless they 4796 // have been redefined by the header (in which case they are not 4797 // isBuiltinMacro). 4798 if (!MI || MI->isBuiltinMacro()) 4799 return 0; 4800 4801 MacroID &ID = MacroIDs[MI]; 4802 if (ID == 0) { 4803 ID = NextMacroID++; 4804 MacroInfoToEmitData Info = { Name, MI, ID }; 4805 MacroInfosToEmit.push_back(Info); 4806 } 4807 return ID; 4808 } 4809 4810 MacroID ASTWriter::getMacroID(MacroInfo *MI) { 4811 if (!MI || MI->isBuiltinMacro()) 4812 return 0; 4813 4814 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!"); 4815 return MacroIDs[MI]; 4816 } 4817 4818 uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) { 4819 return IdentMacroDirectivesOffsetMap.lookup(Name); 4820 } 4821 4822 void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { 4823 Record.push_back(getSelectorRef(SelRef)); 4824 } 4825 4826 SelectorID ASTWriter::getSelectorRef(Selector Sel) { 4827 if (Sel.getAsOpaquePtr() == nullptr) { 4828 return 0; 4829 } 4830 4831 SelectorID SID = SelectorIDs[Sel]; 4832 if (SID == 0 && Chain) { 4833 // This might trigger a ReadSelector callback, which will set the ID for 4834 // this selector. 4835 Chain->LoadSelector(Sel); 4836 SID = SelectorIDs[Sel]; 4837 } 4838 if (SID == 0) { 4839 SID = NextSelectorID++; 4840 SelectorIDs[Sel] = SID; 4841 } 4842 return SID; 4843 } 4844 4845 void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { 4846 AddDeclRef(Temp->getDestructor(), Record); 4847 } 4848 4849 void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits, 4850 RecordDataImpl &Record) { 4851 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded"); 4852 CXXCtorInitializersToWrite.push_back( 4853 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits)); 4854 Record.push_back(NextCXXCtorInitializersID++); 4855 } 4856 4857 void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, 4858 CXXBaseSpecifier const *BasesEnd, 4859 RecordDataImpl &Record) { 4860 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); 4861 CXXBaseSpecifiersToWrite.push_back( 4862 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, 4863 Bases, BasesEnd)); 4864 Record.push_back(NextCXXBaseSpecifiersID++); 4865 } 4866 4867 void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 4868 const TemplateArgumentLocInfo &Arg, 4869 RecordDataImpl &Record) { 4870 switch (Kind) { 4871 case TemplateArgument::Expression: 4872 AddStmt(Arg.getAsExpr()); 4873 break; 4874 case TemplateArgument::Type: 4875 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); 4876 break; 4877 case TemplateArgument::Template: 4878 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); 4879 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 4880 break; 4881 case TemplateArgument::TemplateExpansion: 4882 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); 4883 AddSourceLocation(Arg.getTemplateNameLoc(), Record); 4884 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record); 4885 break; 4886 case TemplateArgument::Null: 4887 case TemplateArgument::Integral: 4888 case TemplateArgument::Declaration: 4889 case TemplateArgument::NullPtr: 4890 case TemplateArgument::Pack: 4891 // FIXME: Is this right? 4892 break; 4893 } 4894 } 4895 4896 void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, 4897 RecordDataImpl &Record) { 4898 AddTemplateArgument(Arg.getArgument(), Record); 4899 4900 if (Arg.getArgument().getKind() == TemplateArgument::Expression) { 4901 bool InfoHasSameExpr 4902 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); 4903 Record.push_back(InfoHasSameExpr); 4904 if (InfoHasSameExpr) 4905 return; // Avoid storing the same expr twice. 4906 } 4907 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), 4908 Record); 4909 } 4910 4911 void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, 4912 RecordDataImpl &Record) { 4913 if (!TInfo) { 4914 AddTypeRef(QualType(), Record); 4915 return; 4916 } 4917 4918 AddTypeLoc(TInfo->getTypeLoc(), Record); 4919 } 4920 4921 void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) { 4922 AddTypeRef(TL.getType(), Record); 4923 4924 TypeLocWriter TLW(*this, Record); 4925 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 4926 TLW.Visit(TL); 4927 } 4928 4929 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { 4930 Record.push_back(GetOrCreateTypeID(T)); 4931 } 4932 4933 TypeID ASTWriter::GetOrCreateTypeID(QualType T) { 4934 assert(Context); 4935 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx { 4936 if (T.isNull()) 4937 return TypeIdx(); 4938 assert(!T.getLocalFastQualifiers()); 4939 4940 TypeIdx &Idx = TypeIdxs[T]; 4941 if (Idx.getIndex() == 0) { 4942 if (DoneWritingDeclsAndTypes) { 4943 assert(0 && "New type seen after serializing all the types to emit!"); 4944 return TypeIdx(); 4945 } 4946 4947 // We haven't seen this type before. Assign it a new ID and put it 4948 // into the queue of types to emit. 4949 Idx = TypeIdx(NextTypeID++); 4950 DeclTypesToEmit.push(T); 4951 } 4952 return Idx; 4953 }); 4954 } 4955 4956 TypeID ASTWriter::getTypeID(QualType T) const { 4957 assert(Context); 4958 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx { 4959 if (T.isNull()) 4960 return TypeIdx(); 4961 assert(!T.getLocalFastQualifiers()); 4962 4963 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 4964 assert(I != TypeIdxs.end() && "Type not emitted!"); 4965 return I->second; 4966 }); 4967 } 4968 4969 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { 4970 Record.push_back(GetDeclRef(D)); 4971 } 4972 4973 DeclID ASTWriter::GetDeclRef(const Decl *D) { 4974 assert(WritingAST && "Cannot request a declaration ID before AST writing"); 4975 4976 if (!D) { 4977 return 0; 4978 } 4979 4980 // If D comes from an AST file, its declaration ID is already known and 4981 // fixed. 4982 if (D->isFromASTFile()) 4983 return D->getGlobalID(); 4984 4985 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); 4986 DeclID &ID = DeclIDs[D]; 4987 if (ID == 0) { 4988 if (DoneWritingDeclsAndTypes) { 4989 assert(0 && "New decl seen after serializing all the decls to emit!"); 4990 return 0; 4991 } 4992 4993 // We haven't seen this declaration before. Give it a new ID and 4994 // enqueue it in the list of declarations to emit. 4995 ID = NextDeclID++; 4996 DeclTypesToEmit.push(const_cast<Decl *>(D)); 4997 } 4998 4999 return ID; 5000 } 5001 5002 DeclID ASTWriter::getDeclID(const Decl *D) { 5003 if (!D) 5004 return 0; 5005 5006 // If D comes from an AST file, its declaration ID is already known and 5007 // fixed. 5008 if (D->isFromASTFile()) 5009 return D->getGlobalID(); 5010 5011 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); 5012 return DeclIDs[D]; 5013 } 5014 5015 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) { 5016 assert(ID); 5017 assert(D); 5018 5019 SourceLocation Loc = D->getLocation(); 5020 if (Loc.isInvalid()) 5021 return; 5022 5023 // We only keep track of the file-level declarations of each file. 5024 if (!D->getLexicalDeclContext()->isFileContext()) 5025 return; 5026 // FIXME: ParmVarDecls that are part of a function type of a parameter of 5027 // a function/objc method, should not have TU as lexical context. 5028 if (isa<ParmVarDecl>(D)) 5029 return; 5030 5031 SourceManager &SM = Context->getSourceManager(); 5032 SourceLocation FileLoc = SM.getFileLoc(Loc); 5033 assert(SM.isLocalSourceLocation(FileLoc)); 5034 FileID FID; 5035 unsigned Offset; 5036 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); 5037 if (FID.isInvalid()) 5038 return; 5039 assert(SM.getSLocEntry(FID).isFile()); 5040 5041 DeclIDInFileInfo *&Info = FileDeclIDs[FID]; 5042 if (!Info) 5043 Info = new DeclIDInFileInfo(); 5044 5045 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID); 5046 LocDeclIDsTy &Decls = Info->DeclIDs; 5047 5048 if (Decls.empty() || Decls.back().first <= Offset) { 5049 Decls.push_back(LocDecl); 5050 return; 5051 } 5052 5053 LocDeclIDsTy::iterator I = 5054 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first()); 5055 5056 Decls.insert(I, LocDecl); 5057 } 5058 5059 void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { 5060 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. 5061 Record.push_back(Name.getNameKind()); 5062 switch (Name.getNameKind()) { 5063 case DeclarationName::Identifier: 5064 AddIdentifierRef(Name.getAsIdentifierInfo(), Record); 5065 break; 5066 5067 case DeclarationName::ObjCZeroArgSelector: 5068 case DeclarationName::ObjCOneArgSelector: 5069 case DeclarationName::ObjCMultiArgSelector: 5070 AddSelectorRef(Name.getObjCSelector(), Record); 5071 break; 5072 5073 case DeclarationName::CXXConstructorName: 5074 case DeclarationName::CXXDestructorName: 5075 case DeclarationName::CXXConversionFunctionName: 5076 AddTypeRef(Name.getCXXNameType(), Record); 5077 break; 5078 5079 case DeclarationName::CXXOperatorName: 5080 Record.push_back(Name.getCXXOverloadedOperator()); 5081 break; 5082 5083 case DeclarationName::CXXLiteralOperatorName: 5084 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); 5085 break; 5086 5087 case DeclarationName::CXXUsingDirective: 5088 // No extra data to emit 5089 break; 5090 } 5091 } 5092 5093 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) { 5094 assert(needsAnonymousDeclarationNumber(D) && 5095 "expected an anonymous declaration"); 5096 5097 // Number the anonymous declarations within this context, if we've not 5098 // already done so. 5099 auto It = AnonymousDeclarationNumbers.find(D); 5100 if (It == AnonymousDeclarationNumbers.end()) { 5101 auto *DC = D->getLexicalDeclContext(); 5102 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) { 5103 AnonymousDeclarationNumbers[ND] = Number; 5104 }); 5105 5106 It = AnonymousDeclarationNumbers.find(D); 5107 assert(It != AnonymousDeclarationNumbers.end() && 5108 "declaration not found within its lexical context"); 5109 } 5110 5111 return It->second; 5112 } 5113 5114 void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 5115 DeclarationName Name, RecordDataImpl &Record) { 5116 switch (Name.getNameKind()) { 5117 case DeclarationName::CXXConstructorName: 5118 case DeclarationName::CXXDestructorName: 5119 case DeclarationName::CXXConversionFunctionName: 5120 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); 5121 break; 5122 5123 case DeclarationName::CXXOperatorName: 5124 AddSourceLocation( 5125 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), 5126 Record); 5127 AddSourceLocation( 5128 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), 5129 Record); 5130 break; 5131 5132 case DeclarationName::CXXLiteralOperatorName: 5133 AddSourceLocation( 5134 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), 5135 Record); 5136 break; 5137 5138 case DeclarationName::Identifier: 5139 case DeclarationName::ObjCZeroArgSelector: 5140 case DeclarationName::ObjCOneArgSelector: 5141 case DeclarationName::ObjCMultiArgSelector: 5142 case DeclarationName::CXXUsingDirective: 5143 break; 5144 } 5145 } 5146 5147 void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 5148 RecordDataImpl &Record) { 5149 AddDeclarationName(NameInfo.getName(), Record); 5150 AddSourceLocation(NameInfo.getLoc(), Record); 5151 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); 5152 } 5153 5154 void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, 5155 RecordDataImpl &Record) { 5156 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record); 5157 Record.push_back(Info.NumTemplParamLists); 5158 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) 5159 AddTemplateParameterList(Info.TemplParamLists[i], Record); 5160 } 5161 5162 void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, 5163 RecordDataImpl &Record) { 5164 // Nested name specifiers usually aren't too long. I think that 8 would 5165 // typically accommodate the vast majority. 5166 SmallVector<NestedNameSpecifier *, 8> NestedNames; 5167 5168 // Push each of the NNS's onto a stack for serialization in reverse order. 5169 while (NNS) { 5170 NestedNames.push_back(NNS); 5171 NNS = NNS->getPrefix(); 5172 } 5173 5174 Record.push_back(NestedNames.size()); 5175 while(!NestedNames.empty()) { 5176 NNS = NestedNames.pop_back_val(); 5177 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); 5178 Record.push_back(Kind); 5179 switch (Kind) { 5180 case NestedNameSpecifier::Identifier: 5181 AddIdentifierRef(NNS->getAsIdentifier(), Record); 5182 break; 5183 5184 case NestedNameSpecifier::Namespace: 5185 AddDeclRef(NNS->getAsNamespace(), Record); 5186 break; 5187 5188 case NestedNameSpecifier::NamespaceAlias: 5189 AddDeclRef(NNS->getAsNamespaceAlias(), Record); 5190 break; 5191 5192 case NestedNameSpecifier::TypeSpec: 5193 case NestedNameSpecifier::TypeSpecWithTemplate: 5194 AddTypeRef(QualType(NNS->getAsType(), 0), Record); 5195 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 5196 break; 5197 5198 case NestedNameSpecifier::Global: 5199 // Don't need to write an associated value. 5200 break; 5201 5202 case NestedNameSpecifier::Super: 5203 AddDeclRef(NNS->getAsRecordDecl(), Record); 5204 break; 5205 } 5206 } 5207 } 5208 5209 void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, 5210 RecordDataImpl &Record) { 5211 // Nested name specifiers usually aren't too long. I think that 8 would 5212 // typically accommodate the vast majority. 5213 SmallVector<NestedNameSpecifierLoc , 8> NestedNames; 5214 5215 // Push each of the nested-name-specifiers's onto a stack for 5216 // serialization in reverse order. 5217 while (NNS) { 5218 NestedNames.push_back(NNS); 5219 NNS = NNS.getPrefix(); 5220 } 5221 5222 Record.push_back(NestedNames.size()); 5223 while(!NestedNames.empty()) { 5224 NNS = NestedNames.pop_back_val(); 5225 NestedNameSpecifier::SpecifierKind Kind 5226 = NNS.getNestedNameSpecifier()->getKind(); 5227 Record.push_back(Kind); 5228 switch (Kind) { 5229 case NestedNameSpecifier::Identifier: 5230 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record); 5231 AddSourceRange(NNS.getLocalSourceRange(), Record); 5232 break; 5233 5234 case NestedNameSpecifier::Namespace: 5235 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record); 5236 AddSourceRange(NNS.getLocalSourceRange(), Record); 5237 break; 5238 5239 case NestedNameSpecifier::NamespaceAlias: 5240 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record); 5241 AddSourceRange(NNS.getLocalSourceRange(), Record); 5242 break; 5243 5244 case NestedNameSpecifier::TypeSpec: 5245 case NestedNameSpecifier::TypeSpecWithTemplate: 5246 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); 5247 AddTypeLoc(NNS.getTypeLoc(), Record); 5248 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); 5249 break; 5250 5251 case NestedNameSpecifier::Global: 5252 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); 5253 break; 5254 5255 case NestedNameSpecifier::Super: 5256 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record); 5257 AddSourceRange(NNS.getLocalSourceRange(), Record); 5258 break; 5259 } 5260 } 5261 } 5262 5263 void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { 5264 TemplateName::NameKind Kind = Name.getKind(); 5265 Record.push_back(Kind); 5266 switch (Kind) { 5267 case TemplateName::Template: 5268 AddDeclRef(Name.getAsTemplateDecl(), Record); 5269 break; 5270 5271 case TemplateName::OverloadedTemplate: { 5272 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); 5273 Record.push_back(OvT->size()); 5274 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); 5275 I != E; ++I) 5276 AddDeclRef(*I, Record); 5277 break; 5278 } 5279 5280 case TemplateName::QualifiedTemplate: { 5281 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); 5282 AddNestedNameSpecifier(QualT->getQualifier(), Record); 5283 Record.push_back(QualT->hasTemplateKeyword()); 5284 AddDeclRef(QualT->getTemplateDecl(), Record); 5285 break; 5286 } 5287 5288 case TemplateName::DependentTemplate: { 5289 DependentTemplateName *DepT = Name.getAsDependentTemplateName(); 5290 AddNestedNameSpecifier(DepT->getQualifier(), Record); 5291 Record.push_back(DepT->isIdentifier()); 5292 if (DepT->isIdentifier()) 5293 AddIdentifierRef(DepT->getIdentifier(), Record); 5294 else 5295 Record.push_back(DepT->getOperator()); 5296 break; 5297 } 5298 5299 case TemplateName::SubstTemplateTemplateParm: { 5300 SubstTemplateTemplateParmStorage *subst 5301 = Name.getAsSubstTemplateTemplateParm(); 5302 AddDeclRef(subst->getParameter(), Record); 5303 AddTemplateName(subst->getReplacement(), Record); 5304 break; 5305 } 5306 5307 case TemplateName::SubstTemplateTemplateParmPack: { 5308 SubstTemplateTemplateParmPackStorage *SubstPack 5309 = Name.getAsSubstTemplateTemplateParmPack(); 5310 AddDeclRef(SubstPack->getParameterPack(), Record); 5311 AddTemplateArgument(SubstPack->getArgumentPack(), Record); 5312 break; 5313 } 5314 } 5315 } 5316 5317 void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, 5318 RecordDataImpl &Record) { 5319 Record.push_back(Arg.getKind()); 5320 switch (Arg.getKind()) { 5321 case TemplateArgument::Null: 5322 break; 5323 case TemplateArgument::Type: 5324 AddTypeRef(Arg.getAsType(), Record); 5325 break; 5326 case TemplateArgument::Declaration: 5327 AddDeclRef(Arg.getAsDecl(), Record); 5328 AddTypeRef(Arg.getParamTypeForDecl(), Record); 5329 break; 5330 case TemplateArgument::NullPtr: 5331 AddTypeRef(Arg.getNullPtrType(), Record); 5332 break; 5333 case TemplateArgument::Integral: 5334 AddAPSInt(Arg.getAsIntegral(), Record); 5335 AddTypeRef(Arg.getIntegralType(), Record); 5336 break; 5337 case TemplateArgument::Template: 5338 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); 5339 break; 5340 case TemplateArgument::TemplateExpansion: 5341 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); 5342 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions()) 5343 Record.push_back(*NumExpansions + 1); 5344 else 5345 Record.push_back(0); 5346 break; 5347 case TemplateArgument::Expression: 5348 AddStmt(Arg.getAsExpr()); 5349 break; 5350 case TemplateArgument::Pack: 5351 Record.push_back(Arg.pack_size()); 5352 for (const auto &P : Arg.pack_elements()) 5353 AddTemplateArgument(P, Record); 5354 break; 5355 } 5356 } 5357 5358 void 5359 ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, 5360 RecordDataImpl &Record) { 5361 assert(TemplateParams && "No TemplateParams!"); 5362 AddSourceLocation(TemplateParams->getTemplateLoc(), Record); 5363 AddSourceLocation(TemplateParams->getLAngleLoc(), Record); 5364 AddSourceLocation(TemplateParams->getRAngleLoc(), Record); 5365 Record.push_back(TemplateParams->size()); 5366 for (TemplateParameterList::const_iterator 5367 P = TemplateParams->begin(), PEnd = TemplateParams->end(); 5368 P != PEnd; ++P) 5369 AddDeclRef(*P, Record); 5370 } 5371 5372 /// \brief Emit a template argument list. 5373 void 5374 ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, 5375 RecordDataImpl &Record) { 5376 assert(TemplateArgs && "No TemplateArgs!"); 5377 Record.push_back(TemplateArgs->size()); 5378 for (int i=0, e = TemplateArgs->size(); i != e; ++i) 5379 AddTemplateArgument(TemplateArgs->get(i), Record); 5380 } 5381 5382 void 5383 ASTWriter::AddASTTemplateArgumentListInfo 5384 (const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) { 5385 assert(ASTTemplArgList && "No ASTTemplArgList!"); 5386 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record); 5387 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record); 5388 Record.push_back(ASTTemplArgList->NumTemplateArgs); 5389 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs(); 5390 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i) 5391 AddTemplateArgumentLoc(TemplArgs[i], Record); 5392 } 5393 5394 void 5395 ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) { 5396 Record.push_back(Set.size()); 5397 for (ASTUnresolvedSet::const_iterator 5398 I = Set.begin(), E = Set.end(); I != E; ++I) { 5399 AddDeclRef(I.getDecl(), Record); 5400 Record.push_back(I.getAccess()); 5401 } 5402 } 5403 5404 void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, 5405 RecordDataImpl &Record) { 5406 Record.push_back(Base.isVirtual()); 5407 Record.push_back(Base.isBaseOfClass()); 5408 Record.push_back(Base.getAccessSpecifierAsWritten()); 5409 Record.push_back(Base.getInheritConstructors()); 5410 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); 5411 AddSourceRange(Base.getSourceRange(), Record); 5412 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() 5413 : SourceLocation(), 5414 Record); 5415 } 5416 5417 void ASTWriter::FlushCXXBaseSpecifiers() { 5418 RecordData Record; 5419 unsigned N = CXXBaseSpecifiersToWrite.size(); 5420 for (unsigned I = 0; I != N; ++I) { 5421 Record.clear(); 5422 5423 // Record the offset of this base-specifier set. 5424 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1; 5425 if (Index == CXXBaseSpecifiersOffsets.size()) 5426 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); 5427 else { 5428 if (Index > CXXBaseSpecifiersOffsets.size()) 5429 CXXBaseSpecifiersOffsets.resize(Index + 1); 5430 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); 5431 } 5432 5433 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, 5434 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; 5435 Record.push_back(BEnd - B); 5436 for (; B != BEnd; ++B) 5437 AddCXXBaseSpecifier(*B, Record); 5438 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); 5439 5440 // Flush any expressions that were written as part of the base specifiers. 5441 FlushStmts(); 5442 } 5443 5444 assert(N == CXXBaseSpecifiersToWrite.size() && 5445 "added more base specifiers while writing base specifiers"); 5446 CXXBaseSpecifiersToWrite.clear(); 5447 } 5448 5449 void ASTWriter::AddCXXCtorInitializers( 5450 const CXXCtorInitializer * const *CtorInitializers, 5451 unsigned NumCtorInitializers, 5452 RecordDataImpl &Record) { 5453 Record.push_back(NumCtorInitializers); 5454 for (unsigned i=0; i != NumCtorInitializers; ++i) { 5455 const CXXCtorInitializer *Init = CtorInitializers[i]; 5456 5457 if (Init->isBaseInitializer()) { 5458 Record.push_back(CTOR_INITIALIZER_BASE); 5459 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); 5460 Record.push_back(Init->isBaseVirtual()); 5461 } else if (Init->isDelegatingInitializer()) { 5462 Record.push_back(CTOR_INITIALIZER_DELEGATING); 5463 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); 5464 } else if (Init->isMemberInitializer()){ 5465 Record.push_back(CTOR_INITIALIZER_MEMBER); 5466 AddDeclRef(Init->getMember(), Record); 5467 } else { 5468 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER); 5469 AddDeclRef(Init->getIndirectMember(), Record); 5470 } 5471 5472 AddSourceLocation(Init->getMemberLocation(), Record); 5473 AddStmt(Init->getInit()); 5474 AddSourceLocation(Init->getLParenLoc(), Record); 5475 AddSourceLocation(Init->getRParenLoc(), Record); 5476 Record.push_back(Init->isWritten()); 5477 if (Init->isWritten()) { 5478 Record.push_back(Init->getSourceOrder()); 5479 } else { 5480 Record.push_back(Init->getNumArrayIndices()); 5481 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) 5482 AddDeclRef(Init->getArrayIndex(i), Record); 5483 } 5484 } 5485 } 5486 5487 void ASTWriter::FlushCXXCtorInitializers() { 5488 RecordData Record; 5489 5490 unsigned N = CXXCtorInitializersToWrite.size(); 5491 (void)N; // Silence unused warning in non-assert builds. 5492 for (auto &Init : CXXCtorInitializersToWrite) { 5493 Record.clear(); 5494 5495 // Record the offset of this mem-initializer list. 5496 unsigned Index = Init.ID - 1; 5497 if (Index == CXXCtorInitializersOffsets.size()) 5498 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo()); 5499 else { 5500 if (Index > CXXCtorInitializersOffsets.size()) 5501 CXXCtorInitializersOffsets.resize(Index + 1); 5502 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo(); 5503 } 5504 5505 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record); 5506 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record); 5507 5508 // Flush any expressions that were written as part of the initializers. 5509 FlushStmts(); 5510 } 5511 5512 assert(N == CXXCtorInitializersToWrite.size() && 5513 "added more ctor initializers while writing ctor initializers"); 5514 CXXCtorInitializersToWrite.clear(); 5515 } 5516 5517 void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { 5518 auto &Data = D->data(); 5519 Record.push_back(Data.IsLambda); 5520 Record.push_back(Data.UserDeclaredConstructor); 5521 Record.push_back(Data.UserDeclaredSpecialMembers); 5522 Record.push_back(Data.Aggregate); 5523 Record.push_back(Data.PlainOldData); 5524 Record.push_back(Data.Empty); 5525 Record.push_back(Data.Polymorphic); 5526 Record.push_back(Data.Abstract); 5527 Record.push_back(Data.IsStandardLayout); 5528 Record.push_back(Data.HasNoNonEmptyBases); 5529 Record.push_back(Data.HasPrivateFields); 5530 Record.push_back(Data.HasProtectedFields); 5531 Record.push_back(Data.HasPublicFields); 5532 Record.push_back(Data.HasMutableFields); 5533 Record.push_back(Data.HasVariantMembers); 5534 Record.push_back(Data.HasOnlyCMembers); 5535 Record.push_back(Data.HasInClassInitializer); 5536 Record.push_back(Data.HasUninitializedReferenceMember); 5537 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor); 5538 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment); 5539 Record.push_back(Data.NeedOverloadResolutionForDestructor); 5540 Record.push_back(Data.DefaultedMoveConstructorIsDeleted); 5541 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted); 5542 Record.push_back(Data.DefaultedDestructorIsDeleted); 5543 Record.push_back(Data.HasTrivialSpecialMembers); 5544 Record.push_back(Data.DeclaredNonTrivialSpecialMembers); 5545 Record.push_back(Data.HasIrrelevantDestructor); 5546 Record.push_back(Data.HasConstexprNonCopyMoveConstructor); 5547 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr); 5548 Record.push_back(Data.HasConstexprDefaultConstructor); 5549 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases); 5550 Record.push_back(Data.ComputedVisibleConversions); 5551 Record.push_back(Data.UserProvidedDefaultConstructor); 5552 Record.push_back(Data.DeclaredSpecialMembers); 5553 Record.push_back(Data.ImplicitCopyConstructorHasConstParam); 5554 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam); 5555 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam); 5556 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam); 5557 // IsLambda bit is already saved. 5558 5559 Record.push_back(Data.NumBases); 5560 if (Data.NumBases > 0) 5561 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, 5562 Record); 5563 5564 // FIXME: Make VBases lazily computed when needed to avoid storing them. 5565 Record.push_back(Data.NumVBases); 5566 if (Data.NumVBases > 0) 5567 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, 5568 Record); 5569 5570 AddUnresolvedSet(Data.Conversions.get(*Context), Record); 5571 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record); 5572 // Data.Definition is the owning decl, no need to write it. 5573 AddDeclRef(D->getFirstFriend(), Record); 5574 5575 // Add lambda-specific data. 5576 if (Data.IsLambda) { 5577 auto &Lambda = D->getLambdaData(); 5578 Record.push_back(Lambda.Dependent); 5579 Record.push_back(Lambda.IsGenericLambda); 5580 Record.push_back(Lambda.CaptureDefault); 5581 Record.push_back(Lambda.NumCaptures); 5582 Record.push_back(Lambda.NumExplicitCaptures); 5583 Record.push_back(Lambda.ManglingNumber); 5584 AddDeclRef(Lambda.ContextDecl, Record); 5585 AddTypeSourceInfo(Lambda.MethodTyInfo, Record); 5586 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { 5587 const LambdaCapture &Capture = Lambda.Captures[I]; 5588 AddSourceLocation(Capture.getLocation(), Record); 5589 Record.push_back(Capture.isImplicit()); 5590 Record.push_back(Capture.getCaptureKind()); 5591 switch (Capture.getCaptureKind()) { 5592 case LCK_This: 5593 case LCK_VLAType: 5594 break; 5595 case LCK_ByCopy: 5596 case LCK_ByRef: 5597 VarDecl *Var = 5598 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr; 5599 AddDeclRef(Var, Record); 5600 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc() 5601 : SourceLocation(), 5602 Record); 5603 break; 5604 } 5605 } 5606 } 5607 } 5608 5609 void ASTWriter::ReaderInitialized(ASTReader *Reader) { 5610 assert(Reader && "Cannot remove chain"); 5611 assert((!Chain || Chain == Reader) && "Cannot replace chain"); 5612 assert(FirstDeclID == NextDeclID && 5613 FirstTypeID == NextTypeID && 5614 FirstIdentID == NextIdentID && 5615 FirstMacroID == NextMacroID && 5616 FirstSubmoduleID == NextSubmoduleID && 5617 FirstSelectorID == NextSelectorID && 5618 "Setting chain after writing has started."); 5619 5620 Chain = Reader; 5621 5622 // Note, this will get called multiple times, once one the reader starts up 5623 // and again each time it's done reading a PCH or module. 5624 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls(); 5625 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes(); 5626 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers(); 5627 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros(); 5628 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules(); 5629 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors(); 5630 NextDeclID = FirstDeclID; 5631 NextTypeID = FirstTypeID; 5632 NextIdentID = FirstIdentID; 5633 NextMacroID = FirstMacroID; 5634 NextSelectorID = FirstSelectorID; 5635 NextSubmoduleID = FirstSubmoduleID; 5636 } 5637 5638 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { 5639 // Always keep the highest ID. See \p TypeRead() for more information. 5640 IdentID &StoredID = IdentifierIDs[II]; 5641 if (ID > StoredID) 5642 StoredID = ID; 5643 } 5644 5645 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) { 5646 // Always keep the highest ID. See \p TypeRead() for more information. 5647 MacroID &StoredID = MacroIDs[MI]; 5648 if (ID > StoredID) 5649 StoredID = ID; 5650 } 5651 5652 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { 5653 // Always take the highest-numbered type index. This copes with an interesting 5654 // case for chained AST writing where we schedule writing the type and then, 5655 // later, deserialize the type from another AST. In this case, we want to 5656 // keep the higher-numbered entry so that we can properly write it out to 5657 // the AST file. 5658 TypeIdx &StoredIdx = TypeIdxs[T]; 5659 if (Idx.getIndex() >= StoredIdx.getIndex()) 5660 StoredIdx = Idx; 5661 } 5662 5663 void ASTWriter::SelectorRead(SelectorID ID, Selector S) { 5664 // Always keep the highest ID. See \p TypeRead() for more information. 5665 SelectorID &StoredID = SelectorIDs[S]; 5666 if (ID > StoredID) 5667 StoredID = ID; 5668 } 5669 5670 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID, 5671 MacroDefinitionRecord *MD) { 5672 assert(MacroDefinitions.find(MD) == MacroDefinitions.end()); 5673 MacroDefinitions[MD] = ID; 5674 } 5675 5676 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) { 5677 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end()); 5678 SubmoduleIDs[Mod] = ID; 5679 } 5680 5681 void ASTWriter::CompletedTagDefinition(const TagDecl *D) { 5682 assert(D->isCompleteDefinition()); 5683 assert(!WritingAST && "Already writing the AST!"); 5684 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 5685 // We are interested when a PCH decl is modified. 5686 if (RD->isFromASTFile()) { 5687 // A forward reference was mutated into a definition. Rewrite it. 5688 // FIXME: This happens during template instantiation, should we 5689 // have created a new definition decl instead ? 5690 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) && 5691 "completed a tag from another module but not by instantiation?"); 5692 DeclUpdates[RD].push_back( 5693 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION)); 5694 } 5695 } 5696 } 5697 5698 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { 5699 // TU and namespaces are handled elsewhere. 5700 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) 5701 return; 5702 5703 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile())) 5704 return; // Not a source decl added to a DeclContext from PCH. 5705 5706 assert(DC == DC->getPrimaryContext() && "added to non-primary context"); 5707 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!"); 5708 assert(!WritingAST && "Already writing the AST!"); 5709 UpdatedDeclContexts.insert(DC); 5710 UpdatingVisibleDecls.push_back(D); 5711 } 5712 5713 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { 5714 assert(D->isImplicit()); 5715 if (!(!D->isFromASTFile() && RD->isFromASTFile())) 5716 return; // Not a source member added to a class from PCH. 5717 if (!isa<CXXMethodDecl>(D)) 5718 return; // We are interested in lazily declared implicit methods. 5719 5720 // A decl coming from PCH was modified. 5721 assert(RD->isCompleteDefinition()); 5722 assert(!WritingAST && "Already writing the AST!"); 5723 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D)); 5724 } 5725 5726 void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, 5727 const ClassTemplateSpecializationDecl *D) { 5728 // The specializations set is kept in the canonical template. 5729 TD = TD->getCanonicalDecl(); 5730 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 5731 return; // Not a source specialization added to a template from PCH. 5732 5733 assert(!WritingAST && "Already writing the AST!"); 5734 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, 5735 D)); 5736 } 5737 5738 void ASTWriter::AddedCXXTemplateSpecialization( 5739 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) { 5740 // The specializations set is kept in the canonical template. 5741 TD = TD->getCanonicalDecl(); 5742 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 5743 return; // Not a source specialization added to a template from PCH. 5744 5745 assert(!WritingAST && "Already writing the AST!"); 5746 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, 5747 D)); 5748 } 5749 5750 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, 5751 const FunctionDecl *D) { 5752 // The specializations set is kept in the canonical template. 5753 TD = TD->getCanonicalDecl(); 5754 if (!(!D->isFromASTFile() && TD->isFromASTFile())) 5755 return; // Not a source specialization added to a template from PCH. 5756 5757 assert(!WritingAST && "Already writing the AST!"); 5758 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, 5759 D)); 5760 } 5761 5762 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) { 5763 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!"); 5764 if (!Chain) return; 5765 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) { 5766 // If we don't already know the exception specification for this redecl 5767 // chain, add an update record for it. 5768 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D) 5769 ->getType() 5770 ->castAs<FunctionProtoType>() 5771 ->getExceptionSpecType())) 5772 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC); 5773 }); 5774 } 5775 5776 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) { 5777 assert(!WritingAST && "Already writing the AST!"); 5778 if (!Chain) return; 5779 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) { 5780 DeclUpdates[D].push_back( 5781 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType)); 5782 }); 5783 } 5784 5785 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD, 5786 const FunctionDecl *Delete) { 5787 assert(!WritingAST && "Already writing the AST!"); 5788 assert(Delete && "Not given an operator delete"); 5789 if (!Chain) return; 5790 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) { 5791 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete)); 5792 }); 5793 } 5794 5795 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) { 5796 assert(!WritingAST && "Already writing the AST!"); 5797 if (!D->isFromASTFile()) 5798 return; // Declaration not imported from PCH. 5799 5800 // Implicit function decl from a PCH was defined. 5801 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION)); 5802 } 5803 5804 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) { 5805 assert(!WritingAST && "Already writing the AST!"); 5806 if (!D->isFromASTFile()) 5807 return; 5808 5809 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION)); 5810 } 5811 5812 void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) { 5813 assert(!WritingAST && "Already writing the AST!"); 5814 if (!D->isFromASTFile()) 5815 return; 5816 5817 // Since the actual instantiation is delayed, this really means that we need 5818 // to update the instantiation location. 5819 DeclUpdates[D].push_back( 5820 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER, 5821 D->getMemberSpecializationInfo()->getPointOfInstantiation())); 5822 } 5823 5824 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, 5825 const ObjCInterfaceDecl *IFD) { 5826 assert(!WritingAST && "Already writing the AST!"); 5827 if (!IFD->isFromASTFile()) 5828 return; // Declaration not imported from PCH. 5829 5830 assert(IFD->getDefinition() && "Category on a class without a definition?"); 5831 ObjCClassesWithCategories.insert( 5832 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition())); 5833 } 5834 5835 5836 void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop, 5837 const ObjCPropertyDecl *OrigProp, 5838 const ObjCCategoryDecl *ClassExt) { 5839 const ObjCInterfaceDecl *D = ClassExt->getClassInterface(); 5840 if (!D) 5841 return; 5842 5843 assert(!WritingAST && "Already writing the AST!"); 5844 if (!D->isFromASTFile()) 5845 return; // Declaration not imported from PCH. 5846 5847 RewriteDecl(D); 5848 } 5849 5850 void ASTWriter::DeclarationMarkedUsed(const Decl *D) { 5851 assert(!WritingAST && "Already writing the AST!"); 5852 if (!D->isFromASTFile()) 5853 return; 5854 5855 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED)); 5856 } 5857 5858 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) { 5859 assert(!WritingAST && "Already writing the AST!"); 5860 if (!D->isFromASTFile()) 5861 return; 5862 5863 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE)); 5864 } 5865 5866 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) { 5867 assert(!WritingAST && "Already writing the AST!"); 5868 assert(D->isHidden() && "expected a hidden declaration"); 5869 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M)); 5870 } 5871 5872 void ASTWriter::AddedAttributeToRecord(const Attr *Attr, 5873 const RecordDecl *Record) { 5874 assert(!WritingAST && "Already writing the AST!"); 5875 if (!Record->isFromASTFile()) 5876 return; 5877 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr)); 5878 } 5879