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