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.
16 use middle::def_id::DefId;
18 use middle::ty::FnSig;
19 use middle::ty::{self, Ty};
20 use middle::ty::fold::TypeFolder;
21 use {CrateCtxt, require_same_types};
23 use std::collections::{HashMap};
26 use syntax::attr::AttrMetaMethods;
27 use syntax::codemap::Span;
28 use syntax::parse::token;
32 fn equate_intrinsic_type<'a, 'tcx>(tcx: &ty::ctxt<'tcx>, it: &hir::ForeignItem,
35 inputs: Vec<ty::Ty<'tcx>>,
36 output: ty::FnOutput<'tcx>) {
37 let fty = tcx.mk_fn(None, tcx.mk_bare_fn(ty::BareFnTy {
38 unsafety: hir::Unsafety::Unsafe,
40 sig: ty::Binder(FnSig {
46 let i_ty = tcx.lookup_item_type(DefId::local(it.id));
47 let i_n_tps = i_ty.generics.types.len(subst::FnSpace);
49 span_err!(tcx.sess, it.span, E0094,
50 "intrinsic has wrong number of type \
51 parameters: found {}, expected {}",
54 require_same_types(tcx,
61 format!("intrinsic has wrong type: expected `{}`",
67 /// Remember to add all intrinsics here, in librustc_trans/trans/intrinsic.rs,
68 /// and in libcore/intrinsics.rs
69 pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &hir::ForeignItem) {
70 fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: u32) -> Ty<'tcx> {
71 let name = token::intern(&format!("P{}", n));
72 ccx.tcx.mk_param(subst::FnSpace, n, name)
76 let name = it.ident.name.as_str();
77 let (n_tps, inputs, output) = if name.starts_with("atomic_") {
78 let split : Vec<&str> = name.split('_').collect();
79 assert!(split.len() >= 2, "Atomic intrinsic not correct format");
81 //We only care about the operation here
82 let (n_tps, inputs, output) = match split[1] {
83 "cxchg" => (1, vec!(tcx.mk_mut_ptr(param(ccx, 0)),
87 "load" => (1, vec!(tcx.mk_imm_ptr(param(ccx, 0))),
89 "store" => (1, vec!(tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0)),
92 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" |
93 "min" | "umax" | "umin" => {
94 (1, vec!(tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0)),
97 "fence" | "singlethreadfence" => {
98 (0, Vec::new(), tcx.mk_nil())
101 span_err!(tcx.sess, it.span, E0092,
102 "unrecognized atomic operation function: `{}`", op);
106 (n_tps, inputs, ty::FnConverging(output))
107 } else if &name[..] == "abort" || &name[..] == "unreachable" {
108 (0, Vec::new(), ty::FnDiverging)
110 let (n_tps, inputs, output) = match &name[..] {
111 "breakpoint" => (0, Vec::new(), tcx.mk_nil()),
113 "pref_align_of" | "min_align_of" => (1, Vec::new(), ccx.tcx.types.usize),
114 "size_of_val" | "min_align_of_val" => {
116 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1),
119 ], ccx.tcx.types.usize)
121 "init" | "init_dropped" => (1, Vec::new(), param(ccx, 0)),
122 "uninit" => (1, Vec::new(), param(ccx, 0)),
123 "forget" => (1, vec!( param(ccx, 0) ), tcx.mk_nil()),
124 "transmute" => (2, vec!( param(ccx, 0) ), param(ccx, 1)),
128 tcx.mk_mut_ptr(param(ccx, 0)),
134 (1, vec![tcx.mk_mut_ptr(param(ccx, 0))], tcx.mk_nil())
136 "needs_drop" => (1, Vec::new(), ccx.tcx.types.bool),
138 "type_name" => (1, Vec::new(), tcx.mk_static_str()),
139 "type_id" => (1, Vec::new(), ccx.tcx.types.u64),
140 "offset" | "arith_offset" => {
143 tcx.mk_ptr(ty::TypeAndMut {
145 mutbl: hir::MutImmutable
149 tcx.mk_ptr(ty::TypeAndMut {
151 mutbl: hir::MutImmutable
154 "copy" | "copy_nonoverlapping" => {
157 tcx.mk_ptr(ty::TypeAndMut {
159 mutbl: hir::MutImmutable
161 tcx.mk_ptr(ty::TypeAndMut {
163 mutbl: hir::MutMutable
169 "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
172 tcx.mk_ptr(ty::TypeAndMut {
174 mutbl: hir::MutMutable
176 tcx.mk_ptr(ty::TypeAndMut {
178 mutbl: hir::MutImmutable
184 "write_bytes" | "volatile_set_memory" => {
187 tcx.mk_ptr(ty::TypeAndMut {
189 mutbl: hir::MutMutable
196 "sqrtf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
197 "sqrtf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
200 vec!( tcx.types.f32, tcx.types.i32 ),
205 vec!( tcx.types.f64, tcx.types.i32 ),
208 "sinf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
209 "sinf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
210 "cosf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
211 "cosf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
214 vec!( tcx.types.f32, tcx.types.f32 ),
219 vec!( tcx.types.f64, tcx.types.f64 ),
222 "expf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
223 "expf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
224 "exp2f32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
225 "exp2f64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
226 "logf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
227 "logf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
228 "log10f32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
229 "log10f64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
230 "log2f32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
231 "log2f64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
234 vec!( tcx.types.f32, tcx.types.f32, tcx.types.f32 ),
239 vec!( tcx.types.f64, tcx.types.f64, tcx.types.f64 ),
242 "fabsf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
243 "fabsf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
244 "copysignf32" => (0, vec!( tcx.types.f32, tcx.types.f32 ), tcx.types.f32),
245 "copysignf64" => (0, vec!( tcx.types.f64, tcx.types.f64 ), tcx.types.f64),
246 "floorf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
247 "floorf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
248 "ceilf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
249 "ceilf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
250 "truncf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
251 "truncf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
252 "rintf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
253 "rintf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
254 "nearbyintf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
255 "nearbyintf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
256 "roundf32" => (0, vec!( tcx.types.f32 ), tcx.types.f32),
257 "roundf64" => (0, vec!( tcx.types.f64 ), tcx.types.f64),
258 "ctpop8" => (0, vec!( tcx.types.u8 ), tcx.types.u8),
259 "ctpop16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
260 "ctpop32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
261 "ctpop64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
262 "ctlz8" => (0, vec!( tcx.types.u8 ), tcx.types.u8),
263 "ctlz16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
264 "ctlz32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
265 "ctlz64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
266 "cttz8" => (0, vec!( tcx.types.u8 ), tcx.types.u8),
267 "cttz16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
268 "cttz32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
269 "cttz64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
270 "bswap16" => (0, vec!( tcx.types.u16 ), tcx.types.u16),
271 "bswap32" => (0, vec!( tcx.types.u32 ), tcx.types.u32),
272 "bswap64" => (0, vec!( tcx.types.u64 ), tcx.types.u64),
275 (1, vec!( tcx.mk_imm_ptr(param(ccx, 0)) ), param(ccx, 0)),
277 (1, vec!( tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0) ), tcx.mk_nil()),
279 "i8_add_with_overflow" | "i8_sub_with_overflow" | "i8_mul_with_overflow" =>
280 (0, vec!(tcx.types.i8, tcx.types.i8),
281 tcx.mk_tup(vec!(tcx.types.i8, tcx.types.bool))),
283 "i16_add_with_overflow" | "i16_sub_with_overflow" | "i16_mul_with_overflow" =>
284 (0, vec!(tcx.types.i16, tcx.types.i16),
285 tcx.mk_tup(vec!(tcx.types.i16, tcx.types.bool))),
287 "i32_add_with_overflow" | "i32_sub_with_overflow" | "i32_mul_with_overflow" =>
288 (0, vec!(tcx.types.i32, tcx.types.i32),
289 tcx.mk_tup(vec!(tcx.types.i32, tcx.types.bool))),
291 "i64_add_with_overflow" | "i64_sub_with_overflow" | "i64_mul_with_overflow" =>
292 (0, vec!(tcx.types.i64, tcx.types.i64),
293 tcx.mk_tup(vec!(tcx.types.i64, tcx.types.bool))),
295 "u8_add_with_overflow" | "u8_sub_with_overflow" | "u8_mul_with_overflow" =>
296 (0, vec!(tcx.types.u8, tcx.types.u8),
297 tcx.mk_tup(vec!(tcx.types.u8, tcx.types.bool))),
299 "u16_add_with_overflow" | "u16_sub_with_overflow" | "u16_mul_with_overflow" =>
300 (0, vec!(tcx.types.u16, tcx.types.u16),
301 tcx.mk_tup(vec!(tcx.types.u16, tcx.types.bool))),
303 "u32_add_with_overflow" | "u32_sub_with_overflow" | "u32_mul_with_overflow"=>
304 (0, vec!(tcx.types.u32, tcx.types.u32),
305 tcx.mk_tup(vec!(tcx.types.u32, tcx.types.bool))),
307 "u64_add_with_overflow" | "u64_sub_with_overflow" | "u64_mul_with_overflow" =>
308 (0, vec!(tcx.types.u64, tcx.types.u64),
309 tcx.mk_tup(vec!(tcx.types.u64, tcx.types.bool))),
311 "unchecked_udiv" | "unchecked_sdiv" | "unchecked_urem" | "unchecked_srem" =>
312 (1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),
314 "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
315 (1, vec![param(ccx, 0), param(ccx, 0)], param(ccx, 0)),
317 "return_address" => (0, vec![], tcx.mk_imm_ptr(tcx.types.u8)),
319 "assume" => (0, vec![tcx.types.bool], tcx.mk_nil()),
321 "discriminant_value" => (1, vec![
322 tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1),
324 param(ccx, 0))], tcx.types.u64),
327 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
328 let fn_ty = ty::BareFnTy {
329 unsafety: hir::Unsafety::Normal,
331 sig: ty::Binder(FnSig {
332 inputs: vec![mut_u8],
333 output: ty::FnOutput::FnConverging(tcx.mk_nil()),
337 let fn_ty = tcx.mk_bare_fn(fn_ty);
338 (0, vec![tcx.mk_fn(None, fn_ty), mut_u8], mut_u8)
342 span_err!(tcx.sess, it.span, E0093,
343 "unrecognized intrinsic function: `{}`", *other);
347 (n_tps, inputs, ty::FnConverging(output))
349 equate_intrinsic_type(
359 /// Type-check `extern "platform-intrinsic" { ... }` functions.
360 pub fn check_platform_intrinsic_type(ccx: &CrateCtxt,
361 it: &hir::ForeignItem) {
363 let name = token::intern(&format!("P{}", n));
364 ccx.tcx.mk_param(subst::FnSpace, n, name)
368 let i_ty = tcx.lookup_item_type(DefId::local(it.id));
369 let i_n_tps = i_ty.generics.types.len(subst::FnSpace);
370 let name = it.ident.name.as_str();
372 let (n_tps, inputs, output) = match &*name {
373 "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
374 (2, vec![param(0), param(0)], param(1))
376 "simd_add" | "simd_sub" | "simd_mul" |
377 "simd_div" | "simd_shl" | "simd_shr" |
378 "simd_and" | "simd_or" | "simd_xor" => {
379 (1, vec![param(0), param(0)], param(0))
381 "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
382 "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
383 "simd_cast" => (2, vec![param(0)], param(1)),
384 name if name.starts_with("simd_shuffle") => {
385 match name["simd_shuffle".len()..].parse() {
387 let params = vec![param(0), param(0),
388 tcx.mk_ty(ty::TyArray(tcx.types.u32, n))];
389 (2, params, param(1))
392 span_err!(tcx.sess, it.span, E0439,
393 "invalid `simd_shuffle`, needs length: `{}`", name);
399 match intrinsics::Intrinsic::find(tcx, &name) {
401 // this function is a platform specific intrinsic
403 span_err!(tcx.sess, it.span, E0440,
404 "platform-specific intrinsic has wrong number of type \
405 parameters: found {}, expected 0",
410 let mut structural_to_nomimal = HashMap::new();
412 let sig = tcx.no_late_bound_regions(i_ty.ty.fn_sig()).unwrap();
413 if intr.inputs.len() != sig.inputs.len() {
414 span_err!(tcx.sess, it.span, E0444,
415 "platform-specific intrinsic has invalid number of \
416 arguments: found {}, expected {}",
417 intr.inputs.len(), sig.inputs.len());
420 let input_pairs = intr.inputs.iter().zip(&sig.inputs);
421 for (i, (expected_arg, arg)) in input_pairs.enumerate() {
422 match_intrinsic_type_to_type(tcx, &format!("argument {}", i + 1), it.span,
423 &mut structural_to_nomimal, expected_arg, arg);
425 match_intrinsic_type_to_type(tcx, "return value", it.span,
426 &mut structural_to_nomimal,
427 &intr.output, sig.output.unwrap());
431 span_err!(tcx.sess, it.span, E0441,
432 "unrecognized platform-specific intrinsic function: `{}`", name);
439 equate_intrinsic_type(
443 abi::PlatformIntrinsic,
445 ty::FnConverging(output)
449 // walk the expected type and the actual type in lock step, checking they're
450 // the same, in a kinda-structural way, i.e. `Vector`s have to be simd structs with
451 // exactly the right element type
452 fn match_intrinsic_type_to_type<'tcx, 'a>(
453 tcx: &ty::ctxt<'tcx>,
456 structural_to_nominal: &mut HashMap<&'a intrinsics::Type, ty::Ty<'tcx>>,
457 expected: &'a intrinsics::Type, t: ty::Ty<'tcx>)
459 use intrinsics::Type::*;
461 let simple_error = |real: &str, expected: &str| {
462 span_err!(tcx.sess, span, E0442,
463 "intrinsic {} has wrong type: found {}, expected {}",
464 position, real, expected)
468 Void => match t.sty {
469 ty::TyTuple(ref v) if v.is_empty() => {},
470 _ => simple_error(&format!("`{}`", t), "()"),
472 // (The width we pass to LLVM doesn't concern the type checker.)
473 Integer(signed, bits, _llvm_width) => match (signed, bits, &t.sty) {
474 (true, 8, &ty::TyInt(ast::IntTy::TyI8)) |
475 (false, 8, &ty::TyUint(ast::UintTy::TyU8)) |
476 (true, 16, &ty::TyInt(ast::IntTy::TyI16)) |
477 (false, 16, &ty::TyUint(ast::UintTy::TyU16)) |
478 (true, 32, &ty::TyInt(ast::IntTy::TyI32)) |
479 (false, 32, &ty::TyUint(ast::UintTy::TyU32)) |
480 (true, 64, &ty::TyInt(ast::IntTy::TyI64)) |
481 (false, 64, &ty::TyUint(ast::UintTy::TyU64)) => {},
482 _ => simple_error(&format!("`{}`", t),
484 if signed {"i"} else {"u"},
487 Float(bits) => match (bits, &t.sty) {
488 (32, &ty::TyFloat(ast::FloatTy::TyF32)) |
489 (64, &ty::TyFloat(ast::FloatTy::TyF64)) => {},
490 _ => simple_error(&format!("`{}`", t),
491 &format!("`f{n}`", n = bits)),
493 Pointer(ref inner_expected, ref _llvm_type, const_) => {
495 ty::TyRawPtr(ty::TypeAndMut { ty, mutbl }) => {
496 if (mutbl == hir::MutImmutable) != const_ {
497 simple_error(&format!("`{}`", t),
498 if const_ {"const pointer"} else {"mut pointer"})
500 match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
503 _ => simple_error(&format!("`{}`", t),
504 &format!("raw pointer")),
507 Vector(ref inner_expected, ref _llvm_type, len) => {
509 simple_error(&format!("non-simd type `{}`", t),
513 let t_len = t.simd_size(tcx);
514 if len as usize != t_len {
515 simple_error(&format!("vector with length {}", t_len),
516 &format!("length {}", len));
519 let t_ty = t.simd_type(tcx);
521 // check that a given structural type always has the same an intrinsic definition
522 let previous = structural_to_nominal.entry(expected).or_insert(t);
524 // this gets its own error code because it is non-trivial
525 span_err!(tcx.sess, span, E0443,
526 "intrinsic {} has wrong type: found `{}`, expected `{}` which \
527 was used for this vector type previously in this signature",
534 match_intrinsic_type_to_type(tcx,
537 structural_to_nominal,
541 Aggregate(_flatten, ref expected_contents) => {
543 ty::TyTuple(ref contents) => {
544 if contents.len() != expected_contents.len() {
545 simple_error(&format!("tuple with length {}", contents.len()),
546 &format!("tuple with length {}", expected_contents.len()));
549 for (e, c) in expected_contents.iter().zip(contents) {
550 match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
554 _ => simple_error(&format!("`{}`", t),