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 "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Casting.h"
20 #include <cassert>
21 #include <cstddef>
22 #include <iterator>
23 
24 namespace clang {
25 
26 class ASTContext;
27 class Attr;
28 
29 } // namespace clang
30 
31 // Defined in ASTContext.h
32 void *operator new(size_t Bytes, const clang::ASTContext &C,
33  size_t Alignment = 8);
34 
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,
38  size_t Alignment);
39 
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);
45 
46 namespace clang {
47 
48 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
50 
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>
55  using Iterator = typename Container::const_iterator;
56 
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;
65 
66  void AdvanceToNext() const {
67  while (!isa<SpecificAttr>(*Current))
68  ++Current;
69  }
70 
71  void AdvanceToNext(Iterator I) const {
72  while (Current != I && !isa<SpecificAttr>(*Current))
73  ++Current;
74  }
75 
76 public:
77  using value_type = SpecificAttr *;
78  using reference = SpecificAttr *;
79  using pointer = SpecificAttr *;
80  using iterator_category = std::forward_iterator_tag;
82 
83  specific_attr_iterator() = default;
84  explicit specific_attr_iterator(Iterator i) : Current(i) {}
85 
86  reference operator*() const {
87  AdvanceToNext();
88  return cast<SpecificAttr>(*Current);
89  }
90  pointer operator->() const {
91  AdvanceToNext();
92  return cast<SpecificAttr>(*Current);
93  }
94 
96  ++Current;
97  return *this;
98  }
100  specific_attr_iterator Tmp(*this);
101  ++(*this);
102  return Tmp;
103  }
104 
106  specific_attr_iterator Right) {
107  assert((Left.Current == nullptr) == (Right.Current == nullptr));
108  if (Left.Current < Right.Current)
109  Left.AdvanceToNext(Right.Current);
110  else
111  Right.AdvanceToNext(Left.Current);
112  return Left.Current == Right.Current;
113  }
115  specific_attr_iterator Right) {
116  return !(Left == Right);
117  }
118 };
119 
120 template <typename SpecificAttr, typename Container>
122  specific_attr_begin(const Container& container) {
123  return specific_attr_iterator<SpecificAttr, Container>(container.begin());
124 }
125 template <typename SpecificAttr, typename Container>
127  specific_attr_end(const Container& container) {
128  return specific_attr_iterator<SpecificAttr, Container>(container.end());
129 }
130 
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);
135 }
136 template <typename SpecificAttr, typename Container>
137 inline SpecificAttr *getSpecificAttr(const Container& container) {
139  specific_attr_begin<SpecificAttr>(container);
140  if (i != specific_attr_end<SpecificAttr>(container))
141  return *i;
142  else
143  return nullptr;
144 }
145 
146 } // namespace clang
147 
148 #endif // LLVM_CLANG_AST_ATTRITERATOR_H
specific_attr_iterator operator++(int)
Definition: AttrIterator.h:99
__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:54
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
specific_attr_iterator & operator++()
Definition: AttrIterator.h:95
std::forward_iterator_tag iterator_category
Definition: AttrIterator.h:80
SpecificAttr * getSpecificAttr(const Container &container)
Definition: AttrIterator.h:137
specific_attr_iterator< SpecificAttr, Container > specific_attr_begin(const Container &container)
Definition: AttrIterator.h:122
__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:132
reference operator*() const
Definition: AttrIterator.h:86
friend bool operator!=(specific_attr_iterator Left, specific_attr_iterator Right)
Definition: AttrIterator.h:114
std::ptrdiff_t difference_type
Definition: AttrIterator.h:81
friend bool operator==(specific_attr_iterator Left, specific_attr_iterator Right)
Definition: AttrIterator.h:105
specific_attr_iterator< SpecificAttr, Container > specific_attr_end(const Container &container)
Definition: AttrIterator.h:127