-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_expression.h
More file actions
548 lines (466 loc) · 11.5 KB
/
Copy pathcpp_expression.h
File metadata and controls
548 lines (466 loc) · 11.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef BE6CEA53_1EF6_4B14_AA5B_A7FAA7AE5512
#define BE6CEA53_1EF6_4B14_AA5B_A7FAA7AE5512
#include "cppast/cpp_entity.h"
#include "cppast/cpp_expression_operators.h"
#include "cppast/cpp_expression_type.h"
#include "cppast/cpp_typecast_type.h"
#include "cppast/cpp_var_type.h"
#include <string>
namespace cppast {
class CppExpression;
class CppLambda;
class CppVarType;
/**
* @brief An expression in a C/C++ program.
*
* Its a base class for all other specialized expression classes.
*/
class CppExpression : public CppEntity
{
public:
static constexpr auto EntityType()
{
return CppEntityType::EXPRESSION;
}
public:
CppExpressionType expressionType() const
{
return expressionType_;
}
protected:
CppExpression(CppExpressionType expressionType)
: CppEntity(EntityType())
, expressionType_(expressionType)
{
}
private:
const CppExpressionType expressionType_;
};
enum class CppAtomicExprType
{
STRING_LITERAL,
CHAR_LITERAL,
NUMBER_LITEREL,
NAME,
VARTYPE,
LAMBDA,
};
/**
* @brief Atomic expression is an expression that involves no operator.
*
* Expressions like string literal or number literals are atomic expressions.
* @see CppAtomicExprType.
*/
class CppAtomicExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::ATOMIC;
}
auto atomicExpressionType() const
{
return atomicExprType_;
}
protected:
CppAtomicExpr(CppAtomicExprType atomicExprType)
: CppExpression(ExpressionType())
, atomicExprType_(atomicExprType)
{
}
private:
const CppAtomicExprType atomicExprType_;
};
template <CppAtomicExprType _AtomicExprType>
class CppAtomicExprImplBase
{
public:
static constexpr auto AtomicExprType()
{
return _AtomicExprType;
}
protected:
CppAtomicExprImplBase() {}
};
template <CppAtomicExprType _AtomicExprType>
class CppCommonAtomicExprImplBase : public CppAtomicExprImplBase<_AtomicExprType>
{
public:
const std::string& value() const
{
return atom_;
}
protected:
CppCommonAtomicExprImplBase(std::string atom)
: atom_(std::move(atom))
{
}
private:
std::string atom_;
};
class CppStringLiteralExpr : public CppAtomicExpr, public CppCommonAtomicExprImplBase<CppAtomicExprType::STRING_LITERAL>
{
public:
CppStringLiteralExpr(std::string atom)
: CppAtomicExpr(AtomicExprType())
, CppCommonAtomicExprImplBase(std::move(atom))
{
}
friend bool operator==(const CppStringLiteralExpr& lhs, const CppStringLiteralExpr& rhs)
{
return lhs.value() == rhs.value();
}
};
class CppCharLiteralExpr : public CppAtomicExpr, public CppCommonAtomicExprImplBase<CppAtomicExprType::CHAR_LITERAL>
{
public:
CppCharLiteralExpr(std::string atom)
: CppAtomicExpr(AtomicExprType())
, CppCommonAtomicExprImplBase(std::move(atom))
{
}
};
class CppNumberLiteralExpr : public CppAtomicExpr, public CppCommonAtomicExprImplBase<CppAtomicExprType::NUMBER_LITEREL>
{
public:
CppNumberLiteralExpr(std::string atom)
: CppAtomicExpr(AtomicExprType())
, CppCommonAtomicExprImplBase(std::move(atom))
{
}
};
class CppNameExpr : public CppAtomicExpr, public CppCommonAtomicExprImplBase<CppAtomicExprType::NAME>
{
public:
CppNameExpr(std::string atom)
: CppAtomicExpr(AtomicExprType())
, CppCommonAtomicExprImplBase(std::move(atom))
{
}
friend bool operator==(const CppNameExpr& lhs, const CppNameExpr& rhs)
{
return lhs.value() == rhs.value();
}
};
class CppVartypeExpr : public CppAtomicExpr, public CppAtomicExprImplBase<CppAtomicExprType::VARTYPE>
{
public:
CppVartypeExpr(std::unique_ptr<CppVarType> atom)
: CppAtomicExpr(AtomicExprType())
, atom_(std::move(atom))
{
}
const CppVarType& value() const
{
return *atom_;
}
private:
std::unique_ptr<CppVarType> atom_;
};
// TODO: Eliminate CppLambda by merging to CppLambdaExpr.
class CppLambdaExpr : public CppAtomicExpr, public CppAtomicExprImplBase<CppAtomicExprType::LAMBDA>
{
public:
CppLambdaExpr(std::unique_ptr<CppLambda> lambda)
: CppAtomicExpr(AtomicExprType())
, lambda_(std::move(lambda))
{
}
public:
const CppLambda& lamda() const
{
return *lambda_;
}
private:
std::unique_ptr<CppLambda> lambda_;
};
class CppMonomialExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::MONOMIAL;
}
public:
CppMonomialExpr(CppUnaryOperator oper, std::unique_ptr<CppExpression> term)
: CppExpression(ExpressionType())
, oper_(oper)
, term_(std::move(term))
{
}
public:
const CppExpression& term() const
{
return *term_;
}
CppUnaryOperator oper() const
{
return oper_;
}
private:
CppUnaryOperator oper_;
std::unique_ptr<CppExpression> term_;
};
class CppBinomialExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::BINOMIAL;
}
public:
CppBinomialExpr(CppBinaryOperator oper,
std::unique_ptr<CppExpression> term1,
std::unique_ptr<CppExpression> term2)
: CppExpression(ExpressionType())
, oper_(oper)
, term1_(std::move(term1))
, term2_(std::move(term2))
{
}
public:
CppBinaryOperator oper() const
{
return oper_;
}
const CppExpression& term1() const
{
return *term1_;
}
const CppExpression& term2() const
{
return *term2_;
}
private:
CppBinaryOperator oper_;
std::unique_ptr<CppExpression> term1_;
std::unique_ptr<CppExpression> term2_;
};
class CppTrinomialExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::TRINOMIAL;
}
public:
CppTrinomialExpr(CppTernaryOperator oper,
std::unique_ptr<CppExpression> term1,
std::unique_ptr<CppExpression> term2,
std::unique_ptr<CppExpression> term3)
: CppExpression(ExpressionType())
, oper_(oper)
, term1_(std::move(term1))
, term2_(std::move(term2))
, term3_(std::move(term3))
{
}
public:
CppTernaryOperator oper() const
{
return oper_;
}
const CppExpression& term1() const
{
return *term1_;
}
const CppExpression& term2() const
{
return *term2_;
}
const CppExpression& term3() const
{
return *term3_;
}
private:
CppTernaryOperator oper_;
std::unique_ptr<CppExpression> term1_;
std::unique_ptr<CppExpression> term2_;
std::unique_ptr<CppExpression> term3_;
};
class CppFunctionCallExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::FUNCTION_CALL;
}
public:
CppFunctionCallExpr(std::unique_ptr<CppExpression> func, std::vector<std::unique_ptr<CppExpression>> args)
: CppExpression(ExpressionType())
, function_(std::move(func))
, arguments_(std::move(args))
{
}
public:
const CppExpression& function() const
{
return *function_;
}
size_t numArgs() const
{
return arguments_.size();
}
const CppExpression& arg(size_t argIndex) const
{
return *(arguments_[argIndex]);
}
private:
std::unique_ptr<CppExpression> function_;
std::vector<std::unique_ptr<CppExpression>> arguments_;
};
class CppUniformInitializerExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::UNIFORM_INITIALIZER;
}
public:
CppUniformInitializerExpr(std::string name, std::vector<std::unique_ptr<CppExpression>> args)
: CppExpression(ExpressionType())
, name_(std::move(name))
, arguments_(std::move(args))
{
}
public:
const std::string& name() const
{
return name_;
}
size_t numArgs() const
{
return arguments_.size();
}
const CppExpression& arg(size_t argIndex) const
{
return *(arguments_[argIndex]);
}
private:
std::string name_;
std::vector<std::unique_ptr<CppExpression>> arguments_;
};
class CppInitializerListExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::INITIALIZER_LIST;
}
public:
CppInitializerListExpr(std::vector<std::unique_ptr<CppExpression>> exprList)
: CppExpression(ExpressionType())
, exprList_(std::move(exprList))
{
}
public:
size_t numArgs() const
{
return exprList_.size();
}
const CppExpression& arg(size_t argIndex) const
{
return *(exprList_[argIndex]);
}
private:
std::vector<std::unique_ptr<CppExpression>> exprList_;
};
class CppTypecastExpr : public CppExpression
{
public:
static constexpr auto ExpressionType()
{
return CppExpressionType::TYPECAST;
}
public:
auto castType() const
{
return castType_;
}
public:
const CppVarType& targetType() const
{
return *targetType_;
}
const CppExpression& inputExpresion() const
{
return *expr_;
}
protected:
CppTypecastExpr(CppTypecastType typecastType,
std::unique_ptr<CppVarType> targetType,
std::unique_ptr<CppExpression> expr)
: CppExpression(ExpressionType())
, castType_(typecastType)
, targetType_(std::move(targetType))
, expr_(std::move(expr))
{
}
private:
CppTypecastType castType_;
std::unique_ptr<CppVarType> targetType_;
std::unique_ptr<CppExpression> expr_;
};
template <CppTypecastType _TypecastType>
class CppTypecastExprImplBase
{
public:
static constexpr auto TypecastType()
{
return _TypecastType;
}
public:
};
class CppCStyleTypecastExpr : public CppTypecastExpr, public CppTypecastExprImplBase<CppTypecastType::C_STYLE>
{
public:
CppCStyleTypecastExpr(std::unique_ptr<CppVarType> targetType, std::unique_ptr<CppExpression> expr)
: CppTypecastExpr(TypecastType(), std::move(targetType), std::move(expr))
{
}
};
class CppFunctionStyleTypecastExpr : public CppTypecastExpr,
public CppTypecastExprImplBase<CppTypecastType::FUNCTION_STYLE>
{
public:
CppFunctionStyleTypecastExpr(std::unique_ptr<CppVarType> targetType, std::unique_ptr<CppExpression> expr)
: CppTypecastExpr(TypecastType(), std::move(targetType), std::move(expr))
{
}
};
class CppStaticCastExpr : public CppTypecastExpr, public CppTypecastExprImplBase<CppTypecastType::STATIC>
{
public:
CppStaticCastExpr(std::unique_ptr<CppVarType> targetType, std::unique_ptr<CppExpression> expr)
: CppTypecastExpr(TypecastType(), std::move(targetType), std::move(expr))
{
}
};
class CppConstCastExpr : public CppTypecastExpr, public CppTypecastExprImplBase<CppTypecastType::CONST>
{
public:
CppConstCastExpr(std::unique_ptr<CppVarType> targetType, std::unique_ptr<CppExpression> expr)
: CppTypecastExpr(TypecastType(), std::move(targetType), std::move(expr))
{
}
};
class CppDynamiCastExpr : public CppTypecastExpr, public CppTypecastExprImplBase<CppTypecastType::DYNAMIC>
{
public:
CppDynamiCastExpr(std::unique_ptr<CppVarType> targetType, std::unique_ptr<CppExpression> expr)
: CppTypecastExpr(TypecastType(), std::move(targetType), std::move(expr))
{
}
};
class CppReinterpretCastExpr : public CppTypecastExpr, public CppTypecastExprImplBase<CppTypecastType::REINTERPRET>
{
public:
CppReinterpretCastExpr(std::unique_ptr<CppVarType> targetType, std::unique_ptr<CppExpression> expr)
: CppTypecastExpr(TypecastType(), std::move(targetType), std::move(expr))
{
}
};
} // namespace cppast
#endif /* BE6CEA53_1EF6_4B14_AA5B_A7FAA7AE5512 */