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