DeclLookups.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeclLookups.h - Low-level interface to all names in a DC -*- 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 DeclContext::all_lookups_iterator.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_DECLLOOKUPS_H
  18. #define LLVM_CLANG_AST_DECLLOOKUPS_H
  19. #include "clang/AST/ASTContext.h"
  20. #include "clang/AST/DeclBase.h"
  21. #include "clang/AST/DeclContextInternals.h"
  22. #include "clang/AST/DeclarationName.h"
  23. #include "clang/AST/ExternalASTSource.h"
  24. #include <cstddef>
  25. #include <iterator>
  26. namespace clang {
  27. /// all_lookups_iterator - An iterator that provides a view over the results
  28. /// of looking up every possible name.
  29. class DeclContext::all_lookups_iterator {
  30. StoredDeclsMap::iterator It, End;
  31. public:
  32. using value_type = lookup_result;
  33. using reference = lookup_result;
  34. using pointer = lookup_result;
  35. using iterator_category = std::forward_iterator_tag;
  36. using difference_type = std::ptrdiff_t;
  37. all_lookups_iterator() = default;
  38. all_lookups_iterator(StoredDeclsMap::iterator It,
  39. StoredDeclsMap::iterator End)
  40. : It(It), End(End) {}
  41. DeclarationName getLookupName() const { return It->first; }
  42. reference operator*() const { return It->second.getLookupResult(); }
  43. pointer operator->() const { return It->second.getLookupResult(); }
  44. all_lookups_iterator& operator++() {
  45. // Filter out using directives. They don't belong as results from name
  46. // lookup anyways, except as an implementation detail. Users of the API
  47. // should not expect to get them (or worse, rely on it).
  48. do {
  49. ++It;
  50. } while (It != End &&
  51. It->first == DeclarationName::getUsingDirectiveName());
  52. return *this;
  53. }
  54. all_lookups_iterator operator++(int) {
  55. all_lookups_iterator tmp(*this);
  56. ++(*this);
  57. return tmp;
  58. }
  59. friend bool operator==(all_lookups_iterator x, all_lookups_iterator y) {
  60. return x.It == y.It;
  61. }
  62. friend bool operator!=(all_lookups_iterator x, all_lookups_iterator y) {
  63. return x.It != y.It;
  64. }
  65. };
  66. inline DeclContext::lookups_range DeclContext::lookups() const {
  67. DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
  68. if (Primary->hasExternalVisibleStorage())
  69. getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
  70. if (StoredDeclsMap *Map = Primary->buildLookup())
  71. return lookups_range(all_lookups_iterator(Map->begin(), Map->end()),
  72. all_lookups_iterator(Map->end(), Map->end()));
  73. // Synthesize an empty range. This requires that two default constructed
  74. // versions of these iterators form a valid empty range.
  75. return lookups_range(all_lookups_iterator(), all_lookups_iterator());
  76. }
  77. inline DeclContext::lookups_range
  78. DeclContext::noload_lookups(bool PreserveInternalState) const {
  79. DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
  80. if (!PreserveInternalState)
  81. Primary->loadLazyLocalLexicalLookups();
  82. if (StoredDeclsMap *Map = Primary->getLookupPtr())
  83. return lookups_range(all_lookups_iterator(Map->begin(), Map->end()),
  84. all_lookups_iterator(Map->end(), Map->end()));
  85. // Synthesize an empty range. This requires that two default constructed
  86. // versions of these iterators form a valid empty range.
  87. return lookups_range(all_lookups_iterator(), all_lookups_iterator());
  88. }
  89. } // namespace clang
  90. #endif // LLVM_CLANG_AST_DECLLOOKUPS_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif