clang  14.0.0git
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 
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 protected:
80 
81 public:
82  using iterator_category = std::forward_iterator_tag;
83  using value_type = REFERENCE;
85  using pointer = REFERENCE;
86  using reference = REFERENCE;
87 
88  StmtIteratorImpl() = default;
90  StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
92 
93  DERIVED& operator++() {
94  if (inStmt())
95  ++stmt;
96  else if (getVAPtr())
97  NextVA();
98  else
99  NextDecl();
100 
101  return static_cast<DERIVED&>(*this);
102  }
103 
104  DERIVED operator++(int) {
105  DERIVED tmp = static_cast<DERIVED&>(*this);
106  operator++();
107  return tmp;
108  }
109 
110  friend bool operator==(const DERIVED &LHS, const DERIVED &RHS) {
111  return LHS.stmt == RHS.stmt && LHS.DGI == RHS.DGI &&
112  LHS.RawVAPtr == RHS.RawVAPtr;
113  }
114 
115  friend bool operator!=(const DERIVED &LHS, const DERIVED &RHS) {
116  return !(LHS == RHS);
117  }
118 
119  REFERENCE operator*() const {
120  return inStmt() ? *stmt : GetDeclExpr();
121  }
122 
123  REFERENCE operator->() const { return operator*(); }
124 };
125 
126 struct ConstStmtIterator;
127 
128 struct StmtIterator : public StmtIteratorImpl<StmtIterator, Stmt*&> {
129  explicit StmtIterator() = default;
131  StmtIterator(Decl** dgi, Decl** dge)
132  : StmtIteratorImpl<StmtIterator, Stmt*&>(dgi, dge) {}
135 
136 private:
137  StmtIterator(const StmtIteratorBase &RHS)
138  : StmtIteratorImpl<StmtIterator, Stmt *&>(RHS) {}
139 
140  inline friend StmtIterator
141  cast_away_const(const ConstStmtIterator &RHS);
142 };
143 
144 struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
145  const Stmt*> {
146  explicit ConstStmtIterator() = default;
148  : StmtIteratorImpl<ConstStmtIterator, const Stmt*>(RHS) {}
149 
152  const_cast<Stmt **>(S)) {}
153 };
154 
156  return RHS;
157 }
158 
159 } // namespace clang
160 
161 #endif // LLVM_CLANG_AST_STMTITERATOR_H
clang::StmtIteratorBase::RawVAPtr
uintptr_t RawVAPtr
Definition: StmtIterator.h:40
clang::StmtIteratorBase::DeclGroupMode
@ DeclGroupMode
Definition: StmtIterator.h:32
clang::StmtIteratorBase::inDeclGroup
bool inDeclGroup() const
Definition: StmtIterator.h:48
clang::StmtIteratorImpl< StmtIterator, Stmt *& >::difference_type
std::ptrdiff_t difference_type
Definition: StmtIterator.h:84
clang::StmtIteratorBase::StmtMode
@ StmtMode
Definition: StmtIterator.h:30
clang::StmtIteratorBase::getVAPtr
const VariableArrayType * getVAPtr() const
Definition: StmtIterator.h:60
clang::StmtIterator::StmtIterator
StmtIterator()=default
clang::StmtIteratorBase::StmtIteratorBase
StmtIteratorBase()
Definition: StmtIterator.h:46
clang::StmtIteratorImpl::operator++
DERIVED & operator++()
Definition: StmtIterator.h:93
clang::StmtIteratorBase::DGI
Decl ** DGI
Definition: StmtIterator.h:38
clang::StmtIteratorImpl::StmtIteratorImpl
StmtIteratorImpl(const StmtIteratorBase &RHS)
Definition: StmtIterator.h:79
clang::cast_away_const
StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:155
clang::StmtIteratorBase::SizeOfTypeVAMode
@ SizeOfTypeVAMode
Definition: StmtIterator.h:31
clang::CodeGen::AlignmentSource::Decl
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::StmtIteratorBase::StmtIteratorBase
StmtIteratorBase(Stmt **s)
Definition: StmtIterator.h:43
clang::StmtIteratorImpl
Definition: StmtIterator.h:77
clang::StmtIteratorBase::stmt
Stmt ** stmt
Definition: StmtIterator.h:37
clang::StmtIteratorImpl::operator++
DERIVED operator++(int)
Definition: StmtIterator.h:104
clang::StmtIterator
Definition: StmtIterator.h:128
clang::StmtIteratorImpl::operator*
REFERENCE operator*() const
Definition: StmtIterator.h:119
clang::StmtIteratorBase::setVAPtr
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:64
clang::StmtIteratorImpl::StmtIteratorImpl
StmtIteratorImpl()=default
clang::ConstStmtIterator::ConstStmtIterator
ConstStmtIterator()=default
uintptr_t
__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-base.h:124
clang::StmtIteratorBase::Flags
@ Flags
Definition: StmtIterator.h:33
clang::ConstStmtIterator::ConstStmtIterator
ConstStmtIterator(const StmtIterator &RHS)
Definition: StmtIterator.h:147
clang::StmtIteratorBase::DGE
Decl ** DGE
Definition: StmtIterator.h:41
clang::StmtIterator::cast_away_const
friend StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:155
clang::StmtIteratorImpl::operator==
friend bool operator==(const DERIVED &LHS, const DERIVED &RHS)
Definition: StmtIterator.h:110
clang::StmtIteratorImpl::operator!=
friend bool operator!=(const DERIVED &LHS, const DERIVED &RHS)
Definition: StmtIterator.h:115
clang::StmtIteratorBase
Definition: StmtIterator.h:27
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::VariableArrayType
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3033
clang::StmtIteratorImpl::StmtIteratorImpl
StmtIteratorImpl(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:90
clang::StmtIteratorBase::inSizeOfTypeVA
bool inSizeOfTypeVA() const
Definition: StmtIterator.h:52
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::StmtIteratorImpl< StmtIterator, Stmt *& >::iterator_category
std::forward_iterator_tag iterator_category
Definition: StmtIterator.h:82
clang::StmtIteratorImpl::StmtIteratorImpl
StmtIteratorImpl(Stmt **s)
Definition: StmtIterator.h:89
clang::StmtIterator::StmtIterator
StmtIterator(Stmt **S)
Definition: StmtIterator.h:130
clang::StmtIteratorBase::GetDeclExpr
Stmt *& GetDeclExpr() const
Definition: StmtIterator.cpp:109
clang::ConstStmtIterator::ConstStmtIterator
ConstStmtIterator(Stmt *const *S)
Definition: StmtIterator.h:150
clang::StmtIteratorImpl::StmtIteratorImpl
StmtIteratorImpl(const VariableArrayType *t)
Definition: StmtIterator.h:91
clang::ConstStmtIterator
Definition: StmtIterator.h:144
clang::StmtIteratorBase::NextDecl
void NextDecl(bool ImmediateAdvance=true)
Definition: StmtIterator.cpp:60
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
ptrdiff_t
__PTRDIFF_TYPE__ ptrdiff_t
A signed integer type that is the result of subtracting two pointers.
Definition: opencl-c-base.h:110
clang::StmtIteratorBase::NextVA
void NextVA()
Definition: StmtIterator.cpp:37
clang::StmtIteratorBase::HandleDecl
bool HandleDecl(Decl *D)
Definition: StmtIterator.cpp:74
s
__device__ __2f16 float bool s
Definition: __clang_hip_libdevice_declares.h:315
clang::StmtIterator::StmtIterator
StmtIterator(const VariableArrayType *t)
Definition: StmtIterator.h:133
clang::StmtIteratorBase::inStmt
bool inStmt() const
Definition: StmtIterator.h:56
clang::StmtIterator::StmtIterator
StmtIterator(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:131
clang::StmtIteratorImpl::operator->
REFERENCE operator->() const
Definition: StmtIterator.h:123