1 pub use crate::llvm::Type;
4 use crate::llvm::{Bool, False, True};
5 use crate::context::CodegenCx;
6 use crate::value::Value;
7 use rustc_codegen_ssa::traits::*;
11 use crate::type_of::LayoutLlvmExt;
12 use crate::abi::{LlvmType, FnAbiLlvmExt};
15 use rustc::ty::layout::{self, Align, Size, TyLayout};
16 use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
17 use rustc_data_structures::small_c_str::SmallCStr;
18 use rustc_codegen_ssa::common::TypeKind;
25 impl PartialEq for Type {
26 fn eq(&self, other: &Self) -> bool {
31 impl fmt::Debug for Type {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 f.write_str(&llvm::build_string(|s| unsafe {
34 llvm::LLVMRustWriteTypeToString(self, s);
35 }).expect("non-UTF8 type description from LLVM"))
39 impl CodegenCx<'ll, 'tcx> {
40 crate fn type_named_struct(&self, name: &str) -> &'ll Type {
41 let name = SmallCStr::new(name);
43 llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
47 crate fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
49 llvm::LLVMStructSetBody(ty, els.as_ptr(),
50 els.len() as c_uint, packed as Bool)
54 crate fn type_void(&self) -> &'ll Type {
56 llvm::LLVMVoidTypeInContext(self.llcx)
60 crate fn type_metadata(&self) -> &'ll Type {
62 llvm::LLVMRustMetadataTypeInContext(self.llcx)
66 ///x Creates an integer type with the given number of bits, e.g., i24
67 crate fn type_ix(&self, num_bits: u64) -> &'ll Type {
69 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
73 crate fn type_x86_mmx(&self) -> &'ll Type {
75 llvm::LLVMX86MMXTypeInContext(self.llcx)
79 crate fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
81 llvm::LLVMVectorType(ty, len as c_uint)
85 crate fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
87 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
88 let mut args = Vec::with_capacity(n_args);
89 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
95 crate fn type_bool(&self) -> &'ll Type {
99 crate fn type_int_from_ty(&self, t: ast::IntTy) -> &'ll Type {
101 ast::IntTy::Isize => self.type_isize(),
102 ast::IntTy::I8 => self.type_i8(),
103 ast::IntTy::I16 => self.type_i16(),
104 ast::IntTy::I32 => self.type_i32(),
105 ast::IntTy::I64 => self.type_i64(),
106 ast::IntTy::I128 => self.type_i128(),
110 crate fn type_uint_from_ty(&self, t: ast::UintTy) -> &'ll Type {
112 ast::UintTy::Usize => self.type_isize(),
113 ast::UintTy::U8 => self.type_i8(),
114 ast::UintTy::U16 => self.type_i16(),
115 ast::UintTy::U32 => self.type_i32(),
116 ast::UintTy::U64 => self.type_i64(),
117 ast::UintTy::U128 => self.type_i128(),
121 crate fn type_float_from_ty(&self, t: ast::FloatTy) -> &'ll Type {
123 ast::FloatTy::F32 => self.type_f32(),
124 ast::FloatTy::F64 => self.type_f64(),
128 crate fn type_pointee_for_align(&self, align: Align) -> &'ll Type {
129 // FIXME(eddyb) We could find a better approximation if ity.align < align.
130 let ity = layout::Integer::approximate_align(self, align);
131 self.type_from_integer(ity)
134 /// Return a LLVM type that has at most the required alignment,
135 /// and exactly the required size, as a best-effort padding array.
136 crate fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
137 let unit = layout::Integer::approximate_align(self, align);
138 let size = size.bytes();
139 let unit_size = unit.size().bytes();
140 assert_eq!(size % unit_size, 0);
141 self.type_array(self.type_from_integer(unit), size / unit_size)
144 crate fn type_variadic_func(
150 llvm::LLVMFunctionType(ret, args.as_ptr(),
151 args.len() as c_uint, True)
155 crate fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
157 llvm::LLVMRustArrayType(ty, len)
162 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
163 fn type_i1(&self) -> &'ll Type {
165 llvm::LLVMInt1TypeInContext(self.llcx)
169 fn type_i8(&self) -> &'ll Type {
171 llvm::LLVMInt8TypeInContext(self.llcx)
176 fn type_i16(&self) -> &'ll Type {
178 llvm::LLVMInt16TypeInContext(self.llcx)
182 fn type_i32(&self) -> &'ll Type {
184 llvm::LLVMInt32TypeInContext(self.llcx)
188 fn type_i64(&self) -> &'ll Type {
190 llvm::LLVMInt64TypeInContext(self.llcx)
194 fn type_i128(&self) -> &'ll Type {
196 llvm::LLVMIntTypeInContext(self.llcx, 128)
200 fn type_isize(&self) -> &'ll Type {
204 fn type_f32(&self) -> &'ll Type {
206 llvm::LLVMFloatTypeInContext(self.llcx)
210 fn type_f64(&self) -> &'ll Type {
212 llvm::LLVMDoubleTypeInContext(self.llcx)
222 llvm::LLVMFunctionType(ret, args.as_ptr(),
223 args.len() as c_uint, False)
233 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
239 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
241 llvm::LLVMRustGetTypeKind(ty).to_generic()
245 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
246 assert_ne!(self.type_kind(ty), TypeKind::Function,
247 "don't call ptr_to on function types, use ptr_to_llvm_type on FnAbi instead");
251 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
253 llvm::LLVMGetElementType(ty)
257 fn vector_length(&self, ty: &'ll Type) -> usize {
259 llvm::LLVMGetVectorSize(ty) as usize
263 fn float_width(&self, ty: &'ll Type) -> usize {
264 match self.type_kind(ty) {
265 TypeKind::Float => 32,
266 TypeKind::Double => 64,
267 TypeKind::X86_FP80 => 80,
268 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
269 _ => bug!("llvm_float_width called on a non-float type")
273 fn int_width(&self, ty: &'ll Type) -> u64 {
275 llvm::LLVMGetIntTypeWidth(ty) as u64
279 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
285 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
287 llvm::LLVMInt8TypeInContext(llcx)
291 // Creates an integer type with the given number of bits, e.g., i24
293 llcx: &llvm::Context,
297 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
301 pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
302 Type::i8_llcx(llcx).ptr_to()
305 fn ptr_to(&self) -> &Type {
307 llvm::LLVMPointerType(&self, 0)
313 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
314 fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
315 layout.llvm_type(self)
317 fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
318 layout.immediate_llvm_type(self)
320 fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
321 layout.is_llvm_immediate()
323 fn is_backend_scalar_pair(&self, layout: TyLayout<'tcx>) -> bool {
324 layout.is_llvm_scalar_pair()
326 fn backend_field_index(&self, layout: TyLayout<'tcx>, index: usize) -> u64 {
327 layout.llvm_field_index(index)
329 fn scalar_pair_element_backend_type(
331 layout: TyLayout<'tcx>,
335 layout.scalar_pair_element_llvm_type(self, index, immediate)
337 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
340 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
341 fn_abi.ptr_to_llvm_type(self)
343 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {