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