transmutes a value to a certain type, accounting for structs.
(
bx: &mut Builder<'ll>,
cx: &CodegenCx<'ll, '_>,
a_val: &'ll Value,
ty: &'ll Type,
)
| 89 | |
| 90 | /// transmutes a value to a certain type, accounting for structs. |
| 91 | pub(crate) fn transmute_llval<'ll>( |
| 92 | bx: &mut Builder<'ll>, |
| 93 | cx: &CodegenCx<'ll, '_>, |
| 94 | a_val: &'ll Value, |
| 95 | ty: &'ll Type, |
| 96 | ) -> &'ll Value { |
| 97 | trace!("transmute_llval: transmuting `{:?}` to `{:?}`", a_val, ty); |
| 98 | unsafe { |
| 99 | let kind = LLVMRustGetTypeKind(ty); |
| 100 | match kind { |
| 101 | // structs cannot be bitcasted, so we need to do it using a bunch of extract/insert values. |
| 102 | TypeKind::Struct => { |
| 103 | let new_struct = LLVMGetUndef(ty); |
| 104 | let mut last_val = new_struct; |
| 105 | for (idx, field) in struct_type_fields(ty).into_iter().enumerate() { |
| 106 | let field_val = LLVMBuildExtractValue(bx, a_val, idx as u32, unnamed()); |
| 107 | let new_val = transmute_llval(bx, cx, field_val, field); |
| 108 | last_val = LLVMBuildInsertValue(bx, last_val, new_val, idx as u32, unnamed()); |
| 109 | } |
| 110 | last_val |
| 111 | } |
| 112 | _ => LLVMBuildBitCast(bx, a_val, ty, unnamed()), |
| 113 | } |
| 114 | } |
| 115 | } |
no test coverage detected