Skip to content

Commit 9db4a94

Browse files
committed
transpile: Remove EnumMode again
1 parent 254bda3 commit 9db4a94

5 files changed

Lines changed: 19 additions & 65 deletions

File tree

c2rust-transpile/src/lib.rs

Lines changed: 1 addition & 2 deletions
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::{EnumMode, ReplaceMode};
40+
pub use crate::translator::ReplaceMode;
4141
use std::prelude::v1::Vec;
4242

4343
type PragmaVec = Vec<(&'static str, Vec<&'static str>)>;
@@ -108,7 +108,6 @@ 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,
112111

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

c2rust-transpile/src/translator/enums.rs

Lines changed: 16 additions & 53 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, EnumMode, ExprContext, Translation},
8+
translator::{signed_int_expr, ConvertedDecl, ExprContext, Translation},
99
with_stmts::WithStmts,
1010
CDeclKind, CEnumConstantId, CEnumId, CExprId, CExprKind, CLiteral, CQualTypeId, CTypeKind,
1111
ConstIntExpr,
@@ -24,21 +24,13 @@ impl<'c> Translation<'c> {
2424
.resolve_decl_name(enum_id)
2525
.expect("Enums should already be renamed");
2626
let integral_type_rs = self.convert_type(integral_type.ctype)?;
27-
let item = match self.tcfg.enum_mode {
28-
EnumMode::NewType => {
29-
let field = mk().pub_().enum_field(integral_type_rs);
30-
mk().span(span)
31-
.call_attr("derive", vec!["Clone", "Copy"])
32-
.call_attr("repr", vec!["transparent"])
33-
.pub_()
34-
.struct_item(enum_name, vec![field], true)
35-
}
36-
37-
EnumMode::Consts => mk()
38-
.span(span)
39-
.pub_()
40-
.type_item(enum_name, integral_type_rs),
41-
};
27+
let field = mk().pub_().enum_field(integral_type_rs);
28+
let item = mk()
29+
.span(span)
30+
.call_attr("derive", vec!["Clone", "Copy"])
31+
.call_attr("repr", vec!["transparent"])
32+
.pub_()
33+
.struct_item(enum_name, vec![field], true);
4234

4335
Ok(ConvertedDecl::Item(item))
4436
}
@@ -99,11 +91,8 @@ impl<'c> Translation<'c> {
9991
target_cty: CQualTypeId,
10092
mut val: Box<Expr>,
10193
) -> TranslationResult<WithStmts<Box<Expr>>> {
102-
match self.tcfg.enum_mode {
103-
// First extract the enum's inner type...
104-
EnumMode::NewType => val = self.integer_from_enum(val),
105-
EnumMode::Consts => {}
106-
}
94+
// First extract the enum's inner type...
95+
val = self.integer_from_enum(val);
10796

10897
// Cast from the enum's integral type to the expected integral type.
10998
let source_cty = self.enum_integral_type(enum_id);
@@ -120,10 +109,7 @@ impl<'c> Translation<'c> {
120109

121110
/// Gets the inner integral value of an enum value.
122111
pub fn integer_from_enum(&self, val: Box<Expr>) -> Box<Expr> {
123-
match self.tcfg.enum_mode {
124-
EnumMode::NewType => mk().anon_field_expr(val, 0),
125-
EnumMode::Consts => val,
126-
}
112+
mk().anon_field_expr(val, 0)
127113
}
128114

129115
/// Translates a cast where the target type is an `enum` type.
@@ -181,35 +167,15 @@ impl<'c> Translation<'c> {
181167
return Ok(WithStmts::new_val(val));
182168
}
183169

184-
match self.tcfg.enum_mode {
185-
// Enum-to-enum casts need to be translated via the inner value as an intermediate.
186-
EnumMode::NewType => val = self.integer_from_enum(val),
187-
EnumMode::Consts => {}
188-
}
189-
170+
// Enum-to-enum casts need to be translated via the inner value as an intermediate.
171+
val = self.integer_from_enum(val);
190172
source_cty = self.enum_integral_type(source_enum_id);
191173
}
192174

193175
let enum_integral_type = self.enum_integral_type(enum_id);
194176
let mut val = WithStmts::new_val(val);
195-
196-
match self.tcfg.enum_mode {
197-
EnumMode::NewType => {
198-
val =
199-
self.convert_cast(ctx, source_cty, enum_integral_type, val, None, None, None)?;
200-
val = val.map(|val| self.enum_constructor_expr(enum_id, val));
201-
}
202-
203-
EnumMode::Consts => {
204-
let source_type_kind = &self.ast_context.resolve_type(source_cty.ctype).kind;
205-
let enum_integral_type_kind =
206-
&self.ast_context.resolve_type(enum_integral_type.ctype).kind;
207-
208-
if source_type_kind != enum_integral_type_kind {
209-
val = val.map(|val| self.enum_constructor_expr(enum_id, val));
210-
}
211-
}
212-
}
177+
val = self.convert_cast(ctx, source_cty, enum_integral_type, val, None, None, None)?;
178+
val = val.map(|val| self.enum_constructor_expr(enum_id, val));
213179

214180
Ok(val)
215181
}
@@ -263,10 +229,7 @@ impl<'c> Translation<'c> {
263229
.unwrap();
264230
self.add_import(enum_id, &enum_name);
265231

266-
match self.tcfg.enum_mode {
267-
EnumMode::NewType => mk().call_expr(mk().ident_expr(enum_name), vec![value]),
268-
EnumMode::Consts => mk().cast_expr(value, mk().ident_ty(enum_name)),
269-
}
232+
mk().call_expr(mk().ident_expr(enum_name), vec![value])
270233
}
271234

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

c2rust-transpile/src/translator/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,9 +4363,3 @@ fn neg_expr(arg: Box<Expr>) -> Box<Expr> {
43634363
fn wrapping_neg_expr(arg: Box<Expr>) -> Box<Expr> {
43644364
mk().method_call_expr(arg, "wrapping_neg", vec![])
43654365
}
4366-
4367-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4368-
pub enum EnumMode {
4369-
NewType,
4370-
Consts,
4371-
}

c2rust-transpile/tests/snapshots.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use c2rust_rust_tools::RustEdition;
1111
use c2rust_rust_tools::RustEdition::Edition2021;
1212
use c2rust_rust_tools::RustEdition::Edition2024;
1313
use c2rust_transpile::renamer::RUST_KEYWORDS;
14-
use c2rust_transpile::{EnumMode, ReplaceMode, TranspilerConfig};
14+
use c2rust_transpile::{ReplaceMode, TranspilerConfig};
1515
use itertools::Itertools;
1616

1717
fn config(edition: RustEdition) -> TranspilerConfig {
@@ -67,7 +67,6 @@ fn config(edition: RustEdition) -> TranspilerConfig {
6767
.unwrap()
6868
.to_path_buf(),
6969
),
70-
enum_mode: EnumMode::NewType,
7170
}
7271
}
7372

c2rust/src/bin/c2rust-transpile.rs

Lines changed: 1 addition & 2 deletions
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, EnumMode, ReplaceMode, TranspilerConfig};
7+
use c2rust_transpile::{Diagnostic, ReplaceMode, TranspilerConfig};
88

99
#[derive(Debug, Parser)]
1010
#[clap(
@@ -326,7 +326,6 @@ 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::NewType,
330329
};
331330
// binaries imply emit-build-files
332331
if !tcfg.binaries.is_empty() {

0 commit comments

Comments
 (0)