1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Type-checking for the rust-intrinsic and platform-intrinsic
12 //! intrinsics that the compiler exposes.
15 use rustc::traits::{ObligationCause, ObligationCauseCode};
16 use rustc::ty::{self, TyCtxt, Ty};
17 use rustc::ty::subst::Subst;
18 use rustc::util::nodemap::FxHashMap;
19 use require_same_types;
21 use rustc_target::spec::abi::Abi;
23 use syntax::symbol::Symbol;
30 fn equate_intrinsic_type<'a, 'tcx>(
31 tcx: TyCtxt<'a, 'tcx, 'tcx>,
32 it: &hir::ForeignItem,
35 safety: hir::Unsafety,
36 inputs: Vec<Ty<'tcx>>,
39 let def_id = tcx.hir().local_def_id(it.id);
42 hir::ForeignItemKind::Fn(..) => {}
44 struct_span_err!(tcx.sess, it.span, E0622,
45 "intrinsic must be a function")
46 .span_label(it.span, "expected a function")
52 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
54 let span = match it.node {
55 hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
59 struct_span_err!(tcx.sess, span, E0094,
60 "intrinsic has wrong number of type \
61 parameters: found {}, expected {}",
63 .span_label(span, format!("expected {} type parameter", n_tps))
68 let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
75 let cause = ObligationCause::new(it.span, it.id, ObligationCauseCode::IntrinsicType);
76 require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
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.name.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 = match &name[..] {
131 "size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal,
132 _ => hir::Unsafety::Unsafe,
134 let (n_tps, inputs, output) = match &name[..] {
135 "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
137 "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
138 "size_of_val" | "min_align_of_val" => {
140 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
145 "rustc_peek" => (1, vec![param(0)], param(0)),
146 "init" => (1, Vec::new(), param(0)),
147 "uninit" => (1, Vec::new(), param(0)),
148 "forget" => (1, vec![param(0)], tcx.mk_unit()),
149 "transmute" => (2, vec![ param(0) ], param(1)),
153 tcx.mk_mut_ptr(param(0)),
158 "prefetch_read_data" | "prefetch_write_data" |
159 "prefetch_read_instruction" | "prefetch_write_instruction" => {
160 (1, vec![tcx.mk_ptr(ty::TypeAndMut {
162 mutbl: hir::MutImmutable
167 (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 {
178 mutbl: hir::MutImmutable
182 tcx.mk_ptr(ty::TypeAndMut {
184 mutbl: hir::MutImmutable
187 "copy" | "copy_nonoverlapping" => {
190 tcx.mk_ptr(ty::TypeAndMut {
192 mutbl: hir::MutImmutable
194 tcx.mk_ptr(ty::TypeAndMut {
196 mutbl: hir::MutMutable
202 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
205 tcx.mk_ptr(ty::TypeAndMut {
207 mutbl: hir::MutMutable
209 tcx.mk_ptr(ty::TypeAndMut {
211 mutbl: hir::MutImmutable
217 "write_bytes" | "volatile_set_memory" => {
220 tcx.mk_ptr(ty::TypeAndMut {
222 mutbl: hir::MutMutable
229 "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
230 "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
233 vec![ tcx.types.f32, tcx.types.i32 ],
238 vec![ tcx.types.f64, tcx.types.i32 ],
241 "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
242 "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
243 "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
244 "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
247 vec![ tcx.types.f32, tcx.types.f32 ],
252 vec![ tcx.types.f64, tcx.types.f64 ],
255 "expf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
256 "expf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
257 "exp2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
258 "exp2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
259 "logf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
260 "logf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
261 "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
262 "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
263 "log2f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
264 "log2f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
267 vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ],
272 vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ],
275 "fabsf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
276 "fabsf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
277 "copysignf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
278 "copysignf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
279 "floorf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
280 "floorf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
281 "ceilf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
282 "ceilf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
283 "truncf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
284 "truncf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
285 "rintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
286 "rintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
287 "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
288 "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
289 "roundf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
290 "roundf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
292 "volatile_load" | "unaligned_volatile_load" =>
293 (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)),
294 "volatile_store" | "unaligned_volatile_store" =>
295 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit()),
297 "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" |
298 "bswap" | "bitreverse" =>
299 (1, vec![param(0)], param(0)),
301 "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" =>
302 (1, vec![param(0), param(0)],
303 tcx.intern_tup(&[param(0), tcx.types.bool])),
305 "unchecked_div" | "unchecked_rem" | "exact_div" =>
306 (1, vec![param(0), param(0)], param(0)),
307 "unchecked_shl" | "unchecked_shr" |
308 "rotate_left" | "rotate_right" =>
309 (1, vec![param(0), param(0)], param(0)),
311 "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
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 def_id = tcx.hir().local_def_id(it.id);
405 let i_n_tps = tcx.generics_of(def_id).own_counts().types;
406 let name = it.name.as_str();
408 let (n_tps, inputs, output) = match &*name {
409 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
410 (2, vec![param(0), param(0)], param(1))
412 "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
413 "simd_div" | "simd_shl" | "simd_shr" |
414 "simd_and" | "simd_or" | "simd_xor" |
415 "simd_fmin" | "simd_fmax" | "simd_fpow" => {
416 (1, vec![param(0), param(0)], param(0))
418 "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" |
419 "simd_flog2" | "simd_flog10" | "simd_flog" |
420 "simd_fabs" | "simd_floor" | "simd_ceil" => {
421 (1, vec![param(0)], param(0))
424 (1, vec![param(0), tcx.types.i32], param(0))
427 (1, vec![param(0), param(0), param(0)], param(0))
430 (3, vec![param(0), param(1), param(2)], param(0))
433 (3, vec![param(0), param(1), param(2)], tcx.mk_unit())
435 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
436 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
437 "simd_cast" => (2, vec![param(0)], param(1)),
438 "simd_select" => (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 match intrinsics::Intrinsic::find(&name) {
464 // this function is a platform specific intrinsic
466 span_err!(tcx.sess, it.span, E0440,
467 "platform-specific intrinsic has wrong number of type \
468 parameters: found {}, expected 0",
473 let mut structural_to_nomimal = FxHashMap::default();
475 let sig = tcx.fn_sig(def_id);
476 let sig = sig.no_bound_vars().unwrap();
477 if intr.inputs.len() != sig.inputs().len() {
478 span_err!(tcx.sess, it.span, E0444,
479 "platform-specific intrinsic has invalid number of \
480 arguments: found {}, expected {}",
481 sig.inputs().len(), intr.inputs.len());
484 let input_pairs = intr.inputs.iter().zip(sig.inputs());
485 for (i, (expected_arg, arg)) in input_pairs.enumerate() {
486 match_intrinsic_type_to_type(tcx, &format!("argument {}", i + 1), it.span,
487 &mut structural_to_nomimal, expected_arg, arg);
489 match_intrinsic_type_to_type(tcx, "return value", it.span,
490 &mut structural_to_nomimal,
491 &intr.output, sig.output());
495 span_err!(tcx.sess, it.span, E0441,
496 "unrecognized platform-specific intrinsic function: `{}`", name);
503 equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, hir::Unsafety::Unsafe,
507 // walk the expected type and the actual type in lock step, checking they're
508 // the same, in a kinda-structural way, i.e., `Vector`s have to be simd structs with
509 // exactly the right element type
510 fn match_intrinsic_type_to_type<'a, 'tcx>(
511 tcx: TyCtxt<'a, 'tcx, 'tcx>,
514 structural_to_nominal: &mut FxHashMap<&'a intrinsics::Type, Ty<'tcx>>,
515 expected: &'a intrinsics::Type, t: Ty<'tcx>)
517 use intrinsics::Type::*;
519 let simple_error = |real: &str, expected: &str| {
520 span_err!(tcx.sess, span, E0442,
521 "intrinsic {} has wrong type: found {}, expected {}",
522 position, real, expected)
526 Void => match t.sty {
527 ty::Tuple(ref v) if v.is_empty() => {},
528 _ => simple_error(&format!("`{}`", t), "()"),
530 // (The width we pass to LLVM doesn't concern the type checker.)
531 Integer(signed, bits, _llvm_width) => match (signed, bits, &t.sty) {
532 (true, 8, &ty::Int(ast::IntTy::I8)) |
533 (false, 8, &ty::Uint(ast::UintTy::U8)) |
534 (true, 16, &ty::Int(ast::IntTy::I16)) |
535 (false, 16, &ty::Uint(ast::UintTy::U16)) |
536 (true, 32, &ty::Int(ast::IntTy::I32)) |
537 (false, 32, &ty::Uint(ast::UintTy::U32)) |
538 (true, 64, &ty::Int(ast::IntTy::I64)) |
539 (false, 64, &ty::Uint(ast::UintTy::U64)) |
540 (true, 128, &ty::Int(ast::IntTy::I128)) |
541 (false, 128, &ty::Uint(ast::UintTy::U128)) => {},
542 _ => simple_error(&format!("`{}`", t),
544 if signed {"i"} else {"u"},
547 Float(bits) => match (bits, &t.sty) {
548 (32, &ty::Float(ast::FloatTy::F32)) |
549 (64, &ty::Float(ast::FloatTy::F64)) => {},
550 _ => simple_error(&format!("`{}`", t),
551 &format!("`f{n}`", n = bits)),
553 Pointer(ref inner_expected, ref _llvm_type, const_) => {
555 ty::RawPtr(ty::TypeAndMut { ty, mutbl }) => {
556 if (mutbl == hir::MutImmutable) != const_ {
557 simple_error(&format!("`{}`", t),
558 if const_ {"const pointer"} else {"mut pointer"})
560 match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
563 _ => simple_error(&format!("`{}`", t), "raw pointer"),
566 Vector(ref inner_expected, ref _llvm_type, len) => {
568 simple_error(&format!("non-simd type `{}`", t), "simd type");
571 let t_len = t.simd_size(tcx);
572 if len as usize != t_len {
573 simple_error(&format!("vector with length {}", t_len),
574 &format!("length {}", len));
577 let t_ty = t.simd_type(tcx);
579 // check that a given structural type always has the same an intrinsic definition
580 let previous = structural_to_nominal.entry(expected).or_insert(t);
582 // this gets its own error code because it is non-trivial
583 span_err!(tcx.sess, span, E0443,
584 "intrinsic {} has wrong type: found `{}`, expected `{}` which \
585 was used for this vector type previously in this signature",
592 match_intrinsic_type_to_type(tcx,
595 structural_to_nominal,
599 Aggregate(_flatten, ref expected_contents) => {
601 ty::Tuple(contents) => {
602 if contents.len() != expected_contents.len() {
603 simple_error(&format!("tuple with length {}", contents.len()),
604 &format!("tuple with length {}", expected_contents.len()));
607 for (e, c) in expected_contents.iter().zip(contents) {
608 match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
612 _ => simple_error(&format!("`{}`", t),