-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse.rs
648 lines (576 loc) · 20.3 KB
/
parse.rs
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
use core::ops::Deref;
use std::path::Path;
use lang_c::ast::*;
use lang_c::driver::{parse, Config, Error as ParseError};
use lang_c::span::Node;
use crate::utils::AssertSupported;
use crate::Translate;
/// TODO(document)
#[derive(Debug)]
pub enum Error {
ParseError(ParseError),
Unsupported,
}
/// TODO(document)
#[derive(Default, Clone, Copy, Debug)]
pub struct Parse;
impl<P: AsRef<Path>> Translate<P> for Parse {
type Target = TranslationUnit;
type Error = Error;
fn translate(&mut self, source: &P) -> Result<Self::Target, Self::Error> {
let config = Config::default();
let ast = parse(&config, source).map_err(Error::ParseError)?;
let unit = ast.unit;
unit.assert_supported();
Ok(unit)
}
}
impl<T: AssertSupported> AssertSupported for Node<T> {
fn assert_supported(&self) {
self.node.assert_supported();
}
}
impl<T: AssertSupported> AssertSupported for Option<T> {
fn assert_supported(&self) {
if let Some(this) = self {
this.assert_supported();
}
}
}
impl<T: AssertSupported> AssertSupported for Box<T> {
fn assert_supported(&self) {
self.deref().assert_supported();
}
}
impl<T: AssertSupported> AssertSupported for Vec<T> {
fn assert_supported(&self) {
self.iter().for_each(AssertSupported::assert_supported);
}
}
impl<T: AssertSupported> AssertSupported for [T] {
fn assert_supported(&self) {
self.iter().for_each(AssertSupported::assert_supported);
}
}
impl AssertSupported for TranslationUnit {
fn assert_supported(&self) {
self.0.assert_supported();
}
}
impl AssertSupported for ExternalDeclaration {
fn assert_supported(&self) {
match self {
Self::Declaration(decl) => {
assert!(is_valid_global_variable_declaration(&decl.node));
decl.assert_supported()
}
Self::StaticAssert(_) => panic!("ExternalDeclaration::StaticAssert"),
Self::FunctionDefinition(fdef) => fdef.assert_supported(),
}
}
}
impl AssertSupported for Declaration {
fn assert_supported(&self) {
self.specifiers.assert_supported();
self.declarators.assert_supported();
}
}
impl AssertSupported for FunctionDefinition {
fn assert_supported(&self) {
self.specifiers.assert_supported();
self.declarator.assert_supported();
assert!(self.declarations.is_empty());
self.statement.assert_supported();
}
}
impl AssertSupported for DeclarationSpecifier {
fn assert_supported(&self) {
match self {
Self::StorageClass(storage_class) => storage_class.assert_supported(),
Self::TypeSpecifier(type_specifier) => type_specifier.assert_supported(),
Self::TypeQualifier(type_qualifier) => type_qualifier.assert_supported(),
Self::Function(_) => panic!("DeclarationSpecifier::Function"),
Self::Alignment(_) => panic!("DeclarationSpecifier::Alignment"),
Self::Extension(_) => panic!("DeclarationSpecifier::Extension"),
}
}
}
impl AssertSupported for StorageClassSpecifier {
fn assert_supported(&self) {
assert_eq!(*self, Self::Typedef)
}
}
impl AssertSupported for TypeSpecifier {
fn assert_supported(&self) {
match self {
Self::Void => (),
Self::Char => (),
Self::Short => (),
Self::Int => (),
Self::Long => (),
Self::Float => (),
Self::Double => (),
Self::Signed => (),
Self::Unsigned => (),
Self::Bool => (),
Self::Complex => panic!("TypeSpecifier::Complex"),
Self::Atomic(_) => panic!("TypeSpecifier::Atomic"),
Self::Struct(struct_type) => struct_type.assert_supported(),
Self::Enum(_) => panic!("TypeSpecifier::Enum"),
Self::TypedefName(_) => (),
Self::TypeOf(_) => panic!("TypeSpecifier::TypeOf"),
Self::TS18661Float(_) => panic!("TypeSpecifier::TS18661Float"),
}
}
}
impl AssertSupported for StructType {
fn assert_supported(&self) {
self.kind.assert_supported();
self.declarations.assert_supported();
}
}
impl AssertSupported for StructDeclaration {
fn assert_supported(&self) {
match self {
Self::Field(field) => field.assert_supported(),
Self::StaticAssert(_) => panic!("StructDeclaration::StaticAssert"),
}
}
}
impl AssertSupported for StructField {
fn assert_supported(&self) {
self.specifiers.assert_supported();
self.declarators.assert_supported();
}
}
impl AssertSupported for StructDeclarator {
fn assert_supported(&self) {
self.declarator.assert_supported();
assert!(self.bit_width.is_none());
}
}
impl AssertSupported for StructKind {
fn assert_supported(&self) {
match self {
Self::Struct => (),
Self::Union => panic!("StructKind::Union"),
}
}
}
impl AssertSupported for AlignmentSpecifier {
fn assert_supported(&self) {
match self {
Self::Type(typename) => typename.assert_supported(),
Self::Constant(_) => std::panic::panic_any(AlignmentSpecifier::Constant),
}
}
}
impl AssertSupported for InitDeclarator {
fn assert_supported(&self) {
self.declarator.assert_supported();
self.initializer.assert_supported();
}
}
impl AssertSupported for Initializer {
fn assert_supported(&self) {
match self {
Self::Expression(expr) => expr.assert_supported(),
Self::List(items) => items.assert_supported(),
}
}
}
impl AssertSupported for InitializerListItem {
fn assert_supported(&self) {
assert!(self.designation.is_empty());
self.initializer.assert_supported();
}
}
impl AssertSupported for Declarator {
fn assert_supported(&self) {
self.kind.assert_supported();
self.derived.assert_supported();
assert!(self.extensions.is_empty());
}
}
impl AssertSupported for DerivedDeclarator {
fn assert_supported(&self) {
match self {
Self::Pointer(pointer_qualifiers) => pointer_qualifiers.assert_supported(),
Self::Array(array_decl) => array_decl.assert_supported(),
Self::Function(func_decl) => func_decl.assert_supported(),
// Support when K&R function has no parameter
Self::KRFunction(kr_func_decl) => assert!(kr_func_decl.is_empty()),
Self::Block(_) => panic!("DerivedDeclarator::Block"),
}
}
}
impl AssertSupported for PointerQualifier {
fn assert_supported(&self) {
match self {
Self::TypeQualifier(type_qualifier) => type_qualifier.assert_supported(),
Self::Extension(_) => panic!("PointerQualifier::Extension"),
}
}
}
impl AssertSupported for ArrayDeclarator {
fn assert_supported(&self) {
// In C99, type qualifier(e.g., const) is allowed when
// array declarator is used as function parameter.
// However, KECC does not allow this feature because
// it complicates IR generating logic.
assert!(self.qualifiers.is_empty());
self.size.assert_supported();
}
}
impl AssertSupported for TypeQualifier {
fn assert_supported(&self) {
match self {
Self::Const => (),
_ => panic!("TypeQualifier::_"),
}
}
}
impl AssertSupported for ArraySize {
fn assert_supported(&self) {
match self {
Self::VariableExpression(expr) => expr.assert_supported(),
_ => panic!("ArraySize::_"),
}
}
}
impl AssertSupported for FunctionDeclarator {
fn assert_supported(&self) {
self.parameters.assert_supported();
assert_eq!(self.ellipsis, Ellipsis::None);
}
}
impl AssertSupported for ParameterDeclaration {
fn assert_supported(&self) {
self.specifiers.assert_supported();
self.declarator.assert_supported();
assert!(self.extensions.is_empty());
}
}
impl AssertSupported for DeclaratorKind {
fn assert_supported(&self) {
match self {
Self::Abstract => (),
Self::Identifier(_) => (),
Self::Declarator(decl) => decl.assert_supported(),
}
}
}
impl AssertSupported for BlockItem {
fn assert_supported(&self) {
match self {
Self::Declaration(decl) => {
decl.node.declarators.assert_supported();
for spec in &decl.node.specifiers {
spec.assert_supported();
match &spec.node {
DeclarationSpecifier::StorageClass(_) => {
// In C, `typedef` can be declared within the function.
// However, KECC does not allow this feature
// because it complicates IR generating logic.
// For example, KECC does not allow a declaration using `typedef`
// such as `typedef int i32_t;` declaration in a function definition.
panic!("`StorageClassifier` is not allowed at `BlockItem`")
}
DeclarationSpecifier::TypeSpecifier(type_specifier) => {
if let TypeSpecifier::Struct(struct_type) = &type_specifier.node {
struct_type.node.kind.assert_supported();
// In C, `struct` can be declared within the function.
// However, KECC does not allow this feature
// because it complicates IR generating logic.
// For example, KECC allows `struct A var;` declaration
// using pre-declared `struct A`, but not `struct A { int a; } var;`
// which tries to declare `struct A` newly.
assert!(struct_type.node.declarations.is_none());
}
}
_ => (),
}
}
}
Self::StaticAssert(_) => panic!("BlockItem::StaticAssert"),
Self::Statement(stmt) => stmt.assert_supported(),
}
}
}
impl AssertSupported for ForInitializer {
fn assert_supported(&self) {
match self {
Self::Empty => (),
Self::Expression(expr) => expr.assert_supported(),
Self::Declaration(decl) => decl.assert_supported(),
Self::StaticAssert(_) => panic!("ForInitializer::StaticAssert"),
}
}
}
impl AssertSupported for Statement {
fn assert_supported(&self) {
match self {
Self::Labeled(_) => panic!("Statement::Labeled"),
Self::Compound(items) => items.assert_supported(),
Self::Expression(expr) => expr.assert_supported(),
Self::If(stmt) => {
stmt.node.condition.assert_supported();
stmt.node.then_statement.assert_supported();
stmt.node.else_statement.assert_supported();
}
Self::Switch(stmt) => stmt.assert_supported(),
Self::While(stmt) => {
stmt.node.expression.assert_supported();
stmt.node.statement.assert_supported();
}
Self::DoWhile(stmt) => {
stmt.node.statement.assert_supported();
stmt.node.expression.assert_supported();
}
Self::For(stmt) => {
stmt.node.initializer.assert_supported();
stmt.node.condition.assert_supported();
stmt.node.step.assert_supported();
stmt.node.statement.assert_supported();
}
Self::Goto(_) => panic!("Statement::Goto"),
Self::Continue | Self::Break => (),
Self::Return(expr) => expr.assert_supported(),
Self::Asm(_) => panic!("Statement::Asm"),
}
}
}
impl AssertSupported for SwitchStatement {
fn assert_supported(&self) {
self.expression.assert_supported();
let items = if let Statement::Compound(items) = &self.statement.node {
items
} else {
panic!("`Statement` in the `switch` is unsupported except `Statement::Compound`")
};
for item in items {
let stmt = if let BlockItem::Statement(stmt) = &item.node {
&stmt.node
} else {
panic!(
"`BlockItem` in the `Statement::Compound` of the `switch` \
is unsupported except `BlockItem::Statement`"
)
};
let stmt_in_label = if let Statement::Labeled(label_stmt) = stmt {
label_stmt.node.label.assert_supported();
&label_stmt.node.statement.node
} else {
panic!(
"`BlockItem::Statement` in the `Statement::Compound` of the `switch` \
is unsupported except `Statement::Labeled`"
)
};
let items = if let Statement::Compound(items) = stmt_in_label {
items
} else {
panic!("`Statement` in the `label` is unsupported except `Statement::Compound`")
};
// Split last and all the rest of the elements of the `Compound` items
let (last, items) = items
.split_last()
.unwrap_or_else(|| panic!("`Statement::Compound` has no item"));
for item in items {
match &item.node {
BlockItem::Declaration(decl) => decl.assert_supported(),
BlockItem::StaticAssert(_) => panic!("BlockItem::StaticAssert"),
BlockItem::Statement(stmt) => {
assert_ne!(
&stmt.node,
&Statement::Break,
"`BlockItem::Statement` in the `Statement::Compound` of the \
`label` should not be `Statement::Break` except the last one"
);
stmt.assert_supported();
}
}
}
// The last element of the `items` must be `Statement::Break`
let stmt = if let BlockItem::Statement(stmt) = &last.node {
&stmt.node
} else {
panic!(
"`BlockItem` in the `Statement::Compound` of the `label` \
is unsupported except `BlockItem::Statement`"
)
};
assert_eq!(
stmt,
&Statement::Break,
"the last `BlockItem` in the `Statement::Compound` \
of the `label` must be `Statement::Break`"
);
}
}
}
impl AssertSupported for Expression {
fn assert_supported(&self) {
match self {
Self::Identifier(_) => (),
Self::Constant(constant) => constant.assert_supported(),
Self::StringLiteral(_) => panic!("Expression::StringLiteral"),
Self::GenericSelection(_) => panic!("Expression::GenericSelection"),
Self::Member(member) => member.assert_supported(),
Self::Call(call) => call.assert_supported(),
Self::CompoundLiteral(_) => panic!("Expression::CompoundLiteral"),
Self::SizeOfTy(size_of_ty) => size_of_ty.assert_supported(),
Self::SizeOfVal(size_of_val) => size_of_val.assert_supported(),
Self::AlignOf(align_of) => align_of.assert_supported(),
Self::UnaryOperator(unary) => unary.assert_supported(),
Self::Cast(cast) => cast.assert_supported(),
Self::BinaryOperator(binary) => binary.assert_supported(),
Self::Conditional(conditional) => conditional.assert_supported(),
Self::Comma(exprs) => exprs.assert_supported(),
Self::OffsetOf(_) => panic!("Expression::OffsetOf"),
Self::VaArg(_) => panic!("Expression::VaArg"),
Self::Statement(_) => panic!("Expression::Statement"),
}
}
}
impl AssertSupported for Label {
fn assert_supported(&self) {
match self {
Self::Identifier(_) => panic!("Label::Identifier"),
Self::Case(_) => (),
Self::CaseRange(_) => panic!("Label::CaseRange"),
Self::Default => (),
}
}
}
impl AssertSupported for MemberExpression {
fn assert_supported(&self) {
self.expression.assert_supported();
}
}
impl AssertSupported for CallExpression {
fn assert_supported(&self) {
self.callee.assert_supported();
self.arguments.assert_supported();
}
}
impl AssertSupported for TypeName {
fn assert_supported(&self) {
self.specifiers.assert_supported();
self.declarator.assert_supported();
}
}
impl AssertSupported for SpecifierQualifier {
fn assert_supported(&self) {
match self {
Self::TypeSpecifier(type_specifier) => type_specifier.assert_supported(),
Self::TypeQualifier(type_qualifier) => type_qualifier.assert_supported(),
Self::Extension(_) => panic!("SpecifierQualifier::Extension"),
}
}
}
impl AssertSupported for UnaryOperatorExpression {
fn assert_supported(&self) {
self.operator.assert_supported();
self.operand.assert_supported();
}
}
impl AssertSupported for CastExpression {
fn assert_supported(&self) {
self.type_name.assert_supported();
self.expression.assert_supported();
}
}
impl AssertSupported for BinaryOperatorExpression {
fn assert_supported(&self) {
self.operator.assert_supported();
self.lhs.assert_supported();
self.rhs.assert_supported();
}
}
impl AssertSupported for Constant {
fn assert_supported(&self) {
match self {
Self::Integer(integer) => integer.assert_supported(),
Self::Float(float) => float.assert_supported(),
Self::Character(_) => (),
}
}
}
impl AssertSupported for Integer {
fn assert_supported(&self) {
assert!(!self.suffix.imaginary);
}
}
impl AssertSupported for Float {
fn assert_supported(&self) {
self.suffix.format.assert_supported();
assert!(!self.suffix.imaginary);
}
}
impl AssertSupported for FloatFormat {
fn assert_supported(&self) {
match self {
Self::Float => (),
Self::Double => (),
Self::LongDouble => (),
Self::TS18661Format(_) => panic!("TS18861"),
}
}
}
impl AssertSupported for UnaryOperator {
fn assert_supported(&self) {}
}
impl AssertSupported for BinaryOperator {
fn assert_supported(&self) {}
}
impl AssertSupported for ConditionalExpression {
fn assert_supported(&self) {
self.condition.assert_supported();
self.then_expression.assert_supported();
self.else_expression.assert_supported();
}
}
impl AssertSupported for SizeOfTy {
fn assert_supported(&self) {
self.0.assert_supported();
}
}
impl AssertSupported for SizeOfVal {
fn assert_supported(&self) {
self.0.assert_supported();
}
}
impl AssertSupported for AlignOf {
fn assert_supported(&self) {
self.0.assert_supported();
}
}
#[inline]
pub fn is_valid_global_variable_declaration(decl: &Declaration) -> bool {
let declarators = &decl.declarators;
declarators.iter().all(|init_decl| {
if let Some(initializer) = &init_decl.node.initializer {
is_valid_global_variable_initializer(&initializer.node)
} else {
true
}
})
}
#[inline]
pub fn is_valid_global_variable_initializer(initializer: &Initializer) -> bool {
match initializer {
Initializer::Expression(expr) => match &expr.node {
Expression::Constant(_) => true,
Expression::UnaryOperator(unary) => {
matches!(
&unary.node.operator.node,
UnaryOperator::Minus | UnaryOperator::Plus
) && matches!(&unary.node.operand.node, Expression::Constant(_))
}
_ => false,
},
Initializer::List(items) => items
.iter()
.all(|item| is_valid_global_variable_initializer(&item.node.initializer.node)),
}
}