1
Fork 0

syntax: Don't parameterize the the pretty printer

The pretty printer constitues an enormous amount of code, there's no reason for
it to be generic. This just least to a huge amount of metadata which isn't
necessary. Instead, this change migrates the pretty printer to using a trait
object instead.

Closes #12985
This commit is contained in:
Alex Crichton 2014-03-17 22:27:37 -07:00
parent 92f0bc2935
commit 87c7c03f45
4 changed files with 26 additions and 25 deletions

View file

@ -596,7 +596,7 @@ struct IdentifiedAnnotation;
impl pprust::PpAnn for IdentifiedAnnotation { impl pprust::PpAnn for IdentifiedAnnotation {
fn pre(&self, fn pre(&self,
s: &mut pprust::State<IdentifiedAnnotation>, s: &mut pprust::State,
node: pprust::AnnNode) -> io::IoResult<()> { node: pprust::AnnNode) -> io::IoResult<()> {
match node { match node {
pprust::NodeExpr(_) => s.popen(), pprust::NodeExpr(_) => s.popen(),
@ -604,7 +604,7 @@ impl pprust::PpAnn for IdentifiedAnnotation {
} }
} }
fn post(&self, fn post(&self,
s: &mut pprust::State<IdentifiedAnnotation>, s: &mut pprust::State,
node: pprust::AnnNode) -> io::IoResult<()> { node: pprust::AnnNode) -> io::IoResult<()> {
match node { match node {
pprust::NodeItem(item) => { pprust::NodeItem(item) => {
@ -634,7 +634,7 @@ struct TypedAnnotation {
impl pprust::PpAnn for TypedAnnotation { impl pprust::PpAnn for TypedAnnotation {
fn pre(&self, fn pre(&self,
s: &mut pprust::State<TypedAnnotation>, s: &mut pprust::State,
node: pprust::AnnNode) -> io::IoResult<()> { node: pprust::AnnNode) -> io::IoResult<()> {
match node { match node {
pprust::NodeExpr(_) => s.popen(), pprust::NodeExpr(_) => s.popen(),
@ -642,7 +642,7 @@ impl pprust::PpAnn for TypedAnnotation {
} }
} }
fn post(&self, fn post(&self,
s: &mut pprust::State<TypedAnnotation>, s: &mut pprust::State,
node: pprust::AnnNode) -> io::IoResult<()> { node: pprust::AnnNode) -> io::IoResult<()> {
let tcx = &self.analysis.ty_cx; let tcx = &self.analysis.ty_cx;
match node { match node {

View file

@ -85,7 +85,7 @@ struct LoopScope<'a> {
impl<'a, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, O> { impl<'a, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, O> {
fn pre(&self, fn pre(&self,
ps: &mut pprust::State<DataFlowContext<'a, O>>, ps: &mut pprust::State,
node: pprust::AnnNode) -> io::IoResult<()> { node: pprust::AnnNode) -> io::IoResult<()> {
let id = match node { let id = match node {
pprust::NodeExpr(expr) => expr.id, pprust::NodeExpr(expr) => expr.id,

View file

@ -880,8 +880,8 @@ mod test {
use super::*; use super::*;
// this version doesn't care about getting comments or docstrings in. // this version doesn't care about getting comments or docstrings in.
fn fake_print_crate<A: pprust::PpAnn>(s: &mut pprust::State<A>, fn fake_print_crate(s: &mut pprust::State,
krate: &ast::Crate) -> io::IoResult<()> { krate: &ast::Crate) -> io::IoResult<()> {
s.print_mod(&krate.module, krate.attrs.as_slice()) s.print_mod(&krate.module, krate.attrs.as_slice())
} }

View file

@ -43,8 +43,8 @@ pub enum AnnNode<'a> {
} }
pub trait PpAnn { pub trait PpAnn {
fn pre(&self, _state: &mut State<Self>, _node: AnnNode) -> IoResult<()> { Ok(()) } fn pre(&self, _state: &mut State, _node: AnnNode) -> IoResult<()> { Ok(()) }
fn post(&self, _state: &mut State<Self>, _node: AnnNode) -> IoResult<()> { Ok(()) } fn post(&self, _state: &mut State, _node: AnnNode) -> IoResult<()> { Ok(()) }
} }
pub struct NoAnn; pub struct NoAnn;
@ -56,7 +56,7 @@ pub struct CurrentCommentAndLiteral {
cur_lit: uint, cur_lit: uint,
} }
pub struct State<'a, A> { pub struct State<'a> {
s: pp::Printer, s: pp::Printer,
cm: Option<&'a CodeMap>, cm: Option<&'a CodeMap>,
intr: @token::IdentInterner, intr: @token::IdentInterner,
@ -64,15 +64,16 @@ pub struct State<'a, A> {
literals: Option<Vec<comments::Literal> >, literals: Option<Vec<comments::Literal> >,
cur_cmnt_and_lit: CurrentCommentAndLiteral, cur_cmnt_and_lit: CurrentCommentAndLiteral,
boxes: RefCell<Vec<pp::Breaks> >, boxes: RefCell<Vec<pp::Breaks> >,
ann: &'a A ann: &'a PpAnn
} }
pub fn rust_printer(writer: ~io::Writer) -> State<'static, NoAnn> { pub fn rust_printer(writer: ~io::Writer) -> State<'static> {
static NO_ANN: NoAnn = NoAnn; static NO_ANN: NoAnn = NoAnn;
rust_printer_annotated(writer, &NO_ANN) rust_printer_annotated(writer, &NO_ANN)
} }
pub fn rust_printer_annotated<'a, A: PpAnn>(writer: ~io::Writer, ann: &'a A) -> State<'a, A> { pub fn rust_printer_annotated<'a>(writer: ~io::Writer,
ann: &'a PpAnn) -> State<'a> {
State { State {
s: pp::mk_printer(writer, default_columns), s: pp::mk_printer(writer, default_columns),
cm: None, cm: None,
@ -95,14 +96,14 @@ pub static default_columns: uint = 78u;
// Requires you to pass an input filename and reader so that // Requires you to pass an input filename and reader so that
// it can scan the input text for comments and literals to // it can scan the input text for comments and literals to
// copy forward. // copy forward.
pub fn print_crate<'a, A: PpAnn>(cm: &'a CodeMap, pub fn print_crate<'a>(cm: &'a CodeMap,
span_diagnostic: &diagnostic::SpanHandler, span_diagnostic: &diagnostic::SpanHandler,
krate: &ast::Crate, krate: &ast::Crate,
filename: ~str, filename: ~str,
input: &mut io::Reader, input: &mut io::Reader,
out: ~io::Writer, out: ~io::Writer,
ann: &'a A, ann: &'a PpAnn,
is_expanded: bool) -> IoResult<()> { is_expanded: bool) -> IoResult<()> {
let (cmnts, lits) = comments::gather_comments_and_literals( let (cmnts, lits) = comments::gather_comments_and_literals(
span_diagnostic, span_diagnostic,
filename, filename,
@ -133,7 +134,7 @@ pub fn print_crate<'a, A: PpAnn>(cm: &'a CodeMap,
eof(&mut s.s) eof(&mut s.s)
} }
pub fn to_str(f: |&mut State<NoAnn>| -> IoResult<()>) -> ~str { pub fn to_str(f: |&mut State| -> IoResult<()>) -> ~str {
let mut s = rust_printer(~MemWriter::new()); let mut s = rust_printer(~MemWriter::new());
f(&mut s).unwrap(); f(&mut s).unwrap();
eof(&mut s.s).unwrap(); eof(&mut s.s).unwrap();
@ -237,7 +238,7 @@ pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> ~str {
} }
} }
impl<'a, A: PpAnn> State<'a, A> { impl<'a> State<'a> {
pub fn ibox(&mut self, u: uint) -> IoResult<()> { pub fn ibox(&mut self, u: uint) -> IoResult<()> {
self.boxes.borrow_mut().get().push(pp::Inconsistent); self.boxes.borrow_mut().get().push(pp::Inconsistent);
pp::ibox(&mut self.s, u) pp::ibox(&mut self.s, u)
@ -365,7 +366,7 @@ impl<'a, A: PpAnn> State<'a, A> {
} }
pub fn commasep<T>(&mut self, b: Breaks, elts: &[T], pub fn commasep<T>(&mut self, b: Breaks, elts: &[T],
op: |&mut State<A>, &T| -> IoResult<()>) op: |&mut State, &T| -> IoResult<()>)
-> IoResult<()> { -> IoResult<()> {
try!(self.rbox(0u, b)); try!(self.rbox(0u, b));
let mut first = true; let mut first = true;
@ -381,7 +382,7 @@ impl<'a, A: PpAnn> State<'a, A> {
&mut self, &mut self,
b: Breaks, b: Breaks,
elts: &[T], elts: &[T],
op: |&mut State<A>, &T| -> IoResult<()>, op: |&mut State, &T| -> IoResult<()>,
get_span: |&T| -> codemap::Span) -> IoResult<()> { get_span: |&T| -> codemap::Span) -> IoResult<()> {
try!(self.rbox(0u, b)); try!(self.rbox(0u, b));
let len = elts.len(); let len = elts.len();