1//===- AttrIterator.h - Classes for attribute iteration ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Attr vector and specific_attr_iterator interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_ATTRITERATOR_H
14#define LLVM_CLANG_AST_ATTRITERATOR_H
15
16#include "clang/Basic/LLVM.h"
17#include "llvm/ADT/ADL.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/iterator_range.h"
20#include "llvm/Support/Casting.h"
21#include <cassert>
22#include <cstddef>
23#include <iterator>
24#include <type_traits>
25
26namespace clang {
27
28class Attr;
29
30/// AttrVec - A vector of Attr, which is how they are stored on the AST.
31using AttrVec = SmallVector<Attr *, 4>;
32
33/// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
34/// providing attributes that are of a specific type.
35template <typename SpecificAttr, typename Container = AttrVec>
36class specific_attr_iterator {
37 using Iterator = typename Container::const_iterator;
38
39 /// Current - The current, underlying iterator.
40 /// In order to ensure we don't dereference an invalid iterator unless
41 /// specifically requested, we don't necessarily advance this all the
42 /// way. Instead, we advance it when an operation is requested; if the
43 /// operation is acting on what should be a past-the-end iterator,
44 /// then we offer no guarantees, but this way we do not dereference a
45 /// past-the-end iterator when we move to a past-the-end position.
46 mutable Iterator Current;
47
48 void AdvanceToNext() const {
49 while (!isa<SpecificAttr>(*Current))
50 ++Current;
51 }
52
53 void AdvanceToNext(Iterator I) const {
54 while (Current != I && !isa<SpecificAttr>(*Current))
55 ++Current;
56 }
57
58public:
59 using value_type = SpecificAttr *;
60 using reference = SpecificAttr *;
61 using pointer = SpecificAttr *;
62 using iterator_category = std::forward_iterator_tag;
63 using difference_type = std::ptrdiff_t;
64
65 specific_attr_iterator() = default;
66 explicit specific_attr_iterator(Iterator i) : Current(i) {}
67
68 reference operator*() const {
69 AdvanceToNext();
70 return cast<SpecificAttr>(*Current);
71 }
72 pointer operator->() const {
73 AdvanceToNext();
74 return cast<SpecificAttr>(*Current);
75 }
76
77 specific_attr_iterator& operator++() {
78 ++Current;
79 return *this;
80 }
81 specific_attr_iterator operator++(int) {
82 specific_attr_iterator Tmp(*this);
83 ++(*this);
84 return Tmp;
85 }
86
87 friend bool operator==(specific_attr_iterator Left,
88 specific_attr_iterator Right) {
89 assert((Left.Current == nullptr) == (Right.Current == nullptr));
90 if (Left.Current < Right.Current)
91 Left.AdvanceToNext(Right.Current);
92 else
93 Right.AdvanceToNext(Left.Current);
94 return Left.Current == Right.Current;
95 }
96 friend bool operator!=(specific_attr_iterator Left,
97 specific_attr_iterator Right) {
98 return !(Left == Right);
99 }
100};
101
102template <typename SpecificAttr, typename Container>
103inline specific_attr_iterator<SpecificAttr, Container>
104 specific_attr_begin(const Container& container) {
105 return specific_attr_iterator<SpecificAttr, Container>(container.begin());
106}
107template <typename SpecificAttr, typename Container>
108inline specific_attr_iterator<SpecificAttr, Container>
109 specific_attr_end(const Container& container) {
110 return specific_attr_iterator<SpecificAttr, Container>(container.end());
111}
112
113template <typename SpecificAttr, typename Container>
114inline bool hasSpecificAttr(const Container& container) {
115 return specific_attr_begin<SpecificAttr>(container) !=
116 specific_attr_end<SpecificAttr>(container);
117}
118template <typename SpecificAttr, typename Container>
119inline auto *getSpecificAttr(const Container &container) {
120 using ValueTy = llvm::detail::ValueOfRange<Container>;
121 using ValuePointeeTy = std::remove_pointer_t<ValueTy>;
122 using IterTy = std::conditional_t<std::is_const_v<ValuePointeeTy>,
123 const SpecificAttr, SpecificAttr>;
124 auto It = specific_attr_begin<IterTy>(container);
125 return It != specific_attr_end<IterTy>(container) ? *It : nullptr;
126}
127
128template <typename SpecificAttr, typename Container>
129inline auto getSpecificAttrs(const Container &container) {
130 using ValueTy = llvm::detail::ValueOfRange<Container>;
131 using ValuePointeeTy = std::remove_pointer_t<ValueTy>;
132 using IterTy = std::conditional_t<std::is_const_v<ValuePointeeTy>,
133 const SpecificAttr, SpecificAttr>;
134 auto Begin = specific_attr_begin<IterTy>(container);
135 auto End = specific_attr_end<IterTy>(container);
136 return llvm::make_range(Begin, End);
137}
138
139} // namespace clang
140
141#endif // LLVM_CLANG_AST_ATTRITERATOR_H
142

source code of clang/include/clang/AST/AttrIterator.h