]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/inline.rs
auto merge of #17011 : nodakai/rust/rustdoc-stronger-crate, r=alexcrichton
[rust.git] / src / librustc / middle / trans / inline.rs
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.
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 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
23     -> 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 local_def(node_id);
31         }
32         Some(&None) => {
33             return fn_id; // 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     return match csearch_result {
45         csearch::not_found => {
46             ccx.external().borrow_mut().insert(fn_id, None);
47             fn_id
48         }
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);
52
53             ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
54             trans_item(ccx, &*item);
55
56             let linkage = match item.node {
57                 ast::ItemFn(_, _, _, ref generics, _) => {
58                     if generics.is_type_parameterized() {
59                         // Generics have no symbol, so they can't be given any
60                         // linkage.
61                         None
62                     } else {
63                         if ccx.sess().opts.cg.codegen_units == 1 {
64                             // We could use AvailableExternallyLinkage here,
65                             // but InternalLinkage allows LLVM to optimize more
66                             // aggressively (at the cost of sometimes
67                             // duplicating code).
68                             Some(InternalLinkage)
69                         } else {
70                             // With multiple compilation units, duplicated code
71                             // is more of a problem.  Also, `codegen_units > 1`
72                             // means the user is okay with losing some
73                             // performance.
74                             Some(AvailableExternallyLinkage)
75                         }
76                     }
77                 }
78                 ast::ItemStatic(_, mutbl, _) => {
79                     if !ast_util::static_has_significant_address(mutbl, item.attrs.as_slice()) {
80                         // Inlined static items use internal linkage when
81                         // possible, so that LLVM will coalesce globals with
82                         // identical initializers.  (It only does this for
83                         // globals with unnamed_addr and either internal or
84                         // private linkage.)
85                         Some(InternalLinkage)
86                     } else {
87                         // The address is significant, so we can't create an
88                         // internal copy of the static.  (The copy would have a
89                         // different address from the original.)
90                         Some(AvailableExternallyLinkage)
91                     }
92                 }
93                 _ => unreachable!(),
94             };
95
96             match linkage {
97                 Some(linkage) => {
98                     let g = get_item_val(ccx, item.id);
99                     SetLinkage(g, linkage);
100                 }
101                 None => {}
102             }
103
104             local_def(item.id)
105         }
106         csearch::found(ast::IIForeign(item)) => {
107             ccx.external().borrow_mut().insert(fn_id, Some(item.id));
108             ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
109             local_def(item.id)
110         }
111         csearch::found_parent(parent_id, ast::IIItem(item)) => {
112             ccx.external().borrow_mut().insert(parent_id, Some(item.id));
113             ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
114
115           let mut my_id = 0;
116           match item.node {
117             ast::ItemEnum(_, _) => {
118               let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
119               let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
120               for (here, there) in vs_here.iter().zip(vs_there.iter()) {
121                   if there.id == fn_id { my_id = here.id.node; }
122                   ccx.external().borrow_mut().insert(there.id, Some(here.id.node));
123               }
124             }
125             ast::ItemStruct(ref struct_def, _) => {
126               match struct_def.ctor_id {
127                 None => {}
128                 Some(ctor_id) => {
129                     ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
130                     my_id = ctor_id;
131                 }
132               }
133             }
134             _ => ccx.sess().bug("maybe_instantiate_inline: item has a \
135                                  non-enum, non-struct parent")
136           }
137           trans_item(ccx, &*item);
138           local_def(my_id)
139         }
140         csearch::found_parent(_, _) => {
141             ccx.sess().bug("maybe_get_item_ast returned a found_parent \
142              with a non-item parent");
143         }
144         csearch::found(ast::IITraitItem(impl_did, impl_item)) => {
145             match impl_item {
146                 ast::ProvidedInlinedTraitItem(mth) |
147                 ast::RequiredInlinedTraitItem(mth) => {
148                     ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
149                     ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
150
151                     ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
152                 }
153             }
154
155             match impl_item {
156                 ast::ProvidedInlinedTraitItem(mth) => {
157                     // If this is a default method, we can't look up the
158                     // impl type. But we aren't going to translate anyways, so
159                     // don't.
160                     local_def(mth.id)
161                 }
162                 ast::RequiredInlinedTraitItem(mth) => {
163                     let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
164                     let unparameterized = impl_tpt.generics.types.is_empty() &&
165                             mth.pe_generics().ty_params.is_empty();
166
167                     if unparameterized {
168                         let llfn = get_item_val(ccx, mth.id);
169                         trans_fn(ccx,
170                                  &*mth.pe_fn_decl(),
171                                  &*mth.pe_body(),
172                                  llfn,
173                                  &param_substs::empty(),
174                                  mth.id,
175                                  []);
176                         // Use InternalLinkage so LLVM can optimize more
177                         // aggressively.
178                         SetLinkage(llfn, InternalLinkage);
179                     }
180                     local_def(mth.id)
181                 }
182             }
183         }
184     };
185 }