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