Skip to content

Commit 520671f

Browse files
committed
move most of front to libsyntax
1 parent b75b0f7 commit 520671f

File tree

9 files changed

+89
-70
lines changed

9 files changed

+89
-70
lines changed

src/librustc/driver/driver.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
166166
}
167167

168168
if sess.show_span() {
169-
front::show_span::run(sess, &krate);
169+
syntax::show_span::run(sess.diagnostic(), &krate);
170170
}
171171

172172
krate
@@ -209,7 +209,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
209209
// baz! should not use this definition unless foo is enabled.
210210

211211
krate = time(time_passes, "configuration 1", krate, |krate|
212-
front::config::strip_unconfigured_items(krate));
212+
syntax::config::strip_unconfigured_items(krate));
213213

214214
let mut addl_plugins = Some(addl_plugins);
215215
let Plugins { macros, registrars }
@@ -290,10 +290,13 @@ pub fn phase_2_configure_and_expand(sess: &Session,
290290

291291
// strip again, in case expansion added anything with a #[cfg].
292292
krate = time(time_passes, "configuration 2", krate, |krate|
293-
front::config::strip_unconfigured_items(krate));
293+
syntax::config::strip_unconfigured_items(krate));
294294

295295
krate = time(time_passes, "maybe building test harness", krate, |krate|
296-
front::test::modify_for_testing(sess, krate));
296+
syntax::test::modify_for_testing(&sess.parse_sess,
297+
&sess.opts.cfg,
298+
krate,
299+
sess.diagnostic()));
297300

298301
krate = time(time_passes, "prelude injection", krate, |krate|
299302
front::std_inject::maybe_inject_prelude(sess, krate));

src/librustc/driver/session.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub struct Session {
4747
pub working_dir: Path,
4848
pub lint_store: RefCell<lint::LintStore>,
4949
pub lints: RefCell<NodeMap<Vec<(lint::LintId, codemap::Span, String)>>>,
50-
pub node_id: Cell<ast::NodeId>,
5150
pub crate_types: RefCell<Vec<config::CrateType>>,
5251
pub crate_metadata: RefCell<Vec<String>>,
5352
pub features: front::feature_gate::Features,
@@ -129,17 +128,10 @@ impl Session {
129128
lints.insert(id, vec!((lint_id, sp, msg)));
130129
}
131130
pub fn next_node_id(&self) -> ast::NodeId {
132-
self.reserve_node_ids(1)
131+
self.parse_sess.next_node_id()
133132
}
134133
pub fn reserve_node_ids(&self, count: ast::NodeId) -> ast::NodeId {
135-
let v = self.node_id.get();
136-
137-
match v.checked_add(&count) {
138-
Some(next) => { self.node_id.set(next); }
139-
None => self.bug("Input too large, ran out of node ids!")
140-
}
141-
142-
v
134+
self.parse_sess.reserve_node_ids(count)
143135
}
144136
pub fn diagnostic<'a>(&'a self) -> &'a diagnostic::SpanHandler {
145137
&self.parse_sess.span_diagnostic
@@ -251,7 +243,6 @@ pub fn build_session_(sopts: config::Options,
251243
working_dir: os::getcwd(),
252244
lint_store: RefCell::new(lint::LintStore::new()),
253245
lints: RefCell::new(NodeMap::new()),
254-
node_id: Cell::new(1),
255246
crate_types: RefCell::new(Vec::new()),
256247
crate_metadata: RefCell::new(Vec::new()),
257248
features: front::feature_gate::Features::new(),

src/librustc/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,8 @@ pub mod middle {
117117
}
118118

119119
pub mod front {
120-
pub mod config;
121-
pub mod test;
122120
pub mod std_inject;
123121
pub mod feature_gate;
124-
pub mod show_span;
125122
}
126123

127124
pub mod metadata;

src/libsyntax/ast_map/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ast_util::PostExpansionMethod;
1515
use codemap::{DUMMY_SP, Span, Spanned};
1616
use fold::Folder;
1717
use parse::token;
18+
use parse::ParseSess;
1819
use print::pprust;
1920
use visit::{mod, Visitor};
2021

@@ -250,6 +251,7 @@ pub struct Map<'ast> {
250251
}
251252

