clang 20.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
21namespace clang {
22
23class Decl;
24class Stmt;
25class VariableArrayType;
26
28protected:
29 enum {
30 StmtMode = 0x0,
33 Flags = 0x3
34 };
35
36 union {
39 };
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
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
76template <typename DERIVED, typename REFERENCE>
78protected:
80
81public:
82 using iterator_category = std::forward_iterator_tag;
83 using value_type = REFERENCE;
84 using difference_type = std::ptrdiff_t;
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
126struct ConstStmtIterator;
127
128struct StmtIterator : public StmtIteratorImpl<StmtIterator, Stmt*&> {
129 explicit StmtIterator() = default;
131 StmtIterator(Decl** dgi, Decl** dge)
132 : StmtIteratorImpl<StmtIterator, Stmt*&>(dgi, dge) {}
135
136private:
138 : StmtIteratorImpl<StmtIterator, Stmt *&>(RHS) {}
139
140 inline friend StmtIterator
141 cast_away_const(const ConstStmtIterator &RHS);
142};
143
144struct 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
StringRef P
const Decl * D
__device__ __2f16 float __ockl_bool s
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void NextDecl(bool ImmediateAdvance=true)
bool HandleDecl(Decl *D)
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:64
Stmt *& GetDeclExpr() const
const VariableArrayType * getVAPtr() const
Definition: StmtIterator.h:60
bool inDeclGroup() const
Definition: StmtIterator.h:48
bool inSizeOfTypeVA() const
Definition: StmtIterator.h:52
friend bool operator!=(const DERIVED &LHS, const DERIVED &RHS)
Definition: StmtIterator.h:115
REFERENCE operator*() const
Definition: StmtIterator.h:119
StmtIteratorImpl(const VariableArrayType *t)
Definition: StmtIterator.h:91
REFERENCE operator->() const
Definition: StmtIterator.h:123
std::forward_iterator_tag iterator_category
Definition: StmtIterator.h:82
StmtIteratorImpl(const StmtIteratorBase &RHS)
Definition: StmtIterator.h:79
std::ptrdiff_t difference_type
Definition: StmtIterator.h:84
friend bool operator==(const DERIVED &LHS, const DERIVED &RHS)
Definition: StmtIterator.h:110
StmtIteratorImpl(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:90
Stmt - This represents one statement.
Definition: Stmt.h:84
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3795
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
The JSON file list parser is used to communicate input to InstallAPI.
StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:155
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
ConstStmtIterator(const StmtIterator &RHS)
Definition: StmtIterator.h:147
ConstStmtIterator(Stmt *const *S)
Definition: StmtIterator.h:150
StmtIterator(Decl **dgi, Decl **dge)
Definition: StmtIterator.h:131
StmtIterator(Stmt **S)
Definition: StmtIterator.h:130
StmtIterator(const VariableArrayType *t)
Definition: StmtIterator.h:133
friend StmtIterator cast_away_const(const ConstStmtIterator &RHS)
Definition: StmtIterator.h:155
StmtIterator()=default