@@ -139,6 +139,7 @@ pub(crate) mod ast_ty {
139139 use crate :: ir:: function:: FunctionSig ;
140140 use crate :: ir:: layout:: Layout ;
141141 use crate :: ir:: ty:: { FloatKind , IntKind } ;
142+ use crate :: ir:: var:: LiteralRadix ;
142143 use crate :: RustTarget ;
143144 use proc_macro2:: TokenStream ;
144145 use std:: str:: FromStr ;
@@ -291,16 +292,70 @@ pub(crate) mod ast_ty {
291292 }
292293 }
293294
294- pub ( crate ) fn int_expr ( val : i64 ) -> TokenStream {
295+ pub ( crate ) fn int_expr (
296+ val : i64 ,
297+ radix : Option < & LiteralRadix > ,
298+ ) -> TokenStream {
295299 // Don't use quote! { #val } because that adds the type suffix.
296- let val = proc_macro2:: Literal :: i64_unsuffixed ( val) ;
297- quote ! ( #val)
300+ let sign = if val. is_negative ( ) { "-" } else { "" } ;
301+ if let Some ( radix) = radix {
302+ match radix {
303+ LiteralRadix :: Decimal => {
304+ let val = proc_macro2:: Literal :: i64_unsuffixed ( val) ;
305+ quote ! ( #val)
306+ }
307+ LiteralRadix :: Binary => {
308+ let val = val. unsigned_abs ( ) ;
309+ let val = format ! ( "{sign}0b{val:b}" ) ;
310+ TokenStream :: from_str ( val. as_str ( ) ) . unwrap ( )
311+ }
312+ LiteralRadix :: Octal => {
313+ let val = val. unsigned_abs ( ) ;
314+ let val = format ! ( "{sign}0o{val:o}" ) ;
315+ TokenStream :: from_str ( val. as_str ( ) ) . unwrap ( )
316+ }
317+ LiteralRadix :: Hexadecimal => {
318+ let val = val. unsigned_abs ( ) ;
319+ let val = format ! ( "{sign}0x{val:x}" ) ;
320+ TokenStream :: from_str ( val. as_str ( ) ) . unwrap ( )
321+ }
322+ }
323+ } else {
324+ // same as for Decimal
325+ let val = proc_macro2:: Literal :: i64_unsuffixed ( val) ;
326+ quote ! ( #val)
327+ }
298328 }
299329
300- pub ( crate ) fn uint_expr ( val : u64 ) -> TokenStream {
330+ pub ( crate ) fn uint_expr (
331+ val : u64 ,
332+ radix : Option < & LiteralRadix > ,
333+ ) -> TokenStream {
301334 // Don't use quote! { #val } because that adds the type suffix.
302- let val = proc_macro2:: Literal :: u64_unsuffixed ( val) ;
303- quote ! ( #val)
335+ if let Some ( radix) = radix {
336+ match radix {
337+ LiteralRadix :: Decimal => {
338+ let val = proc_macro2:: Literal :: u64_unsuffixed ( val) ;
339+ quote ! ( #val)
340+ }
341+ LiteralRadix :: Binary => {
342+ let val = format ! ( "0b{val:b}" ) ;
343+ TokenStream :: from_str ( val. as_str ( ) ) . unwrap ( )
344+ }
345+ LiteralRadix :: Octal => {
346+ let val = format ! ( "0o{val:o}" ) ;
347+ TokenStream :: from_str ( val. as_str ( ) ) . unwrap ( )
348+ }
349+ LiteralRadix :: Hexadecimal => {
350+ let val = format ! ( "0x{val:x}" ) ;
351+ TokenStream :: from_str ( val. as_str ( ) ) . unwrap ( )
352+ }
353+ }
354+ } else {
355+ // same as for Decimal
356+ let val = proc_macro2:: Literal :: u64_unsuffixed ( val) ;
357+ quote ! ( #val)
358+ }
304359 }
305360
306361 pub ( crate ) fn cstr_expr ( mut string : String ) -> TokenStream {
0 commit comments