Skip to content

Commit 42ac79e

Browse files
committed
make int_unsuffixed_lit to generic function so we can produce Signed Literal in IntLit
1 parent 8fc5f09 commit 42ac79e

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

c2rust-ast-builder/src/builder.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,36 @@ impl Make<Signature> for Box<FnDecl> {
500500
}
501501
}
502502

503+
impl Make<String> for i32 {
504+
fn make(self, mk: &Builder) -> String {
505+
(self as i128).make(mk)
506+
}
507+
}
508+
509+
impl Make<String> for i64 {
510+
fn make(self, mk: &Builder) -> String {
511+
(self as i128).make(mk)
512+
}
513+
}
514+
515+
impl Make<String> for u64 {
516+
fn make(self, mk: &Builder) -> String {
517+
(self as u128).make(mk)
518+
}
519+
}
520+
521+
impl Make<String> for i128 {
522+
fn make(self, _mk: &Builder) -> String {
523+
self.to_string()
524+
}
525+
}
526+
527+
impl Make<String> for u128 {
528+
fn make(self, _mk: &Builder) -> String {
529+
self.to_string()
530+
}
531+
}
532+
503533
#[derive(Clone, Debug)]
504534
pub struct Builder {
505535
// The builder holds a set of "modifiers", such as visibility and mutability. Functions for
@@ -1067,8 +1097,12 @@ impl Builder {
10671097
Lit::Int(LitInt::new(&format!("{}{}", i, ty), self.span))
10681098
}
10691099

1070-
pub fn int_unsuffixed_lit(self, i: u128) -> Lit {
1071-
Lit::Int(LitInt::new(&format!("{}", i), self.span))
1100+
pub fn int_unsuffixed_lit<S>(self, s: S) -> Lit
1101+
where
1102+
S: Make<String>,
1103+
{
1104+
let s = s.make(&self);
1105+
Lit::Int(LitInt::new(&s, self.span))
10721106
}
10731107

10741108
pub fn float_lit(self, s: &str, ty: &str) -> Lit {

c2rust-transpile/src/cfg/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,8 +1860,8 @@ impl CfgBuilder {
18601860
let pat = match branch {
18611861
Some(pat) => pat,
18621862
None => match cie {
1863-
ConstIntExpr::U(n) => mk().lit_pat(mk().int_unsuffixed_lit(n as u128)),
1864-
ConstIntExpr::I(n) => mk().lit_pat(mk().int_unsuffixed_lit(n as u128)),
1863+
ConstIntExpr::U(n) => mk().lit_pat(mk().int_unsuffixed_lit(n)),
1864+
ConstIntExpr::I(n) => mk().lit_pat(mk().int_unsuffixed_lit(n)),
18651865
},
18661866
};
18671867

c2rust-transpile/src/translator/literals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'c> Translation<'c> {
1616
base: IntBase,
1717
) -> TranslationResult<Box<Expr>> {
1818
let lit = match base {
19-
IntBase::Dec => mk().int_unsuffixed_lit(val.into()),
19+
IntBase::Dec => mk().int_unsuffixed_lit(val),
2020
IntBase::Hex => mk().float_unsuffixed_lit(&format!("0x{:x}", val)),
2121
IntBase::Oct => mk().float_unsuffixed_lit(&format!("0o{:o}", val)),
2222
};

c2rust-transpile/src/translator/structs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a> Translation<'a> {
313313
} => {
314314
let ty = mk().array_ty(
315315
mk().ident_ty("u8"),
316-
mk().lit_expr(mk().int_unsuffixed_lit(bytes.into())),
316+
mk().lit_expr(mk().int_unsuffixed_lit(bytes)),
317317
);
318318
let mut field = mk();
319319
let field_attrs = attrs.iter().map(|attr| {
@@ -340,7 +340,7 @@ impl<'a> Translation<'a> {
340340
let field_name = next_padding_field();
341341
let ty = mk().array_ty(
342342
mk().ident_ty("u8"),
343-
mk().lit_expr(mk().int_unsuffixed_lit(bytes.into())),
343+
mk().lit_expr(mk().int_unsuffixed_lit(bytes)),
344344
);
345345

346346
// Mark it with `#[bitfield(padding)]`
@@ -442,7 +442,7 @@ impl<'a> Translation<'a> {
442442
} => {
443443
let array_expr = mk().repeat_expr(
444444
mk().lit_expr(mk().int_unsuffixed_lit(0)),
445-
mk().lit_expr(mk().int_unsuffixed_lit(bytes.into())),
445+
mk().lit_expr(mk().int_unsuffixed_lit(bytes)),
446446
);
447447
let field = mk().field(field_name, array_expr);
448448

@@ -452,7 +452,7 @@ impl<'a> Translation<'a> {
452452
let field_name = next_padding_field();
453453
let array_expr = mk().repeat_expr(
454454
mk().lit_expr(mk().int_unsuffixed_lit(0)),
455-
mk().lit_expr(mk().int_unsuffixed_lit(bytes.into())),
455+
mk().lit_expr(mk().int_unsuffixed_lit(bytes)),
456456
);
457457
let field = mk().field(field_name, array_expr);
458458

@@ -612,7 +612,7 @@ impl<'a> Translation<'a> {
612612
} => {
613613
let array_expr = mk().repeat_expr(
614614
mk().lit_expr(mk().int_unsuffixed_lit(0)),
615-
mk().lit_expr(mk().int_unsuffixed_lit(bytes.into())),
615+
mk().lit_expr(mk().int_unsuffixed_lit(bytes)),
616616
);
617617
let field = mk().field(field_name, array_expr);
618618

@@ -622,7 +622,7 @@ impl<'a> Translation<'a> {
622622
let field_name = next_padding_field();
623623
let array_expr = mk().repeat_expr(
624624
mk().lit_expr(mk().int_unsuffixed_lit(0)),
625-
mk().lit_expr(mk().int_unsuffixed_lit(bytes.into())),
625+
mk().lit_expr(mk().int_unsuffixed_lit(bytes)),
626626
);
627627
let field = mk().field(field_name, array_expr);
628628

0 commit comments

Comments
 (0)