MCPcopy Create free account
hub / github.com/IntegralPilot/rustc_codegen_jvm / cast_constant

Function cast_constant

src/oomir/interpret.rs:125–275  ·  view source on GitHub ↗

--- Updated cast_constant ---

(c: Constant, ty: Type)

Source from the content-addressed store, hash-verified

123
124pub fn cast_constant(c: Constant, ty: Type) -> Option<Constant> {
125 match (&ty, &c) {
126 // Identity casts
127 _ if Type::from_constant(&c) == ty => Some(c),
128
129 (_, Constant::I8(val)) => match ty {
130 Type::I8 => Some(Constant::I8(*val)),
131 Type::I16 => Some(Constant::I16(*val as i16)),
132 Type::I32 => Some(Constant::I32(*val as i32)),
133 Type::I64 => Some(Constant::I64(*val as i64)),
134 Type::F32 => Some(Constant::F32(*val as f32)),
135 Type::F64 => Some(Constant::F64(*val as f64)),
136 Type::Char => std::char::from_u32(*val as u32).map(Constant::Char),
137 Type::Boolean => Some(Constant::Boolean(*val != 0)),
138 _ => None,
139 },
140 (_, Constant::I16(val)) => match ty {
141 Type::I8 => Some(Constant::I8(*val as i8)),
142 Type::I16 => Some(Constant::I16(*val)),
143 Type::I32 => Some(Constant::I32(*val as i32)),
144 Type::I64 => Some(Constant::I64(*val as i64)),
145 Type::F32 => Some(Constant::F32(*val as f32)),
146 Type::F64 => Some(Constant::F64(*val as f64)),
147 Type::Char => std::char::from_u32(*val as u32).map(Constant::Char),
148 Type::Boolean => Some(Constant::Boolean(*val != 0)),
149 _ => None,
150 },
151 (_, Constant::I32(val)) => match ty {
152 Type::I8 => Some(Constant::I8(*val as i8)),
153 Type::I16 => Some(Constant::I16(*val as i16)),
154 Type::I32 => Some(Constant::I32(*val)),
155 Type::I64 => Some(Constant::I64(*val as i64)),
156 Type::F32 => Some(Constant::F32(*val as f32)),
157 Type::F64 => Some(Constant::F64(*val as f64)),
158 Type::Char => (*val)
159 .try_into()
160 .ok()
161 .and_then(std::char::from_u32)
162 .map(Constant::Char),
163 Type::Boolean => Some(Constant::Boolean(*val != 0)),
164 _ => None,
165 },
166 (_, Constant::I64(val)) => match ty {
167 Type::I8 => Some(Constant::I8(*val as i8)),
168 Type::I16 => Some(Constant::I16(*val as i16)),
169 Type::I32 => Some(Constant::I32(*val as i32)),
170 Type::I64 => Some(Constant::I64(*val)),
171 Type::F32 => Some(Constant::F32(*val as f32)),
172 Type::F64 => Some(Constant::F64(*val as f64)),
173 Type::Char => (*val)
174 .try_into()
175 .ok()
176 .and_then(std::char::from_u32)
177 .map(Constant::Char),
178 Type::Boolean => Some(Constant::Boolean(*val != 0)),
179 _ => None,
180 },
181 (_, Constant::F32(val)) => match ty {
182 Type::I8 => Some(Constant::I8(*val as i8)),

Callers 2

unify_ops_typeFunction · 0.85

Calls 5

parse_constant_to_bigintFunction · 0.85
F32Class · 0.85
F64Class · 0.85
is_zeroMethod · 0.80

Tested by

no test coverage detected