ASTImporterLookupTable.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTImporterLookupTable.h - ASTImporter specific lookup--*- 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 ASTImporterLookupTable class which implements a
  15. // lookup procedure for the import mechanism.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
  19. #define LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
  20. #include "clang/AST/DeclBase.h" // lookup_result
  21. #include "clang/AST/DeclarationName.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/SetVector.h"
  24. namespace clang {
  25. class NamedDecl;
  26. class DeclContext;
  27. // There are certain cases when normal C/C++ lookup (localUncachedLookup)
  28. // does not find AST nodes. E.g.:
  29. // Example 1:
  30. // template <class T>
  31. // struct X {
  32. // friend void foo(); // this is never found in the DC of the TU.
  33. // };
  34. // Example 2:
  35. // // The fwd decl to Foo is not found in the lookupPtr of the DC of the
  36. // // translation unit decl.
  37. // // Here we could find the node by doing a traverse throught the list of
  38. // // the Decls in the DC, but that would not scale.
  39. // struct A { struct Foo *p; };
  40. // This is a severe problem because the importer decides if it has to create a
  41. // new Decl or not based on the lookup results.
  42. // To overcome these cases we need an importer specific lookup table which
  43. // holds every node and we are not interested in any C/C++ specific visibility
  44. // considerations. Simply, we must know if there is an existing Decl in a
  45. // given DC. Once we found it then we can handle any visibility related tasks.
  46. class ASTImporterLookupTable {
  47. // We store a list of declarations for each name.
  48. // And we collect these lists for each DeclContext.
  49. // We could have a flat map with (DeclContext, Name) tuple as key, but a two
  50. // level map seems easier to handle.
  51. using DeclList = llvm::SmallSetVector<NamedDecl *, 2>;
  52. using NameMap = llvm::SmallDenseMap<DeclarationName, DeclList, 4>;
  53. using DCMap = llvm::DenseMap<DeclContext *, NameMap>;
  54. void add(DeclContext *DC, NamedDecl *ND);
  55. void remove(DeclContext *DC, NamedDecl *ND);
  56. DCMap LookupTable;
  57. public:
  58. ASTImporterLookupTable(TranslationUnitDecl &TU);
  59. void add(NamedDecl *ND);
  60. void remove(NamedDecl *ND);
  61. // Sometimes a declaration is created first with a temporarily value of decl
  62. // context (often the translation unit) and later moved to the final context.
  63. // This happens for declarations that are created before the final declaration
  64. // context. In such cases the lookup table needs to be updated.
  65. // (The declaration is in these cases not added to the temporary decl context,
  66. // only its parent is set.)
  67. // FIXME: It would be better to not add the declaration to the temporary
  68. // context at all in the lookup table, but this requires big change in
  69. // ASTImporter.
  70. // The function should be called when the old context is definitely different
  71. // from the new.
  72. void update(NamedDecl *ND, DeclContext *OldDC);
  73. // Same as 'update' but allow if 'ND' is not in the table or the old context
  74. // is the same as the new.
  75. // FIXME: The old redeclaration context is not handled.
  76. void updateForced(NamedDecl *ND, DeclContext *OldDC);
  77. using LookupResult = DeclList;
  78. LookupResult lookup(DeclContext *DC, DeclarationName Name) const;
  79. // Check if the `ND` is within the lookup table (with its current name) in
  80. // context `DC`. This is intended for debug purposes when the DeclContext of a
  81. // NamedDecl is changed.
  82. bool contains(DeclContext *DC, NamedDecl *ND) const;
  83. void dump(DeclContext *DC) const;
  84. void dump() const;
  85. };
  86. } // namespace clang
  87. #endif // LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
  88. #ifdef __GNUC__
  89. #pragma GCC diagnostic pop
  90. #endif