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