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(astencode::decode_inlined_item));
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.data.id(); }
114 ccx.external().borrow_mut().insert(ty_v.did, Some(ast_v.node.data.id()));
117 hir::ItemStruct(ref struct_def, _) => {
118 if struct_def.is_struct() {
119 ccx.sess().bug("instantiate_inline: called on a \
122 ccx.external().borrow_mut().insert(fn_id, Some(struct_def.id()));
123 my_id = struct_def.id();
126 _ => ccx.sess().bug("instantiate_inline: item has a \
127 non-enum, non-struct parent")
129 trans_item(ccx, &**item);
132 csearch::FoundAst::FoundParent(_, _) => {
133 ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
134 with a non-item parent");
136 csearch::FoundAst::Found(&InlinedItem::TraitItem(_, ref trait_item)) => {
137 ccx.external().borrow_mut().insert(fn_id, Some(trait_item.id));
138 ccx.external_srcs().borrow_mut().insert(trait_item.id, fn_id);
140 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
142 // Associated consts already have to be evaluated in `typeck`, so
143 // the logic to do that already exists in `middle`. In order to
144 // reuse that code, it needs to be able to look up the traits for
146 let ty_trait_item = ccx.tcx().impl_or_trait_item(fn_id).clone();
147 let trait_item_def_id = ccx.tcx().map.local_def_id(trait_item.id);
148 ccx.tcx().impl_or_trait_items.borrow_mut()
149 .insert(trait_item_def_id, ty_trait_item);
151 // If this is a default method, we can't look up the
152 // impl type. But we aren't going to translate anyways, so
156 csearch::FoundAst::Found(&InlinedItem::ImplItem(impl_did, ref impl_item)) => {
157 ccx.external().borrow_mut().insert(fn_id, Some(impl_item.id));
158 ccx.external_srcs().borrow_mut().insert(impl_item.id, fn_id);
160 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
162 // Translate monomorphic impl methods immediately.
163 if let hir::MethodImplItem(ref sig, ref body) = impl_item.node {
164 let impl_tpt = ccx.tcx().lookup_item_type(impl_did);
165 if impl_tpt.generics.types.is_empty() &&
166 sig.generics.ty_params.is_empty() {
167 let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
168 let llfn = get_item_val(ccx, impl_item.id);
176 // See linkage comments on items.
177 if ccx.sess().opts.cg.codegen_units == 1 {
178 SetLinkage(llfn, InternalLinkage);
180 SetLinkage(llfn, AvailableExternallyLinkage);
189 let inline_def_id = ccx.tcx().map.local_def_id(inline_id);
193 pub fn get_local_instance(ccx: &CrateContext, fn_id: DefId)
195 if let Some(_) = ccx.tcx().map.as_local_node_id(fn_id) {
198 instantiate_inline(ccx, fn_id)
202 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: DefId) -> DefId {
203 get_local_instance(ccx, fn_id).unwrap_or(fn_id)