Skip to content

Commit 29626c3

Browse files
committed
transpile: Add EnumMode to select enum output type
1 parent 09ea2e0 commit 29626c3

5 files changed

Lines changed: 34 additions & 14 deletions

File tree

c2rust-transpile/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use c2rust_ast_exporter as ast_exporter;
3737

3838
use crate::build_files::{emit_build_files, get_build_dir, CrateConfig};
3939
use crate::compile_cmds::get_compile_commands;
40-
pub use crate::translator::ReplaceMode;
40+
pub use crate::translator::{EnumMode, ReplaceMode};
4141
use std::prelude::v1::Vec;
4242

4343
type PragmaVec = Vec<(&'static str, Vec<&'static str>)>;
@@ -108,6 +108,7 @@ pub struct TranspilerConfig {
108108
pub log_level: log::LevelFilter,
109109
pub edition: RustEdition,
110110
pub deny_unsafe_op_in_unsafe_fn: bool,
111+
pub enum_mode: EnumMode,
111112

112113
/// Run `c2rust-postprocess` after transpiling and potentially refactoring.
113114
pub postprocess: bool,

c2rust-transpile/src/translator/enums.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syn::Expr;
55
use crate::c_ast::CUnOp;
66
use crate::{
77
diagnostics::TranslationResult,
8-
translator::{signed_int_expr, ConvertedDecl, ExprContext, Translation},
8+
translator::{signed_int_expr, ConvertedDecl, EnumMode, ExprContext, Translation},
99
with_stmts::WithStmts,
1010
CDeclKind, CEnumConstantId, CEnumId, CExprId, CExprKind, CLiteral, CQualTypeId, CTypeKind,
1111
ConstIntExpr,
@@ -23,10 +23,15 @@ impl<'c> Translation<'c> {
2323
.borrow()
2424
.resolve_decl_name(enum_id)
2525
.expect("Enums should already be renamed");
26-
let ty = self.convert_type(integral_type.ctype)?;
27-
Ok(ConvertedDecl::Item(
28-
mk().span(span).pub_().type_item(enum_name, ty),
29-
))
26+
let integral_type_rs = self.convert_type(integral_type.ctype)?;
27+
let item = match self.tcfg.enum_mode {
28+
EnumMode::Consts => mk()
29+
.span(span)
30+
.pub_()
31+
.type_item(enum_name, integral_type_rs),
32+
};
33+
34+
Ok(ConvertedDecl::Item(item))
3035
}
3136

3237
pub fn convert_enum_constant(
@@ -163,11 +168,17 @@ impl<'c> Translation<'c> {
163168

164169
let enum_integral_type = self.enum_integral_type(enum_id);
165170
let mut val = WithStmts::new_val(val);
166-
let source_type_kind = &self.ast_context.resolve_type(source_cty.ctype).kind;
167-
let enum_integral_type_kind = &self.ast_context.resolve_type(enum_integral_type.ctype).kind;
168171

169-
if source_type_kind != enum_integral_type_kind {
170-
val = val.map(|val| self.enum_constructor_expr(enum_id, val));
172+
match self.tcfg.enum_mode {
173+
EnumMode::Consts => {
174+
let source_type_kind = &self.ast_context.resolve_type(source_cty.ctype).kind;
175+
let enum_integral_type_kind =
176+
&self.ast_context.resolve_type(enum_integral_type.ctype).kind;
177+
178+
if source_type_kind != enum_integral_type_kind {
179+
val = val.map(|val| self.enum_constructor_expr(enum_id, val));
180+
}
181+
}
171182
}
172183

173184
Ok(val)
@@ -222,7 +233,9 @@ impl<'c> Translation<'c> {
222233
.unwrap();
223234
self.add_import(enum_id, &enum_name);
224235

225-
mk().cast_expr(value, mk().ident_ty(enum_name))
236+
match self.tcfg.enum_mode {
237+
EnumMode::Consts => mk().cast_expr(value, mk().ident_ty(enum_name)),
238+
}
226239
}
227240

228241
fn is_variant_of_enum(&self, enum_id: CEnumId, enum_constant_id: CEnumConstantId) -> bool {

c2rust-transpile/src/translator/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,3 +4447,8 @@ fn neg_expr(arg: Box<Expr>) -> Box<Expr> {
44474447
fn wrapping_neg_expr(arg: Box<Expr>) -> Box<Expr> {
44484448
mk().method_call_expr(arg, "wrapping_neg", vec![])
44494449
}
4450+
4451+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4452+
pub enum EnumMode {
4453+
Consts,
4454+
}

c2rust-transpile/tests/snapshots.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use c2rust_rust_tools::RustEdition;
1010
use c2rust_rust_tools::RustEdition::Edition2021;
1111
use c2rust_rust_tools::RustEdition::Edition2024;
1212
use c2rust_transpile::renamer::RUST_KEYWORDS;
13-
use c2rust_transpile::ReplaceMode;
14-
use c2rust_transpile::TranspilerConfig;
13+
use c2rust_transpile::{EnumMode, ReplaceMode, TranspilerConfig};
1514
use itertools::Itertools;
1615

1716
fn config(edition: RustEdition) -> TranspilerConfig {
@@ -67,6 +66,7 @@ fn config(edition: RustEdition) -> TranspilerConfig {
6766
.unwrap()
6867
.to_path_buf(),
6968
),
69+
enum_mode: EnumMode::Consts,
7070
}
7171
}
7272

c2rust/src/bin/c2rust-transpile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use regex::Regex;
44
use std::{ffi::OsStr, fs, path::PathBuf};
55

66
use c2rust_rust_tools::RustEdition;
7-
use c2rust_transpile::{Diagnostic, ReplaceMode, TranspilerConfig};
7+
use c2rust_transpile::{Diagnostic, EnumMode, ReplaceMode, TranspilerConfig};
88

99
#[derive(Debug, Parser)]
1010
#[clap(
@@ -326,6 +326,7 @@ fn main() {
326326
log_level: args.log_level,
327327
edition: args.edition,
328328
deny_unsafe_op_in_unsafe_fn: args.deny_unsafe_op_in_unsafe_fn,
329+
enum_mode: EnumMode::Consts,
329330
};
330331
// binaries imply emit-build-files
331332
if !tcfg.binaries.is_empty() {

0 commit comments

Comments
 (0)