1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
4 use crate::require_same_types;
5 use rustc::traits::{ObligationCause, ObligationCauseCode};
6 use rustc::ty::subst::Subst;
7 use rustc::ty::{self, Ty, TyCtxt};
9 use rustc_span::symbol::Symbol;
10 use rustc_target::spec::abi::Abi;
14 use rustc_error_codes::*;
18 fn equate_intrinsic_type<'tcx>(
20 it: &hir::ForeignItem<'_>,
23 safety: hir::Unsafety,
24 inputs: Vec<Ty<'tcx>>,
27 let def_id = tcx.hir().local_def_id(it.hir_id);
30 hir::ForeignItemKind::Fn(..) => {}
32 struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
33 .span_label(it.span, "expected a function")
39 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
41 let span = match it.kind {
42 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
50 "intrinsic has wrong number of type \
51 parameters: found {}, expected {}",
55 .span_label(span, format!("expected {} type parameter", n_tps))
60 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
67 let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
68 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
71 /// Returns `true` if the given intrinsic is unsafe to call or not.
72 pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
74 "size_of" | "min_align_of" | "needs_drop" | "caller_location" | "size_of_val"
75 | "min_align_of_val" | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow"
76 | "wrapping_add" | "wrapping_sub" | "wrapping_mul" | "saturating_add"
77 | "saturating_sub" | "rotate_left" | "rotate_right" | "ctpop" | "ctlz" | "cttz"
78 | "bswap" | "bitreverse" | "discriminant_value" | "type_id" | "likely" | "unlikely"
79 | "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name" => {
82 _ => hir::Unsafety::Unsafe,
86 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
87 /// and in libcore/intrinsics.rs
88 pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
89 let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
90 let name = it.ident.as_str();
92 let mk_va_list_ty = |mutbl| {
93 tcx.lang_items().va_list().map(|did| {
94 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
95 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
96 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
98 tcx.mk_ref(tcx.mk_region(env_region), ty::TypeAndMut { ty: va_list_ty, mutbl }),
104 let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
105 let split: Vec<&str> = name.split('_').collect();
106 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
108 //We only care about the operation here
109 let (n_tps, inputs, output) = match split[1] {
110 "cxchg" | "cxchgweak" => (
112 vec![tcx.mk_mut_ptr(param(0)), param(0), param(0)],
113 tcx.intern_tup(&[param(0), tcx.types.bool]),
115 "load" => (1, vec![tcx.mk_imm_ptr(param(0))], param(0)),
116 "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
118 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | "min" | "umax"
119 | "umin" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], param(0)),
120 "fence" | "singlethreadfence" => (0, Vec::new(), tcx.mk_unit()),
126 "unrecognized atomic operation function: `{}`",
129 .span_label(it.span, "unrecognized atomic operation")
134 (n_tps, inputs, output, hir::Unsafety::Unsafe)
135 } else if &name[..] == "abort" || &name[..] == "unreachable" {
136 (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
138 let unsafety = intrinsic_operation_unsafety(&name[..]);
139 let (n_tps, inputs, output) = match &name[..] {
140 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
141 "size_of" | "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
142 "size_of_val" | "min_align_of_val" => (
145 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
150 "rustc_peek" => (1, vec![param(0)], param(0)),
151 "caller_location" => (0, vec![], tcx.caller_location_ty()),
152 "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
153 "init" => (1, Vec::new(), param(0)),
154 "uninit" => (1, Vec::new(), param(0)),
155 "forget" => (1, vec![param(0)], tcx.mk_unit()),
156 "transmute" => (2, vec![param(0)], param(1)),
157 "move_val_init" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
159 | "prefetch_write_data"
160 | "prefetch_read_instruction"
161 | "prefetch_write_instruction" => (
164 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
169 "drop_in_place" => (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit()),
170 "needs_drop" => (1, Vec::new(), tcx.types.bool),
172 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
173 "type_id" => (1, Vec::new(), tcx.types.u64),
174 "offset" | "arith_offset" => (
177 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
180 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
182 "copy" | "copy_nonoverlapping" => (
185 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
186 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
191 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => (
194 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
195 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
200 "write_bytes" | "volatile_set_memory" => (
203 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
209 "sqrtf32" => (0, vec![tcx.types.f32], tcx.types.f32),
210 "sqrtf64" => (0, vec![tcx.types.f64], tcx.types.f64),
211 "powif32" => (0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),
212 "powif64" => (0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
213 "sinf32" => (0, vec![tcx.types.f32], tcx.types.f32),
214 "sinf64" => (0, vec![tcx.types.f64], tcx.types.f64),
215 "cosf32" => (0, vec![tcx.types.f32], tcx.types.f32),
216 "cosf64" => (0, vec![tcx.types.f64], tcx.types.f64),
217 "powf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
218 "powf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
219 "expf32" => (0, vec![tcx.types.f32], tcx.types.f32),
220 "expf64" => (0, vec![tcx.types.f64], tcx.types.f64),
221 "exp2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
222 "exp2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
223 "logf32" => (0, vec![tcx.types.f32], tcx.types.f32),
224 "logf64" => (0, vec![tcx.types.f64], tcx.types.f64),
225 "log10f32" => (0, vec![tcx.types.f32], tcx.types.f32),
226 "log10f64" => (0, vec![tcx.types.f64], tcx.types.f64),
227 "log2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
228 "log2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
229 "fmaf32" => (0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32),
230 "fmaf64" => (0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64),
231 "fabsf32" => (0, vec![tcx.types.f32], tcx.types.f32),
232 "fabsf64" => (0, vec![tcx.types.f64], tcx.types.f64),
233 "minnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
234 "minnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
235 "maxnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
236 "maxnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
237 "copysignf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
238 "copysignf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
239 "floorf32" => (0, vec![tcx.types.f32], tcx.types.f32),
240 "floorf64" => (0, vec![tcx.types.f64], tcx.types.f64),
241 "ceilf32" => (0, vec![tcx.types.f32], tcx.types.f32),
242 "ceilf64" => (0, vec![tcx.types.f64], tcx.types.f64),
243 "truncf32" => (0, vec![tcx.types.f32], tcx.types.f32),
244 "truncf64" => (0, vec![tcx.types.f64], tcx.types.f64),
245 "rintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
246 "rintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
247 "nearbyintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
248 "nearbyintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
249 "roundf32" => (0, vec![tcx.types.f32], tcx.types.f32),
250 "roundf64" => (0, vec![tcx.types.f64], tcx.types.f64),
252 "volatile_load" | "unaligned_volatile_load" => {
253 (1, vec![tcx.mk_imm_ptr(param(0))], param(0))
255 "volatile_store" | "unaligned_volatile_store" => {
256 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit())
259 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "bswap"
260 | "bitreverse" => (1, vec![param(0)], param(0)),
262 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
263 (1, vec![param(0), param(0)], tcx.intern_tup(&[param(0), tcx.types.bool]))
266 "ptr_offset_from" => {
267 (1, vec![tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0))], tcx.types.isize)
269 "unchecked_div" | "unchecked_rem" | "exact_div" => {
270 (1, vec![param(0), param(0)], param(0))
272 "unchecked_shl" | "unchecked_shr" | "rotate_left" | "rotate_right" => {
273 (1, vec![param(0), param(0)], param(0))
275 "unchecked_add" | "unchecked_sub" | "unchecked_mul" => {
276 (1, vec![param(0), param(0)], param(0))
278 "wrapping_add" | "wrapping_sub" | "wrapping_mul" => {
279 (1, vec![param(0), param(0)], param(0))
281 "saturating_add" | "saturating_sub" => (1, vec![param(0), param(0)], param(0)),
282 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
283 (1, vec![param(0), param(0)], param(0))
285 "float_to_int_approx_unchecked" => (2, vec![param(0)], param(1)),
287 "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
288 "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
289 "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
291 "discriminant_value" => (
294 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
301 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
302 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
306 hir::Unsafety::Normal,
309 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
312 "va_start" | "va_end" => match mk_va_list_ty(hir::Mutability::Mut) {
313 Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
314 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
317 "va_copy" => match mk_va_list_ty(hir::Mutability::Not) {
318 Some((va_list_ref_ty, va_list_ty)) => {
319 let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
320 (0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
322 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
325 "va_arg" => match mk_va_list_ty(hir::Mutability::Mut) {
326 Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
327 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
330 "nontemporal_store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
332 "miri_start_panic" => {
333 // FIXME - the relevant types aren't lang items,
334 // so it's not trivial to check this
343 "unrecognized intrinsic function: `{}`",
346 .span_label(it.span, "unrecognized intrinsic")
351 (n_tps, inputs, output, unsafety)
353 equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
356 /// Type-check `extern "platform-intrinsic" { ... }` functions.
357 pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
359 let name = Symbol::intern(&format!("P{}", n));
360 tcx.mk_ty_param(n, name)
363 let name = it.ident.as_str();
365 let (n_tps, inputs, output) = match &*name {
366 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
367 (2, vec![param(0), param(0)], param(1))
382 | "simd_saturating_add"
383 | "simd_saturating_sub" => (1, vec![param(0), param(0)], param(0)),
384 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" | "simd_flog2"
385 | "simd_flog10" | "simd_flog" | "simd_fabs" | "simd_floor" | "simd_ceil" => {
386 (1, vec![param(0)], param(0))
388 "simd_fpowi" => (1, vec![param(0), tcx.types.i32], param(0)),
389 "simd_fma" => (1, vec![param(0), param(0), param(0)], param(0)),
390 "simd_gather" => (3, vec![param(0), param(1), param(2)], param(0)),
391 "simd_scatter" => (3, vec![param(0), param(1), param(2)], tcx.mk_unit()),
392 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
393 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
394 "simd_cast" => (2, vec![param(0)], param(1)),
395 "simd_bitmask" => (2, vec![param(0)], param(1)),
396 "simd_select" | "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
397 "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
398 "simd_reduce_add_ordered" | "simd_reduce_mul_ordered" => {
399 (2, vec![param(0), param(1)], param(1))
401 "simd_reduce_add_unordered"
402 | "simd_reduce_mul_unordered"
408 | "simd_reduce_min_nanless"
409 | "simd_reduce_max_nanless" => (2, vec![param(0)], param(1)),
410 name if name.starts_with("simd_shuffle") => match name["simd_shuffle".len()..].parse() {
412 let params = vec![param(0), param(0), tcx.mk_array(tcx.types.u32, n)];
413 (2, params, param(1))
420 "invalid `simd_shuffle`, needs length: `{}`",
427 let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
428 tcx.sess.span_err(it.span, &msg);
433 equate_intrinsic_type(
437 Abi::PlatformIntrinsic,
438 hir::Unsafety::Unsafe,