AttrIterator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- AttrIterator.h - Classes for attribute iteration ---------*- 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 Attr vector and specific_attr_iterator interfaces.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_ATTRITERATOR_H
  18. #define LLVM_CLANG_AST_ATTRITERATOR_H
  19. #include "clang/Basic/LLVM.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Support/Casting.h"
  22. #include <cassert>
  23. #include <cstddef>
  24. #include <iterator>
  25. namespace clang {
  26. class Attr;
  27. /// AttrVec - A vector of Attr, which is how they are stored on the AST.
  28. using AttrVec = SmallVector<Attr *, 4>;
  29. /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
  30. /// providing attributes that are of a specific type.
  31. template <typename SpecificAttr, typename Container = AttrVec>
  32. class specific_attr_iterator {
  33. using Iterator = typename Container::const_iterator;
  34. /// Current - The current, underlying iterator.
  35. /// In order to ensure we don't dereference an invalid iterator unless
  36. /// specifically requested, we don't necessarily advance this all the
  37. /// way. Instead, we advance it when an operation is requested; if the
  38. /// operation is acting on what should be a past-the-end iterator,
  39. /// then we offer no guarantees, but this way we do not dereference a
  40. /// past-the-end iterator when we move to a past-the-end position.
  41. mutable Iterator Current;
  42. void AdvanceToNext() const {
  43. while (!isa<SpecificAttr>(*Current))
  44. ++Current;
  45. }
  46. void AdvanceToNext(Iterator I) const {
  47. while (Current != I && !isa<SpecificAttr>(*Current))
  48. ++Current;
  49. }
  50. public:
  51. using value_type = SpecificAttr *;
  52. using reference = SpecificAttr *;
  53. using pointer = SpecificAttr *;
  54. using iterator_category = std::forward_iterator_tag;
  55. using difference_type = std::ptrdiff_t;
  56. specific_attr_iterator() = default;
  57. explicit specific_attr_iterator(Iterator i) : Current(i) {}
  58. reference operator*() const {
  59. AdvanceToNext();
  60. return cast<SpecificAttr>(*Current);
  61. }
  62. pointer operator->() const {
  63. AdvanceToNext();
  64. return cast<SpecificAttr>(*Current);
  65. }
  66. specific_attr_iterator& operator++() {
  67. ++Current;
  68. return *this;
  69. }
  70. specific_attr_iterator operator++(int) {
  71. specific_attr_iterator Tmp(*this);
  72. ++(*this);
  73. return Tmp;
  74. }
  75. friend bool operator==(specific_attr_iterator Left,
  76. specific_attr_iterator Right) {
  77. assert((Left.Current == nullptr) == (Right.Current == nullptr));
  78. if (Left.Current < Right.Current)
  79. Left.AdvanceToNext(Right.Current);
  80. else
  81. Right.AdvanceToNext(Left.Current);
  82. return Left.Current == Right.Current;
  83. }
  84. friend bool operator!=(specific_attr_iterator Left,
  85. specific_attr_iterator Right) {
  86. return !(Left == Right);
  87. }
  88. };
  89. template <typename SpecificAttr, typename Container>
  90. inline specific_attr_iterator<SpecificAttr, Container>
  91. specific_attr_begin(const Container& container) {
  92. return specific_attr_iterator<SpecificAttr, Container>(container.begin());
  93. }
  94. template <typename SpecificAttr, typename Container>
  95. inline specific_attr_iterator<SpecificAttr, Container>
  96. specific_attr_end(const Container& container) {
  97. return specific_attr_iterator<SpecificAttr, Container>(container.end());
  98. }
  99. template <typename SpecificAttr, typename Container>
  100. inline bool hasSpecificAttr(const Container& container) {
  101. return specific_attr_begin<SpecificAttr>(container) !=
  102. specific_attr_end<SpecificAttr>(container);
  103. }
  104. template <typename SpecificAttr, typename Container>
  105. inline SpecificAttr *getSpecificAttr(const Container& container) {
  106. specific_attr_iterator<SpecificAttr, Container> i =
  107. specific_attr_begin<SpecificAttr>(container);
  108. if (i != specific_attr_end<SpecificAttr>(container))
  109. return *i;
  110. else
  111. return nullptr;
  112. }
  113. } // namespace clang
  114. #endif // LLVM_CLANG_AST_ATTRITERATOR_H
  115. #ifdef __GNUC__
  116. #pragma GCC diagnostic pop
  117. #endif