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

Move macro_rules! macros to libstd #11774

Merged
merged 2 commits into from
Jan 25, 2014
Merged
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
11 changes: 5 additions & 6 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,16 @@ pub fn phase_2_configure_and_expand(sess: Session,
time(time_passes, "gated feature checking", (), |_|
front::feature_gate::check_crate(sess, &crate));

crate = time(time_passes, "crate injection", crate, |crate|
front::std_inject::maybe_inject_crates_ref(sess, crate));

// strip before expansion to allow macros to depend on
// configuration variables e.g/ in
//
// #[macro_escape] #[cfg(foo)]
// mod bar { macro_rules! baz!(() => {{}}) }
//
// baz! should not use this definition unless foo is enabled.
crate = time(time_passes, "std macros injection", crate, |crate|
syntax::ext::expand::inject_std_macros(sess.parse_sess,
cfg.clone(),
crate));

crate = time(time_passes, "configuration 1", crate, |crate|
front::config::strip_unconfigured_items(crate));
Expand All @@ -207,8 +206,8 @@ pub fn phase_2_configure_and_expand(sess: Session,
crate = time(time_passes, "maybe building test harness", crate, |crate|
front::test::modify_for_testing(sess, crate));

crate = time(time_passes, "std injection", crate, |crate|
front::std_inject::maybe_inject_libstd_ref(sess, crate));
crate = time(time_passes, "prelude injection", crate, |crate|
front::std_inject::maybe_inject_prelude(sess, crate));

time(time_passes, "assinging node ids and indexing ast", crate, |crate|
front::assign_node_ids_and_map::assign_node_ids_and_map(sess, crate))
Expand Down
66 changes: 46 additions & 20 deletions src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ use syntax::util::small_vector::SmallVector;

pub static VERSION: &'static str = "0.10-pre";

pub fn maybe_inject_libstd_ref(sess: Session, crate: ast::Crate)
pub fn maybe_inject_crates_ref(sess: Session, crate: ast::Crate)
-> ast::Crate {
if use_std(&crate) {
inject_libstd_ref(sess, crate)
inject_crates_ref(sess, crate)
} else {
crate
}
}

pub fn maybe_inject_prelude(sess: Session, crate: ast::Crate) -> ast::Crate {
if use_std(&crate) {
inject_prelude(sess, crate)
} else {
crate
}
Expand All @@ -44,13 +52,6 @@ fn no_prelude(attrs: &[ast::Attribute]) -> bool {
attr::contains_name(attrs, "no_implicit_prelude")
}

fn spanned<T>(x: T) -> codemap::Spanned<T> {
codemap::Spanned {
node: x,
span: DUMMY_SP,
}
}

struct StandardLibraryInjector {
sess: Session,
}
Expand All @@ -71,7 +72,11 @@ impl fold::Folder for StandardLibraryInjector {
node: ast::ViewItemExternMod(self.sess.ident_of("std"),
with_version("std"),
ast::DUMMY_NODE_ID),
attrs: ~[],
attrs: ~[
attr::mk_attr(attr::mk_list_item(@"phase",
~[attr::mk_word_item(@"syntax"),
attr::mk_word_item(@"link")]))
],
vis: ast::Inherited,
span: DUMMY_SP
}];
Expand All @@ -96,22 +101,43 @@ impl fold::Folder for StandardLibraryInjector {
}

vis.push_all(crate.module.view_items);
let mut new_module = ast::Mod {
let new_module = ast::Mod {
view_items: vis,
..crate.module.clone()
};

if !no_prelude(crate.attrs) {
// only add `use std::prelude::*;` if there wasn't a
// `#[no_implicit_prelude];` at the crate level.
new_module = self.fold_mod(&new_module);
}

ast::Crate {
module: new_module,
..crate
}
}
}

fn inject_crates_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
let mut fold = StandardLibraryInjector {
sess: sess,
};
fold.fold_crate(crate)
}

struct PreludeInjector {
sess: Session,
}


