Skip to content

Commit 6ec0996

Browse files
committed
transpile: Move literal_matches_ty to TypedAstContext
1 parent 832d4bd commit 6ec0996

3 files changed

Lines changed: 21 additions & 18 deletions

File tree

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,23 @@ impl TypedAstContext {
13661366
}
13671367
false
13681368
}
1369+
1370+
/// Return whether the literal can be directly translated as this type.
1371+
pub fn literal_matches_ty(&self, lit: &CLiteral, ty: CQualTypeId, is_negated: bool) -> bool {
1372+
let ty_kind = &self.resolve_type(ty.ctype).kind;
1373+
match *lit {
1374+
CLiteral::Integer(value, _) | CLiteral::Character(value)
1375+
if ty_kind.is_integral_type() && !ty_kind.is_bool() =>
1376+
{
1377+
ty_kind.guaranteed_integer_in_range(value)
1378+
&& (!is_negated || ty_kind.is_signed_integral_type())
1379+
}
1380+
CLiteral::Floating(value, _) if ty_kind.is_floating_type() => {
1381+
ty_kind.guaranteed_float_in_range(value)
1382+
}
1383+
_ => false,
1384+
}
1385+
}
13691386
}
13701387

13711388
impl CommentContext {

c2rust-transpile/src/translator/literals.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,6 @@ impl<'c> Translation<'c> {
3131
Ok(mk().cast_expr(expr, target_ty))
3232
}
3333

34-
/// Return whether the literal can be directly translated as this type.
35-
pub fn literal_matches_ty(&self, lit: &CLiteral, ty: CQualTypeId, is_negated: bool) -> bool {
36-
let ty_kind = &self.ast_context.resolve_type(ty.ctype).kind;
37-
match *lit {
38-
CLiteral::Integer(value, _) | CLiteral::Character(value)
39-
if ty_kind.is_integral_type() && !ty_kind.is_bool() =>
40-
{
41-
ty_kind.guaranteed_integer_in_range(value)
42-
&& (!is_negated || ty_kind.is_signed_integral_type())
43-
}
44-
CLiteral::Floating(value, _) if ty_kind.is_floating_type() => {
45-
ty_kind.guaranteed_float_in_range(value)
46-
}
47-
_ => false,
48-
}
49-
}
50-
5134
/// Convert a C literal expression to a Rust expression
5235
pub fn convert_literal(
5336
&self,

c2rust-transpile/src/translator/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,10 @@ impl<'c> Translation<'c> {
32483248
}
32493249

32503250
if let CExprKind::Literal(_, lit) = literal_expr_kind {
3251-
if self.literal_matches_ty(lit, target_ty, is_negated) {
3251+
if self
3252+
.ast_context
3253+
.literal_matches_ty(lit, target_ty, is_negated)
3254+
{
32523255
return self.convert_expr(ctx, expr, Some(target_ty));
32533256
}
32543257
}

0 commit comments

Comments
 (0)