Skip to content

Commit 4a4fe3b

Browse files
committed
transpile: Prefix c_ast::{BinOp, UnOp, UnTypeOp} with C to disambiguate better
1 parent a4dd364 commit 4a4fe3b

11 files changed

Lines changed: 218 additions & 258 deletions

File tree

c2rust-transpile/src/c_ast/c_expr.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ pub enum CExprKind {
2525
Literal(CQualTypeId, CLiteral),
2626

2727
/// Unary operator.
28-
Unary(CQualTypeId, UnOp, CExprId, LRValue),
28+
Unary(CQualTypeId, CUnOp, CExprId, LRValue),
2929

3030
/// Unary type operator.
31-
UnaryType(CQualTypeId, UnTypeOp, Option<CExprId>, CQualTypeId),
31+
UnaryType(CQualTypeId, CUnTypeOp, Option<CExprId>, CQualTypeId),
3232

3333
/// `offsetof` expression.
3434
OffsetOf(CQualTypeId, OffsetOfKind),
3535

3636
/// Binary operator.
3737
Binary(
3838
CQualTypeId,
39-
BinOp,
39+
CBinOp,
4040
CExprId,
4141
CExprId,
4242
Option<CQualTypeId>,
@@ -307,7 +307,7 @@ impl CastKind {
307307

308308
/// Represents a unary operator in C (6.5.3 Unary operators) and GNU C extensions
309309
#[derive(Debug, Clone, Copy)]
310-
pub enum UnOp {
310+
pub enum CUnOp {
311311
AddressOf, // &x
312312
Deref, // *x
313313
Plus, // +x
@@ -324,9 +324,9 @@ pub enum UnOp {
324324
Coawait, // [C++ Coroutines] co_await x
325325
}
326326

327-
impl UnOp {
327+
impl CUnOp {
328328
pub fn as_str(&self) -> &'static str {
329-
use UnOp::*;
329+
use CUnOp::*;
330330
match self {
331331
AddressOf => "&",
332332
Deref => "*",
@@ -351,7 +351,7 @@ impl UnOp {
351351
ast_context: &TypedAstContext,
352352
arg_type: CQualTypeId,
353353
) -> Option<CQualTypeId> {
354-
use UnOp::*;
354+
use CUnOp::*;
355355
let resolved_ty = ast_context.resolve_type(arg_type.ctype);
356356
Some(match self {
357357
// We could construct CTypeKind::Pointer here, but it is not guaranteed to have a
@@ -382,23 +382,23 @@ impl UnOp {
382382
}
383383
}
384384

385-
impl Display for UnOp {
385+
impl Display for CUnOp {
386386
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
387387
write!(f, "{}", self.as_str())
388388
}
389389
}
390390

391391
/// Represents a unary type operator in C
392392
#[derive(Debug, Clone, Copy)]
393-
pub enum UnTypeOp {
393+
pub enum CUnTypeOp {
394394
SizeOf,
395395
AlignOf,
396396
PreferredAlignOf,
397397
}
398398

399-
impl UnTypeOp {
399+
impl CUnTypeOp {
400400
pub fn as_str(&self) -> &'static str {
401-
use UnTypeOp::*;
401+
use CUnTypeOp::*;
402402
match self {
403403
SizeOf => "sizeof",
404404
AlignOf => "alignof",
@@ -407,22 +407,22 @@ impl UnTypeOp {
407407
}
408408
}
409409

410-
impl Display for UnTypeOp {
410+
impl Display for CUnTypeOp {
411411
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
412412
write!(f, "{}", self.as_str())
413413
}
414414
}
415415

416-
impl UnOp {
416+
impl CUnOp {
417417
/// Check is the operator is rendered before or after is operand.
418418
pub fn is_prefix(&self) -> bool {
419-
!matches!(*self, UnOp::PostIncrement | UnOp::PostDecrement)
419+
!matches!(*self, CUnOp::PostIncrement | CUnOp::PostDecrement)
420420
}
421421
}
422422

423423
/// Represents a binary operator in C (6.5.5 Multiplicative operators - 6.5.14 Logical OR operator)
424424
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
425-
pub enum BinOp {
425+
pub enum CBinOp {
426426
Multiply, // *
427427
Divide, // /
428428
Modulus, // %
@@ -457,9 +457,9 @@ pub enum BinOp {
457457
Comma, // ,
458458
}
459459

460-
impl BinOp {
460+
impl CBinOp {
461461
pub fn as_str(&self) -> &'static str {
462-
use BinOp::*;
462+
use CBinOp::*;
463463
match self {
464464
Multiply => "*",
465465
Divide => "/",
@@ -499,7 +499,7 @@ impl BinOp {
499499
/// Does the rust equivalent of this operator have type (T, T) -> U?
500500
#[rustfmt::skip]
501501
pub fn input_types_same(&self) -> bool {
502-
use BinOp::*;
502+
use CBinOp::*;
503503
self.all_types_same() || matches!(self,
504504
Less | Greater | LessEqual | GreaterEqual | EqualEqual | NotEqual
505505
| And | Or
@@ -512,27 +512,27 @@ impl BinOp {
512512
/// Does the rust equivalent of this operator have type (T, T) -> T?
513513
/// This ignores cases where one argument is a pointer and we translate to `.offset()`.
514514
pub fn all_types_same(&self) -> bool {
515-
use BinOp::*;
515+
use CBinOp::*;
516516
matches!(
517517
self,
518518
Multiply | Divide | Modulus | Add | Subtract | BitAnd | BitXor | BitOr
519519
)
520520
}
521521
}
522522

523-
impl Display for BinOp {
523+
impl Display for CBinOp {
524524
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
525525
write!(f, "{}", self.as_str())
526526
}
527527
}
528528

529-
impl BinOp {
529+
impl CBinOp {
530530
/// Maps compound assignment operators to operator underlying them, and returns `None` for all
531531
/// other operators.
532532
///
533533
/// For example, `AssignAdd` maps to `Some(Add)` but `Add` maps to `None`.
534-
pub fn underlying_assignment(&self) -> Option<BinOp> {
535-
use BinOp::*;
534+
pub fn underlying_assignment(&self) -> Option<CBinOp> {
535+
use CBinOp::*;
536536
Some(match *self {
537537
AssignAdd => Add,
538538
AssignSubtract => Subtract,
@@ -623,7 +623,7 @@ impl TypedAstContext {
623623

624624
/// Returns the expression inside an `__extension__` operator.
625625
pub fn resolve_extension(&self, expr_id: CExprId) -> CExprId {
626-
if let CExprKind::Unary(_, UnOp::Extension, subexpr, _) = self.index(expr_id).kind {
626+
if let CExprKind::Unary(_, CUnOp::Extension, subexpr, _) = self.index(expr_id).kind {
627627
subexpr
628628
} else {
629629
expr_id
@@ -730,11 +730,11 @@ impl TypedAstContext {
730730
ShuffleVector(..) |
731731
ConvertVector(..) |
732732
Call(..) |
733-
Unary(_, UnOp::PreIncrement, _, _) |
734-
Unary(_, UnOp::PostIncrement, _, _) |
735-
Unary(_, UnOp::PreDecrement, _, _) |
736-
Unary(_, UnOp::PostDecrement, _, _) |
737-
Binary(_, BinOp::Assign, _, _, _, _) |
733+
Unary(_, CUnOp::PreIncrement, _, _) |
734+
Unary(_, CUnOp::PostIncrement, _, _) |
735+
Unary(_, CUnOp::PreDecrement, _, _) |
736+
Unary(_, CUnOp::PostDecrement, _, _) |
737+
Binary(_, CBinOp::Assign, _, _, _, _) |
738738
InitList { .. } |
739739
ImplicitValueInit { .. } |
740740
Predefined(..) |
@@ -810,8 +810,8 @@ impl TypedAstContext {
810810
UnaryType(_, _, expr, _) => expr.map_or(true, is_const),
811811
// Not sure what a `OffsetOfKind::Variable` means.
812812
OffsetOf(_, _) => true,
813-
// `ptr::offset` (ptr `BinOp::Add`) was `const` stabilized in `1.61.0`.
814-
// `ptr::offset_from` (ptr `BinOp::Subtract`) was `const` stabilized in `1.65.0`.
813+
// `ptr::offset` (ptr `CBinOp::Add`) was `const` stabilized in `1.61.0`.
814+
// `ptr::offset_from` (ptr `CBinOp::Subtract`) was `const` stabilized in `1.65.0`.
815815
// TODO `f128` is not yet handled, as we should eventually
816816
// switch to the (currently unstable) `f128` primitive type (#1262).
817817
Binary(_, _, lhs, rhs, _, _) => is_const(lhs) && is_const(rhs),

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::c_ast::c_decl::{CDecl, CDeclId, CDeclKind};
22
use crate::c_ast::c_expr::{
3-
BinOp, CExpr, CExprId, CExprKind, CLiteral, CastKind, ConstIntExpr, Designator, IntBase,
4-
MemberKind, OffsetOfKind, UnOp, UnTypeOp,
3+
CBinOp, CExpr, CExprId, CExprKind, CLiteral, CUnOp, CUnTypeOp, CastKind, ConstIntExpr,
4+
Designator, IntBase, MemberKind, OffsetOfKind,
55
};
66
use crate::c_ast::c_stmt::{AsmOperand, CStmt, CStmtId, CStmtKind};
77
use crate::c_ast::c_type::{CQualTypeId, CType, CTypeId, CTypeKind, Qualifiers};
@@ -1376,30 +1376,30 @@ impl ConversionContext {
13761376
.expect("Expected operator")
13771377
.as_str()
13781378
{
1379-
"&" => UnOp::AddressOf,
1380-
"*" => UnOp::Deref,
1381-
"+" => UnOp::Plus,
1382-
"-" => UnOp::Negate,
1383-
"~" => UnOp::Complement,
1384-
"!" => UnOp::Not,
1379+
"&" => CUnOp::AddressOf,
1380+
"*" => CUnOp::Deref,
1381+
"+" => CUnOp::Plus,
1382+
"-" => CUnOp::Negate,
1383+
"~" => CUnOp::Complement,
1384+
"!" => CUnOp::Not,
13851385
"++" => {
13861386
if prefix {
1387-
UnOp::PreIncrement
1387+
CUnOp::PreIncrement
13881388
} else {
1389-
UnOp::PostIncrement
1389+
CUnOp::PostIncrement
13901390
}
13911391
}
13921392
"--" => {
13931393
if prefix {
1394-
UnOp::PreDecrement
1394+
CUnOp::PreDecrement
13951395
} else {
1396-
UnOp::PostDecrement
1396+
CUnOp::PostDecrement
13971397
}
13981398
}
1399-
"__real" => UnOp::Real,
1400-
"__imag" => UnOp::Imag,
1401-
"__extension__" => UnOp::Extension,
1402-
"co_await" => UnOp::Coawait,
1399+
"__real" => CUnOp::Real,
1400+
"__imag" => CUnOp::Imag,
1401+
"__extension__" => CUnOp::Extension,
1402+
"co_await" => CUnOp::Coawait,
14031403
o => panic!("Unexpected operator: {}", o),
14041404
};
14051405

@@ -1505,36 +1505,36 @@ impl ConversionContext {
15051505
.expect("Expected operator")
15061506
.as_str()
15071507
{
1508-
"*" => BinOp::Multiply,
1509-
"/" => BinOp::Divide,
1510-
"%" => BinOp::Modulus,
1511-
"+" => BinOp::Add,
1512-
"-" => BinOp::Subtract,
1513-
"<<" => BinOp::ShiftLeft,
1514-
">>" => BinOp::ShiftRight,
1515-
"<" => BinOp::Less,
1516-
">" => BinOp::Greater,
1517-
"<=" => BinOp::LessEqual,
1518-
">=" => BinOp::GreaterEqual,
1519-
"==" => BinOp::EqualEqual,
1520-
"!=" => BinOp::NotEqual,
1521-
"&" => BinOp::BitAnd,
1522-
"^" => BinOp::BitXor,
1523-
"|" => BinOp::BitOr,
1524-
"&&" => BinOp::And,
1525-
"||" => BinOp::Or,
1526-
"+=" => BinOp::AssignAdd,
1527-
"-=" => BinOp::AssignSubtract,
1528-
"*=" => BinOp::AssignMultiply,
1529-
"/=" => BinOp::AssignDivide,
1530-
"%=" => BinOp::AssignModulus,
1531-
"^=" => BinOp::AssignBitXor,
1532-
"<<=" => BinOp::AssignShiftLeft,
1533-
">>=" => BinOp::AssignShiftRight,
1534-
"|=" => BinOp::AssignBitOr,
1535-
"&=" => BinOp::AssignBitAnd,
1536-
"=" => BinOp::Assign,
1537-
"," => BinOp::Comma,
1508+
"*" => CBinOp::Multiply,
1509+
"/" => CBinOp::Divide,
1510+
"%" => CBinOp::Modulus,
1511+
"+" => CBinOp::Add,
1512+
"-" => CBinOp::Subtract,
1513+
"<<" => CBinOp::ShiftLeft,
1514+
">>" => CBinOp::ShiftRight,
1515+
"<" => CBinOp::Less,
1516+
">" => CBinOp::Greater,
1517+
"<=" => CBinOp::LessEqual,
1518+
">=" => CBinOp::GreaterEqual,
1519+
"==" => CBinOp::EqualEqual,
1520+
"!=" => CBinOp::NotEqual,
1521+
"&" => CBinOp::BitAnd,
1522+
"^" => CBinOp::BitXor,
1523+
"|" => CBinOp::BitOr,
1524+
"&&" => CBinOp::And,
1525+
"||" => CBinOp::Or,
1526+
"+=" => CBinOp::AssignAdd,
1527+
"-=" => CBinOp::AssignSubtract,
1528+
"*=" => CBinOp::AssignMultiply,
1529+
"/=" => CBinOp::AssignDivide,
1530+
"%=" => CBinOp::AssignModulus,
1531+
"^=" => CBinOp::AssignBitXor,
1532+
"<<=" => CBinOp::AssignShiftLeft,
1533+
">>=" => CBinOp::AssignShiftRight,
1534+
"|=" => CBinOp::AssignBitOr,
1535+
"&=" => CBinOp::AssignBitAnd,
1536+
"=" => CBinOp::Assign,
1537+
"," => CBinOp::Comma,
15381538
_ => unimplemented!(),
15391539
};
15401540

@@ -1639,9 +1639,9 @@ impl ConversionContext {
16391639
let kind_name =
16401640
from_value::<String>(node.extras[0].clone()).expect("expected kind");
16411641
let kind = match kind_name.as_str() {
1642-
"sizeof" => UnTypeOp::SizeOf,
1643-
"alignof" => UnTypeOp::AlignOf,
1644-
"preferredalignof" => UnTypeOp::PreferredAlignOf,
1642+
"sizeof" => CUnTypeOp::SizeOf,
1643+
"alignof" => CUnTypeOp::AlignOf,
1644+
"preferredalignof" => CUnTypeOp::PreferredAlignOf,
16451645
str => panic!("Unsupported operation: {}", str),
16461646
};
16471647

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::c_ast::c_decl::{CDecl, CDeclId, CDeclKind};
2-
use crate::c_ast::c_expr::{BinOp, CExpr, CExprId, CExprKind, UnTypeOp};
2+
use crate::c_ast::c_expr::{CBinOp, CExpr, CExprId, CExprKind, CUnTypeOp};
33
use crate::c_ast::c_stmt::{CLabelId, CStmt, CStmtId};
44
use crate::c_ast::c_type::{CQualTypeId, CType, CTypeId, CTypeKind};
55
use crate::c_ast::iterators::{immediate_children_all_types, DFNodes, NodeVisitor, SomeId};
@@ -498,7 +498,7 @@ impl TypedAstContext {
498498
} else {
499499
Some(rhs_type_id)
500500
}
501-
} else if op == BinOp::ShiftLeft || op == BinOp::ShiftRight {
501+
} else if op == CBinOp::ShiftLeft || op == CBinOp::ShiftRight {
502502
Some(lhs_type_id)
503503
} else {
504504
return;
@@ -510,11 +510,11 @@ impl TypedAstContext {
510510
),
511511
CExprKind::Paren(_ty, e) => self.ast_context.c_exprs[&e].kind.get_qual_type(),
512512
CExprKind::UnaryType(_, op, _, _) => {
513-
// All of these `UnTypeOp`s should return `size_t`.
513+
// All of these `CUnTypeOp`s should return `size_t`.
514514
let kind = match op {
515-
UnTypeOp::SizeOf => CTypeKind::Size,
516-
UnTypeOp::AlignOf => CTypeKind::Size,
517-
UnTypeOp::PreferredAlignOf => CTypeKind::Size,
515+
CUnTypeOp::SizeOf => CTypeKind::Size,
516+
CUnTypeOp::AlignOf => CTypeKind::Size,
517+
CUnTypeOp::PreferredAlignOf => CTypeKind::Size,
518518
};
519519
let ty = self
520520
.ast_context

c2rust-transpile/src/c_ast/print.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::c_ast::c_decl::{CDeclId, CDeclKind};
2-
use crate::c_ast::c_expr::{BinOp, CExprId, CExprKind, CLiteral, MemberKind, OffsetOfKind, UnOp};
2+
use crate::c_ast::c_expr::{CBinOp, CExprId, CExprKind, CLiteral, CUnOp, MemberKind, OffsetOfKind};
33
use crate::c_ast::c_stmt::{CStmtId, CStmtKind};
44
use crate::c_ast::c_type::{CQualTypeId, CTypeId, CTypeKind, Qualifiers};
55
use crate::c_ast::TypedAstContext;
@@ -291,11 +291,11 @@ impl<W: Write> Printer<W> {
291291
Ok(())
292292
}
293293

294-
pub fn print_unop(&mut self, op: &UnOp, _context: &TypedAstContext) -> Result<()> {
294+
pub fn print_unop(&mut self, op: &CUnOp, _context: &TypedAstContext) -> Result<()> {
295295
self.writer.write_all(op.as_str().as_bytes())
296296
}
297297

298-
pub fn print_binop(&mut self, op: &BinOp, _context: &TypedAstContext) -> Result<()> {
298+
pub fn print_binop(&mut self, op: &CBinOp, _context: &TypedAstContext) -> Result<()> {
299299
self.writer.write_all(op.as_str().as_bytes())
300300
}
301301

0 commit comments

Comments
 (0)