| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #ifndef LLVM_CLANG_AST_EXTERNALASTSOURCE_H |
| 15 | #define LLVM_CLANG_AST_EXTERNALASTSOURCE_H |
| 16 | |
| 17 | #include "clang/AST/CharUnits.h" |
| 18 | #include "clang/AST/DeclBase.h" |
| 19 | #include "clang/Basic/LLVM.h" |
| 20 | #include "clang/Basic/Module.h" |
| 21 | #include "llvm/ADT/ArrayRef.h" |
| 22 | #include "llvm/ADT/DenseMap.h" |
| 23 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 24 | #include "llvm/ADT/Optional.h" |
| 25 | #include "llvm/ADT/PointerUnion.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
| 29 | #include "llvm/ADT/iterator.h" |
| 30 | #include "llvm/Support/PointerLikeTypeTraits.h" |
| 31 | #include <cassert> |
| 32 | #include <cstddef> |
| 33 | #include <cstdint> |
| 34 | #include <iterator> |
| 35 | #include <string> |
| 36 | #include <utility> |
| 37 | |
| 38 | namespace clang { |
| 39 | |
| 40 | class ASTConsumer; |
| 41 | class ASTContext; |
| 42 | class CXXBaseSpecifier; |
| 43 | class CXXCtorInitializer; |
| 44 | class CXXRecordDecl; |
| 45 | class DeclarationName; |
| 46 | class FieldDecl; |
| 47 | class IdentifierInfo; |
| 48 | class NamedDecl; |
| 49 | class ObjCInterfaceDecl; |
| 50 | class RecordDecl; |
| 51 | class Selector; |
| 52 | class Stmt; |
| 53 | class TagDecl; |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | class ExternalASTSource : public RefCountedBase<ExternalASTSource> { |
| 63 | friend class ExternalSemaSource; |
| 64 | |
| 65 | |
| 66 | |
| 67 | uint32_t CurrentGeneration = 0; |
| 68 | |
| 69 | |
| 70 | |
| 71 | bool SemaSource = false; |
| 72 | |
| 73 | public: |
| 74 | ExternalASTSource() = default; |
| 75 | virtual ~ExternalASTSource(); |
| 76 | |
| 77 | |
| 78 | |
| 79 | class Deserializing { |
| 80 | ExternalASTSource *Source; |
| 81 | |
| 82 | public: |
| 83 | explicit Deserializing(ExternalASTSource *source) : Source(source) { |
| 84 | assert(Source); |
| 85 | Source->StartedDeserializing(); |
| 86 | } |
| 87 | |
| 88 | ~Deserializing() { |
| 89 | Source->FinishedDeserializing(); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | uint32_t getGeneration() const { return CurrentGeneration; } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | virtual Decl *GetExternalDecl(uint32_t ID); |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | virtual Selector GetExternalSelector(uint32_t ID); |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | virtual uint32_t GetNumExternalSelectors(); |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | virtual Stmt *GetExternalDeclStmt(uint64_t Offset); |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | virtual CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset); |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset); |
| 142 | |
| 143 | |
| 144 | virtual void updateOutOfDateIdentifier(IdentifierInfo &II) {} |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | virtual bool |
| 154 | FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name); |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | virtual void completeVisibleDeclsMap(const DeclContext *DC); |
| 161 | |
| 162 | |
| 163 | virtual Module *getModule(unsigned ID) { return nullptr; } |
| 164 | |
| 165 | |
| 166 | |
| 167 | virtual bool DeclIsFromPCHWithObjectFile(const Decl *D) { return false; } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | class ASTSourceDescriptor { |
| 173 | StringRef PCHModuleName; |
| 174 | StringRef Path; |
| 175 | StringRef ASTFile; |
| 176 | ASTFileSignature Signature; |
| 177 | const Module *ClangModule = nullptr; |
| 178 | |
| 179 | public: |
| 180 | ASTSourceDescriptor() = default; |
| 181 | ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile, |
| 182 | ASTFileSignature Signature) |
| 183 | : PCHModuleName(std::move(Name)), Path(std::move(Path)), |
| 184 | ASTFile(std::move(ASTFile)), Signature(Signature) {} |
| 185 | ASTSourceDescriptor(const Module &M); |
| 186 | |
| 187 | std::string getModuleName() const; |
| 188 | StringRef getPath() const { return Path; } |
| 189 | StringRef getASTFile() const { return ASTFile; } |
| 190 | ASTFileSignature getSignature() const { return Signature; } |
| 191 | const Module *getModuleOrNull() const { return ClangModule; } |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID); |
| 196 | |
| 197 | enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy }; |
| 198 | |
| 199 | virtual ExtKind hasExternalDefinitions(const Decl *D); |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | virtual void |
| 209 | FindExternalLexicalDecls(const DeclContext *DC, |
| 210 | llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, |
| 211 | SmallVectorImpl<Decl *> &Result); |
| 212 | |
| 213 | |
| 214 | |
| 215 | void FindExternalLexicalDecls(const DeclContext *DC, |
| 216 | SmallVectorImpl<Decl *> &Result) { |
| 217 | FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | virtual void FindFileRegionDecls(FileID File, unsigned Offset, |
| 224 | unsigned Length, |
| 225 | SmallVectorImpl<Decl *> &Decls); |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | virtual void CompleteRedeclChain(const Decl *D); |
| 232 | |
| 233 | |
| 234 | |
| 235 | virtual void CompleteType(TagDecl *Tag); |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | virtual void CompleteType(ObjCInterfaceDecl *Class); |
| 244 | |
| 245 | |
| 246 | virtual void (); |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | virtual void StartedDeserializing(); |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | virtual void FinishedDeserializing(); |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | virtual void StartTranslationUnit(ASTConsumer *Consumer); |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | virtual void PrintStats(); |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | virtual bool layoutRecordType( |
| 301 | const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment, |
| 302 | llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets, |
| 303 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets, |
| 304 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets); |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | struct MemoryBufferSizes { |
| 311 | size_t malloc_bytes; |
| 312 | size_t mmap_bytes; |
| 313 | |
| 314 | MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) |
| 315 | : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} |
| 316 | }; |
| 317 | |
| 318 | |
| 319 | |
| 320 | MemoryBufferSizes getMemoryBufferSizes() const { |
| 321 | MemoryBufferSizes sizes(0, 0); |
| 322 | getMemoryBufferSizes(sizes); |
| 323 | return sizes; |
| 324 | } |
| 325 | |
| 326 | virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const; |
| 327 | |
| 328 | protected: |
| 329 | static DeclContextLookupResult |
| 330 | SetExternalVisibleDeclsForName(const DeclContext *DC, |
| 331 | DeclarationName Name, |
| 332 | ArrayRef<NamedDecl*> Decls); |
| 333 | |
| 334 | static DeclContextLookupResult |
| 335 | SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 336 | DeclarationName Name); |
| 337 | |
| 338 | |
| 339 | uint32_t incrementGeneration(ASTContext &C); |
| 340 | }; |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)> |
| 349 | struct LazyOffsetPtr { |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | mutable uint64_t Ptr = 0; |
| 356 | |
| 357 | public: |
| 358 | LazyOffsetPtr() = default; |
| 359 | explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {} |
| 360 | |
| 361 | explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) { |
| 362 | (0) . __assert_fail ("(Offset << 1 >> 1) == Offset && \"Offsets must require < 63 bits\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/ExternalASTSource.h", 362, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits"); |
| 363 | if (Offset == 0) |
| 364 | Ptr = 0; |
| 365 | } |
| 366 | |
| 367 | LazyOffsetPtr &operator=(T *Ptr) { |
| 368 | this->Ptr = reinterpret_cast<uint64_t>(Ptr); |
| 369 | return *this; |
| 370 | } |
| 371 | |
| 372 | LazyOffsetPtr &operator=(uint64_t Offset) { |
| 373 | (0) . __assert_fail ("(Offset << 1 >> 1) == Offset && \"Offsets must require < 63 bits\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/ExternalASTSource.h", 373, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits"); |
| 374 | if (Offset == 0) |
| 375 | Ptr = 0; |
| 376 | else |
| 377 | Ptr = (Offset << 1) | 0x01; |
| 378 | |
| 379 | return *this; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | explicit operator bool() const { return Ptr != 0; } |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | bool isValid() const { return Ptr != 0; } |
| 391 | |
| 392 | |
| 393 | bool isOffset() const { return Ptr & 0x01; } |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | T* get(ExternalASTSource *Source) const { |
| 401 | if (isOffset()) { |
| 402 | (0) . __assert_fail ("Source && \"Cannot deserialize a lazy pointer without an AST source\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/ExternalASTSource.h", 403, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Source && |
| 403 | (0) . __assert_fail ("Source && \"Cannot deserialize a lazy pointer without an AST source\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/ExternalASTSource.h", 403, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Cannot deserialize a lazy pointer without an AST source"); |
| 404 | Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1)); |
| 405 | } |
| 406 | return reinterpret_cast<T*>(Ptr); |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)> |
| 414 | struct LazyGenerationalUpdatePtr { |
| 415 | |
| 416 | |
| 417 | struct LazyData { |
| 418 | ExternalASTSource *ExternalSource; |
| 419 | uint32_t LastGeneration = 0; |
| 420 | T LastValue; |
| 421 | |
| 422 | LazyData(ExternalASTSource *Source, T Value) |
| 423 | : ExternalSource(Source), LastValue(Value) {} |
| 424 | }; |
| 425 | |
| 426 | |
| 427 | using ValueType = llvm::PointerUnion<T, LazyData*>; |
| 428 | ValueType Value; |
| 429 | |
| 430 | LazyGenerationalUpdatePtr(ValueType V) : Value(V) {} |
| 431 | |
| 432 | |
| 433 | static ValueType makeValue(const ASTContext &Ctx, T Value); |
| 434 | |
| 435 | public: |
| 436 | explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T()) |
| 437 | : Value(makeValue(Ctx, Value)) {} |
| 438 | |
| 439 | |
| 440 | |
| 441 | enum NotUpdatedTag { NotUpdated }; |
| 442 | LazyGenerationalUpdatePtr(NotUpdatedTag, T Value = T()) |
| 443 | : Value(Value) {} |
| 444 | |
| 445 | |
| 446 | void markIncomplete() { |
| 447 | Value.template get<LazyData *>()->LastGeneration = 0; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void set(T NewValue) { |
| 452 | if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) { |
| 453 | LazyVal->LastValue = NewValue; |
| 454 | return; |
| 455 | } |
| 456 | Value = NewValue; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | void setNotUpdated(T NewValue) { Value = NewValue; } |
| 461 | |
| 462 | |
| 463 | T get(Owner O) { |
| 464 | if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) { |
| 465 | if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) { |
| 466 | LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration(); |
| 467 | (LazyVal->ExternalSource->*Update)(O); |
| 468 | } |
| 469 | return LazyVal->LastValue; |
| 470 | } |
| 471 | return Value.template get<T>(); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | T getNotUpdated() const { |
| 476 | if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) |
| 477 | return LazyVal->LastValue; |
| 478 | return Value.template get<T>(); |
| 479 | } |
| 480 | |
| 481 | void *getOpaqueValue() { return Value.getOpaqueValue(); } |
| 482 | static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) { |
| 483 | return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr)); |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | } |
| 488 | |
| 489 | |
| 490 | |
| 491 | namespace llvm { |
| 492 | |
| 493 | template<typename Owner, typename T, |
| 494 | void (clang::ExternalASTSource::*Update)(Owner)> |
| 495 | struct PointerLikeTypeTraits< |
| 496 | clang::LazyGenerationalUpdatePtr<Owner, T, Update>> { |
| 497 | using Ptr = clang::LazyGenerationalUpdatePtr<Owner, T, Update>; |
| 498 | |
| 499 | static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); } |
| 500 | static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); } |
| 501 | |
| 502 | enum { |
| 503 | NumLowBitsAvailable = PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1 |
| 504 | }; |
| 505 | }; |
| 506 | |
| 507 | } |
| 508 | |
| 509 | namespace clang { |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | template<typename T, typename Source, |
| 518 | void (Source::*Loader)(SmallVectorImpl<T>&), |
| 519 | unsigned LoadedStorage = 2, unsigned LocalStorage = 4> |
| 520 | class LazyVector { |
| 521 | SmallVector<T, LoadedStorage> Loaded; |
| 522 | SmallVector<T, LocalStorage> Local; |
| 523 | |
| 524 | public: |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | class iterator |
| 542 | : public llvm::iterator_adaptor_base< |
| 543 | iterator, int, std::random_access_iterator_tag, T, int, T *, T &> { |
| 544 | friend class LazyVector; |
| 545 | |
| 546 | LazyVector *Self; |
| 547 | |
| 548 | iterator(LazyVector *Self, int Position) |
| 549 | : iterator::iterator_adaptor_base(Position), Self(Self) {} |
| 550 | |
| 551 | bool isLoaded() const { return this->I < 0; } |
| 552 | |
| 553 | public: |
| 554 | iterator() : iterator(nullptr, 0) {} |
| 555 | |
| 556 | typename iterator::reference operator*() const { |
| 557 | if (isLoaded()) |
| 558 | return Self->Loaded.end()[this->I]; |
| 559 | return Self->Local.begin()[this->I]; |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | iterator begin(Source *source, bool LocalOnly = false) { |
| 564 | if (LocalOnly) |
| 565 | return iterator(this, 0); |
| 566 | |
| 567 | if (source) |
| 568 | (source->*Loader)(Loaded); |
| 569 | return iterator(this, -(int)Loaded.size()); |
| 570 | } |
| 571 | |
| 572 | iterator end() { |
| 573 | return iterator(this, Local.size()); |
| 574 | } |
| 575 | |
| 576 | void push_back(const T& LocalValue) { |
| 577 | Local.push_back(LocalValue); |
| 578 | } |
| 579 | |
| 580 | void erase(iterator From, iterator To) { |
| 581 | if (From.isLoaded() && To.isLoaded()) { |
| 582 | Loaded.erase(&*From, &*To); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | if (From.isLoaded()) { |
| 587 | Loaded.erase(&*From, Loaded.end()); |
| 588 | From = begin(nullptr, true); |
| 589 | } |
| 590 | |
| 591 | Local.erase(&*From, &*To); |
| 592 | } |
| 593 | }; |
| 594 | |
| 595 | |
| 596 | using LazyDeclStmtPtr = |
| 597 | LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>; |
| 598 | |
| 599 | |
| 600 | using LazyDeclPtr = |
| 601 | LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>; |
| 602 | |
| 603 | |
| 604 | using LazyCXXCtorInitializersPtr = |
| 605 | LazyOffsetPtr<CXXCtorInitializer *, uint64_t, |
| 606 | &ExternalASTSource::GetExternalCXXCtorInitializers>; |
| 607 | |
| 608 | |
| 609 | using LazyCXXBaseSpecifiersPtr = |
| 610 | LazyOffsetPtr<CXXBaseSpecifier, uint64_t, |
| 611 | &ExternalASTSource::GetExternalCXXBaseSpecifiers>; |
| 612 | |
| 613 | } |
| 614 | |
| 615 | #endif |
| 616 | |