impl fold::Folder for PreludeInjector {
fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate {
if !no_prelude(crate.attrs) {
// only add `use std::prelude::*;` if there wasn't a
// `#[no_implicit_prelude];` at the crate level.
ast::Crate {
module: self.fold_mod(&crate.module),
..crate
}
} else {
crate
}
}

fn fold_item(&mut self, item: @ast::Item) -> SmallVector<@ast::Item> {
if !no_prelude(item.attrs) {
Expand Down Expand Up @@ -142,7 +168,7 @@ impl fold::Folder for StandardLibraryInjector {
],
};

let vp = @spanned(ast::ViewPathGlob(prelude_path, ast::DUMMY_NODE_ID));
let vp = @codemap::dummy_spanned(ast::ViewPathGlob(prelude_path, ast::DUMMY_NODE_ID));
let vi2 = ast::ViewItem {
node: ast::ViewItemUse(~[vp]),
attrs: ~[],
Expand All @@ -161,8 +187,8 @@ impl fold::Folder for StandardLibraryInjector {
}
}

fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
let mut fold = StandardLibraryInjector {
fn inject_prelude(sess: Session, crate: ast::Crate) -> ast::Crate {
let mut fold = PreludeInjector {
sess: sess,
};
fold.fold_crate(crate)
Expand Down
4 changes: 3 additions & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -76,6 +76,8 @@
#[cfg(test)] pub use ops = realstd::ops;
#[cfg(test)] pub use cmp = realstd::cmp;

mod macros;

mod rtdeps;

/* The Prelude. */
Expand Down
189 changes: 189 additions & 0 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// 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.
#[macro_escape];

#[macro_export]
macro_rules! log(
($lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= __log_level() {
format_args!(|args| {
::std::logging::log(lvl, args)
}, $($arg)+)
}
})
)
#[macro_export]
macro_rules! error( ($($arg:tt)*) => (log!(1u32, $($arg)*)) )
#[macro_export]
macro_rules! warn ( ($($arg:tt)*) => (log!(2u32, $($arg)*)) )
#[macro_export]
macro_rules! info ( ($($arg:tt)*) => (log!(3u32, $($arg)*)) )
#[macro_export]
macro_rules! debug( ($($arg:tt)*) => (
if cfg!(not(ndebug)) { log!(4u32, $($arg)*) }
))

#[macro_export]
macro_rules! log_enabled(
($lvl:expr) => ( {
let lvl = $lvl;
lvl <= __log_level() && (lvl != 4 || cfg!(not(ndebug)))
} )
)

#[macro_export]
macro_rules! fail(
() => (
fail!("explicit failure")
);
($msg:expr) => (
::std::rt::begin_unwind($msg, file!(), line!())
);
($fmt:expr, $($arg:tt)*) => (
::std::rt::begin_unwind(format!($fmt, $($arg)*), file!(), line!())
)
)

#[macro_export]
macro_rules! assert(
($cond:expr) => {
if !$cond {
fail!("assertion failed: {:s}", stringify!($cond))
}
};
($cond:expr, $msg:expr) => {
if !$cond {
fail!($msg)
}
};
($cond:expr, $( $arg:expr ),+) => {
if !$cond {
fail!( $($arg),+ )
}
}
)

#[macro_export]
macro_rules! assert_eq (
($given:expr , $expected:expr) => (
{
let given_val = &($given);
let expected_val = &($expected);
// check both directions of equality....
if !((*given_val == *expected_val) &&
(*expected_val == *given_val)) {
fail!("assertion failed: `(left == right) && (right == left)` \
(left: `{:?}`, right: `{:?}`)", *given_val, *expected_val)
}
}
)
)

/// A utility macro for indicating unreachable code. It will fail if
/// executed. This is occasionally useful to put after loops that never
/// terminate normally, but instead directly return from a function.
///
/// # Example
///
/// ```rust
/// fn choose_weighted_item(v: &[Item]) -> Item {
/// assert!(!v.is_empty());
/// let mut so_far = 0u;
/// for item in v.iter() {
/// so_far += item.weight;
/// if so_far > 100 {
/// return item;
/// }
/// }
/// // The above loop always returns, so we must hint to the
/// // type checker that it isn't possible to get down here
/// unreachable!();
/// }
/// ```
#[macro_export]
macro_rules! unreachable (() => (
fail!("internal error: entered unreachable code");
))

#[macro_export]
macro_rules! condition (

{ pub $c:ident: $input:ty -> $out:ty; } => {

pub mod $c {
#[allow(unused_imports)];
#[allow(non_uppercase_statics)];
#[allow(missing_doc)];

use super::*;

local_data_key!(key: @::std::condition::Handler<$input, $out>)

pub static cond :
::std::condition::Condition<$input,$out> =
::std::condition::Condition {
name: stringify!($c),
key: key
};
}
};

{ $c:ident: $input:ty -> $out:ty; } => {

mod $c {
#[allow(unused_imports)];
#[allow(non_uppercase_statics)];
#[allow(dead_code)];

use super::*;

local_data_key!(key: @::std::condition::Handler<$input, $out>)

pub static cond :
::std::condition::Condition<$input,$out> =
::std::condition::Condition {
name: stringify!($c),
key: key
};
}
}
)

#[macro_export]
macro_rules! format(($($arg:tt)*) => (
format_args!(::std::fmt::format, $($arg)*)
))
#[macro_export]
macro_rules! write(($dst:expr, $($arg:tt)*) => (
format_args!(|args| { ::std::fmt::write($dst, args) }, $($arg)*)
))
#[macro_export]
macro_rules! writeln(($dst:expr, $($arg:tt)*) => (
format_args!(|args| { ::std::fmt::writeln($dst, args) }, $($arg)*)
))
#[macro_export]
macro_rules! print (
($($arg:tt)*) => (format_args!(::std::io::stdio::print_args, $($arg)*))
)
#[macro_export]
macro_rules! println (
($($arg:tt)*) => (format_args!(::std::io::stdio::println_args, $($arg)*))
)

#[macro_export]
macro_rules! local_data_key (
($name:ident: $ty:ty) => (
static $name: ::std::local_data::Key<$ty> = &::std::local_data::Key;
);
(pub $name:ident: $ty:ty) => (
pub static $name: ::std::local_data::Key<$ty> = &::std::local_data::Key;
)
)
Loading