Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
965f820
refactor: In `alter_ty_path`, return on failed unpack
Rua May 22, 2026
0a9e31d
transpile: Split off `convert_decl_ref`
Rua Apr 26, 2026
d6e50bc
transpile: Modify enum/pointer cast methods to take `CQualTypeId` and…
Rua Apr 25, 2026
0a247b2
transpile: Add `convert_enum_constant_decl_ref`
Rua May 5, 2026
e7f1890
transpile: Add `enum_constructor_expr`, replace params accordingly
Rua May 5, 2026
22dcf7d
transpile: Cast enums only if actually needed
Rua May 5, 2026
2b3d5cd
transpile: Use correct types for enums in `convert_pre_increment`
Rua May 5, 2026
64c9f34
transpile: Add `EnumMode` to select enum output type
Rua May 5, 2026
0820ffa
transpile: Add `EnumMode::NewType` and make it the default
Rua May 5, 2026
8563120
transpile: Factor out `can_propagate_cast`
Rua May 3, 2026
971980f
transpile: Move ctx modifications down in cast handling code
Rua May 4, 2026
707b93d
transpile: Add `enums.c` snapshot test
Rua May 3, 2026
57f222e
transpile: let `can_propagate_cast` handle casting `EnumConstant` to …
Rua May 3, 2026
cbcda23
transpile: let `convert_literal` handle casting literal to enum
Rua May 4, 2026
2121b99
transpile: Propagate cast of `EnumConstant` to its own integral type
Rua May 3, 2026
7749194
transpile: In `Case` translation, do not propagate translation errors
Rua May 5, 2026
5fbe41c
transpile: Add `ExprContext::is_pattern`, error on unsupported exprs
Rua May 5, 2026
5b072e3
transpile: Do not emit casts for literals in patterns
Rua May 5, 2026
75a329a
transpile: Use `convert_expr` for patterns in `ConstantExpr`
Rua May 5, 2026
1cabaa6
transpile: Add `expr_to_pat` function for more thorough conversion
Rua May 5, 2026
677f5cb
transpile: Treat cast failure in macro expansion as expansion failure
Rua May 5, 2026
8e45ee8
transpile: Allow limited cast translation in patterns
Rua May 5, 2026
a8caac6
transpile: In `Case` translation, do not skip over initial cast
Rua May 5, 2026
9464d5c
transpile: Allow `EnumConstant` `DeclRef`s and casts to enums in patt…
Rua May 21, 2026
450bcb0
transpile: Add override type for enums in `SwitchCases`
Rua May 21, 2026
a321448
transpile: Derive `Eq` for enum newtypes
Rua May 21, 2026
a89dd28
transpile: Translate enums in `switch` as their own type instead of i…
Rua May 21, 2026
b24d32c
test
Rua May 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions c2rust-ast-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,20 @@ impl Builder {
})
}

pub fn tuple_struct_pat<Pa>(self, path: Pa, qself: Option<QSelf>, elems: Vec<Pat>) -> Pat
where
Pa: Make<Path>,
{
let path = path.make(&self);
Pat::TupleStruct(PatTupleStruct {
attrs: self.attrs,
qself,
path,
paren_token: token::Paren(self.span),
elems: punct(elems),
})
}

// Types

pub fn barefn_ty(self, decl: BareFnTyParts) -> Box<Type> {
Expand Down
35 changes: 32 additions & 3 deletions c2rust-refactor/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ macro_rules! match_or_else {
([$e:expr] $($arm_pat:pat => $arm_body:expr),*; $or_else:expr) => {
match $e {
$( $arm_pat => $arm_body, )*
ref x @ _ => $or_else(x),
_ => $or_else,
}
};

([$e:expr] $($arm_pat:pat => $arm_body:expr),*; $or_else_var:ident => $or_else:expr) => {
match $e {
$( $arm_pat => $arm_body, )*
ref $or_else_var @ _ => $or_else,
}
};
}
Expand All @@ -22,11 +29,11 @@ macro_rules! match_or_else {
macro_rules! expect {
([$e:expr] $arm_pat:pat => $arm_body:expr) => {
$crate::match_or_else!([$e] $arm_pat => $arm_body;
|x| panic!("expected {}, got {:?}", stringify!($arm_pat), x))
x => panic!("expected {}, got {:?}", stringify!($arm_pat), x))
};
([$e:expr] $($arm_pat:pat => $arm_body:expr),*) => {
$crate::match_or_else!([$e] $($arm_pat => $arm_body),*;
|x| panic!("expected one of: {}, got {:?}", stringify!($($arm_pat),*), x))
x => panic!("expected one of: {}, got {:?}", stringify!($($arm_pat),*), x))
};
}

Expand All @@ -35,6 +42,28 @@ macro_rules! unpack {
([$e:expr] $enum_:ident :: $variant:ident ( $($arg:ident),* )) => {
let ($($arg,)*) = $crate::expect!([$e] $enum_::$variant($($arg),*) => ($($arg,)*));
};

(
[$e:expr]
$enum_:ident :: $variant:ident ( $($arg:ident),* );
$or_else:expr
) => {
let ($($arg,)*) = $crate::match_or_else!(
[$e] $enum_::$variant($($arg),*) => ($($arg,)*);
$or_else
);
};

(
[$e:expr]
$enum_:ident :: $variant:ident ( $($arg:ident),* );
$or_else_var:ident => $or_else:expr
) => {
let ($($arg,)*) = $crate::match_or_else!(
[$e] $enum_::$variant($($arg),*) => ($($arg,)*);
$or_else_var => $or_else
);
};
}

#[macro_export]
Expand Down
6 changes: 5 additions & 1 deletion c2rust-refactor/src/path_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ where
_ => {}
}

unpack!([&mut t.kind] TyKind::Path(qself, path));
if !matches!(t.kind, TyKind::Path(..)) {
panic!("{qpath:?}");
}

unpack!([&mut t.kind] TyKind::Path(qself, path); return);
let (new_qself, new_path) =
self.handle_qpath(id, qself.clone(), path.clone(), qpath);
*qself = new_qself;
Expand Down
8 changes: 8 additions & 0 deletions c2rust-transpile/src/c_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,14 @@ impl TypedAstContext {
TypeOf(ty) => ty,
Paren(ty) => ty,
Auto(ty) => ty,

// As an exception, resolve typedefs in `prenamed_decls`, because they do not
// get translated as type aliases in the final code.
Typedef(decl) if self.prenamed_decls.contains_key(&decl) => match self[decl].kind {
CDeclKind::Typedef { typ: ty, .. } => ty.ctype,
_ => panic!("Typedef decl did not point to a typedef"),
},

_ => return typ,
};
self.resolve_type_id_no_typedef(ty)
Expand Down
Loading
Loading