]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/AttrIterator.h
MFV r329753: 8809 libzpool should leverage work done in libfakekernel
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / AttrIterator.h
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.
49 using AttrVec = SmallVector<Attr *, 4>;
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>
54 class specific_attr_iterator {
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;
81   using difference_type = std::ptrdiff_t;
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
95   specific_attr_iterator& operator++() {
96     ++Current;
97     return *this;
98   }
99   specific_attr_iterator operator++(int) {
100     specific_attr_iterator Tmp(*this);
101     ++(*this);
102     return Tmp;
103   }
104
105   friend bool operator==(specific_attr_iterator Left,
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   }
114   friend bool operator!=(specific_attr_iterator Left,
115                          specific_attr_iterator Right) {
116     return !(Left == Right);
117   }
118 };
119
120 template <typename SpecificAttr, typename Container>
121 inline specific_attr_iterator<SpecificAttr, Container>
122           specific_attr_begin(const Container& container) {
123   return specific_attr_iterator<SpecificAttr, Container>(container.begin());
124 }
125 template <typename SpecificAttr, typename Container>
126 inline specific_attr_iterator<SpecificAttr, 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) {
138   specific_attr_iterator<SpecificAttr, Container> i =
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