Add a lowering context

This commit is contained in:
Nick Cameron 2015-09-25 16:03:28 +12:00
parent 11eda66df8
commit 56713a1684
6 changed files with 487 additions and 463 deletions

View File

@ -31,7 +31,7 @@ use rustc_trans::trans;
use rustc_typeck as typeck;
use rustc_privacy;
use rustc_front::hir;
use rustc_front::lowering::lower_crate;
use rustc_front::lowering::{lower_crate, LoweringContext};
use super::Compilation;
use serialize::json;
@ -112,9 +112,11 @@ pub fn compile_input(sess: Session,
let expanded_crate = assign_node_ids(&sess, expanded_crate);
// Lower ast -> hir.
let foo = &42;
let lcx = LoweringContext::new(foo);
let mut hir_forest = time(sess.time_passes(),
"lowering ast -> hir",
|| hir_map::Forest::new(lower_crate(&expanded_crate)));
|| hir_map::Forest::new(lower_crate(&lcx, &expanded_crate)));
let arenas = ty::CtxtArenas::new();
let ast_map = make_map(&sess, &mut hir_forest);
@ -128,7 +130,8 @@ pub fn compile_input(sess: Session,
&ast_map,
&expanded_crate,
&ast_map.krate(),
&id[..]));
&id[..],
&lcx));
time(sess.time_passes(), "attribute checking", || {
front::check_attr::check_crate(&sess, &expanded_crate);
@ -152,7 +155,8 @@ pub fn compile_input(sess: Session,
&expanded_crate,
tcx.map.krate(),
&analysis,
tcx);
tcx,
&lcx);
(control.after_analysis.callback)(state);
tcx.sess.abort_if_errors();
@ -278,6 +282,7 @@ pub struct CompileState<'a, 'ast: 'a, 'tcx: 'a> {
pub ast_map: Option<&'a hir_map::Map<'ast>>,
pub analysis: Option<&'a ty::CrateAnalysis>,
pub tcx: Option<&'a ty::ctxt<'tcx>>,
pub lcx: Option<&'a LoweringContext<'tcx>>,
pub trans: Option<&'a trans::CrateTranslation>,
}
@ -299,6 +304,7 @@ impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
ast_map: None,
analysis: None,
tcx: None,
lcx: None,
trans: None,
}
}
@ -333,13 +339,15 @@ impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
ast_map: &'a hir_map::Map<'ast>,
krate: &'a ast::Crate,
hir_crate: &'a hir::Crate,
crate_name: &'a str)
crate_name: &'a str,
lcx: &'a LoweringContext<'tcx>)
-> CompileState<'a, 'ast, 'tcx> {
CompileState {
crate_name: Some(crate_name),
ast_map: Some(ast_map),
krate: Some(krate),
hir_crate: Some(hir_crate),
lcx: Some(lcx),
.. CompileState::empty(input, session, out_dir)
}
}
@ -350,13 +358,15 @@ impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
krate: &'a ast::Crate,
hir_crate: &'a hir::Crate,
analysis: &'a ty::CrateAnalysis,
tcx: &'a ty::ctxt<'tcx>)
tcx: &'a ty::ctxt<'tcx>,
lcx: &'a LoweringContext<'tcx>)
-> CompileState<'a, 'ast, 'tcx> {
CompileState {
analysis: Some(analysis),
tcx: Some(tcx),
krate: Some(krate),
hir_crate: Some(hir_crate),
lcx: Some(lcx),
.. CompileState::empty(input, session, out_dir)
}
}

View File

@ -396,6 +396,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
time(state.session.time_passes(),
"save analysis",
|| save::process_crate(state.tcx.unwrap(),
state.lcx.unwrap(),
state.krate.unwrap(),
state.analysis.unwrap(),
state.out_dir));

View File

