1
2
3
4
5
6 #ifndef SNGCPP_AST_EXPRESSION_INCLUDED
7 #define SNGCPP_AST_EXPRESSION_INCLUDED
8 #include <sngcpp/ast/SimpleType.hpp>
9 #include <sngcpp/ast/Statement.hpp>
10
11 namespace sngcpp { namespace ast {
12
13 class CompoundStatementNode;
14
15 enum class Operator : uint8_t
16 {
17 assign, mulAssign, divAssign, remAssign, addAssign, subAssign, shiftRightAssign, shiftLeftAssign, andAssign, xorAssign, orAssign,
18 equal, notEqual, lessOrEqual, greaterOrEqual, less, greater, shiftLeft, shiftRight, add, sub, mul, div, rem, dotStar, arrowStar, arrow,
19 inc, dec, deref, addrOf, unaryPlus, unaryMinus, not_, complement, sizeOf, alignOf, dynamicCast, staticCast, reinterpretCast, constCast,
20 logicalAnd, logicalOr, xor_, and_, or_, apply, subscript, comma, newArray, deleteArray, new_, delete_
21 };
22
23 std::u32string OpStr(Operator op);
24
25 class ExpressionSequenceNode : public BinaryNode
26 {
27 public:
28 ExpressionSequenceNode();
29 ExpressionSequenceNode(const Span& span_, Node* left_, Node* right_);
30 void Accept(Visitor& visitor) override;
31 };
32
33 class CommaExpressionNode : public BinaryNode
34 {
35 public:
36 CommaExpressionNode();
37 CommaExpressionNode(const Span& span_, Node* left_, Node* right_);
38 void Accept(Visitor& visitor) override;
39 };
40
41 class AssignmentExpressionNode : public BinaryNode
42 {
43 public:
44 AssignmentExpressionNode();
45 AssignmentExpressionNode(const Span& span_, Node* left_, Operator op_, Node* right_);
46 void Accept(Visitor& visitor) override;
47 void Write(Writer& writer) override;
48 void Read(Reader& reader) override;
49 Operator Op() const { return op; }
50 private:
51 Operator op;
52 };
53
54 class ConditionalExpressionNode : public Node
55 {
56 public:
57 ConditionalExpressionNode();
58 ConditionalExpressionNode(const Span& span_, Node* condition_, Node* thenExpr_, Node* elseExpr_);
59 void Accept(Visitor& visitor) override;
60 void Write(Writer& writer) override;
61 void Read(Reader& reader) override;
62 Node* Condition() { return condition.get(); }
63 Node* ThenExpr() { return thenExpr.get(); }
64 Node* ElseExpr() { return elseExpr.get(); }
65 private:
66 std::unique_ptr<Node> condition;
67 std::unique_ptr<Node> thenExpr;
68 std::unique_ptr<Node> elseExpr;
69 };
70
71 class ThrowExpressionNode : public UnaryNode
72 {
73 public:
74 ThrowExpressionNode();
75 ThrowExpressionNode(const Span& span_, Node* exceptionExpr_);
76 void Accept(Visitor& visitor) override;
77 };
78
79 class LogicalOrExpressionNode : public BinaryNode
80 {
81 public:
82 LogicalOrExpressionNode();
83 LogicalOrExpressionNode(const Span& span_, Node* left_, Node* right_);
84 void Accept(Visitor& visitor) override;
85 };
86
87 class LogicalAndExpressionNode : public BinaryNode
88 {
89 public:
90 LogicalAndExpressionNode();
91 LogicalAndExpressionNode(const Span& span_, Node* left_, Node* right_);
92 void Accept(Visitor& visitor) override;
93 };
94
95 class InclusiveOrExpressionNode : public BinaryNode
96 {
97 public:
98 InclusiveOrExpressionNode();
99 InclusiveOrExpressionNode(const Span& span_, Node* left_, Node* right_);
100 void Accept(Visitor& visitor) override;
101 };
102
103 class ExclusiveOrExpressionNode : public BinaryNode
104 {
105 public:
106 ExclusiveOrExpressionNode();
107 ExclusiveOrExpressionNode(const Span& span_, Node* left_, Node* right_);
108 void Accept(Visitor& visitor) override;
109 };
110
111 class AndExpressionNode : public BinaryNode
112 {
113 public:
114 AndExpressionNode();
115 AndExpressionNode(const Span& span_, Node* left_, Node* right_);
116 void Accept(Visitor& visitor) override;
117 };
118
119 class EqualityExpressionNode : public BinaryNode
120 {
121 public:
122 EqualityExpressionNode();
123 EqualityExpressionNode(const Span& span_, Node* left_, Node* right_, Operator op_);
124 void Accept(Visitor& visitor) override;
125 void Write(Writer& writer) override;
126 void Read(Reader& reader) override;
127 Operator Op() const { return op; }
128 private:
129 Operator op;
130 };
131
132 class RelationalExpressionNode : public BinaryNode
133 {
134 public:
135 RelationalExpressionNode();
136 RelationalExpressionNode(const Span& span_, Node* left_, Node* right_, Operator op_);
137 void Accept(Visitor& visitor) override;
138 void Write(Writer& writer) override;
139 void Read(Reader& reader) override;
140 Operator Op() const { return op; }
141 private:
142 Operator op;
143 };
144
145 class ShiftExpressionNode : public BinaryNode
146 {
147 public:
148 ShiftExpressionNode();
149 ShiftExpressionNode(const Span& span_, Node* left_, Node* right_, Operator op_);
150 void Accept(Visitor& visitor) override;
151 void Write(Writer& writer) override;
152 void Read(Reader& reader) override;
153 Operator Op() const { return op; }
154 private:
155 Operator op;
156 };
157
158 class AdditiveExpressionNode : public BinaryNode
159 {
160 public:
161 AdditiveExpressionNode();
162 AdditiveExpressionNode(const Span& span_, Node* left_, Node* right_, Operator op_);
163 void Accept(Visitor& visitor) override;
164 void Write(Writer& writer) override;
165 void Read(Reader& reader) override;
166 Operator Op() const { return op; }
167 private:
168 Operator op;
169 };
170
171 class MultiplicativeExpressionNode : public BinaryNode
172 {
173 public:
174 MultiplicativeExpressionNode();
175 MultiplicativeExpressionNode(const Span& span_, Node* left_, Node* right_, Operator op_);
176 void Accept(Visitor& visitor) override;
177 void Write(Writer& writer) override;
178 void Read(Reader& reader) override;
179 Operator Op() const { return op; }
180 private:
181 Operator op;
182 };
183
184 class PMExpressionNode : public BinaryNode
185 {
186 public:
187 PMExpressionNode();
188 PMExpressionNode(const Span& span_, Node* left_, Node* right_, Operator op_);
189 void Accept(Visitor& visitor) override;
190 void Write(Writer& writer) override;
191 void Read(Reader& reader) override;
192 Operator Op() const { return op; }
193 private:
194 Operator op;
195 };
196
197 class CastExpressionNode : public Node
198 {
199 public:
200 CastExpressionNode();
201 CastExpressionNode(const Span& span_, Node* typeExpr_, Node* expr_);
202 void Accept(Visitor& visitor) override;
203 void Write(Writer& writer) override;
204 void Read(Reader& reader) override;
205 Node* TypeExpr() { return typeExpr.get(); }
206 Node* Expr() { return expr.get(); }
207 private:
208 std::unique_ptr<Node> typeExpr;
209 std::unique_ptr<Node> expr;
210 };
211
212 class UnaryExpressionNode : public UnaryNode
213 {
214 public:
215 UnaryExpressionNode();
216 UnaryExpressionNode(const Span& span_, Operator op_, Node* expr_);
217 void Accept(Visitor& visitor) override;
218 void Write(Writer& writer) override;
219 void Read(Reader& reader) override;
220 Operator Op() const { return op; }
221 private:
222 Operator op;
223 };
224
225 class NewExpressionNode : public Node
226 {
227 public:
228 NewExpressionNode();
229 NewExpressionNode(const Span& span_, Node* placement_, Node* typeExpr_, Node* initializer_);
230 void Accept(Visitor& visitor) override;
231 void Write(Writer& writer) override;
232 void Read(Reader& reader) override;
233 Node* Placement() { return placement.get(); }
234 Node* TypeExpr() { return typeExpr.get(); }
235 Node* Initializer() { return initializer.get(); }
236 private:
237 std::unique_ptr<Node> placement;
238 std::unique_ptr<Node> typeExpr;
239 std::unique_ptr<Node> initializer;
240 };
241
242 class DeleteExpressionNode : public UnaryNode
243 {
244 public:
245 DeleteExpressionNode();
246 DeleteExpressionNode(const Span& span_, bool array__, Node* ptr_);
247 void Accept(Visitor& visitor) override;
248 void Write(Writer& writer) override;
249 void Read(Reader& reader) override;
250 bool Array() const { return array_; }
251 private:
252 bool array_;
253 };
254
255 class SubscriptExpressionNode : public UnaryNode
256 {
257 public:
258 SubscriptExpressionNode();
259 SubscriptExpressionNode(const Span& span_, Node* subject_, Node* index_);
260 void Accept(Visitor& visitor) override;
261 void Write(Writer& writer) override;
262 void Read(Reader& reader) override;
263 Node* Index() { return index.get(); }
264 private:
265 std::unique_ptr<Node> index;
266 };
267
268 class InvokeExpressionNode : public UnaryNode
269 {
270 public:
271 InvokeExpressionNode();
272 InvokeExpressionNode(const Span& span_, Node* subject_, Node* arguments_);
273 void Accept(Visitor& visitor) override;
274 void Write(Writer& writer) override;
275 void Read(Reader& reader) override;
276 Node* Arguments() { return arguments.get(); }
277 private:
278 std::unique_ptr<Node> arguments;
279 };
280
281 class DotNode : public UnaryNode
282 {
283 public:
284 DotNode();
285 DotNode(const Span& span_, Node* subject_, Node* id_);
286 void Accept(Visitor& visitor) override;
287 void Write(Writer& writer) override;
288 void Read(Reader& reader) override;
289 Node* Id() { return id.get(); }
290 private:
291 std::unique_ptr<Node> id;
292 };
293
294 class ArrowNode : public UnaryNode
295 {
296 public:
297 ArrowNode();
298 ArrowNode(const Span& span_, Node* subject_, Node* id_);
299 void Accept(Visitor& visitor) override;
300 void Write(Writer& writer) override;
301 void Read(Reader& reader) override;
302 Node* Id() { return id.get(); }
303 private:
304 std::unique_ptr<Node> id;
305 };
306
307 class PostfixIncNode : public UnaryNode
308 {
309 public:
310 PostfixIncNode();
311 PostfixIncNode(const Span& span_, Node* subject_);
312 void Accept(Visitor& visitor) override;
313 };
314
315 class PostfixDecNode : public UnaryNode
316 {
317 public:
318 PostfixDecNode();
319 PostfixDecNode(const Span& span_, Node* subject_);
320 void Accept(Visitor& visitor) override;
321 };
322
323 class CppCastExpressionNode : public Node
324 {
325 public:
326 CppCastExpressionNode();
327 CppCastExpressionNode(const Span& span_, Node* typeExpr_, Node* expr_, Operator op_);
328 void Accept(Visitor& visitor) override;
329 void Write(Writer& writer) override;
330 void Read(Reader& reader) override;
331 Node* TypeExpr() { return typeExpr.get(); }
332 Node* Expr() { return expr.get(); }
333 Operator Op() const { return op; }
334 private:
335 std::unique_ptr<Node> typeExpr;
336 std::unique_ptr<Node> expr;
337 Operator op;
338 };
339
340 class TypeIdExpressionNode : public UnaryNode
341 {
342 public:
343 TypeIdExpressionNode();
344 TypeIdExpressionNode(const Span& span_, Node* subject_);
345 void Accept(Visitor& visitor) override;
346 };
347
348 class ThisNode : public Node
349 {
350 public:
351 ThisNode();
352 ThisNode(const Span& span_);
353 void Accept(Visitor& visitor) override;
354 };
355
356 class IdentifierNode : public Node
357 {
358 public:
359 IdentifierNode();
360 IdentifierNode(NodeType nodeType_);
361 IdentifierNode(const Span& span_, const std::u32string& identifier_);
362 IdentifierNode(NodeType nodeType_, const Span& span_, const std::u32string& identifier_);
363 void Accept(Visitor& visitor) override;
364 void Write(Writer& writer) override;
365 void Read(Reader& reader) override;
366 bool IsIdentifierNode() const override { return true; }
367 const std::u32string& Identifier() const { return identifier; }
368 private:
369 std::u32string identifier;
370 };
371
372 class OperatorFunctionIdNode : public IdentifierNode
373 {
374 public:
375 OperatorFunctionIdNode();
376 OperatorFunctionIdNode(const Span& span_, Operator op_);
377 bool IsOperatorFunctionIdNode() const override { return true; }
378 void Accept(Visitor& visitor) override;
379 void Write(Writer& writer) override;
380 void Read(Reader& reader) override;
381 Operator Op() const { return op; }
382 std::u32string OpStr() const;
383 std::u32string Str() const;
384 std::u32string GroupName() const;
385 private:
386 Operator op;
387 };
388
389 class ConversionFunctionIdNode : public IdentifierNode
390 {
391 public:
392 ConversionFunctionIdNode();
393 ConversionFunctionIdNode(const Span& span_, Node* typeExpr_);
394 bool IsConversionFunctionIdNode() const override { return true; }
395 void Accept(Visitor& visitor) override;
396 void Write(Writer& writer) override;
397 void Read(Reader& reader) override;
398 Node* TypeExpr() { return typeExpr.get(); }
399 private:
400 std::unique_ptr<Node> typeExpr;
401 };
402
403 class DtorIdNode : public IdentifierNode
404 {
405 public:
406 DtorIdNode();
407 DtorIdNode(const Span& span_, const std::u32string& identifier_);
408 bool IsDtorIdNode() const override { return true; }
409 void Accept(Visitor& visitor) override;
410 };
411
412 class NestedIdNode : public BinaryNode
413 {
414 public:
415 NestedIdNode();
416 NestedIdNode(const Span& span_, Node* left_, Node* right_);
417 void Accept(Visitor& visitor) override;
418 };
419
420 class ParenthesizedExprNode : public UnaryNode
421 {
422 public:
423 ParenthesizedExprNode();
424 ParenthesizedExprNode(const Span& span_, Node* expr_);
425 void Accept(Visitor& visitor) override;
426 };
427
428 class LambdaExpressionNode : public Node
429 {
430 public:
431 LambdaExpressionNode();
432 LambdaExpressionNode(const Span& span_);
433 void AddCapture(Node* capture);
434 Node* Captures() { return captures.get(); }
435 void SetParameters(Node* parameters_);
436 Node* Parameters() { return parameters.get(); }
437 void SetBody(CompoundStatementNode* body_);
438 CompoundStatementNode* Body() { return body.get(); }
439 void Accept(Visitor& visitor) override;
440 void Write(Writer& writer) override;
441 void Read(Reader& reader) override;
442 private:
443 std::unique_ptr<Node> captures;
444 std::unique_ptr<Node> parameters;
445 std::unique_ptr<CompoundStatementNode> body;
446 };
447
448 class CaptureSequenceNode : public BinaryNode
449 {
450 public:
451 CaptureSequenceNode();
452 CaptureSequenceNode(const Span& span_, Node* left_, Node* right_);
453 void Accept(Visitor& visitor) override;
454 };
455
456 class AssignCaptureNode : public Node
457 {
458 public:
459 AssignCaptureNode();
460 AssignCaptureNode(const Span& span_);
461 void Accept(Visitor& visitor) override;
462 };
463
464 class RefCaptureNode : public Node
465 {
466 public:
467 RefCaptureNode();
468 RefCaptureNode(const Span& span_);
469 void Accept(Visitor& visitor) override;
470 };
471
472 class ThisCaptureNode : public Node
473 {
474 public:
475 ThisCaptureNode();
476 ThisCaptureNode(const Span& span_);
477 void Accept(Visitor& visitor) override;
478 };
479
480 class IdentifierCaptureNode : public UnaryNode
481 {
482 public:
483 IdentifierCaptureNode();
484 IdentifierCaptureNode(const Span& span_, IdentifierNode* id_);
485 void Accept(Visitor& visitor) override;
486 };
487
488 } }
489
490 #endif // SNGCPP_AST_EXPRESSION_INCLUDED