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.
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.
11 pub use self::AnnNode::*;
15 use syntax::codemap::{CodeMap, Spanned};
16 use syntax::parse::lexer::comments;
17 use syntax::print::pp::{self, break_offset, word, space, hardbreak};
18 use syntax::print::pp::{Breaks, eof};
19 use syntax::print::pp::Breaks::{Consistent, Inconsistent};
20 use syntax::print::pprust::{self as ast_pp, PrintState};
22 use syntax::symbol::keywords;
23 use syntax_pos::{self, BytePos};
27 use hir::{PatKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
29 use std::io::{self, Write, Read};
31 pub enum AnnNode<'a> {
32 NodeName(&'a ast::Name),
33 NodeBlock(&'a hir::Block),
34 NodeItem(&'a hir::Item),
35 NodeSubItem(ast::NodeId),
36 NodeExpr(&'a hir::Expr),
37 NodePat(&'a hir::Pat),
42 TraitItem(hir::TraitItemId),
43 ImplItem(hir::ImplItemId),
45 BodyArgPat(hir::BodyId, usize)
49 fn nested(&self, _state: &mut State, _nested: Nested) -> io::Result<()> {
52 fn pre(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> {
55 fn post(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> {
61 impl PpAnn for NoAnn {}
62 pub const NO_ANN: &'static PpAnn = &NoAnn;
64 impl PpAnn for hir::Crate {
65 fn nested(&self, state: &mut State, nested: Nested) -> io::Result<()> {
67 Nested::Item(id) => state.print_item(self.item(id.id)),
68 Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
69 Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
70 Nested::Body(id) => state.print_expr(&self.body(id).value),
71 Nested::BodyArgPat(id, i) => state.print_pat(&self.body(id).arguments[i].pat)
76 pub struct State<'a> {
77 pub s: pp::Printer<'a>,
78 cm: Option<&'a CodeMap>,
79 comments: Option<Vec<comments::Comment>>,
80 literals: Option<Vec<comments::Literal>>,
81 cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral,
82 boxes: Vec<pp::Breaks>,
83 ann: &'a (PpAnn + 'a),
86 impl<'a> PrintState<'a> for State<'a> {
87 fn writer(&mut self) -> &mut pp::Printer<'a> {
91 fn boxes(&mut self) -> &mut Vec<pp::Breaks> {
95 fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
99 fn cur_cmnt_and_lit(&mut self) -> &mut ast_pp::CurrentCommentAndLiteral {
100 &mut self.cur_cmnt_and_lit
103 fn literals(&self) -> &Option<Vec<comments::Literal>> {
108 #[allow(non_upper_case_globals)]
109 pub const indent_unit: usize = 4;
111 #[allow(non_upper_case_globals)]
112 pub const default_columns: usize = 78;
115 /// Requires you to pass an input filename and reader so that
116 /// it can scan the input text for comments and literals to
118 pub fn print_crate<'a>(cm: &'a CodeMap,
119 span_diagnostic: &errors::Handler,
123 out: Box<Write + 'a>,
127 let mut s = State::new_from_input(cm, span_diagnostic, filename, input,
128 out, ann, is_expanded);
130 // When printing the AST, we sometimes need to inject `#[no_std]` here.
131 // Since you can't compile the HIR, it's not necessary.
133 s.print_mod(&krate.module, &krate.attrs)?;
134 s.print_remaining_comments()?;
139 pub fn new_from_input(cm: &'a CodeMap,
140 span_diagnostic: &errors::Handler,
143 out: Box<Write + 'a>,
147 let (cmnts, lits) = comments::gather_comments_and_literals(span_diagnostic,
155 // If the code is post expansion, don't use the table of
156 // literals, since it doesn't correspond with the literals
157 // in the AST anymore.
165 pub fn new(cm: &'a CodeMap,
166 out: Box<Write + 'a>,
168 comments: Option<Vec<comments::Comment>>,
169 literals: Option<Vec<comments::Literal>>)
172 s: pp::mk_printer(out, default_columns),
174 comments: comments.clone(),
175 literals: literals.clone(),
176 cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral {
186 pub fn to_string<F>(ann: &PpAnn, f: F) -> String
187 where F: FnOnce(&mut State) -> io::Result<()>
189 let mut wr = Vec::new();
191 let mut printer = State {
192 s: pp::mk_printer(Box::new(&mut wr), default_columns),
196 cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral {
203 f(&mut printer).unwrap();
204 eof(&mut printer.s).unwrap();
206 String::from_utf8(wr).unwrap()
209 pub fn visibility_qualified(vis: &hir::Visibility, w: &str) -> String {
210 to_string(NO_ANN, |s| {
211 s.print_visibility(vis)?;
216 fn needs_parentheses(expr: &hir::Expr) -> bool {
218 hir::ExprAssign(..) |
219 hir::ExprBinary(..) |
220 hir::ExprClosure(..) |
221 hir::ExprAssignOp(..) |
223 hir::ExprType(..) => true,
229 pub fn cbox(&mut self, u: usize) -> io::Result<()> {
230 self.boxes.push(pp::Breaks::Consistent);
231 pp::cbox(&mut self.s, u)
234 pub fn nbsp(&mut self) -> io::Result<()> {
235 word(&mut self.s, " ")
238 pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
239 word(&mut self.s, w)?;
243 pub fn head(&mut self, w: &str) -> io::Result<()> {
244 // outer-box is consistent
245 self.cbox(indent_unit)?;
246 // head-box is inconsistent
247 self.ibox(w.len() + 1)?;
248 // keyword that starts the head
255 pub fn bopen(&mut self) -> io::Result<()> {
256 word(&mut self.s, "{")?;
257 self.end() // close the head-box
260 pub fn bclose_(&mut self, span: syntax_pos::Span, indented: usize) -> io::Result<()> {
261 self.bclose_maybe_open(span, indented, true)
263 pub fn bclose_maybe_open(&mut self,
264 span: syntax_pos::Span,
268 self.maybe_print_comment(span.hi)?;
269 self.break_offset_if_not_bol(1, -(indented as isize))?;
270 word(&mut self.s, "}")?;
272 self.end()?; // close the outer-box
276 pub fn bclose(&mut self, span: syntax_pos::Span) -> io::Result<()> {
277 self.bclose_(span, indent_unit)
280 pub fn in_cbox(&self) -> bool {
281 match self.boxes.last() {
282 Some(&last_box) => last_box == pp::Breaks::Consistent,
286 pub fn space_if_not_bol(&mut self) -> io::Result<()> {
292 pub fn break_offset_if_not_bol(&mut self, n: usize, off: isize) -> io::Result<()> {
294 break_offset(&mut self.s, n, off)
296 if off != 0 && self.s.last_token().is_hardbreak_tok() {
297 // We do something pretty sketchy here: tuck the nonzero
298 // offset-adjustment we were going to deposit along with the
299 // break into the previous hardbreak.
300 self.s.replace_last_token(pp::hardbreak_tok_offset(off));
306 // Synthesizes a comment that was not textually present in the original source
308 pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
309 word(&mut self.s, "/*")?;
311 word(&mut self.s, &text[..])?;
313 word(&mut self.s, "*/")
317 pub fn commasep_cmnt<T, F, G>(&mut self,
323 where F: FnMut(&mut State, &T) -> io::Result<()>,
324 G: FnMut(&T) -> syntax_pos::Span
327 let len = elts.len();
330 self.maybe_print_comment(get_span(elt).hi)?;
334 word(&mut self.s, ",")?;
335 self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi))?;
336 self.space_if_not_bol()?;
342 pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr]) -> io::Result<()> {
343 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span)
346 pub fn print_mod(&mut self, _mod: &hir::Mod, attrs: &[ast::Attribute]) -> io::Result<()> {
347 self.print_inner_attributes(attrs)?;
348 for &item_id in &_mod.item_ids {
349 self.ann.nested(self, Nested::Item(item_id))?;
354 pub fn print_foreign_mod(&mut self,
355 nmod: &hir::ForeignMod,
356 attrs: &[ast::Attribute])
358 self.print_inner_attributes(attrs)?;
359 for item in &nmod.items {
360 self.print_foreign_item(item)?;
365 pub fn print_opt_lifetime(&mut self, lifetime: &Option<hir::Lifetime>) -> io::Result<()> {
366 if let Some(l) = *lifetime {
367 self.print_lifetime(&l)?;
373 pub fn print_type(&mut self, ty: &hir::Ty) -> io::Result<()> {
374 self.maybe_print_comment(ty.span.lo)?;
377 hir::TySlice(ref ty) => {
378 word(&mut self.s, "[")?;
379 self.print_type(&ty)?;
380 word(&mut self.s, "]")?;
382 hir::TyPtr(ref mt) => {
383 word(&mut self.s, "*")?;
385 hir::MutMutable => self.word_nbsp("mut")?,
386 hir::MutImmutable => self.word_nbsp("const")?,
388 self.print_type(&mt.ty)?;
390 hir::TyRptr(ref lifetime, ref mt) => {
391 word(&mut self.s, "&")?;
392 self.print_opt_lifetime(lifetime)?;
396 word(&mut self.s, "!")?;
398 hir::TyTup(ref elts) => {
400 self.commasep(Inconsistent, &elts[..], |s, ty| s.print_type(&ty))?;
402 word(&mut self.s, ",")?;
406 hir::TyBareFn(ref f) => {
407 let generics = hir::Generics {
408 lifetimes: f.lifetimes.clone(),
409 ty_params: hir::HirVec::new(),
410 where_clause: hir::WhereClause {
411 id: ast::DUMMY_NODE_ID,
412 predicates: hir::HirVec::new(),
414 span: syntax_pos::DUMMY_SP,
416 self.print_ty_fn(f.abi, f.unsafety, &f.decl, None, &generics)?;
418 hir::TyPath(ref qpath) => {
419 self.print_qpath(qpath, false)?
421 hir::TyTraitObject(ref bounds) => {
422 self.print_bounds("", &bounds[..])?;
424 hir::TyImplTrait(ref bounds) => {
425 self.print_bounds("impl ", &bounds[..])?;
427 hir::TyArray(ref ty, v) => {
428 word(&mut self.s, "[")?;
429 self.print_type(&ty)?;
430 word(&mut self.s, "; ")?;
431 self.ann.nested(self, Nested::Body(v))?;
432 word(&mut self.s, "]")?;
434 hir::TyTypeof(e) => {
435 word(&mut self.s, "typeof(")?;
436 self.ann.nested(self, Nested::Body(e))?;
437 word(&mut self.s, ")")?;
440 word(&mut self.s, "_")?;
446 pub fn print_foreign_item(&mut self, item: &hir::ForeignItem) -> io::Result<()> {
447 self.hardbreak_if_not_bol()?;
448 self.maybe_print_comment(item.span.lo)?;
449 self.print_outer_attributes(&item.attrs)?;
451 hir::ForeignItemFn(ref decl, ref arg_names, ref generics) => {
454 hir::Unsafety::Normal,
455 hir::Constness::NotConst,
462 self.end()?; // end head-ibox
463 word(&mut self.s, ";")?;
464 self.end() // end the outer fn box
466 hir::ForeignItemStatic(ref t, m) => {
467 self.head(&visibility_qualified(&item.vis, "static"))?;
469 self.word_space("mut")?;
471 self.print_name(item.name)?;
472 self.word_space(":")?;
473 self.print_type(&t)?;
474 word(&mut self.s, ";")?;
475 self.end()?; // end the head-ibox
476 self.end() // end the outer cbox
481 fn print_associated_const(&mut self,
484 default: Option<hir::BodyId>,
485 vis: &hir::Visibility)
487 word(&mut self.s, &visibility_qualified(vis, ""))?;
488 self.word_space("const")?;
489 self.print_name(name)?;
490 self.word_space(":")?;
491 self.print_type(ty)?;
492 if let Some(expr) = default {
494 self.word_space("=")?;
495 self.ann.nested(self, Nested::Body(expr))?;
497 word(&mut self.s, ";")
500 fn print_associated_type(&mut self,
502 bounds: Option<&hir::TyParamBounds>,
503 ty: Option<&hir::Ty>)
505 self.word_space("type")?;
506 self.print_name(name)?;
507 if let Some(bounds) = bounds {
508 self.print_bounds(":", bounds)?;
510 if let Some(ty) = ty {
512 self.word_space("=")?;
513 self.print_type(ty)?;
515 word(&mut self.s, ";")
518 /// Pretty-print an item
519 pub fn print_item(&mut self, item: &hir::Item) -> io::Result<()> {
520 self.hardbreak_if_not_bol()?;
521 self.maybe_print_comment(item.span.lo)?;
522 self.print_outer_attributes(&item.attrs)?;
523 self.ann.pre(self, NodeItem(item))?;
525 hir::ItemExternCrate(ref optional_path) => {
526 self.head(&visibility_qualified(&item.vis, "extern crate"))?;
527 if let Some(p) = *optional_path {
528 let val = p.as_str();
529 if val.contains("-") {
530 self.print_string(&val, ast::StrStyle::Cooked)?;
535 word(&mut self.s, "as")?;
538 self.print_name(item.name)?;
539 word(&mut self.s, ";")?;
540 self.end()?; // end inner head-block
541 self.end()?; // end outer head-block
543 hir::ItemUse(ref path, kind) => {
544 self.head(&visibility_qualified(&item.vis, "use"))?;
545 self.print_path(path, false)?;
548 hir::UseKind::Single => {
549 if path.segments.last().unwrap().name != item.name {
551 self.word_space("as")?;
552 self.print_name(item.name)?;
554 word(&mut self.s, ";")?;
556 hir::UseKind::Glob => word(&mut self.s, "::*;")?,
557 hir::UseKind::ListStem => word(&mut self.s, "::{};")?
559 self.end()?; // end inner head-block
560 self.end()?; // end outer head-block
562 hir::ItemStatic(ref ty, m, expr) => {
563 self.head(&visibility_qualified(&item.vis, "static"))?;
564 if m == hir::MutMutable {
565 self.word_space("mut")?;
567 self.print_name(item.name)?;
568 self.word_space(":")?;
569 self.print_type(&ty)?;
571 self.end()?; // end the head-ibox
573 self.word_space("=")?;
574 self.ann.nested(self, Nested::Body(expr))?;
575 word(&mut self.s, ";")?;
576 self.end()?; // end the outer cbox
578 hir::ItemConst(ref ty, expr) => {
579 self.head(&visibility_qualified(&item.vis, "const"))?;
580 self.print_name(item.name)?;
581 self.word_space(":")?;
582 self.print_type(&ty)?;
584 self.end()?; // end the head-ibox
586 self.word_space("=")?;
587 self.ann.nested(self, Nested::Body(expr))?;
588 word(&mut self.s, ";")?;
589 self.end()?; // end the outer cbox
591 hir::ItemFn(ref decl, unsafety, constness, abi, ref typarams, body) => {
602 word(&mut self.s, " ")?;
603 self.end()?; // need to close a box
604 self.end()?; // need to close a box
605 self.ann.nested(self, Nested::Body(body))?;
607 hir::ItemMod(ref _mod) => {
608 self.head(&visibility_qualified(&item.vis, "mod"))?;
609 self.print_name(item.name)?;
612 self.print_mod(_mod, &item.attrs)?;
613 self.bclose(item.span)?;
615 hir::ItemForeignMod(ref nmod) => {
616 self.head("extern")?;
617 self.word_nbsp(&nmod.abi.to_string())?;
619 self.print_foreign_mod(nmod, &item.attrs)?;
620 self.bclose(item.span)?;
622 hir::ItemTy(ref ty, ref params) => {
623 self.ibox(indent_unit)?;
625 self.word_nbsp(&visibility_qualified(&item.vis, "type"))?;
626 self.print_name(item.name)?;
627 self.print_generics(params)?;
628 self.end()?; // end the inner ibox
630 self.print_where_clause(¶ms.where_clause)?;
632 self.word_space("=")?;
633 self.print_type(&ty)?;
634 word(&mut self.s, ";")?;
635 self.end()?; // end the outer ibox
637 hir::ItemEnum(ref enum_definition, ref params) => {
638 self.print_enum_def(enum_definition, params, item.name, item.span, &item.vis)?;
640 hir::ItemStruct(ref struct_def, ref generics) => {
641 self.head(&visibility_qualified(&item.vis, "struct"))?;
642 self.print_struct(struct_def, generics, item.name, item.span, true)?;
644 hir::ItemUnion(ref struct_def, ref generics) => {
645 self.head(&visibility_qualified(&item.vis, "union"))?;
646 self.print_struct(struct_def, generics, item.name, item.span, true)?;
648 hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
650 self.print_visibility(&item.vis)?;
651 self.print_unsafety(unsafety)?;
652 self.word_nbsp("impl")?;
653 self.print_trait_ref(trait_ref)?;
655 self.word_space("for")?;
656 self.word_space("..")?;
658 self.bclose(item.span)?;
660 hir::ItemImpl(unsafety,
667 self.print_visibility(&item.vis)?;
668 self.print_unsafety(unsafety)?;
669 self.word_nbsp("impl")?;
671 if generics.is_parameterized() {
672 self.print_generics(generics)?;
677 hir::ImplPolarity::Negative => {
678 word(&mut self.s, "!")?;
685 self.print_trait_ref(t)?;
687 self.word_space("for")?;
692 self.print_type(&ty)?;
693 self.print_where_clause(&generics.where_clause)?;
697 self.print_inner_attributes(&item.attrs)?;
698 for impl_item in impl_items {
699 self.ann.nested(self, Nested::ImplItem(impl_item.id))?;
701 self.bclose(item.span)?;
703 hir::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
705 self.print_visibility(&item.vis)?;
706 self.print_unsafety(unsafety)?;
707 self.word_nbsp("trait")?;
708 self.print_name(item.name)?;
709 self.print_generics(generics)?;
710 let mut real_bounds = Vec::with_capacity(bounds.len());
711 for b in bounds.iter() {
712 if let TraitTyParamBound(ref ptr, hir::TraitBoundModifier::Maybe) = *b {
714 self.word_space("for ?")?;
715 self.print_trait_ref(&ptr.trait_ref)?;
717 real_bounds.push(b.clone());
720 self.print_bounds(":", &real_bounds[..])?;
721 self.print_where_clause(&generics.where_clause)?;
722 word(&mut self.s, " ")?;
724 for trait_item in trait_items {
725 self.ann.nested(self, Nested::TraitItem(trait_item.id))?;
727 self.bclose(item.span)?;
730 self.ann.post(self, NodeItem(item))
733 pub fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
734 self.print_path(&t.path, false)
737 fn print_formal_lifetime_list(&mut self, lifetimes: &[hir::LifetimeDef]) -> io::Result<()> {
738 if !lifetimes.is_empty() {
739 word(&mut self.s, "for<")?;
740 let mut comma = false;
741 for lifetime_def in lifetimes {
743 self.word_space(",")?
745 self.print_lifetime_def(lifetime_def)?;
748 word(&mut self.s, ">")?;
753 fn print_poly_trait_ref(&mut self, t: &hir::PolyTraitRef) -> io::Result<()> {
754 self.print_formal_lifetime_list(&t.bound_lifetimes)?;
755 self.print_trait_ref(&t.trait_ref)
758 pub fn print_enum_def(&mut self,
759 enum_definition: &hir::EnumDef,
760 generics: &hir::Generics,
762 span: syntax_pos::Span,
763 visibility: &hir::Visibility)
765 self.head(&visibility_qualified(visibility, "enum"))?;
766 self.print_name(name)?;
767 self.print_generics(generics)?;
768 self.print_where_clause(&generics.where_clause)?;
770 self.print_variants(&enum_definition.variants, span)
773 pub fn print_variants(&mut self,
774 variants: &[hir::Variant],
775 span: syntax_pos::Span)
779 self.space_if_not_bol()?;
780 self.maybe_print_comment(v.span.lo)?;
781 self.print_outer_attributes(&v.node.attrs)?;
782 self.ibox(indent_unit)?;
783 self.print_variant(v)?;
784 word(&mut self.s, ",")?;
786 self.maybe_print_trailing_comment(v.span, None)?;
791 pub fn print_visibility(&mut self, vis: &hir::Visibility) -> io::Result<()> {
793 hir::Public => self.word_nbsp("pub"),
794 hir::Visibility::Crate => self.word_nbsp("pub(crate)"),
795 hir::Visibility::Restricted { ref path, .. } => {
796 word(&mut self.s, "pub(")?;
797 self.print_path(path, false)?;
800 hir::Inherited => Ok(()),
804 pub fn print_struct(&mut self,
805 struct_def: &hir::VariantData,
806 generics: &hir::Generics,
808 span: syntax_pos::Span,
809 print_finalizer: bool)
811 self.print_name(name)?;
812 self.print_generics(generics)?;
813 if !struct_def.is_struct() {
814 if struct_def.is_tuple() {
816 self.commasep(Inconsistent, struct_def.fields(), |s, field| {
817 s.maybe_print_comment(field.span.lo)?;
818 s.print_outer_attributes(&field.attrs)?;
819 s.print_visibility(&field.vis)?;
820 s.print_type(&field.ty)
824 self.print_where_clause(&generics.where_clause)?;
826 word(&mut self.s, ";")?;
829 self.end() // close the outer-box
831 self.print_where_clause(&generics.where_clause)?;
834 self.hardbreak_if_not_bol()?;
836 for field in struct_def.fields() {
837 self.hardbreak_if_not_bol()?;
838 self.maybe_print_comment(field.span.lo)?;
839 self.print_outer_attributes(&field.attrs)?;
840 self.print_visibility(&field.vis)?;
841 self.print_name(field.name)?;
842 self.word_nbsp(":")?;
843 self.print_type(&field.ty)?;
844 word(&mut self.s, ",")?;
851 pub fn print_variant(&mut self, v: &hir::Variant) -> io::Result<()> {
853 let generics = hir::Generics::empty();
854 self.print_struct(&v.node.data, &generics, v.node.name, v.span, false)?;
855 if let Some(d) = v.node.disr_expr {
857 self.word_space("=")?;
858 self.ann.nested(self, Nested::Body(d))?;
862 pub fn print_method_sig(&mut self,
865 vis: &hir::Visibility,
866 arg_names: &[Spanned<ast::Name>],
867 body_id: Option<hir::BodyId>)
869 self.print_fn(&m.decl,
880 pub fn print_trait_item(&mut self, ti: &hir::TraitItem) -> io::Result<()> {
881 self.ann.pre(self, NodeSubItem(ti.id))?;
882 self.hardbreak_if_not_bol()?;
883 self.maybe_print_comment(ti.span.lo)?;
884 self.print_outer_attributes(&ti.attrs)?;
886 hir::TraitItemKind::Const(ref ty, default) => {
887 self.print_associated_const(ti.name, &ty, default, &hir::Inherited)?;
889 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref arg_names)) => {
890 self.print_method_sig(ti.name, sig, &hir::Inherited, arg_names, None)?;
891 word(&mut self.s, ";")?;
893 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
895 self.print_method_sig(ti.name, sig, &hir::Inherited, &[], Some(body))?;
897 self.end()?; // need to close a box
898 self.end()?; // need to close a box
899 self.ann.nested(self, Nested::Body(body))?;
901 hir::TraitItemKind::Type(ref bounds, ref default) => {
902 self.print_associated_type(ti.name,
904 default.as_ref().map(|ty| &**ty))?;
907 self.ann.post(self, NodeSubItem(ti.id))
910 pub fn print_impl_item(&mut self, ii: &hir::ImplItem) -> io::Result<()> {
911 self.ann.pre(self, NodeSubItem(ii.id))?;
912 self.hardbreak_if_not_bol()?;
913 self.maybe_print_comment(ii.span.lo)?;
914 self.print_outer_attributes(&ii.attrs)?;
916 match ii.defaultness {
917 hir::Defaultness::Default { .. } => self.word_nbsp("default")?,
918 hir::Defaultness::Final => (),
922 hir::ImplItemKind::Const(ref ty, expr) => {
923 self.print_associated_const(ii.name, &ty, Some(expr), &ii.vis)?;
925 hir::ImplItemKind::Method(ref sig, body) => {
927 self.print_method_sig(ii.name, sig, &ii.vis, &[], Some(body))?;
929 self.end()?; // need to close a box
930 self.end()?; // need to close a box
931 self.ann.nested(self, Nested::Body(body))?;
933 hir::ImplItemKind::Type(ref ty) => {
934 self.print_associated_type(ii.name, None, Some(ty))?;
937 self.ann.post(self, NodeSubItem(ii.id))
940 pub fn print_stmt(&mut self, st: &hir::Stmt) -> io::Result<()> {
941 self.maybe_print_comment(st.span.lo)?;
943 hir::StmtDecl(ref decl, _) => {
944 self.print_decl(&decl)?;
946 hir::StmtExpr(ref expr, _) => {
947 self.space_if_not_bol()?;
948 self.print_expr(&expr)?;
950 hir::StmtSemi(ref expr, _) => {
951 self.space_if_not_bol()?;
952 self.print_expr(&expr)?;
953 word(&mut self.s, ";")?;
956 if stmt_ends_with_semi(&st.node) {
957 word(&mut self.s, ";")?;
959 self.maybe_print_trailing_comment(st.span, None)
962 pub fn print_block(&mut self, blk: &hir::Block) -> io::Result<()> {
963 self.print_block_with_attrs(blk, &[])
966 pub fn print_block_unclosed(&mut self, blk: &hir::Block) -> io::Result<()> {
967 self.print_block_unclosed_indent(blk, indent_unit)
970 pub fn print_block_unclosed_indent(&mut self,
974 self.print_block_maybe_unclosed(blk, indented, &[], false)
977 pub fn print_block_with_attrs(&mut self,
979 attrs: &[ast::Attribute])
981 self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
984 pub fn print_block_maybe_unclosed(&mut self,
987 attrs: &[ast::Attribute],
991 hir::UnsafeBlock(..) => self.word_space("unsafe")?,
992 hir::PushUnsafeBlock(..) => self.word_space("push_unsafe")?,
993 hir::PopUnsafeBlock(..) => self.word_space("pop_unsafe")?,
994 hir::DefaultBlock => (),
996 self.maybe_print_comment(blk.span.lo)?;
997 self.ann.pre(self, NodeBlock(blk))?;
1000 self.print_inner_attributes(attrs)?;
1002 for st in &blk.stmts {
1003 self.print_stmt(st)?;
1007 self.space_if_not_bol()?;
1008 self.print_expr(&expr)?;
1009 self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi))?;
1013 self.bclose_maybe_open(blk.span, indented, close_box)?;
1014 self.ann.post(self, NodeBlock(blk))
1017 fn print_else(&mut self, els: Option<&hir::Expr>) -> io::Result<()> {
1021 // "another else-if"
1022 hir::ExprIf(ref i, ref then, ref e) => {
1023 self.cbox(indent_unit - 1)?;
1025 word(&mut self.s, " else if ")?;
1026 self.print_expr(&i)?;
1027 space(&mut self.s)?;
1028 self.print_block(&then)?;
1029 self.print_else(e.as_ref().map(|e| &**e))
1032 hir::ExprBlock(ref b) => {
1033 self.cbox(indent_unit - 1)?;
1035 word(&mut self.s, " else ")?;
1036 self.print_block(&b)
1038 // BLEAH, constraints would be great here
1040 panic!("print_if saw if with weird alternative");
1048 pub fn print_if(&mut self,
1051 elseopt: Option<&hir::Expr>)
1054 self.print_expr(test)?;
1055 space(&mut self.s)?;
1056 self.print_block(blk)?;
1057 self.print_else(elseopt)
1060 pub fn print_if_let(&mut self,
1064 elseopt: Option<&hir::Expr>)
1066 self.head("if let")?;
1067 self.print_pat(pat)?;
1068 space(&mut self.s)?;
1069 self.word_space("=")?;
1070 self.print_expr(expr)?;
1071 space(&mut self.s)?;
1072 self.print_block(blk)?;
1073 self.print_else(elseopt)
1077 fn print_call_post(&mut self, args: &[hir::Expr]) -> io::Result<()> {
1079 self.commasep_exprs(Inconsistent, args)?;
1083 pub fn print_expr_maybe_paren(&mut self, expr: &hir::Expr) -> io::Result<()> {
1084 let needs_par = needs_parentheses(expr);
1088 self.print_expr(expr)?;
1095 fn print_expr_vec(&mut self, exprs: &[hir::Expr]) -> io::Result<()> {
1096 self.ibox(indent_unit)?;
1097 word(&mut self.s, "[")?;
1098 self.commasep_exprs(Inconsistent, exprs)?;
1099 word(&mut self.s, "]")?;
1103 fn print_expr_repeat(&mut self, element: &hir::Expr, count: hir::BodyId) -> io::Result<()> {
1104 self.ibox(indent_unit)?;
1105 word(&mut self.s, "[")?;
1106 self.print_expr(element)?;
1107 self.word_space(";")?;
1108 self.ann.nested(self, Nested::Body(count))?;
1109 word(&mut self.s, "]")?;
1113 fn print_expr_struct(&mut self,
1115 fields: &[hir::Field],
1116 wth: &Option<P<hir::Expr>>)
1118 self.print_qpath(qpath, true)?;
1119 word(&mut self.s, "{")?;
1120 self.commasep_cmnt(Consistent,
1123 s.ibox(indent_unit)?;
1124 if !field.is_shorthand {
1125 s.print_name(field.name.node)?;
1128 s.print_expr(&field.expr)?;
1134 self.ibox(indent_unit)?;
1135 if !fields.is_empty() {
1136 word(&mut self.s, ",")?;
1137 space(&mut self.s)?;
1139 word(&mut self.s, "..")?;
1140 self.print_expr(&expr)?;
1143 _ => if !fields.is_empty() {
1144 word(&mut self.s, ",")?
1147 word(&mut self.s, "}")?;
1151 fn print_expr_tup(&mut self, exprs: &[hir::Expr]) -> io::Result<()> {
1153 self.commasep_exprs(Inconsistent, exprs)?;
1154 if exprs.len() == 1 {
1155 word(&mut self.s, ",")?;
1160 fn print_expr_call(&mut self, func: &hir::Expr, args: &[hir::Expr]) -> io::Result<()> {
1161 self.print_expr_maybe_paren(func)?;
1162 self.print_call_post(args)
1165 fn print_expr_method_call(&mut self,
1166 name: Spanned<ast::Name>,
1170 let base_args = &args[1..];
1171 self.print_expr(&args[0])?;
1172 word(&mut self.s, ".")?;
1173 self.print_name(name.node)?;
1174 if !tys.is_empty() {
1175 word(&mut self.s, "::<")?;
1176 self.commasep(Inconsistent, tys, |s, ty| s.print_type(&ty))?;
1177 word(&mut self.s, ">")?;
1179 self.print_call_post(base_args)
1182 fn print_expr_binary(&mut self,
1187 self.print_expr(lhs)?;
1188 space(&mut self.s)?;
1189 self.word_space(op.node.as_str())?;
1190 self.print_expr(rhs)
1193 fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr) -> io::Result<()> {
1194 word(&mut self.s, op.as_str())?;
1195 self.print_expr_maybe_paren(expr)
1198 fn print_expr_addr_of(&mut self,
1199 mutability: hir::Mutability,
1202 word(&mut self.s, "&")?;
1203 self.print_mutability(mutability)?;
1204 self.print_expr_maybe_paren(expr)
1207 pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
1208 self.maybe_print_comment(expr.span.lo)?;
1209 self.print_outer_attributes(&expr.attrs)?;
1210 self.ibox(indent_unit)?;
1211 self.ann.pre(self, NodeExpr(expr))?;
1213 hir::ExprBox(ref expr) => {
1214 self.word_space("box")?;
1215 self.print_expr(expr)?;
1217 hir::ExprArray(ref exprs) => {
1218 self.print_expr_vec(exprs)?;
1220 hir::ExprRepeat(ref element, count) => {
1221 self.print_expr_repeat(&element, count)?;
1223 hir::ExprStruct(ref qpath, ref fields, ref wth) => {
1224 self.print_expr_struct(qpath, &fields[..], wth)?;
1226 hir::ExprTup(ref exprs) => {
1227 self.print_expr_tup(exprs)?;
1229 hir::ExprCall(ref func, ref args) => {
1230 self.print_expr_call(&func, args)?;
1232 hir::ExprMethodCall(name, ref tys, ref args) => {
1233 self.print_expr_method_call(name, &tys[..], args)?;
1235 hir::ExprBinary(op, ref lhs, ref rhs) => {
1236 self.print_expr_binary(op, &lhs, &rhs)?;
1238 hir::ExprUnary(op, ref expr) => {
1239 self.print_expr_unary(op, &expr)?;
1241 hir::ExprAddrOf(m, ref expr) => {
1242 self.print_expr_addr_of(m, &expr)?;
1244 hir::ExprLit(ref lit) => {
1245 self.print_literal(&lit)?;
1247 hir::ExprCast(ref expr, ref ty) => {
1248 self.print_expr(&expr)?;
1249 space(&mut self.s)?;
1250 self.word_space("as")?;
1251 self.print_type(&ty)?;
1253 hir::ExprType(ref expr, ref ty) => {
1254 self.print_expr(&expr)?;
1255 self.word_space(":")?;
1256 self.print_type(&ty)?;
1258 hir::ExprIf(ref test, ref blk, ref elseopt) => {
1259 self.print_if(&test, &blk, elseopt.as_ref().map(|e| &**e))?;
1261 hir::ExprWhile(ref test, ref blk, opt_sp_name) => {
1262 if let Some(sp_name) = opt_sp_name {
1263 self.print_name(sp_name.node)?;
1264 self.word_space(":")?;
1266 self.head("while")?;
1267 self.print_expr(&test)?;
1268 space(&mut self.s)?;
1269 self.print_block(&blk)?;
1271 hir::ExprLoop(ref blk, opt_sp_name, _) => {
1272 if let Some(sp_name) = opt_sp_name {
1273 self.print_name(sp_name.node)?;
1274 self.word_space(":")?;
1277 space(&mut self.s)?;
1278 self.print_block(&blk)?;
1280 hir::ExprMatch(ref expr, ref arms, _) => {
1281 self.cbox(indent_unit)?;
1283 self.word_nbsp("match")?;
1284 self.print_expr(&expr)?;
1285 space(&mut self.s)?;
1288 self.print_arm(arm)?;
1290 self.bclose_(expr.span, indent_unit)?;
1292 hir::ExprClosure(capture_clause, ref decl, body, _fn_decl_span) => {
1293 self.print_capture_clause(capture_clause)?;
1295 self.print_closure_args(&decl, body)?;
1296 space(&mut self.s)?;
1298 // this is a bare expression
1299 self.ann.nested(self, Nested::Body(body))?;
1300 self.end()?; // need to close a box
1302 // a box will be closed by print_expr, but we didn't want an overall
1303 // wrapper so we closed the corresponding opening. so create an
1304 // empty box to satisfy the close.
1307 hir::ExprBlock(ref blk) => {
1308 // containing cbox, will be closed by print-block at }
1309 self.cbox(indent_unit)?;
1310 // head-box, will be closed by print-block after {
1312 self.print_block(&blk)?;
1314 hir::ExprAssign(ref lhs, ref rhs) => {
1315 self.print_expr(&lhs)?;
1316 space(&mut self.s)?;
1317 self.word_space("=")?;
1318 self.print_expr(&rhs)?;
1320 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
1321 self.print_expr(&lhs)?;
1322 space(&mut self.s)?;
1323 word(&mut self.s, op.node.as_str())?;
1324 self.word_space("=")?;
1325 self.print_expr(&rhs)?;
1327 hir::ExprField(ref expr, name) => {
1328 self.print_expr(&expr)?;
1329 word(&mut self.s, ".")?;
1330 self.print_name(name.node)?;
1332 hir::ExprTupField(ref expr, id) => {
1333 self.print_expr(&expr)?;
1334 word(&mut self.s, ".")?;
1335 self.print_usize(id.node)?;
1337 hir::ExprIndex(ref expr, ref index) => {
1338 self.print_expr(&expr)?;
1339 word(&mut self.s, "[")?;
1340 self.print_expr(&index)?;
1341 word(&mut self.s, "]")?;
1343 hir::ExprPath(ref qpath) => {
1344 self.print_qpath(qpath, true)?
1346 hir::ExprBreak(opt_label, ref opt_expr) => {
1347 word(&mut self.s, "break")?;
1348 space(&mut self.s)?;
1349 if let Some(label) = opt_label {
1350 self.print_name(label.name)?;
1351 space(&mut self.s)?;
1353 if let Some(ref expr) = *opt_expr {
1354 self.print_expr(expr)?;
1355 space(&mut self.s)?;
1358 hir::ExprAgain(opt_label) => {
1359 word(&mut self.s, "continue")?;
1360 space(&mut self.s)?;
1361 if let Some(label) = opt_label {
1362 self.print_name(label.name)?;
1366 hir::ExprRet(ref result) => {
1367 word(&mut self.s, "return")?;
1370 word(&mut self.s, " ")?;
1371 self.print_expr(&expr)?;
1376 hir::ExprInlineAsm(ref a, ref outputs, ref inputs) => {
1377 word(&mut self.s, "asm!")?;
1379 self.print_string(&a.asm.as_str(), a.asm_str_style)?;
1380 self.word_space(":")?;
1382 let mut out_idx = 0;
1383 self.commasep(Inconsistent, &a.outputs, |s, out| {
1384 let constraint = out.constraint.as_str();
1385 let mut ch = constraint.chars();
1387 Some('=') if out.is_rw => {
1388 s.print_string(&format!("+{}", ch.as_str()),
1389 ast::StrStyle::Cooked)?
1391 _ => s.print_string(&constraint, ast::StrStyle::Cooked)?,
1394 s.print_expr(&outputs[out_idx])?;
1399 space(&mut self.s)?;
1400 self.word_space(":")?;
1403 self.commasep(Inconsistent, &a.inputs, |s, co| {
1404 s.print_string(&co.as_str(), ast::StrStyle::Cooked)?;
1406 s.print_expr(&inputs[in_idx])?;
1411 space(&mut self.s)?;
1412 self.word_space(":")?;
1414 self.commasep(Inconsistent, &a.clobbers, |s, co| {
1415 s.print_string(&co.as_str(), ast::StrStyle::Cooked)?;
1419 let mut options = vec![];
1421 options.push("volatile");
1424 options.push("alignstack");
1426 if a.dialect == ast::AsmDialect::Intel {
1427 options.push("intel");
1430 if !options.is_empty() {
1431 space(&mut self.s)?;
1432 self.word_space(":")?;
1433 self.commasep(Inconsistent, &options, |s, &co| {
1434 s.print_string(co, ast::StrStyle::Cooked)?;
1442 self.ann.post(self, NodeExpr(expr))?;
1446 pub fn print_local_decl(&mut self, loc: &hir::Local) -> io::Result<()> {
1447 self.print_pat(&loc.pat)?;
1448 if let Some(ref ty) = loc.ty {
1449 self.word_space(":")?;
1450 self.print_type(&ty)?;
1455 pub fn print_decl(&mut self, decl: &hir::Decl) -> io::Result<()> {
1456 self.maybe_print_comment(decl.span.lo)?;
1458 hir::DeclLocal(ref loc) => {
1459 self.space_if_not_bol()?;
1460 self.ibox(indent_unit)?;
1461 self.word_nbsp("let")?;
1463 self.ibox(indent_unit)?;
1464 self.print_local_decl(&loc)?;
1466 if let Some(ref init) = loc.init {
1468 self.word_space("=")?;
1469 self.print_expr(&init)?;
1473 hir::DeclItem(item) => {
1474 self.ann.nested(self, Nested::Item(item))
1479 pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
1480 word(&mut self.s, &i.to_string())
1483 pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1484 word(&mut self.s, &name.as_str())?;
1485 self.ann.post(self, NodeName(&name))
1488 pub fn print_for_decl(&mut self, loc: &hir::Local, coll: &hir::Expr) -> io::Result<()> {
1489 self.print_local_decl(loc)?;
1490 space(&mut self.s)?;
1491 self.word_space("in")?;
1492 self.print_expr(coll)
1495 pub fn print_path(&mut self,
1497 colons_before_params: bool)
1499 self.maybe_print_comment(path.span.lo)?;
1501 for (i, segment) in path.segments.iter().enumerate() {
1503 word(&mut self.s, "::")?
1505 if segment.name != keywords::CrateRoot.name() && segment.name != "$crate" {
1506 self.print_name(segment.name)?;
1507 self.print_path_parameters(&segment.parameters, colons_before_params)?;
1514 pub fn print_qpath(&mut self,
1516 colons_before_params: bool)
1519 hir::QPath::Resolved(None, ref path) => {
1520 self.print_path(path, colons_before_params)
1522 hir::QPath::Resolved(Some(ref qself), ref path) => {
1523 word(&mut self.s, "<")?;
1524 self.print_type(qself)?;
1525 space(&mut self.s)?;
1526 self.word_space("as")?;
1528 for (i, segment) in path.segments[..path.segments.len() - 1].iter().enumerate() {
1530 word(&mut self.s, "::")?
1532 if segment.name != keywords::CrateRoot.name() && segment.name != "$crate" {
1533 self.print_name(segment.name)?;
1534 self.print_path_parameters(&segment.parameters, colons_before_params)?;
1538 word(&mut self.s, ">")?;
1539 word(&mut self.s, "::")?;
1540 let item_segment = path.segments.last().unwrap();
1541 self.print_name(item_segment.name)?;
1542 self.print_path_parameters(&item_segment.parameters, colons_before_params)
1544 hir::QPath::TypeRelative(ref qself, ref item_segment) => {
1545 word(&mut self.s, "<")?;
1546 self.print_type(qself)?;
1547 word(&mut self.s, ">")?;
1548 word(&mut self.s, "::")?;
1549 self.print_name(item_segment.name)?;
1550 self.print_path_parameters(&item_segment.parameters, colons_before_params)
1555 fn print_path_parameters(&mut self,
1556 parameters: &hir::PathParameters,
1557 colons_before_params: bool)
1559 if parameters.is_empty() {
1560 let infer_types = match *parameters {
1561 hir::AngleBracketedParameters(ref data) => data.infer_types,
1562 hir::ParenthesizedParameters(_) => false
1565 // FIXME(eddyb) See the comment below about infer_types.
1566 if !(infer_types && false) {
1571 if colons_before_params {
1572 word(&mut self.s, "::")?
1576 hir::AngleBracketedParameters(ref data) => {
1577 word(&mut self.s, "<")?;
1579 let mut comma = false;
1580 for lifetime in &data.lifetimes {
1582 self.word_space(",")?
1584 self.print_lifetime(lifetime)?;
1588 if !data.types.is_empty() {
1590 self.word_space(",")?
1592 self.commasep(Inconsistent, &data.types, |s, ty| s.print_type(&ty))?;
1596 // FIXME(eddyb) This would leak into error messages, e.g.:
1597 // "non-exhaustive patterns: `Some::<..>(_)` not covered".
1598 if data.infer_types && false {
1600 self.word_space(",")?
1602 word(&mut self.s, "..")?;
1606 for binding in data.bindings.iter() {
1608 self.word_space(",")?
1610 self.print_name(binding.name)?;
1611 space(&mut self.s)?;
1612 self.word_space("=")?;
1613 self.print_type(&binding.ty)?;
1617 word(&mut self.s, ">")?
1620 hir::ParenthesizedParameters(ref data) => {
1621 word(&mut self.s, "(")?;
1622 self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(&ty))?;
1623 word(&mut self.s, ")")?;
1625 if let Some(ref ty) = data.output {
1626 self.space_if_not_bol()?;
1627 self.word_space("->")?;
1628 self.print_type(&ty)?;
1636 pub fn print_pat(&mut self, pat: &hir::Pat) -> io::Result<()> {
1637 self.maybe_print_comment(pat.span.lo)?;
1638 self.ann.pre(self, NodePat(pat))?;
1639 // Pat isn't normalized, but the beauty of it
1640 // is that it doesn't matter
1642 PatKind::Wild => word(&mut self.s, "_")?,
1643 PatKind::Binding(binding_mode, _, ref path1, ref sub) => {
1644 match binding_mode {
1645 hir::BindByRef(mutbl) => {
1646 self.word_nbsp("ref")?;
1647 self.print_mutability(mutbl)?;
1649 hir::BindByValue(hir::MutImmutable) => {}
1650 hir::BindByValue(hir::MutMutable) => {
1651 self.word_nbsp("mut")?;
1654 self.print_name(path1.node)?;
1655 if let Some(ref p) = *sub {
1656 word(&mut self.s, "@")?;
1657 self.print_pat(&p)?;
1660 PatKind::TupleStruct(ref qpath, ref elts, ddpos) => {
1661 self.print_qpath(qpath, true)?;
1663 if let Some(ddpos) = ddpos {
1664 self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p))?;
1666 self.word_space(",")?;
1668 word(&mut self.s, "..")?;
1669 if ddpos != elts.len() {
1670 word(&mut self.s, ",")?;
1671 self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p))?;
1674 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p))?;
1678 PatKind::Path(ref qpath) => {
1679 self.print_qpath(qpath, true)?;
1681 PatKind::Struct(ref qpath, ref fields, etc) => {
1682 self.print_qpath(qpath, true)?;
1684 self.word_space("{")?;
1685 self.commasep_cmnt(Consistent,
1688 s.cbox(indent_unit)?;
1689 if !f.node.is_shorthand {
1690 s.print_name(f.node.name)?;
1693 s.print_pat(&f.node.pat)?;
1696 |f| f.node.pat.span)?;
1698 if !fields.is_empty() {
1699 self.word_space(",")?;
1701 word(&mut self.s, "..")?;
1703 space(&mut self.s)?;
1704 word(&mut self.s, "}")?;
1706 PatKind::Tuple(ref elts, ddpos) => {
1708 if let Some(ddpos) = ddpos {
1709 self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p))?;
1711 self.word_space(",")?;
1713 word(&mut self.s, "..")?;
1714 if ddpos != elts.len() {
1715 word(&mut self.s, ",")?;
1716 self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p))?;
1719 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p))?;
1720 if elts.len() == 1 {
1721 word(&mut self.s, ",")?;
1726 PatKind::Box(ref inner) => {
1727 word(&mut self.s, "box ")?;
1728 self.print_pat(&inner)?;
1730 PatKind::Ref(ref inner, mutbl) => {
1731 word(&mut self.s, "&")?;
1732 if mutbl == hir::MutMutable {
1733 word(&mut self.s, "mut ")?;
1735 self.print_pat(&inner)?;
1737 PatKind::Lit(ref e) => self.print_expr(&e)?,
1738 PatKind::Range(ref begin, ref end) => {
1739 self.print_expr(&begin)?;
1740 space(&mut self.s)?;
1741 word(&mut self.s, "...")?;
1742 self.print_expr(&end)?;
1744 PatKind::Slice(ref before, ref slice, ref after) => {
1745 word(&mut self.s, "[")?;
1746 self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p))?;
1747 if let Some(ref p) = *slice {
1748 if !before.is_empty() {
1749 self.word_space(",")?;
1751 if p.node != PatKind::Wild {
1752 self.print_pat(&p)?;
1754 word(&mut self.s, "..")?;
1755 if !after.is_empty() {
1756 self.word_space(",")?;
1759 self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p))?;
1760 word(&mut self.s, "]")?;
1763 self.ann.post(self, NodePat(pat))
1766 fn print_arm(&mut self, arm: &hir::Arm) -> io::Result<()> {
1767 // I have no idea why this check is necessary, but here it
1769 if arm.attrs.is_empty() {
1770 space(&mut self.s)?;
1772 self.cbox(indent_unit)?;
1774 self.print_outer_attributes(&arm.attrs)?;
1775 let mut first = true;
1776 for p in &arm.pats {
1780 space(&mut self.s)?;
1781 self.word_space("|")?;
1783 self.print_pat(&p)?;
1785 space(&mut self.s)?;
1786 if let Some(ref e) = arm.guard {
1787 self.word_space("if")?;
1788 self.print_expr(&e)?;
1789 space(&mut self.s)?;
1791 self.word_space("=>")?;
1793 match arm.body.node {
1794 hir::ExprBlock(ref blk) => {
1795 // the block will close the pattern's ibox
1796 self.print_block_unclosed_indent(&blk, indent_unit)?;
1798 // If it is a user-provided unsafe block, print a comma after it
1799 if let hir::UnsafeBlock(hir::UserProvided) = blk.rules {
1800 word(&mut self.s, ",")?;
1804 self.end()?; // close the ibox for the pattern
1805 self.print_expr(&arm.body)?;
1806 word(&mut self.s, ",")?;
1809 self.end() // close enclosing cbox
1812 pub fn print_fn(&mut self,
1814 unsafety: hir::Unsafety,
1815 constness: hir::Constness,
1817 name: Option<ast::Name>,
1818 generics: &hir::Generics,
1819 vis: &hir::Visibility,
1820 arg_names: &[Spanned<ast::Name>],
1821 body_id: Option<hir::BodyId>)
1823 self.print_fn_header_info(unsafety, constness, abi, vis)?;
1825 if let Some(name) = name {
1827 self.print_name(name)?;
1829 self.print_generics(generics)?;
1833 // Make sure we aren't supplied *both* `arg_names` and `body_id`.
1834 assert!(arg_names.is_empty() || body_id.is_none());
1835 self.commasep(Inconsistent, &decl.inputs, |s, ty| {
1836 s.ibox(indent_unit)?;
1837 if let Some(name) = arg_names.get(i) {
1838 word(&mut s.s, &name.node.as_str())?;
1839 word(&mut s.s, ":")?;
1841 } else if let Some(body_id) = body_id {
1842 s.ann.nested(s, Nested::BodyArgPat(body_id, i))?;
1843 word(&mut s.s, ":")?;
1851 word(&mut self.s, ", ...")?;
1855 self.print_fn_output(decl)?;
1856 self.print_where_clause(&generics.where_clause)
1859 fn print_closure_args(&mut self, decl: &hir::FnDecl, body_id: hir::BodyId) -> io::Result<()> {
1860 word(&mut self.s, "|")?;
1862 self.commasep(Inconsistent, &decl.inputs, |s, ty| {
1863 s.ibox(indent_unit)?;
1865 s.ann.nested(s, Nested::BodyArgPat(body_id, i))?;
1868 if ty.node != hir::TyInfer {
1869 word(&mut s.s, ":")?;
1875 word(&mut self.s, "|")?;
1877 if let hir::DefaultReturn(..) = decl.output {
1881 self.space_if_not_bol()?;
1882 self.word_space("->")?;
1884 hir::Return(ref ty) => {
1885 self.print_type(&ty)?;
1886 self.maybe_print_comment(ty.span.lo)
1888 hir::DefaultReturn(..) => unreachable!(),
1892 pub fn print_capture_clause(&mut self, capture_clause: hir::CaptureClause) -> io::Result<()> {
1893 match capture_clause {
1894 hir::CaptureByValue => self.word_space("move"),
1895 hir::CaptureByRef => Ok(()),
1899 pub fn print_bounds(&mut self, prefix: &str, bounds: &[hir::TyParamBound]) -> io::Result<()> {
1900 if !bounds.is_empty() {
1901 word(&mut self.s, prefix)?;
1902 let mut first = true;
1903 for bound in bounds {
1908 self.word_space("+")?;
1912 TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
1913 self.print_poly_trait_ref(tref)
1915 TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
1916 word(&mut self.s, "?")?;
1917 self.print_poly_trait_ref(tref)
1919 RegionTyParamBound(ref lt) => {
1920 self.print_lifetime(lt)
1930 pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> {
1931 self.print_name(lifetime.name)
1934 pub fn print_lifetime_def(&mut self, lifetime: &hir::LifetimeDef) -> io::Result<()> {
1935 self.print_lifetime(&lifetime.lifetime)?;
1937 for v in &lifetime.bounds {
1938 word(&mut self.s, sep)?;
1939 self.print_lifetime(v)?;
1945 pub fn print_generics(&mut self, generics: &hir::Generics) -> io::Result<()> {
1946 let total = generics.lifetimes.len() + generics.ty_params.len();
1951 word(&mut self.s, "<")?;
1953 let mut ints = Vec::new();
1958 self.commasep(Inconsistent, &ints[..], |s, &idx| {
1959 if idx < generics.lifetimes.len() {
1960 let lifetime = &generics.lifetimes[idx];
1961 s.print_lifetime_def(lifetime)
1963 let idx = idx - generics.lifetimes.len();
1964 let param = &generics.ty_params[idx];
1965 s.print_ty_param(param)
1969 word(&mut self.s, ">")?;
1973 pub fn print_ty_param(&mut self, param: &hir::TyParam) -> io::Result<()> {
1974 self.print_name(param.name)?;
1975 self.print_bounds(":", ¶m.bounds)?;
1976 match param.default {
1977 Some(ref default) => {
1978 space(&mut self.s)?;
1979 self.word_space("=")?;
1980 self.print_type(&default)
1986 pub fn print_where_clause(&mut self, where_clause: &hir::WhereClause) -> io::Result<()> {
1987 if where_clause.predicates.is_empty() {
1991 space(&mut self.s)?;
1992 self.word_space("where")?;
1994 for (i, predicate) in where_clause.predicates.iter().enumerate() {
1996 self.word_space(",")?;
2000 &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ref bound_lifetimes,
2004 self.print_formal_lifetime_list(bound_lifetimes)?;
2005 self.print_type(&bounded_ty)?;
2006 self.print_bounds(":", bounds)?;
2008 &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
2011 self.print_lifetime(lifetime)?;
2012 word(&mut self.s, ":")?;
2014 for (i, bound) in bounds.iter().enumerate() {
2015 self.print_lifetime(bound)?;
2018 word(&mut self.s, ":")?;
2022 &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref lhs_ty,
2025 self.print_type(lhs_ty)?;
2026 space(&mut self.s)?;
2027 self.word_space("=")?;
2028 self.print_type(rhs_ty)?;
2036 pub fn print_mutability(&mut self, mutbl: hir::Mutability) -> io::Result<()> {
2038 hir::MutMutable => self.word_nbsp("mut"),
2039 hir::MutImmutable => Ok(()),
2043 pub fn print_mt(&mut self, mt: &hir::MutTy) -> io::Result<()> {
2044 self.print_mutability(mt.mutbl)?;
2045 self.print_type(&mt.ty)
2048 pub fn print_fn_output(&mut self, decl: &hir::FnDecl) -> io::Result<()> {
2049 if let hir::DefaultReturn(..) = decl.output {
2053 self.space_if_not_bol()?;
2054 self.ibox(indent_unit)?;
2055 self.word_space("->")?;
2057 hir::DefaultReturn(..) => unreachable!(),
2058 hir::Return(ref ty) => self.print_type(&ty)?,
2063 hir::Return(ref output) => self.maybe_print_comment(output.span.lo),
2068 pub fn print_ty_fn(&mut self,
2070 unsafety: hir::Unsafety,
2072 name: Option<ast::Name>,
2073 generics: &hir::Generics)
2075 self.ibox(indent_unit)?;
2076 if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
2077 word(&mut self.s, "for")?;
2078 self.print_generics(generics)?;
2080 let generics = hir::Generics {
2081 lifetimes: hir::HirVec::new(),
2082 ty_params: hir::HirVec::new(),
2083 where_clause: hir::WhereClause {
2084 id: ast::DUMMY_NODE_ID,
2085 predicates: hir::HirVec::new(),
2087 span: syntax_pos::DUMMY_SP,
2091 hir::Constness::NotConst,
2101 pub fn maybe_print_trailing_comment(&mut self,
2102 span: syntax_pos::Span,
2103 next_pos: Option<BytePos>)
2105 let cm = match self.cm {
2109 if let Some(ref cmnt) = self.next_comment() {
2110 if (*cmnt).style != comments::Trailing {
2113 let span_line = cm.lookup_char_pos(span.hi);
2114 let comment_line = cm.lookup_char_pos((*cmnt).pos);
2115 let mut next = (*cmnt).pos + BytePos(1);
2116 if let Some(p) = next_pos {
2119 if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2120 span_line.line == comment_line.line {
2121 self.print_comment(cmnt)?;
2122 self.cur_cmnt_and_lit.cur_cmnt += 1;
2128 pub fn print_remaining_comments(&mut self) -> io::Result<()> {
2129 // If there aren't any remaining comments, then we need to manually
2130 // make sure there is a line break at the end.
2131 if self.next_comment().is_none() {
2132 hardbreak(&mut self.s)?;
2135 match self.next_comment() {
2137 self.print_comment(cmnt)?;
2138 self.cur_cmnt_and_lit.cur_cmnt += 1;
2146 pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
2147 opt_abi: Option<Abi>)
2150 Some(Abi::Rust) => Ok(()),
2152 self.word_nbsp("extern")?;
2153 self.word_nbsp(&abi.to_string())
2159 pub fn print_extern_opt_abi(&mut self, opt_abi: Option<Abi>) -> io::Result<()> {
2162 self.word_nbsp("extern")?;
2163 self.word_nbsp(&abi.to_string())
2169 pub fn print_fn_header_info(&mut self,
2170 unsafety: hir::Unsafety,
2171 constness: hir::Constness,
2173 vis: &hir::Visibility)
2175 word(&mut self.s, &visibility_qualified(vis, ""))?;
2176 self.print_unsafety(unsafety)?;
2179 hir::Constness::NotConst => {}
2180 hir::Constness::Const => self.word_nbsp("const")?,
2183 if abi != Abi::Rust {
2184 self.word_nbsp("extern")?;
2185 self.word_nbsp(&abi.to_string())?;
2188 word(&mut self.s, "fn")
2191 pub fn print_unsafety(&mut self, s: hir::Unsafety) -> io::Result<()> {
2193 hir::Unsafety::Normal => Ok(()),
2194 hir::Unsafety::Unsafe => self.word_nbsp("unsafe"),
2199 // Dup'ed from parse::classify, but adapted for the HIR.
2200 /// Does this expression require a semicolon to be treated
2201 /// as a statement? The negation of this: 'can this expression
2202 /// be used as a statement without a semicolon' -- is used
2203 /// as an early-bail-out in the parser so that, for instance,
2204 /// if true {...} else {...}
2206 /// isn't parsed as (if true {...} else {...} | x) | 5
2207 fn expr_requires_semi_to_be_stmt(e: &hir::Expr) -> bool {
2210 hir::ExprMatch(..) |
2212 hir::ExprWhile(..) |
2213 hir::ExprLoop(..) => false,
2218 /// this statement requires a semicolon after it.
2219 /// note that in one case (stmt_semi), we've already
2220 /// seen the semicolon, and thus don't need another.
2221 fn stmt_ends_with_semi(stmt: &hir::Stmt_) -> bool {
2223 hir::StmtDecl(ref d, _) => {
2225 hir::DeclLocal(_) => true,
2226 hir::DeclItem(_) => false,
2229 hir::StmtExpr(ref e, _) => {
2230 expr_requires_semi_to_be_stmt(&e)
2232 hir::StmtSemi(..) => {