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