2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use metadata::csearch;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::astencode;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::{get_insn_ctxt};
|
|
|
|
use middle::trans::base::{impl_owned_self, impl_self, no_self};
|
2013-03-26 15:38:07 -05:00
|
|
|
use middle::trans::base::{trans_item, get_item_val, trans_fn};
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
|
|
|
use util::ppaux::ty_to_str;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::vec;
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2013-03-26 15:38:07 -05:00
|
|
|
use syntax::ast_map::path_name;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast_util::local_def;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-10-08 14:39:30 -05:00
|
|
|
// `translate` will be true if this function is allowed to translate the
|
|
|
|
// item and false otherwise. Currently, this parameter is set to false when
|
|
|
|
// translating default methods.
|
2013-02-19 01:40:42 -06:00
|
|
|
pub fn maybe_instantiate_inline(ccx: @CrateContext, fn_id: ast::def_id,
|
2013-01-29 19:57:02 -06:00
|
|
|
translate: bool)
|
2012-10-08 14:39:30 -05:00
|
|
|
-> ast::def_id {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = ccx.insn_ctxt("maybe_instantiate_inline");
|
2013-02-05 21:41:45 -06:00
|
|
|
match ccx.external.find(&fn_id) {
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&Some(node_id)) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Already inline
|
|
|
|
debug!("maybe_instantiate_inline(%s): already inline as node id %d",
|
|
|
|
ty::item_path_str(ccx.tcx, fn_id), node_id);
|
|
|
|
local_def(node_id)
|
|
|
|
}
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&None) => fn_id, // Not inlinable
|
2012-08-28 17:54:45 -05:00
|
|
|
None => { // Not seen yet
|
|
|
|
match csearch::maybe_get_item_ast(
|
|
|
|
ccx.tcx, fn_id,
|
|
|
|
|a,b,c,d| {
|
2013-01-10 08:29:26 -06:00
|
|
|
astencode::decode_inlined_item(a, b, ccx.maps,
|
|
|
|
/*bad*/ copy c, d)
|
2012-08-28 17:54:45 -05:00
|
|
|
}) {
|
|
|
|
|
|
|
|
csearch::not_found => {
|
|
|
|
ccx.external.insert(fn_id, None);
|
|
|
|
fn_id
|
|
|
|
}
|
|
|
|
csearch::found(ast::ii_item(item)) => {
|
|
|
|
ccx.external.insert(fn_id, Some(item.id));
|
2012-09-12 16:48:13 -05:00
|
|
|
ccx.stats.n_inlines += 1;
|
2012-10-08 14:39:30 -05:00
|
|
|
if translate { trans_item(ccx, *item); }
|
2012-08-28 17:54:45 -05:00
|
|
|
local_def(item.id)
|
|
|
|
}
|
|
|
|
csearch::found(ast::ii_foreign(item)) => {
|
|
|
|
ccx.external.insert(fn_id, Some(item.id));
|
|
|
|
local_def(item.id)
|
|
|
|
}
|
|
|
|
csearch::found_parent(parent_id, ast::ii_item(item)) => {
|
|
|
|
ccx.external.insert(parent_id, Some(item.id));
|
|
|
|
let mut my_id = 0;
|
|
|
|
match item.node {
|
|
|
|
ast::item_enum(_, _) => {
|
|
|
|
let vs_here = ty::enum_variants(ccx.tcx, local_def(item.id));
|
|
|
|
let vs_there = ty::enum_variants(ccx.tcx, parent_id);
|
2012-09-28 17:48:25 -05:00
|
|
|
for vec::each2(*vs_here, *vs_there) |here, there| {
|
2012-08-28 17:54:45 -05:00
|
|
|
if there.id == fn_id { my_id = here.id.node; }
|
|
|
|
ccx.external.insert(there.id, Some(here.id.node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ccx.sess.bug(~"maybe_instantiate_inline: item has a \
|
|
|
|
non-enum parent")
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
if translate { trans_item(ccx, *item); }
|
2012-08-28 17:54:45 -05:00
|
|
|
local_def(my_id)
|
|
|
|
}
|
|
|
|
csearch::found_parent(_, _) => {
|
|
|
|
ccx.sess.bug(~"maybe_get_item_ast returned a found_parent \
|
|
|
|
with a non-item parent");
|
|
|
|
}
|
|
|
|
csearch::found(ast::ii_method(impl_did, mth)) => {
|
2012-09-12 16:48:13 -05:00
|
|
|
ccx.stats.n_inlines += 1;
|
2012-08-28 17:54:45 -05:00
|
|
|
ccx.external.insert(fn_id, Some(mth.id));
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
let impl_tpt = ty::lookup_item_type(ccx.tcx, impl_did);
|
|
|
|
let num_type_params =
|
|
|
|
impl_tpt.generics.type_param_defs.len() +
|
|
|
|
mth.generics.ty_params.len();
|
|
|
|
if translate && num_type_params == 0 {
|
2012-08-28 17:54:45 -05:00
|
|
|
let llfn = get_item_val(ccx, mth.id);
|
|
|
|
let path = vec::append(
|
|
|
|
ty::item_path(ccx.tcx, impl_did),
|
|
|
|
~[path_name(mth.ident)]);
|
2012-12-02 17:45:20 -06:00
|
|
|
let self_kind = match mth.self_ty.node {
|
|
|
|
ast::sty_static => no_self,
|
|
|
|
_ => {
|
2012-12-05 20:10:45 -06:00
|
|
|
let self_ty = ty::node_id_to_type(ccx.tcx,
|
|
|
|
mth.self_id);
|
2012-12-02 17:45:20 -06:00
|
|
|
debug!("calling inline trans_fn with self_ty %s",
|
|
|
|
ty_to_str(ccx.tcx, self_ty));
|
|
|
|
match mth.self_ty.node {
|
|
|
|
ast::sty_value => impl_owned_self(self_ty),
|
|
|
|
_ => impl_self(self_ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-11-26 20:08:01 -06:00
|
|
|
trans_fn(ccx,
|
|
|
|
path,
|
2013-01-31 19:12:29 -06:00
|
|
|
&mth.decl,
|
|
|
|
&mth.body,
|
2012-11-26 20:08:01 -06:00
|
|
|
llfn,
|
|
|
|
self_kind,
|
|
|
|
None,
|
|
|
|
mth.id,
|
2013-03-29 18:55:04 -05:00
|
|
|
Some(impl_did),
|
|
|
|
[]);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
local_def(mth.id)
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
csearch::found(ast::ii_dtor(ref dtor, _, _, _)) => {
|
|
|
|
ccx.external.insert(fn_id, Some((*dtor).node.id));
|
|
|
|
local_def((*dtor).node.id)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
|