1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
4 use rustc::traits::{ObligationCause, ObligationCauseCode};
5 use rustc::ty::{self, TyCtxt, Ty};
6 use rustc::ty::subst::Subst;
7 use crate::require_same_types;
9 use rustc_target::spec::abi::Abi;
10 use syntax::symbol::Symbol;
16 fn equate_intrinsic_type<'a, 'tcx>(
17 tcx: TyCtxt<'a, 'tcx, 'tcx>,
18 it: &hir::ForeignItem,
21 safety: hir::Unsafety,
22 inputs: Vec<Ty<'tcx>>,
25 let def_id = tcx.hir().local_def_id(it.id);
28 hir::ForeignItemKind::Fn(..) => {}
30 struct_span_err!(tcx.sess, it.span, E0622,
31 "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.node {
41 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
45 struct_span_err!(tcx.sess, span, E0094,
46 "intrinsic has wrong number of type \
47 parameters: found {}, expected {}",
49 .span_label(span, format!("expected {} type parameter", n_tps))
54 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
61 let cause = ObligationCause::new(it.span, it.id, ObligationCauseCode::IntrinsicType);
62 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
65 /// Returns `true` if the given intrinsic is unsafe to call or not.
66 pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
68 "size_of" | "min_align_of" | "needs_drop" |
69 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" |
70 "overflowing_add" | "overflowing_sub" | "overflowing_mul" |
71 "saturating_add" | "saturating_sub" |
72 "rotate_left" | "rotate_right" |
73 "ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse"
74 => hir::Unsafety::Normal,
75 _ => hir::Unsafety::Unsafe,
79 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
80 /// and in libcore/intrinsics.rs
81 pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
82 it: &hir::ForeignItem) {
83 let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)).as_interned_str());
84 let name = it.ident.as_str();
86 let mk_va_list_ty = || {
87 tcx.lang_items().va_list().map(|did| {
88 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
89 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
90 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
91 tcx.mk_mut_ref(tcx.mk_region(env_region), va_list_ty)
95 let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
96 let split : Vec<&str> = name.split('_').collect();
97 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
99 //We only care about the operation here
100 let (n_tps, inputs, output) = match split[1] {
101 "cxchg" | "cxchgweak" => (1, vec![tcx.mk_mut_ptr(param(0)),
104 tcx.intern_tup(&[param(0), tcx.types.bool])),
105 "load" => (1, vec![tcx.mk_imm_ptr(param(0))],
107 "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
110 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" |
111 "min" | "umax" | "umin" => {
112 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
115 "fence" | "singlethreadfence" => {
116 (0, Vec::new(), tcx.mk_unit())
119 struct_span_err!(tcx.sess, it.span, E0092,
120 "unrecognized atomic operation function: `{}`", op)
121 .span_label(it.span, "unrecognized atomic operation")
126 (n_tps, inputs, output, hir::Unsafety::Unsafe)
127 } else if &name[..] == "abort" || &name[..] == "unreachable" {
128 (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
130 let unsafety = intrisic_operation_unsafety(&name[..]);
131 let (n_tps, inputs, output) = match &name[..] {
132 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
134 "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
135 "size_of_val" | "min_align_of_val" => {
137 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
142 "rustc_peek" => (1, vec![param(0)], param(0)),
143 "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
144 "init" => (1, Vec::new(), param(0)),
145 "uninit" => (1, Vec::new(), param(0)),
146 "forget" => (1, vec![param(0)], tcx.mk_unit()),
147 "transmute" => (2, vec![ param(0) ], param(1)),
151 tcx.mk_mut_ptr(param(0)),
156 "prefetch_read_data" | "prefetch_write_data" |
157 "prefetch_read_instruction" | "prefetch_write_instruction" => {
158 (1, vec![tcx.mk_ptr(ty::TypeAndMut {
160 mutbl: hir::MutImmutable
165 (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit())
167 "needs_drop" => (1, Vec::new(), tcx.types.bool),
169 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
170 "type_id" => (1, Vec::new(), tcx.types.u64),
171 "offset" | "arith_offset" => {
174 tcx.mk_ptr(ty::TypeAndMut {
176 mutbl: hir::MutImmutable
180 tcx.mk_ptr(ty::TypeAndMut {
182 mutbl: hir::MutImmutable
185 "copy" | "copy_nonoverlapping" => {
188 tcx.mk_ptr(ty::TypeAndMut {
190 mutbl: hir::MutImmutable
192 tcx.mk_ptr(ty::TypeAndMut {
194 mutbl: hir::MutMutable
200 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
203 tcx.mk_ptr(ty::TypeAndMut {
205 mutbl: hir::MutMutable
207 tcx.mk_ptr(ty::TypeAndMut {
209 mutbl: hir::MutImmutable
215 "write_bytes" | "volatile_set_memory" => {
218 tcx.mk_ptr(ty::TypeAndMut {
220 mutbl: hir::MutMutable
227 "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
228 "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
231 vec![ tcx.types.f32, tcx.types.i32 ],
236 vec![ tcx.types.f64, tcx.types.i32 ],
239 "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
240 "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
241 "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
242 "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
245 vec![ tcx.types.f32, tcx.types.f32 ],
250 vec![ tcx.types.f64, tcx.types.f64 ],
253 "expf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
254 "expf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
255 "exp2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
256 "exp2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
257 "logf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
258 "logf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
259 "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
260 "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
261 "log2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
262 "log2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
265 vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ],
270 vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ],
273 "fabsf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
274 "fabsf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
275 "copysignf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
276 "copysignf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
277 "floorf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
278 "floorf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
279 "ceilf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
280 "ceilf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
281 "truncf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
282 "truncf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
283 "rintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
284 "rintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
285 "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
286 "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
287 "roundf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
288 "roundf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
290 "volatile_load" | "unaligned_volatile_load" =>
291 (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)),
292 "volatile_store" | "unaligned_volatile_store" =>
293 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit()),
295 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" |
296 "bswap" | "bitreverse" =>
297 (1, vec![param(0)], param(0)),
299 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" =>
300 (1, vec![param(0), param(0)],
301 tcx.intern_tup(&[param(0), tcx.types.bool])),
303 "unchecked_div" | "unchecked_rem" | "exact_div" =>
304 (1, vec![param(0), param(0)], param(0)),
305 "unchecked_shl" | "unchecked_shr" |
306 "rotate_left" | "rotate_right" =>
307 (1, vec![param(0), param(0)], param(0)),
309 "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
310 (1, vec![param(0), param(0)], param(0)),
311 "saturating_add" | "saturating_sub" =>
312 (1, vec![param(0), param(0)], param(0)),
313 "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" =>
314 (1, vec![param(0), param(0)], param(0)),
316 "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
317 "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
318 "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
320 "discriminant_value" => (1, vec![
321 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
323 param(0))], tcx.types.u64),
326 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
327 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
331 hir::Unsafety::Normal,
334 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
337 "va_start" | "va_end" => {
338 match mk_va_list_ty() {
339 Some(va_list_ty) => (0, vec![va_list_ty], tcx.mk_unit()),
340 None => bug!("va_list lang_item must be defined to use va_list intrinsics")
345 match tcx.lang_items().va_list() {
347 let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
348 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
349 let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
350 let ret_ty = match va_list_ty.sty {
351 ty::Adt(def, _) if def.is_struct() => {
352 let fields = &def.non_enum_variant().fields;
353 match tcx.type_of(fields[0].did).subst(tcx, &[region.into()]).sty {
354 ty::Ref(_, element_ty, _) => match element_ty.sty {
355 ty::Adt(..) => element_ty,
358 _ => bug!("va_list structure is invalid")
362 bug!("va_list structure is invalid")
365 (0, vec![tcx.mk_imm_ref(tcx.mk_region(env_region), va_list_ty)], ret_ty)
367 None => bug!("va_list lang_item must be defined to use va_list intrinsics")
372 match mk_va_list_ty() {
373 Some(va_list_ty) => (1, vec![va_list_ty], param(0)),
374 None => bug!("va_list lang_item must be defined to use va_list intrinsics")
378 "nontemporal_store" => {
379 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit())
383 struct_span_err!(tcx.sess, it.span, E0093,
384 "unrecognized intrinsic function: `{}`",
386 .span_label(it.span, "unrecognized intrinsic")
391 (n_tps, inputs, output, unsafety)
393 equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
396 /// Type-check `extern "platform-intrinsic" { ... }` functions.
397 pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
398 it: &hir::ForeignItem) {
400 let name = Symbol::intern(&format!("P{}", n)).as_interned_str();
401 tcx.mk_ty_param(n, name)
404 let name = it.ident.as_str();
406 let (n_tps, inputs, output) = match &*name {
407 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
408 (2, vec![param(0), param(0)], param(1))
410 "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
411 "simd_div" | "simd_shl" | "simd_shr" |
412 "simd_and" | "simd_or" | "simd_xor" |
413 "simd_fmin" | "simd_fmax" | "simd_fpow" => {
414 (1, vec![param(0), param(0)], param(0))
416 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" |
417 "simd_flog2" | "simd_flog10" | "simd_flog" |
418 "simd_fabs" | "simd_floor" | "simd_ceil" => {
419 (1, vec![param(0)], param(0))
422 (1, vec![param(0), tcx.types.i32], param(0))
425 (1, vec![param(0), param(0), param(0)], param(0))
428 (3, vec![param(0), param(1), param(2)], param(0))
431 (3, vec![param(0), param(1), param(2)], tcx.mk_unit())
433 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
434 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
435 "simd_cast" => (2, vec![param(0)], param(1)),
436 "simd_bitmask" => (2, vec![param(0)], param(1)),
438 "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
439 "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
440 "simd_reduce_add_ordered" | "simd_reduce_mul_ordered"
441 => (2, vec![param(0), param(1)], param(1)),
442 "simd_reduce_add_unordered" | "simd_reduce_mul_unordered" |
443 "simd_reduce_and" | "simd_reduce_or" | "simd_reduce_xor" |
444 "simd_reduce_min" | "simd_reduce_max" |
445 "simd_reduce_min_nanless" | "simd_reduce_max_nanless"
446 => (2, vec![param(0)], param(1)),
447 name if name.starts_with("simd_shuffle") => {
448 match name["simd_shuffle".len()..].parse() {
450 let params = vec![param(0), param(0),
451 tcx.mk_array(tcx.types.u32, n)];
452 (2, params, param(1))
455 span_err!(tcx.sess, it.span, E0439,
456 "invalid `simd_shuffle`, needs length: `{}`", name);
462 let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
463 tcx.sess.span_err(it.span, &msg);
468 equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, hir::Unsafety::Unsafe,