1use std::borrow::Borrow;
2use std::hash::{Hash, Hasher};
3use std::{fmt, ptr};
4
5use libc::c_uint;
6use rustc_abi::{AddressSpace, Align, Integer, Reg, Size};
7use rustc_codegen_ssa::common::TypeKind;
8use rustc_codegen_ssa::traits::*;
9use rustc_data_structures::small_c_str::SmallCStr;
10use rustc_middle::bug;
11use rustc_middle::ty::layout::TyAndLayout;
12use rustc_middle::ty::{self, Ty};
13use rustc_target::callconv::{CastTarget, FnAbi};
14
15use crate::abi::{FnAbiLlvmExt, LlvmType};
16use crate::common;
17use crate::context::{CodegenCx, GenericCx, SCx};
18use crate::llvm::{self, FALSE, TRUE, ToGeneric, ToLlvmBool, Type, Value};
19use crate::type_of::LayoutLlvmExt;
20
21impl PartialEq for Type {
22 fn eq(&self, other: &Self) -> bool {
23 ptr::eq(self, other)
24 }
25}
26
27impl Eq for Type {}
28
29impl Hash for Type {
30 fn hash<H: Hasher>(&self, state: &mut H) {
31 ptr::hash(self, state);
32 }
33}
34
35impl fmt::Debug for Type {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 f.write_str(
38 &llvm::build_string(|s| unsafe {
39 llvm::LLVMRustWriteTypeToString(self, s);
40 })
41 .expect("non-UTF8 type description from LLVM"),
42 )
43 }
44}
45
46impl<'ll> CodegenCx<'ll, '_> {}
47impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
48 pub(crate) fn type_named_struct(&self, name: &str) -> &'ll Type {
49 let name = SmallCStr::new(name);
50 unsafe { llvm::LLVMStructCreateNamed(self.llcx(), name.as_ptr()) }
51 }
52
53 pub(crate) fn set_struct_body(&self, ty: &'ll Type, els: &[&'ll Type], packed: bool) {
54 unsafe {
55 llvm::LLVMStructSetBody(ty, els.as_ptr(), els.len() as c_uint, packed.to_llvm_bool())
56 }
57 }
58 pub(crate) fn type_void(&self) -> &'ll Type {
59 unsafe { llvm::LLVMVoidTypeInContext(self.llcx()) }
60 }
61
62 pub(crate) fn type_ix(&self, num_bits: u64) -> &'ll Type {
64 llvm::LLVMIntTypeInContext(self.llcx(), num_bits as c_uint)
65 }
66
67 pub(crate) fn type_vector(&self, ty: &'ll Type, len: u64) -> &'ll Type {
68 unsafe { llvm::LLVMVectorType(ty, len as c_uint) }
69 }
70
71 pub(crate) fn type_scalable_vector(&self, ty: &'ll Type, count: u64) -> &'ll Type {
72 unsafe { llvm::LLVMScalableVectorType(ty, count as c_uint) }
73 }
74
75 pub(crate) fn add_func(&self, name: &str, ty: &'ll Type) -> &'ll Value {
76 let name = SmallCStr::new(name);
77 unsafe { llvm::LLVMAddFunction(self.llmod(), name.as_ptr(), ty) }
78 }
79
80 pub(crate) fn get_return_type(&self, ty: &'ll Type) -> &'ll Type {
81 unsafe { llvm::LLVMGetReturnType(ty) }
82 }
83
84 pub(crate) fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
85 unsafe {
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());
89 args.set_len(n_args);
90 args
91 }
92 }
93
94 pub(crate) fn func_is_variadic(&self, ty: &'ll Type) -> bool {
95 unsafe { llvm::LLVMIsFunctionVarArg(ty).is_true() }
96 }
97
98 pub(crate) fn struct_element_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
99 unsafe {
100 let n_args = llvm::LLVMCountStructElementTypes(ty) as usize;
101 let mut args = Vec::with_capacity(n_args);
102 llvm::LLVMGetStructElementTypes(ty, args.as_mut_ptr());
103 args.set_len(n_args);
104 args
105 }
106 }
107}
108impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
109 pub(crate) fn type_bool(&self) -> &'ll Type {
110 self.type_i8()
111 }
112
113 pub(crate) fn type_int_from_ty(&self, t: ty::IntTy) -> &'ll Type {
114 match t {
115 ty::IntTy::Isize => self.type_isize(),
116 ty::IntTy::I8 => self.type_i8(),
117 ty::IntTy::I16 => self.type_i16(),
118 ty::IntTy::I32 => self.type_i32(),
119 ty::IntTy::I64 => self.type_i64(),
120 ty::IntTy::I128 => self.type_i128(),
121 }
122 }
123
124 pub(crate) fn type_uint_from_ty(&self, t: ty::UintTy) -> &'ll Type {
125 match t {
126 ty::UintTy::Usize => self.type_isize(),
127 ty::UintTy::U8 => self.type_i8(),
128 ty::UintTy::U16 => self.type_i16(),
129 ty::UintTy::U32 => self.type_i32(),
130 ty::UintTy::U64 => self.type_i64(),
131 ty::UintTy::U128 => self.type_i128(),
132 }
133 }
134
135 pub(crate) fn type_float_from_ty(&self, t: ty::FloatTy) -> &'ll Type {
136 match t {
137 ty::FloatTy::F16 => self.type_f16(),
138 ty::FloatTy::F32 => self.type_f32(),
139 ty::FloatTy::F64 => self.type_f64(),
140 ty::FloatTy::F128 => self.type_f128(),
141 }
142 }
143
144 pub(crate) fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
147 let unit = Integer::approximate_align(self, align);
148 let size = size.bytes();
149 let unit_size = unit.size().bytes();
150 match (&(size % unit_size), &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(size % unit_size, 0);
151 self.type_array(self.type_from_integer(unit), size / unit_size)
152 }
153}
154
155impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
156 pub(crate) fn llcx(&self) -> &'ll llvm::Context {
157 (**self).borrow().llcx
158 }
159
160 pub(crate) fn llmod(&self) -> &'ll llvm::Module {
161 (**self).borrow().llmod
162 }
163
164 pub(crate) fn isize_ty(&self) -> &'ll Type {
165 (**self).borrow().isize_ty
166 }
167
168 pub(crate) fn type_variadic_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
169 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, TRUE) }
170 }
171
172 pub(crate) fn type_i1(&self) -> &'ll Type {
173 unsafe { llvm::LLVMInt1TypeInContext(self.llcx()) }
174 }
175
176 pub(crate) fn type_struct(&self, els: &[&'ll Type], packed: bool) -> &'ll Type {
177 unsafe {
178 llvm::LLVMStructTypeInContext(
179 self.llcx(),
180 els.as_ptr(),
181 els.len() as c_uint,
182 packed.to_llvm_bool(),
183 )
184 }
185 }
186
187 pub(crate) fn type_bf16(&self) -> &'ll Type {
188 unsafe { llvm::LLVMBFloatTypeInContext(self.llcx()) }
189 }
190}
191
192impl<'ll, CX: Borrow<SCx<'ll>>> BaseTypeCodegenMethods for GenericCx<'ll, CX> {
193 fn type_i8(&self) -> &'ll Type {
194 unsafe { llvm::LLVMInt8TypeInContext(self.llcx()) }
195 }
196
197 fn type_i16(&self) -> &'ll Type {
198 unsafe { llvm::LLVMInt16TypeInContext(self.llcx()) }
199 }
200
201 fn type_i32(&self) -> &'ll Type {
202 unsafe { llvm::LLVMInt32TypeInContext(self.llcx()) }
203 }
204
205 fn type_i64(&self) -> &'ll Type {
206 unsafe { llvm::LLVMInt64TypeInContext(self.llcx()) }
207 }
208
209 fn type_i128(&self) -> &'ll Type {
210 self.type_ix(128)
211 }
212
213 fn type_isize(&self) -> &'ll Type {
214 self.isize_ty()
215 }
216
217 fn type_f16(&self) -> &'ll Type {
218 unsafe { llvm::LLVMHalfTypeInContext(self.llcx()) }
219 }
220
221 fn type_f32(&self) -> &'ll Type {
222 unsafe { llvm::LLVMFloatTypeInContext(self.llcx()) }
223 }
224
225 fn type_f64(&self) -> &'ll Type {
226 unsafe { llvm::LLVMDoubleTypeInContext(self.llcx()) }
227 }
228
229 fn type_f128(&self) -> &'ll Type {
230 unsafe { llvm::LLVMFP128TypeInContext(self.llcx()) }
231 }
232
233 fn type_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
234 unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, FALSE) }
235 }
236
237 fn type_kind(&self, ty: &'ll Type) -> TypeKind {
238 llvm::LLVMGetTypeKind(ty).to_rust().to_generic()
239 }
240
241 fn type_ptr(&self) -> &'ll Type {
242 llvm_type_ptr(self.llcx())
243 }
244
245 fn type_ptr_ext(&self, address_space: AddressSpace) -> &'ll Type {
246 llvm_type_ptr_in_address_space(self.llcx(), address_space)
247 }
248
249 fn element_type(&self, ty: &'ll Type) -> &'ll Type {
250 match self.type_kind(ty) {
251 TypeKind::Array | TypeKind::Vector => unsafe { llvm::LLVMGetElementType(ty) },
252 TypeKind::Pointer => ::rustc_middle::util::bug::bug_fmt(format_args!("element_type is not supported for opaque pointers"))bug!("element_type is not supported for opaque pointers"),
253 other => ::rustc_middle::util::bug::bug_fmt(format_args!("element_type called on unsupported type {0:?}",
other))bug!("element_type called on unsupported type {other:?}"),
254 }
255 }
256
257 fn vector_length(&self, ty: &'ll Type) -> usize {
258 unsafe { llvm::LLVMGetVectorSize(ty) as usize }
259 }
260
261 fn float_width(&self, ty: &'ll Type) -> usize {
262 match self.type_kind(ty) {
263 TypeKind::Half => 16,
264 TypeKind::Float => 32,
265 TypeKind::Double => 64,
266 TypeKind::X86_FP80 => 80,
267 TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
268 other => ::rustc_middle::util::bug::bug_fmt(format_args!("llvm_float_width called on a non-float type {0:?}",
other))bug!("llvm_float_width called on a non-float type {other:?}"),
269 }
270 }
271
272 fn int_width(&self, ty: &'ll Type) -> u64 {
273 unsafe { llvm::LLVMGetIntTypeWidth(ty) as u64 }
274 }
275
276 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
277 common::val_ty(v)
278 }
279
280 fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
281 unsafe { llvm::LLVMArrayType2(ty, len) }
282 }
283}
284
285pub(crate) fn llvm_type_ptr(llcx: &llvm::Context) -> &Type {
286 llvm_type_ptr_in_address_space(llcx, AddressSpace::ZERO)
287}
288
289pub(crate) fn llvm_type_ptr_in_address_space<'ll>(
290 llcx: &'ll llvm::Context,
291 addr_space: AddressSpace,
292) -> &'ll Type {
293 llvm::LLVMPointerTypeInContext(llcx, addr_space.0)
294}
295
296impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
297 fn backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
298 layout.llvm_type(self)
299 }
300 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> &'ll Type {
301 layout.immediate_llvm_type(self)
302 }
303 fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
304 layout.is_llvm_immediate()
305 }
306 fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
307 layout.is_llvm_scalar_pair()
308 }
309 fn scalar_pair_element_backend_type(
310 &self,
311 layout: TyAndLayout<'tcx>,
312 index: usize,
313 immediate: bool,
314 ) -> &'ll Type {
315 layout.scalar_pair_element_llvm_type(self, index, immediate)
316 }
317 fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
318 ty.llvm_type(self)
319 }
320 fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
321 fn_abi.llvm_type(self)
322 }
323 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
324 fn_abi.ptr_to_llvm_type(self)
325 }
326 fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
327 ty.llvm_type(self)
328 }
329}
330
331impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
332 fn add_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
333 let typeid_metadata = self.create_metadata(typeid);
334 let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
335 self.global_add_metadata_node(function, llvm::MD_type, &v);
336 }
337
338 fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
339 let typeid_metadata = self.create_metadata(typeid);
340 let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
341 self.global_set_metadata_node(function, llvm::MD_type, &v);
342 }
343
344 fn add_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
345 let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
346 self.global_add_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
347 }
348
349 fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
350 let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
351 self.global_set_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
352 }
353}