]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/inline.rs
move job of creating local-def-ids to ast-map (with a few stragglers)
[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(|a,b,c,d| astencode::decode_inlined_item(a, b, c, d)));
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.id; }
114                         ccx.external().borrow_mut().insert(ty_v.did, Some(ast_v.node.id));
115                     }
116                 }
117                 hir::ItemStruct(ref struct_def, _) => {
118                     match struct_def.ctor_id {
119                         None => ccx.sess().bug("instantiate_inline: called on a \
120                                                 non-tuple struct"),
121                         Some(ctor_id) => {
122                             ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
123                             my_id = ctor_id;
124                         }
125                     }
126                 }
127                 _ => ccx.sess().bug("instantiate_inline: item has a \
128                                  non-enum, non-struct parent")
129             }
130             trans_item(ccx, &**item);
131             my_id
132         }
133         csearch::FoundAst::FoundParent(_, _) => {
134             ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
135                             with a non-item parent");
136         }
137         csearch::FoundAst::Found(&InlinedItem::TraitItem(_, ref trait_item)) => {
138             ccx.external().borrow_mut().insert(fn_id, Some(trait_item.id));
139             ccx.external_srcs().borrow_mut().insert(trait_item.id, fn_id);
140
141             ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
142
143             // Associated consts already have to be evaluated in `typeck`, so
144             // the logic to do that already exists in `middle`. In order to
145             // reuse that code, it needs to be able to look up the traits for
146             // inlined items.
147             let ty_trait_item = ccx.tcx().impl_or_trait_item(fn_id).clone();
148             let trait_item_def_id = ccx.tcx().map.local_def_id(trait_item.id);
149             ccx.tcx().impl_or_trait_items.borrow_mut()
150                      .insert(trait_item_def_id, ty_trait_item);
151
152             // If this is a default method, we can't look up the
153             // impl type. But we aren't going to translate anyways, so
154             // don't.
155             trait_item.id
156         }
157         csearch::FoundAst::Found(&InlinedItem::ImplItem(impl_did, ref impl_item)) => {
158             ccx.external().borrow_mut().insert(fn_id, Some(impl_item.id));
159             ccx.external_srcs().borrow_mut().insert(impl_item.id, fn_id);
160
161             ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
162
163             // Translate monomorphic impl methods immediately.
164             if let hir::MethodImplItem(ref sig, ref body) = impl_item.node {
165                 let impl_tpt = ccx.tcx().lookup_item_type(impl_did);
166                 if impl_tpt.generics.types.is_empty() &&
167                         sig.generics.ty_params.is_empty() {
168                     let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
169                     let llfn = get_item_val(ccx, impl_item.id);
170                     trans_fn(ccx,
171                              &sig.decl,
172                              body,
173                              llfn,
174                              empty_substs,
175                              impl_item.id,
176                              &[]);
177                     // See linkage comments on items.
178                     if ccx.sess().opts.cg.codegen_units == 1 {
179                         SetLinkage(llfn, InternalLinkage);
180                     } else {
181                         SetLinkage(llfn, AvailableExternallyLinkage);
182                     }
183                 }
184             }
185
186             impl_item.id
187         }
188     };
189
190     let inline_def_id = ccx.tcx().map.local_def_id(inline_id);
191     Some(inline_def_id)
192 }
193
194 pub fn get_local_instance(ccx: &CrateContext, fn_id: DefId)
195     -> Option<DefId> {
196     if fn_id.is_local() {
197         Some(fn_id)
198     } else {
199         instantiate_inline(ccx, fn_id)
200     }
201 }
202
203 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: DefId) -> DefId {
204     get_local_instance(ccx, fn_id).unwrap_or(fn_id)
205 }