1 // Copyright 2012 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 use lib::llvm::{AvailableExternallyLinkage, SetLinkage};
12 use metadata::csearch;
13 use middle::astencode;
14 use middle::trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
15 use middle::trans::common::*;
19 use syntax::ast_util::local_def;
22 pub fn maybe_instantiate_inline(ccx: @CrateContext, fn_id: ast::DefId)
24 let _icx = push_ctxt("maybe_instantiate_inline");
26 let external = ccx.external.borrow();
27 match external.get().find(&fn_id) {
28 Some(&Some(node_id)) => {
30 debug!("maybe_instantiate_inline({}): already inline as node id {}",
31 ty::item_path_str(ccx.tcx, fn_id), node_id);
32 return local_def(node_id);
35 return fn_id; // Not inlinable
44 csearch::maybe_get_item_ast(
46 |a,b,c,d| astencode::decode_inlined_item(a, b, ccx.maps, c, d));
47 return match csearch_result {
48 csearch::not_found => {
49 let mut external = ccx.external.borrow_mut();
50 external.get().insert(fn_id, None);
53 csearch::found(ast::IIItem(item)) => {
55 let mut external = ccx.external.borrow_mut();
56 let mut external_srcs = ccx.external_srcs.borrow_mut();
57 external.get().insert(fn_id, Some(item.id));
58 external_srcs.get().insert(item.id, fn_id);
61 ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
62 trans_item(ccx, item);
64 // We're bringing an external global into this crate, but we don't
65 // want to create two copies of the global. If we do this, then if
66 // you take the address of the global in two separate crates you get
67 // two different addresses. This is bad for things like conditions,
68 // but it could possibly have other adverse side effects. We still
69 // want to achieve the optimizations related to this global,
70 // however, so we use the available_externally linkage which llvm
73 ast::ItemStatic(..) => {
74 let g = get_item_val(ccx, item.id);
75 // see the comment in get_item_val() as to why this check is
77 if !attr::contains_name(item.attrs,
78 "address_insignificant") {
79 SetLinkage(g, AvailableExternallyLinkage);
87 csearch::found(ast::IIForeign(item)) => {
89 let mut external = ccx.external.borrow_mut();
90 let mut external_srcs = ccx.external_srcs.borrow_mut();
91 external.get().insert(fn_id, Some(item.id));
92 external_srcs.get().insert(item.id, fn_id);
96 csearch::found_parent(parent_id, ast::IIItem(item)) => {
98 let mut external = ccx.external.borrow_mut();
99 let mut external_srcs = ccx.external_srcs.borrow_mut();
100 external.get().insert(parent_id, Some(item.id));
101 external_srcs.get().insert(item.id, parent_id);
106 ast::ItemEnum(_, _) => {
107 let vs_here = ty::enum_variants(ccx.tcx, local_def(item.id));
108 let vs_there = ty::enum_variants(ccx.tcx, parent_id);
109 for (here, there) in vs_here.iter().zip(vs_there.iter()) {
110 if there.id == fn_id { my_id = here.id.node; }
111 let mut external = ccx.external.borrow_mut();
112 external.get().insert(there.id, Some(here.id.node));
115 ast::ItemStruct(ref struct_def, _) => {
116 match struct_def.ctor_id {
119 let mut external = ccx.external.borrow_mut();
120 let _ = external.get().insert(fn_id, Some(ctor_id));
125 _ => ccx.sess.bug("maybe_instantiate_inline: item has a \
126 non-enum, non-struct parent")
128 trans_item(ccx, item);
131 csearch::found_parent(_, _) => {
132 ccx.sess.bug("maybe_get_item_ast returned a found_parent \
133 with a non-item parent");
135 csearch::found(ast::IIMethod(impl_did, is_provided, mth)) => {
137 let mut external = ccx.external.borrow_mut();
138 let mut external_srcs = ccx.external_srcs.borrow_mut();
139 external.get().insert(fn_id, Some(mth.id));
140 external_srcs.get().insert(mth.id, fn_id);
143 ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
145 // If this is a default method, we can't look up the
146 // impl type. But we aren't going to translate anyways, so don't.
147 if is_provided { return local_def(mth.id); }
149 let impl_tpt = ty::lookup_item_type(ccx.tcx, impl_did);
150 let num_type_params =
151 impl_tpt.generics.type_param_defs().len() +
152 mth.generics.ty_params.len();
154 if num_type_params == 0 {
155 let llfn = get_item_val(ccx, mth.id);
156 trans_fn(ccx, mth.decl, mth.body, llfn, None, mth.id, []);