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
::{self, CodeMap, BytePos, Spanned}
;
17 use syntax
::parse
::token
::{self, BinOpToken}
;
18 use syntax
::parse
::lexer
::comments
;
20 use syntax
::print
::pp
::{self, break_offset, word, space, hardbreak}
;
21 use syntax
::print
::pp
::{Breaks, eof}
;
22 use syntax
::print
::pp
::Breaks
::{Consistent, Inconsistent}
;
23 use syntax
::print
::pprust
::{self as ast_pp, PrintState}
;
27 use hir
::{Crate, 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
),
41 fn pre(&self, _state
: &mut State
, _node
: AnnNode
) -> io
::Result
<()> {
44 fn post(&self, _state
: &mut State
, _node
: AnnNode
) -> io
::Result
<()> {
49 #[derive(Copy, Clone)]
52 impl PpAnn
for NoAnn {}
55 pub struct State
<'a
> {
56 krate
: Option
<&'a Crate
>,
57 pub s
: pp
::Printer
<'a
>,
58 cm
: Option
<&'a CodeMap
>,
59 comments
: Option
<Vec
<comments
::Comment
>>,
60 literals
: Option
<Vec
<comments
::Literal
>>,
61 cur_cmnt_and_lit
: ast_pp
::CurrentCommentAndLiteral
,
62 boxes
: Vec
<pp
::Breaks
>,
63 ann
: &'
a (PpAnn
+ 'a
),
66 impl<'a
> PrintState
<'a
> for State
<'a
> {
67 fn writer(&mut self) -> &mut pp
::Printer
<'a
> {
71 fn boxes(&mut self) -> &mut Vec
<pp
::Breaks
> {
75 fn comments(&mut self) -> &mut Option
<Vec
<comments
::Comment
>> {
79 fn cur_cmnt_and_lit(&mut self) -> &mut ast_pp
::CurrentCommentAndLiteral
{
80 &mut self.cur_cmnt_and_lit
83 fn literals(&self) -> &Option
<Vec
<comments
::Literal
>> {
88 pub fn rust_printer
<'a
>(writer
: Box
<Write
+ 'a
>, krate
: Option
<&'a Crate
>) -> State
<'a
> {
89 static NO_ANN
: NoAnn
= NoAnn
;
90 rust_printer_annotated(writer
, &NO_ANN
, krate
)
93 pub fn rust_printer_annotated
<'a
>(writer
: Box
<Write
+ 'a
>,
95 krate
: Option
<&'a Crate
>)
99 s
: pp
::mk_printer(writer
, default_columns
),
103 cur_cmnt_and_lit
: ast_pp
::CurrentCommentAndLiteral
{
112 #[allow(non_upper_case_globals)]
113 pub const indent_unit
: usize = 4;
115 #[allow(non_upper_case_globals)]
116 pub const default_columns
: usize = 78;
119 /// Requires you to pass an input filename and reader so that
120 /// it can scan the input text for comments and literals to
122 pub fn print_crate
<'a
>(cm
: &'a CodeMap
,
123 span_diagnostic
: &errors
::Handler
,
127 out
: Box
<Write
+ 'a
>,
131 let mut s
= State
::new_from_input(cm
, span_diagnostic
, filename
, input
,
132 out
, ann
, is_expanded
, Some(krate
));
134 // When printing the AST, we sometimes need to inject `#[no_std]` here.
135 // Since you can't compile the HIR, it's not necessary.
137 s
.print_mod(&krate
.module
, &krate
.attrs
)?
;
138 s
.print_remaining_comments()?
;
143 pub fn new_from_input(cm
: &'a CodeMap
,
144 span_diagnostic
: &errors
::Handler
,
147 out
: Box
<Write
+ 'a
>,
150 krate
: Option
<&'a Crate
>)
152 let (cmnts
, lits
) = comments
::gather_comments_and_literals(span_diagnostic
,
160 // If the code is post expansion, don't use the table of
161 // literals, since it doesn't correspond with the literals
162 // in the AST anymore.
171 pub fn new(cm
: &'a CodeMap
,
172 out
: Box
<Write
+ 'a
>,
174 comments
: Option
<Vec
<comments
::Comment
>>,
175 literals
: Option
<Vec
<comments
::Literal
>>,
176 krate
: Option
<&'a Crate
>)
180 s
: pp
::mk_printer(out
, default_columns
),
182 comments
: comments
.clone(),
183 literals
: literals
.clone(),
184 cur_cmnt_and_lit
: ast_pp
::CurrentCommentAndLiteral
{
194 pub fn to_string
<F
>(f
: F
) -> String
195 where F
: FnOnce(&mut State
) -> io
::Result
<()>
197 let mut wr
= Vec
::new();
199 let mut printer
= rust_printer(Box
::new(&mut wr
), None
);
200 f(&mut printer
).unwrap();
201 eof(&mut printer
.s
).unwrap();
203 String
::from_utf8(wr
).unwrap()
206 pub fn binop_to_string(op
: BinOpToken
) -> &'
static str {
212 token
::Percent
=> "%",
221 pub fn ty_to_string(ty
: &hir
::Ty
) -> String
{
222 to_string(|s
| s
.print_type(ty
))
225 pub fn bounds_to_string(bounds
: &[hir
::TyParamBound
]) -> String
{
226 to_string(|s
| s
.print_bounds("", bounds
))
229 pub fn pat_to_string(pat
: &hir
::Pat
) -> String
{
230 to_string(|s
| s
.print_pat(pat
))
233 pub fn arm_to_string(arm
: &hir
::Arm
) -> String
{
234 to_string(|s
| s
.print_arm(arm
))
237 pub fn expr_to_string(e
: &hir
::Expr
) -> String
{
238 to_string(|s
| s
.print_expr(e
))
241 pub fn lifetime_to_string(e
: &hir
::Lifetime
) -> String
{
242 to_string(|s
| s
.print_lifetime(e
))
245 pub fn stmt_to_string(stmt
: &hir
::Stmt
) -> String
{
246 to_string(|s
| s
.print_stmt(stmt
))
249 pub fn item_to_string(i
: &hir
::Item
) -> String
{
250 to_string(|s
| s
.print_item(i
))
253 pub fn impl_item_to_string(i
: &hir
::ImplItem
) -> String
{
254 to_string(|s
| s
.print_impl_item(i
))
257 pub fn trait_item_to_string(i
: &hir
::TraitItem
) -> String
{
258 to_string(|s
| s
.print_trait_item(i
))
261 pub fn generics_to_string(generics
: &hir
::Generics
) -> String
{
262 to_string(|s
| s
.print_generics(generics
))
265 pub fn where_clause_to_string(i
: &hir
::WhereClause
) -> String
{
266 to_string(|s
| s
.print_where_clause(i
))
269 pub fn fn_block_to_string(p
: &hir
::FnDecl
) -> String
{
270 to_string(|s
| s
.print_fn_block_args(p
))
273 pub fn path_to_string(p
: &hir
::Path
) -> String
{
274 to_string(|s
| s
.print_path(p
, false, 0))
277 pub fn name_to_string(name
: ast
::Name
) -> String
{
278 to_string(|s
| s
.print_name(name
))
281 pub fn fun_to_string(decl
: &hir
::FnDecl
,
282 unsafety
: hir
::Unsafety
,
283 constness
: hir
::Constness
,
285 opt_explicit_self
: Option
<&hir
::ExplicitSelf_
>,
286 generics
: &hir
::Generics
)
298 s
.end()?
; // Close the head box
299 s
.end() // Close the outer box
303 pub fn block_to_string(blk
: &hir
::Block
) -> String
{
305 // containing cbox, will be closed by print-block at }
306 s
.cbox(indent_unit
)?
;
307 // head-ibox, will be closed by print-block after {
313 pub fn explicit_self_to_string(explicit_self
: &hir
::ExplicitSelf_
) -> String
{
314 to_string(|s
| s
.print_explicit_self(explicit_self
, hir
::MutImmutable
).map(|_
| {}
))
317 pub fn variant_to_string(var
: &hir
::Variant
) -> String
{
318 to_string(|s
| s
.print_variant(var
))
321 pub fn arg_to_string(arg
: &hir
::Arg
) -> String
{
322 to_string(|s
| s
.print_arg(arg
, false))
325 pub fn visibility_qualified(vis
: &hir
::Visibility
, s
: &str) -> String
{
327 hir
::Public
=> format
!("pub {}", s
),
328 hir
::Inherited
=> s
.to_string(),
332 fn needs_parentheses(expr
: &hir
::Expr
) -> bool
{
334 hir
::ExprAssign(..) |
335 hir
::ExprBinary(..) |
336 hir
::ExprClosure(..) |
337 hir
::ExprAssignOp(..) |
339 hir
::ExprType(..) => true,
345 pub fn cbox(&mut self, u
: usize) -> io
::Result
<()> {
346 self.boxes
.push(pp
::Breaks
::Consistent
);
347 pp
::cbox(&mut self.s
, u
)
350 pub fn nbsp(&mut self) -> io
::Result
<()> {
351 word(&mut self.s
, " ")
354 pub fn word_nbsp(&mut self, w
: &str) -> io
::Result
<()> {
355 word(&mut self.s
, w
)?
;
359 pub fn head(&mut self, w
: &str) -> io
::Result
<()> {
360 // outer-box is consistent
361 self.cbox(indent_unit
)?
;
362 // head-box is inconsistent
363 self.ibox(w
.len() + 1)?
;
364 // keyword that starts the head
371 pub fn bopen(&mut self) -> io
::Result
<()> {
372 word(&mut self.s
, "{")?
;
373 self.end() // close the head-box
376 pub fn bclose_(&mut self, span
: codemap
::Span
, indented
: usize) -> io
::Result
<()> {
377 self.bclose_maybe_open(span
, indented
, true)
379 pub fn bclose_maybe_open(&mut self,
384 self.maybe_print_comment(span
.hi
)?
;
385 self.break_offset_if_not_bol(1, -(indented
as isize))?
;
386 word(&mut self.s
, "}")?
;
388 self.end()?
; // close the outer-box
392 pub fn bclose(&mut self, span
: codemap
::Span
) -> io
::Result
<()> {
393 self.bclose_(span
, indent_unit
)
396 pub fn in_cbox(&self) -> bool
{
397 match self.boxes
.last() {
398 Some(&last_box
) => last_box
== pp
::Breaks
::Consistent
,
402 pub fn space_if_not_bol(&mut self) -> io
::Result
<()> {
408 pub fn break_offset_if_not_bol(&mut self, n
: usize, off
: isize) -> io
::Result
<()> {
410 break_offset(&mut self.s
, n
, off
)
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
));
422 // Synthesizes a comment that was not textually present in the original source
424 pub fn synth_comment(&mut self, text
: String
) -> io
::Result
<()> {
425 word(&mut self.s
, "/*")?
;
427 word(&mut self.s
, &text
[..])?
;
429 word(&mut self.s
, "*/")
433 pub fn commasep_cmnt
<T
, F
, G
>(&mut self,
439 where F
: FnMut(&mut State
, &T
) -> io
::Result
<()>,
440 G
: FnMut(&T
) -> codemap
::Span
443 let len
= elts
.len();
446 self.maybe_print_comment(get_span(elt
).hi
)?
;
450 word(&mut self.s
, ",")?
;
451 self.maybe_print_trailing_comment(get_span(elt
), Some(get_span(&elts
[i
]).hi
))?
;
452 self.space_if_not_bol()?
;
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
)
462 pub fn print_mod(&mut self, _mod
: &hir
::Mod
, attrs
: &[ast
::Attribute
]) -> io
::Result
<()> {
463 self.print_inner_attributes(attrs
)?
;
464 for item_id
in &_mod
.item_ids
{
465 self.print_item_id(item_id
)?
;
470 pub fn print_foreign_mod(&mut self,
471 nmod
: &hir
::ForeignMod
,
472 attrs
: &[ast
::Attribute
])
474 self.print_inner_attributes(attrs
)?
;
475 for item
in &nmod
.items
{
476 self.print_foreign_item(item
)?
;
481 pub fn print_opt_lifetime(&mut self, lifetime
: &Option
<hir
::Lifetime
>) -> io
::Result
<()> {
482 if let Some(l
) = *lifetime
{
483 self.print_lifetime(&l
)?
;
489 pub fn print_type(&mut self, ty
: &hir
::Ty
) -> io
::Result
<()> {
490 self.maybe_print_comment(ty
.span
.lo
)?
;
493 hir
::TyVec(ref ty
) => {
494 word(&mut self.s
, "[")?
;
495 self.print_type(&ty
)?
;
496 word(&mut self.s
, "]")?
;
498 hir
::TyPtr(ref mt
) => {
499 word(&mut self.s
, "*")?
;
501 hir
::MutMutable
=> self.word_nbsp("mut")?
,
502 hir
::MutImmutable
=> self.word_nbsp("const")?
,
504 self.print_type(&mt
.ty
)?
;
506 hir
::TyRptr(ref lifetime
, ref mt
) => {
507 word(&mut self.s
, "&")?
;
508 self.print_opt_lifetime(lifetime
)?
;
511 hir
::TyTup(ref elts
) => {
513 self.commasep(Inconsistent
, &elts
[..], |s
, ty
| s
.print_type(&ty
))?
;
515 word(&mut self.s
, ",")?
;
519 hir
::TyBareFn(ref f
) => {
520 let generics
= hir
::Generics
{
521 lifetimes
: f
.lifetimes
.clone(),
522 ty_params
: hir
::HirVec
::new(),
523 where_clause
: hir
::WhereClause
{
524 id
: ast
::DUMMY_NODE_ID
,
525 predicates
: hir
::HirVec
::new(),
528 self.print_ty_fn(f
.abi
, f
.unsafety
, &f
.decl
, None
, &generics
, None
)?
;
530 hir
::TyPath(None
, ref path
) => {
531 self.print_path(path
, false, 0)?
;
533 hir
::TyPath(Some(ref qself
), ref path
) => {
534 self.print_qpath(path
, qself
, false)?
536 hir
::TyObjectSum(ref ty
, ref bounds
) => {
537 self.print_type(&ty
)?
;
538 self.print_bounds("+", &bounds
[..])?
;
540 hir
::TyPolyTraitRef(ref bounds
) => {
541 self.print_bounds("", &bounds
[..])?
;
543 hir
::TyFixedLengthVec(ref ty
, ref v
) => {
544 word(&mut self.s
, "[")?
;
545 self.print_type(&ty
)?
;
546 word(&mut self.s
, "; ")?
;
547 self.print_expr(&v
)?
;
548 word(&mut self.s
, "]")?
;
550 hir
::TyTypeof(ref e
) => {
551 word(&mut self.s
, "typeof(")?
;
552 self.print_expr(&e
)?
;
553 word(&mut self.s
, ")")?
;
556 word(&mut self.s
, "_")?
;
562 pub fn print_foreign_item(&mut self, item
: &hir
::ForeignItem
) -> io
::Result
<()> {
563 self.hardbreak_if_not_bol()?
;
564 self.maybe_print_comment(item
.span
.lo
)?
;
565 self.print_outer_attributes(&item
.attrs
)?
;
567 hir
::ForeignItemFn(ref decl
, ref generics
) => {
570 hir
::Unsafety
::Normal
,
571 hir
::Constness
::NotConst
,
577 self.end()?
; // end head-ibox
578 word(&mut self.s
, ";")?
;
579 self.end() // end the outer fn box
581 hir
::ForeignItemStatic(ref t
, m
) => {
582 self.head(&visibility_qualified(&item
.vis
, "static"))?
;
584 self.word_space("mut")?
;
586 self.print_name(item
.name
)?
;
587 self.word_space(":")?
;
588 self.print_type(&t
)?
;
589 word(&mut self.s
, ";")?
;
590 self.end()?
; // end the head-ibox
591 self.end() // end the outer cbox
596 fn print_associated_const(&mut self,
599 default: Option
<&hir
::Expr
>,
600 vis
: &hir
::Visibility
)
602 word(&mut self.s
, &visibility_qualified(vis
, ""))?
;
603 self.word_space("const")?
;
604 self.print_name(name
)?
;
605 self.word_space(":")?
;
606 self.print_type(ty
)?
;
607 if let Some(expr
) = default {
609 self.word_space("=")?
;
610 self.print_expr(expr
)?
;
612 word(&mut self.s
, ";")
615 fn print_associated_type(&mut self,
617 bounds
: Option
<&hir
::TyParamBounds
>,
618 ty
: Option
<&hir
::Ty
>)
620 self.word_space("type")?
;
621 self.print_name(name
)?
;
622 if let Some(bounds
) = bounds
{
623 self.print_bounds(":", bounds
)?
;
625 if let Some(ty
) = ty
{
627 self.word_space("=")?
;
628 self.print_type(ty
)?
;
630 word(&mut self.s
, ";")
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
)
643 /// Pretty-print an item
644 pub fn print_item(&mut self, item
: &hir
::Item
) -> io
::Result
<()> {
645 self.hardbreak_if_not_bol()?
;
646 self.maybe_print_comment(item
.span
.lo
)?
;
647 self.print_outer_attributes(&item
.attrs
)?
;
648 self.ann
.pre(self, NodeItem(item
))?
;
650 hir
::ItemExternCrate(ref optional_path
) => {
651 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 self.print_string(&val
, ast
::StrStyle
::Cooked
)?
;
660 word(&mut self.s
, "as")?
;
663 self.print_name(item
.name
)?
;
664 word(&mut self.s
, ";")?
;
665 self.end()?
; // end inner head-block
666 self.end()?
; // end outer head-block
668 hir
::ItemUse(ref vp
) => {
669 self.head(&visibility_qualified(&item
.vis
, "use"))?
;
670 self.print_view_path(&vp
)?
;
671 word(&mut self.s
, ";")?
;
672 self.end()?
; // end inner head-block
673 self.end()?
; // end outer head-block
675 hir
::ItemStatic(ref ty
, m
, ref expr
) => {
676 self.head(&visibility_qualified(&item
.vis
, "static"))?
;
677 if m
== hir
::MutMutable
{
678 self.word_space("mut")?
;
680 self.print_name(item
.name
)?
;
681 self.word_space(":")?
;
682 self.print_type(&ty
)?
;
684 self.end()?
; // end the head-ibox
686 self.word_space("=")?
;
687 self.print_expr(&expr
)?
;
688 word(&mut self.s
, ";")?
;
689 self.end()?
; // end the outer cbox
691 hir
::ItemConst(ref ty
, ref expr
) => {
692 self.head(&visibility_qualified(&item
.vis
, "const"))?
;
693 self.print_name(item
.name
)?
;
694 self.word_space(":")?
;
695 self.print_type(&ty
)?
;
697 self.end()?
; // end the head-ibox
699 self.word_space("=")?
;
700 self.print_expr(&expr
)?
;
701 word(&mut self.s
, ";")?
;
702 self.end()?
; // end the outer cbox
704 hir
::ItemFn(ref decl
, unsafety
, constness
, abi
, ref typarams
, ref body
) => {
714 word(&mut self.s
, " ")?
;
715 self.print_block_with_attrs(&body
, &item
.attrs
)?
;
717 hir
::ItemMod(ref _mod
) => {
718 self.head(&visibility_qualified(&item
.vis
, "mod"))?
;
719 self.print_name(item
.name
)?
;
722 self.print_mod(_mod
, &item
.attrs
)?
;
723 self.bclose(item
.span
)?
;
725 hir
::ItemForeignMod(ref nmod
) => {
726 self.head("extern")?
;
727 self.word_nbsp(&nmod
.abi
.to_string())?
;
729 self.print_foreign_mod(nmod
, &item
.attrs
)?
;
730 self.bclose(item
.span
)?
;
732 hir
::ItemTy(ref ty
, ref params
) => {
733 self.ibox(indent_unit
)?
;
735 self.word_nbsp(&visibility_qualified(&item
.vis
, "type"))?
;
736 self.print_name(item
.name
)?
;
737 self.print_generics(params
)?
;
738 self.end()?
; // end the inner ibox
740 self.print_where_clause(¶ms
.where_clause
)?
;
742 self.word_space("=")?
;
743 self.print_type(&ty
)?
;
744 word(&mut self.s
, ";")?
;
745 self.end()?
; // end the outer ibox
747 hir
::ItemEnum(ref enum_definition
, ref params
) => {
748 self.print_enum_def(enum_definition
, params
, item
.name
, item
.span
, &item
.vis
)?
;
750 hir
::ItemStruct(ref struct_def
, ref generics
) => {
751 self.head(&visibility_qualified(&item
.vis
, "struct"))?
;
752 self.print_struct(struct_def
, generics
, item
.name
, item
.span
, true)?
;
755 hir
::ItemDefaultImpl(unsafety
, ref trait_ref
) => {
757 self.print_visibility(&item
.vis
)?
;
758 self.print_unsafety(unsafety
)?
;
759 self.word_nbsp("impl")?
;
760 self.print_trait_ref(trait_ref
)?
;
762 self.word_space("for")?
;
763 self.word_space("..")?
;
765 self.bclose(item
.span
)?
;
767 hir
::ItemImpl(unsafety
,
774 self.print_visibility(&item
.vis
)?
;
775 self.print_unsafety(unsafety
)?
;
776 self.word_nbsp("impl")?
;
778 if generics
.is_parameterized() {
779 self.print_generics(generics
)?
;
784 hir
::ImplPolarity
::Negative
=> {
785 word(&mut self.s
, "!")?
;
792 self.print_trait_ref(t
)?
;
794 self.word_space("for")?
;
799 self.print_type(&ty
)?
;
800 self.print_where_clause(&generics
.where_clause
)?
;
804 self.print_inner_attributes(&item
.attrs
)?
;
805 for impl_item
in impl_items
{
806 self.print_impl_item(impl_item
)?
;
808 self.bclose(item
.span
)?
;
810 hir
::ItemTrait(unsafety
, ref generics
, ref bounds
, ref trait_items
) => {
812 self.print_visibility(&item
.vis
)?
;
813 self.print_unsafety(unsafety
)?
;
814 self.word_nbsp("trait")?
;
815 self.print_name(item
.name
)?
;
816 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
{
821 self.word_space("for ?")?
;
822 self.print_trait_ref(&ptr
.trait_ref
)?
;
824 real_bounds
.push(b
.clone());
827 self.print_bounds(":", &real_bounds
[..])?
;
828 self.print_where_clause(&generics
.where_clause
)?
;
829 word(&mut self.s
, " ")?
;
831 for trait_item
in trait_items
{
832 self.print_trait_item(trait_item
)?
;
834 self.bclose(item
.span
)?
;
837 self.ann
.post(self, NodeItem(item
))
840 fn print_trait_ref(&mut self, t
: &hir
::TraitRef
) -> io
::Result
<()> {
841 self.print_path(&t
.path
, false, 0)
844 fn print_formal_lifetime_list(&mut self, lifetimes
: &[hir
::LifetimeDef
]) -> io
::Result
<()> {
845 if !lifetimes
.is_empty() {
846 word(&mut self.s
, "for<")?
;
847 let mut comma
= false;
848 for lifetime_def
in lifetimes
{
850 self.word_space(",")?
852 self.print_lifetime_def(lifetime_def
)?
;
855 word(&mut self.s
, ">")?
;
860 fn print_poly_trait_ref(&mut self, t
: &hir
::PolyTraitRef
) -> io
::Result
<()> {
861 self.print_formal_lifetime_list(&t
.bound_lifetimes
)?
;
862 self.print_trait_ref(&t
.trait_ref
)
865 pub fn print_enum_def(&mut self,
866 enum_definition
: &hir
::EnumDef
,
867 generics
: &hir
::Generics
,
870 visibility
: &hir
::Visibility
)
872 self.head(&visibility_qualified(visibility
, "enum"))?
;
873 self.print_name(name
)?
;
874 self.print_generics(generics
)?
;
875 self.print_where_clause(&generics
.where_clause
)?
;
877 self.print_variants(&enum_definition
.variants
, span
)
880 pub fn print_variants(&mut self,
881 variants
: &[hir
::Variant
],
886 self.space_if_not_bol()?
;
887 self.maybe_print_comment(v
.span
.lo
)?
;
888 self.print_outer_attributes(&v
.node
.attrs
)?
;
889 self.ibox(indent_unit
)?
;
890 self.print_variant(v
)?
;
891 word(&mut self.s
, ",")?
;
893 self.maybe_print_trailing_comment(v
.span
, None
)?
;
898 pub fn print_visibility(&mut self, vis
: &hir
::Visibility
) -> io
::Result
<()> {
900 hir
::Public
=> self.word_nbsp("pub"),
901 hir
::Inherited
=> Ok(()),
905 pub fn print_struct(&mut self,
906 struct_def
: &hir
::VariantData
,
907 generics
: &hir
::Generics
,
910 print_finalizer
: bool
)
912 self.print_name(name
)?
;
913 self.print_generics(generics
)?
;
914 if !struct_def
.is_struct() {
915 if struct_def
.is_tuple() {
917 self.commasep(Inconsistent
, struct_def
.fields(), |s
, field
| {
918 s
.print_visibility(&field
.vis
)?
;
919 s
.maybe_print_comment(field
.span
.lo
)?
;
920 s
.print_type(&field
.ty
)
924 self.print_where_clause(&generics
.where_clause
)?
;
926 word(&mut self.s
, ";")?
;
929 self.end() // close the outer-box
931 self.print_where_clause(&generics
.where_clause
)?
;
934 self.hardbreak_if_not_bol()?
;
936 for field
in struct_def
.fields() {
937 self.hardbreak_if_not_bol()?
;
938 self.maybe_print_comment(field
.span
.lo
)?
;
939 self.print_outer_attributes(&field
.attrs
)?
;
940 self.print_visibility(&field
.vis
)?
;
941 self.print_name(field
.name
)?
;
942 self.word_nbsp(":")?
;
943 self.print_type(&field
.ty
)?
;
944 word(&mut self.s
, ",")?
;
951 pub fn print_variant(&mut self, v
: &hir
::Variant
) -> io
::Result
<()> {
953 let generics
= hir
::Generics
::empty();
954 self.print_struct(&v
.node
.data
, &generics
, v
.node
.name
, v
.span
, false)?
;
955 match v
.node
.disr_expr
{
958 self.word_space("=")?
;
964 pub fn print_method_sig(&mut self,
967 vis
: &hir
::Visibility
)
969 self.print_fn(&m
.decl
,
975 Some(&m
.explicit_self
.node
),
979 pub fn print_trait_item(&mut self, ti
: &hir
::TraitItem
) -> io
::Result
<()> {
980 self.ann
.pre(self, NodeSubItem(ti
.id
))?
;
981 self.hardbreak_if_not_bol()?
;
982 self.maybe_print_comment(ti
.span
.lo
)?
;
983 self.print_outer_attributes(&ti
.attrs
)?
;
985 hir
::ConstTraitItem(ref ty
, ref default) => {
986 self.print_associated_const(ti
.name
,
988 default.as_ref().map(|expr
| &**expr
),
991 hir
::MethodTraitItem(ref sig
, ref body
) => {
995 self.print_method_sig(ti
.name
, sig
, &hir
::Inherited
)?
;
996 if let Some(ref body
) = *body
{
998 self.print_block_with_attrs(body
, &ti
.attrs
)?
;
1000 word(&mut self.s
, ";")?
;
1003 hir
::TypeTraitItem(ref bounds
, ref default) => {
1004 self.print_associated_type(ti
.name
,
1006 default.as_ref().map(|ty
| &**ty
))?
;
1009 self.ann
.post(self, NodeSubItem(ti
.id
))
1012 pub fn print_impl_item(&mut self, ii
: &hir
::ImplItem
) -> io
::Result
<()> {
1013 self.ann
.pre(self, NodeSubItem(ii
.id
))?
;
1014 self.hardbreak_if_not_bol()?
;
1015 self.maybe_print_comment(ii
.span
.lo
)?
;
1016 self.print_outer_attributes(&ii
.attrs
)?
;
1018 if let hir
::Defaultness
::Default
= ii
.defaultness
{
1019 self.word_nbsp("default")?
;
1023 hir
::ImplItemKind
::Const(ref ty
, ref expr
) => {
1024 self.print_associated_const(ii
.name
, &ty
, Some(&expr
), &ii
.vis
)?
;
1026 hir
::ImplItemKind
::Method(ref sig
, ref body
) => {
1028 self.print_method_sig(ii
.name
, sig
, &ii
.vis
)?
;
1030 self.print_block_with_attrs(body
, &ii
.attrs
)?
;
1032 hir
::ImplItemKind
::Type(ref ty
) => {
1033 self.print_associated_type(ii
.name
, None
, Some(ty
))?
;
1036 self.ann
.post(self, NodeSubItem(ii
.id
))
1039 pub fn print_stmt(&mut self, st
: &hir
::Stmt
) -> io
::Result
<()> {
1040 self.maybe_print_comment(st
.span
.lo
)?
;
1042 hir
::StmtDecl(ref decl
, _
) => {
1043 self.print_decl(&decl
)?
;
1045 hir
::StmtExpr(ref expr
, _
) => {
1046 self.space_if_not_bol()?
;
1047 self.print_expr(&expr
)?
;
1049 hir
::StmtSemi(ref expr
, _
) => {
1050 self.space_if_not_bol()?
;
1051 self.print_expr(&expr
)?
;
1052 word(&mut self.s
, ";")?
;
1055 if stmt_ends_with_semi(&st
.node
) {
1056 word(&mut self.s
, ";")?
;
1058 self.maybe_print_trailing_comment(st
.span
, None
)
1061 pub fn print_block(&mut self, blk
: &hir
::Block
) -> io
::Result
<()> {
1062 self.print_block_with_attrs(blk
, &[])
1065 pub fn print_block_unclosed(&mut self, blk
: &hir
::Block
) -> io
::Result
<()> {
1066 self.print_block_unclosed_indent(blk
, indent_unit
)
1069 pub fn print_block_unclosed_indent(&mut self,
1073 self.print_block_maybe_unclosed(blk
, indented
, &[], false)
1076 pub fn print_block_with_attrs(&mut self,
1078 attrs
: &[ast
::Attribute
])
1080 self.print_block_maybe_unclosed(blk
, indent_unit
, attrs
, true)
1083 pub fn print_block_maybe_unclosed(&mut self,
1086 attrs
: &[ast
::Attribute
],
1090 hir
::UnsafeBlock(..) => self.word_space("unsafe")?
,
1091 hir
::PushUnsafeBlock(..) => self.word_space("push_unsafe")?
,
1092 hir
::PopUnsafeBlock(..) => self.word_space("pop_unsafe")?
,
1093 hir
::PushUnstableBlock
=> self.word_space("push_unstable")?
,
1094 hir
::PopUnstableBlock
=> self.word_space("pop_unstable")?
,
1095 hir
::DefaultBlock
=> (),
1097 self.maybe_print_comment(blk
.span
.lo
)?
;
1098 self.ann
.pre(self, NodeBlock(blk
))?
;
1101 self.print_inner_attributes(attrs
)?
;
1103 for st
in &blk
.stmts
{
1104 self.print_stmt(st
)?
;
1108 self.space_if_not_bol()?
;
1109 self.print_expr(&expr
)?
;
1110 self.maybe_print_trailing_comment(expr
.span
, Some(blk
.span
.hi
))?
;
1114 self.bclose_maybe_open(blk
.span
, indented
, close_box
)?
;
1115 self.ann
.post(self, NodeBlock(blk
))
1118 fn print_else(&mut self, els
: Option
<&hir
::Expr
>) -> io
::Result
<()> {
1122 // "another else-if"
1123 hir
::ExprIf(ref i
, ref then
, ref e
) => {
1124 self.cbox(indent_unit
- 1)?
;
1126 word(&mut self.s
, " else if ")?
;
1127 self.print_expr(&i
)?
;
1128 space(&mut self.s
)?
;
1129 self.print_block(&then
)?
;
1130 self.print_else(e
.as_ref().map(|e
| &**e
))
1133 hir
::ExprBlock(ref b
) => {
1134 self.cbox(indent_unit
- 1)?
;
1136 word(&mut self.s
, " else ")?
;
1137 self.print_block(&b
)
1139 // BLEAH, constraints would be great here
1141 panic
!("print_if saw if with weird alternative");
1149 pub fn print_if(&mut self,
1152 elseopt
: Option
<&hir
::Expr
>)
1155 self.print_expr(test
)?
;
1156 space(&mut self.s
)?
;
1157 self.print_block(blk
)?
;
1158 self.print_else(elseopt
)
1161 pub fn print_if_let(&mut self,
1165 elseopt
: Option
<&hir
::Expr
>)
1167 self.head("if let")?
;
1168 self.print_pat(pat
)?
;
1169 space(&mut self.s
)?
;
1170 self.word_space("=")?
;
1171 self.print_expr(expr
)?
;
1172 space(&mut self.s
)?
;
1173 self.print_block(blk
)?
;
1174 self.print_else(elseopt
)
1178 fn print_call_post(&mut self, args
: &[P
<hir
::Expr
>]) -> io
::Result
<()> {
1180 self.commasep_exprs(Inconsistent
, args
)?
;
1184 pub fn print_expr_maybe_paren(&mut self, expr
: &hir
::Expr
) -> io
::Result
<()> {
1185 let needs_par
= needs_parentheses(expr
);
1189 self.print_expr(expr
)?
;
1196 fn print_expr_vec(&mut self, exprs
: &[P
<hir
::Expr
>]) -> io
::Result
<()> {
1197 self.ibox(indent_unit
)?
;
1198 word(&mut self.s
, "[")?
;
1199 self.commasep_exprs(Inconsistent
, &exprs
[..])?
;
1200 word(&mut self.s
, "]")?
;
1204 fn print_expr_repeat(&mut self, element
: &hir
::Expr
, count
: &hir
::Expr
) -> io
::Result
<()> {
1205 self.ibox(indent_unit
)?
;
1206 word(&mut self.s
, "[")?
;
1207 self.print_expr(element
)?
;
1208 self.word_space(";")?
;
1209 self.print_expr(count
)?
;
1210 word(&mut self.s
, "]")?
;
1214 fn print_expr_struct(&mut self,
1216 fields
: &[hir
::Field
],
1217 wth
: &Option
<P
<hir
::Expr
>>)
1219 self.print_path(path
, true, 0)?
;
1220 word(&mut self.s
, "{")?
;
1221 self.commasep_cmnt(Consistent
,
1224 s
.ibox(indent_unit
)?
;
1225 s
.print_name(field
.name
.node
)?
;
1227 s
.print_expr(&field
.expr
)?
;
1233 self.ibox(indent_unit
)?
;
1234 if !fields
.is_empty() {
1235 word(&mut self.s
, ",")?
;
1236 space(&mut self.s
)?
;
1238 word(&mut self.s
, "..")?
;
1239 self.print_expr(&expr
)?
;
1242 _
=> if !fields
.is_empty() {
1243 word(&mut self.s
, ",")?
1246 word(&mut self.s
, "}")?
;
1250 fn print_expr_tup(&mut self, exprs
: &[P
<hir
::Expr
>]) -> io
::Result
<()> {
1252 self.commasep_exprs(Inconsistent
, &exprs
[..])?
;
1253 if exprs
.len() == 1 {
1254 word(&mut self.s
, ",")?
;
1259 fn print_expr_call(&mut self, func
: &hir
::Expr
, args
: &[P
<hir
::Expr
>]) -> io
::Result
<()> {
1260 self.print_expr_maybe_paren(func
)?
;
1261 self.print_call_post(args
)
1264 fn print_expr_method_call(&mut self,
1265 name
: Spanned
<ast
::Name
>,
1267 args
: &[P
<hir
::Expr
>])
1269 let base_args
= &args
[1..];
1270 self.print_expr(&args
[0])?
;
1271 word(&mut self.s
, ".")?
;
1272 self.print_name(name
.node
)?
;
1273 if !tys
.is_empty() {
1274 word(&mut self.s
, "::<")?
;
1275 self.commasep(Inconsistent
, tys
, |s
, ty
| s
.print_type(&ty
))?
;
1276 word(&mut self.s
, ">")?
;
1278 self.print_call_post(base_args
)
1281 fn print_expr_binary(&mut self,
1286 self.print_expr(lhs
)?
;
1287 space(&mut self.s
)?
;
1288 self.word_space(op
.node
.as_str())?
;
1289 self.print_expr(rhs
)
1292 fn print_expr_unary(&mut self, op
: hir
::UnOp
, expr
: &hir
::Expr
) -> io
::Result
<()> {
1293 word(&mut self.s
, op
.as_str())?
;
1294 self.print_expr_maybe_paren(expr
)
1297 fn print_expr_addr_of(&mut self,
1298 mutability
: hir
::Mutability
,
1301 word(&mut self.s
, "&")?
;
1302 self.print_mutability(mutability
)?
;
1303 self.print_expr_maybe_paren(expr
)
1306 pub fn print_expr(&mut self, expr
: &hir
::Expr
) -> io
::Result
<()> {
1307 self.maybe_print_comment(expr
.span
.lo
)?
;
1308 self.ibox(indent_unit
)?
;
1309 self.ann
.pre(self, NodeExpr(expr
))?
;
1311 hir
::ExprBox(ref expr
) => {
1312 self.word_space("box")?
;
1313 self.print_expr(expr
)?
;
1315 hir
::ExprVec(ref exprs
) => {
1316 self.print_expr_vec(&exprs
[..])?
;
1318 hir
::ExprRepeat(ref element
, ref count
) => {
1319 self.print_expr_repeat(&element
, &count
)?
;
1321 hir
::ExprStruct(ref path
, ref fields
, ref wth
) => {
1322 self.print_expr_struct(path
, &fields
[..], wth
)?
;
1324 hir
::ExprTup(ref exprs
) => {
1325 self.print_expr_tup(&exprs
[..])?
;
1327 hir
::ExprCall(ref func
, ref args
) => {
1328 self.print_expr_call(&func
, &args
[..])?
;
1330 hir
::ExprMethodCall(name
, ref tys
, ref args
) => {
1331 self.print_expr_method_call(name
, &tys
[..], &args
[..])?
;
1333 hir
::ExprBinary(op
, ref lhs
, ref rhs
) => {
1334 self.print_expr_binary(op
, &lhs
, &rhs
)?
;
1336 hir
::ExprUnary(op
, ref expr
) => {
1337 self.print_expr_unary(op
, &expr
)?
;
1339 hir
::ExprAddrOf(m
, ref expr
) => {
1340 self.print_expr_addr_of(m
, &expr
)?
;
1342 hir
::ExprLit(ref lit
) => {
1343 self.print_literal(&lit
)?
;
1345 hir
::ExprCast(ref expr
, ref ty
) => {
1346 self.print_expr(&expr
)?
;
1347 space(&mut self.s
)?
;
1348 self.word_space("as")?
;
1349 self.print_type(&ty
)?
;
1351 hir
::ExprType(ref expr
, ref ty
) => {
1352 self.print_expr(&expr
)?
;
1353 self.word_space(":")?
;
1354 self.print_type(&ty
)?
;
1356 hir
::ExprIf(ref test
, ref blk
, ref elseopt
) => {
1357 self.print_if(&test
, &blk
, elseopt
.as_ref().map(|e
| &**e
))?
;
1359 hir
::ExprWhile(ref test
, ref blk
, opt_ident
) => {
1360 if let Some(ident
) = opt_ident
{
1361 self.print_name(ident
.name
)?
;
1362 self.word_space(":")?
;
1364 self.head("while")?
;
1365 self.print_expr(&test
)?
;
1366 space(&mut self.s
)?
;
1367 self.print_block(&blk
)?
;
1369 hir
::ExprLoop(ref blk
, opt_ident
) => {
1370 if let Some(ident
) = opt_ident
{
1371 self.print_name(ident
.name
)?
;
1372 self.word_space(":")?
;
1375 space(&mut self.s
)?
;
1376 self.print_block(&blk
)?
;
1378 hir
::ExprMatch(ref expr
, ref arms
, _
) => {
1379 self.cbox(indent_unit
)?
;
1381 self.word_nbsp("match")?
;
1382 self.print_expr(&expr
)?
;
1383 space(&mut self.s
)?
;
1386 self.print_arm(arm
)?
;
1388 self.bclose_(expr
.span
, indent_unit
)?
;
1390 hir
::ExprClosure(capture_clause
, ref decl
, ref body
) => {
1391 self.print_capture_clause(capture_clause
)?
;
1393 self.print_fn_block_args(&decl
)?
;
1394 space(&mut self.s
)?
;
1396 let default_return
= match decl
.output
{
1397 hir
::DefaultReturn(..) => true,
1401 if !default_return
|| !body
.stmts
.is_empty() || body
.expr
.is_none() {
1402 self.print_block_unclosed(&body
)?
;
1404 // we extract the block, so as not to create another set of boxes
1405 match body
.expr
.as_ref().unwrap().node
{
1406 hir
::ExprBlock(ref blk
) => {
1407 self.print_block_unclosed(&blk
)?
;
1410 // this is a bare expression
1411 self.print_expr(body
.expr
.as_ref().map(|e
| &**e
).unwrap())?
;
1412 self.end()?
; // need to close a box
1416 // a box will be closed by print_expr, but we didn't want an overall
1417 // wrapper so we closed the corresponding opening. so create an
1418 // empty box to satisfy the close.
1421 hir
::ExprBlock(ref blk
) => {
1422 // containing cbox, will be closed by print-block at }
1423 self.cbox(indent_unit
)?
;
1424 // head-box, will be closed by print-block after {
1426 self.print_block(&blk
)?
;
1428 hir
::ExprAssign(ref lhs
, ref rhs
) => {
1429 self.print_expr(&lhs
)?
;
1430 space(&mut self.s
)?
;
1431 self.word_space("=")?
;
1432 self.print_expr(&rhs
)?
;
1434 hir
::ExprAssignOp(op
, ref lhs
, ref rhs
) => {
1435 self.print_expr(&lhs
)?
;
1436 space(&mut self.s
)?
;
1437 word(&mut self.s
, op
.node
.as_str())?
;
1438 self.word_space("=")?
;
1439 self.print_expr(&rhs
)?
;
1441 hir
::ExprField(ref expr
, name
) => {
1442 self.print_expr(&expr
)?
;
1443 word(&mut self.s
, ".")?
;
1444 self.print_name(name
.node
)?
;
1446 hir
::ExprTupField(ref expr
, id
) => {
1447 self.print_expr(&expr
)?
;
1448 word(&mut self.s
, ".")?
;
1449 self.print_usize(id
.node
)?
;
1451 hir
::ExprIndex(ref expr
, ref index
) => {
1452 self.print_expr(&expr
)?
;
1453 word(&mut self.s
, "[")?
;
1454 self.print_expr(&index
)?
;
1455 word(&mut self.s
, "]")?
;
1457 hir
::ExprPath(None
, ref path
) => {
1458 self.print_path(path
, true, 0)?
1460 hir
::ExprPath(Some(ref qself
), ref path
) => {
1461 self.print_qpath(path
, qself
, true)?
1463 hir
::ExprBreak(opt_ident
) => {
1464 word(&mut self.s
, "break")?
;
1465 space(&mut self.s
)?
;
1466 if let Some(ident
) = opt_ident
{
1467 self.print_name(ident
.node
.name
)?
;
1468 space(&mut self.s
)?
;
1471 hir
::ExprAgain(opt_ident
) => {
1472 word(&mut self.s
, "continue")?
;
1473 space(&mut self.s
)?
;
1474 if let Some(ident
) = opt_ident
{
1475 self.print_name(ident
.node
.name
)?
;
1479 hir
::ExprRet(ref result
) => {
1480 word(&mut self.s
, "return")?
;
1483 word(&mut self.s
, " ")?
;
1484 self.print_expr(&expr
)?
;
1489 hir
::ExprInlineAsm(ref a
, ref outputs
, ref inputs
) => {
1490 word(&mut self.s
, "asm!")?
;
1492 self.print_string(&a
.asm
, a
.asm_str_style
)?
;
1493 self.word_space(":")?
;
1495 let mut out_idx
= 0;
1496 self.commasep(Inconsistent
, &a
.outputs
, |s
, out
| {
1497 let mut ch
= out
.constraint
.chars();
1499 Some('
='
) if out
.is_rw
=> {
1500 s
.print_string(&format
!("+{}", ch
.as_str()),
1501 ast
::StrStyle
::Cooked
)?
1503 _
=> s
.print_string(&out
.constraint
,
1504 ast
::StrStyle
::Cooked
)?
,
1507 s
.print_expr(&outputs
[out_idx
])?
;
1512 space(&mut self.s
)?
;
1513 self.word_space(":")?
;
1516 self.commasep(Inconsistent
, &a
.inputs
, |s
, co
| {
1517 s
.print_string(&co
, ast
::StrStyle
::Cooked
)?
;
1519 s
.print_expr(&inputs
[in_idx
])?
;
1524 space(&mut self.s
)?
;
1525 self.word_space(":")?
;
1527 self.commasep(Inconsistent
, &a
.clobbers
, |s
, co
| {
1528 s
.print_string(&co
, ast
::StrStyle
::Cooked
)?
;
1532 let mut options
= vec
![];
1534 options
.push("volatile");
1537 options
.push("alignstack");
1539 if a
.dialect
== ast
::AsmDialect
::Intel
{
1540 options
.push("intel");
1543 if !options
.is_empty() {
1544 space(&mut self.s
)?
;
1545 self.word_space(":")?
;
1546 self.commasep(Inconsistent
, &options
, |s
, &co
| {
1547 s
.print_string(co
, ast
::StrStyle
::Cooked
)?
;
1555 self.ann
.post(self, NodeExpr(expr
))?
;
1559 pub fn print_local_decl(&mut self, loc
: &hir
::Local
) -> io
::Result
<()> {
1560 self.print_pat(&loc
.pat
)?
;
1561 if let Some(ref ty
) = loc
.ty
{
1562 self.word_space(":")?
;
1563 self.print_type(&ty
)?
;
1568 pub fn print_decl(&mut self, decl
: &hir
::Decl
) -> io
::Result
<()> {
1569 self.maybe_print_comment(decl
.span
.lo
)?
;
1571 hir
::DeclLocal(ref loc
) => {
1572 self.space_if_not_bol()?
;
1573 self.ibox(indent_unit
)?
;
1574 self.word_nbsp("let")?
;
1576 self.ibox(indent_unit
)?
;
1577 self.print_local_decl(&loc
)?
;
1579 if let Some(ref init
) = loc
.init
{
1581 self.word_space("=")?
;
1582 self.print_expr(&init
)?
;
1586 hir
::DeclItem(ref item
) => {
1587 self.print_item_id(item
)
1592 pub fn print_usize(&mut self, i
: usize) -> io
::Result
<()> {
1593 word(&mut self.s
, &i
.to_string())
1596 pub fn print_name(&mut self, name
: ast
::Name
) -> io
::Result
<()> {
1597 word(&mut self.s
, &name
.as_str())?
;
1598 self.ann
.post(self, NodeName(&name
))
1601 pub fn print_for_decl(&mut self, loc
: &hir
::Local
, coll
: &hir
::Expr
) -> io
::Result
<()> {
1602 self.print_local_decl(loc
)?
;
1603 space(&mut self.s
)?
;
1604 self.word_space("in")?
;
1605 self.print_expr(coll
)
1608 fn print_path(&mut self,
1610 colons_before_params
: bool
,
1613 self.maybe_print_comment(path
.span
.lo
)?
;
1615 let mut first
= !path
.global
;
1616 for segment
in &path
.segments
[..path
.segments
.len() - depth
] {
1620 word(&mut self.s
, "::")?
1623 self.print_name(segment
.identifier
.name
)?
;
1625 self.print_path_parameters(&segment
.parameters
, colons_before_params
)?
;
1631 fn print_qpath(&mut self,
1634 colons_before_params
: bool
)
1636 word(&mut self.s
, "<")?
;
1637 self.print_type(&qself
.ty
)?
;
1638 if qself
.position
> 0 {
1639 space(&mut self.s
)?
;
1640 self.word_space("as")?
;
1641 let depth
= path
.segments
.len() - qself
.position
;
1642 self.print_path(&path
, false, depth
)?
;
1644 word(&mut self.s
, ">")?
;
1645 word(&mut self.s
, "::")?
;
1646 let item_segment
= path
.segments
.last().unwrap();
1647 self.print_name(item_segment
.identifier
.name
)?
;
1648 self.print_path_parameters(&item_segment
.parameters
, colons_before_params
)
1651 fn print_path_parameters(&mut self,
1652 parameters
: &hir
::PathParameters
,
1653 colons_before_params
: bool
)
1655 if parameters
.is_empty() {
1659 if colons_before_params
{
1660 word(&mut self.s
, "::")?
1664 hir
::AngleBracketedParameters(ref data
) => {
1665 word(&mut self.s
, "<")?
;
1667 let mut comma
= false;
1668 for lifetime
in &data
.lifetimes
{
1670 self.word_space(",")?
1672 self.print_lifetime(lifetime
)?
;
1676 if !data
.types
.is_empty() {
1678 self.word_space(",")?
1680 self.commasep(Inconsistent
, &data
.types
, |s
, ty
| s
.print_type(&ty
))?
;
1684 for binding
in data
.bindings
.iter() {
1686 self.word_space(",")?
1688 self.print_name(binding
.name
)?
;
1689 space(&mut self.s
)?
;
1690 self.word_space("=")?
;
1691 self.print_type(&binding
.ty
)?
;
1695 word(&mut self.s
, ">")?
1698 hir
::ParenthesizedParameters(ref data
) => {
1699 word(&mut self.s
, "(")?
;
1700 self.commasep(Inconsistent
, &data
.inputs
, |s
, ty
| s
.print_type(&ty
))?
;
1701 word(&mut self.s
, ")")?
;
1706 self.space_if_not_bol()?
;
1707 self.word_space("->")?
;
1708 self.print_type(&ty
)?
;
1717 pub fn print_pat(&mut self, pat
: &hir
::Pat
) -> io
::Result
<()> {
1718 self.maybe_print_comment(pat
.span
.lo
)?
;
1719 self.ann
.pre(self, NodePat(pat
))?
;
1720 // Pat isn't normalized, but the beauty of it
1721 // is that it doesn't matter
1723 PatKind
::Wild
=> word(&mut self.s
, "_")?
,
1724 PatKind
::Ident(binding_mode
, ref path1
, ref sub
) => {
1725 match binding_mode
{
1726 hir
::BindByRef(mutbl
) => {
1727 self.word_nbsp("ref")?
;
1728 self.print_mutability(mutbl
)?
;
1730 hir
::BindByValue(hir
::MutImmutable
) => {}
1731 hir
::BindByValue(hir
::MutMutable
) => {
1732 self.word_nbsp("mut")?
;
1735 self.print_name(path1
.node
.name
)?
;
1738 word(&mut self.s
, "@")?
;
1739 self.print_pat(&p
)?
;
1744 PatKind
::TupleStruct(ref path
, ref args_
) => {
1745 self.print_path(path
, true, 0)?
;
1747 None
=> word(&mut self.s
, "(..)")?
,
1750 self.commasep(Inconsistent
, &args
[..], |s
, p
| s
.print_pat(&p
))?
;
1755 PatKind
::Path(ref path
) => {
1756 self.print_path(path
, true, 0)?
;
1758 PatKind
::QPath(ref qself
, ref path
) => {
1759 self.print_qpath(path
, qself
, false)?
;
1761 PatKind
::Struct(ref path
, ref fields
, etc
) => {
1762 self.print_path(path
, true, 0)?
;
1764 self.word_space("{")?
;
1765 self.commasep_cmnt(Consistent
,
1768 s
.cbox(indent_unit
)?
;
1769 if !f
.node
.is_shorthand
{
1770 s
.print_name(f
.node
.name
)?
;
1773 s
.print_pat(&f
.node
.pat
)?
;
1776 |f
| f
.node
.pat
.span
)?
;
1778 if !fields
.is_empty() {
1779 self.word_space(",")?
;
1781 word(&mut self.s
, "..")?
;
1783 space(&mut self.s
)?
;
1784 word(&mut self.s
, "}")?
;
1786 PatKind
::Tup(ref elts
) => {
1788 self.commasep(Inconsistent
, &elts
[..], |s
, p
| s
.print_pat(&p
))?
;
1789 if elts
.len() == 1 {
1790 word(&mut self.s
, ",")?
;
1794 PatKind
::Box(ref inner
) => {
1795 word(&mut self.s
, "box ")?
;
1796 self.print_pat(&inner
)?
;
1798 PatKind
::Ref(ref inner
, mutbl
) => {
1799 word(&mut self.s
, "&")?
;
1800 if mutbl
== hir
::MutMutable
{
1801 word(&mut self.s
, "mut ")?
;
1803 self.print_pat(&inner
)?
;
1805 PatKind
::Lit(ref e
) => self.print_expr(&e
)?
,
1806 PatKind
::Range(ref begin
, ref end
) => {
1807 self.print_expr(&begin
)?
;
1808 space(&mut self.s
)?
;
1809 word(&mut self.s
, "...")?
;
1810 self.print_expr(&end
)?
;
1812 PatKind
::Vec(ref before
, ref slice
, ref after
) => {
1813 word(&mut self.s
, "[")?
;
1814 self.commasep(Inconsistent
, &before
[..], |s
, p
| s
.print_pat(&p
))?
;
1815 if let Some(ref p
) = *slice
{
1816 if !before
.is_empty() {
1817 self.word_space(",")?
;
1819 if p
.node
!= PatKind
::Wild
{
1820 self.print_pat(&p
)?
;
1822 word(&mut self.s
, "..")?
;
1823 if !after
.is_empty() {
1824 self.word_space(",")?
;
1827 self.commasep(Inconsistent
, &after
[..], |s
, p
| s
.print_pat(&p
))?
;
1828 word(&mut self.s
, "]")?
;
1831 self.ann
.post(self, NodePat(pat
))
1834 fn print_arm(&mut self, arm
: &hir
::Arm
) -> io
::Result
<()> {
1835 // I have no idea why this check is necessary, but here it
1837 if arm
.attrs
.is_empty() {
1838 space(&mut self.s
)?
;
1840 self.cbox(indent_unit
)?
;
1842 self.print_outer_attributes(&arm
.attrs
)?
;
1843 let mut first
= true;
1844 for p
in &arm
.pats
{
1848 space(&mut self.s
)?
;
1849 self.word_space("|")?
;
1851 self.print_pat(&p
)?
;
1853 space(&mut self.s
)?
;
1854 if let Some(ref e
) = arm
.guard
{
1855 self.word_space("if")?
;
1856 self.print_expr(&e
)?
;
1857 space(&mut self.s
)?
;
1859 self.word_space("=>")?
;
1861 match arm
.body
.node
{
1862 hir
::ExprBlock(ref blk
) => {
1863 // the block will close the pattern's ibox
1864 self.print_block_unclosed_indent(&blk
, indent_unit
)?
;
1866 // If it is a user-provided unsafe block, print a comma after it
1867 if let hir
::UnsafeBlock(hir
::UserProvided
) = blk
.rules
{
1868 word(&mut self.s
, ",")?
;
1872 self.end()?
; // close the ibox for the pattern
1873 self.print_expr(&arm
.body
)?
;
1874 word(&mut self.s
, ",")?
;
1877 self.end() // close enclosing cbox
1880 // Returns whether it printed anything
1881 fn print_explicit_self(&mut self,
1882 explicit_self
: &hir
::ExplicitSelf_
,
1883 mutbl
: hir
::Mutability
)
1884 -> io
::Result
<bool
> {
1885 self.print_mutability(mutbl
)?
;
1886 match *explicit_self
{
1887 hir
::SelfStatic
=> {
1890 hir
::SelfValue(_
) => {
1891 word(&mut self.s
, "self")?
;
1893 hir
::SelfRegion(ref lt
, m
, _
) => {
1894 word(&mut self.s
, "&")?
;
1895 self.print_opt_lifetime(lt
)?
;
1896 self.print_mutability(m
)?
;
1897 word(&mut self.s
, "self")?
;
1899 hir
::SelfExplicit(ref typ
, _
) => {
1900 word(&mut self.s
, "self")?
;
1901 self.word_space(":")?
;
1902 self.print_type(&typ
)?
;
1908 pub fn print_fn(&mut self,
1910 unsafety
: hir
::Unsafety
,
1911 constness
: hir
::Constness
,
1913 name
: Option
<ast
::Name
>,
1914 generics
: &hir
::Generics
,
1915 opt_explicit_self
: Option
<&hir
::ExplicitSelf_
>,
1916 vis
: &hir
::Visibility
)
1918 self.print_fn_header_info(unsafety
, constness
, abi
, vis
)?
;
1920 if let Some(name
) = name
{
1922 self.print_name(name
)?
;
1924 self.print_generics(generics
)?
;
1925 self.print_fn_args_and_ret(decl
, opt_explicit_self
)?
;
1926 self.print_where_clause(&generics
.where_clause
)
1929 pub fn print_fn_args(&mut self,
1931 opt_explicit_self
: Option
<&hir
::ExplicitSelf_
>,
1934 // It is unfortunate to duplicate the commasep logic, but we want the
1935 // self type and the args all in the same box.
1936 self.rbox(0, Inconsistent
)?
;
1937 let mut first
= true;
1938 if let Some(explicit_self
) = opt_explicit_self
{
1939 let m
= match explicit_self
{
1940 &hir
::SelfStatic
=> hir
::MutImmutable
,
1941 _
=> match decl
.inputs
[0].pat
.node
{
1942 PatKind
::Ident(hir
::BindByValue(m
), _
, _
) => m
,
1943 _
=> hir
::MutImmutable
,
1946 first
= !self.print_explicit_self(explicit_self
, m
)?
;
1949 // HACK(eddyb) ignore the separately printed self argument.
1950 let args
= if first
{
1960 self.word_space(",")?
;
1962 self.print_arg(arg
, is_closure
)?
;
1968 pub fn print_fn_args_and_ret(&mut self,
1970 opt_explicit_self
: Option
<&hir
::ExplicitSelf_
>)
1973 self.print_fn_args(decl
, opt_explicit_self
, false)?
;
1975 word(&mut self.s
, ", ...")?
;
1979 self.print_fn_output(decl
)
1982 pub fn print_fn_block_args(&mut self, decl
: &hir
::FnDecl
) -> io
::Result
<()> {
1983 word(&mut self.s
, "|")?
;
1984 self.print_fn_args(decl
, None
, true)?
;
1985 word(&mut self.s
, "|")?
;
1987 if let hir
::DefaultReturn(..) = decl
.output
{
1991 self.space_if_not_bol()?
;
1992 self.word_space("->")?
;
1994 hir
::Return(ref ty
) => {
1995 self.print_type(&ty
)?
;
1996 self.maybe_print_comment(ty
.span
.lo
)
1998 hir
::DefaultReturn(..) => unreachable
!(),
1999 hir
::NoReturn(span
) => {
2000 self.word_nbsp("!")?
;
2001 self.maybe_print_comment(span
.lo
)
2006 pub fn print_capture_clause(&mut self, capture_clause
: hir
::CaptureClause
) -> io
::Result
<()> {
2007 match capture_clause
{
2008 hir
::CaptureByValue
=> self.word_space("move"),
2009 hir
::CaptureByRef
=> Ok(()),
2013 pub fn print_bounds(&mut self, prefix
: &str, bounds
: &[hir
::TyParamBound
]) -> io
::Result
<()> {
2014 if !bounds
.is_empty() {
2015 word(&mut self.s
, prefix
)?
;
2016 let mut first
= true;
2017 for bound
in bounds
{
2022 self.word_space("+")?
;
2026 TraitTyParamBound(ref tref
, TraitBoundModifier
::None
) => {
2027 self.print_poly_trait_ref(tref
)
2029 TraitTyParamBound(ref tref
, TraitBoundModifier
::Maybe
) => {
2030 word(&mut self.s
, "?")?
;
2031 self.print_poly_trait_ref(tref
)
2033 RegionTyParamBound(ref lt
) => {
2034 self.print_lifetime(lt
)
2044 pub fn print_lifetime(&mut self, lifetime
: &hir
::Lifetime
) -> io
::Result
<()> {
2045 self.print_name(lifetime
.name
)
2048 pub fn print_lifetime_def(&mut self, lifetime
: &hir
::LifetimeDef
) -> io
::Result
<()> {
2049 self.print_lifetime(&lifetime
.lifetime
)?
;
2051 for v
in &lifetime
.bounds
{
2052 word(&mut self.s
, sep
)?
;
2053 self.print_lifetime(v
)?
;
2059 pub fn print_generics(&mut self, generics
: &hir
::Generics
) -> io
::Result
<()> {
2060 let total
= generics
.lifetimes
.len() + generics
.ty_params
.len();
2065 word(&mut self.s
, "<")?
;
2067 let mut ints
= Vec
::new();
2072 self.commasep(Inconsistent
, &ints
[..], |s
, &idx
| {
2073 if idx
< generics
.lifetimes
.len() {
2074 let lifetime
= &generics
.lifetimes
[idx
];
2075 s
.print_lifetime_def(lifetime
)
2077 let idx
= idx
- generics
.lifetimes
.len();
2078 let param
= &generics
.ty_params
[idx
];
2079 s
.print_ty_param(param
)
2083 word(&mut self.s
, ">")?
;
2087 pub fn print_ty_param(&mut self, param
: &hir
::TyParam
) -> io
::Result
<()> {
2088 self.print_name(param
.name
)?
;
2089 self.print_bounds(":", ¶m
.bounds
)?
;
2090 match param
.default {
2091 Some(ref default) => {
2092 space(&mut self.s
)?
;
2093 self.word_space("=")?
;
2094 self.print_type(&default)
2100 pub fn print_where_clause(&mut self, where_clause
: &hir
::WhereClause
) -> io
::Result
<()> {
2101 if where_clause
.predicates
.is_empty() {
2105 space(&mut self.s
)?
;
2106 self.word_space("where")?
;
2108 for (i
, predicate
) in where_clause
.predicates
.iter().enumerate() {
2110 self.word_space(",")?
;
2114 &hir
::WherePredicate
::BoundPredicate(hir
::WhereBoundPredicate
{ref bound_lifetimes
,
2118 self.print_formal_lifetime_list(bound_lifetimes
)?
;
2119 self.print_type(&bounded_ty
)?
;
2120 self.print_bounds(":", bounds
)?
;
2122 &hir
::WherePredicate
::RegionPredicate(hir
::WhereRegionPredicate
{ref lifetime
,
2125 self.print_lifetime(lifetime
)?
;
2126 word(&mut self.s
, ":")?
;
2128 for (i
, bound
) in bounds
.iter().enumerate() {
2129 self.print_lifetime(bound
)?
;
2132 word(&mut self.s
, ":")?
;
2136 &hir
::WherePredicate
::EqPredicate(hir
::WhereEqPredicate{ref path, ref ty, ..}
) => {
2137 self.print_path(path
, false, 0)?
;
2138 space(&mut self.s
)?
;
2139 self.word_space("=")?
;
2140 self.print_type(&ty
)?
;
2148 pub fn print_view_path(&mut self, vp
: &hir
::ViewPath
) -> io
::Result
<()> {
2150 hir
::ViewPathSimple(name
, ref path
) => {
2151 self.print_path(path
, false, 0)?
;
2153 if path
.segments
.last().unwrap().identifier
.name
!= name
{
2154 space(&mut self.s
)?
;
2155 self.word_space("as")?
;
2156 self.print_name(name
)?
;
2162 hir
::ViewPathGlob(ref path
) => {
2163 self.print_path(path
, false, 0)?
;
2164 word(&mut self.s
, "::*")
2167 hir
::ViewPathList(ref path
, ref segments
) => {
2168 if path
.segments
.is_empty() {
2169 word(&mut self.s
, "{")?
;
2171 self.print_path(path
, false, 0)?
;
2172 word(&mut self.s
, "::{")?
;
2174 self.commasep(Inconsistent
, &segments
[..], |s
, w
| {
2176 hir
::PathListIdent { name, .. }
=> {
2179 hir
::PathListMod { .. }
=> {
2180 word(&mut s
.s
, "self")
2184 word(&mut self.s
, "}")
2189 pub fn print_mutability(&mut self, mutbl
: hir
::Mutability
) -> io
::Result
<()> {
2191 hir
::MutMutable
=> self.word_nbsp("mut"),
2192 hir
::MutImmutable
=> Ok(()),
2196 pub fn print_mt(&mut self, mt
: &hir
::MutTy
) -> io
::Result
<()> {
2197 self.print_mutability(mt
.mutbl
)?
;
2198 self.print_type(&mt
.ty
)
2201 pub fn print_arg(&mut self, input
: &hir
::Arg
, is_closure
: bool
) -> io
::Result
<()> {
2202 self.ibox(indent_unit
)?
;
2203 match input
.ty
.node
{
2204 hir
::TyInfer
if is_closure
=> self.print_pat(&input
.pat
)?
,
2206 match input
.pat
.node
{
2207 PatKind
::Ident(_
, ref path1
, _
) if
2209 parse
::token
::special_idents
::invalid
.name
=> {
2213 self.print_pat(&input
.pat
)?
;
2214 word(&mut self.s
, ":")?
;
2215 space(&mut self.s
)?
;
2218 self.print_type(&input
.ty
)?
;
2224 pub fn print_fn_output(&mut self, decl
: &hir
::FnDecl
) -> io
::Result
<()> {
2225 if let hir
::DefaultReturn(..) = decl
.output
{
2229 self.space_if_not_bol()?
;
2230 self.ibox(indent_unit
)?
;
2231 self.word_space("->")?
;
2233 hir
::NoReturn(_
) => self.word_nbsp("!")?
,
2234 hir
::DefaultReturn(..) => unreachable
!(),
2235 hir
::Return(ref ty
) => self.print_type(&ty
)?
,
2240 hir
::Return(ref output
) => self.maybe_print_comment(output
.span
.lo
),
2245 pub fn print_ty_fn(&mut self,
2247 unsafety
: hir
::Unsafety
,
2249 name
: Option
<ast
::Name
>,
2250 generics
: &hir
::Generics
,
2251 opt_explicit_self
: Option
<&hir
::ExplicitSelf_
>)
2253 self.ibox(indent_unit
)?
;
2254 if !generics
.lifetimes
.is_empty() || !generics
.ty_params
.is_empty() {
2255 word(&mut self.s
, "for")?
;
2256 self.print_generics(generics
)?
;
2258 let generics
= hir
::Generics
{
2259 lifetimes
: hir
::HirVec
::new(),
2260 ty_params
: hir
::HirVec
::new(),
2261 where_clause
: hir
::WhereClause
{
2262 id
: ast
::DUMMY_NODE_ID
,
2263 predicates
: hir
::HirVec
::new(),
2268 hir
::Constness
::NotConst
,
2277 pub fn maybe_print_trailing_comment(&mut self,
2278 span
: codemap
::Span
,
2279 next_pos
: Option
<BytePos
>)
2281 let cm
= match self.cm
{
2285 match self.next_comment() {
2287 if (*cmnt
).style
!= comments
::Trailing
{
2290 let span_line
= cm
.lookup_char_pos(span
.hi
);
2291 let comment_line
= cm
.lookup_char_pos((*cmnt
).pos
);
2292 let mut next
= (*cmnt
).pos
+ BytePos(1);
2295 Some(p
) => next
= p
,
2297 if span
.hi
< (*cmnt
).pos
&& (*cmnt
).pos
< next
&&
2298 span_line
.line
== comment_line
.line
{
2299 self.print_comment(cmnt
)?
;
2300 self.cur_cmnt_and_lit
.cur_cmnt
+= 1;
2308 pub fn print_remaining_comments(&mut self) -> io
::Result
<()> {
2309 // If there aren't any remaining comments, then we need to manually
2310 // make sure there is a line break at the end.
2311 if self.next_comment().is_none() {
2312 hardbreak(&mut self.s
)?
;
2315 match self.next_comment() {
2317 self.print_comment(cmnt
)?
;
2318 self.cur_cmnt_and_lit
.cur_cmnt
+= 1;
2326 pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
2327 opt_abi
: Option
<Abi
>)
2330 Some(Abi
::Rust
) => Ok(()),
2332 self.word_nbsp("extern")?
;
2333 self.word_nbsp(&abi
.to_string())
2339 pub fn print_extern_opt_abi(&mut self, opt_abi
: Option
<Abi
>) -> io
::Result
<()> {
2342 self.word_nbsp("extern")?
;
2343 self.word_nbsp(&abi
.to_string())
2349 pub fn print_fn_header_info(&mut self,
2350 unsafety
: hir
::Unsafety
,
2351 constness
: hir
::Constness
,
2353 vis
: &hir
::Visibility
)
2355 word(&mut self.s
, &visibility_qualified(vis
, ""))?
;
2356 self.print_unsafety(unsafety
)?
;
2359 hir
::Constness
::NotConst
=> {}
2360 hir
::Constness
::Const
=> self.word_nbsp("const")?
,
2363 if abi
!= Abi
::Rust
{
2364 self.word_nbsp("extern")?
;
2365 self.word_nbsp(&abi
.to_string())?
;
2368 word(&mut self.s
, "fn")
2371 pub fn print_unsafety(&mut self, s
: hir
::Unsafety
) -> io
::Result
<()> {
2373 hir
::Unsafety
::Normal
=> Ok(()),
2374 hir
::Unsafety
::Unsafe
=> self.word_nbsp("unsafe"),
2379 // Dup'ed from parse::classify, but adapted for the HIR.
2380 /// Does this expression require a semicolon to be treated
2381 /// as a statement? The negation of this: 'can this expression
2382 /// be used as a statement without a semicolon' -- is used
2383 /// as an early-bail-out in the parser so that, for instance,
2384 /// if true {...} else {...}
2386 /// isn't parsed as (if true {...} else {...} | x) | 5
2387 fn expr_requires_semi_to_be_stmt(e
: &hir
::Expr
) -> bool
{
2390 hir
::ExprMatch(..) |
2392 hir
::ExprWhile(..) |
2393 hir
::ExprLoop(..) => false,
2398 /// this statement requires a semicolon after it.
2399 /// note that in one case (stmt_semi), we've already
2400 /// seen the semicolon, and thus don't need another.
2401 fn stmt_ends_with_semi(stmt
: &hir
::Stmt_
) -> bool
{
2403 hir
::StmtDecl(ref d
, _
) => {
2405 hir
::DeclLocal(_
) => true,
2406 hir
::DeclItem(_
) => false,
2409 hir
::StmtExpr(ref e
, _
) => {
2410 expr_requires_semi_to_be_stmt(&e
)
2412 hir
::StmtSemi(..) => {