]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/inline.rs
auto merge of #17223 : retep998/rust/into_string, r=huonw
[rust.git] / src / librustc / middle / 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::trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
15 use middle::trans::common::*;
16 use middle::ty;
17
18 use syntax::ast;
19 use syntax::ast_util::{local_def, PostExpansionMethod};
20 use syntax::ast_util;
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().find(&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             |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
44
45     let inline_def = match csearch_result {
46         csearch::not_found => {
47             ccx.external().borrow_mut().insert(fn_id, None);
48             return None;
49         }
50         csearch::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::ItemStatic(_, mutbl, _) => {
80                     if !ast_util::static_has_significant_address(mutbl, item.attrs.as_slice()) {
81                         // Inlined static items use internal linkage when
82                         // possible, so that LLVM will coalesce globals with
83                         // identical initializers.  (It only does this for
84                         // globals with unnamed_addr and either internal or
85                         // private linkage.)
86                         Some(InternalLinkage)
87                     } else {
88                         // The address is significant, so we can't create an
89                         // internal copy of the static.  (The copy would have a
90                         // different address from the original.)
91                         Some(AvailableExternallyLinkage)
92                     }
93                 }
94                 _ => unreachable!(),
95             };
96
97             match linkage {
98                 Some(linkage) => {
99                     let g = get_item_val(ccx, item.id);
100                     SetLinkage(g, linkage);
101                 }
102                 None => {}
103             }
104
105             local_def(item.id)
106         }
107         csearch::found(&ast::IIForeign(ref item)) => {
108             ccx.external().borrow_mut().insert(fn_id, Some(item.id));
109             ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
110             local_def(item.id)
111         }
112         csearch::found_parent(parent_id, &ast::IIItem(ref item)) => {
113             ccx.external().borrow_mut().insert(parent_id, Some(item.id));
114             ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
115
116           let mut my_id = 0;
117           match item.node {
118             ast::ItemEnum(_, _) => {
119               let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
120               let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
121               for (here, there) in vs_here.iter().zip(vs_there.iter()) {
122                   if there.id == fn_id { my_id = here.id.node; }
123                   ccx.external().borrow_mut().insert(there.id, Some(here.id.node));
124               }
125             }
126             ast::ItemStruct(ref struct_def, _) => {
127               match struct_def.ctor_id {
128                 None => {}
129                 Some(ctor_id) => {
130                     ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
131                     my_id = ctor_id;
132                 }
133               }
134             }
135             _ => ccx.sess().bug("maybe_instantiate_inline: item has a \
136                                  non-enum, non-struct parent")
137           }
138           trans_item(ccx, &**item);
139           local_def(my_id)
140         }
141         csearch::found_parent(_, _) => {
142             ccx.sess().bug("maybe_get_item_ast returned a found_parent \
143              with a non-item parent");
144         }
145         csearch::found(&ast::IITraitItem(_, ref trait_item)) => {
146             match *trait_item {
147                 ast::RequiredMethod(_) => ccx.sess().bug("found RequiredMethod IITraitItem"),
148                 ast::ProvidedMethod(ref mth) => {
149                     ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
150                     ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
151
152                     ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
153
154                     // If this is a default method, we can't look up the
155                     // impl type. But we aren't going to translate anyways, so
156                     // don't.
157                     local_def(mth.id)
158                 }
159             }
160         }
161         csearch::found(&ast::IIImplItem(impl_did, ref impl_item)) => {
162             match *impl_item {
163                 ast::MethodImplItem(ref mth) => {
164                     ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
165                     ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
166
167                     ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
168
169                     let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
170                     let unparameterized = impl_tpt.generics.types.is_empty() &&
171                             mth.pe_generics().ty_params.is_empty();
172
173                     if unparameterized {
174                         let llfn = get_item_val(ccx, mth.id);
175                         trans_fn(ccx,
176                                  &*mth.pe_fn_decl(),
177                                  &*mth.pe_body(),
178                                  llfn,
179                                  &param_substs::empty(),
180                                  mth.id,
181                                  []);
182                         // Use InternalLinkage so LLVM can optimize more
183                         // aggressively.
184                         SetLinkage(llfn, InternalLinkage);
185                     }
186                     local_def(mth.id)
187                 }
188             }
189         }
190     };
191
192     return Some(inline_def);
193 }
194
195 pub fn get_local_instance(ccx: &CrateContext, fn_id: ast::DefId)
196     -> Option<ast::DefId> {
197     if fn_id.krate == ast::LOCAL_CRATE {
198         Some(fn_id)
199     } else {
200         instantiate_inline(ccx, fn_id)
201     }
202 }
203
204 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId) -> ast::DefId {
205     get_local_instance(ccx, fn_id).unwrap_or(fn_id)
206 }