]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/inline.rs
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
[rust.git] / src / librustc / middle / trans / inline.rs
1 // Copyright 2012 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, SetLinkage};
12 use metadata::csearch;
13 use middle::astencode;
14 use middle::trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
15 use middle::trans::common::*;
16 use middle::ty;
17
18 use syntax::ast;
19 use syntax::ast_util::{local_def, PostExpansionMethod};
20 use syntax::ast_util;
21
22 pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
23     -> ast::DefId {
24     let _icx = push_ctxt("maybe_instantiate_inline");
25     match ccx.external.borrow().find(&fn_id) {
26         Some(&Some(node_id)) => {
27             // Already inline
28             debug!("maybe_instantiate_inline({}): already inline as node id {}",
29                    ty::item_path_str(ccx.tcx(), fn_id), node_id);
30             return local_def(node_id);
31         }
32         Some(&None) => {
33             return fn_id; // Not inlinable
34         }
35         None => {
36             // Not seen yet
37         }
38     }
39
40     let csearch_result =
41         csearch::maybe_get_item_ast(
42             ccx.tcx(), fn_id,
43             |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
44     return match csearch_result {
45         csearch::not_found => {
46             ccx.external.borrow_mut().insert(fn_id, None);
47             fn_id
48         }
49         csearch::found(ast::IIItem(item)) => {
50             ccx.external.borrow_mut().insert(fn_id, Some(item.id));
51             ccx.external_srcs.borrow_mut().insert(item.id, fn_id);
52
53             ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
54             trans_item(ccx, &*item);
55
56             // We're bringing an external global into this crate, but we don't
57             // want to create two copies of the global. If we do this, then if
58             // you take the address of the global in two separate crates you get
59             // two different addresses. This is bad for things like conditions,
60             // but it could possibly have other adverse side effects. We still
61             // want to achieve the optimizations related to this global,
62             // however, so we use the available_externally linkage which llvm
63             // provides
64             match item.node {
65                 ast::ItemStatic(_, mutbl, _) => {
66                     let g = get_item_val(ccx, item.id);
67                     // see the comment in get_item_val() as to why this check is
68                     // performed here.
69                     if ast_util::static_has_significant_address(
70                             mutbl,
71                             item.attrs.as_slice()) {
72                         SetLinkage(g, AvailableExternallyLinkage);
73                     }
74                 }
75                 _ => {}
76             }
77
78             local_def(item.id)
79         }
80         csearch::found(ast::IIForeign(item)) => {
81             ccx.external.borrow_mut().insert(fn_id, Some(item.id));
82             ccx.external_srcs.borrow_mut().insert(item.id, fn_id);
83             local_def(item.id)
84         }
85         csearch::found_parent(parent_id, ast::IIItem(item)) => {
86             ccx.external.borrow_mut().insert(parent_id, Some(item.id));
87             ccx.external_srcs.borrow_mut().insert(item.id, parent_id);
88
89           let mut my_id = 0;
90           match item.node {
91             ast::ItemEnum(_, _) => {
92               let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
93               let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
94               for (here, there) in vs_here.iter().zip(vs_there.iter()) {
95                   if there.id == fn_id { my_id = here.id.node; }
96                   ccx.external.borrow_mut().insert(there.id, Some(here.id.node));
97               }
98             }
99             ast::ItemStruct(ref struct_def, _) => {
100               match struct_def.ctor_id {
101                 None => {}
102                 Some(ctor_id) => {
103                     ccx.external.borrow_mut().insert(fn_id, Some(ctor_id));
104                     my_id = ctor_id;
105                 }
106               }
107             }
108             _ => ccx.sess().bug("maybe_instantiate_inline: item has a \
109                                  non-enum, non-struct parent")
110           }
111           trans_item(ccx, &*item);
112           local_def(my_id)
113         }
114         csearch::found_parent(_, _) => {
115             ccx.sess().bug("maybe_get_item_ast returned a found_parent \
116              with a non-item parent");
117         }
118         csearch::found(ast::IIMethod(impl_did, is_provided, mth)) => {
119             ccx.external.borrow_mut().insert(fn_id, Some(mth.id));
120             ccx.external_srcs.borrow_mut().insert(mth.id, fn_id);
121
122             ccx.stats.n_inlines.set(ccx.stats.n_inlines.get() + 1);
123
124             // If this is a default method, we can't look up the
125             // impl type. But we aren't going to translate anyways, so don't.
126             if is_provided { return local_def(mth.id); }
127
128             let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
129             let unparameterized =
130                 impl_tpt.generics.types.is_empty() &&
131                 mth.pe_generics().ty_params.is_empty();
132
133           if unparameterized {
134               let llfn = get_item_val(ccx, mth.id);
135                 trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), llfn,
136                        &param_substs::empty(), mth.id, [], TranslateItems);
137           }
138           local_def(mth.id)
139         }
140     };
141 }