Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring and features in rustc driver for embedders #15820

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions src/librustc/driver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ pub struct Options {
pub color: ColorConfig,
pub externs: HashMap<String, Vec<String>>,
pub crate_name: Option<String>,
/// An optional name to use as the crate for std during std injection,
/// written `extern crate std = "name"`. Default to "std". Used by
/// out-of-tree drivers.
pub alt_std_name: Option<String>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doc-comment here?

}

/// Some reasonable defaults
Expand Down Expand Up @@ -124,6 +128,7 @@ pub fn basic_options() -> Options {
color: Auto,
externs: HashMap::new(),
crate_name: None,
alt_std_name: None,
}
}

Expand Down Expand Up @@ -593,24 +598,10 @@ fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
}

pub fn build_session_options(matches: &getopts::Matches) -> Options {
let mut crate_types: Vec<CrateType> = Vec::new();

let unparsed_crate_types = matches.opt_strs("crate-type");
for unparsed_crate_type in unparsed_crate_types.iter() {
for part in unparsed_crate_type.as_slice().split(',') {
let new_part = match part {
"lib" => default_lib_output(),
"rlib" => CrateTypeRlib,
"staticlib" => CrateTypeStaticlib,
"dylib" => CrateTypeDylib,
"bin" => CrateTypeExecutable,
_ => {
early_error(format!("unknown crate type: `{}`",
part).as_slice())
}
};
crate_types.push(new_part)
}
}
let crate_types = parse_crate_types_from_list(unparsed_crate_types)
.unwrap_or_else(|e| early_error(e.as_slice()));

let parse_only = matches.opt_present("parse-only");
let no_trans = matches.opt_present("no-trans");
Expand Down Expand Up @@ -801,9 +792,33 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
color: color,
externs: externs,
crate_name: crate_name,
alt_std_name: None
}
}

pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateType>, String> {

let mut crate_types: Vec<CrateType> = Vec::new();
for unparsed_crate_type in list_list.iter() {
for part in unparsed_crate_type.as_slice().split(',') {
let new_part = match part {
"lib" => default_lib_output(),
"rlib" => CrateTypeRlib,
"staticlib" => CrateTypeStaticlib,
"dylib" => CrateTypeDylib,
"bin" => CrateTypeExecutable,
_ => {
return Err(format!("unknown crate type: `{}`",
part));
}
};
crate_types.push(new_part)
}
}

return Ok(crate_types);
}

