pub fn bigint_to_expression(
    loc: &pt::Loc,
    n: &BigInt,
    ns: &Namespace,
    diagnostics: &mut Vec<Diagnostic>,
    resolve_to: Option<&Type>,
) -> Result<Expression, ()> {
    let bits = n.bits();

    if let Some(resolve_to) = resolve_to {
        if !resolve_to.ordered() {
            diagnostics.push(Diagnostic::error(
                *loc,
                format!("expected ‘{}’, found integer", resolve_to.to_string(ns)),
            ));
            return Err(());
        }

        let permitted_bits = if resolve_to.is_signed_int() {
            resolve_to.bits(ns) as u64 - 1
        } else {
            resolve_to.bits(ns) as u64
        };

        return if n.sign() == Sign::Minus {
            if !resolve_to.is_signed_int() {
                diagnostics.push(Diagnostic::error(
                    *loc,
                    format!(
                        "negative literal {} not allowed for unsigned type ‘{}’",
                        n,
                        resolve_to.to_string(ns)
                    ),
                ));
                Err(())
            } else if n.add(1u32).bits() > permitted_bits {
                diagnostics.push(Diagnostic::error(
                    *loc,
                    format!(
                        "literal {} is too large to fit into type ‘{}’",
                        n,
                        resolve_to.to_string(ns)
                    ),
                ));
                Err(())
            } else {
                Ok(Expression::NumberLiteral(
                    *loc,
                    resolve_to.clone(),
                    n.clone(),
                ))
            }
        } else if bits > permitted_bits {
            diagnostics.push(Diagnostic::error(
                *loc,
                format!(
                    "literal {} is too large to fit into type ‘{}’",
                    n,
                    resolve_to.to_string(ns)
                ),
            ));
            Err(())
        } else {
            Ok(Expression::NumberLiteral(
                *loc,
                resolve_to.clone(),
                n.clone(),
            ))
        };
    }

    // Return smallest type

    let int_size = if bits < 7 { 8 } else { (bits + 7) & !7 } as u16;

    if n.sign() == Sign::Minus {
        if bits > 255 {
            diagnostics.push(Diagnostic::error(*loc, format!("{} is too large", n)));
            Err(())
        } else {
            Ok(Expression::NumberLiteral(
                *loc,
                Type::Int(int_size),
                n.clone(),
            ))
        }
    } else if bits > 256 {
        diagnostics.push(Diagnostic::error(*loc, format!("{} is too large", n)));
        Err(())
    } else {
        Ok(Expression::NumberLiteral(
            *loc,
            Type::Uint(int_size),
            n.clone(),
        ))
    }
}
/// Try to convert a BigInt into a Expression::NumberLiteral. This checks for sign,
/// width and creates to correct Type.
Try to convert a BigInt into a Expression::NumberLiteral. This checks for sign,
width and creates to correct Type.
Try to convert a BigInt into a Expression :: NumberLiteral . This checks for sign width and creates to correct Type .
pub fn bigint_to_expression(
    loc: &pt::Loc,
    n: &BigInt,
    ns: &Namespace,
    diagnostics: &mut Vec<Diagnostic>,
    resolve_to: Option<&Type>,
) -> Result<Expression, ()> {
    let bits = n.bits();
    if let Some(resolve_to) = resolve_to {
        if !resolve_to.ordered() {
            diagnostics.push(Diagnostic::error(
                *loc,
                format!("expected ‘{}’, found integer", resolve_to.to_string(ns)),
            ));
            return Err(());
        }
        let permitted_bits = if resolve_to.is_signed_int() {
            resolve_to.bits(ns) as u64 - 1
        } else {
            resolve_to.bits(ns) as u64
        };
        return if n.sign() == Sign::Minus {
            if !resolve_to.is_signed_int() {
                diagnostics.push(Diagnostic::error(
                    *loc,
                    format!(
                        "negative literal {} not allowed for unsigned type ‘{}’",
                        n,
                        resolve_to.to_string(ns)
                    ),
                ));
                Err(())
            } else if n.add(1u32).bits() > permitted_bits {
                diagnostics.push(Diagnostic::error(
                    *loc,
                    format!(
                        "literal {} is too large to fit into type ‘{}’",
                        n,
                        resolve_to.to_string(ns)
                    ),
                ));
                Err(())
            } else {
                Ok(Expression::NumberLiteral(
                    *loc,
                    resolve_to.clone(),
                    n.clone(),
                ))
            }
        } else if bits > permitted_bits {
            diagnostics.push(Diagnostic::error(
                *loc,
                format!(
                    "literal {} is too large to fit into type ‘{}’",
                    n,
                    resolve_to.to_string(ns)
                ),
            ));
            Err(())
        } else {
            Ok(Expression::NumberLiteral(
                *loc,
                resolve_to.clone(),
                n.clone(),
            ))
        };
    }
    // Return smallest type
    let int_size = if bits < 7 { 8 } else { (bits + 7) & !7 } as u16;
    if n.sign() == Sign::Minus {
        if bits > 255 {
            diagnostics.push(Diagnostic::error(*loc, format!("{} is too large", n)));
            Err(())
        } else {
            Ok(Expression::NumberLiteral(
                *loc,
                Type::Int(int_size),
                n.clone(),
            ))
        }
    } else if bits > 256 {
        diagnostics.push(Diagnostic::error(*loc, format!("{} is too large", n)));
        Err(())
    } else {
        Ok(Expression::NumberLiteral(
            *loc,
            Type::Uint(int_size),
            n.clone(),
        ))
    }
}
pub fn bigint_to_expression ( loc : & pt :: Loc , n : & BigInt , ns : & Namespace , diagnostics : & mut Vec < Diagnostic > , resolve_to : Option < & Type > , ) -> Result < Expression , ( ) > { let bits = n . bits ( ) ; if let Some ( resolve_to ) = resolve_to { if ! resolve_to . ordered ( ) { diagnostics . push ( Diagnostic :: error ( * loc , format ! ( " e lve_to.to_ tring(ns) ) , ) ) ; return Err ( ( ) ) ; } let permitted_bits = if resolve_to . is_signed_int ( ) { resolve_to . bits ( ns ) as u64 - 1 } else { resolve_to . bits ( ns ) as u64 } ; return if n . sign ( ) == Sign :: Minus { if ! resolve_to . is_signed_int ( ) { diagnostics . push ( Diagnostic :: error ( * loc , format ! ( " n resolve_to to_string ( ns ) ) , ) ) ; Err ( ( ) ) } else if n . add ( 1u32 ) . bits ( ) > permitted_bits { diagnostics . push ( Diagnostic :: error ( * loc , format ! ( " n resolve_to to_string ( ns ) ) , ) ) ; Err ( ( ) ) } else { Ok ( Expression :: NumberLiteral ( * loc , resolve_to . clone ( ) , n . clone ( ) , ) ) } } else if bits > permitted_bits { diagnostics . push ( Diagnostic :: error ( * loc , format ! ( " n resolve_to to_string ( ns ) ) , ) ) ; Err ( ( ) ) } else { Ok ( Expression :: NumberLiteral ( * loc , resolve_to . clone ( ) , n . clone ( ) , ) ) } ; } // Return smallest type let int_size = if bits < 7 { 8 } else { ( bits + 7 ) & ! 7 } as u16 ; if n . sign ( ) == Sign :: Minus { if bits > 255 { diagnostics . push ( Diagnostic :: error ( * loc , format ! ( " " n ) ) ) ; Err ( ( ) ) } else { Ok ( Expression :: NumberLiteral ( * loc , Type :: Int ( int_size ) , n . clone ( ) , ) ) } } else if bits > 256 { diagnostics . push ( Diagnostic :: error ( * loc , format ! ( " " n ) ) ) ; Err ( ( ) ) } else { Ok ( Expression :: NumberLiteral ( * loc , Type :: Uint ( int_size ) , n . clone ( ) , ) ) } }
Try to convert a BigInt into a Expression::NumberLiteral.
Try to convert a BigInt into a Expression :: NumberLiteral .
loc
&pt::Loc
n
&BigInt
ns
&Namespace
diagnostics
&mut Vec<Diagnostic>
resolve_to
Option<&Type>
loc
&pt::Loc
n
&BigInt
ns
&Namespace
diagnostics
&mut Vec<Diagnostic>
resolve_to
Option<&Type>