ASTUnresolvedSet.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTUnresolvedSet.h - Unresolved sets of declarations -----*- 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 provides an UnresolvedSet-like class, whose contents are
  15. // allocated using the allocator associated with an ASTContext.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
  19. #define LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
  20. #include "clang/AST/ASTVector.h"
  21. #include "clang/AST/DeclAccessPair.h"
  22. #include "clang/AST/UnresolvedSet.h"
  23. #include "clang/Basic/Specifiers.h"
  24. #include <cassert>
  25. #include <cstdint>
  26. namespace clang {
  27. class NamedDecl;
  28. /// An UnresolvedSet-like class which uses the ASTContext's allocator.
  29. class ASTUnresolvedSet {
  30. friend class LazyASTUnresolvedSet;
  31. struct DeclsTy : ASTVector<DeclAccessPair> {
  32. DeclsTy() = default;
  33. DeclsTy(ASTContext &C, unsigned N) : ASTVector<DeclAccessPair>(C, N) {}
  34. bool isLazy() const { return getTag(); }
  35. void setLazy(bool Lazy) { setTag(Lazy); }
  36. };
  37. DeclsTy Decls;
  38. public:
  39. ASTUnresolvedSet() = default;
  40. ASTUnresolvedSet(ASTContext &C, unsigned N) : Decls(C, N) {}
  41. using iterator = UnresolvedSetIterator;
  42. using const_iterator = UnresolvedSetIterator;
  43. iterator begin() { return iterator(Decls.begin()); }
  44. iterator end() { return iterator(Decls.end()); }
  45. const_iterator begin() const { return const_iterator(Decls.begin()); }
  46. const_iterator end() const { return const_iterator(Decls.end()); }
  47. void addDecl(ASTContext &C, NamedDecl *D, AccessSpecifier AS) {
  48. Decls.push_back(DeclAccessPair::make(D, AS), C);
  49. }
  50. /// Replaces the given declaration with the new one, once.
  51. ///
  52. /// \return true if the set changed
  53. bool replace(const NamedDecl *Old, NamedDecl *New, AccessSpecifier AS) {
  54. for (DeclsTy::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
  55. if (I->getDecl() == Old) {
  56. I->set(New, AS);
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. void erase(unsigned I) {
  63. if (I == Decls.size() - 1)
  64. Decls.pop_back();
  65. else
  66. Decls[I] = Decls.pop_back_val();
  67. }
  68. void clear() { Decls.clear(); }
  69. bool empty() const { return Decls.empty(); }
  70. unsigned size() const { return Decls.size(); }
  71. void reserve(ASTContext &C, unsigned N) {
  72. Decls.reserve(C, N);
  73. }
  74. void append(ASTContext &C, iterator I, iterator E) {
  75. Decls.append(C, I.I, E.I);
  76. }
  77. DeclAccessPair &operator[](unsigned I) { return Decls[I]; }
  78. const DeclAccessPair &operator[](unsigned I) const { return Decls[I]; }
  79. };
  80. /// An UnresolvedSet-like class that might not have been loaded from the
  81. /// external AST source yet.
  82. class LazyASTUnresolvedSet {
  83. mutable ASTUnresolvedSet Impl;
  84. void getFromExternalSource(ASTContext &C) const;
  85. public:
  86. ASTUnresolvedSet &get(ASTContext &C) const {
  87. if (Impl.Decls.isLazy())
  88. getFromExternalSource(C);
  89. return Impl;
  90. }
  91. void reserve(ASTContext &C, unsigned N) { Impl.reserve(C, N); }
  92. void addLazyDecl(ASTContext &C, uintptr_t ID, AccessSpecifier AS) {
  93. assert(Impl.empty() || Impl.Decls.isLazy());
  94. Impl.Decls.setLazy(true);
  95. Impl.addDecl(C, reinterpret_cast<NamedDecl *>(ID << 2), AS);
  96. }
  97. };
  98. } // namespace clang
  99. #endif // LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
  100. #ifdef __GNUC__
  101. #pragma GCC diagnostic pop
  102. #endif