Skip to content

Commit cd138dc

Browse files
committed
Auto merge of #28138 - nrc:hir, r=nikomatsakis
r? @nikomatsakis Trying to land this first stab, which basically just duplicates the AST. Will file issues for the various things I've got in mind to improve.
2 parents b7b1dce + facdf2e commit cd138dc

File tree

160 files changed

+13918
-4452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+13918
-4452
lines changed

mk/crates.mk

+10-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TARGET_CRATES := libc std flate arena term \
5656
alloc_system
5757
RUSTC_CRATES := rustc rustc_typeck rustc_borrowck rustc_resolve rustc_driver \
5858
rustc_trans rustc_back rustc_llvm rustc_privacy rustc_lint \
59-
rustc_data_structures rustc_platform_intrinsics
59+
rustc_data_structures rustc_front rustc_platform_intrinsics
6060
HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
6161
TOOLS := compiletest rustdoc rustc rustbook error-index-generator
6262

@@ -71,23 +71,24 @@ DEPS_graphviz := std
7171
DEPS_syntax := std term serialize log fmt_macros arena libc rustc_bitflags
7272
DEPS_rustc_driver := arena flate getopts graphviz libc rustc rustc_back rustc_borrowck \
7373
rustc_typeck rustc_resolve log syntax serialize rustc_llvm \
74-
rustc_trans rustc_privacy rustc_lint
74+
rustc_trans rustc_privacy rustc_lint rustc_front
7575

7676
DEPS_rustc_trans := arena flate getopts graphviz libc rustc rustc_back \
77-
log syntax serialize rustc_llvm rustc_platform_intrinsics
78-
DEPS_rustc_typeck := rustc syntax rustc_platform_intrinsics
79-
DEPS_rustc_borrowck := rustc log graphviz syntax
80-
DEPS_rustc_resolve := rustc log syntax
81-
DEPS_rustc_privacy := rustc log syntax
77+
log syntax serialize rustc_llvm rustc_front rustc_platform_intrinsics
78+
DEPS_rustc_typeck := rustc syntax rustc_front rustc_platform_intrinsics
79+
DEPS_rustc_borrowck := rustc rustc_front log graphviz syntax
80+
DEPS_rustc_resolve := rustc rustc_front log syntax
81+
DEPS_rustc_privacy := rustc rustc_front log syntax
8282
DEPS_rustc_lint := rustc log syntax
8383
DEPS_rustc := syntax flate arena serialize getopts rbml \
8484
log graphviz rustc_llvm rustc_back rustc_data_structures
8585
DEPS_rustc_llvm := native:rustllvm libc std rustc_bitflags
8686
DEPS_rustc_platform_intrinsics := rustc rustc_llvm
87-
DEPS_rustc_back := std syntax rustc_llvm flate log libc
87+
DEPS_rustc_back := std syntax rustc_llvm rustc_front flate log libc
88+
DEPS_rustc_front := std syntax log serialize
8889
DEPS_rustc_data_structures := std log serialize
8990
DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts \
90-
test rustc_lint
91+
test rustc_lint rustc_front
9192
DEPS_rustc_bitflags := core
9293
DEPS_flate := std native:miniz
9394
DEPS_arena := std

src/compiletest/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
268268
// FIXME (#9639): This needs to handle non-utf8 paths
269269
let mut args = vec!("-".to_owned(),
270270
"-Zunstable-options".to_owned(),
271-
"--pretty".to_owned(),
271+
"--unpretty".to_owned(),
272272
pretty_type,
273273
format!("--target={}", config.target),
274274
"-L".to_owned(),

src/librustc/ast_map/blocks.rs renamed to src/librustc/front/map/blocks.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
2424
pub use self::Code::*;
2525

26-
use ast_map::{self, Node};
26+
use front::map::{self, Node};
2727
use syntax::abi;
28-
use syntax::ast::{Block, FnDecl, NodeId};
29-
use syntax::ast;
28+
use rustc_front::hir::{Block, FnDecl};
29+
use syntax::ast::{NodeId, Ident};
30+
use rustc_front::hir as ast;
3031
use syntax::codemap::Span;
31-
use syntax::visit::FnKind;
32+
use rustc_front::visit::FnKind;
3233

3334
/// An FnLikeNode is a Node that is like a fn, in that it has a decl
3435
/// and a body (as well as a NodeId, a span, etc).
@@ -40,7 +41,7 @@ use syntax::visit::FnKind;
4041
///
4142
/// To construct one, use the `Code::from_node` function.
4243
#[derive(Copy, Clone)]
43-
pub struct FnLikeNode<'a> { node: ast_map::Node<'a> }
44+
pub struct FnLikeNode<'a> { node: map::Node<'a> }
4445

