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"
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,
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);
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;
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;
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;
60 void AdvanceToNext() const {
61 while (!isa<SpecificAttr>(*Current))
65 void AdvanceToNext(Iterator I) const {
66 while (Current != I && !isa<SpecificAttr>(*Current))
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;
77 specific_attr_iterator() : Current() { }
78 explicit specific_attr_iterator(Iterator i) : Current(i) { }
80 reference operator*() const {
82 return cast<SpecificAttr>(*Current);
84 pointer operator->() const {
86 return cast<SpecificAttr>(*Current);
89 specific_attr_iterator& operator++() {
93 specific_attr_iterator operator++(int) {
94 specific_attr_iterator Tmp(*this);
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);
105 Right.AdvanceToNext(Left.Current);
106 return Left.Current == Right.Current;
108 friend bool operator!=(specific_attr_iterator Left,
109 specific_attr_iterator Right) {
110 return !(Left == Right);
114 template <typename SpecificAttr, typename Container>
115 inline specific_attr_iterator<SpecificAttr, Container>
116 specific_attr_begin(const Container& container) {
117 return specific_attr_iterator<SpecificAttr, Container>(container.begin());
119 template <typename SpecificAttr, typename Container>
120 inline specific_attr_iterator<SpecificAttr, Container>
121 specific_attr_end(const Container& container) {
122 return specific_attr_iterator<SpecificAttr, Container>(container.end());
125 template <typename SpecificAttr, typename Container>
126 inline bool hasSpecificAttr(const Container& container) {
127 return specific_attr_begin<SpecificAttr>(container) !=
128 specific_attr_end<SpecificAttr>(container);
130 template <typename SpecificAttr, typename 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))
140 } // end namespace clang