-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.rs
More file actions
398 lines (362 loc) · 9.61 KB
/
expr.rs
File metadata and controls
398 lines (362 loc) · 9.61 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
//! Expression AST nodes for C++20
//!
//! Analogous to `syn::Expr`.
use crate::SourceSpan;
use crate::ast::ty::{FundamentalKind, Type};
use super::item::{Ident, Path};
use super::punct::Punctuated;
/// The kind of a literal value.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LitKind {
Integer,
Float,
String,
Char,
}
impl LitKind {
/// Check if the fundamental kind matches the literal kind
pub fn match_fundamental(&self, kind: FundamentalKind) -> bool {
match self {
LitKind::Integer => matches!(
kind,
FundamentalKind::Short
| FundamentalKind::Int
| FundamentalKind::Long
| FundamentalKind::LongLong
| FundamentalKind::SignedChar
| FundamentalKind::UnsignedChar
| FundamentalKind::UnsignedShort
| FundamentalKind::UnsignedInt
| FundamentalKind::UnsignedLong
| FundamentalKind::UnsignedLongLong
),
LitKind::Float => matches!(
kind,
FundamentalKind::Float | FundamentalKind::Double | FundamentalKind::LongDouble
),
LitKind::Char => matches!(
kind,
FundamentalKind::Char
| FundamentalKind::Wchar
| FundamentalKind::Char8
| FundamentalKind::Char16
| FundamentalKind::Char32
),
_ => false,
}
}
/// Check if the literal kind matches the type
pub fn match_type(&self, ty: &Type) -> bool {
match ty {
Type::Fundamental(fund) => self.match_fundamental(fund.kind),
Type::Qualified(qualified) => self.match_type(&qualified.ty),
_ => false,
}
}
}
/// Unary operator.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum UnaryOp {
// Prefix
Plus,
Negate,
LogicalNot,
BitwiseNot,
Deref,
AddressOf,
PreIncrement,
PreDecrement,
// Postfix
PostIncrement,
PostDecrement,
}
/// Binary operator.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BinaryOp {
// Arithmetic
Add,
Sub,
Mul,
Div,
Mod,
// Shift
ShiftLeft,
ShiftRight,
// Comparison
Less,
LessEqual,
Greater,
GreaterEqual,
ThreeWay,
Equal,
NotEqual,
// Bitwise
BitAnd,
BitXor,
BitOr,
// Logical
LogicalAnd,
LogicalOr,
// Assignment
Assign,
AddAssign,
SubAssign,
MulAssign,
DivAssign,
ModAssign,
ShiftLeftAssign,
ShiftRightAssign,
BitAndAssign,
BitXorAssign,
BitOrAssign,
// Member access
Dot,
Arrow,
DotStar,
ArrowStar,
// Comma
Comma,
}
/// C++ cast kind.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CastKind {
Static,
Dynamic,
Const,
Reinterpret,
}
/// A C++ expression, analogous to `syn::Expr`.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr<'de> {
/// Literal value: `42`, `3.14`, `"hello"`, `'a'`
Lit(ExprLit<'de>),
/// Simple identifier: `foo`
Ident(ExprIdent<'de>),
/// Qualified path: `std::cout`, `::global`
Path(ExprPath<'de>),
/// Parenthesized expression: `(expr)`
Paren(ExprParen<'de>),
/// Boolean literal: `true` / `false`
Bool(ExprBool<'de>),
/// `nullptr`
Nullptr(ExprNullptr<'de>),
/// `this`
This(ExprThis<'de>),
/// Unary expression: `-x`, `!flag`, `*ptr`, `++i`, `i++`
Unary(ExprUnary<'de>),
/// Binary expression: `a + b`, `x = 1`
Binary(ExprBinary<'de>),
/// Ternary conditional: `a ? b : c`
Conditional(ExprConditional<'de>),
/// Function call: `foo(a, b)`
Call(ExprCall<'de>),
/// Method call: `obj.method(a, b)`
MethodCall(ExprMethodCall<'de>),
/// Array subscript: `arr[i]`
Index(ExprIndex<'de>),
/// Member access: `obj.field`
Field(ExprField<'de>),
/// C++ named cast: `static_cast<int>(x)`
Cast(ExprCast<'de>),
/// C-style cast: `(int)x`
CStyleCast(ExprCStyleCast<'de>),
/// `sizeof(expr)` or `sizeof expr`
Sizeof(ExprSizeof<'de>),
/// `alignof(type)`
Alignof(ExprAlignof<'de>),
/// `new` expression: `new int(42)`
New(ExprNew<'de>),
/// `delete` expression: `delete ptr`
Delete(ExprDelete<'de>),
/// `throw` expression: `throw runtime_error("...")`
Throw(ExprThrow<'de>),
/// Lambda expression: `[captures](params) { body }`
Lambda(ExprLambda<'de>),
/// `typeid(expr)` or `typeid(type)`
Typeid(ExprTypeid<'de>),
/// Braced initializer list: `{1, 2, 3}`
InitList(ExprInitList<'de>),
}
/// Literal expression: numbers, strings, chars.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExprLit<'de> {
pub span: SourceSpan<'de>,
pub kind: LitKind,
}
/// Simple identifier expression.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExprIdent<'de> {
pub ident: Ident<'de>,
}
/// Qualified path expression: `std::cout`, `::global::func`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprPath<'de> {
pub path: Path<'de>,
}
/// Parenthesized expression: `(expr)`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprParen<'de> {
pub expr: Box<Expr<'de>>,
}
/// Boolean literal.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExprBool<'de> {
pub span: SourceSpan<'de>,
pub value: bool,
}
/// `nullptr` expression.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExprNullptr<'de> {
pub span: SourceSpan<'de>,
}
/// `this` expression.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExprThis<'de> {
pub span: SourceSpan<'de>,
}
/// Unary expression (prefix or postfix).
#[derive(Debug, Clone, PartialEq)]
pub struct ExprUnary<'de> {
pub op: UnaryOp,
pub operand: Box<Expr<'de>>,
}
/// Binary expression: `lhs op rhs`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprBinary<'de> {
pub lhs: Box<Expr<'de>>,
pub op: BinaryOp,
pub rhs: Box<Expr<'de>>,
}
/// Ternary conditional: `condition ? then_expr : else_expr`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprConditional<'de> {
pub condition: Box<Expr<'de>>,
pub then_expr: Box<Expr<'de>>,
pub else_expr: Box<Expr<'de>>,
}
/// Function call: `callee(args...)`.
///
/// Analogous to `syn::ExprCall`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprCall<'de> {
pub func: Box<Expr<'de>>,
pub args: Punctuated<'de, Expr<'de>>,
}
/// Method call: `receiver.method(args...)`.
///
/// Analogous to `syn::ExprMethodCall`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprMethodCall<'de> {
pub receiver: Box<Expr<'de>>,
pub arrow: bool,
pub method: Ident<'de>,
pub args: Punctuated<'de, Expr<'de>>,
}
/// Array subscript: `object[index]`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprIndex<'de> {
pub object: Box<Expr<'de>>,
pub index: Box<Expr<'de>>,
}
/// Member access: `object.field` or `ptr->field`.
///
/// Analogous to `syn::ExprField`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprField<'de> {
pub object: Box<Expr<'de>>,
pub arrow: bool,
pub member: Ident<'de>,
}
/// C++ named cast: `static_cast<T>(expr)`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprCast<'de> {
pub cast_kind: CastKind,
pub ty: Box<super::ty::Type<'de>>,
pub expr: Box<Expr<'de>>,
}
/// C-style cast: `(type)expr`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprCStyleCast<'de> {
pub ty: Box<super::ty::Type<'de>>,
pub expr: Box<Expr<'de>>,
}
/// `sizeof` expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprSizeof<'de> {
pub operand: Box<Expr<'de>>,
}
/// `alignof` expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprAlignof<'de> {
pub ty: Box<super::ty::Type<'de>>,
}
/// `new` expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprNew<'de> {
pub global: bool,
pub placement: Option<Punctuated<'de, Expr<'de>>>,
pub ty: Box<super::ty::Type<'de>>,
pub initializer: Option<NewInitializer<'de>>,
}
/// Initializer for a `new` expression.
#[derive(Debug, Clone, PartialEq)]
pub enum NewInitializer<'de> {
/// `new T(args)`
Parens(Punctuated<'de, Expr<'de>>),
/// `new T{args}`
Braces(Vec<Expr<'de>>),
}
/// `delete` expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprDelete<'de> {
pub global: bool,
pub array: bool,
pub expr: Box<Expr<'de>>,
}
/// `throw` expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprThrow<'de> {
pub expr: Option<Box<Expr<'de>>>,
}
/// Lambda expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprLambda<'de> {
pub captures: Vec<LambdaCapture<'de>>,
pub inputs: Option<Punctuated<'de, super::item::FnArg<'de>>>,
pub return_type: Option<Box<super::ty::Type<'de>>>,
pub body: super::stmt::Block<'de>,
}
/// A lambda capture.
#[derive(Debug, Clone, PartialEq)]
pub enum LambdaCapture<'de> {
/// Default copy capture: `=`
DefaultCopy,
/// Default reference capture: `&`
DefaultRef,
/// Capture by value: `x`
ByValue(Ident<'de>),
/// Capture by reference: `&x`
ByRef(Ident<'de>),
/// Capture this: `this`
This,
/// Capture *this: `*this`
StarThis,
/// Init capture: `x = expr`
Init(Ident<'de>, Box<Expr<'de>>),
}
/// `typeid` expression.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprTypeid<'de> {
pub operand: TypeidOperand<'de>,
}
/// Operand of a `typeid` expression.
#[derive(Debug, Clone, PartialEq)]
pub enum TypeidOperand<'de> {
Type(Box<super::ty::Type<'de>>),
Expr(Box<Expr<'de>>),
}
/// Braced initializer list: `{1, 2, 3}`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprInitList<'de> {
pub elements: Vec<Expr<'de>>,
}