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