1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
4 use crate::require_same_types;
6 use rustc::traits::{ObligationCause, ObligationCauseCode};
7 use rustc::ty::subst::Subst;
8 use rustc::ty::{self, Ty, TyCtxt};
9 use rustc_error_codes::*;
10 use rustc_errors::struct_span_err;
12 use rustc_span::symbol::Symbol;
13 use rustc_target::spec::abi::Abi;
17 fn equate_intrinsic_type<'tcx>(
19 it: &hir::ForeignItem<'_>,
22 safety: hir::Unsafety,
23 inputs: Vec<Ty<'tcx>>,
26 let def_id = tcx.hir().local_def_id(it.hir_id);
29 hir::ForeignItemKind::Fn(..) => {}
31 struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
32 .span_label(it.span, "expected a function")
38 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
40 let span = match it.kind {
41 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
49 "intrinsic has wrong number of type \
50 parameters: found {}, expected {}",
54 .span_label(span, format!("expected {} type parameter", n_tps))
59 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
66 let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
67 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
70 /// Returns `true` if the given intrinsic is unsafe to call or not.
71 pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
73 "size_of" | "min_align_of" | "needs_drop" | "caller_location" | "size_of_val"
74 | "min_align_of_val" | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow"
75 | "wrapping_add" | "wrapping_sub" | "wrapping_mul" | "saturating_add"
76 | "saturating_sub" | "rotate_left" | "rotate_right" | "ctpop" | "ctlz" | "cttz"
77 | "bswap" | "bitreverse" | "discriminant_value" | "type_id" | "likely" | "unlikely"
78 | "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name" => {
81 _ => hir::Unsafety::Unsafe,
85 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
86 /// and in libcore/intrinsics.rs
87 pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
88 let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
89 let name = it.ident.as_str();
91 let mk_va_list_ty = |mutbl| {
92 tcx.lang_items().va_list().map(|did| {
93 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
94 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
95 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
97 tcx.mk_ref(tcx.mk_region(env_region), ty::TypeAndMut { ty: va_list_ty, mutbl }),
103 let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
104 let split: Vec<&str> = name.split('_').collect();
105 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
107 //We only care about the operation here
108 let (n_tps, inputs, output) = match split[1] {
109 "cxchg" | "cxchgweak" => (
111 vec![tcx.mk_mut_ptr(param(0)), param(0), param(0)],
112 tcx.intern_tup(&[param(0), tcx.types.bool]),
114 "load" => (1, vec![tcx.mk_imm_ptr(param(0))], param(0)),
115 "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
117 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | "min" | "umax"
118 | "umin" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], param(0)),
119 "fence" | "singlethreadfence" => (0, Vec::new(), tcx.mk_unit()),
125 "unrecognized atomic operation function: `{}`",
128 .span_label(it.span, "unrecognized atomic operation")
133 (n_tps, inputs, output, hir::Unsafety::Unsafe)
134 } else if &name[..] == "abort" || &name[..] == "unreachable" {
135 (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
137 let unsafety = intrinsic_operation_unsafety(&name[..]);
138 let (n_tps, inputs, output) = match &name[..] {
139 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
140 "size_of" | "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
141 "size_of_val" | "min_align_of_val" => (
144 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
149 "rustc_peek" => (1, vec![param(0)], param(0)),
150 "caller_location" => (0, vec![], tcx.caller_location_ty()),
151 "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
152 "init" => (1, Vec::new(), param(0)),
153 "uninit" => (1, Vec::new(), param(0)),
154 "forget" => (1, vec![param(0)], tcx.mk_unit()),
155 "transmute" => (2, vec![param(0)], param(1)),
156 "move_val_init" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
158 | "prefetch_write_data"
159 | "prefetch_read_instruction"
160 | "prefetch_write_instruction" => (
163 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
168 "drop_in_place" => (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit()),
169 "needs_drop" => (1, Vec::new(), tcx.types.bool),
171 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
172 "type_id" => (1, Vec::new(), tcx.types.u64),
173 "offset" | "arith_offset" => (
176 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
179 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
181 "copy" | "copy_nonoverlapping" => (
184 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
185 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
190 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => (
193 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
194 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
199 "write_bytes" | "volatile_set_memory" => (
202 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
208 "sqrtf32" => (0, vec![tcx.types.f32], tcx.types.f32),
209 "sqrtf64" => (0, vec![tcx.types.f64], tcx.types.f64),
210 "powif32" => (0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),
211 "powif64" => (0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
212 "sinf32" => (0, vec![tcx.types.f32], tcx.types.f32),
213 "sinf64" => (0, vec![tcx.types.f64], tcx.types.f64),
214 "cosf32" => (0, vec![tcx.types.f32], tcx.types.f32),
215 "cosf64" => (0, vec![tcx.types.f64], tcx.types.f64),
216 "powf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
217 "powf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
218 "expf32" => (0, vec![tcx.types.f32], tcx.types.f32),
219 "expf64" => (0, vec![tcx.types.f64], tcx.types.f64),
220 "exp2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
221 "exp2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
222 "logf32" => (0, vec![tcx.types.f32], tcx.types.f32),
223 "logf64" => (0, vec![tcx.types.f64], tcx.types.f64),
224 "log10f32" => (0, vec![tcx.types.f32], tcx.types.f32),
225 "log10f64" => (0, vec![tcx.types.f64], tcx.types.f64),
226 "log2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
227 "log2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
228 "fmaf32" => (0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32),
229 "fmaf64" => (0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64),
230 "fabsf32" => (0, vec![tcx.types.f32], tcx.types.f32),
231 "fabsf64" => (0, vec![tcx.types.f64], tcx.types.f64),
232 "minnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
233 "minnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
234 "maxnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
235 "maxnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
236 "copysignf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
237 "copysignf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
238 "floorf32" => (0, vec![tcx.types.f32], tcx.types.f32),
239 "floorf64" => (0, vec![tcx.types.f64], tcx.types.f64),
240 "ceilf32" => (0, vec![tcx.types.f32], tcx.types.f32),
241 "ceilf64" => (0, vec![tcx.types.f64], tcx.types.f64),
242 "truncf32" => (0, vec![tcx.types.f32], tcx.types.f32),
243 "truncf64" => (0, vec![tcx.types.f64], tcx.types.f64),
244 "rintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
245 "rintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
246 "nearbyintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
247 "nearbyintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
248 "roundf32" => (0, vec![tcx.types.f32], tcx.types.f32),
249 "roundf64" => (0, vec![tcx.types.f64], tcx.types.f64),
251 "volatile_load" | "unaligned_volatile_load" => {
252 (1, vec![tcx.mk_imm_ptr(param(0))], param(0))
254 "volatile_store" | "unaligned_volatile_store" => {
255 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit())
258 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "bswap"
259 | "bitreverse" => (1, vec![param(0)], param(0)),
261 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
262 (1, vec![param(0), param(0)], tcx.intern_tup(&[param(0), tcx.types.bool]))
265 "ptr_offset_from" => {
266 (1, vec![tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0))], tcx.types.isize)
268 "unchecked_div" | "unchecked_rem" | "exact_div" => {
269 (1, vec![param(0), param(0)], param(0))
271 "unchecked_shl" | "unchecked_shr" | "rotate_left" | "rotate_right" => {
272 (1, vec![param(0), param(0)], param(0))
274 "unchecked_add" | "unchecked_sub" | "unchecked_mul" => {
275 (1, vec![param(0), param(0)], param(0))
277 "wrapping_add" | "wrapping_sub" | "wrapping_mul" => {
278 (1, vec![param(0), param(0)], param(0))
280 "saturating_add" | "saturating_sub" => (1, vec![param(0), param(0)], param(0)),
281 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
282 (1, vec![param(0), param(0)], param(0))
284 "float_to_int_approx_unchecked" => (2, vec![param(0)], param(1)),
286 "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
287 "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
288 "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
290 "discriminant_value" => (
293 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
300 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
301 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
305 hir::Unsafety::Normal,
308 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
311 "va_start" | "va_end" => match mk_va_list_ty(hir::Mutability::Mut) {
312 Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
313 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
316 "va_copy" => match mk_va_list_ty(hir::Mutability::Not) {
317 Some((va_list_ref_ty, va_list_ty)) => {
318 let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
319 (0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
321 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
324 "va_arg" => match mk_va_list_ty(hir::Mutability::Mut) {
325 Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
326 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
329 "nontemporal_store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
331 "miri_start_panic" => {
332 // FIXME - the relevant types aren't lang items,
333 // so it's not trivial to check this
342 "unrecognized intrinsic function: `{}`",
345 .span_label(it.span, "unrecognized intrinsic")
350 (n_tps, inputs, output, unsafety)
352 equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
355 /// Type-check `extern "platform-intrinsic" { ... }` functions.
356 pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
358 let name = Symbol::intern(&format!("P{}", n));
359 tcx.mk_ty_param(n, name)
362 let name = it.ident.as_str();
364 let (n_tps, inputs, output) = match &*name {
365 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
366 (2, vec![param(0), param(0)], param(1))
381 | "simd_saturating_add"
382 | "simd_saturating_sub" => (1, vec![param(0), param(0)], param(0)),
383 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" | "simd_flog2"
384 | "simd_flog10" | "simd_flog" | "simd_fabs" | "simd_floor" | "simd_ceil" => {
385 (1, vec![param(0)], param(0))
387 "simd_fpowi" => (1, vec![param(0), tcx.types.i32], param(0)),
388 "simd_fma" => (1, vec![param(0), param(0), param(0)], param(0)),
389 "simd_gather" => (3, vec![param(0), param(1), param(2)], param(0)),
390 "simd_scatter" => (3, vec![param(0), param(1), param(2)], tcx.mk_unit()),
391 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
392 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
393 "simd_cast" => (2, vec![param(0)], param(1)),
394 "simd_bitmask" => (2, vec![param(0)], param(1)),
395 "simd_select" | "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
396 "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
397 "simd_reduce_add_ordered" | "simd_reduce_mul_ordered" => {
398 (2, vec![param(0), param(1)], param(1))
400 "simd_reduce_add_unordered"
401 | "simd_reduce_mul_unordered"
407 | "simd_reduce_min_nanless"
408 | "simd_reduce_max_nanless" => (2, vec![param(0)], param(1)),
409 name if name.starts_with("simd_shuffle") => match name["simd_shuffle".len()..].parse() {
411 let params = vec![param(0), param(0), tcx.mk_array(tcx.types.u32, n)];
412 (2, params, param(1))
419 "invalid `simd_shuffle`, needs length: `{}`",
427 let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
428 tcx.sess.struct_span_err(it.span, &msg).emit();
433 equate_intrinsic_type(
437 Abi::PlatformIntrinsic,
438 hir::Unsafety::Unsafe,