1 // Copyright 2014 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, InternalLinkage, SetLinkage};
12 use metadata::csearch;
13 use metadata::inline::InlinedItem;
14 use middle::astencode;
15 use middle::def_id::DefId;
16 use middle::subst::Substs;
17 use trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
23 fn instantiate_inline(ccx: &CrateContext, fn_id: DefId)
25 debug!("instantiate_inline({:?})", fn_id);
26 let _icx = push_ctxt("instantiate_inline");
28 match ccx.external().borrow().get(&fn_id) {
29 Some(&Some(node_id)) => {
31 debug!("instantiate_inline({}): already inline as node id {}",
32 ccx.tcx().item_path_str(fn_id), node_id);
33 let node_def_id = ccx.tcx().map.local_def_id(node_id);
34 return Some(node_def_id);
37 return None; // Not inlinable
45 csearch::maybe_get_item_ast(
47 Box::new(|a,b,c,d| astencode::decode_inlined_item(a, b, c, d)));
49 let inline_id = match csearch_result {
50 csearch::FoundAst::NotFound => {
51 ccx.external().borrow_mut().insert(fn_id, None);
54 csearch::FoundAst::Found(&InlinedItem::Item(ref item)) => {
55 ccx.external().borrow_mut().insert(fn_id, Some(item.id));
56 ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
58 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
59 trans_item(ccx, item);
61 let linkage = match item.node {
62 hir::ItemFn(_, _, _, _, ref generics, _) => {
63 if generics.is_type_parameterized() {
64 // Generics have no symbol, so they can't be given any
68 if ccx.sess().opts.cg.codegen_units == 1 {
69 // We could use AvailableExternallyLinkage here,
70 // but InternalLinkage allows LLVM to optimize more
71 // aggressively (at the cost of sometimes
75 // With multiple compilation units, duplicated code
76 // is more of a problem. Also, `codegen_units > 1`
77 // means the user is okay with losing some
79 Some(AvailableExternallyLinkage)
83 hir::ItemConst(..) => None,
89 let g = get_item_val(ccx, item.id);
90 SetLinkage(g, linkage);
97 csearch::FoundAst::Found(&InlinedItem::Foreign(ref item)) => {
98 ccx.external().borrow_mut().insert(fn_id, Some(item.id));
99 ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
102 csearch::FoundAst::FoundParent(parent_id, &InlinedItem::Item(ref item)) => {
103 ccx.external().borrow_mut().insert(parent_id, Some(item.id));
104 ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
108 hir::ItemEnum(ref ast_def, _) => {
109 let ast_vs = &ast_def.variants;
110 let ty_vs = &ccx.tcx().lookup_adt_def(parent_id).variants;
111 assert_eq!(ast_vs.len(), ty_vs.len());
112 for (ast_v, ty_v) in ast_vs.iter().zip(ty_vs.iter()) {
113 if ty_v.did == fn_id { my_id = ast_v.node.id; }
114 ccx.external().borrow_mut().insert(ty_v.did, Some(ast_v.node.id));
117 hir::ItemStruct(ref struct_def, _) => {
118 match struct_def.ctor_id {
119 None => ccx.sess().bug("instantiate_inline: called on a \
122 ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
127 _ => ccx.sess().bug("instantiate_inline: item has a \
128 non-enum, non-struct parent")
130 trans_item(ccx, &**item);
133 csearch::FoundAst::FoundParent(_, _) => {
134 ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
135 with a non-item parent");
137 csearch::FoundAst::Found(&InlinedItem::TraitItem(_, ref trait_item)) => {
138 ccx.external().borrow_mut().insert(fn_id, Some(trait_item.id));
139 ccx.external_srcs().borrow_mut().insert(trait_item.id, fn_id);
141 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
143 // Associated consts already have to be evaluated in `typeck`, so
144 // the logic to do that already exists in `middle`. In order to
145 // reuse that code, it needs to be able to look up the traits for
147 let ty_trait_item = ccx.tcx().impl_or_trait_item(fn_id).clone();
148 let trait_item_def_id = ccx.tcx().map.local_def_id(trait_item.id);
149 ccx.tcx().impl_or_trait_items.borrow_mut()
150 .insert(trait_item_def_id, ty_trait_item);
152 // If this is a default method, we can't look up the
153 // impl type. But we aren't going to translate anyways, so
157 csearch::FoundAst::Found(&InlinedItem::ImplItem(impl_did, ref impl_item)) => {
158 ccx.external().borrow_mut().insert(fn_id, Some(impl_item.id));
159 ccx.external_srcs().borrow_mut().insert(impl_item.id, fn_id);
161 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
163 // Translate monomorphic impl methods immediately.
164 if let hir::MethodImplItem(ref sig, ref body) = impl_item.node {
165 let impl_tpt = ccx.tcx().lookup_item_type(impl_did);
166 if impl_tpt.generics.types.is_empty() &&
167 sig.generics.ty_params.is_empty() {
168 let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
169 let llfn = get_item_val(ccx, impl_item.id);
177 // See linkage comments on items.
178 if ccx.sess().opts.cg.codegen_units == 1 {
179 SetLinkage(llfn, InternalLinkage);
181 SetLinkage(llfn, AvailableExternallyLinkage);
190 let inline_def_id = ccx.tcx().map.local_def_id(inline_id);
194 pub fn get_local_instance(ccx: &CrateContext, fn_id: DefId)
196 if let Some(_) = ccx.tcx().map.as_local_node_id(fn_id) {
199 instantiate_inline(ccx, fn_id)
203 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: DefId) -> DefId {
204 get_local_instance(ccx, fn_id).unwrap_or(fn_id)