clang  9.0.0svn
StmtIterator.h
Go to the documentation of this file.
1 //===- StmtIterator.h - Iterators for Statements ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the StmtIterator and ConstStmtIterator classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_STMTITERATOR_H
14 #define LLVM_CLANG_AST_STMTITERATOR_H
15 
16 #include <cassert>
17 #include <cstddef>
18 #include <cstdint>
19 #include <iterator>
20 
21 namespace clang {
22 
23 class Decl;
24 class Stmt;
25 class VariableArrayType;
26 
28 protected:
29  enum {
30  StmtMode = 0x0,
33  Flags = 0x3
34  };
35 
36  union {
38  Decl **DGI;
39  };
41  Decl **DGE;
42 
43  StmtIteratorBase(Stmt **s) : stmt(s) {}
45  StmtIteratorBase(Decl **dgi, Decl **dge);
46  StmtIteratorBase() : stmt(nullptr) {}
47 
48  bool inDeclGroup() const {
49  return (RawVAPtr & Flags) == DeclGroupMode;
50  }
51 
52  bool inSizeOfTypeVA() const {
53  return (RawVAPtr & Flags) == SizeOfTypeVAMode;
54  }
55 
56  bool inStmt() const {
57  return (RawVAPtr & Flags) == StmtMode;
58  }
59 
60  const VariableArrayType *getVAPtr() const {
61  return reinterpret_cast<const VariableArrayType*>(RawVAPtr & ~Flags);
62  }
63 
64  void setVAPtr(const VariableArrayType *P) {
65  assert(inDeclGroup() || inSizeOfTypeVA());
66  RawVAPtr = reinterpret_cast<uintptr_t>(P) | (RawVAPtr & Flags);
67  }
68 
69  void NextDecl(bool ImmediateAdvance = true);
70  bool HandleDecl(Decl* D);
71  void NextVA();
72 
73  Stmt*& GetDeclExpr() const;
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 
84 public:
85  StmtIteratorImpl() = default;
87  StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
89 
90  DERIVED& operator++() {
91  if (inStmt())
92  ++stmt;
93  else if (getVAPtr())
94  NextVA();
95  else
96  NextDecl();
97 
98  return static_cast<DERIVED&>(*this);
99  }
100 
101  DERIVED operator++(int) {
102  DERIVED tmp = static_cast<DERIVED&>(*this);
103  operator++();
104  return tmp;
105  }
106 
107  bool operator==(const DERIVED& RHS) const {
108  return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr;
109  }
110 
111  bool operator!=(const DERIVED& RHS) const {
112  return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr;
113  }
114 
115  REFERENCE operator*() const {
116  return inStmt() ? *stmt : GetDeclExpr();
117  }
118 
119  REFERENCE operator->() const { return operator*(); }
120 };
121 
122 struct ConstStmtIterator;
123 
124 struct StmtIterator : public StmtIteratorImpl<StmtIterator, Stmt*&> {
125  explicit StmtIterator() = default;
127  StmtIterator(Decl** dgi, Decl** dge)
128  : StmtIteratorImpl<StmtIterator, Stmt*&>(dgi, dge) {}
131 
132 private:
133  StmtIterator(const StmtIteratorBase &RHS)
135 
136  inline friend StmtIterator
138 };
139 
140 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
141  const Stmt*> {
142  explicit ConstStmtIterator() = default;
144  : StmtIteratorImpl<ConstStmtIterator, const Stmt*>(RHS) {}
145 
148  const_cast<Stmt **>(S)) {}
149 };
150 
152  return RHS;
153 }
154 
155 } // namespace clang
156 
157 #endif // LLVM_CLANG_AST_STMTITERATOR_H
ConstStmtIterator(const StmtIterator &RHS)
Definition: StmtIterator.h:143
Stmt - This represents one statement.
Definition: Stmt.h:65
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
StringRef P
bool inDeclGroup() const
Definition: StmtIterator.h:48
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:60
StmtIteratorImpl(const VariableArrayType *t)
Definition: StmtIterator.h:88
StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:151
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)
Definition: CharUnits.h:207
REFERENCE operator->() const
Definition: StmtIterator.h:119
void NextDecl(bool ImmediateAdvance=true)
StmtIteratorImpl(const StmtIteratorBase &RHS)
Definition: StmtIterator.h:82
bool operator==(const DERIVED &RHS) const
Definition: StmtIterator.h:107
ConstStmtIterator(Stmt *const *S)
Definition: StmtIterator.h:146
__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:89
StmtIterator(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:127
bool inSizeOfTypeVA() const
Definition: StmtIterator.h:52
bool operator!=(const DERIVED &RHS) const
Definition: StmtIterator.h:111
Stmt *& GetDeclExpr() const
Dataflow Directional Tag Classes.
bool HandleDecl(Decl *D)
StmtIterator(const VariableArrayType *t)
Definition: StmtIterator.h:129
StmtIteratorImpl(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:87
REFERENCE operator*() const
Definition: StmtIterator.h:115
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:64
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:2976