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