252253
impl<'ast> Map<'ast> {
254+
impl Map {
253255
fn entry_count(&self) -> uint {
254256
self.map.borrow().len()
255257
}

src/librustc/front/config.rs renamed to src/libsyntax/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::fold::Folder;
12-
use syntax::{ast, fold, attr};
13-
use syntax::codemap::Spanned;
14-
use syntax::ptr::P;
11+
use fold::Folder;
12+
use {ast, fold, attr};
13+
use codemap::Spanned;
14+
use ptr::P;
1515

1616
/// A folder that strips out items that do not belong in the current
1717
/// configuration.

src/libsyntax/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ pub mod ast_map;
5959
pub mod ast_util;
6060
pub mod attr;
6161
pub mod codemap;
62+
pub mod config;
6263
pub mod crateid;
6364
pub mod diagnostic;
6465
pub mod fold;
6566
pub mod owned_slice;
6667
pub mod parse;
6768
pub mod ptr;
69+
pub mod show_span;
70+
pub mod test;
6871
pub mod visit;
6972

7073
pub mod print {

src/libsyntax/parse/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,38 @@ pub struct ParseSess {
3737
pub span_diagnostic: SpanHandler, // better be the same as the one in the reader!
3838
/// Used to determine and report recursive mod inclusions
3939
included_mod_stack: RefCell<Vec<Path>>,
40+
pub node_id: Cell<ast::NodeId>,
4041
}
4142

4243
pub fn new_parse_sess() -> ParseSess {
4344
ParseSess {
4445
span_diagnostic: mk_span_handler(default_handler(Auto, None), CodeMap::new()),
4546
included_mod_stack: RefCell::new(Vec::new()),
47+
node_id: Cell::new(1),
4648
}
4749
}
4850

4951
pub fn new_parse_sess_special_handler(sh: SpanHandler) -> ParseSess {
5052
ParseSess {
5153
span_diagnostic: sh,
5254
included_mod_stack: RefCell::new(Vec::new()),
55+
node_id: Cell::new(1),
56+
}
57+
}
58+
59+
impl ParseSess {
60+
pub fn next_node_id(&self) -> ast::NodeId {
61+
self.reserve_node_ids(1)
62+
}
63+
pub fn reserve_node_ids(&self, count: ast::NodeId) -> ast::NodeId {
64+
let v = self.node_id.get();
65+
66+
match v.checked_add(&count) {
67+
Some(next) => { self.node_id.set(next); }
68+
None => fail!("Input too large, ran out of node ids!")
69+
}
70+
71+
v
5372
}
5473
}
5574

src/librustc/front/show_span.rs renamed to src/libsyntax/show_span.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,27 @@
1313
//! This module shows spans for all expressions in the crate
1414
//! to help with compiler debugging.
1515
16-
use syntax::ast;
17-
use syntax::visit;
18-
use syntax::visit::Visitor;
19-
20-
use driver::session::Session;
16+
use ast;
17+
use diagnostic;
18+
use visit;
19+
use visit::Visitor;
2120

2221
struct ShowSpanVisitor<'a> {
23-
sess: &'a Session
22+
span_diagnostic: &'a diagnostic::SpanHandler,
2423
}
2524

26-
impl<'a, 'v> Visitor<'v> for ShowSpanVisitor<'a> {
27-
fn visit_expr(&mut self, e: &ast::Expr) {
28-
self.sess.span_note(e.span, "expression");
29-
visit::walk_expr(self, e);
25+
impl<'a> Visitor<()> for ShowSpanVisitor<'a> {
26+
fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
27+
self.span_diagnostic.span_note(e.span, "expression");
28+
visit::walk_expr(self, e, ());
3029
}
3130

3231
fn visit_mac(&mut self, macro: &ast::Mac) {
3332
visit::walk_mac(self, macro);
3433
}
3534
}
3635

37-
pub fn run(sess: &Session, krate: &ast::Crate) {
38-
let mut v = ShowSpanVisitor { sess: sess };
36+
pub fn run(span_diagnostic: &diagnostic::SpanHandler, krate: &ast::Crate) {
37+
let mut v = ShowSpanVisitor { span_diagnostic: span_diagnostic };
3938
visit::walk_crate(&mut v, krate);
4039
}

src/librustc/front/test.rs renamed to src/libsyntax/test.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@
1313
#![allow(dead_code)]
1414
#![allow(unused_imports)]
1515

16-
use driver::session::Session;
17-
use front::config;
18-
16+
use std::gc::{Gc, GC};
1917
use std::slice;
2018
use std::mem;
2119
use std::vec;
22-
use syntax::{ast, ast_util};
23-
use syntax::ast_util::*;
24-
use syntax::attr::AttrMetaMethods;
25-
use syntax::attr;
26-
use syntax::codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
27-
use syntax::codemap;
28-
use syntax::ext::base::ExtCtxt;
29-
use syntax::ext::build::AstBuilder;
30-
use syntax::ext::expand::ExpansionConfig;
31-
use syntax::fold::{Folder, MoveMap};
32-
use syntax::fold;
33-
use syntax::owned_slice::OwnedSlice;
34-
use syntax::parse::token::InternedString;
35-
use syntax::parse::token;
36-
use syntax::print::pprust;
37-
use syntax::ptr::P;
38-
use syntax::util::small_vector::SmallVector;
20+
use ast_util::*;
21+
use attr::AttrMetaMethods;
22+
use attr;
23+
use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
24+
use codemap;
25+
use diagnostic;
26+
use config;
27+
use ext::base::ExtCtxt;
28+
use ext::build::AstBuilder;
29+
use ext::expand::ExpansionConfig;
30+
use fold::{Folder, MoveMap};
31+
use fold;
32+
use owned_slice::OwnedSlice;
33+
use parse::token::InternedString;
34+
use parse::{token, ParseSess};
35+
use print::pprust;
36+
use {ast, ast_util};
37+
use ptr::P;
38+
use util::small_vector::SmallVector;
3939

4040
struct Test {
4141
span: Span,
@@ -46,7 +46,8 @@ struct Test {
4646
}
4747

4848
struct TestCtxt<'a> {
49-
sess: &'a Session,
49+
sess: &'a ParseSess,
50+
span_diagnostic: &'a diagnostic::SpanHandler,
5051
path: Vec<ast::Ident>,
5152
ext_cx: ExtCtxt<'a>,
5253
testfns: Vec<Test>,
@@ -60,8 +61,10 @@ struct TestCtxt<'a> {
6061

6162
// Traverse the crate, collecting all the test functions, eliding any
6263
// existing main functions, and synthesizing a main test harness
63-
pub fn modify_for_testing(sess: &Session,
64-
krate: ast::Crate) -> ast::Crate {
64+
pub fn modify_for_testing(sess: &ParseSess,
65+
cfg: &ast::CrateConfig,
66+
krate: ast::Crate,
67+
span_diagnostic: &diagnostic::SpanHandler) -> ast::Crate {
6568
// We generate the test harness when building in the 'test'
6669
// configuration, either with the '--test' or '--cfg test'
6770
// command line options.
@@ -76,7 +79,7 @@ pub fn modify_for_testing(sess: &Session,
7679
"reexport_test_harness_main");
7780

7881
if should_test {
79-
generate_test_harness(sess, reexport_test_harness_main, krate)
82+
generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
8083
} else {
8184
strip_test_functions(krate)
8285
}
@@ -113,8 +116,8 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
113116
if is_test_fn(&self.cx, &*i) || is_bench_fn(&self.cx, &*i) {
114117
match i.node {
115118
ast::ItemFn(_, ast::UnsafeFn, _, _, _) => {
116-
let sess = self.cx.sess;
117-
sess.span_fatal(i.span,
119+
let diag = self.cx.span_diagnostic;
120+
diag.span_fatal(i.span,
118121
"unsafe functions cannot be used for \
119122
tests");
120123
}
@@ -223,12 +226,15 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
223226
(it, sym)
224227
}
225228

226-
fn generate_test_harness(sess: &Session,
229+
fn generate_test_harness(sess: &ParseSess,
227230
reexport_test_harness_main: Option<InternedString>,
228-
krate: ast::Crate) -> ast::Crate {
231+
krate: ast::Crate,
232+
cfg: &ast::CrateConfig,
233+
sd: &diagnostic::SpanHandler) -> ast::Crate {
229234
let mut cx: TestCtxt = TestCtxt {
230235
sess: sess,
231-
ext_cx: ExtCtxt::new(&sess.parse_sess, sess.opts.cfg.clone(),
236+
span_diagnostic: sd,
237+
ext_cx: ExtCtxt::new(sess, cfg.clone(),
232238
ExpansionConfig {
233239
deriving_hash_type_parameter: false,
234240
crate_name: "test".to_string(),
@@ -288,8 +294,8 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
288294
}
289295

290296
if has_test_attr && !has_test_signature(i) {
291-
let sess = cx.sess;
292-
sess.span_err(
297+
let diag = cx.span_diagnostic;
298+
diag.span_err(
293299
i.span,
294300
"functions used as tests must have signature fn() -> ()."
295301
);
@@ -320,8 +326,8 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
320326
}
321327

322328
if has_bench_attr && !has_test_signature(i) {
323-
let sess = cx.sess;
324-
sess.span_err(i.span, "functions used as benches must have signature \
329+
let diag = cx.span_diagnostic;
330+
diag.span_err(i.span, "functions used as benches must have signature \
325331
`fn(&mut Bencher) -> ()`");
326332
}
327333

@@ -547,9 +553,8 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
547553
let mut visible_path = match cx.toplevel_reexport {
548554
Some(id) => vec![id],
549555
None => {
550-
cx.sess.bug(
551-
"expected to find top-level re-export name, but found None"
552-
);
556+
let diag = cx.span_diagnostic;
557+
diag.handler.bug("expected to find top-level re-export name, but found None");
553558
}
554559
};
555560
visible_path.extend(path.into_iter());

0 commit comments

Comments
 (0)