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::*;
10 use crate::type_of::LayoutLlvmExt;
11 use crate::abi::{LlvmType, FnTypeLlvmExt};
14 use rustc::ty::layout::{self, Align, Size, TyLayout};
15 use rustc_target::abi::call::{CastTarget, FnType, Reg};
16 use rustc_data_structures::small_c_str::SmallCStr;
17 use rustc_codegen_ssa::common::TypeKind;
24 impl PartialEq for Type {
25 fn eq(&self, other: &Self) -> bool {
30 impl fmt::Debug for Type {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 f.write_str(&llvm::build_string(|s| unsafe {
33 llvm::LLVMRustWriteTypeToString(self, s);
34 }).expect("non-UTF8 type description from LLVM"))
38 impl CodegenCx<'ll, 'tcx> {
39 crate fn type_named_struct(&self, name: &str) -> &'ll Type {
40 let name = SmallCStr::new(name);
42 llvm::LLVMStructCreateNamed(self.llcx, name.as_ptr())
46 crate fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
48 llvm::LLVMStructSetBody(ty, els.as_ptr(),
49 els.len() as c_uint, packed as Bool)
53 crate fn type_void(&self) -> &'ll Type {
55 llvm::LLVMVoidTypeInContext(self.llcx)
59 crate fn type_metadata(&self) -> &'ll Type {
61 llvm::LLVMRustMetadataTypeInContext(self.llcx)
65 ///x Creates an integer type with the given number of bits, e.g., i24
66 crate fn type_ix(&self, num_bits: u64) -> &'ll Type {
68 llvm::LLVMIntTypeInContext(self.llcx, num_bits as c_uint)
72 crate fn type_x86_mmx(&self) -> &'ll Type {
74 llvm::LLVMX86MMXTypeInContext(self.llcx)
78 crate fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
80 llvm::LLVMVectorType(ty, len as c_uint)
84 crate fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
86 let n_args = llvm::LLVMCountParamTypes(ty) as usize;
87 let mut args = Vec::with_capacity(n_args);
88 llvm::LLVMGetParamTypes(ty, args.as_mut_ptr());
94 crate fn type_bool(&self) -> &'ll Type {
98 crate fn type_int_from_ty(&self, t: ast::IntTy) -> &'ll Type {
100 ast::IntTy::Isize => self.type_isize(),
101 ast::IntTy::I8 => self.type_i8(),
102 ast::IntTy::I16 => self.type_i16(),
103 ast::IntTy::I32 => self.type_i32(),
104 ast::IntTy::I64 => self.type_i64(),
105 ast::IntTy::I128 => self.type_i128(),
109 crate fn type_uint_from_ty(&self, t: ast::UintTy) -> &'ll Type {
111 ast::UintTy::Usize => self.type_isize(),
112 ast::UintTy::U8 => self.type_i8(),
113 ast::UintTy::U16 => self.type_i16(),
114 ast::UintTy::U32 => self.type_i32(),
115 ast::UintTy::U64 => self.type_i64(),
116 ast::UintTy::U128 => self.type_i128(),
120 crate fn type_float_from_ty(&self, t: ast::FloatTy) -> &'ll Type {
122 ast::FloatTy::F32 => self.type_f32(),
123 ast::FloatTy::F64 => self.type_f64(),
127 crate fn type_pointee_for_align(&self, align: Align) -> &'ll Type {
128 // FIXME(eddyb) We could find a better approximation if ity.align < align.
129 let ity = layout::Integer::approximate_align(self, align);
130 self.type_from_integer(ity)
133 /// Return a LLVM type that has at most the required alignment,
134 /// and exactly the required size, as a best-effort padding array.
135 crate fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
136 let unit = layout::Integer::approximate_align(self, align);
137 let size = size.bytes();
138 let unit_size = unit.size().bytes();
139 assert_eq!(size % unit_size, 0);
140 self.type_array(self.type_from_integer(unit), size / unit_size)
143 crate fn type_variadic_func(
149 llvm::LLVMFunctionType(ret, args.as_ptr(),
150 args.len() as c_uint, True)
154 crate fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
156 llvm::LLVMRustArrayType(ty, len)
161 impl BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
162 fn type_i1(&self) -> &'ll Type {
164 llvm::LLVMInt1TypeInContext(self.llcx)
168 fn type_i8(&self) -> &'ll Type {
170 llvm::LLVMInt8TypeInContext(self.llcx)
175 fn type_i16(&self) -> &'ll Type {
177 llvm::LLVMInt16TypeInContext(self.llcx)
181 fn type_i32(&self) -> &'ll Type {
183 llvm::LLVMInt32TypeInContext(self.llcx)
187 fn type_i64(&self) -> &'ll Type {
189 llvm::LLVMInt64TypeInContext(self.llcx)
193 fn type_i128(&self) -> &'ll Type {
195 llvm::LLVMIntTypeInContext(self.llcx, 128)
199 fn type_isize(&self) -> &'ll Type {
203 fn type_f32(&self) -> &'ll Type {
205 llvm::LLVMFloatTypeInContext(self.llcx)
209 fn type_f64(&self) -> &'ll Type {
211 llvm::LLVMDoubleTypeInContext(self.llcx)
221 llvm::LLVMFunctionType(ret, args.as_ptr(),
222 args.len() as c_uint, False)
232 llvm::LLVMStructTypeInContext(self.llcx, els.as_ptr(),
238 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
240 llvm::LLVMRustGetTypeKind(ty).to_generic()
244 fn type_ptr_to(&self, ty: &'ll Type) -> &'ll Type {
245 assert_ne!(self.type_kind(ty), TypeKind::Function,
246 "don't call ptr_to on function types, use ptr_to_llvm_type on FnType instead");
250 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
252 llvm::LLVMGetElementType(ty)
256 fn vector_length(&self, ty: &'ll Type) -> usize {
258 llvm::LLVMGetVectorSize(ty) as usize
262 fn float_width(&self, ty: &'ll Type) -> usize {
263 match self.type_kind(ty) {
264 TypeKind::Float => 32,
265 TypeKind::Double => 64,
266 TypeKind::X86_FP80 => 80,
267 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
268 _ => bug!("llvm_float_width called on a non-float type")
272 fn int_width(&self, ty: &'ll Type) -> u64 {
274 llvm::LLVMGetIntTypeWidth(ty) as u64
278 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
284 pub fn i8_llcx(llcx: &llvm::Context) -> &Type {
286 llvm::LLVMInt8TypeInContext(llcx)
290 // Creates an integer type with the given number of bits, e.g., i24
292 llcx: &llvm::Context,
296 llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint)
300 pub fn i8p_llcx(llcx: &'ll llvm::Context) -> &'ll Type {
301 Type::i8_llcx(llcx).ptr_to()
304 fn ptr_to(&self) -> &Type {
306 llvm::LLVMPointerType(&self, 0)
312 impl LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
313 fn backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
314 layout.llvm_type(self)
316 fn immediate_backend_type(&self, layout: TyLayout<'tcx>) -> &'ll Type {
317 layout.immediate_llvm_type(self)
319 fn is_backend_immediate(&self, layout: TyLayout<'tcx>) -> bool {
320 layout.is_llvm_immediate()
322 fn is_backend_scalar_pair(&self, layout: TyLayout<'tcx>) -> bool {
323 layout.is_llvm_scalar_pair()
325 fn backend_field_index(&self, layout: TyLayout<'tcx>, index: usize) -> u64 {
326 layout.llvm_field_index(index)
328 fn scalar_pair_element_backend_type(
330 layout: TyLayout<'tcx>,
334 layout.scalar_pair_element_llvm_type(self, index, immediate)
336 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
339 fn fn_ptr_backend_type(&self, ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Type {
340 ty.ptr_to_llvm_type(self)
342 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {