From e14d053a509c31c6859ce630427c9053cb1bb261 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 13 Jan 2015 21:16:56 -0800 Subject: [PATCH] syntax: factor out print_expr arms to reduce non-optimized stack usage --- src/libsyntax/print/pprust.rs | 216 ++++++++++++++++++++++------------ 1 file changed, 138 insertions(+), 78 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index cbf7e206987..17f6e36603f 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1511,108 +1511,168 @@ pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> IoResult<()> { Ok(()) } + fn print_expr_box(&mut self, + place: &Option>, + expr: &ast::Expr) -> IoResult<()> { + try!(word(&mut self.s, "box")); + try!(word(&mut self.s, "(")); + try!(place.as_ref().map_or(Ok(()), |e|self.print_expr(&**e))); + try!(self.word_space(")")); + self.print_expr(expr) + } + + fn print_expr_vec(&mut self, exprs: &[P]) -> IoResult<()> { + try!(self.ibox(indent_unit)); + try!(word(&mut self.s, "[")); + try!(self.commasep_exprs(Inconsistent, &exprs[])); + try!(word(&mut self.s, "]")); + self.end() + } + + fn print_expr_repeat(&mut self, + element: &ast::Expr, + count: &ast::Expr) -> IoResult<()> { + try!(self.ibox(indent_unit)); + try!(word(&mut self.s, "[")); + try!(self.print_expr(element)); + try!(self.word_space(";")); + try!(self.print_expr(count)); + try!(word(&mut self.s, "]")); + self.end() + } + + fn print_expr_struct(&mut self, + path: &ast::Path, + fields: &[ast::Field], + wth: &Option>) -> IoResult<()> { + try!(self.print_path(path, true)); + if !(fields.is_empty() && wth.is_none()) { + try!(word(&mut self.s, "{")); + try!(self.commasep_cmnt( + Consistent, + &fields[], + |s, field| { + try!(s.ibox(indent_unit)); + try!(s.print_ident(field.ident.node)); + try!(s.word_space(":")); + try!(s.print_expr(&*field.expr)); + s.end() + }, + |f| f.span)); + match *wth { + Some(ref expr) => { + try!(self.ibox(indent_unit)); + if !fields.is_empty() { + try!(word(&mut self.s, ",")); + try!(space(&mut self.s)); + } + try!(word(&mut self.s, "..")); + try!(self.print_expr(&**expr)); + try!(self.end()); + } + _ => try!(word(&mut self.s, ",")), + } + try!(word(&mut self.s, "}")); + } + Ok(()) + } + + fn print_expr_tup(&mut self, exprs: &[P]) -> IoResult<()> { + try!(self.popen()); + try!(self.commasep_exprs(Inconsistent, &exprs[])); + if exprs.len() == 1 { + try!(word(&mut self.s, ",")); + } + self.pclose() + } + + fn print_expr_call(&mut self, + func: &ast::Expr, + args: &[P]) -> IoResult<()> { + try!(self.print_expr_maybe_paren(func)); + self.print_call_post(args) + } + + fn print_expr_method_call(&mut self, + ident: ast::SpannedIdent, + tys: &[P], + args: &[P]) -> IoResult<()> { + let base_args = args.slice_from(1); + try!(self.print_expr(&*args[0])); + try!(word(&mut self.s, ".")); + try!(self.print_ident(ident.node)); + if tys.len() > 0u { + try!(word(&mut self.s, "::<")); + try!(self.commasep(Inconsistent, tys, + |s, ty| s.print_type(&**ty))); + try!(word(&mut self.s, ">")); + } + self.print_call_post(base_args) + } + + fn print_expr_binary(&mut self, + op: ast::BinOp, + lhs: &ast::Expr, + rhs: &ast::Expr) -> IoResult<()> { + try!(self.print_expr(lhs)); + try!(space(&mut self.s)); + try!(self.word_space(ast_util::binop_to_string(op))); + self.print_expr(rhs) + } + + fn print_expr_unary(&mut self, + op: ast::UnOp, + expr: &ast::Expr) -> IoResult<()> { + try!(word(&mut self.s, ast_util::unop_to_string(op))); + self.print_expr_maybe_paren(expr) + } + + fn print_expr_addr_of(&mut self, + mutability: ast::Mutability, + expr: &ast::Expr) -> IoResult<()> { + try!(word(&mut self.s, "&")); + try!(self.print_mutability(mutability)); + self.print_expr_maybe_paren(expr) + } + pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(self.maybe_print_comment(expr.span.lo)); try!(self.ibox(indent_unit)); try!(self.ann.pre(self, NodeExpr(expr))); match expr.node { - ast::ExprBox(ref p, ref e) => { - try!(word(&mut self.s, "box")); - try!(word(&mut self.s, "(")); - try!(p.as_ref().map_or(Ok(()), |e|self.print_expr(&**e))); - try!(self.word_space(")")); - try!(self.print_expr(&**e)); + ast::ExprBox(ref place, ref expr) => { + try!(self.print_expr_box(place, &**expr)); } ast::ExprVec(ref exprs) => { - try!(self.ibox(indent_unit)); - try!(word(&mut self.s, "[")); - try!(self.commasep_exprs(Inconsistent, &exprs[])); - try!(word(&mut self.s, "]")); - try!(self.end()); + try!(self.print_expr_vec(&exprs[])); } - ast::ExprRepeat(ref element, ref count) => { - try!(self.ibox(indent_unit)); - try!(word(&mut self.s, "[")); - try!(self.print_expr(&**element)); - try!(self.word_space(";")); - try!(self.print_expr(&**count)); - try!(word(&mut self.s, "]")); - try!(self.end()); + try!(self.print_expr_repeat(&**element, &**count)); } - ast::ExprStruct(ref path, ref fields, ref wth) => { - try!(self.print_path(path, true)); - if !(fields.is_empty() && wth.is_none()) { - try!(word(&mut self.s, "{")); - try!(self.commasep_cmnt( - Consistent, - &fields[], - |s, field| { - try!(s.ibox(indent_unit)); - try!(s.print_ident(field.ident.node)); - try!(s.word_space(":")); - try!(s.print_expr(&*field.expr)); - s.end() - }, - |f| f.span)); - match *wth { - Some(ref expr) => { - try!(self.ibox(indent_unit)); - if !fields.is_empty() { - try!(word(&mut self.s, ",")); - try!(space(&mut self.s)); - } - try!(word(&mut self.s, "..")); - try!(self.print_expr(&**expr)); - try!(self.end()); - } - _ => try!(word(&mut self.s, ",")), - } - try!(word(&mut self.s, "}")); - } + try!(self.print_expr_struct(path, &fields[], wth)); } ast::ExprTup(ref exprs) => { - try!(self.popen()); - try!(self.commasep_exprs(Inconsistent, &exprs[])); - if exprs.len() == 1 { - try!(word(&mut self.s, ",")); - } - try!(self.pclose()); + try!(self.print_expr_tup(&exprs[])); } ast::ExprCall(ref func, ref args) => { - try!(self.print_expr_maybe_paren(&**func)); - try!(self.print_call_post(&args[])); + try!(self.print_expr_call(&**func, &args[])); } ast::ExprMethodCall(ident, ref tys, ref args) => { - let base_args = args.slice_from(1); - try!(self.print_expr(&*args[0])); - try!(word(&mut self.s, ".")); - try!(self.print_ident(ident.node)); - if tys.len() > 0u { - try!(word(&mut self.s, "::<")); - try!(self.commasep(Inconsistent, &tys[], - |s, ty| s.print_type(&**ty))); - try!(word(&mut self.s, ">")); - } - try!(self.print_call_post(base_args)); + try!(self.print_expr_method_call(ident, &tys[], &args[])); } ast::ExprBinary(op, ref lhs, ref rhs) => { - try!(self.print_expr(&**lhs)); - try!(space(&mut self.s)); - try!(self.word_space(ast_util::binop_to_string(op))); - try!(self.print_expr(&**rhs)); + try!(self.print_expr_binary(op, &**lhs, &**rhs)); } ast::ExprUnary(op, ref expr) => { - try!(word(&mut self.s, ast_util::unop_to_string(op))); - try!(self.print_expr_maybe_paren(&**expr)); + try!(self.print_expr_unary(op, &**expr)); } ast::ExprAddrOf(m, ref expr) => { - try!(word(&mut self.s, "&")); - try!(self.print_mutability(m)); - try!(self.print_expr_maybe_paren(&**expr)); + try!(self.print_expr_addr_of(m, &**expr)); + } + ast::ExprLit(ref lit) => { + try!(self.print_literal(&**lit)); } - ast::ExprLit(ref lit) => try!(self.print_literal(&**lit)), ast::ExprCast(ref expr, ref ty) => { try!(self.print_expr(&**expr)); try!(space(&mut self.s)); -- 2.44.0