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 = 8);
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 *, 4> AttrVec;
44 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
45 /// providing attributes that are of a specific type.
46 template <typename SpecificAttr, typename Container = AttrVec>
47 class specific_attr_iterator {
48 typedef typename Container::const_iterator Iterator;
50 /// Current - The current, underlying iterator.
51 /// In order to ensure we don't dereference an invalid iterator unless
52 /// specifically requested, we don't necessarily advance this all the
53 /// way. Instead, we advance it when an operation is requested; if the
54 /// operation is acting on what should be a past-the-end iterator,
55 /// then we offer no guarantees, but this way we do not dereference a
56 /// past-the-end iterator when we move to a past-the-end position.
57 mutable Iterator Current;
59 void AdvanceToNext() const {
60 while (!isa<SpecificAttr>(*Current))
64 void AdvanceToNext(Iterator I) const {
65 while (Current != I && !isa<SpecificAttr>(*Current))
70 typedef SpecificAttr* value_type;
71 typedef SpecificAttr* reference;
72 typedef SpecificAttr* pointer;
73 typedef std::forward_iterator_tag iterator_category;
74 typedef std::ptrdiff_t difference_type;
76 specific_attr_iterator() : Current() { }
77 explicit specific_attr_iterator(Iterator i) : Current(i) { }
79 reference operator*() const {
81 return cast<SpecificAttr>(*Current);
83 pointer operator->() const {
85 return cast<SpecificAttr>(*Current);
88 specific_attr_iterator& operator++() {
92 specific_attr_iterator operator++(int) {
93 specific_attr_iterator Tmp(*this);
98 friend bool operator==(specific_attr_iterator Left,
99 specific_attr_iterator Right) {
100 assert((Left.Current == nullptr) == (Right.Current == nullptr));
101 if (Left.Current < Right.Current)
102 Left.AdvanceToNext(Right.Current);
104 Right.AdvanceToNext(Left.Current);
105 return Left.Current == Right.Current;
107 friend bool operator!=(specific_attr_iterator Left,
108 specific_attr_iterator Right) {
109 return !(Left == Right);
113 template <typename SpecificAttr, typename Container>
114 inline specific_attr_iterator<SpecificAttr, Container>
115 specific_attr_begin(const Container& container) {
116 return specific_attr_iterator<SpecificAttr, Container>(container.begin());
118 template <typename SpecificAttr, typename Container>
119 inline specific_attr_iterator<SpecificAttr, Container>
120 specific_attr_end(const Container& container) {
121 return specific_attr_iterator<SpecificAttr, Container>(container.end());
124 template <typename SpecificAttr, typename Container>
125 inline bool hasSpecificAttr(const Container& container) {
126 return specific_attr_begin<SpecificAttr>(container) !=
127 specific_attr_end<SpecificAttr>(container);
129 template <typename SpecificAttr, typename Container>
130 inline SpecificAttr *getSpecificAttr(const Container& container) {
131 specific_attr_iterator<SpecificAttr, Container> i =
132 specific_attr_begin<SpecificAttr>(container);
133 if (i != specific_attr_end<SpecificAttr>(container))
139 } // end namespace clang