clang  6.0.0svn
AttrIterator.h
Go to the documentation of this file.
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 = 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,
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.
43 
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>
48  typedef typename Container::const_iterator Iterator;
49 
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;
58 
59  void AdvanceToNext() const {
60  while (!isa<SpecificAttr>(*Current))
61  ++Current;
62  }
63 
64  void AdvanceToNext(Iterator I) const {
65  while (Current != I && !isa<SpecificAttr>(*Current))
66  ++Current;
67  }
68 
69 public:
70  typedef SpecificAttr* value_type;
71  typedef SpecificAttr* reference;
72  typedef SpecificAttr* pointer;
73  typedef std::forward_iterator_tag iterator_category;
75 
76  specific_attr_iterator() : Current() { }
77  explicit specific_attr_iterator(Iterator i) : Current(i) { }
78 
79  reference operator*() const {
80  AdvanceToNext();
81  return cast<SpecificAttr>(*Current);
82  }
83  pointer operator->() const {
84  AdvanceToNext();
85  return cast<SpecificAttr>(*Current);
86  }
87 
89  ++Current;
90  return *this;
91  }
93  specific_attr_iterator Tmp(*this);
94  ++(*this);
95  return Tmp;
96  }
97 
99  specific_attr_iterator Right) {
100  assert((Left.Current == nullptr) == (Right.Current == nullptr));
101  if (Left.Current < Right.Current)
102  Left.AdvanceToNext(Right.Current);
103  else
104  Right.AdvanceToNext(Left.Current);
105  return Left.Current == Right.Current;
106  }
108  specific_attr_iterator Right) {
109  return !(Left == Right);
110  }
111 };
112 
113 template <typename SpecificAttr, typename Container>
115  specific_attr_begin(const Container& container) {
116  return specific_attr_iterator<SpecificAttr, Container>(container.begin());
117 }
118 template <typename SpecificAttr, typename Container>
120  specific_attr_end(const Container& container) {
121  return specific_attr_iterator<SpecificAttr, Container>(container.end());
122 }
123 
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);
128 }
129 template <typename SpecificAttr, typename Container>
130 inline SpecificAttr *getSpecificAttr(const Container& container) {
132  specific_attr_begin<SpecificAttr>(container);
133  if (i != specific_attr_end<SpecificAttr>(container))
134  return *i;
135  else
136  return nullptr;
137 }
138 
139 } // end namespace clang
140 
141 #endif
specific_attr_iterator operator++(int)
Definition: AttrIterator.h:92
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c.h:60
specific_attr_iterator - Iterates over a subrange of an AttrVec, only providing attributes that are o...
Definition: AttrIterator.h:47
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
SmallVector< Attr *, 4 > AttrVec
AttrVec - A vector of Attr, which is how they are stored on the AST.
Definition: AttrIterator.h:42
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
std::ptrdiff_t difference_type
Definition: AttrIterator.h:74
std::forward_iterator_tag iterator_category
Definition: AttrIterator.h:73
specific_attr_iterator & operator++()
Definition: AttrIterator.h:88
SpecificAttr * getSpecificAttr(const Container &container)
Definition: AttrIterator.h:130
specific_attr_iterator< SpecificAttr, Container > specific_attr_begin(const Container &container)
Definition: AttrIterator.h:115
__PTRDIFF_TYPE__ ptrdiff_t
A signed integer type that is the result of subtracting two pointers.
Definition: opencl-c.h:68
Dataflow Directional Tag Classes.
bool hasSpecificAttr(const Container &container)
Definition: AttrIterator.h:125
reference operator*() const
Definition: AttrIterator.h:79
friend bool operator!=(specific_attr_iterator Left, specific_attr_iterator Right)
Definition: AttrIterator.h:107
friend bool operator==(specific_attr_iterator Left, specific_attr_iterator Right)
Definition: AttrIterator.h:98
specific_attr_iterator< SpecificAttr, Container > specific_attr_end(const Container &container)
Definition: AttrIterator.h:120