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-09-11 12:06:16 -05:00
|
|
|
use lib::llvm::{AvailableExternallyLinkage, SetLinkage};
|
2013-02-25 13:11:21 -06:00
|
|
|
use metadata::csearch;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::astencode;
|
2014-01-11 08:39:32 -06:00
|
|
|
use middle::trans::base::{push_ctxt, 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;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast_util::local_def;
|
2013-09-17 13:24:05 -05:00
|
|
|
use syntax::attr;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn maybe_instantiate_inline(ccx: @CrateContext, fn_id: ast::DefId)
|
2013-09-01 20:45:37 -05:00
|
|
|
-> ast::DefId {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("maybe_instantiate_inline");
|
2013-12-18 20:32:52 -06:00
|
|
|
{
|
|
|
|
let external = ccx.external.borrow();
|
|
|
|
match external.get().find(&fn_id) {
|
|
|
|
Some(&Some(node_id)) => {
|
|
|
|
// Already inline
|
|
|
|
debug!("maybe_instantiate_inline({}): already inline as node id {}",
|
|
|
|
ty::item_path_str(ccx.tcx, fn_id), node_id);
|
|
|
|
return local_def(node_id);
|
|
|
|
}
|
|
|
|
Some(&None) => {
|
|
|
|
return fn_id; // Not inlinable
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Not seen yet
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let csearch_result =
|
|
|
|
csearch::maybe_get_item_ast(
|
2012-08-28 17:54:45 -05:00
|
|
|
ccx.tcx, fn_id,
|
2014-02-13 23:07:09 -06:00
|
|
|
|a,b,c,d| astencode::decode_inlined_item(a, b, ccx.maps, c, d));
|
2013-03-15 14:24:24 -05:00
|
|
|
return match csearch_result {
|
|
|
|
csearch::not_found => {
|
2013-12-18 20:32:52 -06:00
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
external.get().insert(fn_id, None);
|
2012-08-28 17:54:45 -05:00
|
|
|
fn_id
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
csearch::found(ast::IIItem(item)) => {
|
2013-12-18 20:32:52 -06:00
|
|
|
{
|
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
let mut external_srcs = ccx.external_srcs.borrow_mut();
|
|
|
|
external.get().insert(fn_id, Some(item.id));
|
|
|
|
external_srcs.get().insert(item.id, fn_id);
|
|
|
|
}
|
|
|
|
|
2013-12-22 15:48:37 -06:00
|
|
|
ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
|
2013-07-11 16:34:10 -05:00
|
|
|
trans_item(ccx, item);
|
2013-09-11 12:06:16 -05:00
|
|
|
|
|
|
|
// We're bringing an external global into this crate, but we don't
|
|
|
|
// want to create two copies of the global. If we do this, then if
|
|
|
|
// you take the address of the global in two separate crates you get
|
|
|
|
// two different addresses. This is bad for things like conditions,
|
|
|
|
// but it could possibly have other adverse side effects. We still
|
|
|
|
// want to achieve the optimizations related to this global,
|
|
|
|
// however, so we use the available_externally linkage which llvm
|
|
|
|
// provides
|
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemStatic(..) => {
|
2013-09-11 12:06:16 -05:00
|
|
|
let g = get_item_val(ccx, item.id);
|
2013-09-17 13:24:05 -05:00
|
|
|
// see the comment in get_item_val() as to why this check is
|
|
|
|
// performed here.
|
|
|
|
if !attr::contains_name(item.attrs,
|
|
|
|
"address_insignificant") {
|
|
|
|
SetLinkage(g, AvailableExternallyLinkage);
|
|
|
|
}
|
2013-09-11 12:06:16 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
local_def(item.id)
|
2013-05-05 14:11:04 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
csearch::found(ast::IIForeign(item)) => {
|
2013-12-18 20:32:52 -06:00
|
|
|
{
|
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
let mut external_srcs = ccx.external_srcs.borrow_mut();
|
|
|
|
external.get().insert(fn_id, Some(item.id));
|
|
|
|
external_srcs.get().insert(item.id, fn_id);
|
|
|
|
}
|
|
|
|
local_def(item.id)
|
2013-05-05 14:11:04 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
csearch::found_parent(parent_id, ast::IIItem(item)) => {
|
2013-12-18 20:32:52 -06:00
|
|
|
{
|
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
let mut external_srcs = ccx.external_srcs.borrow_mut();
|
|
|
|
external.get().insert(parent_id, Some(item.id));
|
|
|
|
external_srcs.get().insert(item.id, parent_id);
|
|
|
|
}
|
|
|
|
|
2013-05-05 14:11:04 -05:00
|
|
|
let mut my_id = 0;
|
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemEnum(_, _) => {
|
2013-05-05 14:11:04 -05:00
|
|
|
let vs_here = ty::enum_variants(ccx.tcx, local_def(item.id));
|
|
|
|
let vs_there = ty::enum_variants(ccx.tcx, parent_id);
|
2013-08-03 11:45:23 -05:00
|
|
|
for (here, there) in vs_here.iter().zip(vs_there.iter()) {
|
2013-05-05 14:11:04 -05:00
|
|
|
if there.id == fn_id { my_id = here.id.node; }
|
2013-12-18 20:32:52 -06:00
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
external.get().insert(there.id, Some(here.id.node));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemStruct(ref struct_def, _) => {
|
2013-09-26 23:53:40 -05:00
|
|
|
match struct_def.ctor_id {
|
|
|
|
None => {}
|
|
|
|
Some(ctor_id) => {
|
2013-12-18 20:32:52 -06:00
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
let _ = external.get().insert(fn_id, Some(ctor_id));
|
2013-09-26 23:53:40 -05:00
|
|
|
my_id = ctor_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-19 00:07:44 -05:00
|
|
|
_ => ccx.sess.bug("maybe_instantiate_inline: item has a \
|
2013-09-26 23:53:40 -05:00
|
|
|
non-enum, non-struct parent")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-07-11 16:34:10 -05:00
|
|
|
trans_item(ccx, item);
|
2013-05-05 14:11:04 -05:00
|
|
|
local_def(my_id)
|
|
|
|
}
|
|
|
|
csearch::found_parent(_, _) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.sess.bug("maybe_get_item_ast returned a found_parent \
|
2013-05-05 14:11:04 -05:00
|
|
|
with a non-item parent");
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
csearch::found(ast::IIMethod(impl_did, is_provided, mth)) => {
|
2013-12-18 20:32:52 -06:00
|
|
|
{
|
|
|
|
let mut external = ccx.external.borrow_mut();
|
|
|
|
let mut external_srcs = ccx.external_srcs.borrow_mut();
|
|
|
|
external.get().insert(fn_id, Some(mth.id));
|
|
|
|
external_srcs.get().insert(mth.id, fn_id);
|
|
|
|
}
|
|
|
|
|
2013-12-22 15:48:37 -06:00
|
|
|
ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
|
|
|
|
|
2013-06-12 16:22:17 -05:00
|
|
|
// If this is a default method, we can't look up the
|
|
|
|
// impl type. But we aren't going to translate anyways, so don't.
|
2013-07-11 16:34:10 -05:00
|
|
|
if is_provided { return local_def(mth.id); }
|
2013-06-12 16:22:17 -05:00
|
|
|
|
|
|
|
let impl_tpt = ty::lookup_item_type(ccx.tcx, impl_did);
|
|
|
|
let num_type_params =
|
2014-01-31 22:57:59 -06:00
|
|
|
impl_tpt.generics.type_param_defs().len() +
|
2013-06-12 16:22:17 -05:00
|
|
|
mth.generics.ty_params.len();
|
|
|
|
|
|
|
|
if num_type_params == 0 {
|
2013-05-05 14:11:04 -05:00
|
|
|
let llfn = get_item_val(ccx, mth.id);
|
2014-02-13 23:07:09 -06:00
|
|
|
trans_fn(ccx, mth.decl, mth.body, llfn, None, mth.id, []);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-05-05 14:11:04 -05:00
|
|
|
local_def(mth.id)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|