]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/inline.rs
3f44bc40f356b7f51362525272d4123adade4c39
[rustc.git] / src / librustc_trans / trans / inline.rs
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.
4 //
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.
10
11 use llvm::{AvailableExternallyLinkage, InternalLinkage, SetLinkage};
12 use metadata::csearch;
13 use middle::astencode;
14 use middle::subst::Substs;
15 use trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
16 use trans::common::*;
17 use middle::ty;
18
19 use syntax::ast;
20 use syntax::ast_util::local_def;
21
22 fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
23 -> Option<ast::DefId> {
24 let _icx = push_ctxt("maybe_instantiate_inline");
25 match ccx.external().borrow().get(&fn_id) {
26 Some(&Some(node_id)) => {
27 // Already inline
28 debug!("maybe_instantiate_inline({}): already inline as node id {}",
29 ty::item_path_str(ccx.tcx(), fn_id), node_id);
30 return Some(local_def(node_id));
31 }
32 Some(&None) => {
33 return None; // Not inlinable
34 }
35 None => {
36 // Not seen yet
37 }
38 }
39
40 let csearch_result =
41 csearch::maybe_get_item_ast(
42 ccx.tcx(), fn_id,
43 Box::new(|a,b,c,d| astencode::decode_inlined_item(a, b, c, d)));
44
45 let inline_id = match csearch_result {
46 csearch::FoundAst::NotFound => {
47 ccx.external().borrow_mut().insert(fn_id, None);
48 return None;
49 }
50 csearch::FoundAst::Found(&ast::IIItem(ref item)) => {
51 ccx.external().borrow_mut().insert(fn_id, Some(item.id));
52 ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
53
54 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
55 trans_item(ccx, &**item);
56
57 let linkage = match item.node {
58 ast::ItemFn(_, _, _, ref generics, _) => {
59 if generics.is_type_parameterized() {
60 // Generics have no symbol, so they can't be given any
61 // linkage.
62 None
63 } else {
64 if ccx.sess().opts.cg.codegen_units == 1 {
65 // We could use AvailableExternallyLinkage here,
66 // but InternalLinkage allows LLVM to optimize more
67 // aggressively (at the cost of sometimes
68 // duplicating code).
69 Some(InternalLinkage)
70 } else {
71 // With multiple compilation units, duplicated code
72 // is more of a problem. Also, `codegen_units > 1`
73 // means the user is okay with losing some
74 // performance.
75 Some(AvailableExternallyLinkage)
76 }
77 }
78 }
79 ast::ItemConst(..) => None,
80 _ => unreachable!(),
81 };
82
83 match linkage {
84 Some(linkage) => {
85 let g = get_item_val(ccx, item.id);
86 SetLinkage(g, linkage);
87 }
88 None => {}
89 }
90
91 item.id
92 }
93 csearch::FoundAst::Found(&ast::IIForeign(ref item)) => {
94 ccx.external().borrow_mut().insert(fn_id, Some(item.id));
95 ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
96 item.id
97 }
98 csearch::FoundAst::FoundParent(parent_id, &ast::IIItem(ref item)) => {
99 ccx.external().borrow_mut().insert(parent_id, Some(item.id));
100 ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
101
102 let mut my_id = 0;
103 match item.node {
104 ast::ItemEnum(_, _) => {
105 let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
106 let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
107 for (here, there) in vs_here.iter().zip(vs_there.iter()) {
108 if there.id == fn_id { my_id = here.id.node; }
109 ccx.external().borrow_mut().insert(there.id, Some(here.id.node));
110 }
111 }
112 ast::ItemStruct(ref struct_def, _) => {
113 match struct_def.ctor_id {
114 None => {}
115 Some(ctor_id) => {
116 ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
117 my_id = ctor_id;
118 }
119 }
120 }
121 _ => ccx.sess().bug("maybe_instantiate_inline: item has a \
122 non-enum, non-struct parent")
123 }
124 trans_item(ccx, &**item);
125 my_id
126 }
127 csearch::FoundAst::FoundParent(_, _) => {
128 ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
129 with a non-item parent");
130 }
131 csearch::FoundAst::Found(&ast::IITraitItem(_, ref trait_item)) => {
132 ccx.external().borrow_mut().insert(fn_id, Some(trait_item.id));
133 ccx.external_srcs().borrow_mut().insert(trait_item.id, fn_id);
134
135 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
136
137 // Associated consts already have to be evaluated in `typeck`, so
138 // the logic to do that already exists in `middle`. In order to
139 // reuse that code, it needs to be able to look up the traits for
140 // inlined items.
141 let ty_trait_item = ty::impl_or_trait_item(ccx.tcx(), fn_id).clone();
142 ccx.tcx().impl_or_trait_items.borrow_mut()
143 .insert(local_def(trait_item.id), ty_trait_item);
144
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
147 // don't.
148 trait_item.id
149 }
150 csearch::FoundAst::Found(&ast::IIImplItem(impl_did, ref impl_item)) => {
151 ccx.external().borrow_mut().insert(fn_id, Some(impl_item.id));
152 ccx.external_srcs().borrow_mut().insert(impl_item.id, fn_id);
153
154 ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
155
156 // Translate monomorphic impl methods immediately.
157 if let ast::MethodImplItem(ref sig, ref body) = impl_item.node {
158 let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
159 if impl_tpt.generics.types.is_empty() &&
160 sig.generics.ty_params.is_empty() {
161 let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
162 let llfn = get_item_val(ccx, impl_item.id);
163 trans_fn(ccx,
164 &sig.decl,
165 body,
166 llfn,
167 empty_substs,
168 impl_item.id,
169 &[]);
170 // Use InternalLinkage so LLVM can optimize more aggressively.
171 SetLinkage(llfn, InternalLinkage);
172 }
173 }
174
175 impl_item.id
176 }
177 };
178
179 Some(local_def(inline_id))
180 }
181
182 pub fn get_local_instance(ccx: &CrateContext, fn_id: ast::DefId)
183 -> Option<ast::DefId> {
184 if fn_id.krate == ast::LOCAL_CRATE {
185 Some(fn_id)
186 } else {
187 instantiate_inline(ccx, fn_id)
188 }
189 }
190
191 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId) -> ast::DefId {
192 get_local_instance(ccx, fn_id).unwrap_or(fn_id)
193 }