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

Add an intital HIR and lowering step #28138

Merged
merged 1 commit into from
Sep 3, 2015
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
19 changes: 10 additions & 9 deletions mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TARGET_CRATES := libc std flate arena term \
alloc_system
RUSTC_CRATES := rustc rustc_typeck rustc_borrowck rustc_resolve rustc_driver \
rustc_trans rustc_back rustc_llvm rustc_privacy rustc_lint \
rustc_data_structures rustc_platform_intrinsics
rustc_data_structures rustc_front rustc_platform_intrinsics
HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
TOOLS := compiletest rustdoc rustc rustbook error-index-generator

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

DEPS_rustc_trans := arena flate getopts graphviz libc rustc rustc_back \
log syntax serialize rustc_llvm rustc_platform_intrinsics
DEPS_rustc_typeck := rustc syntax rustc_platform_intrinsics
DEPS_rustc_borrowck := rustc log graphviz syntax
DEPS_rustc_resolve := rustc log syntax
DEPS_rustc_privacy := rustc log syntax
log syntax serialize rustc_llvm rustc_front rustc_platform_intrinsics
DEPS_rustc_typeck := rustc syntax rustc_front rustc_platform_intrinsics
DEPS_rustc_borrowck := rustc rustc_front log graphviz syntax
DEPS_rustc_resolve := rustc rustc_front log syntax
DEPS_rustc_privacy := rustc rustc_front log syntax
DEPS_rustc_lint := rustc log syntax
DEPS_rustc := syntax flate arena serialize getopts rbml \
log graphviz rustc_llvm rustc_back rustc_data_structures
DEPS_rustc_llvm := native:rustllvm libc std rustc_bitflags
DEPS_rustc_platform_intrinsics := rustc rustc_llvm
DEPS_rustc_back := std syntax rustc_llvm flate log libc
DEPS_rustc_back := std syntax rustc_llvm rustc_front flate log libc
DEPS_rustc_front := std syntax log serialize
DEPS_rustc_data_structures := std log serialize
DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts \
test rustc_lint
test rustc_lint rustc_front
DEPS_rustc_bitflags := core
DEPS_flate := std native:miniz
DEPS_arena := std
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
// FIXME (#9639): This needs to handle non-utf8 paths
let mut args = vec!("-".to_owned(),
"-Zunstable-options".to_owned(),
"--pretty".to_owned(),
"--unpretty".to_owned(),
pretty_type,
format!("--target={}", config.target),
"-L".to_owned(),
Expand Down
37 changes: 19 additions & 18 deletions src/librustc/ast_map/blocks.rs → src/librustc/front/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

pub use self::Code::*;

use ast_map::{self, Node};
use front::map::{self, Node};
use syntax::abi;
use syntax::ast::{Block, FnDecl, NodeId};
use syntax::ast;
use rustc_front::hir::{Block, FnDecl};
use syntax::ast::{NodeId, Ident};
use rustc_front::hir as ast;
use syntax::codemap::Span;
use syntax::visit::FnKind;
use rustc_front::visit::FnKind;

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

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

impl<'a> Code<'a> {
pub fn id(&self) -> ast::NodeId {
pub fn id(&self) -> NodeId {
match *self {
FnLikeCode(node) => node.id(),
BlockCode(block) => block.id,
Expand All @@ -95,7 +96,7 @@ impl<'a> Code<'a> {

/// Attempts to construct a Code from presumed FnLike or Block node input.
pub fn from_node(node: Node) -> Option<Code> {
if let ast_map::NodeBlock(block) = node {
if let map::NodeBlock(block) = node {
Some(BlockCode(block))
} else {
FnLikeNode::from_node(node).map(|fn_like| FnLikeCode(fn_like))
Expand All @@ -106,15 +107,15 @@ impl<'a> Code<'a> {
/// These are all the components one can extract from a fn item for
/// use when implementing FnLikeNode operations.
struct ItemFnParts<'a> {
ident: ast::Ident,
ident: Ident,
decl: &'a ast::FnDecl,
unsafety: ast::Unsafety,
constness: ast::Constness,
abi: abi::Abi,
vis: ast::Visibility,
generics: &'a ast::Generics,
body: &'a Block,
id: ast::NodeId,
id: NodeId,
span: Span
}

Expand All @@ -137,10 +138,10 @@ impl<'a> FnLikeNode<'a> {
/// Attempts to construct a FnLikeNode from presumed FnLike node input.
pub fn from_node(node: Node) -> Option<FnLikeNode> {
let fn_like = match node {
ast_map::NodeItem(item) => item.is_fn_like(),
ast_map::NodeTraitItem(tm) => tm.is_fn_like(),
ast_map::NodeImplItem(_) => true,
ast_map::NodeExpr(e) => e.is_fn_like(),
map::NodeItem(item) => item.is_fn_like(),
map::NodeTraitItem(tm) => tm.is_fn_like(),
map::NodeImplItem(_) => true,
map::NodeExpr(e) => e.is_fn_like(),
_ => false
};
if fn_like {
Expand Down Expand Up @@ -202,7 +203,7 @@ impl<'a> FnLikeNode<'a> {
fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
I: FnOnce(ItemFnParts<'a>) -> A,
M: FnOnce(NodeId,
ast::Ident,
Ident,
&'a ast::MethodSig,
Option<ast::Visibility>,
&'a ast::Block,
Expand All @@ -211,7 +212,7 @@ impl<'a> FnLikeNode<'a> {
C: FnOnce(ClosureParts<'a>) -> A,
{
match self.node {
ast_map::NodeItem(i) => match i.node {
map::NodeItem(i) => match i.node {
ast::ItemFn(ref decl, unsafety, constness, abi, ref generics, ref block) =>
item_fn(ItemFnParts {
id: i.id,
Expand All @@ -227,13 +228,13 @@ impl<'a> FnLikeNode<'a> {
}),
_ => panic!("item FnLikeNode that is not fn-like"),
},
ast_map::NodeTraitItem(ti) => match ti.node {
map::NodeTraitItem(ti) => match ti.node {
ast::MethodTraitItem(ref sig, Some(ref body)) => {
method(ti.id, ti.ident, sig, None, body, ti.span)
}
_ => panic!("trait method FnLikeNode that is not fn-like"),
},
ast_map::NodeImplItem(ii) => {
map::NodeImplItem(ii) => {
match ii.node {
ast::MethodImplItem(ref sig, ref body) => {
method(ii.id, ii.ident, sig, Some(ii.vis), body, ii.span)
Expand All @@ -243,7 +244,7 @@ impl<'a> FnLikeNode<'a> {
}
}
}
ast_map::NodeExpr(e) => match e.node {
map::NodeExpr(e) => match e.node {
ast::ExprClosure(_, ref decl, ref block) =>
closure(ClosureParts::new(&**decl, &**block, e.id, e.span)),
_ => panic!("expr FnLikeNode that is not fn-like"),
Expand Down
50 changes: 18 additions & 32 deletions src/librustc/ast_map/mod.rs → src/librustc/front/map/mod.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-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -15,14 +15,17 @@ use self::MapEntry::*;
use metadata::inline::InlinedItem;
use metadata::inline::InlinedItem as II;
use middle::def_id::DefId;

use syntax::abi;
use syntax::ast::*;
use syntax::ast_util;
use syntax::codemap::{DUMMY_SP, Span, Spanned};
use syntax::fold::Folder;
use syntax::ast::{Name, NodeId, Ident, CRATE_NODE_ID, DUMMY_NODE_ID};
use syntax::codemap::{Span, Spanned};
use syntax::parse::token;
use syntax::print::pprust;
use syntax::visit::{self, Visitor};

use rustc_front::hir::*;
use rustc_front::fold::Folder;
use rustc_front::visit::{self, Visitor};
use rustc_front::util;
use rustc_front::print::pprust;

use arena::TypedArena;
use std::cell::RefCell;
Expand Down Expand Up @@ -159,7 +162,7 @@ impl<'ast> Clone for MapEntry<'ast> {
}

#[derive(Debug)]
struct InlinedParent {
pub struct InlinedParent {
path: Vec<PathElem>,
ii: InlinedItem
}
Expand Down Expand Up @@ -227,7 +230,7 @@ impl<'ast> MapEntry<'ast> {

/// Stores a crate and any number of inlined items from other crates.
pub struct Forest {
krate: Crate,
pub krate: Crate,
inlined_items: TypedArena<InlinedParent>
}

Expand All @@ -246,9 +249,10 @@ impl Forest {

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

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

fn visit_stmt(&mut self, stmt: &'ast Stmt) {
let id = ast_util::stmt_id(stmt);
let id = util::stmt_id(stmt);
self.insert(id, NodeStmt(stmt));
let parent_node = self.parent_node;
self.parent_node = id;
Expand Down Expand Up @@ -917,20 +921,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
}
}

pub fn map_crate<'ast, F: FoldOps>(forest: &'ast mut Forest, fold_ops: F) -> Map<'ast> {
// Replace the crate with an empty one to take it out.
let krate = mem::replace(&mut forest.krate, Crate {
module: Mod {
inner: DUMMY_SP,
items: vec![],
},
attrs: vec![],
config: vec![],
exported_macros: vec![],
span: DUMMY_SP
});
forest.krate = IdAndSpanUpdater { fold_ops: fold_ops }.fold_crate(krate);

pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
let mut collector = NodeCollector {
map: vec![],
parent_node: CRATE_NODE_ID,
Expand Down Expand Up @@ -974,11 +965,11 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
II::Item(i) => II::Item(fld.fold_item(i).expect_one("expected one item")),
II::TraitItem(d, ti) => {
II::TraitItem(fld.fold_ops.new_def_id(d),
fld.fold_trait_item(ti).expect_one("expected one trait item"))
fld.fold_trait_item(ti).expect_one("expected one trait item"))
}
II::ImplItem(d, ii) => {
II::ImplItem(fld.fold_ops.new_def_id(d),
fld.fold_impl_item(ii).expect_one("expected one impl item"))
fld.fold_impl_item(ii).expect_one("expected one impl item"))
}
II::Foreign(i) => II::Foreign(fld.fold_foreign_item(i))
};
Expand Down Expand Up @@ -1064,7 +1055,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
ItemTrait(..) => "trait",
ItemImpl(..) => "impl",
ItemDefaultImpl(..) => "default impl",
ItemMac(..) => "macro"
};
format!("{} {}{}", item_str, path_str, id_str)
}
Expand All @@ -1091,10 +1081,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
map.path_to_string(id),
id_str)
}
MacImplItem(ref mac) => {
format!("method macro {}{}",
pprust::mac_to_string(mac), id_str)
}
}
}
Some(NodeTraitItem(ti)) => {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ extern crate graphviz;
extern crate libc;
extern crate rustc_llvm;
extern crate rustc_back;
extern crate rustc_front;
extern crate rustc_data_structures;
extern crate serialize;
extern crate rbml;
Expand Down Expand Up @@ -101,7 +102,9 @@ pub mod back {
pub use rustc_back::svh;
}

pub mod ast_map;
pub mod front {
pub mod map;
}

pub mod middle {
pub mod astconv_util;
Expand Down
37 changes: 36 additions & 1 deletion src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ macro_rules! run_lints { ($cx:expr, $f:ident, $($args:expr),*) => ({

/// Parse the lint attributes into a vector, with `Err`s for malformed lint
/// attributes. Writing this as an iterator is an enormous mess.
// See also the hir version just below.
pub fn gather_attrs(attrs: &[ast::Attribute])
-> Vec<Result<(InternedString, Level, Span), Span>> {
let mut out = vec!();
Expand Down Expand Up @@ -312,6 +313,40 @@ pub fn gather_attrs(attrs: &[ast::Attribute])
}
out
}
// Copy-pasted from the above function :-(
pub fn gather_attrs_from_hir(attrs: &[::rustc_front::hir::Attribute])
-> Vec<Result<(InternedString, Level, Span), Span>> {
use ::rustc_front::attr::AttrMetaMethods;

let mut out = vec!();
for attr in attrs {
let level = match Level::from_str(&attr.name()) {
None => continue,
Some(lvl) => lvl,
};

::rustc_front::attr::mark_used(attr);

let meta = &attr.node.value;
let metas = match meta.node {
::rustc_front::hir::MetaList(_, ref metas) => metas,
_ => {
out.push(Err(meta.span));
continue;
}
};

for meta in metas {
out.push(match meta.node {
::rustc_front::hir::MetaWord(ref lint_name) => {
Ok((lint_name.clone(), level, meta.span))
}
_ => Err(meta.span),
});
}
}
out
}

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

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

// Visit the whole crate.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use syntax::visit::FnKind;
use syntax::ast;

pub use lint::context::{Context, LintStore, raw_emit_lint, check_crate, gather_attrs,
GatherNodeLevels};
gather_attrs_from_hir, GatherNodeLevels};

/// Specification of a single lint.
#[derive(Copy, Clone, Debug)]
Expand Down
Loading