ASTImporterSharedState.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTImporterSharedState.h - ASTImporter specific state --*- C++ -*---===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the ASTImporter specific state, which may be shared
  15. // amongst several ASTImporter objects.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
  19. #define LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
  20. #include "clang/AST/ASTImportError.h"
  21. #include "clang/AST/ASTImporterLookupTable.h"
  22. #include "clang/AST/Decl.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include <optional>
  25. namespace clang {
  26. class TranslationUnitDecl;
  27. /// Importer specific state, which may be shared amongst several ASTImporter
  28. /// objects.
  29. class ASTImporterSharedState {
  30. /// Pointer to the import specific lookup table.
  31. std::unique_ptr<ASTImporterLookupTable> LookupTable;
  32. /// Mapping from the already-imported declarations in the "to"
  33. /// context to the error status of the import of that declaration.
  34. /// This map contains only the declarations that were not correctly
  35. /// imported. The same declaration may or may not be included in
  36. /// ImportedFromDecls. This map is updated continuously during imports and
  37. /// never cleared (like ImportedFromDecls).
  38. llvm::DenseMap<Decl *, ASTImportError> ImportErrors;
  39. /// Set of the newly created declarations.
  40. llvm::DenseSet<Decl *> NewDecls;
  41. // FIXME put ImportedFromDecls here!
  42. // And from that point we can better encapsulate the lookup table.
  43. public:
  44. ASTImporterSharedState() = default;
  45. ASTImporterSharedState(TranslationUnitDecl &ToTU) {
  46. LookupTable = std::make_unique<ASTImporterLookupTable>(ToTU);
  47. }
  48. ASTImporterLookupTable *getLookupTable() { return LookupTable.get(); }
  49. void addDeclToLookup(Decl *D) {
  50. if (LookupTable)
  51. if (auto *ND = dyn_cast<NamedDecl>(D))
  52. LookupTable->add(ND);
  53. }
  54. void removeDeclFromLookup(Decl *D) {
  55. if (LookupTable)
  56. if (auto *ND = dyn_cast<NamedDecl>(D))
  57. LookupTable->remove(ND);
  58. }
  59. std::optional<ASTImportError> getImportDeclErrorIfAny(Decl *ToD) const {
  60. auto Pos = ImportErrors.find(ToD);
  61. if (Pos != ImportErrors.end())
  62. return Pos->second;
  63. else
  64. return std::nullopt;
  65. }
  66. void setImportDeclError(Decl *To, ASTImportError Error) {
  67. ImportErrors[To] = Error;
  68. }
  69. bool isNewDecl(const Decl *ToD) const { return NewDecls.count(ToD); }
  70. void markAsNewDecl(Decl *ToD) { NewDecls.insert(ToD); }
  71. };
  72. } // namespace clang
  73. #endif // LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
  74. #ifdef __GNUC__
  75. #pragma GCC diagnostic pop
  76. #endif