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