2014-09-05 19:39:51 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-21 18:42:34 -05:00
|
|
|
use llvm::{AvailableExternallyLinkage, InternalLinkage, SetLinkage};
|
2013-02-25 13:11:21 -06:00
|
|
|
use metadata::csearch;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::astencode;
|
2014-11-06 01:24:44 -06:00
|
|
|
use middle::subst::Substs;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
|
|
|
|
use trans::common::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2014-07-14 18:31:52 -05:00
|
|
|
use syntax::ast_util::{local_def, PostExpansionMethod};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-09-05 19:39:51 -05:00
|
|
|
fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
|
|
|
|
-> Option<ast::DefId> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("maybe_instantiate_inline");
|
2014-11-06 11:25:16 -06:00
|
|
|
match ccx.external().borrow().get(&fn_id) {
|
2014-03-20 21:49:20 -05:00
|
|
|
Some(&Some(node_id)) => {
|
|
|
|
// Already inline
|
|
|
|
debug!("maybe_instantiate_inline({}): already inline as node id {}",
|
|
|
|
ty::item_path_str(ccx.tcx(), fn_id), node_id);
|
2014-09-05 19:39:51 -05:00
|
|
|
return Some(local_def(node_id));
|
2014-03-20 21:49:20 -05:00
|
|
|
}
|
|
|
|
Some(&None) => {
|
2014-09-05 19:39:51 -05:00
|
|
|
return None; // Not inlinable
|
2014-03-20 21:49:20 -05:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Not seen yet
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let csearch_result =
|
|
|
|
csearch::maybe_get_item_ast(
|
2014-03-15 15:29:34 -05:00
|
|
|
ccx.tcx(), fn_id,
|
2015-01-04 08:07:13 -06:00
|
|
|
box |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
|
2014-09-05 19:39:51 -05:00
|
|
|
|
|
|
|
let inline_def = match csearch_result {
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::NotFound => {
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(fn_id, None);
|
2014-09-05 19:39:51 -05:00
|
|
|
return None;
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::Found(&ast::IIItem(ref item)) => {
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
|
|
|
|
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
|
2013-12-18 20:32:52 -06:00
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
|
2014-09-07 12:09:06 -05:00
|
|
|
trans_item(ccx, &**item);
|
2013-09-11 12:06:16 -05:00
|
|
|
|
2014-07-21 18:42:34 -05:00
|
|
|
let linkage = match item.node {
|
|
|
|
ast::ItemFn(_, _, _, ref generics, _) => {
|
|
|
|
if generics.is_type_parameterized() {
|
|
|
|
// Generics have no symbol, so they can't be given any
|
|
|
|
// linkage.
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
if ccx.sess().opts.cg.codegen_units == 1 {
|
|
|
|
// We could use AvailableExternallyLinkage here,
|
|
|
|
// but InternalLinkage allows LLVM to optimize more
|
|
|
|
// aggressively (at the cost of sometimes
|
|
|
|
// duplicating code).
|
|
|
|
Some(InternalLinkage)
|
|
|
|
} else {
|
|
|
|
// With multiple compilation units, duplicated code
|
|
|
|
// is more of a problem. Also, `codegen_units > 1`
|
|
|
|
// means the user is okay with losing some
|
|
|
|
// performance.
|
|
|
|
Some(AvailableExternallyLinkage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
ast::ItemConst(..) => None,
|
2014-07-21 18:42:34 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
match linkage {
|
|
|
|
Some(linkage) => {
|
|
|
|
let g = get_item_val(ccx, item.id);
|
|
|
|
SetLinkage(g, linkage);
|
|
|
|
}
|
|
|
|
None => {}
|
2013-09-11 12:06:16 -05:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
local_def(item.id)
|
2013-05-05 14:11:04 -05:00
|
|
|
}
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::Found(&ast::IIForeign(ref item)) => {
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
|
|
|
|
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
|
2013-12-18 20:32:52 -06:00
|
|
|
local_def(item.id)
|
2013-05-05 14:11:04 -05:00
|
|
|
}
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::FoundParent(parent_id, &ast::IIItem(ref item)) => {
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(parent_id, Some(item.id));
|
|
|
|
ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
|
2013-12-18 20:32:52 -06:00
|
|
|
|
2013-05-05 14:11:04 -05:00
|
|
|
let mut my_id = 0;
|
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemEnum(_, _) => {
|
2014-03-15 15:29:34 -05:00
|
|
|
let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
|
|
|
|
let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
|
2013-08-03 11:45:23 -05:00
|
|
|
for (here, there) in vs_here.iter().zip(vs_there.iter()) {
|
2013-05-05 14:11:04 -05:00
|
|
|
if there.id == fn_id { my_id = here.id.node; }
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(there.id, Some(here.id.node));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemStruct(ref struct_def, _) => {
|
2013-09-26 23:53:40 -05:00
|
|
|
match struct_def.ctor_id {
|
|
|
|
None => {}
|
|
|
|
Some(ctor_id) => {
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
|
2013-09-26 23:53:40 -05:00
|
|
|
my_id = ctor_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 08:36:01 -06:00
|
|
|
_ => ccx.sess().bug("maybe_instantiate_inline: item has a \
|
|
|
|
non-enum, non-struct parent")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
trans_item(ccx, &**item);
|
2013-05-05 14:11:04 -05:00
|
|
|
local_def(my_id)
|
|
|
|
}
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::FoundParent(_, _) => {
|
|
|
|
ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
|
2013-05-05 14:11:04 -05:00
|
|
|
with a non-item parent");
|
|
|
|
}
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::Found(&ast::IITraitItem(_, ref trait_item)) => {
|
2014-09-07 12:09:06 -05:00
|
|
|
match *trait_item {
|
|
|
|
ast::RequiredMethod(_) => ccx.sess().bug("found RequiredMethod IITraitItem"),
|
|
|
|
ast::ProvidedMethod(ref mth) => {
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
|
|
|
|
ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
|
2013-12-18 20:32:52 -06:00
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
|
2013-06-12 16:22:17 -05:00
|
|
|
|
2014-08-04 15:56:56 -05:00
|
|
|
// If this is a default method, we can't look up the
|
|
|
|
// impl type. But we aren't going to translate anyways, so
|
|
|
|
// don't.
|
|
|
|
local_def(mth.id)
|
|
|
|
}
|
2014-08-05 21:44:21 -05:00
|
|
|
ast::TypeTraitItem(_) => {
|
|
|
|
ccx.sess().bug("found TypeTraitItem IITraitItem")
|
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
}
|
|
|
|
}
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::Found(&ast::IIImplItem(impl_did, ref impl_item)) => {
|
2014-09-07 12:09:06 -05:00
|
|
|
match *impl_item {
|
|
|
|
ast::MethodImplItem(ref mth) => {
|
|
|
|
ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
|
|
|
|
ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
|
|
|
|
|
|
|
|
ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
|
|
|
|
|
2014-08-04 15:56:56 -05:00
|
|
|
let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
|
|
|
|
let unparameterized = impl_tpt.generics.types.is_empty() &&
|
|
|
|
mth.pe_generics().ty_params.is_empty();
|
2013-06-12 16:22:17 -05:00
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
|
2014-08-04 15:56:56 -05:00
|
|
|
if unparameterized {
|
|
|
|
let llfn = get_item_val(ccx, mth.id);
|
|
|
|
trans_fn(ccx,
|
|
|
|
&*mth.pe_fn_decl(),
|
|
|
|
&*mth.pe_body(),
|
|
|
|
llfn,
|
2015-01-29 06:03:34 -06:00
|
|
|
empty_substs,
|
2014-08-04 15:56:56 -05:00
|
|
|
mth.id,
|
2014-11-17 02:39:01 -06:00
|
|
|
&[]);
|
2014-07-21 18:42:34 -05:00
|
|
|
// Use InternalLinkage so LLVM can optimize more
|
|
|
|
// aggressively.
|
|
|
|
SetLinkage(llfn, InternalLinkage);
|
2014-08-04 15:56:56 -05:00
|
|
|
}
|
|
|
|
local_def(mth.id)
|
|
|
|
}
|
2014-08-05 21:44:21 -05:00
|
|
|
ast::TypeImplItem(_) => {
|
|
|
|
ccx.sess().bug("found TypeImplItem IIImplItem")
|
|
|
|
}
|
2014-08-04 15:56:56 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
};
|
2014-09-05 19:39:51 -05:00
|
|
|
|
|
|
|
return Some(inline_def);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_local_instance(ccx: &CrateContext, fn_id: ast::DefId)
|
|
|
|
-> Option<ast::DefId> {
|
|
|
|
if fn_id.krate == ast::LOCAL_CRATE {
|
|
|
|
Some(fn_id)
|
|
|
|
} else {
|
|
|
|
instantiate_inline(ccx, fn_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId) -> ast::DefId {
|
|
|
|
get_local_instance(ccx, fn_id).unwrap_or(fn_id)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|