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