syntax: factor out print_expr arms to reduce non-optimized stack usage
This commit is contained in:
parent
caf4a90c66
commit
e14d053a50
1 changed files with 138 additions and 78 deletions
|
|
@ -1511,108 +1511,168 @@ impl<'a> State<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn print_expr_box(&mut self,
|
||||
place: &Option<P<ast::Expr>>,
|
||||
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<ast::Expr>]) -> 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<P<ast::Expr>>) -> 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<ast::Expr>]) -> 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<ast::Expr>]) -> 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<ast::Ty>],
|
||||
args: &[P<ast::Expr>]) -> 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));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue