|
| 1 | +use super::{ConformMode, Objective, ObjectiveResult}; |
| 2 | +use crate::{ |
| 3 | + asg::{ |
| 4 | + Cast, CastFrom, Expr, ExprKind, IntegerBits, Type, TypeKind, TypedExpr, UnaryMathOperation, |
| 5 | + UnaryMathOperator, |
| 6 | + }, |
| 7 | + ast::{CInteger, ConformBehavior}, |
| 8 | + ir::IntegerSign, |
| 9 | + source_files::Source, |
| 10 | +}; |
| 11 | + |
| 12 | +pub fn from_size_integer<O: Objective>( |
| 13 | + expr: &Expr, |
| 14 | + from_type: &Type, |
| 15 | + mode: ConformMode, |
| 16 | + behavior: ConformBehavior, |
| 17 | + from_sign: IntegerSign, |
| 18 | + to_type: &Type, |
| 19 | + source: Source, |
| 20 | +) -> ObjectiveResult<O> { |
| 21 | + match &to_type.kind { |
| 22 | + TypeKind::Boolean => { |
| 23 | + from_size_integer_to_bool::<O>(expr, from_type, mode, behavior, source) |
| 24 | + } |
| 25 | + TypeKind::Integer(to_bits, to_sign) => { |
| 26 | + from_size_integer_to_integer::<O>(expr, mode, from_sign, *to_bits, *to_sign, source) |
| 27 | + } |
| 28 | + TypeKind::CInteger(to_c_integer, to_sign) => from_size_integer_to_c_integer::<O>( |
| 29 | + expr, |
| 30 | + mode, |
| 31 | + from_sign, |
| 32 | + *to_c_integer, |
| 33 | + *to_sign, |
| 34 | + source, |
| 35 | + ), |
| 36 | + _ => O::fail(), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn from_size_integer_to_bool<O: Objective>( |
| 41 | + expr: &Expr, |
| 42 | + from_type: &Type, |
| 43 | + mode: ConformMode, |
| 44 | + behavior: ConformBehavior, |
| 45 | + source: Source, |
| 46 | +) -> ObjectiveResult<O> { |
| 47 | + if !behavior.auto_c_integer_to_bool_conversion() && !mode.allow_lossy_integer() { |
| 48 | + return O::fail(); |
| 49 | + } |
| 50 | + |
| 51 | + O::success(|| { |
| 52 | + TypedExpr::new( |
| 53 | + TypeKind::Boolean.at(source), |
| 54 | + ExprKind::UnaryMathOperation(Box::new(UnaryMathOperation { |
| 55 | + operator: UnaryMathOperator::IsNonZero, |
| 56 | + inner: TypedExpr::new(from_type.clone(), expr.clone()), |
| 57 | + })) |
| 58 | + .at(source), |
| 59 | + ) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +pub fn from_size_integer_to_c_integer<O: Objective>( |
| 64 | + expr: &Expr, |
| 65 | + mode: ConformMode, |
| 66 | + from_sign: IntegerSign, |
| 67 | + to_c_integer: CInteger, |
| 68 | + to_sign: Option<IntegerSign>, |
| 69 | + source: Source, |
| 70 | +) -> ObjectiveResult<O> { |
| 71 | + if !(mode.allow_lossless_integer() && mode.allow_lossy_integer()) { |
| 72 | + return O::fail(); |
| 73 | + } |
| 74 | + |
| 75 | + let target_type = TypeKind::CInteger(to_c_integer, to_sign).at(source); |
| 76 | + |
| 77 | + return O::success(|| { |
| 78 | + TypedExpr::new( |
| 79 | + target_type.clone(), |
| 80 | + ExprKind::IntegerCast(Box::new(CastFrom { |
| 81 | + cast: Cast::new(target_type, expr.clone()), |
| 82 | + from_type: TypeKind::SizeInteger(from_sign).at(source), |
| 83 | + })) |
| 84 | + .at(source), |
| 85 | + ) |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +fn from_size_integer_to_integer<O: Objective>( |
| 90 | + expr: &Expr, |
| 91 | + mode: ConformMode, |
| 92 | + from_sign: IntegerSign, |
| 93 | + to_bits: IntegerBits, |
| 94 | + to_sign: IntegerSign, |
| 95 | + source: Source, |
| 96 | +) -> ObjectiveResult<O> { |
| 97 | + if !mode.allow_lossy_integer() { |
| 98 | + return O::fail(); |
| 99 | + } |
| 100 | + |
| 101 | + let target_type = TypeKind::Integer(to_bits, to_sign).at(source); |
| 102 | + |
| 103 | + O::success(|| { |
| 104 | + TypedExpr::new( |
| 105 | + target_type.clone(), |
| 106 | + ExprKind::IntegerCast(Box::new(CastFrom { |
| 107 | + cast: Cast::new(target_type, expr.clone()), |
| 108 | + from_type: TypeKind::SizeInteger(from_sign).at(source), |
| 109 | + })) |
| 110 | + .at(source), |
| 111 | + ) |
| 112 | + }) |
| 113 | +} |
0 commit comments