2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-18 07:45:17 -05:00
|
|
|
#![allow(non_camel_case_types, non_snake_case)]
|
2014-02-10 08:36:31 -06:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
//! Code that is useful in various trans modules.
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::ExprOrMethodCall::*;
|
|
|
|
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::Session;
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm;
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef};
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm::{True, False, Bool};
|
2014-12-15 17:21:08 -06:00
|
|
|
use middle::cfg;
|
2014-05-14 14:31:30 -05:00
|
|
|
use middle::def;
|
2014-11-25 15:59:02 -06:00
|
|
|
use middle::infer;
|
2013-07-15 22:42:13 -05:00
|
|
|
use middle::lang_items::LangItem;
|
2014-08-18 10:29:44 -05:00
|
|
|
use middle::mem_categorization as mc;
|
2014-11-18 07:22:59 -06:00
|
|
|
use middle::region;
|
2015-01-03 21:42:21 -06:00
|
|
|
use middle::subst::{self, Subst, Substs};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::base;
|
|
|
|
use trans::build;
|
|
|
|
use trans::cleanup;
|
2015-01-04 10:47:58 -06:00
|
|
|
use trans::consts;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::datum;
|
|
|
|
use trans::debuginfo;
|
|
|
|
use trans::machine;
|
2014-12-17 13:16:28 -06:00
|
|
|
use trans::monomorphize;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::type_::Type;
|
|
|
|
use trans::type_of;
|
2014-09-12 10:42:58 -05:00
|
|
|
use middle::traits;
|
2015-01-03 21:42:21 -06:00
|
|
|
use middle::ty::{self, HasProjectionTypes, Ty};
|
2014-09-12 10:42:58 -05:00
|
|
|
use middle::ty_fold;
|
2014-12-17 13:16:28 -06:00
|
|
|
use middle::ty_fold::{TypeFolder, TypeFoldable};
|
2013-09-30 16:45:53 -05:00
|
|
|
use util::ppaux::Repr;
|
2015-01-01 15:04:51 -06:00
|
|
|
use util::nodemap::{FnvHashMap, NodeMap};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-01-28 20:50:05 -06:00
|
|
|
use arena::TypedArena;
|
2014-10-14 15:36:11 -05:00
|
|
|
use libc::{c_uint, c_char};
|
2014-11-25 15:28:35 -06:00
|
|
|
use std::ffi::CString;
|
2013-12-18 16:54:42 -06:00
|
|
|
use std::cell::{Cell, RefCell};
|
2014-02-26 11:58:41 -06:00
|
|
|
use std::vec::Vec;
|
2014-02-13 23:07:09 -06:00
|
|
|
use syntax::ast::Ident;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_map::{PathElem, PathName};
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2014-01-10 16:02:36 -06:00
|
|
|
use syntax::parse::token::InternedString;
|
2013-06-04 14:34:25 -05:00
|
|
|
use syntax::parse::token;
|
2014-12-16 14:00:05 -06:00
|
|
|
use util::common::memoized;
|
|
|
|
use util::nodemap::FnvHashSet;
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2014-11-15 19:30:33 -06:00
|
|
|
pub use trans::context::CrateContext;
|
2013-06-12 21:02:33 -05:00
|
|
|
|
2014-12-18 08:26:10 -06:00
|
|
|
// Is the type's representation size known at compile time?
|
|
|
|
pub fn type_is_sized<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty::type_contents(cx, ty).is_sized(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lltype_is_sized<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
match ty.sty {
|
|
|
|
ty::ty_open(_) => true,
|
|
|
|
_ => type_is_sized(cx, ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_is_fat_ptr<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
match ty.sty {
|
|
|
|
ty::ty_ptr(ty::mt{ty, ..}) |
|
|
|
|
ty::ty_rptr(_, ty::mt{ty, ..}) |
|
|
|
|
ty::ty_uniq(ty) => {
|
|
|
|
!type_is_sized(cx, ty)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the smallest part of `ty` which is unsized. Fails if `ty` is sized.
|
|
|
|
// 'Smallest' here means component of the static representation of the type; not
|
|
|
|
// the size of an object at runtime.
|
|
|
|
pub fn unsized_part_of_type<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
match ty.sty {
|
|
|
|
ty::ty_str | ty::ty_trait(..) | ty::ty_vec(..) => ty,
|
|
|
|
ty::ty_struct(def_id, substs) => {
|
|
|
|
let unsized_fields: Vec<_> =
|
|
|
|
ty::struct_fields(cx, def_id, substs)
|
|
|
|
.iter()
|
|
|
|
.map(|f| f.mt.ty)
|
|
|
|
.filter(|ty| !type_is_sized(cx, *ty))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Exactly one of the fields must be unsized.
|
|
|
|
assert!(unsized_fields.len() == 1);
|
|
|
|
|
|
|
|
unsized_part_of_type(cx, unsized_fields[0])
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
assert!(type_is_sized(cx, ty),
|
|
|
|
"unsized_part_of_type failed even though ty is unsized");
|
|
|
|
panic!("called unsized_part_of_type with sized ty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 14:00:05 -06:00
|
|
|
// Some things don't need cleanups during unwinding because the
|
|
|
|
// task can free them all at once later. Currently only things
|
|
|
|
// that only contain scalars and shared boxes can avoid unwind
|
|
|
|
// cleanups.
|
|
|
|
pub fn type_needs_unwind_cleanup<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
return memoized(ccx.needs_unwind_cleanup_cache(), ty, |ty| {
|
|
|
|
type_needs_unwind_cleanup_(ccx.tcx(), ty, &mut FnvHashSet::new())
|
|
|
|
});
|
|
|
|
|
|
|
|
fn type_needs_unwind_cleanup_<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
tycache: &mut FnvHashSet<Ty<'tcx>>)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
// Prevent infinite recursion
|
|
|
|
if !tycache.insert(ty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut needs_unwind_cleanup = false;
|
|
|
|
ty::maybe_walk_ty(ty, |ty| {
|
|
|
|
needs_unwind_cleanup |= match ty.sty {
|
|
|
|
ty::ty_bool | ty::ty_int(_) | ty::ty_uint(_) |
|
|
|
|
ty::ty_float(_) | ty::ty_tup(_) | ty::ty_ptr(_) => false,
|
|
|
|
|
|
|
|
ty::ty_enum(did, substs) =>
|
|
|
|
ty::enum_variants(tcx, did).iter().any(|v|
|
2014-12-17 13:16:28 -06:00
|
|
|
v.args.iter().any(|&aty| {
|
2014-12-16 14:00:05 -06:00
|
|
|
let t = aty.subst(tcx, substs);
|
|
|
|
type_needs_unwind_cleanup_(tcx, t, tycache)
|
|
|
|
})
|
|
|
|
),
|
|
|
|
|
|
|
|
_ => true
|
|
|
|
};
|
|
|
|
!needs_unwind_cleanup
|
|
|
|
});
|
|
|
|
needs_unwind_cleanup
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_needs_drop<'tcx>(cx: &ty::ctxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> bool {
|
|
|
|
ty::type_contents(cx, ty).needs_drop(cx)
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn type_is_newtype_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
ty: Ty<'tcx>) -> bool {
|
2014-10-31 03:51:16 -05:00
|
|
|
match ty.sty {
|
2014-12-03 18:01:29 -06:00
|
|
|
ty::ty_struct(def_id, substs) => {
|
2014-03-15 15:29:34 -05:00
|
|
|
let fields = ty::struct_fields(ccx.tcx(), def_id, substs);
|
2013-09-30 16:45:53 -05:00
|
|
|
fields.len() == 1 &&
|
2014-09-30 19:11:34 -05:00
|
|
|
fields[0].name ==
|
2014-03-08 14:36:22 -06:00
|
|
|
token::special_idents::unnamed_field.name &&
|
2014-10-15 01:05:01 -05:00
|
|
|
type_is_immediate(ccx, fields[0].mt.ty)
|
2013-09-30 16:45:53 -05:00
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn type_is_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::machine::llsize_of_alloc;
|
|
|
|
use trans::type_of::sizing_type_of;
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
let tcx = ccx.tcx();
|
2014-09-30 17:26:04 -05:00
|
|
|
let simple = ty::type_is_scalar(ty) ||
|
2013-09-30 16:45:53 -05:00
|
|
|
ty::type_is_unique(ty) || ty::type_is_region_ptr(ty) ||
|
2014-10-24 14:14:37 -05:00
|
|
|
type_is_newtype_immediate(ccx, ty) ||
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
ty::type_is_simd(tcx, ty);
|
2014-12-18 08:26:10 -06:00
|
|
|
if simple && !type_is_fat_ptr(tcx, ty) {
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-18 08:26:10 -06:00
|
|
|
if !type_is_sized(tcx, ty) {
|
2014-08-06 04:59:40 -05:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-31 03:51:16 -05:00
|
|
|
match ty.sty {
|
2014-05-29 00:26:56 -05:00
|
|
|
ty::ty_struct(..) | ty::ty_enum(..) | ty::ty_tup(..) |
|
|
|
|
ty::ty_unboxed_closure(..) => {
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
let llty = sizing_type_of(ccx, ty);
|
2014-09-05 11:18:53 -05:00
|
|
|
llsize_of_alloc(ccx, llty) <= llsize_of_alloc(ccx, ccx.int_type())
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
}
|
2014-01-16 18:10:17 -06:00
|
|
|
_ => type_is_zero_size(ccx, ty)
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
}
|
2013-09-30 16:45:53 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Identify types which have size zero at runtime.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::machine::llsize_of_alloc;
|
|
|
|
use trans::type_of::sizing_type_of;
|
2014-01-16 14:11:22 -06:00
|
|
|
let llty = sizing_type_of(ccx, ty);
|
|
|
|
llsize_of_alloc(ccx, llty) == 0
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Identifies types which we declare to be equivalent to `void` in C for the purpose of function
|
|
|
|
/// return types. These are `()`, bot, and uninhabited enums. Note that all such types are also
|
|
|
|
/// zero-size, but not all zero-size types use a `void` return type (in order to aid with C ABI
|
|
|
|
/// compatibility).
|
2014-09-13 13:09:25 -05:00
|
|
|
pub fn return_type_is_void(ccx: &CrateContext, ty: Ty) -> bool {
|
2014-10-24 14:14:37 -05:00
|
|
|
ty::type_is_nil(ty) || ty::type_is_empty(ccx.tcx(), ty)
|
2014-01-16 18:10:17 -06:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:24:46 -05:00
|
|
|
/// Generates a unique symbol based off the name given. This is used to create
|
|
|
|
/// unique symbols for things like closures.
|
2014-02-13 23:07:09 -06:00
|
|
|
pub fn gensym_name(name: &str) -> PathElem {
|
2014-07-31 21:05:45 -05:00
|
|
|
let num = token::gensym(name).uint();
|
2014-03-13 18:24:46 -05:00
|
|
|
// use one colon which will get translated to a period by the mangler, and
|
|
|
|
// we're guaranteed that `num` is globally unique for this crate.
|
2015-01-01 18:56:28 -06:00
|
|
|
PathName(token::gensym(format!("{}:{}", name, num).index(&FullRange)))
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Copy)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub struct tydesc_info<'tcx> {
|
|
|
|
pub ty: Ty<'tcx>,
|
2014-03-28 12:05:27 -05:00
|
|
|
pub tydesc: ValueRef,
|
|
|
|
pub size: ValueRef,
|
|
|
|
pub align: ValueRef,
|
|
|
|
pub name: ValueRef,
|
2013-02-04 16:02:01 -06:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
/*
|
2012-06-26 18:18:37 -05:00
|
|
|
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
|
2011-07-21 19:27:34 -05:00
|
|
|
*
|
|
|
|
* An "extern" is an LLVM symbol we wind up emitting an undefined external
|
|
|
|
* reference to. This means "we don't have the thing in this compilation unit,
|
|
|
|
* please make sure you link it in at runtime". This could be a reference to
|
|
|
|
* C code found in a C library, or rust code found in a rust crate.
|
|
|
|
*
|
2012-06-26 18:18:37 -05:00
|
|
|
* Most "externs" are implicitly declared (automatically) as a result of a
|
|
|
|
* user declaring an extern _module_ dependency; this causes the rust driver
|
|
|
|
* to locate an extern crate, scan its compilation metadata, and emit extern
|
|
|
|
* declarations for any symbols used by the declaring crate.
|
2011-07-21 19:27:34 -05:00
|
|
|
*
|
2012-06-26 18:18:37 -05:00
|
|
|
* A "foreign" is an extern that references C (or other non-rust ABI) code.
|
|
|
|
* There is no metadata to scan for extern references so in these cases either
|
|
|
|
* a header-digester like bindgen, or manual function prototypes, have to
|
|
|
|
* serve as declarators. So these are usually given explicitly as prototype
|
|
|
|
* declarations, in rust code, with ABI attributes on them noting which ABI to
|
|
|
|
* link via.
|
|
|
|
*
|
|
|
|
* An "upcall" is a foreign call generated by the compiler (not corresponding
|
|
|
|
* to any user-written call in the code) into the runtime library, to perform
|
|
|
|
* some helper task such as bringing a task to life, allocating memory, etc.
|
2011-07-21 19:27:34 -05:00
|
|
|
*
|
|
|
|
*/
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Copy)]
|
2014-01-15 13:39:08 -06:00
|
|
|
pub struct NodeInfo {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub span: Span,
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expr_info(expr: &ast::Expr) -> NodeInfo {
|
|
|
|
NodeInfo { id: expr.id, span: expr.span }
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub struct BuilderRef_res {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub b: BuilderRef,
|
2013-02-27 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for BuilderRef_res {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-02-16 00:02:31 -06:00
|
|
|
llvm::LLVMDisposeBuilder(self.b);
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
2012-06-22 13:53:25 -05:00
|
|
|
}
|
2011-08-24 09:30:20 -05:00
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn BuilderRef_res(b: BuilderRef) -> BuilderRef_res {
|
2012-09-05 17:58:43 -05:00
|
|
|
BuilderRef_res {
|
2014-02-16 00:02:31 -06:00
|
|
|
b: b
|
2012-09-05 17:58:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 16:59:56 -06:00
|
|
|
pub type ExternMap = FnvHashMap<String, ValueRef>;
|
2013-02-04 17:30:32 -06:00
|
|
|
|
2014-11-06 01:24:44 -06:00
|
|
|
pub fn validate_substs(substs: &Substs) {
|
|
|
|
assert!(substs.types.all(|t| !ty::type_needs_infer(*t)));
|
2014-05-07 06:20:15 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// work around bizarre resolve errors
|
2014-12-28 15:22:37 -06:00
|
|
|
type RvalueDatum<'tcx> = datum::Datum<'tcx, datum::Rvalue>;
|
|
|
|
type LvalueDatum<'tcx> = datum::Datum<'tcx, datum::Lvalue>;
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// Function context. Every LLVM function we create will have one of
|
|
|
|
// these.
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct FunctionContext<'a, 'tcx: 'a> {
|
2011-07-27 07:19:39 -05:00
|
|
|
// The ValueRef returned from a call to llvm::LLVMAddFunction; the
|
2011-08-03 17:39:43 -05:00
|
|
|
// address of the first instruction in the sequence of
|
|
|
|
// instructions for this function that will go in the .text
|
|
|
|
// section of the executable we're generating.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub llfn: ValueRef,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2015-01-02 03:09:35 -06:00
|
|
|
// always an empty parameter-environment
|
|
|
|
pub param_env: ty::ParameterEnvironment<'a, 'tcx>,
|
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
// The environment argument in a closure.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub llenv: Option<ValueRef>,
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-07-29 14:25:06 -05:00
|
|
|
// A pointer to where to store the return value. If the return type is
|
|
|
|
// immediate, this points to an alloca in the function. Otherwise, it's a
|
|
|
|
// pointer to the hidden first parameter of the function. After function
|
|
|
|
// construction, this should always be Some.
|
|
|
|
pub llretslotptr: Cell<Option<ValueRef>>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2014-03-28 12:05:27 -05:00
|
|
|
// These pub elements: "hoisted basic blocks" containing
|
2011-07-27 07:19:39 -05:00
|
|
|
// administrative activities that have to happen in only one place in
|
|
|
|
// the function, due to LLVM's quirks.
|
2013-07-21 09:19:34 -05:00
|
|
|
// A marker for the place where we want to insert the function's static
|
|
|
|
// allocas, so that LLVM will coalesce them into a single alloca call.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub alloca_insert_pt: Cell<Option<ValueRef>>,
|
|
|
|
pub llreturn: Cell<Option<BasicBlockRef>>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2014-08-11 21:16:00 -05:00
|
|
|
// If the function has any nested return's, including something like:
|
|
|
|
// fn foo() -> Option<Foo> { Some(Foo { x: return None }) }, then
|
|
|
|
// we use a separate alloca for each return
|
|
|
|
pub needs_ret_allocas: bool,
|
|
|
|
|
2012-02-15 06:57:29 -06:00
|
|
|
// The a value alloca'd for calls to upcalls.rust_personality. Used when
|
|
|
|
// outputting the resume instruction.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub personality: Cell<Option<ValueRef>>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// True if the caller expects this fn to use the out pointer to
|
2014-07-29 14:25:06 -05:00
|
|
|
// return. Either way, your code should write into the slot llretslotptr
|
|
|
|
// points to, but if this value is false, that slot will be a local alloca.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub caller_expects_out_pointer: bool,
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-09-17 09:28:19 -05:00
|
|
|
// Maps the DefId's for local variables to the allocas created for
|
2011-07-27 07:19:39 -05:00
|
|
|
// them in llallocas.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub lllocals: RefCell<NodeMap<LvalueDatum<'tcx>>>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2012-02-03 06:37:55 -06:00
|
|
|
// Same as above, but for closure upvars
|
2014-03-28 12:05:27 -05:00
|
|
|
pub llupvars: RefCell<NodeMap<ValueRef>>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
// The NodeId of the function, or -1 if it doesn't correspond to
|
2011-08-03 17:39:43 -05:00
|
|
|
// a user-defined function.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub id: ast::NodeId,
|
2012-02-03 06:37:55 -06:00
|
|
|
|
|
|
|
// If this function is being monomorphized, this contains the type
|
|
|
|
// substitutions used.
|
2014-11-06 01:24:44 -06:00
|
|
|
pub param_substs: &'a Substs<'tcx>,
|
2012-02-03 06:37:55 -06:00
|
|
|
|
|
|
|
// The source span and nesting context where this function comes from, for
|
|
|
|
// error reporting and symbol generation.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub span: Option<Span>,
|
2011-08-02 17:13:08 -05:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
// The arena that blocks are allocated from.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,
|
2014-01-07 10:54:58 -06:00
|
|
|
|
2012-02-03 06:37:55 -06:00
|
|
|
// This function's enclosing crate context.
|
2014-04-22 07:56:37 -05:00
|
|
|
pub ccx: &'a CrateContext<'a, 'tcx>,
|
2013-08-05 04:12:40 -05:00
|
|
|
|
|
|
|
// Used and maintained by the debuginfo module.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub debug_context: debuginfo::FunctionDebugContext,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
// Cleanup scopes.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub scopes: RefCell<Vec<cleanup::CleanupScope<'a, 'tcx>>>,
|
2014-12-15 17:21:08 -06:00
|
|
|
|
|
|
|
pub cfg: Option<cfg::CFG>,
|
2013-01-06 13:16:14 -06:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
|
2013-05-27 20:33:57 -05:00
|
|
|
pub fn arg_pos(&self, arg: uint) -> uint {
|
2014-01-27 06:18:36 -06:00
|
|
|
let arg = self.env_arg_pos() + arg;
|
|
|
|
if self.llenv.is_some() {
|
|
|
|
arg + 1
|
2013-05-21 14:25:44 -05:00
|
|
|
} else {
|
2014-01-27 06:18:36 -06:00
|
|
|
arg
|
2013-05-27 20:33:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn env_arg_pos(&self) -> uint {
|
2013-05-21 14:25:44 -05:00
|
|
|
if self.caller_expects_out_pointer {
|
2013-05-27 20:33:57 -05:00
|
|
|
1u
|
|
|
|
} else {
|
|
|
|
0u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 23:03:04 -06:00
|
|
|
pub fn cleanup(&self) {
|
2013-07-21 09:19:34 -05:00
|
|
|
unsafe {
|
2013-12-20 22:38:15 -06:00
|
|
|
llvm::LLVMInstructionEraseFromParent(self.alloca_insert_pt
|
|
|
|
.get()
|
|
|
|
.unwrap());
|
2013-07-12 21:09:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 23:03:04 -06:00
|
|
|
pub fn get_llreturn(&self) -> BasicBlockRef {
|
2013-12-20 22:41:54 -06:00
|
|
|
if self.llreturn.get().is_none() {
|
2014-03-15 15:29:34 -05:00
|
|
|
|
|
|
|
self.llreturn.set(Some(unsafe {
|
2014-11-25 15:28:35 -06:00
|
|
|
llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(), self.llfn,
|
|
|
|
"return\0".as_ptr() as *const _)
|
2014-03-15 15:29:34 -05:00
|
|
|
}))
|
2013-07-12 20:25:46 -05:00
|
|
|
}
|
|
|
|
|
2013-12-20 22:41:54 -06:00
|
|
|
self.llreturn.get().unwrap()
|
2013-07-12 20:25:46 -05:00
|
|
|
}
|
2012-06-26 15:50:43 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn get_ret_slot(&self, bcx: Block<'a, 'tcx>,
|
|
|
|
output: ty::FnOutput<'tcx>,
|
|
|
|
name: &str) -> ValueRef {
|
2014-08-11 21:16:00 -05:00
|
|
|
if self.needs_ret_allocas {
|
2014-10-24 14:14:37 -05:00
|
|
|
base::alloca_no_lifetime(bcx, match output {
|
|
|
|
ty::FnConverging(output_type) => type_of::type_of(bcx.ccx(), output_type),
|
|
|
|
ty::FnDiverging => Type::void(bcx.ccx())
|
|
|
|
}, name)
|
2014-08-11 21:16:00 -05:00
|
|
|
} else {
|
|
|
|
self.llretslotptr.get().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn new_block(&'a self,
|
|
|
|
is_lpad: bool,
|
|
|
|
name: &str,
|
|
|
|
opt_node_id: Option<ast::NodeId>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'a, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
unsafe {
|
2014-11-25 15:28:35 -06:00
|
|
|
let name = CString::from_slice(name.as_bytes());
|
|
|
|
let llbb = llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(),
|
|
|
|
self.llfn,
|
|
|
|
name.as_ptr());
|
2014-09-06 11:13:04 -05:00
|
|
|
BlockS::new(llbb, is_lpad, opt_node_id, self)
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-16 07:51:50 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn new_id_block(&'a self,
|
|
|
|
name: &str,
|
|
|
|
node_id: ast::NodeId)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'a, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
self.new_block(false, name, Some(node_id))
|
2012-03-23 19:52:20 -05:00
|
|
|
}
|
Implement scopes independent of LLVM basic blocks
Currently, scopes are tied to LLVM basic blocks. For each scope, there
are two new basic blocks, which means two extra jumps in the unoptimized
IR. These blocks aren't actually required, but only used to act as the
boundary for cleanups.
By keeping track of the current scope within a single basic block, we
can avoid those extra blocks and jumps, shrinking the pre-optimization
IR quite considerably. For example, the IR for trans_intrinsic goes
from ~22k lines to ~16k lines, almost 30% less.
The impact on the build times of optimized builds is rather small (about
1%), but unoptimized builds are about 11% faster. The testsuite for
unoptimized builds runs between 15% (CPU time) and 7.5% (wallclock time on
my i7) faster.
Also, in some situations this helps LLVM to generate better code by
inlining functions that it previously considered to be too large.
Likely because of the pointless blocks/jumps that were still present at
the time the inlining pass runs.
Refs #7462
2013-07-07 07:53:57 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn new_temp_block(&'a self,
|
|
|
|
name: &str)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'a, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
self.new_block(false, name, None)
|
|
|
|
}
|
2013-12-30 20:57:48 -06:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn join_blocks(&'a self,
|
|
|
|
id: ast::NodeId,
|
2014-09-06 11:13:04 -05:00
|
|
|
in_cxs: &[Block<'a, 'tcx>])
|
|
|
|
-> Block<'a, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let out = self.new_id_block("join", id);
|
|
|
|
let mut reachable = false;
|
|
|
|
for bcx in in_cxs.iter() {
|
|
|
|
if !bcx.unreachable.get() {
|
|
|
|
build::Br(*bcx, out.llbb);
|
|
|
|
reachable = true;
|
|
|
|
}
|
2013-12-21 17:47:08 -06:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
if !reachable {
|
|
|
|
build::Unreachable(out);
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
return out;
|
2013-05-02 20:15:36 -05:00
|
|
|
}
|
2014-12-17 13:16:28 -06:00
|
|
|
|
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
|
|
|
where T : TypeFoldable<'tcx> + Repr<'tcx> + HasProjectionTypes + Clone
|
|
|
|
{
|
|
|
|
monomorphize::apply_param_substs(self.ccx.tcx(),
|
|
|
|
self.param_substs,
|
|
|
|
value)
|
|
|
|
}
|
2013-05-02 20:15:36 -05:00
|
|
|
}
|
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// Basic block context. We create a block context for each basic block
|
|
|
|
// (single-entry, single-exit sequence of instructions) we generate from Rust
|
|
|
|
// code. Each basic block we generate is attached to a function, typically
|
|
|
|
// with many basic blocks per function. All the basic blocks attached to a
|
|
|
|
// function are organized as a directed graph.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub struct BlockS<'blk, 'tcx: 'blk> {
|
2011-07-27 07:19:39 -05:00
|
|
|
// The BasicBlockRef returned from a call to
|
2011-08-03 17:39:43 -05:00
|
|
|
// llvm::LLVMAppendBasicBlock(llfn, name), which adds a basic
|
|
|
|
// block to the function pointed to by llfn. We insert
|
|
|
|
// instructions into that block by way of this block context.
|
2011-09-02 17:34:58 -05:00
|
|
|
// The block pointing to this one in the function's digraph.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub llbb: BasicBlockRef,
|
|
|
|
pub terminated: Cell<bool>,
|
|
|
|
pub unreachable: Cell<bool>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2012-07-23 18:00:19 -05:00
|
|
|
// Is this block part of a landing pad?
|
2014-03-28 12:05:27 -05:00
|
|
|
pub is_lpad: bool,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
// AST node-id associated with this block, if any. Used for
|
|
|
|
// debugging purposes only.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub opt_node_id: Option<ast::NodeId>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
// The function context for the function to which this block is
|
|
|
|
// attached.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fcx: &'blk FunctionContext<'blk, 'tcx>,
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub type Block<'blk, 'tcx> = &'blk BlockS<'blk, 'tcx>;
|
|
|
|
|
|
|
|
impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn new(llbb: BasicBlockRef,
|
2013-07-17 05:12:08 -05:00
|
|
|
is_lpad: bool,
|
2014-01-15 13:39:08 -06:00
|
|
|
opt_node_id: Option<ast::NodeId>,
|
2014-09-06 11:13:04 -05:00
|
|
|
fcx: &'blk FunctionContext<'blk, 'tcx>)
|
|
|
|
-> Block<'blk, 'tcx> {
|
|
|
|
fcx.block_arena.alloc(BlockS {
|
2013-07-17 05:12:08 -05:00
|
|
|
llbb: llbb,
|
2013-12-18 16:54:42 -06:00
|
|
|
terminated: Cell::new(false),
|
|
|
|
unreachable: Cell::new(false),
|
2013-07-17 05:12:08 -05:00
|
|
|
is_lpad: is_lpad,
|
2014-01-15 13:39:08 -06:00
|
|
|
opt_node_id: opt_node_id,
|
|
|
|
fcx: fcx
|
2014-01-07 10:54:58 -06:00
|
|
|
})
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
|
|
|
|
self.fcx.ccx
|
|
|
|
}
|
|
|
|
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
|
2014-09-05 11:18:53 -05:00
|
|
|
self.fcx.ccx.tcx()
|
2014-01-07 10:54:58 -06:00
|
|
|
}
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }
|
2012-09-05 17:58:43 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn ident(&self, ident: Ident) -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
token::get_ident(ident).get().to_string()
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
2012-09-05 17:58:43 -05:00
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn node_id_to_string(&self, id: ast::NodeId) -> String {
|
|
|
|
self.tcx().map.node_to_string(id).to_string()
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn expr_to_string(&self, e: &ast::Expr) -> String {
|
2013-07-17 05:12:08 -05:00
|
|
|
e.repr(self.tcx())
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
pub fn def(&self, nid: ast::NodeId) -> def::Def {
|
2014-11-06 11:25:16 -06:00
|
|
|
match self.tcx().def_map.borrow().get(&nid) {
|
2014-09-07 12:09:06 -05:00
|
|
|
Some(v) => v.clone(),
|
2013-07-17 05:12:08 -05:00
|
|
|
None => {
|
2013-09-28 00:38:08 -05:00
|
|
|
self.tcx().sess.bug(format!(
|
2015-01-01 18:56:28 -06:00
|
|
|
"no def associated with node id {}", nid).index(&FullRange));
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn val_to_string(&self, val: ValueRef) -> String {
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx().tn().val_to_string(val)
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn llty_str(&self, ty: Type) -> String {
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx().tn().type_to_string(ty)
|
2012-06-12 16:55:44 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
|
2013-07-17 05:12:08 -05:00
|
|
|
t.repr(self.tcx())
|
|
|
|
}
|
2012-06-12 16:55:44 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn to_str(&self) -> String {
|
2014-09-06 11:13:04 -05:00
|
|
|
format!("[block {:p}]", self)
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
2014-12-17 13:16:28 -06:00
|
|
|
|
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
|
|
|
where T : TypeFoldable<'tcx> + Repr<'tcx> + HasProjectionTypes + Clone
|
|
|
|
{
|
|
|
|
monomorphize::apply_param_substs(self.tcx(),
|
|
|
|
self.fcx.param_substs,
|
|
|
|
value)
|
|
|
|
}
|
2012-06-12 16:55:44 -05:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
impl<'blk, 'tcx> mc::Typer<'tcx> for BlockS<'blk, 'tcx> {
|
2014-04-22 07:56:37 -05:00
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
|
2014-08-01 21:38:10 -05:00
|
|
|
self.tcx()
|
|
|
|
}
|
|
|
|
|
2015-01-02 03:09:35 -06:00
|
|
|
fn node_ty(&self, id: ast::NodeId) -> mc::McResult<Ty<'tcx>> {
|
|
|
|
Ok(node_id_type(self, id))
|
2014-08-01 21:38:10 -05:00
|
|
|
}
|
|
|
|
|
2015-01-02 03:09:35 -06:00
|
|
|
fn expr_ty_adjusted(&self, expr: &ast::Expr) -> mc::McResult<Ty<'tcx>> {
|
|
|
|
Ok(expr_ty_adjusted(self, expr))
|
2014-12-03 12:06:48 -06:00
|
|
|
}
|
|
|
|
|
2014-11-25 13:21:20 -06:00
|
|
|
fn node_method_ty(&self, method_call: ty::MethodCall) -> Option<Ty<'tcx>> {
|
2014-11-01 16:59:22 -05:00
|
|
|
self.tcx()
|
|
|
|
.method_map
|
|
|
|
.borrow()
|
2014-11-06 11:25:16 -06:00
|
|
|
.get(&method_call)
|
2014-11-01 16:59:22 -05:00
|
|
|
.map(|method| monomorphize_type(self, method.ty))
|
2014-08-01 21:38:10 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 12:20:25 -06:00
|
|
|
fn node_method_origin(&self, method_call: ty::MethodCall)
|
|
|
|
-> Option<ty::MethodOrigin<'tcx>>
|
|
|
|
{
|
|
|
|
self.tcx()
|
|
|
|
.method_map
|
|
|
|
.borrow()
|
|
|
|
.get(&method_call)
|
|
|
|
.map(|method| method.origin.clone())
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn adjustments<'a>(&'a self) -> &'a RefCell<NodeMap<ty::AutoAdjustment<'tcx>>> {
|
2014-08-01 21:38:10 -05:00
|
|
|
&self.tcx().adjustments
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_method_call(&self, id: ast::NodeId) -> bool {
|
2014-11-25 13:21:20 -06:00
|
|
|
self.tcx().method_map.borrow().contains_key(&ty::MethodCall::expr(id))
|
2014-08-01 21:38:10 -05:00
|
|
|
}
|
|
|
|
|
2014-11-18 07:22:59 -06:00
|
|
|
fn temporary_scope(&self, rvalue_id: ast::NodeId) -> Option<region::CodeExtent> {
|
2014-08-01 21:38:10 -05:00
|
|
|
self.tcx().region_maps.temporary_scope(rvalue_id)
|
|
|
|
}
|
|
|
|
|
2015-01-01 15:04:51 -06:00
|
|
|
fn upvar_borrow(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarBorrow> {
|
|
|
|
Some(self.tcx().upvar_borrow_map.borrow()[upvar_id].clone())
|
2014-08-01 21:38:10 -05:00
|
|
|
}
|
2014-07-23 14:43:29 -05:00
|
|
|
|
|
|
|
fn capture_mode(&self, closure_expr_id: ast::NodeId)
|
2014-09-14 15:55:25 -05:00
|
|
|
-> ast::CaptureClause {
|
2014-11-07 13:35:18 -06:00
|
|
|
self.tcx().capture_modes.borrow()[closure_expr_id].clone()
|
2014-07-23 14:43:29 -05:00
|
|
|
}
|
2015-01-02 03:01:30 -06:00
|
|
|
|
|
|
|
fn type_moves_by_default(&self, span: Span, ty: Ty<'tcx>) -> bool {
|
2015-01-02 03:09:35 -06:00
|
|
|
self.fcx.param_env.type_moves_by_default(span, ty)
|
2015-01-02 03:01:30 -06:00
|
|
|
}
|
2014-08-01 21:38:10 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 15:04:51 -06:00
|
|
|
impl<'blk, 'tcx> ty::UnboxedClosureTyper<'tcx> for BlockS<'blk, 'tcx> {
|
2015-01-02 03:09:35 -06:00
|
|
|
fn param_env<'a>(&'a self) -> &'a ty::ParameterEnvironment<'a, 'tcx> {
|
|
|
|
&self.fcx.param_env
|
|
|
|
}
|
|
|
|
|
2015-01-01 15:04:51 -06:00
|
|
|
fn unboxed_closure_kind(&self,
|
|
|
|
def_id: ast::DefId)
|
|
|
|
-> ty::UnboxedClosureKind
|
|
|
|
{
|
2015-01-01 16:58:57 -06:00
|
|
|
let typer = NormalizingUnboxedClosureTyper::new(self.tcx());
|
|
|
|
typer.unboxed_closure_kind(def_id)
|
2015-01-01 15:04:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unboxed_closure_type(&self,
|
|
|
|
def_id: ast::DefId,
|
|
|
|
substs: &subst::Substs<'tcx>)
|
|
|
|
-> ty::ClosureTy<'tcx>
|
|
|
|
{
|
2015-01-01 16:58:57 -06:00
|
|
|
let typer = NormalizingUnboxedClosureTyper::new(self.tcx());
|
|
|
|
typer.unboxed_closure_type(def_id, substs)
|
2015-01-01 15:04:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unboxed_closure_upvars(&self,
|
|
|
|
def_id: ast::DefId,
|
|
|
|
substs: &Substs<'tcx>)
|
|
|
|
-> Option<Vec<ty::UnboxedClosureUpvar<'tcx>>>
|
|
|
|
{
|
2015-01-01 16:58:57 -06:00
|
|
|
let typer = NormalizingUnboxedClosureTyper::new(self.tcx());
|
|
|
|
typer.unboxed_closure_upvars(def_id, substs)
|
2015-01-01 15:04:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct Result<'blk, 'tcx: 'blk> {
|
2014-09-06 11:13:04 -05:00
|
|
|
pub bcx: Block<'blk, 'tcx>,
|
2014-03-28 12:05:27 -05:00
|
|
|
pub val: ValueRef
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'b, 'tcx> Result<'b, 'tcx> {
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn new(bcx: Block<'b, 'tcx>, val: ValueRef) -> Result<'b, 'tcx> {
|
2014-05-03 06:14:56 -05:00
|
|
|
Result {
|
|
|
|
bcx: bcx,
|
|
|
|
val: val,
|
|
|
|
}
|
2014-01-07 10:54:58 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
pub fn val_ty(v: ValueRef) -> Type {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-16 05:52:44 -05:00
|
|
|
Type::from_ref(llvm::LLVMTypeOf(v))
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
// LLVM constant constructors.
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_null(t: Type) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-15 22:45:48 -05:00
|
|
|
llvm::LLVMConstNull(t.to_ref())
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_undef(t: Type) -> ValueRef {
|
2013-02-18 16:16:21 -06:00
|
|
|
unsafe {
|
2013-06-15 22:45:48 -05:00
|
|
|
llvm::LLVMGetUndef(t.to_ref())
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-15 22:45:48 -05:00
|
|
|
llvm::LLVMConstInt(t.to_ref(), u, sign_extend as Bool)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_floating(s: &str, t: Type) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-11-25 15:28:35 -06:00
|
|
|
let s = CString::from_slice(s.as_bytes());
|
|
|
|
llvm::LLVMConstRealOfString(t.to_ref(), s.as_ptr())
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_nil(ccx: &CrateContext) -> ValueRef {
|
2014-11-17 02:39:01 -06:00
|
|
|
C_struct(ccx, &[], false)
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_bool(ccx: &CrateContext, val: bool) -> ValueRef {
|
|
|
|
C_integral(Type::i1(ccx), val as u64, false)
|
2013-02-06 16:28:02 -06:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_i32(ccx: &CrateContext, i: i32) -> ValueRef {
|
|
|
|
C_integral(Type::i32(ccx), i as u64, true)
|
2011-10-26 00:23:28 -05:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_i64(ccx: &CrateContext, i: i64) -> ValueRef {
|
|
|
|
C_integral(Type::i64(ccx), i as u64, true)
|
2011-11-15 20:11:22 -06:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
|
|
|
|
C_integral(Type::i64(ccx), i, false)
|
2013-09-29 04:20:11 -05:00
|
|
|
}
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
|
2014-10-15 12:26:43 -05:00
|
|
|
let v = i.as_i64();
|
|
|
|
|
2014-10-15 13:12:30 -05:00
|
|
|
match machine::llbitsize_of_real(ccx, ccx.int_type()) {
|
2014-10-15 12:26:43 -05:00
|
|
|
32 => assert!(v < (1<<31) && v >= -(1<<31)),
|
|
|
|
64 => {},
|
2014-10-09 14:17:22 -05:00
|
|
|
n => panic!("unsupported target size: {}", n)
|
2014-10-15 12:26:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
C_integral(ccx.int_type(), v as u64, true)
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn C_uint<I: AsU64>(ccx: &CrateContext, i: I) -> ValueRef {
|
2014-10-15 12:26:43 -05:00
|
|
|
let v = i.as_u64();
|
|
|
|
|
2014-10-15 13:12:30 -05:00
|
|
|
match machine::llbitsize_of_real(ccx, ccx.int_type()) {
|
2014-10-15 12:26:43 -05:00
|
|
|
32 => assert!(v < (1<<32)),
|
|
|
|
64 => {},
|
2014-10-09 14:17:22 -05:00
|
|
|
n => panic!("unsupported target size: {}", n)
|
2014-10-15 12:26:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
C_integral(ccx.int_type(), v, false)
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub trait AsI64 { fn as_i64(self) -> i64; }
|
|
|
|
pub trait AsU64 { fn as_u64(self) -> u64; }
|
|
|
|
|
2014-10-15 12:26:43 -05:00
|
|
|
// FIXME: remove the intptr conversions, because they
|
|
|
|
// are host-architecture-dependent
|
2014-10-14 15:36:11 -05:00
|
|
|
impl AsI64 for i64 { fn as_i64(self) -> i64 { self as i64 }}
|
|
|
|
impl AsI64 for i32 { fn as_i64(self) -> i64 { self as i64 }}
|
|
|
|
impl AsI64 for int { fn as_i64(self) -> i64 { self as i64 }}
|
|
|
|
|
|
|
|
impl AsU64 for u64 { fn as_u64(self) -> u64 { self as u64 }}
|
|
|
|
impl AsU64 for u32 { fn as_u64(self) -> u64 { self as u64 }}
|
|
|
|
impl AsU64 for uint { fn as_u64(self) -> u64 { self as u64 }}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_u8(ccx: &CrateContext, i: uint) -> ValueRef {
|
|
|
|
C_integral(Type::i8(ccx), i as u64, false)
|
2013-01-30 13:46:19 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
// This is a 'c-like' raw string, which differs from
|
|
|
|
// our boxed-and-length-annotated strings.
|
2014-04-03 15:26:08 -05:00
|
|
|
pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-11-06 11:25:16 -06:00
|
|
|
match cx.const_cstr_cache().borrow().get(&s) {
|
2014-03-20 21:49:20 -05:00
|
|
|
Some(&llval) => return llval,
|
|
|
|
None => ()
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-04-21 15:23:25 -05:00
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
let sc = llvm::LLVMConstStringInContext(cx.llcx(),
|
2014-06-25 14:47:34 -05:00
|
|
|
s.get().as_ptr() as *const c_char,
|
2014-01-10 16:02:36 -06:00
|
|
|
s.get().len() as c_uint,
|
2014-04-03 15:26:08 -05:00
|
|
|
!null_terminated as Bool);
|
2013-06-15 22:45:48 -05:00
|
|
|
|
|
|
|
let gsym = token::gensym("str");
|
2014-11-25 15:28:35 -06:00
|
|
|
let buf = CString::from_vec(format!("str{}", gsym.uint()).into_bytes());
|
|
|
|
let g = llvm::LLVMAddGlobal(cx.llmod(), val_ty(sc).to_ref(), buf.as_ptr());
|
2013-01-10 23:23:07 -06:00
|
|
|
llvm::LLVMSetInitializer(g, sc);
|
|
|
|
llvm::LLVMSetGlobalConstant(g, True);
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::SetLinkage(g, llvm::InternalLinkage);
|
2012-04-21 15:23:25 -05:00
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
cx.const_cstr_cache().borrow_mut().insert(s, g);
|
2013-12-18 19:02:30 -06:00
|
|
|
g
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2013-01-05 01:06:25 -06:00
|
|
|
// NB: Do not use `do_spill_noroot` to make this into a constant string, or
|
|
|
|
// you will be kicked off fast isel. See issue #4352 for an example of this.
|
2014-01-10 16:02:36 -06:00
|
|
|
pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
|
2015-01-04 10:47:58 -06:00
|
|
|
let len = s.get().len();
|
|
|
|
let cs = consts::ptrcast(C_cstr(cx, s, false), Type::i8p(cx));
|
|
|
|
C_named_struct(cx.tn().find_type("str_slice").unwrap(), &[cs, C_uint(cx, len)])
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn C_binary_slice(cx: &CrateContext, data: &[u8]) -> ValueRef {
|
2013-10-14 10:24:17 -05:00
|
|
|
unsafe {
|
|
|
|
let len = data.len();
|
2014-03-15 15:29:34 -05:00
|
|
|
let lldata = C_bytes(cx, data);
|
2013-10-14 10:24:17 -05:00
|
|
|
|
|
|
|
let gsym = token::gensym("binary");
|
2014-11-25 15:28:35 -06:00
|
|
|
let name = format!("binary{}", gsym.uint());
|
|
|
|
let name = CString::from_vec(name.into_bytes());
|
|
|
|
let g = llvm::LLVMAddGlobal(cx.llmod(), val_ty(lldata).to_ref(),
|
|
|
|
name.as_ptr());
|
2013-10-14 10:24:17 -05:00
|
|
|
llvm::LLVMSetInitializer(g, lldata);
|
|
|
|
llvm::LLVMSetGlobalConstant(g, True);
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::SetLinkage(g, llvm::InternalLinkage);
|
2013-10-14 10:24:17 -05:00
|
|
|
|
2015-01-04 10:47:58 -06:00
|
|
|
let cs = consts::ptrcast(g, Type::i8p(cx));
|
2014-11-17 02:39:01 -06:00
|
|
|
C_struct(cx, &[cs, C_uint(cx, len)], false)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
pub fn C_struct(cx: &CrateContext, elts: &[ValueRef], packed: bool) -> ValueRef {
|
|
|
|
C_struct_in_context(cx.llcx(), elts, packed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn C_struct_in_context(llcx: ContextRef, elts: &[ValueRef], packed: bool) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
llvm::LLVMConstStructInContext(llcx,
|
2013-12-17 08:49:31 -06:00
|
|
|
elts.as_ptr(), elts.len() as c_uint,
|
|
|
|
packed as Bool)
|
2013-01-23 18:25:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn C_named_struct(t: Type, elts: &[ValueRef]) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMConstNamedStruct(t.to_ref(), elts.as_ptr(), elts.len() as c_uint)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
pub fn C_array(ty: Type, elts: &[ValueRef]) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-12-15 06:35:12 -06:00
|
|
|
return llvm::LLVMConstArray(ty.to_ref(), elts.as_ptr(), elts.len() as c_uint);
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-27 17:14:59 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
pub fn C_bytes(cx: &CrateContext, bytes: &[u8]) -> ValueRef {
|
|
|
|
C_bytes_in_context(cx.llcx(), bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn C_bytes_in_context(llcx: ContextRef, bytes: &[u8]) -> ValueRef {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-06-25 14:47:34 -05:00
|
|
|
let ptr = bytes.as_ptr() as *const c_char;
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-09-05 17:27:22 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
|
2013-02-18 16:16:21 -06:00
|
|
|
-> ValueRef {
|
|
|
|
unsafe {
|
2013-12-17 08:49:31 -06:00
|
|
|
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("const_get_elt(v={}, us={}, r={})",
|
2014-09-05 11:18:53 -05:00
|
|
|
cx.tn().val_to_string(v), us, cx.tn().val_to_string(r));
|
2013-02-18 16:16:21 -06:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 03:51:45 -05:00
|
|
|
pub fn is_const(v: ValueRef) -> bool {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsConstant(v) == True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn const_to_int(v: ValueRef) -> i64 {
|
2013-02-18 16:16:21 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstIntGetSExtValue(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn const_to_uint(v: ValueRef) -> u64 {
|
2013-02-18 16:16:21 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstIntGetZExtValue(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_undef(val: ValueRef) -> bool {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsUndef(val) != False
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:44:51 -06:00
|
|
|
#[allow(dead_code)] // potentially useful
|
2013-03-31 17:55:30 -05:00
|
|
|
pub fn is_null(val: ValueRef) -> bool {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsNull(val) != False
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn monomorphize_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, t: Ty<'tcx>) -> Ty<'tcx> {
|
2014-12-17 13:16:28 -06:00
|
|
|
bcx.fcx.monomorphize(&t)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn node_id_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, id: ast::NodeId) -> Ty<'tcx> {
|
2012-02-21 07:20:18 -06:00
|
|
|
let tcx = bcx.tcx();
|
2012-02-02 05:37:17 -06:00
|
|
|
let t = ty::node_id_to_type(tcx, id);
|
2012-09-11 23:25:01 -05:00
|
|
|
monomorphize_type(bcx, t)
|
2012-02-02 05:37:17 -06:00
|
|
|
}
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2014-12-03 12:06:48 -06:00
|
|
|
pub fn expr_ty<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, ex: &ast::Expr) -> Ty<'tcx> {
|
2012-02-02 05:37:17 -06:00
|
|
|
node_id_type(bcx, ex.id)
|
|
|
|
}
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2014-12-03 12:06:48 -06:00
|
|
|
pub fn expr_ty_adjusted<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, ex: &ast::Expr) -> Ty<'tcx> {
|
2014-04-09 10:18:40 -05:00
|
|
|
monomorphize_type(bcx, ty::expr_ty_adjusted(bcx.tcx(), ex))
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Attempts to resolve an obligation. The result is a shallow vtable resolution -- meaning that we
|
|
|
|
/// do not (necessarily) resolve all nested obligations on the impl. Note that type check should
|
|
|
|
/// guarantee to us that all nested obligations *could be* resolved if we wanted to.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn fulfill_obligation<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
span: Span,
|
2014-12-17 13:16:28 -06:00
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> traits::Vtable<'tcx, ()>
|
2014-09-12 10:42:58 -05:00
|
|
|
{
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
|
|
|
|
// Remove any references to regions; this helps improve caching.
|
|
|
|
let trait_ref = ty_fold::erase_regions(tcx, trait_ref);
|
|
|
|
|
|
|
|
// First check the cache.
|
2014-11-06 11:25:16 -06:00
|
|
|
match ccx.trait_cache().borrow().get(&trait_ref) {
|
2014-09-12 10:42:58 -05:00
|
|
|
Some(vtable) => {
|
|
|
|
info!("Cache hit: {}", trait_ref.repr(ccx.tcx()));
|
|
|
|
return (*vtable).clone();
|
|
|
|
}
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
|
2014-11-15 15:50:34 -06:00
|
|
|
debug!("trans fulfill_obligation: trait_ref={}", trait_ref.repr(ccx.tcx()));
|
|
|
|
|
2014-12-11 12:37:37 -06:00
|
|
|
ty::populate_implementations_for_trait_if_necessary(tcx, trait_ref.def_id());
|
2014-09-12 10:42:58 -05:00
|
|
|
let infcx = infer::new_infer_ctxt(tcx);
|
|
|
|
|
|
|
|
// Do the initial selection for the obligation. This yields the
|
|
|
|
// shallow result we are looking for -- that is, what specific impl.
|
2015-01-02 03:09:35 -06:00
|
|
|
let typer = NormalizingUnboxedClosureTyper::new(tcx);
|
|
|
|
let mut selcx = traits::SelectionContext::new(&infcx, &typer);
|
2014-12-05 00:59:17 -06:00
|
|
|
let obligation = traits::Obligation::new(traits::ObligationCause::dummy(),
|
2014-12-17 13:16:28 -06:00
|
|
|
trait_ref.to_poly_trait_predicate());
|
2014-09-12 10:42:58 -05:00
|
|
|
let selection = match selcx.select(&obligation) {
|
|
|
|
Ok(Some(selection)) => selection,
|
|
|
|
Ok(None) => {
|
2014-10-09 16:19:50 -05:00
|
|
|
// Ambiguity can happen when monomorphizing during trans
|
|
|
|
// expands to some humongo type that never occurred
|
|
|
|
// statically -- this humongo type can then overflow,
|
|
|
|
// leading to an ambiguous result. So report this as an
|
|
|
|
// overflow bug, since I believe this is the only case
|
|
|
|
// where ambiguity can result.
|
|
|
|
debug!("Encountered ambiguity selecting `{}` during trans, \
|
|
|
|
presuming due to overflow",
|
|
|
|
trait_ref.repr(tcx));
|
|
|
|
ccx.sess().span_fatal(
|
2014-09-12 10:42:58 -05:00
|
|
|
span,
|
2014-10-09 16:19:50 -05:00
|
|
|
"reached the recursion limit during monomorphization");
|
2014-09-12 10:42:58 -05:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tcx.sess.span_bug(
|
|
|
|
span,
|
|
|
|
format!("Encountered error `{}` selecting `{}` during trans",
|
|
|
|
e.repr(tcx),
|
2015-01-01 18:56:28 -06:00
|
|
|
trait_ref.repr(tcx)).index(&FullRange))
|
2014-09-12 10:42:58 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Currently, we use a fulfillment context to completely resolve
|
|
|
|
// all nested obligations. This is because they can inform the
|
2014-12-31 13:42:06 -06:00
|
|
|
// inference of the impl's type parameters.
|
2014-09-12 10:42:58 -05:00
|
|
|
let mut fulfill_cx = traits::FulfillmentContext::new();
|
2014-12-07 10:10:48 -06:00
|
|
|
let vtable = selection.map_move_nested(|predicate| {
|
2014-12-30 16:42:02 -06:00
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, predicate);
|
2014-09-12 10:42:58 -05:00
|
|
|
});
|
2015-01-02 03:09:35 -06:00
|
|
|
let vtable = drain_fulfillment_cx(span, &infcx, &mut fulfill_cx, &vtable);
|
2014-12-31 13:42:06 -06:00
|
|
|
|
|
|
|
info!("Cache miss: {}", trait_ref.repr(ccx.tcx()));
|
|
|
|
ccx.trait_cache().borrow_mut().insert(trait_ref,
|
|
|
|
vtable.clone());
|
|
|
|
|
|
|
|
vtable
|
|
|
|
}
|
|
|
|
|
2015-01-01 16:58:57 -06:00
|
|
|
pub struct NormalizingUnboxedClosureTyper<'a,'tcx:'a> {
|
2015-01-02 03:09:35 -06:00
|
|
|
param_env: ty::ParameterEnvironment<'a, 'tcx>
|
2015-01-01 16:58:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx> NormalizingUnboxedClosureTyper<'a,'tcx> {
|
|
|
|
pub fn new(tcx: &'a ty::ctxt<'tcx>) -> NormalizingUnboxedClosureTyper<'a,'tcx> {
|
2015-01-02 03:09:35 -06:00
|
|
|
// Parameter environment is used to give details about type parameters,
|
|
|
|
// but since we are in trans, everything is fully monomorphized.
|
|
|
|
NormalizingUnboxedClosureTyper { param_env: ty::empty_parameter_environment(tcx) }
|
2015-01-01 16:58:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx> ty::UnboxedClosureTyper<'tcx> for NormalizingUnboxedClosureTyper<'a,'tcx> {
|
2015-01-02 03:09:35 -06:00
|
|
|
fn param_env<'b>(&'b self) -> &'b ty::ParameterEnvironment<'b,'tcx> {
|
|
|
|
&self.param_env
|
|
|
|
}
|
|
|
|
|
2015-01-01 16:58:57 -06:00
|
|
|
fn unboxed_closure_kind(&self,
|
|
|
|
def_id: ast::DefId)
|
|
|
|
-> ty::UnboxedClosureKind
|
|
|
|
{
|
2015-01-02 03:09:35 -06:00
|
|
|
self.param_env.tcx.unboxed_closure_kind(def_id)
|
2015-01-01 16:58:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unboxed_closure_type(&self,
|
|
|
|
def_id: ast::DefId,
|
|
|
|
substs: &subst::Substs<'tcx>)
|
|
|
|
-> ty::ClosureTy<'tcx>
|
|
|
|
{
|
|
|
|
// the substitutions in `substs` are already monomorphized,
|
|
|
|
// but we still must normalize associated types
|
2015-01-02 03:09:35 -06:00
|
|
|
let closure_ty = self.param_env.tcx.unboxed_closure_type(def_id, substs);
|
|
|
|
monomorphize::normalize_associated_type(self.param_env.tcx, &closure_ty)
|
2015-01-01 16:58:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unboxed_closure_upvars(&self,
|
|
|
|
def_id: ast::DefId,
|
|
|
|
substs: &Substs<'tcx>)
|
|
|
|
-> Option<Vec<ty::UnboxedClosureUpvar<'tcx>>>
|
|
|
|
{
|
|
|
|
// the substitutions in `substs` are already monomorphized,
|
|
|
|
// but we still must normalize associated types
|
2015-01-02 03:09:35 -06:00
|
|
|
let result = ty::unboxed_closure_upvars(&self.param_env, def_id, substs);
|
|
|
|
monomorphize::normalize_associated_type(self.param_env.tcx, &result)
|
2015-01-01 16:58:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:42:06 -06:00
|
|
|
pub fn drain_fulfillment_cx<'a,'tcx,T>(span: Span,
|
|
|
|
infcx: &infer::InferCtxt<'a,'tcx>,
|
|
|
|
fulfill_cx: &mut traits::FulfillmentContext<'tcx>,
|
|
|
|
result: &T)
|
|
|
|
-> T
|
|
|
|
where T : TypeFoldable<'tcx> + Repr<'tcx>
|
|
|
|
{
|
|
|
|
debug!("drain_fulfillment_cx(result={})",
|
|
|
|
result.repr(infcx.tcx));
|
|
|
|
|
|
|
|
// In principle, we only need to do this so long as `result`
|
|
|
|
// contains unbound type parameters. It could be a slight
|
|
|
|
// optimization to stop iterating early.
|
2015-01-01 16:58:57 -06:00
|
|
|
let typer = NormalizingUnboxedClosureTyper::new(infcx.tcx);
|
2015-01-02 03:09:35 -06:00
|
|
|
match fulfill_cx.select_all_or_error(infcx, &typer) {
|
2014-09-12 10:42:58 -05:00
|
|
|
Ok(()) => { }
|
2014-10-09 16:19:50 -05:00
|
|
|
Err(errors) => {
|
|
|
|
if errors.iter().all(|e| e.is_overflow()) {
|
|
|
|
// See Ok(None) case above.
|
2014-12-31 13:42:06 -06:00
|
|
|
infcx.tcx.sess.span_fatal(
|
2014-10-09 16:19:50 -05:00
|
|
|
span,
|
|
|
|
"reached the recursion limit during monomorphization");
|
|
|
|
} else {
|
2014-12-31 13:42:06 -06:00
|
|
|
infcx.tcx.sess.span_bug(
|
2014-10-09 16:19:50 -05:00
|
|
|
span,
|
2014-12-31 13:42:06 -06:00
|
|
|
format!("Encountered errors `{}` fulfilling during trans",
|
2015-01-01 18:56:28 -06:00
|
|
|
errors.repr(infcx.tcx)).index(&FullRange));
|
2014-10-09 16:19:50 -05:00
|
|
|
}
|
2014-09-12 10:42:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:13:42 -06:00
|
|
|
// Use freshen to simultaneously replace all type variables with
|
2014-09-12 10:42:58 -05:00
|
|
|
// their bindings and replace all regions with 'static. This is
|
|
|
|
// sort of overkill because we do not expect there to be any
|
2014-12-12 05:13:42 -06:00
|
|
|
// unbound type variables, hence no `TyFresh` types should ever be
|
|
|
|
// inserted.
|
2014-12-31 13:42:06 -06:00
|
|
|
result.fold_with(&mut infcx.freshener())
|
2014-09-12 10:42:58 -05:00
|
|
|
}
|
|
|
|
|
2014-03-08 10:33:39 -06:00
|
|
|
// Key used to lookup values supplied for type parameters in an expr.
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Copy, PartialEq, Show)]
|
2014-03-06 11:24:11 -06:00
|
|
|
pub enum ExprOrMethodCall {
|
2014-03-08 10:33:39 -06:00
|
|
|
// Type parameters for a path like `None::<int>`
|
2014-03-06 11:24:11 -06:00
|
|
|
ExprId(ast::NodeId),
|
2014-03-08 10:33:39 -06:00
|
|
|
|
|
|
|
// Type parameters for a method call like `a.foo::<int>()`
|
2014-12-14 22:03:00 -06:00
|
|
|
MethodCallKey(ty::MethodCall)
|
2014-03-06 11:24:11 -06:00
|
|
|
}
|
|
|
|
|
2015-01-04 10:47:58 -06:00
|
|
|
pub fn node_id_substs<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
node: ExprOrMethodCall,
|
|
|
|
param_substs: &subst::Substs<'tcx>)
|
|
|
|
-> subst::Substs<'tcx> {
|
|
|
|
let tcx = ccx.tcx();
|
2014-05-07 06:20:15 -05:00
|
|
|
|
|
|
|
let substs = match node {
|
|
|
|
ExprId(id) => {
|
|
|
|
ty::node_id_item_substs(tcx, id).substs
|
|
|
|
}
|
2014-12-14 22:03:00 -06:00
|
|
|
MethodCallKey(method_call) => {
|
2014-10-15 01:05:01 -05:00
|
|
|
(*tcx.method_map.borrow())[method_call].substs.clone()
|
2014-03-06 11:24:11 -06:00
|
|
|
}
|
2014-02-26 08:06:45 -06:00
|
|
|
};
|
2013-03-21 07:34:18 -05:00
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
if substs.types.any(|t| ty::type_needs_infer(*t)) {
|
2015-01-04 10:47:58 -06:00
|
|
|
tcx.sess.bug(format!("type parameters for node {} include inference types: {}",
|
2015-01-01 18:56:28 -06:00
|
|
|
node, substs.repr(tcx)).index(&FullRange));
|
2013-03-21 07:34:18 -05:00
|
|
|
}
|
|
|
|
|
2015-01-04 10:47:58 -06:00
|
|
|
monomorphize::apply_param_substs(tcx,
|
|
|
|
param_substs,
|
|
|
|
&substs.erase_regions())
|
2012-02-09 07:33:00 -06:00
|
|
|
}
|
2012-02-02 05:37:17 -06:00
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn langcall(bcx: Block,
|
2014-01-07 10:54:58 -06:00
|
|
|
span: Option<Span>,
|
|
|
|
msg: &str,
|
|
|
|
li: LangItem)
|
|
|
|
-> ast::DefId {
|
2013-07-15 22:42:13 -05:00
|
|
|
match bcx.tcx().lang_items.require(li) {
|
|
|
|
Ok(id) => id,
|
|
|
|
Err(s) => {
|
2013-09-28 00:38:08 -05:00
|
|
|
let msg = format!("{} {}", msg, s);
|
2013-07-15 22:42:13 -05:00
|
|
|
match span {
|
2015-01-01 18:56:28 -06:00
|
|
|
Some(span) => bcx.tcx().sess.span_fatal(span, msg.index(&FullRange)),
|
|
|
|
None => bcx.tcx().sess.fatal(msg.index(&FullRange)),
|
2013-07-15 22:42:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|