2014-02-10 15:36:31 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-01-07 14:16:52 -08:00
|
|
|
|
2011-07-07 18:39:44 -07:00
|
|
|
// The crate store - a central repo for information collected about external
|
|
|
|
// crates and libraries
|
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::MetadataBlob::*;
|
|
|
|
|
2016-07-02 14:41:31 +03:00
|
|
|
use common;
|
2015-11-25 00:00:26 +02:00
|
|
|
use creader;
|
|
|
|
use decoder;
|
|
|
|
use index;
|
|
|
|
use loader;
|
|
|
|
|
2016-03-29 13:19:37 -04:00
|
|
|
use rustc::dep_graph::DepGraph;
|
2016-05-06 14:52:57 -04:00
|
|
|
use rustc::hir::def_id::{DefIndex, DefId};
|
|
|
|
use rustc::hir::map::DefKey;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::svh::Svh;
|
2016-07-26 10:58:35 -04:00
|
|
|
use rustc::middle::cstore::ExternCrate;
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
use rustc::session::config::PanicStrategy;
|
2016-06-28 23:41:09 +03:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap, FnvHashSet};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-06-25 10:07:01 -07:00
|
|
|
use std::cell::{RefCell, Ref, Cell};
|
2014-03-27 19:28:38 +02:00
|
|
|
use std::rc::Rc;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::path::PathBuf;
|
2014-11-25 13:28:35 -08:00
|
|
|
use flate::Bytes;
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
use syntax::ast::{self, Ident};
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr;
|
2015-02-11 18:29:49 +01:00
|
|
|
use syntax::codemap;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos;
|
2015-11-25 00:00:26 +02:00
|
|
|
|
|
|
|
pub use middle::cstore::{NativeLibraryKind, LinkagePreference};
|
|
|
|
pub use middle::cstore::{NativeStatic, NativeFramework, NativeUnknown};
|
|
|
|
pub use middle::cstore::{CrateSource, LinkMeta};
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2011-07-08 12:08:43 -07:00
|
|
|
// A map from external crate numbers (as decoded from some crate file) to
|
|
|
|
// local crate numbers (as generated during this session). Each external
|
|
|
|
// crate may refer to types in other external crates, and each has their
|
|
|
|
// own crate numbers.
|
2016-06-28 23:41:09 +03:00
|
|
|
pub type CrateNumMap = IndexVec<ast::CrateNum, ast::CrateNum>;
|
2011-07-08 12:08:43 -07:00
|
|
|
|
2013-12-18 14:10:28 -08:00
|
|
|
pub enum MetadataBlob {
|
2014-11-25 13:28:35 -08:00
|
|
|
MetadataVec(Bytes),
|
2013-12-16 20:58:21 -08:00
|
|
|
MetadataArchive(loader::ArchiveMetadata),
|
2013-12-18 14:10:28 -08:00
|
|
|
}
|
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
/// Holds information about a syntax_pos::FileMap imported from another crate.
|
2015-02-11 18:29:49 +01:00
|
|
|
/// See creader::import_codemap() for more information.
|
|
|
|
pub struct ImportedFileMap {
|
|
|
|
/// This FileMap's byte-offset within the codemap of its original crate
|
2016-06-21 18:08:13 -04:00
|
|
|
pub original_start_pos: syntax_pos::BytePos,
|
2015-02-11 18:29:49 +01:00
|
|
|
/// The end of this FileMap within the codemap of its original crate
|
2016-06-21 18:08:13 -04:00
|
|
|
pub original_end_pos: syntax_pos::BytePos,
|
2015-02-11 18:29:49 +01:00
|
|
|
/// The imported FileMap's representation within the local codemap
|
2016-06-21 18:08:13 -04:00
|
|
|
pub translated_filemap: Rc<syntax_pos::FileMap>
|
2015-02-11 18:29:49 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:41:09 +03:00
|
|
|
pub struct CrateMetadata {
|
2014-05-22 16:57:53 -07:00
|
|
|
pub name: String,
|
2016-03-16 05:50:38 -04:00
|
|
|
|
|
|
|
/// Information about the extern crate that caused this crate to
|
|
|
|
/// be loaded. If this is `None`, then the crate was injected
|
|
|
|
/// (e.g., by the allocator)
|
|
|
|
pub extern_crate: Cell<Option<ExternCrate>>,
|
|
|
|
|
2014-03-28 10:05:27 -07:00
|
|
|
pub data: MetadataBlob,
|
2016-06-28 23:41:09 +03:00
|
|
|
pub cnum_map: RefCell<CrateNumMap>,
|
2014-03-28 10:05:27 -07:00
|
|
|
pub cnum: ast::CrateNum,
|
2015-05-22 16:15:21 +03:00
|
|
|
pub codemap_import_info: RefCell<Vec<ImportedFileMap>>,
|
2015-06-25 10:07:01 -07:00
|
|
|
pub staged_api: bool,
|
2015-09-17 16:04:18 +03:00
|
|
|
|
2015-09-03 01:22:31 +03:00
|
|
|
pub index: index::Index,
|
2015-09-17 16:04:18 +03:00
|
|
|
pub xref_index: index::DenseIndex,
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2016-05-06 14:52:57 -04:00
|
|
|
/// For each public item in this crate, we encode a key. When the
|
|
|
|
/// crate is loaded, we read all the keys and put them in this
|
|
|
|
/// hashmap, which gives the reverse mapping. This allows us to
|
|
|
|
/// quickly retrace a `DefPath`, which is needed for incremental
|
|
|
|
/// compilation support.
|
|
|
|
pub key_map: FnvHashMap<DefKey, DefIndex>,
|
|
|
|
|
2015-06-25 10:07:01 -07:00
|
|
|
/// Flag if this crate is required by an rlib version of this crate, or in
|
|
|
|
/// other words whether it was explicitly linked to. An example of a crate
|
|
|
|
/// where this is false is when an allocator crate is injected into the
|
|
|
|
/// dependency list, and therefore isn't actually needed to link an rlib.
|
|
|
|
pub explicitly_linked: Cell<bool>,
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2016-07-26 10:58:35 -04:00
|
|
|
pub struct CachedInlinedItem {
|
|
|
|
/// The NodeId of the RootInlinedParent HIR map entry
|
|
|
|
pub inlined_root: ast::NodeId,
|
|
|
|
/// The local NodeId of the inlined entity
|
|
|
|
pub item_id: ast::NodeId,
|
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
pub struct CStore {
|
2016-04-22 15:47:14 -04:00
|
|
|
pub dep_graph: DepGraph,
|
2016-06-28 23:41:09 +03:00
|
|
|
metas: RefCell<FnvHashMap<ast::CrateNum, Rc<CrateMetadata>>>,
|
2014-11-10 00:59:56 +02:00
|
|
|
/// Map from NodeId's of local extern crate statements to crate numbers
|
|
|
|
extern_mod_crate_map: RefCell<NodeMap<ast::CrateNum>>,
|
2014-03-28 10:05:27 -07:00
|
|
|
used_crate_sources: RefCell<Vec<CrateSource>>,
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
used_libraries: RefCell<Vec<(String, NativeLibraryKind)>>,
|
2014-05-22 16:57:53 -07:00
|
|
|
used_link_args: RefCell<Vec<String>>,
|
2015-07-30 14:20:36 -07:00
|
|
|
statically_included_foreign_items: RefCell<NodeSet>,
|
2016-07-26 10:58:35 -04:00
|
|
|
pub inlined_item_cache: RefCell<DefIdMap<Option<CachedInlinedItem>>>,
|
|
|
|
pub defid_for_inlined_node: RefCell<NodeMap<DefId>>,
|
2016-03-19 09:01:29 +00:00
|
|
|
pub visible_parent_map: RefCell<DefIdMap<DefId>>,
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
pub used_for_derive_macro: RefCell<FnvHashSet<Ident>>,
|
2013-02-04 14:02:01 -08:00
|
|
|
}
|
2011-07-09 22:56:12 -07:00
|
|
|
|
2013-12-25 13:08:04 -07:00
|
|
|
impl CStore {
|
2016-07-11 09:42:31 +00:00
|
|
|
pub fn new(dep_graph: &DepGraph) -> CStore {
|
2013-12-25 13:08:04 -07:00
|
|
|
CStore {
|
2016-04-22 15:47:14 -04:00
|
|
|
dep_graph: dep_graph.clone(),
|
2015-01-16 14:27:43 -08:00
|
|
|
metas: RefCell::new(FnvHashMap()),
|
|
|
|
extern_mod_crate_map: RefCell::new(FnvHashMap()),
|
2014-03-04 10:02:49 -08:00
|
|
|
used_crate_sources: RefCell::new(Vec::new()),
|
|
|
|
used_libraries: RefCell::new(Vec::new()),
|
|
|
|
used_link_args: RefCell::new(Vec::new()),
|
2015-07-30 14:20:36 -07:00
|
|
|
statically_included_foreign_items: RefCell::new(NodeSet()),
|
2016-03-19 09:01:29 +00:00
|
|
|
visible_parent_map: RefCell::new(FnvHashMap()),
|
2016-07-26 10:58:35 -04:00
|
|
|
inlined_item_cache: RefCell::new(FnvHashMap()),
|
|
|
|
defid_for_inlined_node: RefCell::new(FnvHashMap()),
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
used_for_derive_macro: RefCell::new(FnvHashSet()),
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2014-04-07 12:14:33 -07:00
|
|
|
pub fn next_crate_num(&self) -> ast::CrateNum {
|
|
|
|
self.metas.borrow().len() as ast::CrateNum + 1
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:41:09 +03:00
|
|
|
pub fn get_crate_data(&self, cnum: ast::CrateNum) -> Rc<CrateMetadata> {
|
2015-03-21 21:15:47 -04:00
|
|
|
self.metas.borrow().get(&cnum).unwrap().clone()
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2014-02-24 19:45:20 -08:00
|
|
|
pub fn get_crate_hash(&self, cnum: ast::CrateNum) -> Svh {
|
2013-12-25 13:08:04 -07:00
|
|
|
let cdata = self.get_crate_data(cnum);
|
|
|
|
decoder::get_crate_hash(cdata.data())
|
|
|
|
}
|
2012-04-06 18:45:49 +08:00
|
|
|
|
2016-06-28 23:41:09 +03:00
|
|
|
pub fn set_crate_data(&self, cnum: ast::CrateNum, data: Rc<CrateMetadata>) {
|
2014-03-20 19:49:20 -07:00
|
|
|
self.metas.borrow_mut().insert(cnum, data);
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn iter_crate_data<I>(&self, mut i: I) where
|
2016-06-28 23:41:09 +03:00
|
|
|
I: FnMut(ast::CrateNum, &Rc<CrateMetadata>),
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-06-11 13:56:07 +01:00
|
|
|
for (&k, v) in self.metas.borrow().iter() {
|
2015-06-25 10:07:01 -07:00
|
|
|
i(k, v);
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2013-02-04 14:02:01 -08:00
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2014-04-25 18:06:49 +02:00
|
|
|
/// Like `iter_crate_data`, but passes source paths (if available) as well.
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn iter_crate_data_origins<I>(&self, mut i: I) where
|
2016-06-28 23:41:09 +03:00
|
|
|
I: FnMut(ast::CrateNum, &CrateMetadata, Option<CrateSource>),
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-06-11 13:56:07 +01:00
|
|
|
for (&k, v) in self.metas.borrow().iter() {
|
2015-11-25 17:02:59 +02:00
|
|
|
let origin = self.opt_used_crate_source(k);
|
2014-04-25 18:06:49 +02:00
|
|
|
origin.as_ref().map(|cs| { assert!(k == cs.cnum); });
|
2016-02-09 21:37:21 +01:00
|
|
|
i(k, &v, origin);
|
2014-04-25 18:06:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:00:58 -08:00
|
|
|
pub fn add_used_crate_source(&self, src: CrateSource) {
|
2013-12-20 19:50:13 -08:00
|
|
|
let mut used_crate_sources = self.used_crate_sources.borrow_mut();
|
2014-03-20 19:49:20 -07:00
|
|
|
if !used_crate_sources.contains(&src) {
|
|
|
|
used_crate_sources.push(src);
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2011-07-07 18:25:56 -07:00
|
|
|
}
|
|
|
|
|
2015-11-25 17:02:59 +02:00
|
|
|
pub fn opt_used_crate_source(&self, cnum: ast::CrateNum)
|
|
|
|
-> Option<CrateSource> {
|
2014-03-20 19:49:20 -07:00
|
|
|
self.used_crate_sources.borrow_mut()
|
2015-02-13 07:33:44 +00:00
|
|
|
.iter().find(|source| source.cnum == cnum).cloned()
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&self) {
|
2014-03-20 19:55:52 -07:00
|
|
|
self.metas.borrow_mut().clear();
|
|
|
|
self.extern_mod_crate_map.borrow_mut().clear();
|
|
|
|
self.used_crate_sources.borrow_mut().clear();
|
|
|
|
self.used_libraries.borrow_mut().clear();
|
|
|
|
self.used_link_args.borrow_mut().clear();
|
2015-07-30 14:20:36 -07:00
|
|
|
self.statically_included_foreign_items.borrow_mut().clear();
|
2013-12-25 11:10:33 -07:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:41:09 +03:00
|
|
|
pub fn crate_dependencies_in_rpo(&self, krate: ast::CrateNum) -> Vec<ast::CrateNum>
|
|
|
|
{
|
|
|
|
let mut ordering = Vec::new();
|
|
|
|
self.push_dependencies_in_postorder(&mut ordering, krate);
|
|
|
|
ordering.reverse();
|
|
|
|
ordering
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_dependencies_in_postorder(&self,
|
|
|
|
ordering: &mut Vec<ast::CrateNum>,
|
|
|
|
krate: ast::CrateNum)
|
|
|
|
{
|
|
|
|
if ordering.contains(&krate) { return }
|
|
|
|
|
|
|
|
let data = self.get_crate_data(krate);
|
|
|
|
for &dep in data.cnum_map.borrow().iter() {
|
|
|
|
if dep != krate {
|
|
|
|
self.push_dependencies_in_postorder(ordering, dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ordering.push(krate);
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:47:43 -07:00
|
|
|
// This method is used when generating the command line to pass through to
|
|
|
|
// system linker. The linker expects undefined symbols on the left of the
|
|
|
|
// command line to be defined in libraries on the right, not the other way
|
|
|
|
// around. For more info, see some comments in the add_used_library function
|
|
|
|
// below.
|
|
|
|
//
|
|
|
|
// In order to get this left-to-right dependency ordering, we perform a
|
|
|
|
// topological sort of all crates putting the leaves at the right-most
|
|
|
|
// positions.
|
2015-11-21 01:08:09 +02:00
|
|
|
pub fn do_get_used_crates(&self, prefer: LinkagePreference)
|
|
|
|
-> Vec<(ast::CrateNum, Option<PathBuf>)> {
|
2014-03-13 18:47:43 -07:00
|
|
|
let mut ordering = Vec::new();
|
2015-06-11 13:56:07 +01:00
|
|
|
for (&num, _) in self.metas.borrow().iter() {
|
2016-06-28 23:41:09 +03:00
|
|
|
self.push_dependencies_in_postorder(&mut ordering, num);
|
2014-03-13 18:47:43 -07:00
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
info!("topological ordering: {:?}", ordering);
|
2014-11-27 16:47:48 -05:00
|
|
|
ordering.reverse();
|
2014-03-20 19:49:20 -07:00
|
|
|
let mut libs = self.used_crate_sources.borrow()
|
2013-12-20 19:50:13 -08:00
|
|
|
.iter()
|
2013-12-25 13:08:04 -07:00
|
|
|
.map(|src| (src.cnum, match prefer {
|
2015-11-25 00:00:26 +02:00
|
|
|
LinkagePreference::RequireDynamic => src.dylib.clone().map(|p| p.0),
|
|
|
|
LinkagePreference::RequireStatic => src.rlib.clone().map(|p| p.0),
|
2013-12-25 13:08:04 -07:00
|
|
|
}))
|
2015-01-06 08:46:07 -08:00
|
|
|
.collect::<Vec<_>>();
|
2014-03-13 18:47:43 -07:00
|
|
|
libs.sort_by(|&(a, _), &(b, _)| {
|
2015-07-08 08:33:13 -07:00
|
|
|
let a = ordering.iter().position(|x| *x == a);
|
|
|
|
let b = ordering.iter().position(|x| *x == b);
|
|
|
|
a.cmp(&b)
|
2014-03-13 18:47:43 -07:00
|
|
|
});
|
|
|
|
libs
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
pub fn add_used_library(&self, lib: String, kind: NativeLibraryKind) {
|
2013-12-25 13:08:04 -07:00
|
|
|
assert!(!lib.is_empty());
|
2014-03-20 19:49:20 -07:00
|
|
|
self.used_libraries.borrow_mut().push((lib, kind));
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2011-07-07 18:25:56 -07:00
|
|
|
|
2013-12-20 19:54:01 -08:00
|
|
|
pub fn get_used_libraries<'a>(&'a self)
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
-> &'a RefCell<Vec<(String,
|
|
|
|
NativeLibraryKind)>> {
|
2013-12-20 19:54:01 -08:00
|
|
|
&self.used_libraries
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2011-07-07 18:33:59 -07:00
|
|
|
|
2013-12-20 20:00:58 -08:00
|
|
|
pub fn add_used_link_args(&self, args: &str) {
|
2014-07-07 09:54:38 -07:00
|
|
|
for s in args.split(' ').filter(|s| !s.is_empty()) {
|
2014-05-25 03:17:19 -07:00
|
|
|
self.used_link_args.borrow_mut().push(s.to_string());
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-07 18:33:59 -07:00
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn get_used_link_args<'a>(&'a self) -> &'a RefCell<Vec<String> > {
|
2013-12-20 19:56:29 -08:00
|
|
|
&self.used_link_args
|
2013-12-25 13:08:04 -07:00
|
|
|
}
|
2011-07-07 18:33:59 -07:00
|
|
|
|
2013-12-20 20:00:58 -08:00
|
|
|
pub fn add_extern_mod_stmt_cnum(&self,
|
2013-12-25 13:08:04 -07:00
|
|
|
emod_id: ast::NodeId,
|
|
|
|
cnum: ast::CrateNum) {
|
2014-03-20 19:49:20 -07:00
|
|
|
self.extern_mod_crate_map.borrow_mut().insert(emod_id, cnum);
|
2013-03-24 07:51:18 +01:00
|
|
|
}
|
2011-07-07 18:38:42 -07:00
|
|
|
|
2015-07-30 14:20:36 -07:00
|
|
|
pub fn add_statically_included_foreign_item(&self, id: ast::NodeId) {
|
|
|
|
self.statically_included_foreign_items.borrow_mut().insert(id);
|
|
|
|
}
|
|
|
|
|
2015-11-21 21:39:05 +02:00
|
|
|
pub fn do_is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool {
|
2015-07-30 14:20:36 -07:00
|
|
|
self.statically_included_foreign_items.borrow().contains(&id)
|
|
|
|
}
|
2015-11-25 17:02:59 +02:00
|
|
|
|
|
|
|
pub fn do_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>
|
|
|
|
{
|
|
|
|
self.extern_mod_crate_map.borrow().get(&emod_id).cloned()
|
|
|
|
}
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
|
|
|
|
pub fn was_used_for_derive_macros(&self, i: &ast::Item) -> bool {
|
|
|
|
self.used_for_derive_macro.borrow().contains(&i.ident)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_used_for_derive_macros(&self, i: &ast::Item) {
|
|
|
|
self.used_for_derive_macro.borrow_mut().insert(i.ident);
|
|
|
|
}
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:41:09 +03:00
|
|
|
impl CrateMetadata {
|
2013-12-18 14:10:28 -08:00
|
|
|
pub fn data<'a>(&'a self) -> &'a [u8] { self.data.as_slice() }
|
2016-03-01 08:19:00 -05:00
|
|
|
pub fn name(&self) -> &str { decoder::get_crate_name(self.data()) }
|
2014-04-07 12:14:33 -07:00
|
|
|
pub fn hash(&self) -> Svh { decoder::get_crate_hash(self.data()) }
|
2016-02-26 16:25:25 -05:00
|
|
|
pub fn disambiguator(&self) -> &str {
|
|
|
|
decoder::get_crate_disambiguator(self.data())
|
|
|
|
}
|
2015-05-22 16:15:21 +03:00
|
|
|
pub fn imported_filemaps<'a>(&'a self, codemap: &codemap::CodeMap)
|
|
|
|
-> Ref<'a, Vec<ImportedFileMap>> {
|
|
|
|
let filemaps = self.codemap_import_info.borrow();
|
|
|
|
if filemaps.is_empty() {
|
|
|
|
drop(filemaps);
|
|
|
|
let filemaps = creader::import_codemap(codemap, &self.data);
|
|
|
|
|
|
|
|
// This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
|
|
|
|
*self.codemap_import_info.borrow_mut() = filemaps;
|
|
|
|
self.codemap_import_info.borrow()
|
|
|
|
} else {
|
|
|
|
filemaps
|
|
|
|
}
|
2015-08-01 16:14:45 +02:00
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
|
|
|
|
pub fn is_allocator(&self) -> bool {
|
|
|
|
let attrs = decoder::get_crate_attributes(self.data());
|
|
|
|
attr::contains_name(&attrs, "allocator")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_allocator(&self) -> bool {
|
|
|
|
let attrs = decoder::get_crate_attributes(self.data());
|
|
|
|
attr::contains_name(&attrs, "needs_allocator")
|
|
|
|
}
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
|
|
|
|
pub fn is_panic_runtime(&self) -> bool {
|
|
|
|
let attrs = decoder::get_crate_attributes(self.data());
|
|
|
|
attr::contains_name(&attrs, "panic_runtime")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_panic_runtime(&self) -> bool {
|
|
|
|
let attrs = decoder::get_crate_attributes(self.data());
|
|
|
|
attr::contains_name(&attrs, "needs_panic_runtime")
|
|
|
|
}
|
|
|
|
|
2016-07-24 21:42:11 -05:00
|
|
|
pub fn is_compiler_builtins(&self) -> bool {
|
|
|
|
let attrs = decoder::get_crate_attributes(self.data());
|
|
|
|
attr::contains_name(&attrs, "compiler_builtins")
|
|
|
|
}
|
|
|
|
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
pub fn panic_strategy(&self) -> PanicStrategy {
|
|
|
|
decoder::get_panic_strategy(self.data())
|
|
|
|
}
|
2013-12-18 14:10:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MetadataBlob {
|
2016-07-02 14:41:31 +03:00
|
|
|
pub fn as_slice_raw<'a>(&'a self) -> &'a [u8] {
|
|
|
|
match *self {
|
2015-03-18 09:14:54 -07:00
|
|
|
MetadataVec(ref vec) => &vec[..],
|
2013-12-16 20:58:21 -08:00
|
|
|
MetadataArchive(ref ar) => ar.as_slice(),
|
2016-07-02 14:41:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [u8] {
|
|
|
|
let slice = self.as_slice_raw();
|
|
|
|
let len_offset = 4 + common::metadata_encoding_version.len();
|
|
|
|
if slice.len() < len_offset+4 {
|
2014-12-12 18:00:17 -08:00
|
|
|
&[] // corrupt metadata
|
2014-12-04 21:32:34 -08:00
|
|
|
} else {
|
2016-07-02 14:41:31 +03:00
|
|
|
let len = (((slice[len_offset+0] as u32) << 24) |
|
|
|
|
((slice[len_offset+1] as u32) << 16) |
|
|
|
|
((slice[len_offset+2] as u32) << 8) |
|
|
|
|
((slice[len_offset+3] as u32) << 0)) as usize;
|
|
|
|
if len <= slice.len() - 4 - len_offset {
|
|
|
|
&slice[len_offset + 4..len_offset + len + 4]
|
2014-12-12 18:00:17 -08:00
|
|
|
} else {
|
|
|
|
&[] // corrupt or old metadata
|
|
|
|
}
|
2013-12-18 14:10:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|