impl fmt::Show for CrateType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
15 changes: 10 additions & 5 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub fn compile_input(sess: Session,
cfg: ast::CrateConfig,
input: &Input,
outdir: &Option<Path>,
output: &Option<Path>) {
output: &Option<Path>,
addl_plugins: Option<Plugins>) {
// 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
Expand All @@ -85,7 +86,8 @@ pub fn compile_input(sess: Session,
let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
input);
let (expanded_crate, ast_map)
= match phase_2_configure_and_expand(&sess, krate, id.as_slice()) {
= match phase_2_configure_and_expand(&sess, krate, id.as_slice(),
addl_plugins) {
None => return,
Some(p) => p,
};
Expand Down Expand Up @@ -179,14 +181,16 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
// modified

/// Run the "early phases" of the compiler: initial `cfg` processing,
/// loading compiler plugins (including those from `addl_plugins`),
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
/// harness if one is to be provided and injection of a dependency on the
/// standard library and prelude.
///
/// Returns `None` if we're aborting after handling -W help.
pub fn phase_2_configure_and_expand(sess: &Session,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you adjust the doc comment on this to include something like "initial cfg processing, loading of compiler plugins (including registering those specified in addl_plugins), syntax expansion, ..."?

mut krate: ast::Crate,
crate_name: &str)
crate_name: &str,
addl_plugins: Option<Plugins>)
-> Option<(ast::Crate, syntax::ast_map::Map)> {
let time_passes = sess.time_passes();

Expand All @@ -212,9 +216,10 @@ pub fn phase_2_configure_and_expand(sess: &Session,
krate = time(time_passes, "configuration 1", krate, |krate|
front::config::strip_unconfigured_items(krate));

let mut addl_plugins = Some(addl_plugins);
let Plugins { macros, registrars }
= time(time_passes, "plugin loading", (), |_|
plugin::load::load_plugins(sess, &krate));
plugin::load::load_plugins(sess, &krate, addl_plugins.take_unwrap()));

let mut registry = Registry::new(&krate);

Expand Down Expand Up @@ -697,7 +702,7 @@ pub fn pretty_print_input(sess: Session,
PpmExpanded | PpmExpandedIdentified | PpmTyped | PpmFlowGraph(_) => {
let (krate, ast_map)
= match phase_2_configure_and_expand(&sess, krate,
id.as_slice()) {
id.as_slice(), None) {
None => return,
Some(p) => p,
};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn run_compiler(args: &[String]) {
return;
}

driver::compile_input(sess, cfg, &input, &odir, &ofile);
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
}

/// Prints version information and returns None on success or an error
Expand Down Expand Up @@ -418,7 +418,7 @@ pub fn list_metadata(sess: &Session, path: &Path,
///
/// The diagnostic emitter yielded to the procedure should be used for reporting
/// errors of the compiler.
fn monitor(f: proc():Send) {
pub fn monitor(f: proc():Send) {
// FIXME: This is a hack for newsched since it doesn't support split stacks.
// rustc needs a lot of stack! When optimizations are disabled, it needs
// even *more* stack than usual as well.
Expand Down
9 changes: 8 additions & 1 deletion src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ struct StandardLibraryInjector<'a> {

impl<'a> fold::Folder for StandardLibraryInjector<'a> {
fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {

// The name to use in `extern crate std = "name";`
let actual_crate_name = match self.sess.opts.alt_std_name {
Some(ref s) => token::intern_and_get_ident(s.as_slice()),
None => token::intern_and_get_ident("std"),
};

let mut vis = vec!(ast::ViewItem {
node: ast::ViewItemExternCrate(token::str_to_ident("std"),
None,
Some((actual_crate_name, ast::CookedStr)),
ast::DUMMY_NODE_ID),
attrs: vec!(
attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_list_item(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/infer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn test_env(_test_name: &str,
let input = driver::StrInput(source_string.to_string());
let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
let (krate, ast_map) =
driver::phase_2_configure_and_expand(&sess, krate, "test")
driver::phase_2_configure_and_expand(&sess, krate, "test", None)
.expect("phase 2 aborted");

// run just enough stuff to build a tcx:
Expand Down
18 changes: 16 additions & 2 deletions src/librustc/plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,24 @@ impl<'a> PluginLoader<'a> {
}

/// Read plugin metadata and dynamically load registrar functions.
pub fn load_plugins(sess: &Session, krate: &ast::Crate) -> Plugins {
pub fn load_plugins(sess: &Session, krate: &ast::Crate,
addl_plugins: Option<Plugins>) -> Plugins {
let mut loader = PluginLoader::new(sess);
visit::walk_crate(&mut loader, krate, ());
loader.plugins

let mut plugins = loader.plugins;

match addl_plugins {
Some(addl_plugins) => {
// Add in the additional plugins requested by the frontend
let Plugins { macros: addl_macros, registrars: addl_registrars } = addl_plugins;
plugins.macros.push_all_move(addl_macros);
plugins.registrars.push_all_move(addl_registrars);
}
None => ()
}

return plugins;
}

// note that macros aren't expanded yet, and therefore macros can't add plugins.
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)
&input);

let (krate, ast_map)
= phase_2_configure_and_expand(&sess, krate, name.as_slice())
= phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
.expect("phase_2_configure_and_expand aborted in rustdoc!");

let driver::driver::CrateAnalysis {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn run(input: &str,
}));
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
let (krate, _) = driver::phase_2_configure_and_expand(&sess, krate,
"rustdoc-test")
"rustdoc-test", None)
.expect("phase_2_configure_and_expand aborted in rustdoc!");

let ctx = box(GC) core::DocContext {
Expand Down Expand Up @@ -166,7 +166,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
let out = Some(outdir.path().clone());
let cfg = config::build_configuration(&sess);
let libdir = sess.target_filesearch().get_lib_path();
driver::compile_input(sess, cfg, &input, &out, &None);
driver::compile_input(sess, cfg, &input, &out, &None, None);

if no_run { return }

Expand Down