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