clang  6.0.0svn
StmtIterator.h
Go to the documentation of this file.
1 //===- StmtIterator.h - Iterators for Statements ----------------*- 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 StmtIterator and ConstStmtIterator classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_STMTITERATOR_H
15 #define LLVM_CLANG_AST_STMTITERATOR_H
16 
17 #include <cassert>
18 #include <cstddef>
19 #include <cstdint>
20 #include <iterator>
21 
22 namespace clang {
23 
24 class Decl;
25 class Stmt;
26 class VariableArrayType;
27 
29 protected:
30  enum {
31  StmtMode = 0x0,
34  Flags = 0x3
35  };
36 
37  union {
39  Decl **DGI;
40  };
42  Decl **DGE;
43 
44  StmtIteratorBase(Stmt **s) : stmt(s) {}
46  StmtIteratorBase(Decl **dgi, Decl **dge);
47  StmtIteratorBase() : stmt(nullptr) {}
48 
49  bool inDeclGroup() const {
50  return (RawVAPtr & Flags) == DeclGroupMode;
51  }
52 
53  bool inSizeOfTypeVA() const {
54  return (RawVAPtr & Flags) == SizeOfTypeVAMode;
55  }
56 
57  bool inStmt() const {
58  return (RawVAPtr & Flags) == StmtMode;
59  }
60 
61  const VariableArrayType *getVAPtr() const {
62  return reinterpret_cast<const VariableArrayType*>(RawVAPtr & ~Flags);
63  }
64 
65  void setVAPtr(const VariableArrayType *P) {
66  assert(inDeclGroup() || inSizeOfTypeVA());
67  RawVAPtr = reinterpret_cast<uintptr_t>(P) | (RawVAPtr & Flags);
68  }
69 
70  void NextDecl(bool ImmediateAdvance = true);
71  bool HandleDecl(Decl* D);
72  void NextVA();
73 
74  Stmt*& GetDeclExpr() const;
75 };
76 
77 template <typename DERIVED, typename REFERENCE>
79  public std::iterator<std::forward_iterator_tag,
80  REFERENCE, ptrdiff_t,
81  REFERENCE, REFERENCE> {
82 protected:
84 
85 public:
86  StmtIteratorImpl() = default;
88  StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
90 
91  DERIVED& operator++() {
92  if (inStmt())
93  ++stmt;
94  else if (getVAPtr())
95  NextVA();
96  else
97  NextDecl();
98 
99  return static_cast<DERIVED&>(*this);
100  }
101 
102  DERIVED operator++(int) {
103  DERIVED tmp = static_cast<DERIVED&>(*this);
104  operator++();
105  return tmp;
106  }
107 
108  bool operator==(const DERIVED& RHS) const {
109  return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr;
110  }
111 
112  bool operator!=(const DERIVED& RHS) const {
113  return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr;
114  }
115 
116  REFERENCE operator*() const {
117  return inStmt() ? *stmt : GetDeclExpr();
118  }
119 
120  REFERENCE operator->() const { return operator*(); }
121 };
122 
123 struct ConstStmtIterator;
124 
125 struct StmtIterator : public StmtIteratorImpl<StmtIterator, Stmt*&> {
126  explicit StmtIterator() = default;
128  StmtIterator(Decl** dgi, Decl** dge)
129  : StmtIteratorImpl<StmtIterator, Stmt*&>(dgi, dge) {}
132 
133 private:
134  StmtIterator(const StmtIteratorBase &RHS)
136 
137  inline friend StmtIterator
139 };
140 
141 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
142  const Stmt*> {
143  explicit ConstStmtIterator() = default;
145  : StmtIteratorImpl<ConstStmtIterator, const Stmt*>(RHS) {}
146 
149  const_cast<Stmt **>(S)) {}
150 };
151 
153  return RHS;
154 }
155 
156 } // namespace clang
157 
158 #endif // LLVM_CLANG_AST_STMTITERATOR_H
ConstStmtIterator(const StmtIterator &RHS)
Definition: StmtIterator.h:144
Stmt - This represents one statement.
Definition: Stmt.h:66
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
StringRef P
bool inDeclGroup() const
Definition: StmtIterator.h:49
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const VariableArrayType * getVAPtr() const
Definition: StmtIterator.h:61
StmtIteratorImpl(const VariableArrayType *t)
Definition: StmtIterator.h:89
StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:152
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)
Definition: CharUnits.h:208
REFERENCE operator->() const
Definition: StmtIterator.h:120
void NextDecl(bool ImmediateAdvance=true)
StmtIteratorImpl(const StmtIteratorBase &RHS)
Definition: StmtIterator.h:83
bool operator==(const DERIVED &RHS) const
Definition: StmtIterator.h:108
ConstStmtIterator(Stmt *const *S)
Definition: StmtIterator.h:147
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:82
StmtIterator(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:128
bool inSizeOfTypeVA() const
Definition: StmtIterator.h:53
bool operator!=(const DERIVED &RHS) const
Definition: StmtIterator.h:112
Stmt *& GetDeclExpr() const
Dataflow Directional Tag Classes.
bool HandleDecl(Decl *D)
StmtIterator(const VariableArrayType *t)
Definition: StmtIterator.h:130
StmtIteratorImpl(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:88
REFERENCE operator*() const
Definition: StmtIterator.h:116
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:65
StmtIterator(Stmt **S)
Definition: StmtIterator.h:127
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:2719