2015-02-03 01:40:52 +01:00
|
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 01:48:01 +01: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.
|
|
|
|
|
|
2016-12-15 12:13:24 +01:00
|
|
|
|
use rustc::hir::{self, map as hir_map};
|
2016-06-21 04:11:45 +02:00
|
|
|
|
use rustc::hir::lowering::lower_crate;
|
2017-03-20 16:35:06 +01:00
|
|
|
|
use rustc::ich::Fingerprint;
|
2016-12-14 00:45:03 +01:00
|
|
|
|
use rustc_data_structures::stable_hasher::StableHasher;
|
2015-08-19 00:01:44 +02:00
|
|
|
|
use rustc_mir as mir;
|
2016-01-27 07:01:01 +01:00
|
|
|
|
use rustc::session::{Session, CompileResult, compile_result_from_err_count};
|
2016-08-02 22:53:58 +02:00
|
|
|
|
use rustc::session::config::{self, Input, OutputFilenames, OutputType,
|
|
|
|
|
OutputTypes};
|
2014-12-16 23:32:02 +01:00
|
|
|
|
use rustc::session::search_paths::PathKind;
|
2014-11-27 15:57:47 +01:00
|
|
|
|
use rustc::lint;
|
2016-03-22 16:30:57 +01:00
|
|
|
|
use rustc::middle::{self, dependency_format, stability, reachable};
|
2016-01-27 21:49:18 +01:00
|
|
|
|
use rustc::middle::privacy::AccessLevels;
|
2016-12-24 04:48:21 +01:00
|
|
|
|
use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas};
|
2014-11-27 15:57:47 +01:00
|
|
|
|
use rustc::util::common::time;
|
2016-10-04 01:19:40 +02:00
|
|
|
|
use rustc::util::nodemap::NodeSet;
|
2017-02-03 10:42:01 +01:00
|
|
|
|
use rustc::util::fs::rename_or_copy_remove;
|
2014-12-05 20:17:35 +01:00
|
|
|
|
use rustc_borrowck as borrowck;
|
2016-08-23 13:47:14 +02:00
|
|
|
|
use rustc_incremental::{self, IncrementalHashesMap};
|
2016-06-22 03:54:34 +02:00
|
|
|
|
use rustc_resolve::{MakeGlobMap, Resolver};
|
2016-09-16 04:52:09 +02:00
|
|
|
|
use rustc_metadata::creader::CrateLoader;
|
2016-09-29 01:30:53 +02:00
|
|
|
|
use rustc_metadata::cstore::{self, CStore};
|
2016-06-21 04:11:45 +02:00
|
|
|
|
use rustc_trans::back::{link, write};
|
2016-03-22 18:23:36 +01:00
|
|
|
|
use rustc_trans as trans;
|
2014-11-26 12:11:29 +01:00
|
|
|
|
use rustc_typeck as typeck;
|
2015-01-15 19:47:17 +01:00
|
|
|
|
use rustc_privacy;
|
2015-11-22 21:14:09 +01:00
|
|
|
|
use rustc_plugin::registry::Registry;
|
|
|
|
|
use rustc_plugin as plugin;
|
2017-04-11 22:53:20 +02:00
|
|
|
|
use rustc_passes::{ast_validation, no_asm, loops, consts,
|
2016-11-30 18:14:17 +01:00
|
|
|
|
static_recursion, hir_stats, mir_stats};
|
2017-04-14 18:00:08 +02:00
|
|
|
|
use rustc_const_eval::{self, check_match};
|
2016-01-27 07:01:01 +01:00
|
|
|
|
use super::Compilation;
|
2012-12-23 23:41:37 +01:00
|
|
|
|
|
2014-12-12 19:59:41 +01:00
|
|
|
|
use serialize::json;
|
2014-02-19 08:27:49 +01:00
|
|
|
|
|
2015-01-27 21:20:58 +01:00
|
|
|
|
use std::env;
|
2015-06-11 03:18:04 +02:00
|
|
|
|
use std::ffi::{OsString, OsStr};
|
2015-02-27 06:00:43 +01:00
|
|
|
|
use std::fs;
|
|
|
|
|
use std::io::{self, Write};
|
2017-01-09 23:46:34 +01:00
|
|
|
|
use std::iter;
|
2015-02-27 06:00:43 +01:00
|
|
|
|
use std::path::{Path, PathBuf};
|
2017-03-23 20:13:29 +01:00
|
|
|
|
use std::rc::Rc;
|
2016-06-22 03:16:56 +02:00
|
|
|
|
use syntax::{ast, diagnostics, visit};
|
2016-08-23 05:54:53 +02:00
|
|
|
|
use syntax::attr;
|
2016-09-08 01:21:59 +02:00
|
|
|
|
use syntax::ext::base::ExtCtxt;
|
2016-11-16 09:21:52 +01:00
|
|
|
|
use syntax::parse::{self, PResult};
|
2016-11-17 15:04:36 +01:00
|
|
|
|
use syntax::symbol::Symbol;
|
2015-11-11 06:26:14 +01:00
|
|
|
|
use syntax::util::node_count::NodeCounter;
|
2012-12-23 23:41:37 +01:00
|
|
|
|
use syntax;
|
2015-12-10 15:23:14 +01:00
|
|
|
|
use syntax_ext;
|
2016-12-24 04:48:21 +01:00
|
|
|
|
use arena::DroplessArena;
|
2011-12-20 11:17:13 +01:00
|
|
|
|
|
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-23 02:07:11 +02:00
|
|
|
|
use derive_registrar;
|
|
|
|
|
|
2016-01-21 01:19:20 +01:00
|
|
|
|
pub fn compile_input(sess: &Session,
|
2015-11-21 20:39:05 +01:00
|
|
|
|
cstore: &CStore,
|
2014-05-06 13:38:01 +02:00
|
|
|
|
input: &Input,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
outdir: &Option<PathBuf>,
|
|
|
|
|
output: &Option<PathBuf>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
addl_plugins: Option<Vec<String>>,
|
2016-01-05 23:38:11 +01:00
|
|
|
|
control: &CompileController) -> CompileResult {
|
2016-01-27 21:49:18 +01:00
|
|
|
|
macro_rules! controller_entry_point {
|
|
|
|
|
($point: ident, $tsess: expr, $make_state: expr, $phase_result: expr) => {{
|
2016-04-20 06:24:14 +02:00
|
|
|
|
let state = &mut $make_state;
|
2016-01-27 21:49:18 +01:00
|
|
|
|
let phase_result: &CompileResult = &$phase_result;
|
|
|
|
|
if phase_result.is_ok() || control.$point.run_callback_on_error {
|
|
|
|
|
(control.$point.callback)(state);
|
|
|
|
|
}
|
2015-02-09 20:01:45 +01:00
|
|
|
|
|
2016-01-27 21:49:18 +01:00
|
|
|
|
if control.$point.stop == Compilation::Stop {
|
|
|
|
|
return compile_result_from_err_count($tsess.err_count());
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
}
|
2015-01-11 03:03:34 +01:00
|
|
|
|
|
2014-05-06 13:38:01 +02:00
|
|
|
|
// We need nested scopes here, because the intermediate results can keep
|
|
|
|
|
// large chunks of memory alive and we want to free them as soon as
|
|
|
|
|
// possible to keep the peak memory usage low
|
2016-08-12 01:02:39 +02:00
|
|
|
|
let (outputs, trans) = {
|
2016-10-27 08:36:56 +02:00
|
|
|
|
let krate = match phase_1_parse_input(sess, input) {
|
2016-05-26 01:15:23 +02:00
|
|
|
|
Ok(krate) => krate,
|
|
|
|
|
Err(mut parse_error) => {
|
|
|
|
|
parse_error.emit();
|
|
|
|
|
return Err(1);
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-01-11 03:03:34 +01:00
|
|
|
|
|
2016-09-04 05:22:56 +02:00
|
|
|
|
let (krate, registry) = {
|
2016-04-20 06:24:14 +02:00
|
|
|
|
let mut compile_state = CompileState::state_after_parse(input,
|
|
|
|
|
sess,
|
|
|
|
|
outdir,
|
|
|
|
|
output,
|
|
|
|
|
krate,
|
|
|
|
|
&cstore);
|
2015-01-11 03:03:34 +01:00
|
|
|
|
controller_entry_point!(after_parse,
|
2015-02-09 20:01:45 +01:00
|
|
|
|
sess,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
compile_state,
|
2016-01-27 21:49:18 +01:00
|
|
|
|
Ok(()));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2016-09-04 05:22:56 +02:00
|
|
|
|
(compile_state.krate.unwrap(), compile_state.registry)
|
2016-05-26 01:15:23 +02:00
|
|
|
|
};
|
2014-06-10 23:03:19 +02:00
|
|
|
|
|
2016-05-26 01:15:23 +02:00
|
|
|
|
let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
|
2016-07-22 16:39:30 +02:00
|
|
|
|
let crate_name = link::find_crate_name(Some(sess), &krate.attrs, input);
|
2016-05-26 01:15:23 +02:00
|
|
|
|
let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
|
2016-06-28 03:45:54 +02:00
|
|
|
|
phase_2_configure_and_expand(
|
2016-09-04 05:22:56 +02:00
|
|
|
|
sess, &cstore, krate, registry, &crate_name, addl_plugins, control.make_glob_map,
|
2016-06-28 03:45:54 +02:00
|
|
|
|
|expanded_crate| {
|
|
|
|
|
let mut state = CompileState::state_after_expand(
|
2016-07-22 16:39:30 +02:00
|
|
|
|
input, sess, outdir, output, &cstore, expanded_crate, &crate_name,
|
2016-06-28 03:45:54 +02:00
|
|
|
|
);
|
|
|
|
|
controller_entry_point!(after_expand, sess, state, Ok(()));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
)?
|
2014-05-06 13:38:01 +02:00
|
|
|
|
};
|
2014-09-07 19:09:06 +02:00
|
|
|
|
|
2016-07-22 16:39:30 +02:00
|
|
|
|
write_out_deps(sess, &outputs, &crate_name);
|
2017-03-08 00:15:55 +01:00
|
|
|
|
if sess.opts.output_types.contains_key(&OutputType::DepInfo) &&
|
|
|
|
|
sess.opts.output_types.keys().count() == 1 {
|
|
|
|
|
return Ok(())
|
|
|
|
|
}
|
2016-05-06 06:48:44 +02:00
|
|
|
|
|
2016-12-24 04:48:21 +01:00
|
|
|
|
let arena = DroplessArena::new();
|
|
|
|
|
let arenas = GlobalArenas::new();
|
2016-04-14 02:39:18 +02:00
|
|
|
|
|
|
|
|
|
// Construct the HIR map
|
|
|
|
|
let hir_map = time(sess.time_passes(),
|
|
|
|
|
"indexing hir",
|
2016-05-26 01:15:23 +02:00
|
|
|
|
|| hir_map::map_crate(&mut hir_forest, defs));
|
2016-04-14 02:39:18 +02:00
|
|
|
|
|
2016-01-29 21:04:07 +01:00
|
|
|
|
{
|
|
|
|
|
let _ignore = hir_map.dep_graph.in_ignore();
|
2016-05-08 19:08:09 +02:00
|
|
|
|
controller_entry_point!(after_hir_lowering,
|
2016-01-29 21:04:07 +01:00
|
|
|
|
sess,
|
2016-05-08 19:08:09 +02:00
|
|
|
|
CompileState::state_after_hir_lowering(input,
|
2016-05-06 06:48:44 +02:00
|
|
|
|
sess,
|
|
|
|
|
outdir,
|
|
|
|
|
output,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
&arena,
|
2016-05-06 06:48:44 +02:00
|
|
|
|
&arenas,
|
|
|
|
|
&cstore,
|
|
|
|
|
&hir_map,
|
|
|
|
|
&analysis,
|
|
|
|
|
&resolutions,
|
|
|
|
|
&expanded_crate,
|
|
|
|
|
&hir_map.krate(),
|
2016-07-22 16:39:30 +02:00
|
|
|
|
&crate_name),
|
2016-01-29 21:04:07 +01:00
|
|
|
|
Ok(()));
|
|
|
|
|
}
|
2015-01-14 21:30:34 +01:00
|
|
|
|
|
2015-09-25 08:25:59 +02:00
|
|
|
|
time(sess.time_passes(), "attribute checking", || {
|
2016-03-29 07:50:44 +02:00
|
|
|
|
hir::check_attr::check_crate(sess, &expanded_crate);
|
2015-09-25 08:25:59 +02:00
|
|
|
|
});
|
|
|
|
|
|
2016-04-26 00:14:44 +02:00
|
|
|
|
let opt_crate = if keep_ast(sess) {
|
2015-12-25 17:17:45 +01:00
|
|
|
|
Some(&expanded_crate)
|
|
|
|
|
} else {
|
|
|
|
|
drop(expanded_crate);
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-23 04:01:37 +01:00
|
|
|
|
phase_3_run_analysis_passes(sess,
|
2016-03-22 23:58:45 +01:00
|
|
|
|
hir_map,
|
2016-05-04 08:00:27 +02:00
|
|
|
|
analysis,
|
|
|
|
|
resolutions,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
&arena,
|
2016-03-22 23:58:45 +01:00
|
|
|
|
&arenas,
|
2016-07-22 16:39:30 +02:00
|
|
|
|
&crate_name,
|
2016-10-28 12:55:49 +02:00
|
|
|
|
|tcx, analysis, incremental_hashes_map, result| {
|
2016-01-21 01:19:20 +01:00
|
|
|
|
{
|
2016-01-29 21:04:07 +01:00
|
|
|
|
// Eventually, we will want to track plugins.
|
|
|
|
|
let _ignore = tcx.dep_graph.in_ignore();
|
|
|
|
|
|
2016-04-20 06:24:14 +02:00
|
|
|
|
let mut state = CompileState::state_after_analysis(input,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
sess,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
outdir,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
output,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
opt_crate,
|
2017-01-26 01:41:06 +01:00
|
|
|
|
tcx.hir.krate(),
|
2016-04-20 06:24:14 +02:00
|
|
|
|
&analysis,
|
|
|
|
|
tcx,
|
2016-07-22 16:39:30 +02:00
|
|
|
|
&crate_name);
|
2016-04-20 06:24:14 +02:00
|
|
|
|
(control.after_analysis.callback)(&mut state);
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
|
|
|
|
if control.after_analysis.stop == Compilation::Stop {
|
2016-04-08 01:47:12 +02:00
|
|
|
|
return result.and_then(|_| Err(0usize));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-06 13:38:01 +02:00
|
|
|
|
|
2016-03-23 04:01:37 +01:00
|
|
|
|
result?;
|
2016-01-27 21:49:18 +01:00
|
|
|
|
|
2017-02-15 16:57:59 +01:00
|
|
|
|
if log_enabled!(::log::LogLevel::Info) {
|
2016-01-21 01:19:20 +01:00
|
|
|
|
println!("Pre-trans");
|
|
|
|
|
tcx.print_debug_stats();
|
|
|
|
|
}
|
2017-02-13 10:57:50 +01:00
|
|
|
|
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map,
|
|
|
|
|
&outputs);
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2017-02-15 16:57:59 +01:00
|
|
|
|
if log_enabled!(::log::LogLevel::Info) {
|
2016-01-21 01:19:20 +01:00
|
|
|
|
println!("Post-trans");
|
|
|
|
|
tcx.print_debug_stats();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 22:59:09 +01:00
|
|
|
|
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
|
|
|
|
|
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
|
|
|
|
|
sess.err(&format!("could not emit MIR: {}", e));
|
|
|
|
|
sess.abort_if_errors();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 01:02:39 +02:00
|
|
|
|
Ok((outputs, trans))
|
2016-03-23 04:01:37 +01:00
|
|
|
|
})??
|
2014-05-06 13:38:01 +02:00
|
|
|
|
};
|
2015-06-14 03:50:23 +02:00
|
|
|
|
|
2016-11-14 17:46:20 +01:00
|
|
|
|
if sess.opts.debugging_opts.print_type_sizes {
|
|
|
|
|
sess.code_stats.borrow().print_type_sizes();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 01:02:39 +02:00
|
|
|
|
let phase5_result = phase_5_run_llvm_passes(sess, &trans, &outputs);
|
2015-01-11 03:03:34 +01:00
|
|
|
|
|
|
|
|
|
controller_entry_point!(after_llvm,
|
2015-02-09 20:01:45 +01:00
|
|
|
|
sess,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
CompileState::state_after_llvm(input, sess, outdir, output, &trans),
|
2016-01-27 21:49:18 +01:00
|
|
|
|
phase5_result);
|
2016-03-23 04:01:37 +01:00
|
|
|
|
phase5_result?;
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2016-06-30 22:32:13 +02:00
|
|
|
|
write::cleanup_llvm(&trans);
|
|
|
|
|
|
2016-01-21 01:19:20 +01:00
|
|
|
|
phase_6_link_output(sess, &trans, &outputs);
|
2015-01-11 03:03:34 +01:00
|
|
|
|
|
2016-08-12 01:02:39 +02:00
|
|
|
|
// Now that we won't touch anything in the incremental compilation directory
|
|
|
|
|
// any more, we can finalize it (which involves renaming it)
|
|
|
|
|
rustc_incremental::finalize_session_directory(sess, trans.link.crate_hash);
|
|
|
|
|
|
2016-08-23 19:23:58 +02:00
|
|
|
|
if sess.opts.debugging_opts.perf_stats {
|
|
|
|
|
sess.print_perf_stats();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-04 08:47:53 +02:00
|
|
|
|
controller_entry_point!(compilation_done,
|
2016-07-01 10:54:37 +02:00
|
|
|
|
sess,
|
2016-07-04 08:47:53 +02:00
|
|
|
|
CompileState::state_when_compilation_done(input, sess, outdir, output),
|
2016-07-01 10:54:37 +02:00
|
|
|
|
Ok(()));
|
|
|
|
|
|
2016-01-21 01:19:20 +01:00
|
|
|
|
Ok(())
|
2012-12-23 23:41:37 +01:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
|
2016-07-16 21:11:28 +02:00
|
|
|
|
fn keep_hygiene_data(sess: &Session) -> bool {
|
|
|
|
|
sess.opts.debugging_opts.keep_hygiene_data
|
2016-04-26 00:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn keep_ast(sess: &Session) -> bool {
|
2017-03-23 04:32:49 +01:00
|
|
|
|
sess.opts.debugging_opts.keep_ast || ::save_analysis(sess)
|
2016-04-26 00:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 02:06:06 +01:00
|
|
|
|
/// The name used for source code that doesn't originate in a file
|
|
|
|
|
/// (e.g. source from stdin or a string)
|
2014-05-23 01:57:53 +02:00
|
|
|
|
pub fn anon_src() -> String {
|
2014-05-25 12:17:19 +02:00
|
|
|
|
"<anon>".to_string()
|
2014-01-16 01:42:51 +01:00
|
|
|
|
}
|
2012-05-10 04:41:24 +02:00
|
|
|
|
|
2014-05-23 01:57:53 +02:00
|
|
|
|
pub fn source_name(input: &Input) -> String {
|
2013-04-17 18:15:37 +02:00
|
|
|
|
match *input {
|
2014-03-16 19:56:24 +01:00
|
|
|
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
2015-02-27 06:00:43 +01:00
|
|
|
|
Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
|
2016-03-10 04:49:40 +01:00
|
|
|
|
Input::Str { ref name, .. } => name.clone(),
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 07:11:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-11 03:03:34 +01:00
|
|
|
|
/// CompileController is used to customise compilation, it allows compilation to
|
|
|
|
|
/// be stopped and/or to call arbitrary code at various points in compilation.
|
|
|
|
|
/// It also allows for various flags to be set to influence what information gets
|
2015-02-05 05:00:02 +01:00
|
|
|
|
/// collected during compilation.
|
2015-01-11 03:03:34 +01:00
|
|
|
|
///
|
|
|
|
|
/// This is a somewhat higher level controller than a Session - the Session
|
|
|
|
|
/// controls what happens in each phase, whereas the CompileController controls
|
|
|
|
|
/// whether a phase is run at all and whether other code (from outside the
|
|
|
|
|
/// the compiler) is run between phases.
|
|
|
|
|
///
|
|
|
|
|
/// Note that if compilation is set to stop and a callback is provided for a
|
|
|
|
|
/// given entry point, the callback is called before compilation is stopped.
|
|
|
|
|
///
|
|
|
|
|
/// Expect more entry points to be added in the future.
|
|
|
|
|
pub struct CompileController<'a> {
|
|
|
|
|
pub after_parse: PhaseController<'a>,
|
2016-06-28 03:45:54 +02:00
|
|
|
|
pub after_expand: PhaseController<'a>,
|
2016-05-08 19:08:09 +02:00
|
|
|
|
pub after_hir_lowering: PhaseController<'a>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
pub after_analysis: PhaseController<'a>,
|
|
|
|
|
pub after_llvm: PhaseController<'a>,
|
2016-07-04 08:47:53 +02:00
|
|
|
|
pub compilation_done: PhaseController<'a>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
|
2016-06-22 03:54:34 +02:00
|
|
|
|
pub make_glob_map: MakeGlobMap,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> CompileController<'a> {
|
|
|
|
|
pub fn basic() -> CompileController<'a> {
|
|
|
|
|
CompileController {
|
|
|
|
|
after_parse: PhaseController::basic(),
|
2016-06-28 03:45:54 +02:00
|
|
|
|
after_expand: PhaseController::basic(),
|
2016-05-08 19:08:09 +02:00
|
|
|
|
after_hir_lowering: PhaseController::basic(),
|
2015-01-11 03:03:34 +01:00
|
|
|
|
after_analysis: PhaseController::basic(),
|
|
|
|
|
after_llvm: PhaseController::basic(),
|
2016-07-04 08:47:53 +02:00
|
|
|
|
compilation_done: PhaseController::basic(),
|
2016-06-22 03:54:34 +02:00
|
|
|
|
make_glob_map: MakeGlobMap::No,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct PhaseController<'a> {
|
2015-02-03 01:40:52 +01:00
|
|
|
|
pub stop: Compilation,
|
2016-01-27 21:49:18 +01:00
|
|
|
|
// If true then the compiler will try to run the callback even if the phase
|
|
|
|
|
// ends with an error. Note that this is not always possible.
|
|
|
|
|
pub run_callback_on_error: bool,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
pub callback: Box<Fn(&mut CompileState) + 'a>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> PhaseController<'a> {
|
|
|
|
|
pub fn basic() -> PhaseController<'a> {
|
|
|
|
|
PhaseController {
|
2015-02-03 01:40:52 +01:00
|
|
|
|
stop: Compilation::Continue,
|
2016-01-27 21:49:18 +01:00
|
|
|
|
run_callback_on_error: false,
|
2015-02-01 18:44:15 +01:00
|
|
|
|
callback: box |_| {},
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// State that is passed to a callback. What state is available depends on when
|
|
|
|
|
/// during compilation the callback is made. See the various constructor methods
|
|
|
|
|
/// (`state_*`) in the impl to see which data is provided for any given entry point.
|
2016-10-29 12:19:59 +02:00
|
|
|
|
pub struct CompileState<'a, 'tcx: 'a> {
|
2015-01-11 03:03:34 +01:00
|
|
|
|
pub input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
pub session: &'tcx Session,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
pub krate: Option<ast::Crate>,
|
2016-09-04 05:22:56 +02:00
|
|
|
|
pub registry: Option<Registry<'a>>,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
pub cstore: Option<&'a CStore>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
pub crate_name: Option<&'a str>,
|
|
|
|
|
pub output_filenames: Option<&'a OutputFilenames>,
|
|
|
|
|
pub out_dir: Option<&'a Path>,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
pub out_file: Option<&'a Path>,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
pub arena: Option<&'tcx DroplessArena>,
|
|
|
|
|
pub arenas: Option<&'tcx GlobalArenas<'tcx>>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
pub expanded_crate: Option<&'a ast::Crate>,
|
2015-07-31 09:04:06 +02:00
|
|
|
|
pub hir_crate: Option<&'a hir::Crate>,
|
2017-01-26 02:21:50 +01:00
|
|
|
|
pub hir_map: Option<&'a hir_map::Map<'tcx>>,
|
2016-05-05 10:54:18 +02:00
|
|
|
|
pub resolutions: Option<&'a Resolutions>,
|
2016-10-04 01:19:40 +02:00
|
|
|
|
pub analysis: Option<&'a ty::CrateAnalysis>,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
pub trans: Option<&'a trans::CrateTranslation>,
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 12:19:59 +02:00
|
|
|
|
impl<'a, 'tcx> CompileState<'a, 'tcx> {
|
2015-01-11 03:03:34 +01:00
|
|
|
|
fn empty(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
out_dir: &'a Option<PathBuf>)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2015-01-11 03:03:34 +01:00
|
|
|
|
CompileState {
|
|
|
|
|
input: input,
|
|
|
|
|
session: session,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
out_dir: out_dir.as_ref().map(|s| &**s),
|
2016-04-20 06:24:14 +02:00
|
|
|
|
out_file: None,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
arena: None,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
arenas: None,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
krate: None,
|
2016-09-04 05:22:56 +02:00
|
|
|
|
registry: None,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
cstore: None,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
crate_name: None,
|
|
|
|
|
output_filenames: None,
|
|
|
|
|
expanded_crate: None,
|
2015-07-31 09:04:06 +02:00
|
|
|
|
hir_crate: None,
|
2017-01-26 02:21:50 +01:00
|
|
|
|
hir_map: None,
|
2016-05-05 10:54:18 +02:00
|
|
|
|
resolutions: None,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
analysis: None,
|
|
|
|
|
tcx: None,
|
|
|
|
|
trans: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state_after_parse(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
out_dir: &'a Option<PathBuf>,
|
2016-04-20 06:24:14 +02:00
|
|
|
|
out_file: &'a Option<PathBuf>,
|
|
|
|
|
krate: ast::Crate,
|
|
|
|
|
cstore: &'a CStore)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2016-04-20 06:24:14 +02:00
|
|
|
|
CompileState {
|
2016-09-04 05:22:56 +02:00
|
|
|
|
// Initialize the registry before moving `krate`
|
|
|
|
|
registry: Some(Registry::new(&session, krate.span)),
|
2016-04-20 06:24:14 +02:00
|
|
|
|
krate: Some(krate),
|
|
|
|
|
cstore: Some(cstore),
|
|
|
|
|
out_file: out_file.as_ref().map(|s| &**s),
|
|
|
|
|
..CompileState::empty(input, session, out_dir)
|
|
|
|
|
}
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 03:45:54 +02:00
|
|
|
|
fn state_after_expand(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2016-06-28 03:45:54 +02:00
|
|
|
|
out_dir: &'a Option<PathBuf>,
|
|
|
|
|
out_file: &'a Option<PathBuf>,
|
|
|
|
|
cstore: &'a CStore,
|
|
|
|
|
expanded_crate: &'a ast::Crate,
|
|
|
|
|
crate_name: &'a str)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2016-06-28 03:45:54 +02:00
|
|
|
|
CompileState {
|
|
|
|
|
crate_name: Some(crate_name),
|
|
|
|
|
cstore: Some(cstore),
|
|
|
|
|
expanded_crate: Some(expanded_crate),
|
|
|
|
|
out_file: out_file.as_ref().map(|s| &**s),
|
|
|
|
|
..CompileState::empty(input, session, out_dir)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 19:08:09 +02:00
|
|
|
|
fn state_after_hir_lowering(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2016-05-08 19:08:09 +02:00
|
|
|
|
out_dir: &'a Option<PathBuf>,
|
|
|
|
|
out_file: &'a Option<PathBuf>,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
arena: &'tcx DroplessArena,
|
|
|
|
|
arenas: &'tcx GlobalArenas<'tcx>,
|
2016-05-08 19:08:09 +02:00
|
|
|
|
cstore: &'a CStore,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
hir_map: &'a hir_map::Map<'tcx>,
|
2016-10-04 01:19:40 +02:00
|
|
|
|
analysis: &'a ty::CrateAnalysis,
|
2016-05-08 19:08:09 +02:00
|
|
|
|
resolutions: &'a Resolutions,
|
|
|
|
|
krate: &'a ast::Crate,
|
|
|
|
|
hir_crate: &'a hir::Crate,
|
|
|
|
|
crate_name: &'a str)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2015-01-14 21:30:34 +01:00
|
|
|
|
CompileState {
|
|
|
|
|
crate_name: Some(crate_name),
|
2016-12-24 04:48:21 +01:00
|
|
|
|
arena: Some(arena),
|
2016-04-21 00:29:49 +02:00
|
|
|
|
arenas: Some(arenas),
|
|
|
|
|
cstore: Some(cstore),
|
2017-01-26 02:21:50 +01:00
|
|
|
|
hir_map: Some(hir_map),
|
2016-05-05 10:54:18 +02:00
|
|
|
|
analysis: Some(analysis),
|
|
|
|
|
resolutions: Some(resolutions),
|
2016-04-20 06:24:14 +02:00
|
|
|
|
expanded_crate: Some(krate),
|
2015-07-31 09:04:06 +02:00
|
|
|
|
hir_crate: Some(hir_crate),
|
2016-04-21 00:29:49 +02:00
|
|
|
|
out_file: out_file.as_ref().map(|s| &**s),
|
2015-11-10 21:48:44 +01:00
|
|
|
|
..CompileState::empty(input, session, out_dir)
|
2015-01-14 21:30:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-11 03:03:34 +01:00
|
|
|
|
fn state_after_analysis(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
out_dir: &'a Option<PathBuf>,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
out_file: &'a Option<PathBuf>,
|
2015-12-25 17:17:45 +01:00
|
|
|
|
krate: Option<&'a ast::Crate>,
|
2015-07-31 09:04:06 +02:00
|
|
|
|
hir_crate: &'a hir::Crate,
|
2016-10-04 01:19:40 +02:00
|
|
|
|
analysis: &'a ty::CrateAnalysis,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-10-19 21:54:19 +02:00
|
|
|
|
crate_name: &'a str)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2015-01-11 03:03:34 +01:00
|
|
|
|
CompileState {
|
|
|
|
|
analysis: Some(analysis),
|
|
|
|
|
tcx: Some(tcx),
|
2016-04-20 06:24:14 +02:00
|
|
|
|
expanded_crate: krate,
|
2015-07-31 09:04:06 +02:00
|
|
|
|
hir_crate: Some(hir_crate),
|
2015-10-19 21:54:19 +02:00
|
|
|
|
crate_name: Some(crate_name),
|
2016-04-21 00:29:49 +02:00
|
|
|
|
out_file: out_file.as_ref().map(|s| &**s),
|
2015-11-10 21:48:44 +01:00
|
|
|
|
..CompileState::empty(input, session, out_dir)
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn state_after_llvm(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
out_dir: &'a Option<PathBuf>,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
out_file: &'a Option<PathBuf>,
|
2015-01-11 03:03:34 +01:00
|
|
|
|
trans: &'a trans::CrateTranslation)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2016-04-21 00:29:49 +02:00
|
|
|
|
CompileState {
|
|
|
|
|
trans: Some(trans),
|
|
|
|
|
out_file: out_file.as_ref().map(|s| &**s),
|
|
|
|
|
..CompileState::empty(input, session, out_dir)
|
|
|
|
|
}
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
2016-07-01 10:54:37 +02:00
|
|
|
|
|
2016-07-04 08:47:53 +02:00
|
|
|
|
fn state_when_compilation_done(input: &'a Input,
|
2016-10-29 12:19:59 +02:00
|
|
|
|
session: &'tcx Session,
|
2016-07-01 10:54:37 +02:00
|
|
|
|
out_dir: &'a Option<PathBuf>,
|
|
|
|
|
out_file: &'a Option<PathBuf>)
|
2016-10-29 12:19:59 +02:00
|
|
|
|
-> Self {
|
2016-07-01 10:54:37 +02:00
|
|
|
|
CompileState {
|
|
|
|
|
out_file: out_file.as_ref().map(|s| &**s),
|
|
|
|
|
..CompileState::empty(input, session, out_dir)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-11 03:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 08:36:56 +02:00
|
|
|
|
pub fn phase_1_parse_input<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
|
2016-08-02 22:53:58 +02:00
|
|
|
|
let continue_after_error = sess.opts.debugging_opts.continue_parse_after_error;
|
2016-03-25 18:17:04 +01:00
|
|
|
|
sess.diagnostic().set_continue_after_error(continue_after_error);
|
2014-12-02 22:55:07 +01:00
|
|
|
|
|
2016-03-23 04:01:37 +01:00
|
|
|
|
let krate = time(sess.time_passes(), "parsing", || {
|
2013-07-27 08:49:38 +02:00
|
|
|
|
match *input {
|
2014-11-27 13:21:26 +01:00
|
|
|
|
Input::File(ref file) => {
|
2016-10-27 08:36:56 +02:00
|
|
|
|
parse::parse_crate_from_file(file, &sess.parse_sess)
|
2013-07-27 08:49:38 +02:00
|
|
|
|
}
|
2016-03-10 04:49:40 +01:00
|
|
|
|
Input::Str { ref input, ref name } => {
|
2016-10-27 08:36:56 +02:00
|
|
|
|
parse::parse_crate_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
|
2013-07-27 08:49:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-23 04:01:37 +01:00
|
|
|
|
})?;
|
2014-02-19 08:27:49 +01:00
|
|
|
|
|
2016-03-25 18:17:04 +01:00
|
|
|
|
sess.diagnostic().set_continue_after_error(true);
|
|
|
|
|
|
2014-12-09 10:55:49 +01:00
|
|
|
|
if sess.opts.debugging_opts.ast_json_noexpand {
|
2014-12-12 19:59:41 +01:00
|
|
|
|
println!("{}", json::as_json(&krate));
|
2014-02-19 08:27:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 06:26:14 +01:00
|
|
|
|
if sess.opts.debugging_opts.input_stats {
|
|
|
|
|
println!("Lines of code: {}", sess.codemap().count_lines());
|
|
|
|
|
println!("Pre-expansion node count: {}", count_nodes(&krate));
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 05:44:24 +01:00
|
|
|
|
if let Some(ref s) = sess.opts.debugging_opts.show_span {
|
2015-02-02 03:53:25 +01:00
|
|
|
|
syntax::show_span::run(sess.diagnostic(), s, &krate);
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 07:11:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 16:37:39 +01:00
|
|
|
|
if sess.opts.debugging_opts.hir_stats {
|
|
|
|
|
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-13 18:05:16 +01:00
|
|
|
|
Ok(krate)
|
2012-01-17 16:42:45 +01:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
|
2015-11-11 06:26:14 +01:00
|
|
|
|
fn count_nodes(krate: &ast::Crate) -> usize {
|
|
|
|
|
let mut counter = NodeCounter::new();
|
|
|
|
|
visit::walk_crate(&mut counter, krate);
|
|
|
|
|
counter.count
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 02:19:13 +01:00
|
|
|
|
// For continuing compilation after a parsed crate has been
|
|
|
|
|
// modified
|
2013-05-29 00:31:32 +02:00
|
|
|
|
|
2016-10-29 12:19:59 +02:00
|
|
|
|
pub struct ExpansionResult {
|
2016-05-26 01:15:23 +02:00
|
|
|
|
pub expanded_crate: ast::Crate,
|
|
|
|
|
pub defs: hir_map::Definitions,
|
2016-10-04 01:19:40 +02:00
|
|
|
|
pub analysis: ty::CrateAnalysis,
|
2016-05-26 01:15:23 +02:00
|
|
|
|
pub resolutions: Resolutions,
|
|
|
|
|
pub hir_forest: hir_map::Forest,
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 08:49:38 +02:00
|
|
|
|
/// Run the "early phases" of the compiler: initial `cfg` processing,
|
2014-07-21 01:32:46 +02:00
|
|
|
|
/// loading compiler plugins (including those from `addl_plugins`),
|
2013-07-27 08:49:38 +02:00
|
|
|
|
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
|
2016-05-26 01:15:23 +02:00
|
|
|
|
/// harness if one is to be provided, injection of a dependency on the
|
|
|
|
|
/// standard library and prelude, and name resolution.
|
2014-06-10 23:03:19 +02:00
|
|
|
|
///
|
|
|
|
|
/// Returns `None` if we're aborting after handling -W help.
|
2016-10-29 12:19:59 +02:00
|
|
|
|
pub fn phase_2_configure_and_expand<F>(sess: &Session,
|
|
|
|
|
cstore: &CStore,
|
|
|
|
|
krate: ast::Crate,
|
|
|
|
|
registry: Option<Registry>,
|
|
|
|
|
crate_name: &str,
|
|
|
|
|
addl_plugins: Option<Vec<String>>,
|
|
|
|
|
make_glob_map: MakeGlobMap,
|
|
|
|
|
after_expand: F)
|
|
|
|
|
-> Result<ExpansionResult, usize>
|
2016-06-28 03:45:54 +02:00
|
|
|
|
where F: FnOnce(&ast::Crate) -> CompileResult,
|
|
|
|
|
{
|
2012-05-18 06:53:49 +02:00
|
|
|
|
let time_passes = sess.time_passes();
|
2011-12-20 11:17:13 +01:00
|
|
|
|
|
2016-09-01 01:39:16 +02:00
|
|
|
|
let (mut krate, features) = syntax::config::features(krate, &sess.parse_sess, sess.opts.test);
|
|
|
|
|
// these need to be set "early" so that expansion sees `quote` if enabled.
|
|
|
|
|
*sess.features.borrow_mut() = features;
|
2015-04-14 23:25:40 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
*sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
|
2016-11-16 11:52:37 +01:00
|
|
|
|
*sess.crate_disambiguator.borrow_mut() = Symbol::intern(&compute_crate_disambiguator(sess));
|
2015-05-14 09:54:05 +02:00
|
|
|
|
|
2015-06-25 19:07:01 +02:00
|
|
|
|
time(time_passes, "recursion limit", || {
|
2016-11-15 22:25:59 +01:00
|
|
|
|
middle::recursion_limit::update_limits(sess, &krate);
|
2015-05-14 09:54:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
krate = time(time_passes, "crate injection", || {
|
2016-06-05 11:56:05 +02:00
|
|
|
|
let alt_std_name = sess.opts.alt_std_name.clone();
|
2017-03-17 05:04:41 +01:00
|
|
|
|
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
|
2015-11-10 21:48:44 +01:00
|
|
|
|
});
|
2014-11-04 23:59:42 +01:00
|
|
|
|
|
2014-07-20 06:54:37 +02:00
|
|
|
|
let mut addl_plugins = Some(addl_plugins);
|
2015-11-10 21:48:44 +01:00
|
|
|
|
let registrars = time(time_passes, "plugin loading", || {
|
2016-02-26 22:25:25 +01:00
|
|
|
|
plugin::load::load_plugins(sess,
|
|
|
|
|
&cstore,
|
|
|
|
|
&krate,
|
|
|
|
|
crate_name,
|
|
|
|
|
addl_plugins.take().unwrap())
|
2015-11-10 21:48:44 +01:00
|
|
|
|
});
|
2014-05-25 01:16:10 +02:00
|
|
|
|
|
2016-09-04 05:22:56 +02:00
|
|
|
|
let mut registry = registry.unwrap_or(Registry::new(sess, krate.span));
|
2014-05-25 01:16:10 +02:00
|
|
|
|
|
2015-06-25 19:07:01 +02:00
|
|
|
|
time(time_passes, "plugin registration", || {
|
2014-09-11 02:55:42 +02:00
|
|
|
|
if sess.features.borrow().rustc_diagnostic_macros {
|
2014-07-01 18:39:41 +02:00
|
|
|
|
registry.register_macro("__diagnostic_used",
|
2015-11-10 21:48:44 +01:00
|
|
|
|
diagnostics::plugin::expand_diagnostic_used);
|
2014-07-01 18:39:41 +02:00
|
|
|
|
registry.register_macro("__register_diagnostic",
|
2015-11-10 21:48:44 +01:00
|
|
|
|
diagnostics::plugin::expand_register_diagnostic);
|
2014-07-01 18:39:41 +02:00
|
|
|
|
registry.register_macro("__build_diagnostic_array",
|
2015-11-10 21:48:44 +01:00
|
|
|
|
diagnostics::plugin::expand_build_diagnostic_array);
|
2014-07-01 18:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-01 02:03:04 +01:00
|
|
|
|
for registrar in registrars {
|
2015-01-03 03:26:00 +01:00
|
|
|
|
registry.args_hidden = Some(registrar.args);
|
|
|
|
|
(registrar.fun)(&mut registry);
|
2014-04-29 20:38:51 +02:00
|
|
|
|
}
|
2013-12-25 19:10:33 +01:00
|
|
|
|
});
|
2013-05-28 02:45:16 +02:00
|
|
|
|
|
2016-12-22 07:03:19 +01:00
|
|
|
|
let whitelisted_legacy_custom_derives = registry.take_whitelisted_custom_derives();
|
2015-09-15 01:35:25 +02:00
|
|
|
|
let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
|
2017-03-03 18:11:34 +01:00
|
|
|
|
llvm_passes, attributes, .. } = registry;
|
2014-05-25 01:16:10 +02:00
|
|
|
|
|
2016-03-23 04:01:37 +01:00
|
|
|
|
sess.track_errors(|| {
|
2014-06-19 02:26:14 +02:00
|
|
|
|
let mut ls = sess.lint_store.borrow_mut();
|
2015-09-15 01:35:25 +02:00
|
|
|
|
for pass in early_lint_passes {
|
|
|
|
|
ls.register_early_pass(Some(sess), true, pass);
|
|
|
|
|
}
|
|
|
|
|
for pass in late_lint_passes {
|
|
|
|
|
ls.register_late_pass(Some(sess), true, pass);
|
2014-06-19 02:26:14 +02:00
|
|
|
|
}
|
2014-07-21 05:27:59 +02:00
|
|
|
|
|
2015-02-01 02:03:04 +01:00
|
|
|
|
for (name, to) in lint_groups {
|
2014-07-21 05:27:59 +02:00
|
|
|
|
ls.register_group(Some(sess), true, name, to);
|
|
|
|
|
}
|
2015-04-08 21:52:58 +02:00
|
|
|
|
|
|
|
|
|
*sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
|
2015-05-06 18:38:36 +02:00
|
|
|
|
*sess.plugin_attributes.borrow_mut() = attributes.clone();
|
2016-03-23 04:01:37 +01:00
|
|
|
|
})?;
|
2014-06-19 02:26:14 +02:00
|
|
|
|
|
|
|
|
|
// Lint plugins are registered; now we can process command line flags.
|
2014-06-10 23:03:19 +02:00
|
|
|
|
if sess.opts.describe_lints {
|
2016-02-08 23:42:39 +01:00
|
|
|
|
super::describe_lints(&sess.lint_store.borrow(), true);
|
2016-01-21 01:19:20 +01:00
|
|
|
|
return Err(0);
|
2014-06-10 23:03:19 +02:00
|
|
|
|
}
|
2016-03-23 04:01:37 +01:00
|
|
|
|
sess.track_errors(|| sess.lint_store.borrow_mut().process_command_line(sess))?;
|
2014-06-10 23:03:19 +02:00
|
|
|
|
|
2016-09-14 23:03:09 +02:00
|
|
|
|
// Currently, we ignore the name resolution data structures for the purposes of dependency
|
|
|
|
|
// tracking. Instead we will run name resolution and include its output in the hash of each
|
|
|
|
|
// item, much like we do for macro expansion. In other words, the hash reflects not just
|
|
|
|
|
// its contents but the results of name resolution on those contents. Hopefully we'll push
|
|
|
|
|
// this back at some point.
|
|
|
|
|
let _ignore = sess.dep_graph.in_ignore();
|
2016-10-27 08:36:56 +02:00
|
|
|
|
let mut crate_loader = CrateLoader::new(sess, &cstore, crate_name);
|
2016-10-19 11:34:19 +02:00
|
|
|
|
crate_loader.preprocess(&krate);
|
2016-09-05 05:46:05 +02:00
|
|
|
|
let resolver_arenas = Resolver::arenas();
|
2017-04-03 19:20:26 +02:00
|
|
|
|
let mut resolver = Resolver::new(sess,
|
|
|
|
|
&krate,
|
|
|
|
|
crate_name,
|
|
|
|
|
make_glob_map,
|
|
|
|
|
&mut crate_loader,
|
|
|
|
|
&resolver_arenas);
|
2016-12-22 07:03:19 +01:00
|
|
|
|
resolver.whitelisted_legacy_custom_derives = whitelisted_legacy_custom_derives;
|
2016-09-29 00:48:55 +02:00
|
|
|
|
syntax_ext::register_builtins(&mut resolver, syntax_exts, sess.features.borrow().quote);
|
2016-09-05 05:46:05 +02:00
|
|
|
|
|
2015-06-25 19:07:01 +02:00
|
|
|
|
krate = time(time_passes, "expansion", || {
|
|
|
|
|
// Windows dlls do not have rpaths, so they don't know how to find their
|
|
|
|
|
// dependencies. It's up to us to tell the system where to find all the
|
|
|
|
|
// dependent dlls. Note that this uses cfg!(windows) as opposed to
|
|
|
|
|
// targ_cfg because syntax extensions are always loaded for the host
|
|
|
|
|
// compiler, not for the target.
|
2016-06-05 07:30:17 +02:00
|
|
|
|
//
|
|
|
|
|
// This is somewhat of an inherently racy operation, however, as
|
|
|
|
|
// multiple threads calling this function could possibly continue
|
|
|
|
|
// extending PATH far beyond what it should. To solve this for now we
|
|
|
|
|
// just don't add any new elements to PATH which are already there
|
|
|
|
|
// within PATH. This is basically a targeted fix at #17360 for rustdoc
|
|
|
|
|
// which runs rustc in parallel but has been seen (#33844) to cause
|
|
|
|
|
// problems with PATH becoming too long.
|
|
|
|
|
let mut old_path = OsString::new();
|
2015-06-25 19:07:01 +02:00
|
|
|
|
if cfg!(windows) {
|
2016-06-05 07:30:17 +02:00
|
|
|
|
old_path = env::var_os("PATH").unwrap_or(old_path);
|
2015-06-25 19:07:01 +02:00
|
|
|
|
let mut new_path = sess.host_filesearch(PathKind::All)
|
|
|
|
|
.get_dylib_search_paths();
|
2016-06-05 07:30:17 +02:00
|
|
|
|
for path in env::split_paths(&old_path) {
|
|
|
|
|
if !new_path.contains(&path) {
|
|
|
|
|
new_path.push(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-09 23:46:34 +01:00
|
|
|
|
env::set_var("PATH",
|
|
|
|
|
&env::join_paths(new_path.iter()
|
|
|
|
|
.filter(|p| env::join_paths(iter::once(p)).is_ok()))
|
|
|
|
|
.unwrap());
|
2014-05-25 01:16:10 +02:00
|
|
|
|
}
|
2015-06-25 19:07:01 +02:00
|
|
|
|
let features = sess.features.borrow();
|
|
|
|
|
let cfg = syntax::ext::expand::ExpansionConfig {
|
|
|
|
|
features: Some(&features),
|
|
|
|
|
recursion_limit: sess.recursion_limit.get(),
|
|
|
|
|
trace_mac: sess.opts.debugging_opts.trace_macros,
|
2016-06-01 03:27:12 +02:00
|
|
|
|
should_test: sess.opts.test,
|
2016-09-12 11:47:54 +02:00
|
|
|
|
..syntax::ext::expand::ExpansionConfig::default(crate_name.to_string())
|
2015-06-25 19:07:01 +02:00
|
|
|
|
};
|
2017-01-09 10:31:14 +01:00
|
|
|
|
|
2016-10-27 08:36:56 +02:00
|
|
|
|
let mut ecx = ExtCtxt::new(&sess.parse_sess, cfg, &mut resolver);
|
2016-09-29 02:22:46 +02:00
|
|
|
|
let err_count = ecx.parse_sess.span_diagnostic.err_count();
|
|
|
|
|
|
|
|
|
|
let krate = ecx.monotonic_expander().expand_crate(krate);
|
|
|
|
|
|
2017-02-26 04:25:22 +01:00
|
|
|
|
let mut missing_fragment_specifiers: Vec<_> =
|
|
|
|
|
ecx.parse_sess.missing_fragment_specifiers.borrow().iter().cloned().collect();
|
|
|
|
|
missing_fragment_specifiers.sort();
|
|
|
|
|
for span in missing_fragment_specifiers {
|
|
|
|
|
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
|
|
|
|
|
let msg = "missing fragment specifier".to_string();
|
|
|
|
|
sess.add_lint(lint, ast::CRATE_NODE_ID, span, msg);
|
|
|
|
|
}
|
2016-09-29 02:22:46 +02:00
|
|
|
|
if ecx.parse_sess.span_diagnostic.err_count() - ecx.resolve_err_count > err_count {
|
|
|
|
|
ecx.parse_sess.span_diagnostic.abort_if_errors();
|
|
|
|
|
}
|
2015-06-25 19:07:01 +02:00
|
|
|
|
if cfg!(windows) {
|
2016-06-05 07:30:17 +02:00
|
|
|
|
env::set_var("PATH", &old_path);
|
2015-06-25 19:07:01 +02:00
|
|
|
|
}
|
2016-09-29 02:22:46 +02:00
|
|
|
|
krate
|
2015-06-25 19:07:01 +02:00
|
|
|
|
});
|
2014-05-25 01:16:10 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
krate = time(time_passes, "maybe building test harness", || {
|
2016-05-23 17:44:44 +02:00
|
|
|
|
syntax::test::modify_for_testing(&sess.parse_sess,
|
2016-09-06 09:52:09 +02:00
|
|
|
|
&mut resolver,
|
2016-05-23 17:44:44 +02:00
|
|
|
|
sess.opts.test,
|
|
|
|
|
krate,
|
|
|
|
|
sess.diagnostic())
|
2015-11-10 21:48:44 +01:00
|
|
|
|
});
|
2013-07-19 13:51:37 +02:00
|
|
|
|
|
2016-09-30 04:10:29 +02:00
|
|
|
|
// If we're in rustdoc we're always compiling as an rlib, but that'll trip a
|
|
|
|
|
// bunch of checks in the `modify` function below. For now just skip this
|
|
|
|
|
// step entirely if we're rustdoc as it's not too useful anyway.
|
|
|
|
|
if !sess.opts.actually_rustdoc {
|
|
|
|
|
krate = time(time_passes, "maybe creating a macro crate", || {
|
|
|
|
|
let crate_types = sess.crate_types.borrow();
|
|
|
|
|
let num_crate_types = crate_types.len();
|
2016-10-03 18:49:39 +02:00
|
|
|
|
let is_proc_macro_crate = crate_types.contains(&config::CrateTypeProcMacro);
|
2016-11-17 12:36:36 +01:00
|
|
|
|
let is_test_crate = sess.opts.test;
|
2016-10-03 18:49:39 +02:00
|
|
|
|
syntax_ext::proc_macro_registrar::modify(&sess.parse_sess,
|
|
|
|
|
&mut resolver,
|
|
|
|
|
krate,
|
|
|
|
|
is_proc_macro_crate,
|
2016-11-17 12:36:36 +01:00
|
|
|
|
is_test_crate,
|
2016-10-03 18:49:39 +02:00
|
|
|
|
num_crate_types,
|
2017-01-02 01:14:35 +01:00
|
|
|
|
sess.diagnostic())
|
2016-09-30 04:10:29 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
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-23 02:07:11 +02:00
|
|
|
|
|
2017-02-06 12:44:38 +01:00
|
|
|
|
after_expand(&krate)?;
|
|
|
|
|
|
2016-06-22 03:56:39 +02:00
|
|
|
|
if sess.opts.debugging_opts.input_stats {
|
|
|
|
|
println!("Post-expansion node count: {}", count_nodes(&krate));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 16:37:39 +01:00
|
|
|
|
if sess.opts.debugging_opts.hir_stats {
|
|
|
|
|
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS");
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 03:56:39 +02:00
|
|
|
|
if sess.opts.debugging_opts.ast_json {
|
|
|
|
|
println!("{}", json::as_json(&krate));
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"checking for inline asm in case the target doesn't support it",
|
2016-01-21 10:52:37 +01:00
|
|
|
|
|| no_asm::check_crate(sess, &krate));
|
2015-08-21 00:47:21 +02:00
|
|
|
|
|
2017-01-30 03:22:26 +01:00
|
|
|
|
time(time_passes,
|
2016-05-26 01:15:23 +02:00
|
|
|
|
"early lint checks",
|
|
|
|
|
|| lint::check_ast_crate(sess, &krate));
|
|
|
|
|
|
2017-01-30 03:22:26 +01:00
|
|
|
|
time(time_passes,
|
2016-05-26 01:15:23 +02:00
|
|
|
|
"AST validation",
|
|
|
|
|
|| ast_validation::check_crate(sess, &krate));
|
|
|
|
|
|
2017-01-30 03:22:26 +01:00
|
|
|
|
time(time_passes, "name resolution", || -> CompileResult {
|
2016-06-22 03:54:34 +02:00
|
|
|
|
resolver.resolve_crate(&krate);
|
2016-06-28 03:45:54 +02:00
|
|
|
|
Ok(())
|
|
|
|
|
})?;
|
2016-06-22 03:54:34 +02:00
|
|
|
|
|
2017-02-06 12:44:38 +01:00
|
|
|
|
if resolver.found_unresolved_macro {
|
|
|
|
|
sess.parse_sess.span_diagnostic.abort_if_errors();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 07:58:17 +01:00
|
|
|
|
// Needs to go *after* expansion to be able to check the results of macro expansion.
|
|
|
|
|
time(time_passes, "complete gated feature checking", || {
|
|
|
|
|
sess.track_errors(|| {
|
|
|
|
|
syntax::feature_gate::check_crate(&krate,
|
|
|
|
|
&sess.parse_sess,
|
|
|
|
|
&sess.features.borrow(),
|
|
|
|
|
&attributes,
|
|
|
|
|
sess.opts.unstable_features);
|
|
|
|
|
})
|
|
|
|
|
})?;
|
|
|
|
|
|
2016-06-22 03:54:34 +02:00
|
|
|
|
// Lower ast -> hir.
|
2017-01-30 03:22:26 +01:00
|
|
|
|
let hir_forest = time(time_passes, "lowering ast -> hir", || {
|
2016-11-04 16:37:39 +01:00
|
|
|
|
let hir_crate = lower_crate(sess, &krate, &mut resolver);
|
|
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.hir_stats {
|
|
|
|
|
hir_stats::print_hir_stats(&hir_crate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hir_map::Forest::new(hir_crate, &sess.dep_graph)
|
2016-06-22 03:54:34 +02:00
|
|
|
|
});
|
2016-05-26 01:15:23 +02:00
|
|
|
|
|
2017-01-30 03:12:10 +01:00
|
|
|
|
// Discard hygiene data, which isn't required after lowering to HIR.
|
2016-07-16 21:11:28 +02:00
|
|
|
|
if !keep_hygiene_data(sess) {
|
2017-03-17 05:04:41 +01:00
|
|
|
|
syntax::ext::hygiene::clear_markings();
|
2016-05-26 01:15:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(ExpansionResult {
|
|
|
|
|
expanded_crate: krate,
|
2016-06-22 03:54:34 +02:00
|
|
|
|
defs: resolver.definitions,
|
|
|
|
|
analysis: ty::CrateAnalysis {
|
2017-03-23 20:13:29 +01:00
|
|
|
|
access_levels: Rc::new(AccessLevels::default()),
|
2017-04-14 21:33:52 +02:00
|
|
|
|
reachable: Rc::new(NodeSet()),
|
2016-10-29 12:19:59 +02:00
|
|
|
|
name: crate_name.to_string(),
|
2016-06-22 03:54:34 +02:00
|
|
|
|
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
|
|
|
|
|
},
|
|
|
|
|
resolutions: Resolutions {
|
|
|
|
|
freevars: resolver.freevars,
|
2017-03-23 19:18:25 +01:00
|
|
|
|
export_map: resolver.export_map,
|
2016-06-22 03:54:34 +02:00
|
|
|
|
trait_map: resolver.trait_map,
|
|
|
|
|
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
|
|
|
|
|
},
|
2017-03-23 19:18:25 +01:00
|
|
|
|
hir_forest: hir_forest,
|
2016-05-26 01:15:23 +02:00
|
|
|
|
})
|
2014-09-07 19:09:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 08:49:38 +02:00
|
|
|
|
/// Run the resolution, typechecking, region checking and other
|
|
|
|
|
/// miscellaneous analysis passes on the crate. Return various
|
|
|
|
|
/// structures carrying the results of the analysis.
|
2015-09-28 04:00:15 +02:00
|
|
|
|
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
|
2015-12-24 21:30:27 +01:00
|
|
|
|
hir_map: hir_map::Map<'tcx>,
|
2016-10-04 01:19:40 +02:00
|
|
|
|
mut analysis: ty::CrateAnalysis,
|
2016-05-04 08:00:27 +02:00
|
|
|
|
resolutions: Resolutions,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
arena: &'tcx DroplessArena,
|
|
|
|
|
arenas: &'tcx GlobalArenas<'tcx>,
|
2015-10-19 21:54:19 +02:00
|
|
|
|
name: &str,
|
2015-06-14 03:50:23 +02:00
|
|
|
|
f: F)
|
2016-01-21 01:19:20 +01:00
|
|
|
|
-> Result<R, usize>
|
2016-05-03 04:23:22 +02:00
|
|
|
|
where F: for<'a> FnOnce(TyCtxt<'a, 'tcx, 'tcx>,
|
2016-10-04 01:19:40 +02:00
|
|
|
|
ty::CrateAnalysis,
|
2016-08-23 13:47:14 +02:00
|
|
|
|
IncrementalHashesMap,
|
2016-05-03 03:56:42 +02:00
|
|
|
|
CompileResult) -> R
|
2015-06-14 03:50:23 +02:00
|
|
|
|
{
|
2016-01-27 21:49:18 +01:00
|
|
|
|
macro_rules! try_with_f {
|
2016-10-28 12:55:49 +02:00
|
|
|
|
($e: expr, ($t: expr, $a: expr, $h: expr)) => {
|
2016-01-27 21:49:18 +01:00
|
|
|
|
match $e {
|
|
|
|
|
Ok(x) => x,
|
|
|
|
|
Err(x) => {
|
2016-10-28 12:55:49 +02:00
|
|
|
|
f($t, $a, $h, Err(x));
|
2016-01-27 21:49:18 +01:00
|
|
|
|
return Err(x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 08:49:38 +02:00
|
|
|
|
let time_passes = sess.time_passes();
|
2013-09-07 04:11:55 +02:00
|
|
|
|
|
2016-04-07 08:16:12 +02:00
|
|
|
|
let lang_items = time(time_passes, "language item collection", || {
|
|
|
|
|
sess.track_errors(|| {
|
|
|
|
|
middle::lang_items::collect_language_items(&sess, &hir_map)
|
|
|
|
|
})
|
|
|
|
|
})?;
|
|
|
|
|
|
2016-03-23 04:01:37 +01:00
|
|
|
|
let named_region_map = time(time_passes,
|
2016-03-22 23:58:45 +01:00
|
|
|
|
"lifetime resolution",
|
2016-11-25 12:21:19 +01:00
|
|
|
|
|| middle::resolve_lifetime::krate(sess, &hir_map))?;
|
2013-10-28 22:37:10 +01:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"looking for entry point",
|
2015-12-24 21:30:27 +01:00
|
|
|
|
|| middle::entry::find_entry_point(sess, &hir_map));
|
2012-07-01 01:19:07 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
sess.plugin_registrar_fn.set(time(time_passes, "looking for plugin registrar", || {
|
2016-01-29 21:04:07 +01:00
|
|
|
|
plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map)
|
2015-11-10 21:48:44 +01:00
|
|
|
|
}));
|
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-23 02:07:11 +02:00
|
|
|
|
sess.derive_registrar_fn.set(derive_registrar::find(&hir_map));
|
2013-12-25 19:10:33 +01:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
let region_map = time(time_passes,
|
|
|
|
|
"region resolution",
|
2016-01-29 21:04:07 +01:00
|
|
|
|
|| middle::region::resolve_crate(sess, &hir_map));
|
2012-01-17 16:42:45 +01:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"loop checking",
|
2016-11-25 12:21:19 +01:00
|
|
|
|
|| loops::check_crate(sess, &hir_map));
|
2014-04-05 01:05:31 +02:00
|
|
|
|
|
2016-03-23 04:01:37 +01:00
|
|
|
|
time(time_passes,
|
2016-01-27 07:01:01 +01:00
|
|
|
|
"static item recursion checking",
|
2016-11-25 12:21:19 +01:00
|
|
|
|
|| static_recursion::check_crate(sess, &hir_map))?;
|
2016-01-29 21:04:07 +01:00
|
|
|
|
|
|
|
|
|
let index = stability::Index::new(&hir_map);
|
2014-09-15 02:21:25 +02:00
|
|
|
|
|
2017-02-08 10:16:14 +01:00
|
|
|
|
let mut local_providers = ty::maps::Providers::default();
|
2017-04-17 15:08:34 +02:00
|
|
|
|
borrowck::provide(&mut local_providers);
|
2017-02-20 02:55:28 +01:00
|
|
|
|
mir::provide(&mut local_providers);
|
2017-04-17 15:08:34 +02:00
|
|
|
|
reachable::provide(&mut local_providers);
|
2017-03-23 20:13:29 +01:00
|
|
|
|
rustc_privacy::provide(&mut local_providers);
|
2017-04-24 18:35:47 +02:00
|
|
|
|
trans::provide(&mut local_providers);
|
2017-02-14 00:11:24 +01:00
|
|
|
|
typeck::provide(&mut local_providers);
|
2017-03-20 10:37:52 +01:00
|
|
|
|
ty::provide(&mut local_providers);
|
2017-03-28 00:55:56 +02:00
|
|
|
|
reachable::provide(&mut local_providers);
|
2017-04-14 18:00:08 +02:00
|
|
|
|
rustc_const_eval::provide(&mut local_providers);
|
2016-09-29 01:30:53 +02:00
|
|
|
|
|
2017-02-08 10:16:14 +01:00
|
|
|
|
let mut extern_providers = ty::maps::Providers::default();
|
2016-09-29 01:30:53 +02:00
|
|
|
|
cstore::provide(&mut extern_providers);
|
2017-04-24 18:35:47 +02:00
|
|
|
|
trans::provide(&mut extern_providers);
|
2017-04-15 14:40:38 +02:00
|
|
|
|
ty::provide_extern(&mut extern_providers);
|
2017-04-15 03:14:44 +02:00
|
|
|
|
// FIXME(eddyb) get rid of this once we replace const_eval with miri.
|
|
|
|
|
rustc_const_eval::provide(&mut extern_providers);
|
2016-09-29 01:30:53 +02:00
|
|
|
|
|
2016-03-01 00:36:51 +01:00
|
|
|
|
TyCtxt::create_and_enter(sess,
|
2016-09-29 01:30:53 +02:00
|
|
|
|
local_providers,
|
|
|
|
|
extern_providers,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
arenas,
|
2016-12-24 04:48:21 +01:00
|
|
|
|
arena,
|
2016-12-15 12:13:24 +01:00
|
|
|
|
resolutions,
|
2016-04-21 00:29:49 +02:00
|
|
|
|
named_region_map,
|
|
|
|
|
hir_map,
|
|
|
|
|
region_map,
|
|
|
|
|
lang_items,
|
|
|
|
|
index,
|
|
|
|
|
name,
|
|
|
|
|
|tcx| {
|
2016-08-23 13:47:14 +02:00
|
|
|
|
let incremental_hashes_map =
|
2016-08-19 13:23:36 +02:00
|
|
|
|
time(time_passes,
|
2016-08-23 13:47:14 +02:00
|
|
|
|
"compute_incremental_hashes_map",
|
|
|
|
|
|| rustc_incremental::compute_incremental_hashes_map(tcx));
|
2016-03-28 23:42:39 +02:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"load_dep_graph",
|
2016-08-23 13:47:14 +02:00
|
|
|
|
|| rustc_incremental::load_dep_graph(tcx, &incremental_hashes_map));
|
2016-03-28 23:42:39 +02:00
|
|
|
|
|
2016-11-10 18:08:21 +01:00
|
|
|
|
time(time_passes, "stability index", || {
|
|
|
|
|
tcx.stability.borrow_mut().build(tcx)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"stability checking",
|
|
|
|
|
|| stability::check_unstable_api_usage(tcx));
|
|
|
|
|
|
2016-01-21 01:19:20 +01:00
|
|
|
|
// passes are timed inside typeck
|
2016-10-04 01:19:40 +02:00
|
|
|
|
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"const checking",
|
2016-01-26 06:28:31 +01:00
|
|
|
|
|| consts::check_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2016-01-27 21:49:18 +01:00
|
|
|
|
analysis.access_levels =
|
2017-03-23 20:13:29 +01:00
|
|
|
|
time(time_passes, "privacy checking", || rustc_privacy::check_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"intrinsic checking",
|
|
|
|
|
|| middle::intrinsicck::check_crate(tcx));
|
|
|
|
|
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"effect checking",
|
|
|
|
|
|| middle::effect::check_crate(tcx));
|
|
|
|
|
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"match checking",
|
2016-03-30 13:43:36 +02:00
|
|
|
|
|| check_match::check_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2016-02-11 14:38:52 +01:00
|
|
|
|
// this must run before MIR dump, because
|
|
|
|
|
// "not all control paths return a value" is reported here.
|
|
|
|
|
//
|
|
|
|
|
// maybe move the check to a MIR pass?
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"liveness checking",
|
|
|
|
|
|| middle::liveness::check_crate(tcx));
|
|
|
|
|
|
2016-10-28 12:55:49 +02:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"MIR dump",
|
|
|
|
|
|| mir::mir_map::build_mir_for_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2016-11-30 18:14:17 +01:00
|
|
|
|
if sess.opts.debugging_opts.mir_stats {
|
|
|
|
|
mir_stats::print_mir_stats(tcx, "PRE CLEANUP MIR STATS");
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 03:59:50 +02:00
|
|
|
|
time(time_passes, "MIR cleanup and validation", || {
|
2016-02-26 17:05:50 +01:00
|
|
|
|
let mut passes = sess.mir_passes.borrow_mut();
|
2016-08-16 03:59:50 +02:00
|
|
|
|
// Push all the built-in validation passes.
|
|
|
|
|
// NB: if you’re adding an *optimisation* it ought to go to another set of passes
|
|
|
|
|
// in stage 4 below.
|
2016-06-08 20:03:06 +02:00
|
|
|
|
passes.push_hook(box mir::transform::dump_mir::DumpMir);
|
2016-08-16 03:59:50 +02:00
|
|
|
|
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("initial"));
|
2016-02-26 17:05:50 +01:00
|
|
|
|
passes.push_pass(box mir::transform::type_check::TypeckMir);
|
2017-04-11 22:53:20 +02:00
|
|
|
|
passes.push_pass(box mir::transform::qualify_consts::QualifyAndPromoteConstants);
|
2016-06-09 16:23:23 +02:00
|
|
|
|
passes.push_pass(
|
|
|
|
|
box mir::transform::simplify_branches::SimplifyBranches::new("initial"));
|
2016-08-16 03:59:50 +02:00
|
|
|
|
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("qualify-consts"));
|
2016-02-26 17:05:50 +01:00
|
|
|
|
// And run everything.
|
2016-10-28 12:55:49 +02:00
|
|
|
|
passes.run_passes(tcx);
|
2016-02-26 17:05:50 +01:00
|
|
|
|
});
|
2016-02-05 09:35:54 +01:00
|
|
|
|
|
2016-01-21 01:19:20 +01:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"borrow checking",
|
2016-10-28 12:55:49 +02:00
|
|
|
|
|| borrowck::check_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
|
|
|
|
// Avoid overwhelming user with errors if type checking failed.
|
|
|
|
|
// I'm not sure how helpful this is, to be honest, but it avoids
|
|
|
|
|
// a
|
|
|
|
|
// lot of annoying errors in the compile-fail tests (basically,
|
|
|
|
|
// lint warnings and so on -- kindck used to do this abort, but
|
|
|
|
|
// kindck is gone now). -nmatsakis
|
2016-01-27 21:49:18 +01:00
|
|
|
|
if sess.err_count() > 0 {
|
2016-10-28 12:55:49 +02:00
|
|
|
|
return Ok(f(tcx, analysis, incremental_hashes_map, Err(sess.err_count())));
|
2016-01-27 21:49:18 +01:00
|
|
|
|
}
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2016-01-27 21:49:18 +01:00
|
|
|
|
analysis.reachable =
|
2016-01-21 01:19:20 +01:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"reachability checking",
|
2017-03-23 20:13:29 +01:00
|
|
|
|
|| reachable::find_reachable(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
2017-03-23 20:13:29 +01:00
|
|
|
|
time(time_passes, "death checking", || middle::dead::check_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
|
|
|
|
time(time_passes, "unused lib feature checking", || {
|
2017-03-23 20:13:29 +01:00
|
|
|
|
stability::check_unused_or_stable_features(tcx)
|
2016-01-21 01:19:20 +01:00
|
|
|
|
});
|
|
|
|
|
|
2017-03-23 20:13:29 +01:00
|
|
|
|
time(time_passes, "lint checking", || lint::check_crate(tcx));
|
2016-01-21 01:19:20 +01:00
|
|
|
|
|
|
|
|
|
// The above three passes generate errors w/o aborting
|
2016-01-27 21:49:18 +01:00
|
|
|
|
if sess.err_count() > 0 {
|
2016-10-28 12:55:49 +02:00
|
|
|
|
return Ok(f(tcx, analysis, incremental_hashes_map, Err(sess.err_count())));
|
2016-01-27 21:49:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 12:55:49 +02:00
|
|
|
|
Ok(f(tcx, analysis, incremental_hashes_map, Ok(())))
|
2016-01-21 01:19:20 +01:00
|
|
|
|
})
|
2013-07-27 08:49:38 +02:00
|
|
|
|
}
|
2013-06-14 07:38:17 +02:00
|
|
|
|
|
2013-07-27 08:49:38 +02:00
|
|
|
|
/// Run the translation phase to LLVM, after which the AST and analysis can
|
2017-02-09 18:07:58 +01:00
|
|
|
|
/// be discarded.
|
2016-05-03 04:23:22 +02:00
|
|
|
|
pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-08-19 13:23:36 +02:00
|
|
|
|
analysis: ty::CrateAnalysis,
|
2017-02-13 10:57:50 +01:00
|
|
|
|
incremental_hashes_map: &IncrementalHashesMap,
|
|
|
|
|
output_filenames: &OutputFilenames)
|
2016-05-03 03:56:42 +02:00
|
|
|
|
-> trans::CrateTranslation {
|
2015-06-14 00:49:28 +02:00
|
|
|
|
let time_passes = tcx.sess.time_passes();
|
2014-05-02 09:59:27 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"resolving dependency formats",
|
|
|
|
|
|| dependency_format::calculate(&tcx.sess));
|
2014-05-02 09:59:27 +02:00
|
|
|
|
|
2016-11-30 18:14:17 +01:00
|
|
|
|
if tcx.sess.opts.debugging_opts.mir_stats {
|
|
|
|
|
mir_stats::print_mir_stats(tcx, "PRE OPTIMISATION MIR STATS");
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 03:59:50 +02:00
|
|
|
|
// Run the passes that transform the MIR into a more suitable form for translation to LLVM
|
|
|
|
|
// code.
|
|
|
|
|
time(time_passes, "MIR optimisations", || {
|
2016-03-30 03:46:02 +02:00
|
|
|
|
let mut passes = ::rustc::mir::transform::Passes::new();
|
2016-06-08 20:03:06 +02:00
|
|
|
|
passes.push_hook(box mir::transform::dump_mir::DumpMir);
|
2016-03-30 03:46:02 +02:00
|
|
|
|
passes.push_pass(box mir::transform::no_landing_pads::NoLandingPads);
|
2016-08-16 03:59:50 +02:00
|
|
|
|
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("no-landing-pads"));
|
2016-06-06 21:58:28 +02:00
|
|
|
|
|
2016-09-10 23:33:29 +02:00
|
|
|
|
// From here on out, regions are gone.
|
2016-03-30 03:46:02 +02:00
|
|
|
|
passes.push_pass(box mir::transform::erase_regions::EraseRegions);
|
2016-06-06 21:58:28 +02:00
|
|
|
|
|
2016-06-05 08:00:17 +02:00
|
|
|
|
passes.push_pass(box mir::transform::add_call_guards::AddCallGuards);
|
2016-05-17 01:26:18 +02:00
|
|
|
|
passes.push_pass(box borrowck::ElaborateDrops);
|
|
|
|
|
passes.push_pass(box mir::transform::no_landing_pads::NoLandingPads);
|
2016-08-16 03:59:50 +02:00
|
|
|
|
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("elaborate-drops"));
|
2016-06-06 21:58:28 +02:00
|
|
|
|
|
2016-09-10 23:33:29 +02:00
|
|
|
|
// No lifetime analysis based on borrowing can be done from here on out.
|
2017-02-08 10:24:49 +01:00
|
|
|
|
passes.push_pass(box mir::transform::inline::Inline);
|
2016-09-10 23:33:29 +02:00
|
|
|
|
passes.push_pass(box mir::transform::instcombine::InstCombine::new());
|
2016-07-28 02:46:54 +02:00
|
|
|
|
passes.push_pass(box mir::transform::deaggregator::Deaggregator);
|
2016-09-16 03:18:40 +02:00
|
|
|
|
passes.push_pass(box mir::transform::copy_prop::CopyPropagation);
|
2016-07-28 02:46:54 +02:00
|
|
|
|
|
2016-08-16 03:59:50 +02:00
|
|
|
|
passes.push_pass(box mir::transform::simplify::SimplifyLocals);
|
2016-06-05 08:00:17 +02:00
|
|
|
|
passes.push_pass(box mir::transform::add_call_guards::AddCallGuards);
|
2016-06-08 23:16:35 +02:00
|
|
|
|
passes.push_pass(box mir::transform::dump_mir::Marker("PreTrans"));
|
2016-06-06 21:58:28 +02:00
|
|
|
|
|
2016-10-28 12:55:49 +02:00
|
|
|
|
passes.run_passes(tcx);
|
2016-03-30 03:46:02 +02:00
|
|
|
|
});
|
2016-03-11 06:00:52 +01:00
|
|
|
|
|
2016-11-30 18:14:17 +01:00
|
|
|
|
if tcx.sess.opts.debugging_opts.mir_stats {
|
|
|
|
|
mir_stats::print_mir_stats(tcx, "POST OPTIMISATION MIR STATS");
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 23:36:56 +02:00
|
|
|
|
let translation =
|
|
|
|
|
time(time_passes,
|
|
|
|
|
"translation",
|
2017-02-13 10:57:50 +01:00
|
|
|
|
move || trans::trans_crate(tcx, analysis, &incremental_hashes_map, output_filenames));
|
2016-03-28 23:36:56 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(time_passes,
|
2016-03-28 23:36:56 +02:00
|
|
|
|
"assert dep graph",
|
2016-08-12 01:02:39 +02:00
|
|
|
|
|| rustc_incremental::assert_dep_graph(tcx));
|
2016-03-28 23:36:56 +02:00
|
|
|
|
|
2016-03-28 23:42:39 +02:00
|
|
|
|
time(time_passes,
|
|
|
|
|
"serialize dep graph",
|
2016-08-12 01:02:39 +02:00
|
|
|
|
|| rustc_incremental::save_dep_graph(tcx,
|
|
|
|
|
&incremental_hashes_map,
|
2017-04-05 23:39:02 +02:00
|
|
|
|
&translation.metadata.hashes,
|
2016-08-12 01:02:39 +02:00
|
|
|
|
translation.link.crate_hash));
|
2016-03-28 23:36:56 +02:00
|
|
|
|
translation
|
2013-07-27 08:49:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Run LLVM itself, producing a bitcode file, assembly file or object file
|
|
|
|
|
/// as a side effect.
|
2014-03-05 15:36:01 +01:00
|
|
|
|
pub fn phase_5_run_llvm_passes(sess: &Session,
|
2014-11-27 13:21:26 +01:00
|
|
|
|
trans: &trans::CrateTranslation,
|
2016-01-21 01:19:20 +01:00
|
|
|
|
outputs: &OutputFilenames) -> CompileResult {
|
2016-11-14 09:37:46 +01:00
|
|
|
|
if sess.opts.cg.no_integrated_as ||
|
|
|
|
|
(sess.target.target.options.no_integrated_as &&
|
|
|
|
|
(outputs.outputs.contains_key(&OutputType::Object) ||
|
|
|
|
|
outputs.outputs.contains_key(&OutputType::Exe)))
|
|
|
|
|
{
|
2016-08-02 22:53:58 +02:00
|
|
|
|
let output_types = OutputTypes::new(&[(OutputType::Assembly, None)]);
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(sess.time_passes(),
|
|
|
|
|
"LLVM passes",
|
2016-08-02 22:53:58 +02:00
|
|
|
|
|| write::run_passes(sess, trans, &output_types, outputs));
|
2013-03-15 09:32:39 +01:00
|
|
|
|
|
2014-08-11 19:33:58 +02:00
|
|
|
|
write::run_assembler(sess, outputs);
|
2013-08-20 00:21:30 +02:00
|
|
|
|
|
2016-11-12 23:30:06 +01:00
|
|
|
|
// HACK the linker expects the object file to be named foo.0.o but
|
|
|
|
|
// `run_assembler` produces an object named just foo.o. Rename it if we
|
|
|
|
|
// are going to build an executable
|
|
|
|
|
if sess.opts.output_types.contains_key(&OutputType::Exe) {
|
|
|
|
|
let f = outputs.path(OutputType::Object);
|
2017-02-03 10:42:01 +01:00
|
|
|
|
rename_or_copy_remove(&f,
|
2016-11-12 23:30:06 +01:00
|
|
|
|
f.with_file_name(format!("{}.0.o",
|
|
|
|
|
f.file_stem().unwrap().to_string_lossy()))).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 05:13:10 +01:00
|
|
|
|
// Remove assembly source, unless --save-temps was specified
|
2014-02-07 04:57:09 +01:00
|
|
|
|
if !sess.opts.cg.save_temps {
|
2016-05-14 02:48:32 +02:00
|
|
|
|
fs::remove_file(&outputs.temp_path(OutputType::Assembly, None)).unwrap();
|
2013-08-20 00:21:30 +02:00
|
|
|
|
}
|
2013-03-15 09:32:39 +01:00
|
|
|
|
} else {
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(sess.time_passes(),
|
|
|
|
|
"LLVM passes",
|
|
|
|
|
|| write::run_passes(sess, trans, &sess.opts.output_types, outputs));
|
2013-03-15 09:32:39 +01:00
|
|
|
|
}
|
2014-09-27 10:33:36 +02:00
|
|
|
|
|
2016-07-21 18:44:59 +02:00
|
|
|
|
time(sess.time_passes(),
|
|
|
|
|
"serialize work products",
|
2016-08-12 01:02:39 +02:00
|
|
|
|
move || rustc_incremental::save_work_products(sess));
|
2016-07-21 18:44:59 +02:00
|
|
|
|
|
2016-01-27 21:49:18 +01:00
|
|
|
|
if sess.err_count() > 0 {
|
|
|
|
|
Err(sess.err_count())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2013-07-27 08:49:38 +02:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
|
2013-07-27 08:49:38 +02:00
|
|
|
|
/// Run the linker on any artifacts that resulted from the LLVM run.
|
|
|
|
|
/// This should produce either a finished executable or library.
|
2014-03-05 15:36:01 +01:00
|
|
|
|
pub fn phase_6_link_output(sess: &Session,
|
2014-11-27 13:21:26 +01:00
|
|
|
|
trans: &trans::CrateTranslation,
|
2013-07-27 08:49:38 +02:00
|
|
|
|
outputs: &OutputFilenames) {
|
2015-11-10 21:48:44 +01:00
|
|
|
|
time(sess.time_passes(),
|
|
|
|
|
"linking",
|
2017-04-13 20:58:20 +02:00
|
|
|
|
|| link::link_binary(sess, trans, outputs, &trans.crate_name.as_str()));
|
2013-12-26 20:55:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-01 13:47:38 +02:00
|
|
|
|
fn escape_dep_filename(filename: &str) -> String {
|
|
|
|
|
// Apparently clang and gcc *only* escape spaces:
|
|
|
|
|
// http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
|
2015-11-10 21:49:12 +01:00
|
|
|
|
filename.replace(" ", "\\ ")
|
2014-10-01 13:47:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-22 16:39:30 +02:00
|
|
|
|
fn write_out_deps(sess: &Session, outputs: &OutputFilenames, crate_name: &str) {
|
2014-03-04 19:02:49 +01:00
|
|
|
|
let mut out_filenames = Vec::new();
|
2015-09-30 19:08:37 +02:00
|
|
|
|
for output_type in sess.opts.output_types.keys() {
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
let file = outputs.path(*output_type);
|
|
|
|
|
match *output_type {
|
2015-09-30 19:08:37 +02:00
|
|
|
|
OutputType::Exe => {
|
2015-06-11 14:56:07 +02:00
|
|
|
|
for output in sess.crate_types.borrow().iter() {
|
2016-07-22 16:39:30 +02:00
|
|
|
|
let p = link::filename_for_input(sess, *output, crate_name, outputs);
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
out_filenames.push(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 21:48:44 +01:00
|
|
|
|
_ => {
|
|
|
|
|
out_filenames.push(file);
|
|
|
|
|
}
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-28 04:06:35 +01:00
|
|
|
|
|
2015-09-30 19:08:37 +02:00
|
|
|
|
// Write out dependency rules to the dep-info file if requested
|
|
|
|
|
if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
|
2015-11-10 21:48:44 +01:00
|
|
|
|
return;
|
2015-09-30 19:08:37 +02:00
|
|
|
|
}
|
|
|
|
|
let deps_filename = outputs.path(OutputType::DepInfo);
|
2013-12-26 20:55:10 +01:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
let result =
|
|
|
|
|
(|| -> io::Result<()> {
|
|
|
|
|
// Build a list of files used to compile the output and
|
|
|
|
|
// write Makefile-compatible dependency rules
|
|
|
|
|
let files: Vec<String> = sess.codemap()
|
|
|
|
|
.files
|
|
|
|
|
.borrow()
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|fmap| fmap.is_real_file())
|
|
|
|
|
.filter(|fmap| !fmap.is_imported())
|
|
|
|
|
.map(|fmap| escape_dep_filename(&fmap.name))
|
|
|
|
|
.collect();
|
2016-03-23 04:01:37 +01:00
|
|
|
|
let mut file = fs::File::create(&deps_filename)?;
|
2015-11-10 21:48:44 +01:00
|
|
|
|
for path in &out_filenames {
|
2016-03-23 04:01:37 +01:00
|
|
|
|
write!(file, "{}: {}\n\n", path.display(), files.join(" "))?;
|
2015-11-10 21:48:44 +01:00
|
|
|
|
}
|
2015-09-30 19:17:07 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
// Emit a fake target for each input file to the compilation. This
|
|
|
|
|
// prevents `make` from spitting out an error if a file is later
|
|
|
|
|
// deleted. For more info see #28735
|
|
|
|
|
for path in files {
|
2016-03-23 04:01:37 +01:00
|
|
|
|
writeln!(file, "{}:", path)?;
|
2015-11-10 21:48:44 +01:00
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
})();
|
2014-04-15 16:35:17 +02:00
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(e) => {
|
2015-01-07 17:58:31 +01:00
|
|
|
|
sess.fatal(&format!("error writing dependencies to `{}`: {}",
|
2015-11-10 21:48:44 +01:00
|
|
|
|
deps_filename.display(),
|
|
|
|
|
e));
|
2014-04-15 16:35:17 +02:00
|
|
|
|
}
|
2013-07-27 08:49:38 +02:00
|
|
|
|
}
|
2013-01-23 02:19:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
|
2014-05-23 07:29:13 +02:00
|
|
|
|
// Unconditionally collect crate types from attributes to make them used
|
2015-11-10 21:48:44 +01:00
|
|
|
|
let attr_types: Vec<config::CrateType> =
|
|
|
|
|
attrs.iter()
|
|
|
|
|
.filter_map(|a| {
|
|
|
|
|
if a.check_name("crate_type") {
|
|
|
|
|
match a.value_str() {
|
|
|
|
|
Some(ref n) if *n == "rlib" => {
|
|
|
|
|
Some(config::CrateTypeRlib)
|
|
|
|
|
}
|
|
|
|
|
Some(ref n) if *n == "dylib" => {
|
|
|
|
|
Some(config::CrateTypeDylib)
|
|
|
|
|
}
|
2016-05-10 23:17:57 +02:00
|
|
|
|
Some(ref n) if *n == "cdylib" => {
|
|
|
|
|
Some(config::CrateTypeCdylib)
|
|
|
|
|
}
|
2015-11-10 21:48:44 +01:00
|
|
|
|
Some(ref n) if *n == "lib" => {
|
|
|
|
|
Some(config::default_lib_output())
|
|
|
|
|
}
|
|
|
|
|
Some(ref n) if *n == "staticlib" => {
|
|
|
|
|
Some(config::CrateTypeStaticlib)
|
|
|
|
|
}
|
2016-10-03 18:49:39 +02:00
|
|
|
|
Some(ref n) if *n == "proc-macro" => {
|
|
|
|
|
Some(config::CrateTypeProcMacro)
|
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-23 02:07:11 +02:00
|
|
|
|
}
|
2015-11-10 21:48:44 +01:00
|
|
|
|
Some(ref n) if *n == "bin" => Some(config::CrateTypeExecutable),
|
|
|
|
|
Some(_) => {
|
|
|
|
|
session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
|
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
|
a.span,
|
|
|
|
|
"invalid `crate_type` value".to_string());
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2015-12-20 22:00:43 +01:00
|
|
|
|
session.struct_span_err(a.span, "`crate_type` requires a value")
|
|
|
|
|
.note("for example: `#![crate_type=\"lib\"]`")
|
|
|
|
|
.emit();
|
2015-11-10 21:48:44 +01:00
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2014-05-23 07:29:13 +02:00
|
|
|
|
|
2014-05-06 13:38:01 +02:00
|
|
|
|
// If we're generating a test executable, then ignore all other output
|
|
|
|
|
// styles at all other locations
|
|
|
|
|
if session.opts.test {
|
2015-11-10 21:48:44 +01:00
|
|
|
|
return vec![config::CrateTypeExecutable];
|
2013-06-15 09:43:19 +02:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
|
2014-05-06 13:38:01 +02:00
|
|
|
|
// Only check command line flags if present. If no types are specified by
|
|
|
|
|
// command line, then reuse the empty `base` Vec to hold the types that
|
|
|
|
|
// will be found in crate attributes.
|
|
|
|
|
let mut base = session.opts.crate_types.clone();
|
2015-03-25 00:53:34 +01:00
|
|
|
|
if base.is_empty() {
|
2015-06-10 18:22:20 +02:00
|
|
|
|
base.extend(attr_types);
|
2015-03-25 00:53:34 +01:00
|
|
|
|
if base.is_empty() {
|
2014-06-11 09:48:17 +02:00
|
|
|
|
base.push(link::default_output_for_target(session));
|
2014-05-01 01:49:12 +02:00
|
|
|
|
}
|
2014-11-27 22:49:02 +01:00
|
|
|
|
base.sort();
|
2014-05-06 13:38:01 +02:00
|
|
|
|
base.dedup();
|
2012-03-12 18:19:35 +01:00
|
|
|
|
}
|
2014-06-11 09:48:17 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
base.into_iter()
|
|
|
|
|
.filter(|crate_type| {
|
|
|
|
|
let res = !link::invalid_output_for_target(session, *crate_type);
|
2014-06-11 09:48:17 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
if !res {
|
|
|
|
|
session.warn(&format!("dropping unsupported crate type `{}` for target `{}`",
|
|
|
|
|
*crate_type,
|
|
|
|
|
session.opts.target_triple));
|
|
|
|
|
}
|
2014-06-11 09:48:17 +02:00
|
|
|
|
|
2015-11-10 21:48:44 +01:00
|
|
|
|
res
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2011-12-20 11:17:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 14:41:30 +01:00
|
|
|
|
pub fn compute_crate_disambiguator(session: &Session) -> String {
|
2016-10-27 04:42:48 +02:00
|
|
|
|
use std::hash::Hasher;
|
|
|
|
|
|
2016-10-31 00:01:37 +01:00
|
|
|
|
// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
|
|
|
|
|
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
|
|
|
|
|
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
|
|
|
|
|
// should still be safe enough to avoid collisions in practice.
|
|
|
|
|
// FIXME(mw): It seems that the crate_disambiguator is used everywhere as
|
|
|
|
|
// a hex-string instead of raw bytes. We should really use the
|
|
|
|
|
// smaller representation.
|
2016-12-14 00:45:03 +01:00
|
|
|
|
let mut hasher = StableHasher::<Fingerprint>::new();
|
2016-02-12 14:41:30 +01:00
|
|
|
|
|
|
|
|
|
let mut metadata = session.opts.cg.metadata.clone();
|
|
|
|
|
// We don't want the crate_disambiguator to dependent on the order
|
|
|
|
|
// -C metadata arguments, so sort them:
|
|
|
|
|
metadata.sort();
|
|
|
|
|
// Every distinct -C metadata value is only incorporated once:
|
|
|
|
|
metadata.dedup();
|
|
|
|
|
|
2016-10-27 04:42:48 +02:00
|
|
|
|
hasher.write(b"metadata");
|
2016-02-12 14:41:30 +01:00
|
|
|
|
for s in &metadata {
|
|
|
|
|
// Also incorporate the length of a metadata string, so that we generate
|
|
|
|
|
// different values for `-Cmetadata=ab -Cmetadata=c` and
|
|
|
|
|
// `-Cmetadata=a -Cmetadata=bc`
|
2016-10-27 04:42:48 +02:00
|
|
|
|
hasher.write_usize(s.len());
|
|
|
|
|
hasher.write(s.as_bytes());
|
2016-02-12 14:41:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this is an executable, add a special suffix, so that we don't get
|
|
|
|
|
// symbol conflicts when linking against a library of the same name.
|
2016-10-27 04:42:48 +02:00
|
|
|
|
let is_exe = session.crate_types.borrow().contains(&config::CrateTypeExecutable);
|
2016-02-12 14:41:30 +01:00
|
|
|
|
|
2016-12-14 00:45:03 +01:00
|
|
|
|
format!("{}{}", hasher.finish().to_hex(), if is_exe { "-exe" } else {""})
|
2014-07-01 07:52:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-13 17:31:05 +01:00
|
|
|
|
pub fn build_output_filenames(input: &Input,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
odir: &Option<PathBuf>,
|
|
|
|
|
ofile: &Option<PathBuf>,
|
2013-07-19 13:51:37 +02:00
|
|
|
|
attrs: &[ast::Attribute],
|
2014-03-05 15:36:01 +01:00
|
|
|
|
sess: &Session)
|
2015-11-10 21:48:44 +01:00
|
|
|
|
-> OutputFilenames {
|
2012-08-25 00:28:43 +02:00
|
|
|
|
match *ofile {
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
None => {
|
|
|
|
|
// "-" as input file will cause the parser to read from stdin so we
|
|
|
|
|
// have to make up a name
|
|
|
|
|
// We want to toss everything after the final '.'
|
|
|
|
|
let dirpath = match *odir {
|
|
|
|
|
Some(ref d) => d.clone(),
|
2015-11-10 21:48:44 +01:00
|
|
|
|
None => PathBuf::new(),
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
};
|
|
|
|
|
|
2014-06-06 22:21:18 +02:00
|
|
|
|
// If a crate name is present, we use it as the link name
|
2015-11-10 21:48:44 +01:00
|
|
|
|
let stem = sess.opts
|
|
|
|
|
.crate_name
|
|
|
|
|
.clone()
|
|
|
|
|
.or_else(|| attr::find_crate_name(attrs).map(|n| n.to_string()))
|
|
|
|
|
.unwrap_or(input.filestem());
|
2014-07-08 04:04:34 +02:00
|
|
|
|
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
OutputFilenames {
|
|
|
|
|
out_directory: dirpath,
|
|
|
|
|
out_filestem: stem,
|
|
|
|
|
single_output_file: None,
|
2014-07-15 18:13:58 +02:00
|
|
|
|
extra: sess.opts.cg.extra_filename.clone(),
|
2015-09-30 19:08:37 +02:00
|
|
|
|
outputs: sess.opts.output_types.clone(),
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
Some(ref out_file) => {
|
2015-11-10 21:48:44 +01:00
|
|
|
|
let unnamed_output_types = sess.opts
|
|
|
|
|
.output_types
|
|
|
|
|
.values()
|
2015-09-30 19:08:37 +02:00
|
|
|
|
.filter(|a| a.is_none())
|
|
|
|
|
.count();
|
2017-04-05 17:59:16 +02:00
|
|
|
|
let ofile = if unnamed_output_types > 1 {
|
|
|
|
|
sess.warn("due to multiple output types requested, the explicitly specified \
|
|
|
|
|
output file name will be adapted for each output type");
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(out_file.clone())
|
|
|
|
|
};
|
|
|
|
|
if *odir != None {
|
|
|
|
|
sess.warn("ignoring --out-dir flag due to -o flag.");
|
|
|
|
|
}
|
2015-03-09 02:10:33 +01:00
|
|
|
|
|
|
|
|
|
let cur_dir = Path::new("");
|
|
|
|
|
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
OutputFilenames {
|
2015-03-09 02:10:33 +01:00
|
|
|
|
out_directory: out_file.parent().unwrap_or(cur_dir).to_path_buf(),
|
2015-11-10 21:48:44 +01:00
|
|
|
|
out_filestem: out_file.file_stem()
|
|
|
|
|
.unwrap_or(OsStr::new(""))
|
|
|
|
|
.to_str()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
single_output_file: ofile,
|
2014-07-15 18:13:58 +02:00
|
|
|
|
extra: sess.opts.cg.extra_filename.clone(),
|
2015-09-30 19:08:37 +02:00
|
|
|
|
outputs: sess.opts.output_types.clone(),
|
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib,
--lib, and --bin flags from rustc, adding the following flags:
* --emit=[asm,ir,bc,obj,link]
* --crate-type=[dylib,rlib,staticlib,bin,lib]
The -o option has also been redefined to be used for *all* flavors of outputs.
This means that we no longer ignore it for libraries. The --out-dir remains the
same as before.
The new logic for files that rustc emits is as follows:
1. Output types are dictated by the --emit flag. The default value is
--emit=link, and this option can be passed multiple times and have all
options stacked on one another.
2. Crate types are dictated by the --crate-type flag and the #[crate_type]
attribute. The flags can be passed many times and stack with the crate
attribute.
3. If the -o flag is specified, and only one output type is specified, the
output will be emitted at this location. If more than one output type is
specified, then the filename of -o is ignored, and all output goes in the
directory that -o specifies. The -o option always ignores the --out-dir
option.
4. If the --out-dir flag is specified, all output goes in this directory.
5. If -o and --out-dir are both not present, all output goes in the current
directory of the process.
6. When multiple output types are specified, the filestem of all output is the
same as the name of the CrateId (derived from a crate attribute or from the
filestem of the crate file).
Closes #7791
Closes #11056
Closes #11667
2014-02-04 00:27:54 +01:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
}
|
2013-02-19 08:40:42 +01:00
|
|
|
|
}
|
2011-12-20 11:17:13 +01:00
|
|
|
|
}
|