| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H |
| 15 | #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H |
| 16 | |
| 17 | #include "clang/AST/ASTMutationListener.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclarationName.h" |
| 20 | #include "clang/AST/NestedNameSpecifier.h" |
| 21 | #include "clang/AST/OpenMPClause.h" |
| 22 | #include "clang/AST/TemplateBase.h" |
| 23 | #include "clang/AST/TemplateName.h" |
| 24 | #include "clang/AST/Type.h" |
| 25 | #include "clang/AST/TypeLoc.h" |
| 26 | #include "clang/Basic/LLVM.h" |
| 27 | #include "clang/Basic/SourceLocation.h" |
| 28 | #include "clang/Sema/SemaConsumer.h" |
| 29 | #include "clang/Serialization/ASTBitCodes.h" |
| 30 | #include "clang/Serialization/ASTDeserializationListener.h" |
| 31 | #include "clang/Serialization/PCHContainerOperations.h" |
| 32 | #include "llvm/ADT/ArrayRef.h" |
| 33 | #include "llvm/ADT/DenseMap.h" |
| 34 | #include "llvm/ADT/DenseSet.h" |
| 35 | #include "llvm/ADT/MapVector.h" |
| 36 | #include "llvm/ADT/SetVector.h" |
| 37 | #include "llvm/ADT/SmallVector.h" |
| 38 | #include "llvm/ADT/StringRef.h" |
| 39 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 40 | #include <cassert> |
| 41 | #include <cstddef> |
| 42 | #include <cstdint> |
| 43 | #include <ctime> |
| 44 | #include <memory> |
| 45 | #include <queue> |
| 46 | #include <string> |
| 47 | #include <utility> |
| 48 | #include <vector> |
| 49 | |
| 50 | namespace llvm { |
| 51 | |
| 52 | class APFloat; |
| 53 | class APInt; |
| 54 | class APSInt; |
| 55 | |
| 56 | } |
| 57 | |
| 58 | namespace clang { |
| 59 | |
| 60 | class ASTContext; |
| 61 | class ASTReader; |
| 62 | class ASTUnresolvedSet; |
| 63 | class Attr; |
| 64 | class CXXBaseSpecifier; |
| 65 | class CXXCtorInitializer; |
| 66 | class CXXRecordDecl; |
| 67 | class CXXTemporary; |
| 68 | class FileEntry; |
| 69 | class FPOptions; |
| 70 | class FunctionDecl; |
| 71 | class ; |
| 72 | class ; |
| 73 | class IdentifierResolver; |
| 74 | class LangOptions; |
| 75 | class MacroDefinitionRecord; |
| 76 | class MacroInfo; |
| 77 | class Module; |
| 78 | class InMemoryModuleCache; |
| 79 | class ModuleFileExtension; |
| 80 | class ModuleFileExtensionWriter; |
| 81 | class NamedDecl; |
| 82 | class NestedNameSpecifier; |
| 83 | class ObjCInterfaceDecl; |
| 84 | class PreprocessingRecord; |
| 85 | class Preprocessor; |
| 86 | struct QualifierInfo; |
| 87 | class RecordDecl; |
| 88 | class Sema; |
| 89 | class SourceManager; |
| 90 | class Stmt; |
| 91 | struct StoredDeclsList; |
| 92 | class SwitchCase; |
| 93 | class TemplateParameterList; |
| 94 | class Token; |
| 95 | class TypeSourceInfo; |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | class ASTWriter : public ASTDeserializationListener, |
| 104 | public ASTMutationListener { |
| 105 | public: |
| 106 | friend class ASTDeclWriter; |
| 107 | friend class ASTRecordWriter; |
| 108 | friend class ASTStmtWriter; |
| 109 | friend class ASTTypeWriter; |
| 110 | |
| 111 | using RecordData = SmallVector<uint64_t, 64>; |
| 112 | using RecordDataImpl = SmallVectorImpl<uint64_t>; |
| 113 | using RecordDataRef = ArrayRef<uint64_t>; |
| 114 | |
| 115 | private: |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx, |
| 126 | serialization::UnsafeQualTypeDenseMapInfo>; |
| 127 | |
| 128 | |
| 129 | llvm::BitstreamWriter &Stream; |
| 130 | |
| 131 | |
| 132 | const SmallVectorImpl<char> &Buffer; |
| 133 | |
| 134 | |
| 135 | InMemoryModuleCache &ModuleCache; |
| 136 | |
| 137 | |
| 138 | ASTContext *Context = nullptr; |
| 139 | |
| 140 | |
| 141 | Preprocessor *PP = nullptr; |
| 142 | |
| 143 | |
| 144 | ASTReader *Chain = nullptr; |
| 145 | |
| 146 | |
| 147 | Module *WritingModule = nullptr; |
| 148 | |
| 149 | |
| 150 | std::string BaseDirectory; |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | bool IncludeTimestamps; |
| 157 | |
| 158 | |
| 159 | |
| 160 | bool WritingAST = false; |
| 161 | |
| 162 | |
| 163 | |
| 164 | bool DoneWritingDeclsAndTypes = false; |
| 165 | |
| 166 | |
| 167 | bool ASTHasCompilerErrors = false; |
| 168 | |
| 169 | |
| 170 | |
| 171 | llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs; |
| 172 | |
| 173 | |
| 174 | class DeclOrType { |
| 175 | public: |
| 176 | DeclOrType(Decl *D) : Stored(D), IsType(false) {} |
| 177 | DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {} |
| 178 | |
| 179 | bool isType() const { return IsType; } |
| 180 | bool isDecl() const { return !IsType; } |
| 181 | |
| 182 | QualType getType() const { |
| 183 | (0) . __assert_fail ("isType() && \"Not a type!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Serialization/ASTWriter.h", 183, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isType() && "Not a type!"); |
| 184 | return QualType::getFromOpaquePtr(Stored); |
| 185 | } |
| 186 | |
| 187 | Decl *getDecl() const { |
| 188 | (0) . __assert_fail ("isDecl() && \"Not a decl!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Serialization/ASTWriter.h", 188, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isDecl() && "Not a decl!"); |
| 189 | return static_cast<Decl *>(Stored); |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | void *Stored; |
| 194 | bool IsType; |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | std::queue<DeclOrType> DeclTypesToEmit; |
| 199 | |
| 200 | |
| 201 | serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS; |
| 202 | |
| 203 | |
| 204 | serialization::DeclID NextDeclID = FirstDeclID; |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs; |
| 213 | |
| 214 | |
| 215 | |
| 216 | std::vector<serialization::DeclOffset> DeclOffsets; |
| 217 | |
| 218 | |
| 219 | using LocDeclIDsTy = |
| 220 | SmallVector<std::pair<unsigned, serialization::DeclID>, 64>; |
| 221 | struct DeclIDInFileInfo { |
| 222 | LocDeclIDsTy DeclIDs; |
| 223 | |
| 224 | |
| 225 | |
| 226 | unsigned FirstDeclIndex; |
| 227 | }; |
| 228 | using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>; |
| 229 | |
| 230 | |
| 231 | |
| 232 | FileDeclIDsTy FileDeclIDs; |
| 233 | |
| 234 | void associateDeclWithFile(const Decl *D, serialization::DeclID); |
| 235 | |
| 236 | |
| 237 | serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS; |
| 238 | |
| 239 | |
| 240 | serialization::TypeID NextTypeID = FirstTypeID; |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | TypeIdxMap TypeIdxs; |
| 252 | |
| 253 | |
| 254 | |
| 255 | std::vector<uint32_t> TypeOffsets; |
| 256 | |
| 257 | |
| 258 | serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS; |
| 259 | |
| 260 | |
| 261 | serialization::IdentID NextIdentID = FirstIdentID; |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs; |
| 270 | |
| 271 | |
| 272 | serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS; |
| 273 | |
| 274 | |
| 275 | serialization::MacroID NextMacroID = FirstMacroID; |
| 276 | |
| 277 | |
| 278 | llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs; |
| 279 | |
| 280 | struct MacroInfoToEmitData { |
| 281 | const IdentifierInfo *Name; |
| 282 | MacroInfo *MI; |
| 283 | serialization::MacroID ID; |
| 284 | }; |
| 285 | |
| 286 | |
| 287 | std::vector<MacroInfoToEmitData> MacroInfosToEmit; |
| 288 | |
| 289 | llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap; |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | llvm::DenseSet<Stmt *> ParentStmts; |
| 296 | |
| 297 | |
| 298 | |
| 299 | llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries; |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | std::vector<uint32_t> IdentifierOffsets; |
| 306 | |
| 307 | |
| 308 | serialization::SubmoduleID FirstSubmoduleID = |
| 309 | serialization::NUM_PREDEF_SUBMODULE_IDS; |
| 310 | |
| 311 | |
| 312 | serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID; |
| 313 | |
| 314 | |
| 315 | serialization::SelectorID FirstSelectorID = |
| 316 | serialization::NUM_PREDEF_SELECTOR_IDS; |
| 317 | |
| 318 | |
| 319 | serialization::SelectorID NextSelectorID = FirstSelectorID; |
| 320 | |
| 321 | |
| 322 | llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs; |
| 323 | |
| 324 | |
| 325 | |
| 326 | std::vector<uint32_t> SelectorOffsets; |
| 327 | |
| 328 | |
| 329 | |
| 330 | llvm::DenseMap<const MacroDefinitionRecord *, |
| 331 | serialization::PreprocessedEntityID> MacroDefinitions; |
| 332 | |
| 333 | |
| 334 | |
| 335 | llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers; |
| 336 | |
| 337 | |
| 338 | class DeclUpdate { |
| 339 | |
| 340 | unsigned Kind; |
| 341 | union { |
| 342 | const Decl *Dcl; |
| 343 | void *Type; |
| 344 | unsigned Loc; |
| 345 | unsigned Val; |
| 346 | Module *Mod; |
| 347 | const Attr *Attribute; |
| 348 | }; |
| 349 | |
| 350 | public: |
| 351 | DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {} |
| 352 | DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {} |
| 353 | DeclUpdate(unsigned Kind, QualType Type) |
| 354 | : Kind(Kind), Type(Type.getAsOpaquePtr()) {} |
| 355 | DeclUpdate(unsigned Kind, SourceLocation Loc) |
| 356 | : Kind(Kind), Loc(Loc.getRawEncoding()) {} |
| 357 | DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {} |
| 358 | DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {} |
| 359 | DeclUpdate(unsigned Kind, const Attr *Attribute) |
| 360 | : Kind(Kind), Attribute(Attribute) {} |
| 361 | |
| 362 | unsigned getKind() const { return Kind; } |
| 363 | const Decl *getDecl() const { return Dcl; } |
| 364 | QualType getType() const { return QualType::getFromOpaquePtr(Type); } |
| 365 | |
| 366 | SourceLocation getLoc() const { |
| 367 | return SourceLocation::getFromRawEncoding(Loc); |
| 368 | } |
| 369 | |
| 370 | unsigned getNumber() const { return Val; } |
| 371 | Module *getModule() const { return Mod; } |
| 372 | const Attr *getAttr() const { return Attribute; } |
| 373 | }; |
| 374 | |
| 375 | using UpdateRecord = SmallVector<DeclUpdate, 1>; |
| 376 | using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>; |
| 377 | |
| 378 | |
| 379 | |
| 380 | DeclUpdateMap DeclUpdates; |
| 381 | |
| 382 | using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>; |
| 383 | |
| 384 | |
| 385 | |
| 386 | FirstLatestDeclMap FirstLatestDecls; |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | SmallVector<uint64_t, 16> EagerlyDeserializedDecls; |
| 400 | SmallVector<uint64_t, 16> ModularCodegenDecls; |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts; |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced; |
| 415 | |
| 416 | |
| 417 | |
| 418 | llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories; |
| 419 | |
| 420 | |
| 421 | |
| 422 | llvm::SmallVector<const Decl *, 16> Redeclarations; |
| 423 | |
| 424 | |
| 425 | |
| 426 | llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache; |
| 427 | |
| 428 | |
| 429 | llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs; |
| 430 | |
| 431 | |
| 432 | unsigned NumStatements = 0; |
| 433 | |
| 434 | |
| 435 | unsigned NumMacros = 0; |
| 436 | |
| 437 | |
| 438 | |
| 439 | unsigned NumLexicalDeclContexts = 0; |
| 440 | |
| 441 | |
| 442 | |
| 443 | unsigned NumVisibleDeclContexts = 0; |
| 444 | |
| 445 | |
| 446 | |
| 447 | llvm::DenseMap<Module *, unsigned> SubmoduleIDs; |
| 448 | |
| 449 | |
| 450 | std::vector<std::unique_ptr<ModuleFileExtensionWriter>> |
| 451 | ModuleFileExtensionWriters; |
| 452 | |
| 453 | |
| 454 | unsigned getSubmoduleID(Module *Mod); |
| 455 | |
| 456 | |
| 457 | void WriteSubStmt(Stmt *S); |
| 458 | |
| 459 | void WriteBlockInfoBlock(); |
| 460 | void WriteControlBlock(Preprocessor &PP, ASTContext &Context, |
| 461 | StringRef isysroot, const std::string &OutputFile); |
| 462 | |
| 463 | |
| 464 | ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP, |
| 465 | ASTContext &Context); |
| 466 | |
| 467 | |
| 468 | static ASTFileSignature createSignature(StringRef Bytes); |
| 469 | |
| 470 | void (SourceManager &SourceMgr, HeaderSearchOptions &HSOpts, |
| 471 | bool Modules); |
| 472 | void WriteSourceManagerBlock(SourceManager &SourceMgr, |
| 473 | const Preprocessor &PP); |
| 474 | void WritePreprocessor(const Preprocessor &PP, bool IsModule); |
| 475 | void (const HeaderSearch &HS); |
| 476 | void WritePreprocessorDetail(PreprocessingRecord &PPRec); |
| 477 | void WriteSubmodules(Module *WritingModule); |
| 478 | |
| 479 | void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag, |
| 480 | bool isModule); |
| 481 | |
| 482 | unsigned TypeExtQualAbbrev = 0; |
| 483 | unsigned TypeFunctionProtoAbbrev = 0; |
| 484 | void WriteTypeAbbrevs(); |
| 485 | void WriteType(QualType T); |
| 486 | |
| 487 | bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC); |
| 488 | bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC); |
| 489 | |
| 490 | void GenerateNameLookupTable(const DeclContext *DC, |
| 491 | llvm::SmallVectorImpl<char> &LookupTable); |
| 492 | uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC); |
| 493 | uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC); |
| 494 | void WriteTypeDeclOffsets(); |
| 495 | void WriteFileDeclIDsMap(); |
| 496 | void (); |
| 497 | void WriteSelectors(Sema &SemaRef); |
| 498 | void WriteReferencedSelectorsPool(Sema &SemaRef); |
| 499 | void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver, |
| 500 | bool IsModule); |
| 501 | void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord); |
| 502 | void WriteDeclContextVisibleUpdate(const DeclContext *DC); |
| 503 | void WriteFPPragmaOptions(const FPOptions &Opts); |
| 504 | void WriteOpenCLExtensions(Sema &SemaRef); |
| 505 | void WriteOpenCLExtensionTypes(Sema &SemaRef); |
| 506 | void WriteOpenCLExtensionDecls(Sema &SemaRef); |
| 507 | void WriteCUDAPragmas(Sema &SemaRef); |
| 508 | void WriteObjCCategories(); |
| 509 | void WriteLateParsedTemplates(Sema &SemaRef); |
| 510 | void WriteOptimizePragmaOptions(Sema &SemaRef); |
| 511 | void WriteMSStructPragmaOptions(Sema &SemaRef); |
| 512 | void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef); |
| 513 | void WritePackPragmaOptions(Sema &SemaRef); |
| 514 | void WriteModuleFileExtension(Sema &SemaRef, |
| 515 | ModuleFileExtensionWriter &Writer); |
| 516 | |
| 517 | unsigned DeclParmVarAbbrev = 0; |
| 518 | unsigned DeclContextLexicalAbbrev = 0; |
| 519 | unsigned DeclContextVisibleLookupAbbrev = 0; |
| 520 | unsigned UpdateVisibleAbbrev = 0; |
| 521 | unsigned DeclRecordAbbrev = 0; |
| 522 | unsigned DeclTypedefAbbrev = 0; |
| 523 | unsigned DeclVarAbbrev = 0; |
| 524 | unsigned DeclFieldAbbrev = 0; |
| 525 | unsigned DeclEnumAbbrev = 0; |
| 526 | unsigned DeclObjCIvarAbbrev = 0; |
| 527 | unsigned DeclCXXMethodAbbrev = 0; |
| 528 | |
| 529 | unsigned DeclRefExprAbbrev = 0; |
| 530 | unsigned CharacterLiteralAbbrev = 0; |
| 531 | unsigned IntegerLiteralAbbrev = 0; |
| 532 | unsigned ExprImplicitCastAbbrev = 0; |
| 533 | |
| 534 | void WriteDeclAbbrevs(); |
| 535 | void WriteDecl(ASTContext &Context, Decl *D); |
| 536 | |
| 537 | ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot, |
| 538 | const std::string &OutputFile, |
| 539 | Module *WritingModule); |
| 540 | |
| 541 | public: |
| 542 | |
| 543 | |
| 544 | ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer, |
| 545 | InMemoryModuleCache &ModuleCache, |
| 546 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, |
| 547 | bool IncludeTimestamps = true); |
| 548 | ~ASTWriter() override; |
| 549 | |
| 550 | const LangOptions &getLangOpts() const; |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | time_t getTimestampForOutput(const FileEntry *E) const; |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, |
| 572 | Module *WritingModule, StringRef isysroot, |
| 573 | bool hasErrors = false, |
| 574 | bool ShouldCacheASTInMemory = false); |
| 575 | |
| 576 | |
| 577 | void AddToken(const Token &Tok, RecordDataImpl &Record); |
| 578 | |
| 579 | |
| 580 | void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record); |
| 581 | |
| 582 | |
| 583 | void AddSourceRange(SourceRange Range, RecordDataImpl &Record); |
| 584 | |
| 585 | |
| 586 | void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record); |
| 587 | |
| 588 | |
| 589 | serialization::SelectorID getSelectorRef(Selector Sel); |
| 590 | |
| 591 | |
| 592 | serialization::IdentID getIdentifierRef(const IdentifierInfo *II); |
| 593 | |
| 594 | |
| 595 | serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name); |
| 596 | |
| 597 | |
| 598 | serialization::MacroID getMacroID(MacroInfo *MI); |
| 599 | |
| 600 | uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name); |
| 601 | |
| 602 | |
| 603 | void AddTypeRef(QualType T, RecordDataImpl &Record); |
| 604 | |
| 605 | |
| 606 | serialization::TypeID GetOrCreateTypeID(QualType T); |
| 607 | |
| 608 | |
| 609 | serialization::TypeID getTypeID(QualType T) const; |
| 610 | |
| 611 | |
| 612 | |
| 613 | const Decl *getFirstLocalDecl(const Decl *D); |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | bool IsLocalDecl(const Decl *D) { |
| 619 | if (D->isFromASTFile()) |
| 620 | return false; |
| 621 | auto I = DeclIDs.find(D); |
| 622 | return (I == DeclIDs.end() || |
| 623 | I->second >= serialization::NUM_PREDEF_DECL_IDS); |
| 624 | }; |
| 625 | |
| 626 | |
| 627 | void AddDeclRef(const Decl *D, RecordDataImpl &Record); |
| 628 | |
| 629 | |
| 630 | serialization::DeclID GetDeclRef(const Decl *D); |
| 631 | |
| 632 | |
| 633 | |
| 634 | serialization::DeclID getDeclID(const Decl *D); |
| 635 | |
| 636 | unsigned getAnonymousDeclarationNumber(const NamedDecl *D); |
| 637 | |
| 638 | |
| 639 | void AddString(StringRef Str, RecordDataImpl &Record); |
| 640 | |
| 641 | |
| 642 | |
| 643 | bool PreparePathForOutput(SmallVectorImpl<char> &Path); |
| 644 | |
| 645 | |
| 646 | void AddPath(StringRef Path, RecordDataImpl &Record); |
| 647 | |
| 648 | |
| 649 | void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record, |
| 650 | StringRef Path); |
| 651 | |
| 652 | |
| 653 | void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record); |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | unsigned getLocalOrImportedSubmoduleID(Module *Mod); |
| 659 | |
| 660 | |
| 661 | |
| 662 | void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset); |
| 663 | |
| 664 | |
| 665 | |
| 666 | void SetSelectorOffset(Selector Sel, uint32_t Offset); |
| 667 | |
| 668 | |
| 669 | unsigned RecordSwitchCaseID(SwitchCase *S); |
| 670 | |
| 671 | |
| 672 | unsigned getSwitchCaseID(SwitchCase *S); |
| 673 | |
| 674 | void ClearSwitchCaseIDs(); |
| 675 | |
| 676 | unsigned getTypeExtQualAbbrev() const { |
| 677 | return TypeExtQualAbbrev; |
| 678 | } |
| 679 | |
| 680 | unsigned getTypeFunctionProtoAbbrev() const { |
| 681 | return TypeFunctionProtoAbbrev; |
| 682 | } |
| 683 | |
| 684 | unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; } |
| 685 | unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; } |
| 686 | unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; } |
| 687 | unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; } |
| 688 | unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; } |
| 689 | unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; } |
| 690 | unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; } |
| 691 | unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; } |
| 692 | |
| 693 | unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; } |
| 694 | unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; } |
| 695 | unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; } |
| 696 | unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; } |
| 697 | |
| 698 | bool hasChain() const { return Chain; } |
| 699 | ASTReader *getChain() const { return Chain; } |
| 700 | |
| 701 | private: |
| 702 | |
| 703 | void ReaderInitialized(ASTReader *Reader) override; |
| 704 | void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override; |
| 705 | void MacroRead(serialization::MacroID ID, MacroInfo *MI) override; |
| 706 | void TypeRead(serialization::TypeIdx Idx, QualType T) override; |
| 707 | void SelectorRead(serialization::SelectorID ID, Selector Sel) override; |
| 708 | void MacroDefinitionRead(serialization::PreprocessedEntityID ID, |
| 709 | MacroDefinitionRecord *MD) override; |
| 710 | void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override; |
| 711 | |
| 712 | |
| 713 | void CompletedTagDefinition(const TagDecl *D) override; |
| 714 | void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override; |
| 715 | void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override; |
| 716 | void AddedCXXTemplateSpecialization( |
| 717 | const ClassTemplateDecl *TD, |
| 718 | const ClassTemplateSpecializationDecl *D) override; |
| 719 | void AddedCXXTemplateSpecialization( |
| 720 | const VarTemplateDecl *TD, |
| 721 | const VarTemplateSpecializationDecl *D) override; |
| 722 | void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, |
| 723 | const FunctionDecl *D) override; |
| 724 | void ResolvedExceptionSpec(const FunctionDecl *FD) override; |
| 725 | void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override; |
| 726 | void ResolvedOperatorDelete(const CXXDestructorDecl *DD, |
| 727 | const FunctionDecl *Delete, |
| 728 | Expr *ThisArg) override; |
| 729 | void CompletedImplicitDefinition(const FunctionDecl *D) override; |
| 730 | void InstantiationRequested(const ValueDecl *D) override; |
| 731 | void VariableDefinitionInstantiated(const VarDecl *D) override; |
| 732 | void FunctionDefinitionInstantiated(const FunctionDecl *D) override; |
| 733 | void DefaultArgumentInstantiated(const ParmVarDecl *D) override; |
| 734 | void DefaultMemberInitializerInstantiated(const FieldDecl *D) override; |
| 735 | void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, |
| 736 | const ObjCInterfaceDecl *IFD) override; |
| 737 | void DeclarationMarkedUsed(const Decl *D) override; |
| 738 | void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override; |
| 739 | void DeclarationMarkedOpenMPDeclareTarget(const Decl *D, |
| 740 | const Attr *Attr) override; |
| 741 | void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override; |
| 742 | void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override; |
| 743 | void AddedAttributeToRecord(const Attr *Attr, |
| 744 | const RecordDecl *Record) override; |
| 745 | }; |
| 746 | |
| 747 | |
| 748 | class ASTRecordWriter { |
| 749 | ASTWriter *Writer; |
| 750 | ASTWriter::RecordDataImpl *Record; |
| 751 | |
| 752 | |
| 753 | |
| 754 | SmallVector<Stmt *, 16> StmtsToEmit; |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | SmallVector<unsigned, 8> OffsetIndices; |
| 760 | |
| 761 | |
| 762 | |
| 763 | void FlushStmts(); |
| 764 | void FlushSubStmts(); |
| 765 | |
| 766 | void PrepareToEmit(uint64_t MyOffset) { |
| 767 | |
| 768 | for (unsigned I : OffsetIndices) { |
| 769 | auto &StoredOffset = (*Record)[I]; |
| 770 | (0) . __assert_fail ("StoredOffset < MyOffset && \"invalid offset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Serialization/ASTWriter.h", 770, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(StoredOffset < MyOffset && "invalid offset"); |
| 771 | if (StoredOffset) |
| 772 | StoredOffset = MyOffset - StoredOffset; |
| 773 | } |
| 774 | OffsetIndices.clear(); |
| 775 | } |
| 776 | |
| 777 | public: |
| 778 | |
| 779 | ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
| 780 | : Writer(&Writer), Record(&Record) {} |
| 781 | |
| 782 | |
| 783 | |
| 784 | ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record) |
| 785 | : Writer(Parent.Writer), Record(&Record) {} |
| 786 | |
| 787 | |
| 788 | ASTRecordWriter(const ASTRecordWriter &) = delete; |
| 789 | ASTRecordWriter &operator=(const ASTRecordWriter &) = delete; |
| 790 | |
| 791 | |
| 792 | ASTWriter::RecordDataImpl &getRecordData() const { return *Record; } |
| 793 | |
| 794 | |
| 795 | |
| 796 | void push_back(uint64_t N) { Record->push_back(N); } |
| 797 | template<typename InputIterator> |
| 798 | void append(InputIterator begin, InputIterator end) { |
| 799 | Record->append(begin, end); |
| 800 | } |
| 801 | bool empty() const { return Record->empty(); } |
| 802 | size_t size() const { return Record->size(); } |
| 803 | uint64_t &operator[](size_t N) { return (*Record)[N]; } |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | uint64_t Emit(unsigned Code, unsigned Abbrev = 0) { |
| 810 | uint64_t Offset = Writer->Stream.GetCurrentBitNo(); |
| 811 | PrepareToEmit(Offset); |
| 812 | Writer->Stream.EmitRecord(Code, *Record, Abbrev); |
| 813 | FlushStmts(); |
| 814 | return Offset; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) { |
| 819 | FlushSubStmts(); |
| 820 | PrepareToEmit(Writer->Stream.GetCurrentBitNo()); |
| 821 | Writer->Stream.EmitRecord(Code, *Record, Abbrev); |
| 822 | return Writer->Stream.GetCurrentBitNo(); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | |
| 827 | void AddOffset(uint64_t BitOffset) { |
| 828 | OffsetIndices.push_back(Record->size()); |
| 829 | Record->push_back(BitOffset); |
| 830 | } |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | void AddStmt(Stmt *S) { |
| 840 | StmtsToEmit.push_back(S); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | |
| 845 | void AddFunctionDefinition(const FunctionDecl *FD); |
| 846 | |
| 847 | |
| 848 | void AddSourceLocation(SourceLocation Loc) { |
| 849 | return Writer->AddSourceLocation(Loc, *Record); |
| 850 | } |
| 851 | |
| 852 | |
| 853 | void AddSourceRange(SourceRange Range) { |
| 854 | return Writer->AddSourceRange(Range, *Record); |
| 855 | } |
| 856 | |
| 857 | |
| 858 | void AddAPInt(const llvm::APInt &Value); |
| 859 | |
| 860 | |
| 861 | void AddAPSInt(const llvm::APSInt &Value); |
| 862 | |
| 863 | |
| 864 | void AddAPFloat(const llvm::APFloat &Value); |
| 865 | |
| 866 | |
| 867 | void AddIdentifierRef(const IdentifierInfo *II) { |
| 868 | return Writer->AddIdentifierRef(II, *Record); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | void AddSelectorRef(Selector S); |
| 873 | |
| 874 | |
| 875 | void AddCXXTemporary(const CXXTemporary *Temp); |
| 876 | |
| 877 | |
| 878 | void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base); |
| 879 | |
| 880 | |
| 881 | void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases); |
| 882 | |
| 883 | |
| 884 | void AddTypeRef(QualType T) { |
| 885 | return Writer->AddTypeRef(T, *Record); |
| 886 | } |
| 887 | |
| 888 | |
| 889 | void AddTypeSourceInfo(TypeSourceInfo *TInfo); |
| 890 | |
| 891 | |
| 892 | void AddTypeLoc(TypeLoc TL); |
| 893 | |
| 894 | |
| 895 | void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
| 896 | const TemplateArgumentLocInfo &Arg); |
| 897 | |
| 898 | |
| 899 | void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg); |
| 900 | |
| 901 | |
| 902 | void AddASTTemplateArgumentListInfo( |
| 903 | const ASTTemplateArgumentListInfo *ASTTemplArgList); |
| 904 | |
| 905 | |
| 906 | void AddDeclRef(const Decl *D) { |
| 907 | return Writer->AddDeclRef(D, *Record); |
| 908 | } |
| 909 | |
| 910 | |
| 911 | void AddDeclarationName(DeclarationName Name); |
| 912 | |
| 913 | void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, |
| 914 | DeclarationName Name); |
| 915 | void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo); |
| 916 | |
| 917 | void AddQualifierInfo(const QualifierInfo &Info); |
| 918 | |
| 919 | |
| 920 | void AddNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 921 | |
| 922 | |
| 923 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); |
| 924 | |
| 925 | |
| 926 | void AddTemplateName(TemplateName Name); |
| 927 | |
| 928 | |
| 929 | void AddTemplateArgument(const TemplateArgument &Arg); |
| 930 | |
| 931 | |
| 932 | void AddTemplateParameterList(const TemplateParameterList *TemplateParams); |
| 933 | |
| 934 | |
| 935 | void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs); |
| 936 | |
| 937 | |
| 938 | void AddUnresolvedSet(const ASTUnresolvedSet &Set); |
| 939 | |
| 940 | |
| 941 | void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer *> CtorInits); |
| 942 | |
| 943 | void AddCXXDefinitionData(const CXXRecordDecl *D); |
| 944 | |
| 945 | |
| 946 | void AddString(StringRef Str) { |
| 947 | return Writer->AddString(Str, *Record); |
| 948 | } |
| 949 | |
| 950 | |
| 951 | void AddPath(StringRef Path) { |
| 952 | return Writer->AddPath(Path, *Record); |
| 953 | } |
| 954 | |
| 955 | |
| 956 | void AddVersionTuple(const VersionTuple &Version) { |
| 957 | return Writer->AddVersionTuple(Version, *Record); |
| 958 | } |
| 959 | |
| 960 | |
| 961 | void AddAttr(const Attr *A); |
| 962 | |
| 963 | |
| 964 | void AddAttributes(ArrayRef<const Attr*> Attrs); |
| 965 | }; |
| 966 | |
| 967 | |
| 968 | |
| 969 | class PCHGenerator : public SemaConsumer { |
| 970 | const Preprocessor &PP; |
| 971 | std::string OutputFile; |
| 972 | std::string isysroot; |
| 973 | Sema *SemaPtr; |
| 974 | std::shared_ptr<PCHBuffer> Buffer; |
| 975 | llvm::BitstreamWriter Stream; |
| 976 | ASTWriter Writer; |
| 977 | bool AllowASTWithErrors; |
| 978 | bool ShouldCacheASTInMemory; |
| 979 | |
| 980 | protected: |
| 981 | ASTWriter &getWriter() { return Writer; } |
| 982 | const ASTWriter &getWriter() const { return Writer; } |
| 983 | SmallVectorImpl<char> &getPCH() const { return Buffer->Data; } |
| 984 | |
| 985 | public: |
| 986 | PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache, |
| 987 | StringRef OutputFile, StringRef isysroot, |
| 988 | std::shared_ptr<PCHBuffer> Buffer, |
| 989 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, |
| 990 | bool AllowASTWithErrors = false, bool IncludeTimestamps = true, |
| 991 | bool ShouldCacheASTInMemory = false); |
| 992 | ~PCHGenerator() override; |
| 993 | |
| 994 | void InitializeSema(Sema &S) override { SemaPtr = &S; } |
| 995 | void HandleTranslationUnit(ASTContext &Ctx) override; |
| 996 | ASTMutationListener *GetASTMutationListener() override; |
| 997 | ASTDeserializationListener *GetASTDeserializationListener() override; |
| 998 | bool hasEmittedPCH() const { return Buffer->IsComplete; } |
| 999 | }; |
| 1000 | |
| 1001 | class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> { |
| 1002 | ASTRecordWriter &Record; |
| 1003 | |
| 1004 | public: |
| 1005 | OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {} |
| 1006 | #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *S); |
| 1007 | #include "clang/Basic/OpenMPKinds.def" |
| 1008 | void writeClause(OMPClause *C); |
| 1009 | void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); |
| 1010 | void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); |
| 1011 | }; |
| 1012 | |
| 1013 | } |
| 1014 | |
| 1015 | #endif |
| 1016 | |