@ -47,7 +47,7 @@ use std::str::FromStr;
use rustc::front::map as hir_map;
use rustc::front::map::{blocks, NodePrinter};
use rustc_front::hir;
use rustc_front::lowering::lower_crate;
use rustc_front::lowering::{lower_crate, LoweringContext};
use rustc_front::print::pprust as pprust_hir;
#[derive(Copy, Clone, PartialEq, Debug)]
@ -670,9 +670,11 @@ pub fn pretty_print_input(sess: Session,
// There is some twisted, god-forsaken tangle of lifetimes here which makes
// the ordering of stuff super-finicky.
let mut hir_forest;
let foo = &42;
let lcx = LoweringContext::new(foo);
let arenas = ty::CtxtArenas::new();
let ast_map = if compute_ast_map {
hir_forest = hir_map::Forest::new(lower_crate(&krate));
hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate));
let map = driver::make_map(&sess, &mut hir_forest);
Some(map)
} else {

File diff suppressed because it is too large Load Diff

View File

@ -47,7 +47,7 @@ use syntax::visit::{self, Visitor};
use syntax::print::pprust::{path_to_string, ty_to_string};
use syntax::ptr::P;
use rustc_front::lowering::lower_expr;
use rustc_front::lowering::{lower_expr, LoweringContext};
use super::span_utils::SpanUtils;
use super::recorder::{Recorder, FmtStrs};
@ -76,6 +76,7 @@ pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
pub fn new(tcx: &'l ty::ctxt<'tcx>,
lcx: &'l LoweringContext<'tcx>,
analysis: &'l ty::CrateAnalysis,
output_file: Box<File>)
-> DumpCsvVisitor<'l, 'tcx> {
@ -83,7 +84,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
DumpCsvVisitor {
sess: &tcx.sess,
tcx: tcx,
save_ctxt: SaveContext::from_span_utils(tcx, span_utils.clone()),
save_ctxt: SaveContext::from_span_utils(tcx, lcx, span_utils.clone()),
analysis: analysis,
span: span_utils.clone(),
fmt: FmtStrs::new(box Recorder {
@ -1035,7 +1036,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
visit::walk_expr(self, ex);
}
ast::ExprStruct(ref path, ref fields, ref base) => {
let hir_expr = lower_expr(ex);
let hir_expr = lower_expr(self.save_ctxt.lcx, ex);
let adt = self.tcx.expr_ty(&hir_expr).ty_adt_def().unwrap();
let def = self.tcx.resolve_expr(&hir_expr);
self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
@ -1064,7 +1065,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
self.visit_expr(&**sub_ex);
let hir_node = lower_expr(sub_ex);
let hir_node = lower_expr(self.save_ctxt.lcx, sub_ex);
let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
match *ty {
ty::TyStruct(def, _) => {

View File

@ -38,6 +38,7 @@ mod dump_csv;
pub struct SaveContext<'l, 'tcx: 'l> {
tcx: &'l ty::ctxt<'tcx>,
lcx: &'l lowering::LoweringContext<'tcx>,
span_utils: SpanUtils<'l>,
}
@ -176,16 +177,18 @@ pub struct MethodCallData {
impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
pub fn new(tcx: &'l ty::ctxt<'tcx>) -> SaveContext<'l, 'tcx> {
pub fn new(tcx: &'l ty::ctxt<'tcx>, lcx: &'l lowering::LoweringContext<'tcx>) -> SaveContext<'l, 'tcx> {
let span_utils = SpanUtils::new(&tcx.sess);
SaveContext::from_span_utils(tcx, span_utils)
SaveContext::from_span_utils(tcx, lcx, span_utils)
}
pub fn from_span_utils(tcx: &'l ty::ctxt<'tcx>,
lcx: &'l lowering::LoweringContext<'tcx>,
span_utils: SpanUtils<'l>)
-> SaveContext<'l, 'tcx> {
SaveContext {
tcx: tcx,
lcx: lcx,
span_utils: span_utils,
}
}
@ -454,7 +457,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
pub fn get_expr_data(&self, expr: &ast::Expr) -> Option<Data> {
match expr.node {
ast::ExprField(ref sub_ex, ident) => {
let hir_node = lowering::lower_expr(sub_ex);
let hir_node = lowering::lower_expr(self.lcx, sub_ex);
let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
match *ty {
ty::TyStruct(def, _) => {
@ -474,7 +477,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
}
ast::ExprStruct(ref path, _, _) => {
let hir_node = lowering::lower_expr(expr);
let hir_node = lowering::lower_expr(self.lcx, expr);
let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
match *ty {
ty::TyStruct(def, _) => {
@ -705,10 +708,11 @@ impl<'v> Visitor<'v> for PathCollector {
}
}
pub fn process_crate(tcx: &ty::ctxt,
krate: &ast::Crate,
analysis: &ty::CrateAnalysis,
odir: Option<&Path>) {
pub fn process_crate<'l, 'tcx>(tcx: &'l ty::ctxt<'tcx>,
lcx: &'l lowering::LoweringContext<'tcx>,
krate: &ast::Crate,
analysis: &ty::CrateAnalysis,
odir: Option<&Path>) {
if generated_code(krate.span) {
return;
}
@ -757,7 +761,7 @@ pub fn process_crate(tcx: &ty::ctxt,
};
root_path.pop();
let mut visitor = dump_csv::DumpCsvVisitor::new(tcx, analysis, output_file);
let mut visitor = dump_csv::DumpCsvVisitor::new(tcx, lcx, analysis, output_file);
visitor.dump_crate_info(&cratename, krate);
visit::walk_crate(&mut visitor, krate);