1 //===--- AttrIterator.h - Classes for attribute iteration -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the Attr vector and specific_attr_iterator interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_ATTRITERATOR_H
15 #define LLVM_CLANG_AST_ATTRITERATOR_H
16
17 #include "clang/Basic/LLVM.h"
18 #include <iterator>
19
20 namespace clang {
21 class ASTContext;
22 class Attr;
23 }
24
25 // Defined in ASTContext.h
26 void *operator new(size_t Bytes, const clang::ASTContext &C,
27 size_t Alignment = 16);
28 // FIXME: Being forced to not have a default argument here due to redeclaration
29 // rules on default arguments sucks
30 void *operator new[](size_t Bytes, const clang::ASTContext &C,
31 size_t Alignment);
32
33 // It is good practice to pair new/delete operators. Also, MSVC gives many
34 // warnings if a matching delete overload is not declared, even though the
35 // throw() spec guarantees it will not be implicitly called.
36 void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
37 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
38
39 namespace clang {
40
41 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
42 typedef SmallVector<Attr*, 2> AttrVec;
43 typedef SmallVector<const Attr*, 2> ConstAttrVec;
44
45 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
46 /// providing attributes that are of a specifc type.
47 template <typename SpecificAttr, typename Container = AttrVec>
48 class specific_attr_iterator {
49 typedef typename Container::const_iterator Iterator;
50
51 /// Current - The current, underlying iterator.
52 /// In order to ensure we don't dereference an invalid iterator unless
53 /// specifically requested, we don't necessarily advance this all the
54 /// way. Instead, we advance it when an operation is requested; if the
55 /// operation is acting on what should be a past-the-end iterator,
56 /// then we offer no guarantees, but this way we do not dererence a
57 /// past-the-end iterator when we move to a past-the-end position.
58 mutable Iterator Current;
59
AdvanceToNext()60 void AdvanceToNext() const {
61 while (!isa<SpecificAttr>(*Current))
62 ++Current;
63 }
64
AdvanceToNext(Iterator I)65 void AdvanceToNext(Iterator I) const {
66 while (Current != I && !isa<SpecificAttr>(*Current))
67 ++Current;
68 }
69
70 public:
71 typedef SpecificAttr* value_type;
72 typedef SpecificAttr* reference;
73 typedef SpecificAttr* pointer;
74 typedef std::forward_iterator_tag iterator_category;
75 typedef std::ptrdiff_t difference_type;
76
specific_attr_iterator()77 specific_attr_iterator() : Current() { }
specific_attr_iterator(Iterator i)78 explicit specific_attr_iterator(Iterator i) : Current(i) { }
79
80 reference operator*() const {
81 AdvanceToNext();
82 return cast<SpecificAttr>(*Current);
83 }
84 pointer operator->() const {
85 AdvanceToNext();
86 return cast<SpecificAttr>(*Current);
87 }
88
89 specific_attr_iterator& operator++() {
90 ++Current;
91 return *this;
92 }
93 specific_attr_iterator operator++(int) {
94 specific_attr_iterator Tmp(*this);
95 ++(*this);
96 return Tmp;
97 }
98
99 friend bool operator==(specific_attr_iterator Left,
100 specific_attr_iterator Right) {
101 assert((Left.Current == 0) == (Right.Current == 0));
102 if (Left.Current < Right.Current)
103 Left.AdvanceToNext(Right.Current);
104 else
105 Right.AdvanceToNext(Left.Current);
106 return Left.Current == Right.Current;
107 }
108 friend bool operator!=(specific_attr_iterator Left,
109 specific_attr_iterator Right) {
110 return !(Left == Right);
111 }
112 };
113
114 template <typename SpecificAttr, typename Container>
115 inline specific_attr_iterator<SpecificAttr, Container>
specific_attr_begin(const Container & container)116 specific_attr_begin(const Container& container) {
117 return specific_attr_iterator<SpecificAttr, Container>(container.begin());
118 }
119 template <typename SpecificAttr, typename Container>
120 inline specific_attr_iterator<SpecificAttr, Container>
specific_attr_end(const Container & container)121 specific_attr_end(const Container& container) {
122 return specific_attr_iterator<SpecificAttr, Container>(container.end());
123 }
124
125 template <typename SpecificAttr, typename Container>
hasSpecificAttr(const Container & container)126 inline bool hasSpecificAttr(const Container& container) {
127 return specific_attr_begin<SpecificAttr>(container) !=
128 specific_attr_end<SpecificAttr>(container);
129 }
130 template <typename SpecificAttr, typename Container>
getSpecificAttr(const Container & container)131 inline SpecificAttr *getSpecificAttr(const Container& container) {
132 specific_attr_iterator<SpecificAttr, Container> i =
133 specific_attr_begin<SpecificAttr>(container);
134 if (i != specific_attr_end<SpecificAttr>(container))
135 return *i;
136 else
137 return 0;
138 }
139
140 } // end namespace clang
141
142 #endif
143