1 //===- AttrIterator.h - Classes for attribute iteration ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Attr vector and specific_attr_iterator interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ATTRITERATOR_H
15 #define LLVM_CLANG_AST_ATTRITERATOR_H
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Casting.h"
31 // Defined in ASTContext.h
32 void *operator new(size_t Bytes, const clang::ASTContext &C,
33 size_t Alignment = 8);
35 // FIXME: Being forced to not have a default argument here due to redeclaration
36 // rules on default arguments sucks
37 void *operator new[](size_t Bytes, const clang::ASTContext &C,
40 // It is good practice to pair new/delete operators. Also, MSVC gives many
41 // warnings if a matching delete overload is not declared, even though the
42 // throw() spec guarantees it will not be implicitly called.
43 void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
44 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
48 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
49 using AttrVec = SmallVector<Attr *, 4>;
51 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
52 /// providing attributes that are of a specific type.
53 template <typename SpecificAttr, typename Container = AttrVec>
54 class specific_attr_iterator {
55 using Iterator = typename Container::const_iterator;
57 /// Current - The current, underlying iterator.
58 /// In order to ensure we don't dereference an invalid iterator unless
59 /// specifically requested, we don't necessarily advance this all the
60 /// way. Instead, we advance it when an operation is requested; if the
61 /// operation is acting on what should be a past-the-end iterator,
62 /// then we offer no guarantees, but this way we do not dereference a
63 /// past-the-end iterator when we move to a past-the-end position.
64 mutable Iterator Current;
66 void AdvanceToNext() const {
67 while (!isa<SpecificAttr>(*Current))
71 void AdvanceToNext(Iterator I) const {
72 while (Current != I && !isa<SpecificAttr>(*Current))
77 using value_type = SpecificAttr *;
78 using reference = SpecificAttr *;
79 using pointer = SpecificAttr *;
80 using iterator_category = std::forward_iterator_tag;
81 using difference_type = std::ptrdiff_t;
83 specific_attr_iterator() = default;
84 explicit specific_attr_iterator(Iterator i) : Current(i) {}
86 reference operator*() const {
88 return cast<SpecificAttr>(*Current);
90 pointer operator->() const {
92 return cast<SpecificAttr>(*Current);
95 specific_attr_iterator& operator++() {
99 specific_attr_iterator operator++(int) {
100 specific_attr_iterator Tmp(*this);
105 friend bool operator==(specific_attr_iterator Left,
106 specific_attr_iterator Right) {
107 assert((Left.Current == nullptr) == (Right.Current == nullptr));
108 if (Left.Current < Right.Current)
109 Left.AdvanceToNext(Right.Current);
111 Right.AdvanceToNext(Left.Current);
112 return Left.Current == Right.Current;
114 friend bool operator!=(specific_attr_iterator Left,
115 specific_attr_iterator Right) {
116 return !(Left == Right);
120 template <typename SpecificAttr, typename Container>
121 inline specific_attr_iterator<SpecificAttr, Container>
122 specific_attr_begin(const Container& container) {
123 return specific_attr_iterator<SpecificAttr, Container>(container.begin());
125 template <typename SpecificAttr, typename Container>
126 inline specific_attr_iterator<SpecificAttr, Container>
127 specific_attr_end(const Container& container) {
128 return specific_attr_iterator<SpecificAttr, Container>(container.end());
131 template <typename SpecificAttr, typename Container>
132 inline bool hasSpecificAttr(const Container& container) {
133 return specific_attr_begin<SpecificAttr>(container) !=
134 specific_attr_end<SpecificAttr>(container);
136 template <typename SpecificAttr, typename Container>
137 inline SpecificAttr *getSpecificAttr(const Container& container) {
138 specific_attr_iterator<SpecificAttr, Container> i =
139 specific_attr_begin<SpecificAttr>(container);
140 if (i != specific_attr_end<SpecificAttr>(container))
148 #endif // LLVM_CLANG_AST_ATTRITERATOR_H