-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_control_blocks.h
More file actions
234 lines (198 loc) · 4.7 KB
/
Copy pathcpp_control_blocks.h
File metadata and controls
234 lines (198 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef C18A3DB4_37B0_4AC7_BAB2_C9D9735913DC
#define C18A3DB4_37B0_4AC7_BAB2_C9D9735913DC
#include "cppast/cpp_entity.h"
#include "cppast/cpp_expression.h"
#include "cppast/cpp_var.h"
namespace cppast {
/// Some blocks have common structure like if, while, and do-while.
/// They all contain a body and an expression of condition.
template <CppEntityType _EntityType>
class CppControlBlockBase
{
public:
static constexpr auto EntityType()
{
return _EntityType;
}
public:
const CppEntity* condition() const
{
return cond_.get();
}
const CppEntity* body() const
{
return body_.get();
}
protected:
CppControlBlockBase(std::unique_ptr<CppEntity> cond, std::unique_ptr<CppEntity> body)
: cond_(std::move(cond))
, body_(std::move(body))
{
}
private:
std::unique_ptr<CppEntity> cond_;
std::unique_ptr<CppEntity> body_;
};
class CppIfBlock : public CppEntity, public CppControlBlockBase<CppEntityType::IF_BLOCK>
{
public:
CppIfBlock(std::unique_ptr<CppEntity> cond,
std::unique_ptr<CppEntity> body,
std::unique_ptr<CppEntity> elseArg = nullptr)
: CppEntity(EntityType())
, CppControlBlockBase(std::move(cond), std::move(body))
, else_(std::move(elseArg))
{
}
public:
const CppEntity* elsePart() const
{
return else_.get();
}
private:
std::unique_ptr<CppEntity> else_;
};
class CppWhileBlock : public CppEntity, public CppControlBlockBase<CppEntityType::WHILE_BLOCK>
{
public:
CppWhileBlock(std::unique_ptr<CppEntity> cond, std::unique_ptr<CppEntity> body)
: CppEntity(EntityType())
, CppControlBlockBase(std::move(cond), std::move(body))
{
}
};
class CppDoWhileBlock : public CppEntity, public CppControlBlockBase<CppEntityType::DO_WHILE_BLOCK>
{
public:
CppDoWhileBlock(std::unique_ptr<CppEntity> cond, std::unique_ptr<CppEntity> body)
: CppEntity(EntityType())
, CppControlBlockBase(std::move(cond), std::move(body))
{
}
};
class CppForBlock : public CppEntity
{
public:
static constexpr auto EntityType()
{
return CppEntityType::FOR_BLOCK;
}
public:
CppForBlock(std::unique_ptr<CppEntity> start,
std::unique_ptr<CppExpression> stop,
std::unique_ptr<CppExpression> step,
std::unique_ptr<CppEntity> body)
: CppEntity(EntityType())
, start_(std::move(start))
, stop_(std::move(stop))
, step_(std::move(step))
, body_(std::move(body))
{
}
const CppEntity* start() const
{
return start_.get();
}
const CppExpression* stop() const
{
return stop_.get();
}
const CppExpression* step() const
{
return step_.get();
}
const CppEntity* body() const
{
return body_.get();
}
private:
std::unique_ptr<CppEntity> start_;
std::unique_ptr<CppExpression> stop_;
std::unique_ptr<CppExpression> step_;
std::unique_ptr<CppEntity> body_;
};
class CppRangeForBlock : public CppEntity
{
public:
static constexpr auto EntityType()
{
return CppEntityType::RANGE_FOR_BLOCK;
}
public:
CppRangeForBlock(std::unique_ptr<CppVar> var, std::unique_ptr<CppExpression> expr, std::unique_ptr<CppEntity> body)
: CppEntity(EntityType())
, var_(std::move(var))
, expr_(std::move(expr))
, body_(std::move(body))
{
}
public:
const CppVar* var() const
{
return var_.get();
}
const CppExpression* expr() const
{
return expr_.get();
}
const CppEntity* body() const
{
return body_.get();
}
private:
std::unique_ptr<CppVar> var_;
std::unique_ptr<CppExpression> expr_;
std::unique_ptr<CppEntity> body_;
};
class CppCase
{
public:
CppCase(std::unique_ptr<CppExpression> caseExpr, std::unique_ptr<CppCompound> body)
: case_(std::move(caseExpr))
, body_(std::move(body))
{
}
public:
const CppExpression* caseExpr() const
{
return case_.get();
}
const CppCompound* body() const
{
return body_.get();
}
private:
std::unique_ptr<CppExpression> case_;
std::unique_ptr<CppCompound> body_;
};
class CppSwitchBlock : public CppEntity
{
public:
static constexpr auto EntityType()
{
return CppEntityType::SWITCH_BLOCK;
}
public:
CppSwitchBlock(std::unique_ptr<CppExpression> cond, std::vector<CppCase> body)
: CppEntity(EntityType())
, cond_(std::move(cond))
, body_(std::move(body))
{
}
public:
const CppExpression* condition() const
{
return cond_.get();
}
const std::vector<CppCase>& body() const
{
return body_;
}
private:
std::unique_ptr<CppExpression> cond_;
std::vector<CppCase> body_;
};
} // namespace cppast
#endif /* C18A3DB4_37B0_4AC7_BAB2_C9D9735913DC */