4546
/// MaybeFnLike wraps a method that indicates if an object
4647
/// corresponds to some FnLikeNode.
@@ -86,7 +87,7 @@ pub enum Code<'a> {
8687
}
8788

8889
impl<'a> Code<'a> {
89-
pub fn id(&self) -> ast::NodeId {
90+
pub fn id(&self) -> NodeId {
9091
match *self {
9192
FnLikeCode(node) => node.id(),
9293
BlockCode(block) => block.id,
@@ -95,7 +96,7 @@ impl<'a> Code<'a> {
9596

9697
/// Attempts to construct a Code from presumed FnLike or Block node input.
9798
pub fn from_node(node: Node) -> Option<Code> {
98-
if let ast_map::NodeBlock(block) = node {
99+
if let map::NodeBlock(block) = node {
99100
Some(BlockCode(block))
100101
} else {
101102
FnLikeNode::from_node(node).map(|fn_like| FnLikeCode(fn_like))
@@ -106,15 +107,15 @@ impl<'a> Code<'a> {
106107
/// These are all the components one can extract from a fn item for
107108
/// use when implementing FnLikeNode operations.
108109
struct ItemFnParts<'a> {
109-
ident: ast::Ident,
110+
ident: Ident,
110111
decl: &'a ast::FnDecl,
111112
unsafety: ast::Unsafety,
112113
constness: ast::Constness,
113114
abi: abi::Abi,
114115
vis: ast::Visibility,
115116
generics: &'a ast::Generics,
116117
body: &'a Block,
117-
id: ast::NodeId,
118+
id: NodeId,
118119
span: Span
119120
}
120121

@@ -137,10 +138,10 @@ impl<'a> FnLikeNode<'a> {
137138
/// Attempts to construct a FnLikeNode from presumed FnLike node input.
138139
pub fn from_node(node: Node) -> Option<FnLikeNode> {
139140
let fn_like = match node {
140-
ast_map::NodeItem(item) => item.is_fn_like(),
141-
ast_map::NodeTraitItem(tm) => tm.is_fn_like(),
142-
ast_map::NodeImplItem(_) => true,
143-
ast_map::NodeExpr(e) => e.is_fn_like(),
141+
map::NodeItem(item) => item.is_fn_like(),
142+
map::NodeTraitItem(tm) => tm.is_fn_like(),
143+
map::NodeImplItem(_) => true,
144+
map::NodeExpr(e) => e.is_fn_like(),
144145
_ => false
145146
};
146147
if fn_like {
@@ -202,7 +203,7 @@ impl<'a> FnLikeNode<'a> {
202203
fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
203204
I: FnOnce(ItemFnParts<'a>) -> A,
204205
M: FnOnce(NodeId,
205-
ast::Ident,
206+
Ident,
206207
&'a ast::MethodSig,
207208
Option<ast::Visibility>,
208209
&'a ast::Block,
@@ -211,7 +212,7 @@ impl<'a> FnLikeNode<'a> {
211212
C: FnOnce(ClosureParts<'a>) -> A,
212213
{
213214
match self.node {
214-
ast_map::NodeItem(i) => match i.node {
215+
map::NodeItem(i) => match i.node {
215216
ast::ItemFn(ref decl, unsafety, constness, abi, ref generics, ref block) =>
216217
item_fn(ItemFnParts {
217218
id: i.id,
@@ -227,13 +228,13 @@ impl<'a> FnLikeNode<'a> {
227228
}),
228229
_ => panic!("item FnLikeNode that is not fn-like"),
229230
},
230-
ast_map::NodeTraitItem(ti) => match ti.node {
231+
map::NodeTraitItem(ti) => match ti.node {
231232
ast::MethodTraitItem(ref sig, Some(ref body)) => {
232233
method(ti.id, ti.ident, sig, None, body, ti.span)
233234
}
234235
_ => panic!("trait method FnLikeNode that is not fn-like"),
235236
},
236-
ast_map::NodeImplItem(ii) => {
237+
map::NodeImplItem(ii) => {
237238
match ii.node {
238239
ast::MethodImplItem(ref sig, ref body) => {
239240
method(ii.id, ii.ident, sig, Some(ii.vis), body, ii.span)
@@ -243,7 +244,7 @@ impl<'a> FnLikeNode<'a> {
243244
}
244245
}
245246
}
246-
ast_map::NodeExpr(e) => match e.node {
247+
map::NodeExpr(e) => match e.node {
247248
ast::ExprClosure(_, ref decl, ref block) =>
248249
closure(ClosureParts::new(&**decl, &**block, e.id, e.span)),
249250
_ => panic!("expr FnLikeNode that is not fn-like"),

src/librustc/ast_map/mod.rs renamed to src/librustc/front/map/mod.rs

+18-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -15,14 +15,17 @@ use self::MapEntry::*;
1515
use metadata::inline::InlinedItem;
1616
use metadata::inline::InlinedItem as II;
1717
use middle::def_id::DefId;
18+
1819
use syntax::abi;
19-
use syntax::ast::*;
20-
use syntax::ast_util;
21-
use syntax::codemap::{DUMMY_SP, Span, Spanned};
22-
use syntax::fold::Folder;
20+
use syntax::ast::{Name, NodeId, Ident, CRATE_NODE_ID, DUMMY_NODE_ID};
21+
use syntax::codemap::{Span, Spanned};
2322
use syntax::parse::token;
24-
use syntax::print::pprust;
25-
use syntax::visit::{self, Visitor};
23+
24+
use rustc_front::hir::*;
25+
use rustc_front::fold::Folder;
26+
use rustc_front::visit::{self, Visitor};
27+
use rustc_front::util;
28+
use rustc_front::print::pprust;
2629

2730
use arena::TypedArena;
2831
use std::cell::RefCell;
@@ -159,7 +162,7 @@ impl<'ast> Clone for MapEntry<'ast> {
159162
}
160163

161164
#[derive(Debug)]
162-
struct InlinedParent {
165+
pub struct InlinedParent {
163166
path: Vec<PathElem>,
164167
ii: InlinedItem
165168
}
@@ -227,7 +230,7 @@ impl<'ast> MapEntry<'ast> {
227230

228231
/// Stores a crate and any number of inlined items from other crates.
229232
pub struct Forest {
230-
krate: Crate,
233+
pub krate: Crate,
231234
inlined_items: TypedArena<InlinedParent>
232235
}
233236

@@ -246,9 +249,10 @@ impl Forest {
246249

247250
/// Represents a mapping from Node IDs to AST elements and their parent
248251
/// Node IDs
252+
#[derive(Clone)]
249253
pub struct Map<'ast> {
250254
/// The backing storage for all the AST nodes.
251-
forest: &'ast Forest,
255+
pub forest: &'ast Forest,
252256

253257
/// NodeIds are sequential integers from 0, so we can be
254258
/// super-compact by storing them in a vector. Not everything with
@@ -870,7 +874,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
870874
}
871875

872876
fn visit_stmt(&mut self, stmt: &'ast Stmt) {
873-
let id = ast_util::stmt_id(stmt);
877+
let id = util::stmt_id(stmt);
874878
self.insert(id, NodeStmt(stmt));
875879
let parent_node = self.parent_node;
876880
self.parent_node = id;
@@ -917,20 +921,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
917921
}
918922
}
919923

920-
pub fn map_crate<'ast, F: FoldOps>(forest: &'ast mut Forest, fold_ops: F) -> Map<'ast> {
921-
// Replace the crate with an empty one to take it out.
922-
let krate = mem::replace(&mut forest.krate, Crate {
923-
module: Mod {
924-
inner: DUMMY_SP,
925-
items: vec![],
926-
},
927-
attrs: vec![],
928-
config: vec![],
929-
exported_macros: vec![],
930-
span: DUMMY_SP
931-
});
932-
forest.krate = IdAndSpanUpdater { fold_ops: fold_ops }.fold_crate(krate);
933-
924+
pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
934925
let mut collector = NodeCollector {
935926
map: vec![],
936927
parent_node: CRATE_NODE_ID,
@@ -974,11 +965,11 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
974965
II::Item(i) => II::Item(fld.fold_item(i).expect_one("expected one item")),
975966
II::TraitItem(d, ti) => {
976967
II::TraitItem(fld.fold_ops.new_def_id(d),
977-
fld.fold_trait_item(ti).expect_one("expected one trait item"))
968+
fld.fold_trait_item(ti).expect_one("expected one trait item"))
978969
}
979970
II::ImplItem(d, ii) => {
980971
II::ImplItem(fld.fold_ops.new_def_id(d),
981-
fld.fold_impl_item(ii).expect_one("expected one impl item"))
972+
fld.fold_impl_item(ii).expect_one("expected one impl item"))
982973
}
983974
II::Foreign(i) => II::Foreign(fld.fold_foreign_item(i))
984975
};
@@ -1064,7 +1055,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10641055
ItemTrait(..) => "trait",
10651056
ItemImpl(..) => "impl",
10661057
ItemDefaultImpl(..) => "default impl",
1067-
ItemMac(..) => "macro"
10681058
};
10691059
format!("{} {}{}", item_str, path_str, id_str)
10701060
}
@@ -1091,10 +1081,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10911081
map.path_to_string(id),
10921082
id_str)
10931083
}
1094-
MacImplItem(ref mac) => {
1095-
format!("method macro {}{}",
1096-
pprust::mac_to_string(mac), id_str)
1097-
}
10981084
}
10991085
}
11001086
Some(NodeTraitItem(ti)) => {

src/librustc/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ extern crate graphviz;
7373
extern crate libc;
7474
extern crate rustc_llvm;
7575
extern crate rustc_back;
76+
extern crate rustc_front;
7677
extern crate rustc_data_structures;
7778
extern crate serialize;
7879
extern crate rbml;
@@ -101,7 +102,9 @@ pub mod back {
101102
pub use rustc_back::svh;
102103
}
103104

104-
pub mod ast_map;
105+
pub mod front {
106+
pub mod map;
107+
}
105108

106109
pub mod middle {
107110
pub mod astconv_util;

src/librustc/lint/context.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ macro_rules! run_lints { ($cx:expr, $f:ident, $($args:expr),*) => ({
283283

284284
/// Parse the lint attributes into a vector, with `Err`s for malformed lint
285285
/// attributes. Writing this as an iterator is an enormous mess.
286+
// See also the hir version just below.
286287
pub fn gather_attrs(attrs: &[ast::Attribute])
287288
-> Vec<Result<(InternedString, Level, Span), Span>> {
288289
let mut out = vec!();
@@ -312,6 +313,40 @@ pub fn gather_attrs(attrs: &[ast::Attribute])
312313
}
313314
out
314315
}
316+
// Copy-pasted from the above function :-(
317+
pub fn gather_attrs_from_hir(attrs: &[::rustc_front::hir::Attribute])
318+
-> Vec<Result<(InternedString, Level, Span), Span>> {
319+
use ::rustc_front::attr::AttrMetaMethods;
320+
321+
let mut out = vec!();
322+
for attr in attrs {
323+
let level = match Level::from_str(&attr.name()) {
324+
None => continue,
325+
Some(lvl) => lvl,
326+
};
327+
328+
::rustc_front::attr::mark_used(attr);
329+
330+
let meta = &attr.node.value;
331+
let metas = match meta.node {
332+
::rustc_front::hir::MetaList(_, ref metas) => metas,
333+
_ => {
334+
out.push(Err(meta.span));
335+
continue;
336+
}
337+
};
338+
339+
for meta in metas {
340+
out.push(match meta.node {
341+
::rustc_front::hir::MetaWord(ref lint_name) => {
342+
Ok((lint_name.clone(), level, meta.span))
343+
}
344+
_ => Err(meta.span),
345+
});
346+
}
347+
}
348+
out
349+
}
315350

316351
/// Emit a lint as a warning or an error (or not at all)
317352
/// according to `level`.
@@ -696,9 +731,9 @@ impl LintPass for GatherNodeLevels {
696731
///
697732
/// Consumes the `lint_store` field of the `Session`.
698733
pub fn check_crate(tcx: &ty::ctxt,
734+
krate: &ast::Crate,
699735
exported_items: &ExportedItems) {
700736

701-
let krate = tcx.map.krate();
702737
let mut cx = Context::new(tcx, krate, exported_items);
703738

704739
// Visit the whole crate.

src/librustc/lint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use syntax::visit::FnKind;
3838
use syntax::ast;
3939

4040
pub use lint::context::{Context, LintStore, raw_emit_lint, check_crate, gather_attrs,
41-
GatherNodeLevels};
41+
gather_attrs_from_hir, GatherNodeLevels};
4242

4343
/// Specification of a single lint.
4444
#[derive(Copy, Clone, Debug)]

0 commit comments

Comments
 (0)