]> git.lizzy.rs Git - rust.git/blob - src/librustc_front/print/pprust.rs
Rollup merge of #30368 - arielb1:region-unification-2, r=nikomatsakis
[rust.git] / src / librustc_front / print / pprust.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 pub use self::AnnNode::*;
12
13 use syntax::abi;
14 use syntax::ast;
15 use syntax::owned_slice::OwnedSlice;
16 use syntax::codemap::{self, CodeMap, BytePos, Spanned};
17 use syntax::diagnostic;
18 use syntax::parse::token::{self, BinOpToken};
19 use syntax::parse::lexer::comments;
20 use syntax::parse;
21 use syntax::print::pp::{self, break_offset, word, space, hardbreak};
22 use syntax::print::pp::{Breaks, eof};
23 use syntax::print::pp::Breaks::{Consistent, Inconsistent};
24 use syntax::print::pprust::{self as ast_pp, PrintState};
25 use syntax::ptr::P;
26
27 use hir;
28 use hir::{Crate, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
29
30 use std::io::{self, Write, Read};
31
32 pub enum AnnNode<'a> {
33     NodeName(&'a ast::Name),
34     NodeBlock(&'a hir::Block),
35     NodeItem(&'a hir::Item),
36     NodeSubItem(ast::NodeId),
37     NodeExpr(&'a hir::Expr),
38     NodePat(&'a hir::Pat),
39 }
40
41 pub trait PpAnn {
42     fn pre(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> {
43         Ok(())
44     }
45     fn post(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> {
46         Ok(())
47     }
48 }
49
50 #[derive(Copy, Clone)]
51 pub struct NoAnn;
52
53 impl PpAnn for NoAnn {}
54
55
56 pub struct State<'a> {
57     krate: Option<&'a Crate>,
58     pub s: pp::Printer<'a>,
59     cm: Option<&'a CodeMap>,
60     comments: Option<Vec<comments::Comment>>,
61     literals: Option<Vec<comments::Literal>>,
62     cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral,
63     boxes: Vec<pp::Breaks>,
64     ann: &'a (PpAnn + 'a),
65 }
66
67 impl<'a> PrintState<'a> for State<'a> {
68     fn writer(&mut self) -> &mut pp::Printer<'a> {
69         &mut self.s
70     }
71
72     fn boxes(&mut self) -> &mut Vec<pp::Breaks> {
73         &mut self.boxes
74     }
75
76     fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
77         &mut self.comments
78     }
79
80     fn cur_cmnt_and_lit(&mut self) -> &mut ast_pp::CurrentCommentAndLiteral {
81         &mut self.cur_cmnt_and_lit
82     }
83
84     fn literals(&self) -> &Option<Vec<comments::Literal>> {
85         &self.literals
86     }
87 }
88
89 pub fn rust_printer<'a>(writer: Box<Write + 'a>, krate: Option<&'a Crate>) -> State<'a> {
90     static NO_ANN: NoAnn = NoAnn;
91     rust_printer_annotated(writer, &NO_ANN, krate)
92 }
93
94 pub fn rust_printer_annotated<'a>(writer: Box<Write + 'a>,
95                                   ann: &'a PpAnn,
96                                   krate: Option<&'a Crate>)
97                                   -> State<'a> {
98     State {
99         krate: krate,
100         s: pp::mk_printer(writer, default_columns),
101         cm: None,
102         comments: None,
103         literals: None,
104         cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral {
105             cur_cmnt: 0,
106             cur_lit: 0,
107         },
108         boxes: Vec::new(),
109         ann: ann,
110     }
111 }
112
113 #[allow(non_upper_case_globals)]
114 pub const indent_unit: usize = 4;
115
116 #[allow(non_upper_case_globals)]
117 pub const default_columns: usize = 78;
118
119
120 /// Requires you to pass an input filename and reader so that
121 /// it can scan the input text for comments and literals to
122 /// copy forward.
123 pub fn print_crate<'a>(cm: &'a CodeMap,
124                        span_diagnostic: &diagnostic::SpanHandler,
125                        krate: &hir::Crate,
126                        filename: String,
127                        input: &mut Read,
128                        out: Box<Write + 'a>,
129                        ann: &'a PpAnn,
130                        is_expanded: bool)
131                        -> io::Result<()> {
132     let mut s = State::new_from_input(cm, span_diagnostic, filename, input,
133                                       out, ann, is_expanded, Some(krate));
134
135     // When printing the AST, we sometimes need to inject `#[no_std]` here.
136     // Since you can't compile the HIR, it's not necessary.
137
138     try!(s.print_mod(&krate.module, &krate.attrs));
139     try!(s.print_remaining_comments());
140     eof(&mut s.s)
141 }
142
143 impl<'a> State<'a> {
144     pub fn new_from_input(cm: &'a CodeMap,
145                           span_diagnostic: &diagnostic::SpanHandler,
146                           filename: String,
147                           input: &mut Read,
148                           out: Box<Write + 'a>,
149                           ann: &'a PpAnn,
150                           is_expanded: bool,
151                           krate: Option<&'a Crate>)
152                           -> State<'a> {
153         let (cmnts, lits) = comments::gather_comments_and_literals(span_diagnostic,
154                                                                    filename,
155                                                                    input);
156
157         State::new(cm,
158                    out,
159                    ann,
160                    Some(cmnts),
161                    // If the code is post expansion, don't use the table of
162                    // literals, since it doesn't correspond with the literals
163                    // in the AST anymore.
164                    if is_expanded {
165                        None
166                    } else {
167                        Some(lits)
168                    },
169                    krate)
170     }
171
172     pub fn new(cm: &'a CodeMap,
173                out: Box<Write + 'a>,
174                ann: &'a PpAnn,
175                comments: Option<Vec<comments::Comment>>,
176                literals: Option<Vec<comments::Literal>>,
177                krate: Option<&'a Crate>)
178                -> State<'a> {
179         State {
180             krate: krate,
181             s: pp::mk_printer(out, default_columns),
182             cm: Some(cm),
183             comments: comments.clone(),
184             literals: literals.clone(),
185             cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral {
186                 cur_cmnt: 0,
187                 cur_lit: 0,
188             },
189             boxes: Vec::new(),
190             ann: ann,
191         }
192     }
193 }
194
195 pub fn to_string<F>(f: F) -> String
196     where F: FnOnce(&mut State) -> io::Result<()>
197 {
198     let mut wr = Vec::new();
199     {
200         let mut printer = rust_printer(Box::new(&mut wr), None);
201         f(&mut printer).unwrap();
202         eof(&mut printer.s).unwrap();
203     }
204     String::from_utf8(wr).unwrap()
205 }
206
207 pub fn binop_to_string(op: BinOpToken) -> &'static str {
208     match op {
209         token::Plus => "+",
210         token::Minus => "-",
211         token::Star => "*",
212         token::Slash => "/",
213         token::Percent => "%",
214         token::Caret => "^",
215         token::And => "&",
216         token::Or => "|",
217         token::Shl => "<<",
218         token::Shr => ">>",
219     }
220 }
221
222 pub fn ty_to_string(ty: &hir::Ty) -> String {
223     to_string(|s| s.print_type(ty))
224 }
225
226 pub fn bounds_to_string(bounds: &[hir::TyParamBound]) -> String {
227     to_string(|s| s.print_bounds("", bounds))
228 }
229
230 pub fn pat_to_string(pat: &hir::Pat) -> String {
231     to_string(|s| s.print_pat(pat))
232 }
233
234 pub fn arm_to_string(arm: &hir::Arm) -> String {
235     to_string(|s| s.print_arm(arm))
236 }
237
238 pub fn expr_to_string(e: &hir::Expr) -> String {
239     to_string(|s| s.print_expr(e))
240 }
241
242 pub fn lifetime_to_string(e: &hir::Lifetime) -> String {
243     to_string(|s| s.print_lifetime(e))
244 }
245
246 pub fn stmt_to_string(stmt: &hir::Stmt) -> String {
247     to_string(|s| s.print_stmt(stmt))
248 }
249
250 pub fn item_to_string(i: &hir::Item) -> String {
251     to_string(|s| s.print_item(i))
252 }
253
254 pub fn impl_item_to_string(i: &hir::ImplItem) -> String {
255     to_string(|s| s.print_impl_item(i))
256 }
257
258 pub fn trait_item_to_string(i: &hir::TraitItem) -> String {
259     to_string(|s| s.print_trait_item(i))
260 }
261
262 pub fn generics_to_string(generics: &hir::Generics) -> String {
263     to_string(|s| s.print_generics(generics))
264 }
265
266 pub fn where_clause_to_string(i: &hir::WhereClause) -> String {
267     to_string(|s| s.print_where_clause(i))
268 }
269
270 pub fn fn_block_to_string(p: &hir::FnDecl) -> String {
271     to_string(|s| s.print_fn_block_args(p))
272 }
273
274 pub fn path_to_string(p: &hir::Path) -> String {
275     to_string(|s| s.print_path(p, false, 0))
276 }
277
278 pub fn name_to_string(name: ast::Name) -> String {
279     to_string(|s| s.print_name(name))
280 }
281
282 pub fn fun_to_string(decl: &hir::FnDecl,
283                      unsafety: hir::Unsafety,
284                      constness: hir::Constness,
285                      name: ast::Name,
286                      opt_explicit_self: Option<&hir::ExplicitSelf_>,
287                      generics: &hir::Generics)
288                      -> String {
289     to_string(|s| {
290         try!(s.head(""));
291         try!(s.print_fn(decl,
292                         unsafety,
293                         constness,
294                         abi::Rust,
295                         Some(name),
296                         generics,
297                         opt_explicit_self,
298                         hir::Inherited));
299         try!(s.end()); // Close the head box
300         s.end() // Close the outer box
301     })
302 }
303
304 pub fn block_to_string(blk: &hir::Block) -> String {
305     to_string(|s| {
306         // containing cbox, will be closed by print-block at }
307         try!(s.cbox(indent_unit));
308         // head-ibox, will be closed by print-block after {
309         try!(s.ibox(0));
310         s.print_block(blk)
311     })
312 }
313
314 pub fn explicit_self_to_string(explicit_self: &hir::ExplicitSelf_) -> String {
315     to_string(|s| s.print_explicit_self(explicit_self, hir::MutImmutable).map(|_| {}))
316 }
317
318 pub fn variant_to_string(var: &hir::Variant) -> String {
319     to_string(|s| s.print_variant(var))
320 }
321
322 pub fn arg_to_string(arg: &hir::Arg) -> String {
323     to_string(|s| s.print_arg(arg))
324 }
325
326 pub fn visibility_qualified(vis: hir::Visibility, s: &str) -> String {
327     match vis {
328         hir::Public => format!("pub {}", s),
329         hir::Inherited => s.to_string(),
330     }
331 }
332
333 fn needs_parentheses(expr: &hir::Expr) -> bool {
334     match expr.node {
335         hir::ExprAssign(..) |
336         hir::ExprBinary(..) |
337         hir::ExprClosure(..) |
338         hir::ExprAssignOp(..) |
339         hir::ExprCast(..) => true,
340         _ => false,
341     }
342 }
343
344 impl<'a> State<'a> {
345     pub fn cbox(&mut self, u: usize) -> io::Result<()> {
346         self.boxes.push(pp::Breaks::Consistent);
347         pp::cbox(&mut self.s, u)
348     }
349
350     pub fn nbsp(&mut self) -> io::Result<()> {
351         word(&mut self.s, " ")
352     }
353
354     pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
355         try!(word(&mut self.s, w));
356         self.nbsp()
357     }
358
359     pub fn head(&mut self, w: &str) -> io::Result<()> {
360         // outer-box is consistent
361         try!(self.cbox(indent_unit));
362         // head-box is inconsistent
363         try!(self.ibox(w.len() + 1));
364         // keyword that starts the head
365         if !w.is_empty() {
366             try!(self.word_nbsp(w));
367         }
368         Ok(())
369     }
370
371     pub fn bopen(&mut self) -> io::Result<()> {
372         try!(word(&mut self.s, "{"));
373         self.end() // close the head-box
374     }
375
376     pub fn bclose_(&mut self, span: codemap::Span, indented: usize) -> io::Result<()> {
377         self.bclose_maybe_open(span, indented, true)
378     }
379     pub fn bclose_maybe_open(&mut self,
380                              span: codemap::Span,
381                              indented: usize,
382                              close_box: bool)
383                              -> io::Result<()> {
384         try!(self.maybe_print_comment(span.hi));
385         try!(self.break_offset_if_not_bol(1, -(indented as isize)));
386         try!(word(&mut self.s, "}"));
387         if close_box {
388             try!(self.end()); // close the outer-box
389         }
390         Ok(())
391     }
392     pub fn bclose(&mut self, span: codemap::Span) -> io::Result<()> {
393         self.bclose_(span, indent_unit)
394     }
395
396     pub fn in_cbox(&self) -> bool {
397         match self.boxes.last() {
398             Some(&last_box) => last_box == pp::Breaks::Consistent,
399             None => false,
400         }
401     }
402     pub fn space_if_not_bol(&mut self) -> io::Result<()> {
403         if !self.is_bol() {
404             try!(space(&mut self.s));
405         }
406         Ok(())
407     }
408     pub fn break_offset_if_not_bol(&mut self, n: usize, off: isize) -> io::Result<()> {
409         if !self.is_bol() {
410             break_offset(&mut self.s, n, off)
411         } else {
412             if off != 0 && self.s.last_token().is_hardbreak_tok() {
413                 // We do something pretty sketchy here: tuck the nonzero
414                 // offset-adjustment we were going to deposit along with the
415                 // break into the previous hardbreak.
416                 self.s.replace_last_token(pp::hardbreak_tok_offset(off));
417             }
418             Ok(())
419         }
420     }
421
422     // Synthesizes a comment that was not textually present in the original source
423     // file.
424     pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
425         try!(word(&mut self.s, "/*"));
426         try!(space(&mut self.s));
427         try!(word(&mut self.s, &text[..]));
428         try!(space(&mut self.s));
429         word(&mut self.s, "*/")
430     }
431
432
433     pub fn commasep_cmnt<T, F, G>(&mut self,
434                                   b: Breaks,
435                                   elts: &[T],
436                                   mut op: F,
437                                   mut get_span: G)
438                                   -> io::Result<()>
439         where F: FnMut(&mut State, &T) -> io::Result<()>,
440               G: FnMut(&T) -> codemap::Span
441     {
442         try!(self.rbox(0, b));
443         let len = elts.len();
444         let mut i = 0;
445         for elt in elts {
446             try!(self.maybe_print_comment(get_span(elt).hi));
447             try!(op(self, elt));
448             i += 1;
449             if i < len {
450                 try!(word(&mut self.s, ","));
451                 try!(self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi)));
452                 try!(self.space_if_not_bol());
453             }
454         }
455         self.end()
456     }
457
458     pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[P<hir::Expr>]) -> io::Result<()> {
459         self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span)
460     }
461
462     pub fn print_mod(&mut self, _mod: &hir::Mod, attrs: &[ast::Attribute]) -> io::Result<()> {
463         try!(self.print_inner_attributes(attrs));
464         for item_id in &_mod.item_ids {
465             try!(self.print_item_id(item_id));
466         }
467         Ok(())
468     }
469
470     pub fn print_foreign_mod(&mut self,
471                              nmod: &hir::ForeignMod,
472                              attrs: &[ast::Attribute])
473                              -> io::Result<()> {
474         try!(self.print_inner_attributes(attrs));
475         for item in &nmod.items {
476             try!(self.print_foreign_item(item));
477         }
478         Ok(())
479     }
480
481     pub fn print_opt_lifetime(&mut self, lifetime: &Option<hir::Lifetime>) -> io::Result<()> {
482         if let Some(l) = *lifetime {
483             try!(self.print_lifetime(&l));
484             try!(self.nbsp());
485         }
486         Ok(())
487     }
488
489     pub fn print_type(&mut self, ty: &hir::Ty) -> io::Result<()> {
490         try!(self.maybe_print_comment(ty.span.lo));
491         try!(self.ibox(0));
492         match ty.node {
493             hir::TyVec(ref ty) => {
494                 try!(word(&mut self.s, "["));
495                 try!(self.print_type(&**ty));
496                 try!(word(&mut self.s, "]"));
497             }
498             hir::TyPtr(ref mt) => {
499                 try!(word(&mut self.s, "*"));
500                 match mt.mutbl {
501                     hir::MutMutable => try!(self.word_nbsp("mut")),
502                     hir::MutImmutable => try!(self.word_nbsp("const")),
503                 }
504                 try!(self.print_type(&*mt.ty));
505             }
506             hir::TyRptr(ref lifetime, ref mt) => {
507                 try!(word(&mut self.s, "&"));
508                 try!(self.print_opt_lifetime(lifetime));
509                 try!(self.print_mt(mt));
510             }
511             hir::TyTup(ref elts) => {
512                 try!(self.popen());
513                 try!(self.commasep(Inconsistent, &elts[..], |s, ty| s.print_type(&**ty)));
514                 if elts.len() == 1 {
515                     try!(word(&mut self.s, ","));
516                 }
517                 try!(self.pclose());
518             }
519             hir::TyBareFn(ref f) => {
520                 let generics = hir::Generics {
521                     lifetimes: f.lifetimes.clone(),
522                     ty_params: OwnedSlice::empty(),
523                     where_clause: hir::WhereClause {
524                         id: ast::DUMMY_NODE_ID,
525                         predicates: Vec::new(),
526                     },
527                 };
528                 try!(self.print_ty_fn(f.abi, f.unsafety, &*f.decl, None, &generics, None));
529             }
530             hir::TyPath(None, ref path) => {
531                 try!(self.print_path(path, false, 0));
532             }
533             hir::TyPath(Some(ref qself), ref path) => {
534                 try!(self.print_qpath(path, qself, false))
535             }
536             hir::TyObjectSum(ref ty, ref bounds) => {
537                 try!(self.print_type(&**ty));
538                 try!(self.print_bounds("+", &bounds[..]));
539             }
540             hir::TyPolyTraitRef(ref bounds) => {
541                 try!(self.print_bounds("", &bounds[..]));
542             }
543             hir::TyFixedLengthVec(ref ty, ref v) => {
544                 try!(word(&mut self.s, "["));
545                 try!(self.print_type(&**ty));
546                 try!(word(&mut self.s, "; "));
547                 try!(self.print_expr(&**v));
548                 try!(word(&mut self.s, "]"));
549             }
550             hir::TyTypeof(ref e) => {
551                 try!(word(&mut self.s, "typeof("));
552                 try!(self.print_expr(&**e));
553                 try!(word(&mut self.s, ")"));
554             }
555             hir::TyInfer => {
556                 try!(word(&mut self.s, "_"));
557             }
558         }
559         self.end()
560     }
561
562     pub fn print_foreign_item(&mut self, item: &hir::ForeignItem) -> io::Result<()> {
563         try!(self.hardbreak_if_not_bol());
564         try!(self.maybe_print_comment(item.span.lo));
565         try!(self.print_outer_attributes(&item.attrs));
566         match item.node {
567             hir::ForeignItemFn(ref decl, ref generics) => {
568                 try!(self.head(""));
569                 try!(self.print_fn(decl,
570                                    hir::Unsafety::Normal,
571                                    hir::Constness::NotConst,
572                                    abi::Rust,
573                                    Some(item.name),
574                                    generics,
575                                    None,
576                                    item.vis));
577                 try!(self.end()); // end head-ibox
578                 try!(word(&mut self.s, ";"));
579                 self.end() // end the outer fn box
580             }
581             hir::ForeignItemStatic(ref t, m) => {
582                 try!(self.head(&visibility_qualified(item.vis, "static")));
583                 if m {
584                     try!(self.word_space("mut"));
585                 }
586                 try!(self.print_name(item.name));
587                 try!(self.word_space(":"));
588                 try!(self.print_type(&**t));
589                 try!(word(&mut self.s, ";"));
590                 try!(self.end()); // end the head-ibox
591                 self.end() // end the outer cbox
592             }
593         }
594     }
595
596     fn print_associated_const(&mut self,
597                               name: ast::Name,
598                               ty: &hir::Ty,
599                               default: Option<&hir::Expr>,
600                               vis: hir::Visibility)
601                               -> io::Result<()> {
602         try!(word(&mut self.s, &visibility_qualified(vis, "")));
603         try!(self.word_space("const"));
604         try!(self.print_name(name));
605         try!(self.word_space(":"));
606         try!(self.print_type(ty));
607         if let Some(expr) = default {
608             try!(space(&mut self.s));
609             try!(self.word_space("="));
610             try!(self.print_expr(expr));
611         }
612         word(&mut self.s, ";")
613     }
614
615     fn print_associated_type(&mut self,
616                              name: ast::Name,
617                              bounds: Option<&hir::TyParamBounds>,
618                              ty: Option<&hir::Ty>)
619                              -> io::Result<()> {
620         try!(self.word_space("type"));
621         try!(self.print_name(name));
622         if let Some(bounds) = bounds {
623             try!(self.print_bounds(":", bounds));
624         }
625         if let Some(ty) = ty {
626             try!(space(&mut self.s));
627             try!(self.word_space("="));
628             try!(self.print_type(ty));
629         }
630         word(&mut self.s, ";")
631     }
632
633     pub fn print_item_id(&mut self, item_id: &hir::ItemId) -> io::Result<()> {
634         if let Some(krate) = self.krate {
635             // skip nested items if krate context was not provided
636             let item = &krate.items[&item_id.id];
637             self.print_item(item)
638         } else {
639             Ok(())
640         }
641     }
642
643     /// Pretty-print an item
644     pub fn print_item(&mut self, item: &hir::Item) -> io::Result<()> {
645         try!(self.hardbreak_if_not_bol());
646         try!(self.maybe_print_comment(item.span.lo));
647         try!(self.print_outer_attributes(&item.attrs));
648         try!(self.ann.pre(self, NodeItem(item)));
649         match item.node {
650             hir::ItemExternCrate(ref optional_path) => {
651                 try!(self.head(&visibility_qualified(item.vis, "extern crate")));
652                 if let Some(p) = *optional_path {
653                     let val = p.as_str();
654                     if val.contains("-") {
655                         try!(self.print_string(&val, ast::CookedStr));
656                     } else {
657                         try!(self.print_name(p));
658                     }
659                     try!(space(&mut self.s));
660                     try!(word(&mut self.s, "as"));
661                     try!(space(&mut self.s));
662                 }
663                 try!(self.print_name(item.name));
664                 try!(word(&mut self.s, ";"));
665                 try!(self.end()); // end inner head-block
666                 try!(self.end()); // end outer head-block
667             }
668             hir::ItemUse(ref vp) => {
669                 try!(self.head(&visibility_qualified(item.vis, "use")));
670                 try!(self.print_view_path(&**vp));
671                 try!(word(&mut self.s, ";"));
672                 try!(self.end()); // end inner head-block
673                 try!(self.end()); // end outer head-block
674             }
675             hir::ItemStatic(ref ty, m, ref expr) => {
676                 try!(self.head(&visibility_qualified(item.vis, "static")));
677                 if m == hir::MutMutable {
678                     try!(self.word_space("mut"));
679                 }
680                 try!(self.print_name(item.name));
681                 try!(self.word_space(":"));
682                 try!(self.print_type(&**ty));
683                 try!(space(&mut self.s));
684                 try!(self.end()); // end the head-ibox
685
686                 try!(self.word_space("="));
687                 try!(self.print_expr(&**expr));
688                 try!(word(&mut self.s, ";"));
689                 try!(self.end()); // end the outer cbox
690             }
691             hir::ItemConst(ref ty, ref expr) => {
692                 try!(self.head(&visibility_qualified(item.vis, "const")));
693                 try!(self.print_name(item.name));
694                 try!(self.word_space(":"));
695                 try!(self.print_type(&**ty));
696                 try!(space(&mut self.s));
697                 try!(self.end()); // end the head-ibox
698
699                 try!(self.word_space("="));
700                 try!(self.print_expr(&**expr));
701                 try!(word(&mut self.s, ";"));
702                 try!(self.end()); // end the outer cbox
703             }
704             hir::ItemFn(ref decl, unsafety, constness, abi, ref typarams, ref body) => {
705                 try!(self.head(""));
706                 try!(self.print_fn(decl,
707                                    unsafety,
708                                    constness,
709                                    abi,
710                                    Some(item.name),
711                                    typarams,
712                                    None,
713                                    item.vis));
714                 try!(word(&mut self.s, " "));
715                 try!(self.print_block_with_attrs(&**body, &item.attrs));
716             }
717             hir::ItemMod(ref _mod) => {
718                 try!(self.head(&visibility_qualified(item.vis, "mod")));
719                 try!(self.print_name(item.name));
720                 try!(self.nbsp());
721                 try!(self.bopen());
722                 try!(self.print_mod(_mod, &item.attrs));
723                 try!(self.bclose(item.span));
724             }
725             hir::ItemForeignMod(ref nmod) => {
726                 try!(self.head("extern"));
727                 try!(self.word_nbsp(&nmod.abi.to_string()));
728                 try!(self.bopen());
729                 try!(self.print_foreign_mod(nmod, &item.attrs));
730                 try!(self.bclose(item.span));
731             }
732             hir::ItemTy(ref ty, ref params) => {
733                 try!(self.ibox(indent_unit));
734                 try!(self.ibox(0));
735                 try!(self.word_nbsp(&visibility_qualified(item.vis, "type")));
736                 try!(self.print_name(item.name));
737                 try!(self.print_generics(params));
738                 try!(self.end()); // end the inner ibox
739
740                 try!(self.print_where_clause(&params.where_clause));
741                 try!(space(&mut self.s));
742                 try!(self.word_space("="));
743                 try!(self.print_type(&**ty));
744                 try!(word(&mut self.s, ";"));
745                 try!(self.end()); // end the outer ibox
746             }
747             hir::ItemEnum(ref enum_definition, ref params) => {
748                 try!(self.print_enum_def(enum_definition, params, item.name, item.span, item.vis));
749             }
750             hir::ItemStruct(ref struct_def, ref generics) => {
751                 try!(self.head(&visibility_qualified(item.vis, "struct")));
752                 try!(self.print_struct(struct_def, generics, item.name, item.span, true));
753             }
754
755             hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
756                 try!(self.head(""));
757                 try!(self.print_visibility(item.vis));
758                 try!(self.print_unsafety(unsafety));
759                 try!(self.word_nbsp("impl"));
760                 try!(self.print_trait_ref(trait_ref));
761                 try!(space(&mut self.s));
762                 try!(self.word_space("for"));
763                 try!(self.word_space(".."));
764                 try!(self.bopen());
765                 try!(self.bclose(item.span));
766             }
767             hir::ItemImpl(unsafety,
768                           polarity,
769                           ref generics,
770                           ref opt_trait,
771                           ref ty,
772                           ref impl_items) => {
773                 try!(self.head(""));
774                 try!(self.print_visibility(item.vis));
775                 try!(self.print_unsafety(unsafety));
776                 try!(self.word_nbsp("impl"));
777
778                 if generics.is_parameterized() {
779                     try!(self.print_generics(generics));
780                     try!(space(&mut self.s));
781                 }
782
783                 match polarity {
784                     hir::ImplPolarity::Negative => {
785                         try!(word(&mut self.s, "!"));
786                     }
787                     _ => {}
788                 }
789
790                 match opt_trait {
791                     &Some(ref t) => {
792                         try!(self.print_trait_ref(t));
793                         try!(space(&mut self.s));
794                         try!(self.word_space("for"));
795                     }
796                     &None => {}
797                 }
798
799                 try!(self.print_type(&**ty));
800                 try!(self.print_where_clause(&generics.where_clause));
801
802                 try!(space(&mut self.s));
803                 try!(self.bopen());
804                 try!(self.print_inner_attributes(&item.attrs));
805                 for impl_item in impl_items {
806                     try!(self.print_impl_item(impl_item));
807                 }
808                 try!(self.bclose(item.span));
809             }
810             hir::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
811                 try!(self.head(""));
812                 try!(self.print_visibility(item.vis));
813                 try!(self.print_unsafety(unsafety));
814                 try!(self.word_nbsp("trait"));
815                 try!(self.print_name(item.name));
816                 try!(self.print_generics(generics));
817                 let mut real_bounds = Vec::with_capacity(bounds.len());
818                 for b in bounds.iter() {
819                     if let TraitTyParamBound(ref ptr, hir::TraitBoundModifier::Maybe) = *b {
820                         try!(space(&mut self.s));
821                         try!(self.word_space("for ?"));
822                         try!(self.print_trait_ref(&ptr.trait_ref));
823                     } else {
824                         real_bounds.push(b.clone());
825                     }
826                 }
827                 try!(self.print_bounds(":", &real_bounds[..]));
828                 try!(self.print_where_clause(&generics.where_clause));
829                 try!(word(&mut self.s, " "));
830                 try!(self.bopen());
831                 for trait_item in trait_items {
832                     try!(self.print_trait_item(trait_item));
833                 }
834                 try!(self.bclose(item.span));
835             }
836         }
837         self.ann.post(self, NodeItem(item))
838     }
839
840     fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
841         self.print_path(&t.path, false, 0)
842     }
843
844     fn print_formal_lifetime_list(&mut self, lifetimes: &[hir::LifetimeDef]) -> io::Result<()> {
845         if !lifetimes.is_empty() {
846             try!(word(&mut self.s, "for<"));
847             let mut comma = false;
848             for lifetime_def in lifetimes {
849                 if comma {
850                     try!(self.word_space(","))
851                 }
852                 try!(self.print_lifetime_def(lifetime_def));
853                 comma = true;
854             }
855             try!(word(&mut self.s, ">"));
856         }
857         Ok(())
858     }
859
860     fn print_poly_trait_ref(&mut self, t: &hir::PolyTraitRef) -> io::Result<()> {
861         try!(self.print_formal_lifetime_list(&t.bound_lifetimes));
862         self.print_trait_ref(&t.trait_ref)
863     }
864
865     pub fn print_enum_def(&mut self,
866                           enum_definition: &hir::EnumDef,
867                           generics: &hir::Generics,
868                           name: ast::Name,
869                           span: codemap::Span,
870                           visibility: hir::Visibility)
871                           -> io::Result<()> {
872         try!(self.head(&visibility_qualified(visibility, "enum")));
873         try!(self.print_name(name));
874         try!(self.print_generics(generics));
875         try!(self.print_where_clause(&generics.where_clause));
876         try!(space(&mut self.s));
877         self.print_variants(&enum_definition.variants, span)
878     }
879
880     pub fn print_variants(&mut self,
881                           variants: &[hir::Variant],
882                           span: codemap::Span)
883                           -> io::Result<()> {
884         try!(self.bopen());
885         for v in variants {
886             try!(self.space_if_not_bol());
887             try!(self.maybe_print_comment(v.span.lo));
888             try!(self.print_outer_attributes(&v.node.attrs));
889             try!(self.ibox(indent_unit));
890             try!(self.print_variant(v));
891             try!(word(&mut self.s, ","));
892             try!(self.end());
893             try!(self.maybe_print_trailing_comment(v.span, None));
894         }
895         self.bclose(span)
896     }
897
898     pub fn print_visibility(&mut self, vis: hir::Visibility) -> io::Result<()> {
899         match vis {
900             hir::Public => self.word_nbsp("pub"),
901             hir::Inherited => Ok(()),
902         }
903     }
904
905     pub fn print_struct(&mut self,
906                         struct_def: &hir::VariantData,
907                         generics: &hir::Generics,
908                         name: ast::Name,
909                         span: codemap::Span,
910                         print_finalizer: bool)
911                         -> io::Result<()> {
912         try!(self.print_name(name));
913         try!(self.print_generics(generics));
914         if !struct_def.is_struct() {
915             if struct_def.is_tuple() {
916                 try!(self.popen());
917                 try!(self.commasep(Inconsistent, struct_def.fields(), |s, field| {
918                     match field.node.kind {
919                         hir::NamedField(..) => panic!("unexpected named field"),
920                         hir::UnnamedField(vis) => {
921                             try!(s.print_visibility(vis));
922                             try!(s.maybe_print_comment(field.span.lo));
923                             s.print_type(&*field.node.ty)
924                         }
925                     }
926                 }));
927                 try!(self.pclose());
928             }
929             try!(self.print_where_clause(&generics.where_clause));
930             if print_finalizer {
931                 try!(word(&mut self.s, ";"));
932             }
933             try!(self.end());
934             self.end() // close the outer-box
935         } else {
936             try!(self.print_where_clause(&generics.where_clause));
937             try!(self.nbsp());
938             try!(self.bopen());
939             try!(self.hardbreak_if_not_bol());
940
941             for field in struct_def.fields() {
942                 match field.node.kind {
943                     hir::UnnamedField(..) => panic!("unexpected unnamed field"),
944                     hir::NamedField(name, visibility) => {
945                         try!(self.hardbreak_if_not_bol());
946                         try!(self.maybe_print_comment(field.span.lo));
947                         try!(self.print_outer_attributes(&field.node.attrs));
948                         try!(self.print_visibility(visibility));
949                         try!(self.print_name(name));
950                         try!(self.word_nbsp(":"));
951                         try!(self.print_type(&*field.node.ty));
952                         try!(word(&mut self.s, ","));
953                     }
954                 }
955             }
956
957             self.bclose(span)
958         }
959     }
960
961     pub fn print_variant(&mut self, v: &hir::Variant) -> io::Result<()> {
962         try!(self.head(""));
963         let generics = ::util::empty_generics();
964         try!(self.print_struct(&v.node.data, &generics, v.node.name, v.span, false));
965         match v.node.disr_expr {
966             Some(ref d) => {
967                 try!(space(&mut self.s));
968                 try!(self.word_space("="));
969                 self.print_expr(&**d)
970             }
971             _ => Ok(()),
972         }
973     }
974
975     pub fn print_method_sig(&mut self,
976                             name: ast::Name,
977                             m: &hir::MethodSig,
978                             vis: hir::Visibility)
979                             -> io::Result<()> {
980         self.print_fn(&m.decl,
981                       m.unsafety,
982                       m.constness,
983                       m.abi,
984                       Some(name),
985                       &m.generics,
986                       Some(&m.explicit_self.node),
987                       vis)
988     }
989
990     pub fn print_trait_item(&mut self, ti: &hir::TraitItem) -> io::Result<()> {
991         try!(self.ann.pre(self, NodeSubItem(ti.id)));
992         try!(self.hardbreak_if_not_bol());
993         try!(self.maybe_print_comment(ti.span.lo));
994         try!(self.print_outer_attributes(&ti.attrs));
995         match ti.node {
996             hir::ConstTraitItem(ref ty, ref default) => {
997                 try!(self.print_associated_const(ti.name,
998                                                  &ty,
999                                                  default.as_ref().map(|expr| &**expr),
1000                                                  hir::Inherited));
1001             }
1002             hir::MethodTraitItem(ref sig, ref body) => {
1003                 if body.is_some() {
1004                     try!(self.head(""));
1005                 }
1006                 try!(self.print_method_sig(ti.name, sig, hir::Inherited));
1007                 if let Some(ref body) = *body {
1008                     try!(self.nbsp());
1009                     try!(self.print_block_with_attrs(body, &ti.attrs));
1010                 } else {
1011                     try!(word(&mut self.s, ";"));
1012                 }
1013             }
1014             hir::TypeTraitItem(ref bounds, ref default) => {
1015                 try!(self.print_associated_type(ti.name,
1016                                                 Some(bounds),
1017                                                 default.as_ref().map(|ty| &**ty)));
1018             }
1019         }
1020         self.ann.post(self, NodeSubItem(ti.id))
1021     }
1022
1023     pub fn print_impl_item(&mut self, ii: &hir::ImplItem) -> io::Result<()> {
1024         try!(self.ann.pre(self, NodeSubItem(ii.id)));
1025         try!(self.hardbreak_if_not_bol());
1026         try!(self.maybe_print_comment(ii.span.lo));
1027         try!(self.print_outer_attributes(&ii.attrs));
1028         match ii.node {
1029             hir::ImplItemKind::Const(ref ty, ref expr) => {
1030                 try!(self.print_associated_const(ii.name, &ty, Some(&expr), ii.vis));
1031             }
1032             hir::ImplItemKind::Method(ref sig, ref body) => {
1033                 try!(self.head(""));
1034                 try!(self.print_method_sig(ii.name, sig, ii.vis));
1035                 try!(self.nbsp());
1036                 try!(self.print_block_with_attrs(body, &ii.attrs));
1037             }
1038             hir::ImplItemKind::Type(ref ty) => {
1039                 try!(self.print_associated_type(ii.name, None, Some(ty)));
1040             }
1041         }
1042         self.ann.post(self, NodeSubItem(ii.id))
1043     }
1044
1045     pub fn print_stmt(&mut self, st: &hir::Stmt) -> io::Result<()> {
1046         try!(self.maybe_print_comment(st.span.lo));
1047         match st.node {
1048             hir::StmtDecl(ref decl, _) => {
1049                 try!(self.print_decl(&**decl));
1050             }
1051             hir::StmtExpr(ref expr, _) => {
1052                 try!(self.space_if_not_bol());
1053                 try!(self.print_expr(&**expr));
1054             }
1055             hir::StmtSemi(ref expr, _) => {
1056                 try!(self.space_if_not_bol());
1057                 try!(self.print_expr(&**expr));
1058                 try!(word(&mut self.s, ";"));
1059             }
1060         }
1061         if stmt_ends_with_semi(&st.node) {
1062             try!(word(&mut self.s, ";"));
1063         }
1064         self.maybe_print_trailing_comment(st.span, None)
1065     }
1066
1067     pub fn print_block(&mut self, blk: &hir::Block) -> io::Result<()> {
1068         self.print_block_with_attrs(blk, &[])
1069     }
1070
1071     pub fn print_block_unclosed(&mut self, blk: &hir::Block) -> io::Result<()> {
1072         self.print_block_unclosed_indent(blk, indent_unit)
1073     }
1074
1075     pub fn print_block_unclosed_indent(&mut self,
1076                                        blk: &hir::Block,
1077                                        indented: usize)
1078                                        -> io::Result<()> {
1079         self.print_block_maybe_unclosed(blk, indented, &[], false)
1080     }
1081
1082     pub fn print_block_with_attrs(&mut self,
1083                                   blk: &hir::Block,
1084                                   attrs: &[ast::Attribute])
1085                                   -> io::Result<()> {
1086         self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
1087     }
1088
1089     pub fn print_block_maybe_unclosed(&mut self,
1090                                       blk: &hir::Block,
1091                                       indented: usize,
1092                                       attrs: &[ast::Attribute],
1093                                       close_box: bool)
1094                                       -> io::Result<()> {
1095         match blk.rules {
1096             hir::UnsafeBlock(..) => try!(self.word_space("unsafe")),
1097             hir::PushUnsafeBlock(..) => try!(self.word_space("push_unsafe")),
1098             hir::PopUnsafeBlock(..) => try!(self.word_space("pop_unsafe")),
1099             hir::PushUnstableBlock => try!(self.word_space("push_unstable")),
1100             hir::PopUnstableBlock => try!(self.word_space("pop_unstable")),
1101             hir::DefaultBlock => (),
1102         }
1103         try!(self.maybe_print_comment(blk.span.lo));
1104         try!(self.ann.pre(self, NodeBlock(blk)));
1105         try!(self.bopen());
1106
1107         try!(self.print_inner_attributes(attrs));
1108
1109         for st in &blk.stmts {
1110             try!(self.print_stmt(st));
1111         }
1112         match blk.expr {
1113             Some(ref expr) => {
1114                 try!(self.space_if_not_bol());
1115                 try!(self.print_expr(&**expr));
1116                 try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
1117             }
1118             _ => (),
1119         }
1120         try!(self.bclose_maybe_open(blk.span, indented, close_box));
1121         self.ann.post(self, NodeBlock(blk))
1122     }
1123
1124     fn print_else(&mut self, els: Option<&hir::Expr>) -> io::Result<()> {
1125         match els {
1126             Some(_else) => {
1127                 match _else.node {
1128                     // "another else-if"
1129                     hir::ExprIf(ref i, ref then, ref e) => {
1130                         try!(self.cbox(indent_unit - 1));
1131                         try!(self.ibox(0));
1132                         try!(word(&mut self.s, " else if "));
1133                         try!(self.print_expr(&**i));
1134                         try!(space(&mut self.s));
1135                         try!(self.print_block(&**then));
1136                         self.print_else(e.as_ref().map(|e| &**e))
1137                     }
1138                     // "final else"
1139                     hir::ExprBlock(ref b) => {
1140                         try!(self.cbox(indent_unit - 1));
1141                         try!(self.ibox(0));
1142                         try!(word(&mut self.s, " else "));
1143                         self.print_block(&**b)
1144                     }
1145                     // BLEAH, constraints would be great here
1146                     _ => {
1147                         panic!("print_if saw if with weird alternative");
1148                     }
1149                 }
1150             }
1151             _ => Ok(()),
1152         }
1153     }
1154
1155     pub fn print_if(&mut self,
1156                     test: &hir::Expr,
1157                     blk: &hir::Block,
1158                     elseopt: Option<&hir::Expr>)
1159                     -> io::Result<()> {
1160         try!(self.head("if"));
1161         try!(self.print_expr(test));
1162         try!(space(&mut self.s));
1163         try!(self.print_block(blk));
1164         self.print_else(elseopt)
1165     }
1166
1167     pub fn print_if_let(&mut self,
1168                         pat: &hir::Pat,
1169                         expr: &hir::Expr,
1170                         blk: &hir::Block,
1171                         elseopt: Option<&hir::Expr>)
1172                         -> io::Result<()> {
1173         try!(self.head("if let"));
1174         try!(self.print_pat(pat));
1175         try!(space(&mut self.s));
1176         try!(self.word_space("="));
1177         try!(self.print_expr(expr));
1178         try!(space(&mut self.s));
1179         try!(self.print_block(blk));
1180         self.print_else(elseopt)
1181     }
1182
1183
1184     fn print_call_post(&mut self, args: &[P<hir::Expr>]) -> io::Result<()> {
1185         try!(self.popen());
1186         try!(self.commasep_exprs(Inconsistent, args));
1187         self.pclose()
1188     }
1189
1190     pub fn print_expr_maybe_paren(&mut self, expr: &hir::Expr) -> io::Result<()> {
1191         let needs_par = needs_parentheses(expr);
1192         if needs_par {
1193             try!(self.popen());
1194         }
1195         try!(self.print_expr(expr));
1196         if needs_par {
1197             try!(self.pclose());
1198         }
1199         Ok(())
1200     }
1201
1202     fn print_expr_vec(&mut self, exprs: &[P<hir::Expr>]) -> io::Result<()> {
1203         try!(self.ibox(indent_unit));
1204         try!(word(&mut self.s, "["));
1205         try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1206         try!(word(&mut self.s, "]"));
1207         self.end()
1208     }
1209
1210     fn print_expr_repeat(&mut self, element: &hir::Expr, count: &hir::Expr) -> io::Result<()> {
1211         try!(self.ibox(indent_unit));
1212         try!(word(&mut self.s, "["));
1213         try!(self.print_expr(element));
1214         try!(self.word_space(";"));
1215         try!(self.print_expr(count));
1216         try!(word(&mut self.s, "]"));
1217         self.end()
1218     }
1219
1220     fn print_expr_struct(&mut self,
1221                          path: &hir::Path,
1222                          fields: &[hir::Field],
1223                          wth: &Option<P<hir::Expr>>)
1224                          -> io::Result<()> {
1225         try!(self.print_path(path, true, 0));
1226         try!(word(&mut self.s, "{"));
1227         try!(self.commasep_cmnt(Consistent,
1228                                 &fields[..],
1229                                 |s, field| {
1230                                     try!(s.ibox(indent_unit));
1231                                     try!(s.print_name(field.name.node));
1232                                     try!(s.word_space(":"));
1233                                     try!(s.print_expr(&*field.expr));
1234                                     s.end()
1235                                 },
1236                                 |f| f.span));
1237         match *wth {
1238             Some(ref expr) => {
1239                 try!(self.ibox(indent_unit));
1240                 if !fields.is_empty() {
1241                     try!(word(&mut self.s, ","));
1242                     try!(space(&mut self.s));
1243                 }
1244                 try!(word(&mut self.s, ".."));
1245                 try!(self.print_expr(&**expr));
1246                 try!(self.end());
1247             }
1248             _ => if !fields.is_empty() {
1249                 try!(word(&mut self.s, ","))
1250             },
1251         }
1252         try!(word(&mut self.s, "}"));
1253         Ok(())
1254     }
1255
1256     fn print_expr_tup(&mut self, exprs: &[P<hir::Expr>]) -> io::Result<()> {
1257         try!(self.popen());
1258         try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1259         if exprs.len() == 1 {
1260             try!(word(&mut self.s, ","));
1261         }
1262         self.pclose()
1263     }
1264
1265     fn print_expr_call(&mut self, func: &hir::Expr, args: &[P<hir::Expr>]) -> io::Result<()> {
1266         try!(self.print_expr_maybe_paren(func));
1267         self.print_call_post(args)
1268     }
1269
1270     fn print_expr_method_call(&mut self,
1271                               name: Spanned<ast::Name>,
1272                               tys: &[P<hir::Ty>],
1273                               args: &[P<hir::Expr>])
1274                               -> io::Result<()> {
1275         let base_args = &args[1..];
1276         try!(self.print_expr(&*args[0]));
1277         try!(word(&mut self.s, "."));
1278         try!(self.print_name(name.node));
1279         if !tys.is_empty() {
1280             try!(word(&mut self.s, "::<"));
1281             try!(self.commasep(Inconsistent, tys, |s, ty| s.print_type(&**ty)));
1282             try!(word(&mut self.s, ">"));
1283         }
1284         self.print_call_post(base_args)
1285     }
1286
1287     fn print_expr_binary(&mut self,
1288                          op: hir::BinOp,
1289                          lhs: &hir::Expr,
1290                          rhs: &hir::Expr)
1291                          -> io::Result<()> {
1292         try!(self.print_expr(lhs));
1293         try!(space(&mut self.s));
1294         try!(self.word_space(::util::binop_to_string(op.node)));
1295         self.print_expr(rhs)
1296     }
1297
1298     fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr) -> io::Result<()> {
1299         try!(word(&mut self.s, ::util::unop_to_string(op)));
1300         self.print_expr_maybe_paren(expr)
1301     }
1302
1303     fn print_expr_addr_of(&mut self,
1304                           mutability: hir::Mutability,
1305                           expr: &hir::Expr)
1306                           -> io::Result<()> {
1307         try!(word(&mut self.s, "&"));
1308         try!(self.print_mutability(mutability));
1309         self.print_expr_maybe_paren(expr)
1310     }
1311
1312     pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
1313         try!(self.maybe_print_comment(expr.span.lo));
1314         try!(self.ibox(indent_unit));
1315         try!(self.ann.pre(self, NodeExpr(expr)));
1316         match expr.node {
1317             hir::ExprBox(ref expr) => {
1318                 try!(self.word_space("box"));
1319                 try!(self.print_expr(expr));
1320             }
1321             hir::ExprVec(ref exprs) => {
1322                 try!(self.print_expr_vec(&exprs[..]));
1323             }
1324             hir::ExprRepeat(ref element, ref count) => {
1325                 try!(self.print_expr_repeat(&**element, &**count));
1326             }
1327             hir::ExprStruct(ref path, ref fields, ref wth) => {
1328                 try!(self.print_expr_struct(path, &fields[..], wth));
1329             }
1330             hir::ExprTup(ref exprs) => {
1331                 try!(self.print_expr_tup(&exprs[..]));
1332             }
1333             hir::ExprCall(ref func, ref args) => {
1334                 try!(self.print_expr_call(&**func, &args[..]));
1335             }
1336             hir::ExprMethodCall(name, ref tys, ref args) => {
1337                 try!(self.print_expr_method_call(name, &tys[..], &args[..]));
1338             }
1339             hir::ExprBinary(op, ref lhs, ref rhs) => {
1340                 try!(self.print_expr_binary(op, &**lhs, &**rhs));
1341             }
1342             hir::ExprUnary(op, ref expr) => {
1343                 try!(self.print_expr_unary(op, &**expr));
1344             }
1345             hir::ExprAddrOf(m, ref expr) => {
1346                 try!(self.print_expr_addr_of(m, &**expr));
1347             }
1348             hir::ExprLit(ref lit) => {
1349                 try!(self.print_literal(&**lit));
1350             }
1351             hir::ExprCast(ref expr, ref ty) => {
1352                 try!(self.print_expr(&**expr));
1353                 try!(space(&mut self.s));
1354                 try!(self.word_space("as"));
1355                 try!(self.print_type(&**ty));
1356             }
1357             hir::ExprIf(ref test, ref blk, ref elseopt) => {
1358                 try!(self.print_if(&**test, &**blk, elseopt.as_ref().map(|e| &**e)));
1359             }
1360             hir::ExprWhile(ref test, ref blk, opt_ident) => {
1361                 if let Some(ident) = opt_ident {
1362                     try!(self.print_name(ident.name));
1363                     try!(self.word_space(":"));
1364                 }
1365                 try!(self.head("while"));
1366                 try!(self.print_expr(&**test));
1367                 try!(space(&mut self.s));
1368                 try!(self.print_block(&**blk));
1369             }
1370             hir::ExprLoop(ref blk, opt_ident) => {
1371                 if let Some(ident) = opt_ident {
1372                     try!(self.print_name(ident.name));
1373                     try!(self.word_space(":"));
1374                 }
1375                 try!(self.head("loop"));
1376                 try!(space(&mut self.s));
1377                 try!(self.print_block(&**blk));
1378             }
1379             hir::ExprMatch(ref expr, ref arms, _) => {
1380                 try!(self.cbox(indent_unit));
1381                 try!(self.ibox(4));
1382                 try!(self.word_nbsp("match"));
1383                 try!(self.print_expr(&**expr));
1384                 try!(space(&mut self.s));
1385                 try!(self.bopen());
1386                 for arm in arms {
1387                     try!(self.print_arm(arm));
1388                 }
1389                 try!(self.bclose_(expr.span, indent_unit));
1390             }
1391             hir::ExprClosure(capture_clause, ref decl, ref body) => {
1392                 try!(self.print_capture_clause(capture_clause));
1393
1394                 try!(self.print_fn_block_args(&**decl));
1395                 try!(space(&mut self.s));
1396
1397                 let default_return = match decl.output {
1398                     hir::DefaultReturn(..) => true,
1399                     _ => false,
1400                 };
1401
1402                 if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
1403                     try!(self.print_block_unclosed(&**body));
1404                 } else {
1405                     // we extract the block, so as not to create another set of boxes
1406                     match body.expr.as_ref().unwrap().node {
1407                         hir::ExprBlock(ref blk) => {
1408                             try!(self.print_block_unclosed(&**blk));
1409                         }
1410                         _ => {
1411                             // this is a bare expression
1412                             try!(self.print_expr(body.expr.as_ref().map(|e| &**e).unwrap()));
1413                             try!(self.end()); // need to close a box
1414                         }
1415                     }
1416                 }
1417                 // a box will be closed by print_expr, but we didn't want an overall
1418                 // wrapper so we closed the corresponding opening. so create an
1419                 // empty box to satisfy the close.
1420                 try!(self.ibox(0));
1421             }
1422             hir::ExprBlock(ref blk) => {
1423                 // containing cbox, will be closed by print-block at }
1424                 try!(self.cbox(indent_unit));
1425                 // head-box, will be closed by print-block after {
1426                 try!(self.ibox(0));
1427                 try!(self.print_block(&**blk));
1428             }
1429             hir::ExprAssign(ref lhs, ref rhs) => {
1430                 try!(self.print_expr(&**lhs));
1431                 try!(space(&mut self.s));
1432                 try!(self.word_space("="));
1433                 try!(self.print_expr(&**rhs));
1434             }
1435             hir::ExprAssignOp(op, ref lhs, ref rhs) => {
1436                 try!(self.print_expr(&**lhs));
1437                 try!(space(&mut self.s));
1438                 try!(word(&mut self.s, ::util::binop_to_string(op.node)));
1439                 try!(self.word_space("="));
1440                 try!(self.print_expr(&**rhs));
1441             }
1442             hir::ExprField(ref expr, name) => {
1443                 try!(self.print_expr(&**expr));
1444                 try!(word(&mut self.s, "."));
1445                 try!(self.print_name(name.node));
1446             }
1447             hir::ExprTupField(ref expr, id) => {
1448                 try!(self.print_expr(&**expr));
1449                 try!(word(&mut self.s, "."));
1450                 try!(self.print_usize(id.node));
1451             }
1452             hir::ExprIndex(ref expr, ref index) => {
1453                 try!(self.print_expr(&**expr));
1454                 try!(word(&mut self.s, "["));
1455                 try!(self.print_expr(&**index));
1456                 try!(word(&mut self.s, "]"));
1457             }
1458             hir::ExprRange(ref start, ref end) => {
1459                 if let &Some(ref e) = start {
1460                     try!(self.print_expr(&**e));
1461                 }
1462                 try!(word(&mut self.s, ".."));
1463                 if let &Some(ref e) = end {
1464                     try!(self.print_expr(&**e));
1465                 }
1466             }
1467             hir::ExprPath(None, ref path) => {
1468                 try!(self.print_path(path, true, 0))
1469             }
1470             hir::ExprPath(Some(ref qself), ref path) => {
1471                 try!(self.print_qpath(path, qself, true))
1472             }
1473             hir::ExprBreak(opt_ident) => {
1474                 try!(word(&mut self.s, "break"));
1475                 try!(space(&mut self.s));
1476                 if let Some(ident) = opt_ident {
1477                     try!(self.print_name(ident.node.name));
1478                     try!(space(&mut self.s));
1479                 }
1480             }
1481             hir::ExprAgain(opt_ident) => {
1482                 try!(word(&mut self.s, "continue"));
1483                 try!(space(&mut self.s));
1484                 if let Some(ident) = opt_ident {
1485                     try!(self.print_name(ident.node.name));
1486                     try!(space(&mut self.s))
1487                 }
1488             }
1489             hir::ExprRet(ref result) => {
1490                 try!(word(&mut self.s, "return"));
1491                 match *result {
1492                     Some(ref expr) => {
1493                         try!(word(&mut self.s, " "));
1494                         try!(self.print_expr(&**expr));
1495                     }
1496                     _ => (),
1497                 }
1498             }
1499             hir::ExprInlineAsm(ref a) => {
1500                 try!(word(&mut self.s, "asm!"));
1501                 try!(self.popen());
1502                 try!(self.print_string(&a.asm, a.asm_str_style));
1503                 try!(self.word_space(":"));
1504
1505                 try!(self.commasep(Inconsistent, &a.outputs, |s, out| {
1506                     match out.constraint.slice_shift_char() {
1507                         Some(('=', operand)) if out.is_rw => {
1508                             try!(s.print_string(&format!("+{}", operand), ast::CookedStr))
1509                         }
1510                         _ => try!(s.print_string(&out.constraint, ast::CookedStr)),
1511                     }
1512                     try!(s.popen());
1513                     try!(s.print_expr(&*out.expr));
1514                     try!(s.pclose());
1515                     Ok(())
1516                 }));
1517                 try!(space(&mut self.s));
1518                 try!(self.word_space(":"));
1519
1520                 try!(self.commasep(Inconsistent, &a.inputs, |s, &(ref co, ref o)| {
1521                     try!(s.print_string(&co, ast::CookedStr));
1522                     try!(s.popen());
1523                     try!(s.print_expr(&**o));
1524                     try!(s.pclose());
1525                     Ok(())
1526                 }));
1527                 try!(space(&mut self.s));
1528                 try!(self.word_space(":"));
1529
1530                 try!(self.commasep(Inconsistent, &a.clobbers, |s, co| {
1531                     try!(s.print_string(&co, ast::CookedStr));
1532                     Ok(())
1533                 }));
1534
1535                 let mut options = vec![];
1536                 if a.volatile {
1537                     options.push("volatile");
1538                 }
1539                 if a.alignstack {
1540                     options.push("alignstack");
1541                 }
1542                 if a.dialect == ast::AsmDialect::Intel {
1543                     options.push("intel");
1544                 }
1545
1546                 if !options.is_empty() {
1547                     try!(space(&mut self.s));
1548                     try!(self.word_space(":"));
1549                     try!(self.commasep(Inconsistent, &*options, |s, &co| {
1550                         try!(s.print_string(co, ast::CookedStr));
1551                         Ok(())
1552                     }));
1553                 }
1554
1555                 try!(self.pclose());
1556             }
1557         }
1558         try!(self.ann.post(self, NodeExpr(expr)));
1559         self.end()
1560     }
1561
1562     pub fn print_local_decl(&mut self, loc: &hir::Local) -> io::Result<()> {
1563         try!(self.print_pat(&*loc.pat));
1564         if let Some(ref ty) = loc.ty {
1565             try!(self.word_space(":"));
1566             try!(self.print_type(&**ty));
1567         }
1568         Ok(())
1569     }
1570
1571     pub fn print_decl(&mut self, decl: &hir::Decl) -> io::Result<()> {
1572         try!(self.maybe_print_comment(decl.span.lo));
1573         match decl.node {
1574             hir::DeclLocal(ref loc) => {
1575                 try!(self.space_if_not_bol());
1576                 try!(self.ibox(indent_unit));
1577                 try!(self.word_nbsp("let"));
1578
1579                 try!(self.ibox(indent_unit));
1580                 try!(self.print_local_decl(&**loc));
1581                 try!(self.end());
1582                 if let Some(ref init) = loc.init {
1583                     try!(self.nbsp());
1584                     try!(self.word_space("="));
1585                     try!(self.print_expr(&**init));
1586                 }
1587                 self.end()
1588             }
1589             hir::DeclItem(ref item) => {
1590                 self.print_item_id(item)
1591             }
1592         }
1593     }
1594
1595     pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
1596         word(&mut self.s, &i.to_string())
1597     }
1598
1599     pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1600         try!(word(&mut self.s, &name.as_str()));
1601         self.ann.post(self, NodeName(&name))
1602     }
1603
1604     pub fn print_for_decl(&mut self, loc: &hir::Local, coll: &hir::Expr) -> io::Result<()> {
1605         try!(self.print_local_decl(loc));
1606         try!(space(&mut self.s));
1607         try!(self.word_space("in"));
1608         self.print_expr(coll)
1609     }
1610
1611     fn print_path(&mut self,
1612                   path: &hir::Path,
1613                   colons_before_params: bool,
1614                   depth: usize)
1615                   -> io::Result<()> {
1616         try!(self.maybe_print_comment(path.span.lo));
1617
1618         let mut first = !path.global;
1619         for segment in &path.segments[..path.segments.len() - depth] {
1620             if first {
1621                 first = false
1622             } else {
1623                 try!(word(&mut self.s, "::"))
1624             }
1625
1626             try!(self.print_name(segment.identifier.name));
1627
1628             try!(self.print_path_parameters(&segment.parameters, colons_before_params));
1629         }
1630
1631         Ok(())
1632     }
1633
1634     fn print_qpath(&mut self,
1635                    path: &hir::Path,
1636                    qself: &hir::QSelf,
1637                    colons_before_params: bool)
1638                    -> io::Result<()> {
1639         try!(word(&mut self.s, "<"));
1640         try!(self.print_type(&qself.ty));
1641         if qself.position > 0 {
1642             try!(space(&mut self.s));
1643             try!(self.word_space("as"));
1644             let depth = path.segments.len() - qself.position;
1645             try!(self.print_path(&path, false, depth));
1646         }
1647         try!(word(&mut self.s, ">"));
1648         try!(word(&mut self.s, "::"));
1649         let item_segment = path.segments.last().unwrap();
1650         try!(self.print_name(item_segment.identifier.name));
1651         self.print_path_parameters(&item_segment.parameters, colons_before_params)
1652     }
1653
1654     fn print_path_parameters(&mut self,
1655                              parameters: &hir::PathParameters,
1656                              colons_before_params: bool)
1657                              -> io::Result<()> {
1658         if parameters.is_empty() {
1659             return Ok(());
1660         }
1661
1662         if colons_before_params {
1663             try!(word(&mut self.s, "::"))
1664         }
1665
1666         match *parameters {
1667             hir::AngleBracketedParameters(ref data) => {
1668                 try!(word(&mut self.s, "<"));
1669
1670                 let mut comma = false;
1671                 for lifetime in &data.lifetimes {
1672                     if comma {
1673                         try!(self.word_space(","))
1674                     }
1675                     try!(self.print_lifetime(lifetime));
1676                     comma = true;
1677                 }
1678
1679                 if !data.types.is_empty() {
1680                     if comma {
1681                         try!(self.word_space(","))
1682                     }
1683                     try!(self.commasep(Inconsistent, &data.types, |s, ty| s.print_type(&**ty)));
1684                     comma = true;
1685                 }
1686
1687                 for binding in data.bindings.iter() {
1688                     if comma {
1689                         try!(self.word_space(","))
1690                     }
1691                     try!(self.print_name(binding.name));
1692                     try!(space(&mut self.s));
1693                     try!(self.word_space("="));
1694                     try!(self.print_type(&*binding.ty));
1695                     comma = true;
1696                 }
1697
1698                 try!(word(&mut self.s, ">"))
1699             }
1700
1701             hir::ParenthesizedParameters(ref data) => {
1702                 try!(word(&mut self.s, "("));
1703                 try!(self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(&**ty)));
1704                 try!(word(&mut self.s, ")"));
1705
1706                 match data.output {
1707                     None => {}
1708                     Some(ref ty) => {
1709                         try!(self.space_if_not_bol());
1710                         try!(self.word_space("->"));
1711                         try!(self.print_type(&**ty));
1712                     }
1713                 }
1714             }
1715         }
1716
1717         Ok(())
1718     }
1719
1720     pub fn print_pat(&mut self, pat: &hir::Pat) -> io::Result<()> {
1721         try!(self.maybe_print_comment(pat.span.lo));
1722         try!(self.ann.pre(self, NodePat(pat)));
1723         // Pat isn't normalized, but the beauty of it
1724         // is that it doesn't matter
1725         match pat.node {
1726             hir::PatWild => try!(word(&mut self.s, "_")),
1727             hir::PatIdent(binding_mode, ref path1, ref sub) => {
1728                 match binding_mode {
1729                     hir::BindByRef(mutbl) => {
1730                         try!(self.word_nbsp("ref"));
1731                         try!(self.print_mutability(mutbl));
1732                     }
1733                     hir::BindByValue(hir::MutImmutable) => {}
1734                     hir::BindByValue(hir::MutMutable) => {
1735                         try!(self.word_nbsp("mut"));
1736                     }
1737                 }
1738                 try!(self.print_name(path1.node.name));
1739                 match *sub {
1740                     Some(ref p) => {
1741                         try!(word(&mut self.s, "@"));
1742                         try!(self.print_pat(&**p));
1743                     }
1744                     None => (),
1745                 }
1746             }
1747             hir::PatEnum(ref path, ref args_) => {
1748                 try!(self.print_path(path, true, 0));
1749                 match *args_ {
1750                     None => try!(word(&mut self.s, "(..)")),
1751                     Some(ref args) => {
1752                         if !args.is_empty() {
1753                             try!(self.popen());
1754                             try!(self.commasep(Inconsistent, &args[..], |s, p| s.print_pat(&**p)));
1755                             try!(self.pclose());
1756                         }
1757                     }
1758                 }
1759             }
1760             hir::PatQPath(ref qself, ref path) => {
1761                 try!(self.print_qpath(path, qself, false));
1762             }
1763             hir::PatStruct(ref path, ref fields, etc) => {
1764                 try!(self.print_path(path, true, 0));
1765                 try!(self.nbsp());
1766                 try!(self.word_space("{"));
1767                 try!(self.commasep_cmnt(Consistent,
1768                                         &fields[..],
1769                                         |s, f| {
1770                                             try!(s.cbox(indent_unit));
1771                                             if !f.node.is_shorthand {
1772                                                 try!(s.print_name(f.node.name));
1773                                                 try!(s.word_nbsp(":"));
1774                                             }
1775                                             try!(s.print_pat(&*f.node.pat));
1776                                             s.end()
1777                                         },
1778                                         |f| f.node.pat.span));
1779                 if etc {
1780                     if !fields.is_empty() {
1781                         try!(self.word_space(","));
1782                     }
1783                     try!(word(&mut self.s, ".."));
1784                 }
1785                 try!(space(&mut self.s));
1786                 try!(word(&mut self.s, "}"));
1787             }
1788             hir::PatTup(ref elts) => {
1789                 try!(self.popen());
1790                 try!(self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&**p)));
1791                 if elts.len() == 1 {
1792                     try!(word(&mut self.s, ","));
1793                 }
1794                 try!(self.pclose());
1795             }
1796             hir::PatBox(ref inner) => {
1797                 try!(word(&mut self.s, "box "));
1798                 try!(self.print_pat(&**inner));
1799             }
1800             hir::PatRegion(ref inner, mutbl) => {
1801                 try!(word(&mut self.s, "&"));
1802                 if mutbl == hir::MutMutable {
1803                     try!(word(&mut self.s, "mut "));
1804                 }
1805                 try!(self.print_pat(&**inner));
1806             }
1807             hir::PatLit(ref e) => try!(self.print_expr(&**e)),
1808             hir::PatRange(ref begin, ref end) => {
1809                 try!(self.print_expr(&**begin));
1810                 try!(space(&mut self.s));
1811                 try!(word(&mut self.s, "..."));
1812                 try!(self.print_expr(&**end));
1813             }
1814             hir::PatVec(ref before, ref slice, ref after) => {
1815                 try!(word(&mut self.s, "["));
1816                 try!(self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&**p)));
1817                 if let Some(ref p) = *slice {
1818                     if !before.is_empty() {
1819                         try!(self.word_space(","));
1820                     }
1821                     if p.node != hir::PatWild {
1822                         try!(self.print_pat(&**p));
1823                     }
1824                     try!(word(&mut self.s, ".."));
1825                     if !after.is_empty() {
1826                         try!(self.word_space(","));
1827                     }
1828                 }
1829                 try!(self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&**p)));
1830                 try!(word(&mut self.s, "]"));
1831             }
1832         }
1833         self.ann.post(self, NodePat(pat))
1834     }
1835
1836     fn print_arm(&mut self, arm: &hir::Arm) -> io::Result<()> {
1837         // I have no idea why this check is necessary, but here it
1838         // is :(
1839         if arm.attrs.is_empty() {
1840             try!(space(&mut self.s));
1841         }
1842         try!(self.cbox(indent_unit));
1843         try!(self.ibox(0));
1844         try!(self.print_outer_attributes(&arm.attrs));
1845         let mut first = true;
1846         for p in &arm.pats {
1847             if first {
1848                 first = false;
1849             } else {
1850                 try!(space(&mut self.s));
1851                 try!(self.word_space("|"));
1852             }
1853             try!(self.print_pat(&**p));
1854         }
1855         try!(space(&mut self.s));
1856         if let Some(ref e) = arm.guard {
1857             try!(self.word_space("if"));
1858             try!(self.print_expr(&**e));
1859             try!(space(&mut self.s));
1860         }
1861         try!(self.word_space("=>"));
1862
1863         match arm.body.node {
1864             hir::ExprBlock(ref blk) => {
1865                 // the block will close the pattern's ibox
1866                 try!(self.print_block_unclosed_indent(&**blk, indent_unit));
1867
1868                 // If it is a user-provided unsafe block, print a comma after it
1869                 if let hir::UnsafeBlock(hir::UserProvided) = blk.rules {
1870                     try!(word(&mut self.s, ","));
1871                 }
1872             }
1873             _ => {
1874                 try!(self.end()); // close the ibox for the pattern
1875                 try!(self.print_expr(&*arm.body));
1876                 try!(word(&mut self.s, ","));
1877             }
1878         }
1879         self.end() // close enclosing cbox
1880     }
1881
1882     // Returns whether it printed anything
1883     fn print_explicit_self(&mut self,
1884                            explicit_self: &hir::ExplicitSelf_,
1885                            mutbl: hir::Mutability)
1886                            -> io::Result<bool> {
1887         try!(self.print_mutability(mutbl));
1888         match *explicit_self {
1889             hir::SelfStatic => {
1890                 return Ok(false);
1891             }
1892             hir::SelfValue(_) => {
1893                 try!(word(&mut self.s, "self"));
1894             }
1895             hir::SelfRegion(ref lt, m, _) => {
1896                 try!(word(&mut self.s, "&"));
1897                 try!(self.print_opt_lifetime(lt));
1898                 try!(self.print_mutability(m));
1899                 try!(word(&mut self.s, "self"));
1900             }
1901             hir::SelfExplicit(ref typ, _) => {
1902                 try!(word(&mut self.s, "self"));
1903                 try!(self.word_space(":"));
1904                 try!(self.print_type(&**typ));
1905             }
1906         }
1907         return Ok(true);
1908     }
1909
1910     pub fn print_fn(&mut self,
1911                     decl: &hir::FnDecl,
1912                     unsafety: hir::Unsafety,
1913                     constness: hir::Constness,
1914                     abi: abi::Abi,
1915                     name: Option<ast::Name>,
1916                     generics: &hir::Generics,
1917                     opt_explicit_self: Option<&hir::ExplicitSelf_>,
1918                     vis: hir::Visibility)
1919                     -> io::Result<()> {
1920         try!(self.print_fn_header_info(unsafety, constness, abi, vis));
1921
1922         if let Some(name) = name {
1923             try!(self.nbsp());
1924             try!(self.print_name(name));
1925         }
1926         try!(self.print_generics(generics));
1927         try!(self.print_fn_args_and_ret(decl, opt_explicit_self));
1928         self.print_where_clause(&generics.where_clause)
1929     }
1930
1931     pub fn print_fn_args(&mut self,
1932                          decl: &hir::FnDecl,
1933                          opt_explicit_self: Option<&hir::ExplicitSelf_>)
1934                          -> io::Result<()> {
1935         // It is unfortunate to duplicate the commasep logic, but we want the
1936         // self type and the args all in the same box.
1937         try!(self.rbox(0, Inconsistent));
1938         let mut first = true;
1939         if let Some(explicit_self) = opt_explicit_self {
1940             let m = match explicit_self {
1941                 &hir::SelfStatic => hir::MutImmutable,
1942                 _ => match decl.inputs[0].pat.node {
1943                     hir::PatIdent(hir::BindByValue(m), _, _) => m,
1944                     _ => hir::MutImmutable,
1945                 },
1946             };
1947             first = !try!(self.print_explicit_self(explicit_self, m));
1948         }
1949
1950         // HACK(eddyb) ignore the separately printed self argument.
1951         let args = if first {
1952             &decl.inputs[..]
1953         } else {
1954             &decl.inputs[1..]
1955         };
1956
1957         for arg in args {
1958             if first {
1959                 first = false;
1960             } else {
1961                 try!(self.word_space(","));
1962             }
1963             try!(self.print_arg(arg));
1964         }
1965
1966         self.end()
1967     }
1968
1969     pub fn print_fn_args_and_ret(&mut self,
1970                                  decl: &hir::FnDecl,
1971                                  opt_explicit_self: Option<&hir::ExplicitSelf_>)
1972                                  -> io::Result<()> {
1973         try!(self.popen());
1974         try!(self.print_fn_args(decl, opt_explicit_self));
1975         if decl.variadic {
1976             try!(word(&mut self.s, ", ..."));
1977         }
1978         try!(self.pclose());
1979
1980         self.print_fn_output(decl)
1981     }
1982
1983     pub fn print_fn_block_args(&mut self, decl: &hir::FnDecl) -> io::Result<()> {
1984         try!(word(&mut self.s, "|"));
1985         try!(self.print_fn_args(decl, None));
1986         try!(word(&mut self.s, "|"));
1987
1988         if let hir::DefaultReturn(..) = decl.output {
1989             return Ok(());
1990         }
1991
1992         try!(self.space_if_not_bol());
1993         try!(self.word_space("->"));
1994         match decl.output {
1995             hir::Return(ref ty) => {
1996                 try!(self.print_type(&**ty));
1997                 self.maybe_print_comment(ty.span.lo)
1998             }
1999             hir::DefaultReturn(..) => unreachable!(),
2000             hir::NoReturn(span) => {
2001                 try!(self.word_nbsp("!"));
2002                 self.maybe_print_comment(span.lo)
2003             }
2004         }
2005     }
2006
2007     pub fn print_capture_clause(&mut self, capture_clause: hir::CaptureClause) -> io::Result<()> {
2008         match capture_clause {
2009             hir::CaptureByValue => self.word_space("move"),
2010             hir::CaptureByRef => Ok(()),
2011         }
2012     }
2013
2014     pub fn print_bounds(&mut self, prefix: &str, bounds: &[hir::TyParamBound]) -> io::Result<()> {
2015         if !bounds.is_empty() {
2016             try!(word(&mut self.s, prefix));
2017             let mut first = true;
2018             for bound in bounds {
2019                 try!(self.nbsp());
2020                 if first {
2021                     first = false;
2022                 } else {
2023                     try!(self.word_space("+"));
2024                 }
2025
2026                 try!(match *bound {
2027                     TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
2028                         self.print_poly_trait_ref(tref)
2029                     }
2030                     TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
2031                         try!(word(&mut self.s, "?"));
2032                         self.print_poly_trait_ref(tref)
2033                     }
2034                     RegionTyParamBound(ref lt) => {
2035                         self.print_lifetime(lt)
2036                     }
2037                 })
2038             }
2039             Ok(())
2040         } else {
2041             Ok(())
2042         }
2043     }
2044
2045     pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> {
2046         self.print_name(lifetime.name)
2047     }
2048
2049     pub fn print_lifetime_def(&mut self, lifetime: &hir::LifetimeDef) -> io::Result<()> {
2050         try!(self.print_lifetime(&lifetime.lifetime));
2051         let mut sep = ":";
2052         for v in &lifetime.bounds {
2053             try!(word(&mut self.s, sep));
2054             try!(self.print_lifetime(v));
2055             sep = "+";
2056         }
2057         Ok(())
2058     }
2059
2060     pub fn print_generics(&mut self, generics: &hir::Generics) -> io::Result<()> {
2061         let total = generics.lifetimes.len() + generics.ty_params.len();
2062         if total == 0 {
2063             return Ok(());
2064         }
2065
2066         try!(word(&mut self.s, "<"));
2067
2068         let mut ints = Vec::new();
2069         for i in 0..total {
2070             ints.push(i);
2071         }
2072
2073         try!(self.commasep(Inconsistent, &ints[..], |s, &idx| {
2074             if idx < generics.lifetimes.len() {
2075                 let lifetime = &generics.lifetimes[idx];
2076                 s.print_lifetime_def(lifetime)
2077             } else {
2078                 let idx = idx - generics.lifetimes.len();
2079                 let param = &generics.ty_params[idx];
2080                 s.print_ty_param(param)
2081             }
2082         }));
2083
2084         try!(word(&mut self.s, ">"));
2085         Ok(())
2086     }
2087
2088     pub fn print_ty_param(&mut self, param: &hir::TyParam) -> io::Result<()> {
2089         try!(self.print_name(param.name));
2090         try!(self.print_bounds(":", &param.bounds));
2091         match param.default {
2092             Some(ref default) => {
2093                 try!(space(&mut self.s));
2094                 try!(self.word_space("="));
2095                 self.print_type(&**default)
2096             }
2097             _ => Ok(()),
2098         }
2099     }
2100
2101     pub fn print_where_clause(&mut self, where_clause: &hir::WhereClause) -> io::Result<()> {
2102         if where_clause.predicates.is_empty() {
2103             return Ok(());
2104         }
2105
2106         try!(space(&mut self.s));
2107         try!(self.word_space("where"));
2108
2109         for (i, predicate) in where_clause.predicates.iter().enumerate() {
2110             if i != 0 {
2111                 try!(self.word_space(","));
2112             }
2113
2114             match predicate {
2115                 &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ref bound_lifetimes,
2116                                                                               ref bounded_ty,
2117                                                                               ref bounds,
2118                                                                               ..}) => {
2119                     try!(self.print_formal_lifetime_list(bound_lifetimes));
2120                     try!(self.print_type(&**bounded_ty));
2121                     try!(self.print_bounds(":", bounds));
2122                 }
2123                 &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
2124                                                                                 ref bounds,
2125                                                                                 ..}) => {
2126                     try!(self.print_lifetime(lifetime));
2127                     try!(word(&mut self.s, ":"));
2128
2129                     for (i, bound) in bounds.iter().enumerate() {
2130                         try!(self.print_lifetime(bound));
2131
2132                         if i != 0 {
2133                             try!(word(&mut self.s, ":"));
2134                         }
2135                     }
2136                 }
2137                 &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref path, ref ty, ..}) => {
2138                     try!(self.print_path(path, false, 0));
2139                     try!(space(&mut self.s));
2140                     try!(self.word_space("="));
2141                     try!(self.print_type(&**ty));
2142                 }
2143             }
2144         }
2145
2146         Ok(())
2147     }
2148
2149     pub fn print_view_path(&mut self, vp: &hir::ViewPath) -> io::Result<()> {
2150         match vp.node {
2151             hir::ViewPathSimple(name, ref path) => {
2152                 try!(self.print_path(path, false, 0));
2153
2154                 if path.segments.last().unwrap().identifier.name != name {
2155                     try!(space(&mut self.s));
2156                     try!(self.word_space("as"));
2157                     try!(self.print_name(name));
2158                 }
2159
2160                 Ok(())
2161             }
2162
2163             hir::ViewPathGlob(ref path) => {
2164                 try!(self.print_path(path, false, 0));
2165                 word(&mut self.s, "::*")
2166             }
2167
2168             hir::ViewPathList(ref path, ref segments) => {
2169                 if path.segments.is_empty() {
2170                     try!(word(&mut self.s, "{"));
2171                 } else {
2172                     try!(self.print_path(path, false, 0));
2173                     try!(word(&mut self.s, "::{"));
2174                 }
2175                 try!(self.commasep(Inconsistent, &segments[..], |s, w| {
2176                     match w.node {
2177                         hir::PathListIdent { name, .. } => {
2178                             s.print_name(name)
2179                         }
2180                         hir::PathListMod { .. } => {
2181                             word(&mut s.s, "self")
2182                         }
2183                     }
2184                 }));
2185                 word(&mut self.s, "}")
2186             }
2187         }
2188     }
2189
2190     pub fn print_mutability(&mut self, mutbl: hir::Mutability) -> io::Result<()> {
2191         match mutbl {
2192             hir::MutMutable => self.word_nbsp("mut"),
2193             hir::MutImmutable => Ok(()),
2194         }
2195     }
2196
2197     pub fn print_mt(&mut self, mt: &hir::MutTy) -> io::Result<()> {
2198         try!(self.print_mutability(mt.mutbl));
2199         self.print_type(&*mt.ty)
2200     }
2201
2202     pub fn print_arg(&mut self, input: &hir::Arg) -> io::Result<()> {
2203         try!(self.ibox(indent_unit));
2204         match input.ty.node {
2205             hir::TyInfer => try!(self.print_pat(&*input.pat)),
2206             _ => {
2207                 match input.pat.node {
2208                     hir::PatIdent(_, ref path1, _) if
2209                         path1.node.name ==
2210                             parse::token::special_idents::invalid.name => {
2211                         // Do nothing.
2212                     }
2213                     _ => {
2214                         try!(self.print_pat(&*input.pat));
2215                         try!(word(&mut self.s, ":"));
2216                         try!(space(&mut self.s));
2217                     }
2218                 }
2219                 try!(self.print_type(&*input.ty));
2220             }
2221         }
2222         self.end()
2223     }
2224
2225     pub fn print_fn_output(&mut self, decl: &hir::FnDecl) -> io::Result<()> {
2226         if let hir::DefaultReturn(..) = decl.output {
2227             return Ok(());
2228         }
2229
2230         try!(self.space_if_not_bol());
2231         try!(self.ibox(indent_unit));
2232         try!(self.word_space("->"));
2233         match decl.output {
2234             hir::NoReturn(_) => try!(self.word_nbsp("!")),
2235             hir::DefaultReturn(..) => unreachable!(),
2236             hir::Return(ref ty) => try!(self.print_type(&**ty)),
2237         }
2238         try!(self.end());
2239
2240         match decl.output {
2241             hir::Return(ref output) => self.maybe_print_comment(output.span.lo),
2242             _ => Ok(()),
2243         }
2244     }
2245
2246     pub fn print_ty_fn(&mut self,
2247                        abi: abi::Abi,
2248                        unsafety: hir::Unsafety,
2249                        decl: &hir::FnDecl,
2250                        name: Option<ast::Name>,
2251                        generics: &hir::Generics,
2252                        opt_explicit_self: Option<&hir::ExplicitSelf_>)
2253                        -> io::Result<()> {
2254         try!(self.ibox(indent_unit));
2255         if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
2256             try!(word(&mut self.s, "for"));
2257             try!(self.print_generics(generics));
2258         }
2259         let generics = hir::Generics {
2260             lifetimes: Vec::new(),
2261             ty_params: OwnedSlice::empty(),
2262             where_clause: hir::WhereClause {
2263                 id: ast::DUMMY_NODE_ID,
2264                 predicates: Vec::new(),
2265             },
2266         };
2267         try!(self.print_fn(decl,
2268                            unsafety,
2269                            hir::Constness::NotConst,
2270                            abi,
2271                            name,
2272                            &generics,
2273                            opt_explicit_self,
2274                            hir::Inherited));
2275         self.end()
2276     }
2277
2278     pub fn maybe_print_trailing_comment(&mut self,
2279                                         span: codemap::Span,
2280                                         next_pos: Option<BytePos>)
2281                                         -> io::Result<()> {
2282         let cm = match self.cm {
2283             Some(cm) => cm,
2284             _ => return Ok(()),
2285         };
2286         match self.next_comment() {
2287             Some(ref cmnt) => {
2288                 if (*cmnt).style != comments::Trailing {
2289                     return Ok(());
2290                 }
2291                 let span_line = cm.lookup_char_pos(span.hi);
2292                 let comment_line = cm.lookup_char_pos((*cmnt).pos);
2293                 let mut next = (*cmnt).pos + BytePos(1);
2294                 match next_pos {
2295                     None => (),
2296                     Some(p) => next = p,
2297                 }
2298                 if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2299                    span_line.line == comment_line.line {
2300                     try!(self.print_comment(cmnt));
2301                     self.cur_cmnt_and_lit.cur_cmnt += 1;
2302                 }
2303             }
2304             _ => (),
2305         }
2306         Ok(())
2307     }
2308
2309     pub fn print_remaining_comments(&mut self) -> io::Result<()> {
2310         // If there aren't any remaining comments, then we need to manually
2311         // make sure there is a line break at the end.
2312         if self.next_comment().is_none() {
2313             try!(hardbreak(&mut self.s));
2314         }
2315         loop {
2316             match self.next_comment() {
2317                 Some(ref cmnt) => {
2318                     try!(self.print_comment(cmnt));
2319                     self.cur_cmnt_and_lit.cur_cmnt += 1;
2320                 }
2321                 _ => break,
2322             }
2323         }
2324         Ok(())
2325     }
2326
2327     pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
2328                                                   opt_abi: Option<abi::Abi>)
2329                                                   -> io::Result<()> {
2330         match opt_abi {
2331             Some(abi::Rust) => Ok(()),
2332             Some(abi) => {
2333                 try!(self.word_nbsp("extern"));
2334                 self.word_nbsp(&abi.to_string())
2335             }
2336             None => Ok(()),
2337         }
2338     }
2339
2340     pub fn print_extern_opt_abi(&mut self, opt_abi: Option<abi::Abi>) -> io::Result<()> {
2341         match opt_abi {
2342             Some(abi) => {
2343                 try!(self.word_nbsp("extern"));
2344                 self.word_nbsp(&abi.to_string())
2345             }
2346             None => Ok(()),
2347         }
2348     }
2349
2350     pub fn print_fn_header_info(&mut self,
2351                                 unsafety: hir::Unsafety,
2352                                 constness: hir::Constness,
2353                                 abi: abi::Abi,
2354                                 vis: hir::Visibility)
2355                                 -> io::Result<()> {
2356         try!(word(&mut self.s, &visibility_qualified(vis, "")));
2357         try!(self.print_unsafety(unsafety));
2358
2359         match constness {
2360             hir::Constness::NotConst => {}
2361             hir::Constness::Const => try!(self.word_nbsp("const")),
2362         }
2363
2364         if abi != abi::Rust {
2365             try!(self.word_nbsp("extern"));
2366             try!(self.word_nbsp(&abi.to_string()));
2367         }
2368
2369         word(&mut self.s, "fn")
2370     }
2371
2372     pub fn print_unsafety(&mut self, s: hir::Unsafety) -> io::Result<()> {
2373         match s {
2374             hir::Unsafety::Normal => Ok(()),
2375             hir::Unsafety::Unsafe => self.word_nbsp("unsafe"),
2376         }
2377     }
2378 }
2379
2380 // Dup'ed from parse::classify, but adapted for the HIR.
2381 /// Does this expression require a semicolon to be treated
2382 /// as a statement? The negation of this: 'can this expression
2383 /// be used as a statement without a semicolon' -- is used
2384 /// as an early-bail-out in the parser so that, for instance,
2385 ///     if true {...} else {...}
2386 ///      |x| 5
2387 /// isn't parsed as (if true {...} else {...} | x) | 5
2388 fn expr_requires_semi_to_be_stmt(e: &hir::Expr) -> bool {
2389     match e.node {
2390         hir::ExprIf(..) |
2391         hir::ExprMatch(..) |
2392         hir::ExprBlock(_) |
2393         hir::ExprWhile(..) |
2394         hir::ExprLoop(..) => false,
2395         _ => true,
2396     }
2397 }
2398
2399 /// this statement requires a semicolon after it.
2400 /// note that in one case (stmt_semi), we've already
2401 /// seen the semicolon, and thus don't need another.
2402 fn stmt_ends_with_semi(stmt: &hir::Stmt_) -> bool {
2403     match *stmt {
2404         hir::StmtDecl(ref d, _) => {
2405             match d.node {
2406                 hir::DeclLocal(_) => true,
2407                 hir::DeclItem(_) => false,
2408             }
2409         }
2410         hir::StmtExpr(ref e, _) => {
2411             expr_requires_semi_to_be_stmt(&**e)
2412         }
2413         hir::StmtSemi(..) => {
2414             false
2415         }
2416     }
2417 }