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 "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataTypes.h"
19 #include <cassert>
20 #include <cstddef>
21 #include <iterator>
22 #include <utility>
23 
24 namespace clang {
25 
26 class Stmt;
27 class Decl;
28 class VariableArrayType;
29 
31 protected:
32  enum { StmtMode = 0x0, SizeOfTypeVAMode = 0x1, DeclGroupMode = 0x2,
33  Flags = 0x3 };
34 
35  union {
37  Decl **DGI;
38  };
40  Decl **DGE;
41 
42  bool inDeclGroup() const {
43  return (RawVAPtr & Flags) == DeclGroupMode;
44  }
45 
46  bool inSizeOfTypeVA() const {
47  return (RawVAPtr & Flags) == SizeOfTypeVAMode;
48  }
49 
50  bool inStmt() const {
51  return (RawVAPtr & Flags) == StmtMode;
52  }
53 
54  const VariableArrayType *getVAPtr() const {
55  return reinterpret_cast<const VariableArrayType*>(RawVAPtr & ~Flags);
56  }
57 
58  void setVAPtr(const VariableArrayType *P) {
59  assert (inDeclGroup() || inSizeOfTypeVA());
60  RawVAPtr = reinterpret_cast<uintptr_t>(P) | (RawVAPtr & Flags);
61  }
62 
63  void NextDecl(bool ImmediateAdvance = true);
64  bool HandleDecl(Decl* D);
65  void NextVA();
66 
67  Stmt*& GetDeclExpr() const;
68 
69  StmtIteratorBase(Stmt **s) : stmt(s), RawVAPtr(0) {}
71  StmtIteratorBase(Decl **dgi, Decl **dge);
72  StmtIteratorBase() : stmt(nullptr), RawVAPtr(0) {}
73 };
74 
75 
76 template <typename DERIVED, typename REFERENCE>
78  public std::iterator<std::forward_iterator_tag,
79  REFERENCE, ptrdiff_t,
80  REFERENCE, REFERENCE> {
81 protected:
83 public:
86  StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
88 
89  DERIVED& operator++() {
90  if (inStmt())
91  ++stmt;
92  else if (getVAPtr())
93  NextVA();
94  else
95  NextDecl();
96 
97  return static_cast<DERIVED&>(*this);
98  }
99 
100  DERIVED operator++(int) {
101  DERIVED tmp = static_cast<DERIVED&>(*this);
102  operator++();
103  return tmp;
104  }
105 
106  bool operator==(const DERIVED& RHS) const {
107  return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr;
108  }
109 
110  bool operator!=(const DERIVED& RHS) const {
111  return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr;
112  }
113 
114  REFERENCE operator*() const {
115  return inStmt() ? *stmt : GetDeclExpr();
116  }
117 
118  REFERENCE operator->() const { return operator*(); }
119 };
120 
121 struct ConstStmtIterator;
122 
123 struct StmtIterator : public StmtIteratorImpl<StmtIterator,Stmt*&> {
125 
127 
128  StmtIterator(Decl** dgi, Decl** dge)
129  : StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {}
130 
133 
134 private:
135  StmtIterator(const StmtIteratorBase &RHS)
137 
138  inline friend StmtIterator
140 };
141 
142 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
143  const Stmt*> {
144  explicit ConstStmtIterator() :
146 
148  StmtIteratorImpl<ConstStmtIterator,const Stmt*>(RHS) {}
149 
152  const_cast<Stmt **>(S)) {}
153 };
154 
156  return RHS;
157 }
158 } // end namespace clang
159 
160 #endif
ConstStmtIterator(const StmtIterator &RHS)
Definition: StmtIterator.h:147
Stmt - This represents one statement.
Definition: Stmt.h:60
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:81
StringRef P
bool inDeclGroup() const
Definition: StmtIterator.h:42
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:54
StmtIteratorImpl(const VariableArrayType *t)
Definition: StmtIterator.h:87
StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:155
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)
Definition: CharUnits.h:208
REFERENCE operator->() const
Definition: StmtIterator.h:118
void NextDecl(bool ImmediateAdvance=true)
StmtIteratorImpl(const StmtIteratorBase &RHS)
Definition: StmtIterator.h:82
bool operator==(const DERIVED &RHS) const
Definition: StmtIterator.h:106
ConstStmtIterator(Stmt *const *S)
Definition: StmtIterator.h:150
__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:46
bool operator!=(const DERIVED &RHS) const
Definition: StmtIterator.h:110
Stmt *& GetDeclExpr() const
Dataflow Directional Tag Classes.
bool HandleDecl(Decl *D)
StmtIterator(const VariableArrayType *t)
Definition: StmtIterator.h:131
StmtIteratorImpl(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:86
REFERENCE operator*() const
Definition: StmtIterator.h:114
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:58
StmtIterator(Stmt **S)
Definition: StmtIterator.h:126
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:2646