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 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, PostExpansionMethod};
22 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
24 let _icx = push_ctxt("maybe_instantiate_inline");
25 match ccx.external.borrow().find(&fn_id) {
26 Some(&Some(node_id)) => {
28 debug!("maybe_instantiate_inline({}): already inline as node id {}",
29 ty::item_path_str(ccx.tcx(), fn_id), node_id);
30 return local_def(node_id);
33 return fn_id; // Not inlinable
41 csearch::maybe_get_item_ast(
43 |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
44 return match csearch_result {
45 csearch::not_found => {
46 ccx.external.borrow_mut().insert(fn_id, None);
49 csearch::found(ast::IIItem(item)) => {
50 ccx.external.borrow_mut().insert(fn_id, Some(item.id));
51 ccx.external_srcs.borrow_mut().insert(item.id, fn_id);
53 ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
54 trans_item(ccx, &*item);
56 // We're bringing an external global into this crate, but we don't
57 // want to create two copies of the global. If we do this, then if
58 // you take the address of the global in two separate crates you get
59 // two different addresses. This is bad for things like conditions,
60 // but it could possibly have other adverse side effects. We still
61 // want to achieve the optimizations related to this global,
62 // however, so we use the available_externally linkage which llvm
65 ast::ItemStatic(_, mutbl, _) => {
66 let g = get_item_val(ccx, item.id);
67 // see the comment in get_item_val() as to why this check is
69 if ast_util::static_has_significant_address(
71 item.attrs.as_slice()) {
72 SetLinkage(g, AvailableExternallyLinkage);
80 csearch::found(ast::IIForeign(item)) => {
81 ccx.external.borrow_mut().insert(fn_id, Some(item.id));
82 ccx.external_srcs.borrow_mut().insert(item.id, fn_id);
85 csearch::found_parent(parent_id, ast::IIItem(item)) => {
86 ccx.external.borrow_mut().insert(parent_id, Some(item.id));
87 ccx.external_srcs.borrow_mut().insert(item.id, parent_id);
91 ast::ItemEnum(_, _) => {
92 let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
93 let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
94 for (here, there) in vs_here.iter().zip(vs_there.iter()) {
95 if there.id == fn_id { my_id = here.id.node; }
96 ccx.external.borrow_mut().insert(there.id, Some(here.id.node));
99 ast::ItemStruct(ref struct_def, _) => {
100 match struct_def.ctor_id {
103 ccx.external.borrow_mut().insert(fn_id, Some(ctor_id));
108 _ => ccx.sess().bug("maybe_instantiate_inline: item has a \
109 non-enum, non-struct parent")
111 trans_item(ccx, &*item);
114 csearch::found_parent(_, _) => {
115 ccx.sess().bug("maybe_get_item_ast returned a found_parent \
116 with a non-item parent");
118 csearch::found(ast::IIMethod(impl_did, is_provided, mth)) => {
119 ccx.external.borrow_mut().insert(fn_id, Some(mth.id));
120 ccx.external_srcs.borrow_mut().insert(mth.id, fn_id);
122 ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
124 // If this is a default method, we can't look up the
125 // impl type. But we aren't going to translate anyways, so don't.
126 if is_provided { return local_def(mth.id); }
128 let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
129 let unparameterized =
130 impl_tpt.generics.types.is_empty() &&
131 mth.pe_generics().ty_params.is_empty();
134 let llfn = get_item_val(ccx, mth.id);
135 trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), llfn,
136 ¶m_substs::empty(), mth.id, [], TranslateItems);