1mod expr;
6mod fixup;
7mod item;
8
9use std::borrow::Cow;
10use std::sync::Arc;
11
12use rustc_ast::attr::AttrIdGenerator;
13use rustc_ast::ptr::P;
14use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKind};
15use rustc_ast::tokenstream::{Spacing, TokenStream, TokenTree};
16use rustc_ast::util::classify;
17use rustc_ast::util::comments::{Comment, CommentStyle};
18use rustc_ast::{
19 self as ast, AttrArgs, BindingMode, BlockCheckMode, ByRef, DelimArgs, GenericArg, GenericBound,
20 InlineAsmOperand, InlineAsmOptions, InlineAsmRegOrRegClass, InlineAsmTemplatePiece, PatKind,
21 RangeEnd, RangeSyntax, Safety, SelfKind, Term, attr,
22};
23use rustc_span::edition::Edition;
24use rustc_span::source_map::{SourceMap, Spanned};
25use rustc_span::symbol::IdentPrinter;
26use rustc_span::{BytePos, CharPos, DUMMY_SP, FileName, Ident, Pos, Span, Symbol, kw, sym};
27
28use crate::pp::Breaks::{Consistent, Inconsistent};
29use crate::pp::{self, BoxMarker, Breaks};
30use crate::pprust::state::fixup::FixupContext;
31
32pub enum MacHeader<'a> {
33 Path(&'a ast::Path),
34 Keyword(&'static str),
35}
36
37pub enum AnnNode<'a> {
38 Ident(&'a Ident),
39 Name(&'a Symbol),
40 Block(&'a ast::Block),
41 Item(&'a ast::Item),
42 SubItem(ast::NodeId),
43 Expr(&'a ast::Expr),
44 Pat(&'a ast::Pat),
45 Crate(&'a ast::Crate),
46}
47
48pub trait PpAnn {
49 fn pre(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
50 fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
51}
52
53struct NoAnn;
54
55impl PpAnn for NoAnn {}
56
57pub struct Comments<'a> {
58 sm: &'a SourceMap,
59 reversed_comments: Vec<Comment>,
61}
62
63fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
67 let mut idx = 0;
68 for (i, ch) in s.char_indices().take(col.to_usize()) {
69 if !ch.is_whitespace() {
70 return None;
71 }
72 idx = i + ch.len_utf8();
73 }
74 Some(idx)
75}
76
77fn trim_whitespace_prefix(s: &str, col: CharPos) -> &str {
78 let len = s.len();
79 match all_whitespace(s, col) {
80 Some(col) => {
81 if col < len {
82 &s[col..]
83 } else {
84 ""
85 }
86 }
87 None => s,
88 }
89}
90
91fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> {
92 let mut res: Vec<String> = vec![];
93 let mut lines = text.lines();
94 res.extend(lines.next().map(|it| it.to_string()));
96 for line in lines {
98 res.push(trim_whitespace_prefix(line, col).to_string())
99 }
100 res
101}
102
103fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> {
104 let sm = SourceMap::new(sm.path_mapping().clone());
105 let source_file = sm.new_source_file(path, src);
106 let text = Arc::clone(&(*source_file.src.as_ref().unwrap()));
107
108 let text: &str = text.as_str();
109 let start_bpos = source_file.start_pos;
110 let mut pos = 0;
111 let mut comments: Vec<Comment> = Vec::new();
112 let mut code_to_the_left = false;
113
114 if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
115 comments.push(Comment {
116 style: CommentStyle::Isolated,
117 lines: vec![text[..shebang_len].to_string()],
118 pos: start_bpos,
119 });
120 pos += shebang_len;
121 }
122
123 for token in rustc_lexer::tokenize(&text[pos..]) {
124 let token_text = &text[pos..pos + token.len as usize];
125 match token.kind {
126 rustc_lexer::TokenKind::Whitespace => {
127 if let Some(mut idx) = token_text.find('\n') {
128 code_to_the_left = false;
129 while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
130 idx += 1 + next_newline;
131 comments.push(Comment {
132 style: CommentStyle::BlankLine,
133 lines: vec![],
134 pos: start_bpos + BytePos((pos + idx) as u32),
135 });
136 }
137 }
138 }
139 rustc_lexer::TokenKind::BlockComment { doc_style, .. } => {
140 if doc_style.is_none() {
141 let code_to_the_right = !matches!(
142 text[pos + token.len as usize..].chars().next(),
143 Some('\r' | '\n')
144 );
145 let style = match (code_to_the_left, code_to_the_right) {
146 (_, true) => CommentStyle::Mixed,
147 (false, false) => CommentStyle::Isolated,
148 (true, false) => CommentStyle::Trailing,
149 };
150
151 let pos_in_file = start_bpos + BytePos(pos as u32);
153 let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
154 let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
155 let col = CharPos(text[line_begin_pos..pos].chars().count());
156
157 let lines = split_block_comment_into_lines(token_text, col);
158 comments.push(Comment { style, lines, pos: pos_in_file })
159 }
160 }
161 rustc_lexer::TokenKind::LineComment { doc_style } => {
162 if doc_style.is_none() {
163 comments.push(Comment {
164 style: if code_to_the_left {
165 CommentStyle::Trailing
166 } else {
167 CommentStyle::Isolated
168 },
169 lines: vec![token_text.to_string()],
170 pos: start_bpos + BytePos(pos as u32),
171 })
172 }
173 }
174 _ => {
175 code_to_the_left = true;
176 }
177 }
178 pos += token.len as usize;
179 }
180
181 comments
182}
183
184impl<'a> Comments<'a> {
185 pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
186 let mut comments = gather_comments(sm, filename, input);
187 comments.reverse();
188 Comments { sm, reversed_comments: comments }
189 }
190
191 fn peek(&self) -> Option<&Comment> {
192 self.reversed_comments.last()
193 }
194
195 fn next(&mut self) -> Option<Comment> {
196 self.reversed_comments.pop()
197 }
198
199 fn trailing_comment(
200 &mut self,
201 span: rustc_span::Span,
202 next_pos: Option<BytePos>,
203 ) -> Option<Comment> {
204 if let Some(cmnt) = self.peek() {
205 if cmnt.style != CommentStyle::Trailing {
206 return None;
207 }
208 let span_line = self.sm.lookup_char_pos(span.hi());
209 let comment_line = self.sm.lookup_char_pos(cmnt.pos);
210 let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1));
211 if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
212 return Some(self.next().unwrap());
213 }
214 }
215
216 None
217 }
218}
219
220pub struct State<'a> {
221 pub s: pp::Printer,
222 comments: Option<Comments<'a>>,
223 ann: &'a (dyn PpAnn + 'a),
224 is_sdylib_interface: bool,
225}
226
227const INDENT_UNIT: isize = 4;
228
229pub fn print_crate<'a>(
232 sm: &'a SourceMap,
233 krate: &ast::Crate,
234 filename: FileName,
235 input: String,
236 ann: &'a dyn PpAnn,
237 is_expanded: bool,
238 edition: Edition,
239 g: &AttrIdGenerator,
240) -> String {
241 let mut s = State {
242 s: pp::Printer::new(),
243 comments: Some(Comments::new(sm, filename, input)),
244 ann,
245 is_sdylib_interface: false,
246 };
247
248 print_crate_inner(&mut s, krate, is_expanded, edition, g);
249 s.s.eof()
250}
251
252pub fn print_crate_as_interface(
253 krate: &ast::Crate,
254 edition: Edition,
255 g: &AttrIdGenerator,
256) -> String {
257 let mut s =
258 State { s: pp::Printer::new(), comments: None, ann: &NoAnn, is_sdylib_interface: true };
259
260 print_crate_inner(&mut s, krate, false, edition, g);
261 s.s.eof()
262}
263
264fn print_crate_inner<'a>(
265 s: &mut State<'a>,
266 krate: &ast::Crate,
267 is_expanded: bool,
268 edition: Edition,
269 g: &AttrIdGenerator,
270) {
271 s.maybe_print_shebang();
275
276 if is_expanded && !krate.attrs.iter().any(|attr| attr.has_name(sym::no_core)) {
277 let fake_attr = attr::mk_attr_nested_word(
284 g,
285 ast::AttrStyle::Inner,
286 Safety::Default,
287 sym::feature,
288 sym::prelude_import,
289 DUMMY_SP,
290 );
291 s.print_attribute(&fake_attr);
292
293 if edition.is_rust_2015() {
296 let fake_attr = attr::mk_attr_word(
298 g,
299 ast::AttrStyle::Inner,
300 Safety::Default,
301 sym::no_std,
302 DUMMY_SP,
303 );
304 s.print_attribute(&fake_attr);
305 }
306 }
307
308 s.print_inner_attributes(&krate.attrs);
309 for item in &krate.items {
310 s.print_item(item);
311 }
312 s.print_remaining_comments();
313 s.ann.post(s, AnnNode::Crate(krate));
314}
315
316fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
324 use Delimiter::*;
325 use TokenTree::{Delimited as Del, Token as Tok};
326 use token::*;
327
328 fn is_punct(tt: &TokenTree) -> bool {
329 matches!(tt, TokenTree::Token(tok, _) if tok.is_punct())
330 }
331
332 match (tt1, tt2) {
336 (Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,
338
339 (Tok(Token { kind: Dot, .. }, _), tt2) if !is_punct(tt2) => false,
341
342 (Tok(Token { kind: Dollar, .. }, _), Tok(Token { kind: Ident(..), .. }, _)) => false,
344
345 (tt1, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) if !is_punct(tt1) => false,
349
350 (Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: Bang, .. }, _))
352 if !Ident::new(*sym, *span).is_reserved() || matches!(is_raw, IdentIsRaw::Yes) =>
353 {
354 false
355 }
356
357 (Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, _, Parenthesis, _))
360 if !Ident::new(*sym, *span).is_reserved()
361 || *sym == kw::Fn
362 || *sym == kw::SelfUpper
363 || *sym == kw::Pub
364 || matches!(is_raw, IdentIsRaw::Yes) =>
365 {
366 false
367 }
368
369 (Tok(Token { kind: Pound, .. }, _), Del(_, _, Bracket, _)) => false,
371
372 _ => true,
373 }
374}
375
376pub fn doc_comment_to_string(
377 comment_kind: CommentKind,
378 attr_style: ast::AttrStyle,
379 data: Symbol,
380) -> String {
381 match (comment_kind, attr_style) {
382 (CommentKind::Line, ast::AttrStyle::Outer) => format!("///{data}"),
383 (CommentKind::Line, ast::AttrStyle::Inner) => format!("//!{data}"),
384 (CommentKind::Block, ast::AttrStyle::Outer) => format!("/**{data}*/"),
385 (CommentKind::Block, ast::AttrStyle::Inner) => format!("/*!{data}*/"),
386 }
387}
388
389fn literal_to_string(lit: token::Lit) -> String {
390 let token::Lit { kind, symbol, suffix } = lit;
391 let mut out = match kind {
392 token::Byte => format!("b'{symbol}'"),
393 token::Char => format!("'{symbol}'"),
394 token::Str => format!("\"{symbol}\""),
395 token::StrRaw(n) => {
396 format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol)
397 }
398 token::ByteStr => format!("b\"{symbol}\""),
399 token::ByteStrRaw(n) => {
400 format!("br{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol)
401 }
402 token::CStr => format!("c\"{symbol}\""),
403 token::CStrRaw(n) => {
404 format!("cr{delim}\"{symbol}\"{delim}", delim = "#".repeat(n as usize))
405 }
406 token::Integer | token::Float | token::Bool | token::Err(_) => symbol.to_string(),
407 };
408
409 if let Some(suffix) = suffix {
410 out.push_str(suffix.as_str())
411 }
412
413 out
414}
415
416impl std::ops::Deref for State<'_> {
417 type Target = pp::Printer;
418 fn deref(&self) -> &Self::Target {
419 &self.s
420 }
421}
422
423impl std::ops::DerefMut for State<'_> {
424 fn deref_mut(&mut self) -> &mut Self::Target {
425 &mut self.s
426 }
427}
428
429pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::DerefMut {
431 fn comments(&self) -> Option<&Comments<'a>>;
432 fn comments_mut(&mut self) -> Option<&mut Comments<'a>>;
433 fn ann_post(&mut self, ident: Ident);
434 fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool);
435
436 fn print_ident(&mut self, ident: Ident) {
437 self.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string());
438 self.ann_post(ident)
439 }
440
441 fn strsep<'x, T: 'x, F, I>(
442 &mut self,
443 sep: &'static str,
444 space_before: bool,
445 b: Breaks,
446 elts: I,
447 mut op: F,
448 ) where
449 F: FnMut(&mut Self, &T),
450 I: IntoIterator<Item = &'x T>,
451 {
452 let mut it = elts.into_iter();
453
454 let rb = self.rbox(0, b);
455 if let Some(first) = it.next() {
456 op(self, first);
457 for elt in it {
458 if space_before {
459 self.space();
460 }
461 self.word_space(sep);
462 op(self, elt);
463 }
464 }
465 self.end(rb);
466 }
467
468 fn commasep<'x, T: 'x, F, I>(&mut self, b: Breaks, elts: I, op: F)
469 where
470 F: FnMut(&mut Self, &T),
471 I: IntoIterator<Item = &'x T>,
472 {
473 self.strsep(",", false, b, elts, op)
474 }
475
476 fn maybe_print_comment(&mut self, pos: BytePos) -> bool {
477 let mut has_comment = false;
478 while let Some(cmnt) = self.peek_comment() {
479 if cmnt.pos >= pos {
480 break;
481 }
482 has_comment = true;
483 let cmnt = self.next_comment().unwrap();
484 self.print_comment(cmnt);
485 }
486 has_comment
487 }
488
489 fn print_comment(&mut self, cmnt: Comment) {
490 match cmnt.style {
491 CommentStyle::Mixed => {
492 if !self.is_beginning_of_line() {
493 self.zerobreak();
494 }
495 if let Some((last, lines)) = cmnt.lines.split_last() {
496 let ib = self.ibox(0);
497
498 for line in lines {
499 self.word(line.clone());
500 self.hardbreak()
501 }
502
503 self.word(last.clone());
504 self.space();
505
506 self.end(ib);
507 }
508 self.zerobreak()
509 }
510 CommentStyle::Isolated => {
511 self.hardbreak_if_not_bol();
512 for line in &cmnt.lines {
513 if !line.is_empty() {
516 self.word(line.clone());
517 }
518 self.hardbreak();
519 }
520 }
521 CommentStyle::Trailing => {
522 if !self.is_beginning_of_line() {
523 self.word(" ");
524 }
525 if let [line] = cmnt.lines.as_slice() {
526 self.word(line.clone());
527 self.hardbreak()
528 } else {
529 let vb = self.visual_align();
530 for line in &cmnt.lines {
531 if !line.is_empty() {
532 self.word(line.clone());
533 }
534 self.hardbreak();
535 }
536 self.end(vb);
537 }
538 }
539 CommentStyle::BlankLine => {
540 let twice = match self.last_token() {
542 Some(pp::Token::String(s)) => ";" == s,
543 Some(pp::Token::Begin(_)) => true,
544 Some(pp::Token::End) => true,
545 _ => false,
546 };
547 if twice {
548 self.hardbreak();
549 }
550 self.hardbreak();
551 }
552 }
553 }
554
555 fn peek_comment<'b>(&'b self) -> Option<&'b Comment>
556 where
557 'a: 'b,
558 {
559 self.comments().and_then(|c| c.peek())
560 }
561
562 fn next_comment(&mut self) -> Option<Comment> {
563 self.comments_mut().and_then(|c| c.next())
564 }
565
566 fn maybe_print_trailing_comment(&mut self, span: rustc_span::Span, next_pos: Option<BytePos>) {
567 if let Some(cmnts) = self.comments_mut() {
568 if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
569 self.print_comment(cmnt);
570 }
571 }
572 }
573
574 fn print_remaining_comments(&mut self) {
575 if self.peek_comment().is_none() {
578 self.hardbreak();
579 }
580 while let Some(cmnt) = self.next_comment() {
581 self.print_comment(cmnt)
582 }
583 }
584
585 fn print_string(&mut self, st: &str, style: ast::StrStyle) {
586 let st = match style {
587 ast::StrStyle::Cooked => format!("\"{}\"", st.escape_debug()),
588 ast::StrStyle::Raw(n) => {
589 format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st)
590 }
591 };
592 self.word(st)
593 }
594
595 fn maybe_print_shebang(&mut self) {
596 if let Some(cmnt) = self.peek_comment() {
597 if cmnt.style == CommentStyle::Isolated
601 && cmnt.lines.first().map_or(false, |l| l.starts_with("#!"))
602 {
603 let cmnt = self.next_comment().unwrap();
604 self.print_comment(cmnt);
605 }
606 }
607 }
608
609 fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
610 self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
611 }
612
613 fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
614 self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
615 }
616
617 fn print_either_attributes(
618 &mut self,
619 attrs: &[ast::Attribute],
620 kind: ast::AttrStyle,
621 is_inline: bool,
622 trailing_hardbreak: bool,
623 ) -> bool {
624 let mut printed = false;
625 for attr in attrs {
626 if attr.style == kind {
627 if self.print_attribute_inline(attr, is_inline) {
628 if is_inline {
629 self.nbsp();
630 }
631 printed = true;
632 }
633 }
634 }
635 if printed && trailing_hardbreak && !is_inline {
636 self.hardbreak_if_not_bol();
637 }
638 printed
639 }
640
641 fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) -> bool {
642 if attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace) {
643 return false;
646 }
647 if !is_inline {
648 self.hardbreak_if_not_bol();
649 }
650 self.maybe_print_comment(attr.span.lo());
651 match &attr.kind {
652 ast::AttrKind::Normal(normal) => {
653 match attr.style {
654 ast::AttrStyle::Inner => self.word("#!["),
655 ast::AttrStyle::Outer => self.word("#["),
656 }
657 self.print_attr_item(&normal.item, attr.span);
658 self.word("]");
659 }
660 ast::AttrKind::DocComment(comment_kind, data) => {
661 self.word(doc_comment_to_string(*comment_kind, attr.style, *data));
662 self.hardbreak()
663 }
664 }
665 true
666 }
667
668 fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
669 let ib = self.ibox(0);
670 match item.unsafety {
671 ast::Safety::Unsafe(_) => {
672 self.word("unsafe");
673 self.popen();
674 }
675 ast::Safety::Default | ast::Safety::Safe(_) => {}
676 }
677 match &item.args {
678 AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
679 Some(MacHeader::Path(&item.path)),
680 false,
681 None,
682 *delim,
683 None,
684 tokens,
685 true,
686 span,
687 ),
688 AttrArgs::Empty => {
689 self.print_path(&item.path, false, 0);
690 }
691 AttrArgs::Eq { expr, .. } => {
692 self.print_path(&item.path, false, 0);
693 self.space();
694 self.word_space("=");
695 let token_str = self.expr_to_string(expr);
696 self.word(token_str);
697 }
698 }
699 match item.unsafety {
700 ast::Safety::Unsafe(_) => self.pclose(),
701 ast::Safety::Default | ast::Safety::Safe(_) => {}
702 }
703 self.end(ib);
704 }
705
706 fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) -> Spacing {
714 match tt {
715 TokenTree::Token(token, spacing) => {
716 let token_str = self.token_to_string_ext(token, convert_dollar_crate);
717 self.word(token_str);
718 if let token::DocComment(..) = token.kind {
719 self.hardbreak()
720 }
721 *spacing
722 }
723 TokenTree::Delimited(dspan, spacing, delim, tts) => {
724 self.print_mac_common(
725 None,
726 false,
727 None,
728 *delim,
729 Some(spacing.open),
730 tts,
731 convert_dollar_crate,
732 dspan.entire(),
733 );
734 spacing.close
735 }
736 }
737 }
738
739 fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
769 let mut iter = tts.iter().peekable();
770 while let Some(tt) = iter.next() {
771 let spacing = self.print_tt(tt, convert_dollar_crate);
772 if let Some(next) = iter.peek() {
773 if spacing == Spacing::Alone && space_between(tt, next) {
774 self.space();
775 }
776 }
777 }
778 }
779
780 fn print_mac_common(
781 &mut self,
782 header: Option<MacHeader<'_>>,
783 has_bang: bool,
784 ident: Option<Ident>,
785 delim: Delimiter,
786 open_spacing: Option<Spacing>,
787 tts: &TokenStream,
788 convert_dollar_crate: bool,
789 span: Span,
790 ) {
791 let cb = (delim == Delimiter::Brace).then(|| self.cbox(INDENT_UNIT));
792 match header {
793 Some(MacHeader::Path(path)) => self.print_path(path, false, 0),
794 Some(MacHeader::Keyword(kw)) => self.word(kw),
795 None => {}
796 }
797 if has_bang {
798 self.word("!");
799 }
800 if let Some(ident) = ident {
801 self.nbsp();
802 self.print_ident(ident);
803 }
804 match delim {
805 Delimiter::Brace => {
806 if header.is_some() || has_bang || ident.is_some() {
807 self.nbsp();
808 }
809 self.word("{");
810
811 let open_space = (open_spacing == None || open_spacing == Some(Spacing::Alone))
813 && !tts.is_empty();
814 if open_space {
815 self.space();
816 }
817 let ib = self.ibox(0);
818 self.print_tts(tts, convert_dollar_crate);
819 self.end(ib);
820
821 self.bclose(span, !open_space, cb.unwrap());
826 }
827 delim => {
828 let token_str = self.token_kind_to_string(&delim.as_open_token_kind());
831 self.word(token_str);
832 let ib = self.ibox(0);
833 self.print_tts(tts, convert_dollar_crate);
834 self.end(ib);
835 let token_str = self.token_kind_to_string(&delim.as_close_token_kind());
836 self.word(token_str);
837 }
838 }
839 }
840
841 fn print_mac_def(
842 &mut self,
843 macro_def: &ast::MacroDef,
844 ident: &Ident,
845 sp: Span,
846 print_visibility: impl FnOnce(&mut Self),
847 ) {
848 let (kw, has_bang) = if macro_def.macro_rules {
849 ("macro_rules", true)
850 } else {
851 print_visibility(self);
852 ("macro", false)
853 };
854 self.print_mac_common(
855 Some(MacHeader::Keyword(kw)),
856 has_bang,
857 Some(*ident),
858 macro_def.body.delim,
859 None,
860 ¯o_def.body.tokens,
861 true,
862 sp,
863 );
864 if macro_def.body.need_semicolon() {
865 self.word(";");
866 }
867 }
868
869 fn print_path(&mut self, path: &ast::Path, colons_before_params: bool, depth: usize) {
870 self.maybe_print_comment(path.span.lo());
871
872 for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() {
873 if i > 0 {
874 self.word("::")
875 }
876 self.print_path_segment(segment, colons_before_params);
877 }
878 }
879
880 fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) {
881 if segment.ident.name != kw::PathRoot {
882 self.print_ident(segment.ident);
883 if let Some(args) = &segment.args {
884 self.print_generic_args(args, colons_before_params);
885 }
886 }
887 }
888
889 fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) -> (BoxMarker, BoxMarker) {
890 let w = w.into();
891 let cb = self.cbox(INDENT_UNIT);
893 let ib = self.ibox(0);
895 if !w.is_empty() {
897 self.word_nbsp(w);
898 }
899 (cb, ib)
900 }
901
902 fn bopen(&mut self, ib: BoxMarker) {
903 self.word("{");
904 self.end(ib);
905 }
906
907 fn bclose_maybe_open(&mut self, span: rustc_span::Span, no_space: bool, cb: Option<BoxMarker>) {
908 let has_comment = self.maybe_print_comment(span.hi());
909 if !no_space || has_comment {
910 self.break_offset_if_not_bol(1, -INDENT_UNIT);
911 }
912 self.word("}");
913 if let Some(cb) = cb {
914 self.end(cb);
915 }
916 }
917
918 fn bclose(&mut self, span: rustc_span::Span, no_space: bool, cb: BoxMarker) {
919 let cb = Some(cb);
920 self.bclose_maybe_open(span, no_space, cb)
921 }
922
923 fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
924 if !self.is_beginning_of_line() {
925 self.break_offset(n, off)
926 } else if off != 0 {
927 if let Some(last_token) = self.last_token_still_buffered() {
928 if last_token.is_hardbreak_tok() {
929 self.replace_last_token_still_buffered(pp::Printer::hardbreak_tok_offset(off));
933 }
934 }
935 }
936 }
937
938 fn token_kind_to_string(&self, tok: &TokenKind) -> Cow<'static, str> {
940 self.token_kind_to_string_ext(tok, None)
941 }
942
943 fn token_kind_to_string_ext(
944 &self,
945 tok: &TokenKind,
946 convert_dollar_crate: Option<Span>,
947 ) -> Cow<'static, str> {
948 match *tok {
949 token::Eq => "=".into(),
950 token::Lt => "<".into(),
951 token::Le => "<=".into(),
952 token::EqEq => "==".into(),
953 token::Ne => "!=".into(),
954 token::Ge => ">=".into(),
955 token::Gt => ">".into(),
956 token::Bang => "!".into(),
957 token::Tilde => "~".into(),
958 token::OrOr => "||".into(),
959 token::AndAnd => "&&".into(),
960 token::Plus => "+".into(),
961 token::Minus => "-".into(),
962 token::Star => "*".into(),
963 token::Slash => "/".into(),
964 token::Percent => "%".into(),
965 token::Caret => "^".into(),
966 token::And => "&".into(),
967 token::Or => "|".into(),
968 token::Shl => "<<".into(),
969 token::Shr => ">>".into(),
970 token::PlusEq => "+=".into(),
971 token::MinusEq => "-=".into(),
972 token::StarEq => "*=".into(),
973 token::SlashEq => "/=".into(),
974 token::PercentEq => "%=".into(),
975 token::CaretEq => "^=".into(),
976 token::AndEq => "&=".into(),
977 token::OrEq => "|=".into(),
978 token::ShlEq => "<<=".into(),
979 token::ShrEq => ">>=".into(),
980
981 token::At => "@".into(),
983 token::Dot => ".".into(),
984 token::DotDot => "..".into(),
985 token::DotDotDot => "...".into(),
986 token::DotDotEq => "..=".into(),
987 token::Comma => ",".into(),
988 token::Semi => ";".into(),
989 token::Colon => ":".into(),
990 token::PathSep => "::".into(),
991 token::RArrow => "->".into(),
992 token::LArrow => "<-".into(),
993 token::FatArrow => "=>".into(),
994 token::OpenParen => "(".into(),
995 token::CloseParen => ")".into(),
996 token::OpenBracket => "[".into(),
997 token::CloseBracket => "]".into(),
998 token::OpenBrace => "{".into(),
999 token::CloseBrace => "}".into(),
1000 token::OpenInvisible(_) | token::CloseInvisible(_) => "".into(),
1001 token::Pound => "#".into(),
1002 token::Dollar => "$".into(),
1003 token::Question => "?".into(),
1004 token::SingleQuote => "'".into(),
1005
1006 token::Literal(lit) => literal_to_string(lit).into(),
1008
1009 token::Ident(name, is_raw) => {
1011 IdentPrinter::new(name, is_raw.into(), convert_dollar_crate).to_string().into()
1012 }
1013 token::NtIdent(ident, is_raw) => {
1014 IdentPrinter::for_ast_ident(ident, is_raw.into()).to_string().into()
1015 }
1016
1017 token::Lifetime(name, IdentIsRaw::No)
1018 | token::NtLifetime(Ident { name, .. }, IdentIsRaw::No) => name.to_string().into(),
1019 token::Lifetime(name, IdentIsRaw::Yes)
1020 | token::NtLifetime(Ident { name, .. }, IdentIsRaw::Yes) => {
1021 format!("'r#{}", &name.as_str()[1..]).into()
1022 }
1023
1024 token::DocComment(comment_kind, attr_style, data) => {
1026 doc_comment_to_string(comment_kind, attr_style, data).into()
1027 }
1028 token::Eof => "<eof>".into(),
1029 }
1030 }
1031
1032 fn token_to_string(&self, token: &Token) -> Cow<'static, str> {
1034 self.token_to_string_ext(token, false)
1035 }
1036
1037 fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
1038 let convert_dollar_crate = convert_dollar_crate.then_some(token.span);
1039 self.token_kind_to_string_ext(&token.kind, convert_dollar_crate)
1040 }
1041
1042 fn ty_to_string(&self, ty: &ast::Ty) -> String {
1043 Self::to_string(|s| s.print_type(ty))
1044 }
1045
1046 fn pat_to_string(&self, pat: &ast::Pat) -> String {
1047 Self::to_string(|s| s.print_pat(pat))
1048 }
1049
1050 fn expr_to_string(&self, e: &ast::Expr) -> String {
1051 Self::to_string(|s| s.print_expr(e, FixupContext::default()))
1052 }
1053
1054 fn meta_item_lit_to_string(&self, lit: &ast::MetaItemLit) -> String {
1055 Self::to_string(|s| s.print_meta_item_lit(lit))
1056 }
1057
1058 fn stmt_to_string(&self, stmt: &ast::Stmt) -> String {
1059 Self::to_string(|s| s.print_stmt(stmt))
1060 }
1061
1062 fn item_to_string(&self, i: &ast::Item) -> String {
1063 Self::to_string(|s| s.print_item(i))
1064 }
1065
1066 fn assoc_item_to_string(&self, i: &ast::AssocItem) -> String {
1067 Self::to_string(|s| s.print_assoc_item(i))
1068 }
1069
1070 fn foreign_item_to_string(&self, i: &ast::ForeignItem) -> String {
1071 Self::to_string(|s| s.print_foreign_item(i))
1072 }
1073
1074 fn path_to_string(&self, p: &ast::Path) -> String {
1075 Self::to_string(|s| s.print_path(p, false, 0))
1076 }
1077
1078 fn vis_to_string(&self, v: &ast::Visibility) -> String {
1079 Self::to_string(|s| s.print_visibility(v))
1080 }
1081
1082 fn block_to_string(&self, blk: &ast::Block) -> String {
1083 Self::to_string(|s| {
1084 let (cb, ib) = s.head("");
1085 s.print_block(blk, cb, ib)
1086 })
1087 }
1088
1089 fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
1090 Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
1091 }
1092
1093 fn tts_to_string(&self, tokens: &TokenStream) -> String {
1094 Self::to_string(|s| s.print_tts(tokens, false))
1095 }
1096
1097 fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
1098 let mut printer = State::new();
1099 f(&mut printer);
1100 printer.s.eof()
1101 }
1102}
1103
1104impl<'a> PrintState<'a> for State<'a> {
1105 fn comments(&self) -> Option<&Comments<'a>> {
1106 self.comments.as_ref()
1107 }
1108
1109 fn comments_mut(&mut self) -> Option<&mut Comments<'a>> {
1110 self.comments.as_mut()
1111 }
1112
1113 fn ann_post(&mut self, ident: Ident) {
1114 self.ann.post(self, AnnNode::Ident(&ident));
1115 }
1116
1117 fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) {
1118 if colons_before_params {
1119 self.word("::")
1120 }
1121
1122 match args {
1123 ast::GenericArgs::AngleBracketed(data) => {
1124 self.word("<");
1125 self.commasep(Inconsistent, &data.args, |s, arg| match arg {
1126 ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a),
1127 ast::AngleBracketedArg::Constraint(c) => s.print_assoc_item_constraint(c),
1128 });
1129 self.word(">")
1130 }
1131
1132 ast::GenericArgs::Parenthesized(data) => {
1133 self.word("(");
1134 self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
1135 self.word(")");
1136 self.print_fn_ret_ty(&data.output);
1137 }
1138 ast::GenericArgs::ParenthesizedElided(_) => {
1139 self.word("(");
1140 self.word("..");
1141 self.word(")");
1142 }
1143 }
1144 }
1145}
1146
1147impl<'a> State<'a> {
1148 pub fn new() -> State<'a> {
1149 State { s: pp::Printer::new(), comments: None, ann: &NoAnn, is_sdylib_interface: false }
1150 }
1151
1152 fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
1153 where
1154 F: FnMut(&mut State<'_>, &T),
1155 G: FnMut(&T) -> rustc_span::Span,
1156 {
1157 let rb = self.rbox(0, b);
1158 let len = elts.len();
1159 let mut i = 0;
1160 for elt in elts {
1161 self.maybe_print_comment(get_span(elt).hi());
1162 op(self, elt);
1163 i += 1;
1164 if i < len {
1165 self.word(",");
1166 self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
1167 self.space_if_not_bol();
1168 }
1169 }
1170 self.end(rb);
1171 }
1172
1173 fn commasep_exprs(&mut self, b: Breaks, exprs: &[P<ast::Expr>]) {
1174 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e, FixupContext::default()), |e| e.span)
1175 }
1176
1177 pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
1178 if let Some(lt) = *lifetime {
1179 self.print_lifetime(lt);
1180 self.nbsp();
1181 }
1182 }
1183
1184 pub fn print_assoc_item_constraint(&mut self, constraint: &ast::AssocItemConstraint) {
1185 self.print_ident(constraint.ident);
1186 if let Some(args) = constraint.gen_args.as_ref() {
1187 self.print_generic_args(args, false)
1188 }
1189 self.space();
1190 match &constraint.kind {
1191 ast::AssocItemConstraintKind::Equality { term } => {
1192 self.word_space("=");
1193 match term {
1194 Term::Ty(ty) => self.print_type(ty),
1195 Term::Const(c) => self.print_expr_anon_const(c, &[]),
1196 }
1197 }
1198 ast::AssocItemConstraintKind::Bound { bounds } => {
1199 if !bounds.is_empty() {
1200 self.word_nbsp(":");
1201 self.print_type_bounds(bounds);
1202 }
1203 }
1204 }
1205 }
1206
1207 pub fn print_generic_arg(&mut self, generic_arg: &GenericArg) {
1208 match generic_arg {
1209 GenericArg::Lifetime(lt) => self.print_lifetime(*lt),
1210 GenericArg::Type(ty) => self.print_type(ty),
1211 GenericArg::Const(ct) => self.print_expr(&ct.value, FixupContext::default()),
1212 }
1213 }
1214
1215 pub fn print_ty_pat(&mut self, pat: &ast::TyPat) {
1216 match &pat.kind {
1217 rustc_ast::TyPatKind::Range(start, end, include_end) => {
1218 if let Some(start) = start {
1219 self.print_expr_anon_const(start, &[]);
1220 }
1221 self.word("..");
1222 if let Some(end) = end {
1223 if let RangeEnd::Included(_) = include_end.node {
1224 self.word("=");
1225 }
1226 self.print_expr_anon_const(end, &[]);
1227 }
1228 }
1229 rustc_ast::TyPatKind::Or(variants) => {
1230 let mut first = true;
1231 for pat in variants {
1232 if first {
1233 first = false
1234 } else {
1235 self.word(" | ");
1236 }
1237 self.print_ty_pat(pat);
1238 }
1239 }
1240 rustc_ast::TyPatKind::Err(_) => {
1241 self.popen();
1242 self.word("/*ERROR*/");
1243 self.pclose();
1244 }
1245 }
1246 }
1247
1248 pub fn print_type(&mut self, ty: &ast::Ty) {
1249 self.maybe_print_comment(ty.span.lo());
1250 let ib = self.ibox(0);
1251 match &ty.kind {
1252 ast::TyKind::Slice(ty) => {
1253 self.word("[");
1254 self.print_type(ty);
1255 self.word("]");
1256 }
1257 ast::TyKind::Ptr(mt) => {
1258 self.word("*");
1259 self.print_mt(mt, true);
1260 }
1261 ast::TyKind::Ref(lifetime, mt) => {
1262 self.word("&");
1263 self.print_opt_lifetime(lifetime);
1264 self.print_mt(mt, false);
1265 }
1266 ast::TyKind::PinnedRef(lifetime, mt) => {
1267 self.word("&");
1268 self.print_opt_lifetime(lifetime);
1269 self.word("pin ");
1270 self.print_mt(mt, true);
1271 }
1272 ast::TyKind::Never => {
1273 self.word("!");
1274 }
1275 ast::TyKind::Tup(elts) => {
1276 self.popen();
1277 self.commasep(Inconsistent, elts, |s, ty| s.print_type(ty));
1278 if elts.len() == 1 {
1279 self.word(",");
1280 }
1281 self.pclose();
1282 }
1283 ast::TyKind::Paren(typ) => {
1284 self.popen();
1285 self.print_type(typ);
1286 self.pclose();
1287 }
1288 ast::TyKind::FnPtr(f) => {
1289 self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
1290 }
1291 ast::TyKind::UnsafeBinder(f) => {
1292 let ib = self.ibox(INDENT_UNIT);
1293 self.word("unsafe");
1294 self.print_generic_params(&f.generic_params);
1295 self.nbsp();
1296 self.print_type(&f.inner_ty);
1297 self.end(ib);
1298 }
1299 ast::TyKind::Path(None, path) => {
1300 self.print_path(path, false, 0);
1301 }
1302 ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false),
1303 ast::TyKind::TraitObject(bounds, syntax) => {
1304 match syntax {
1305 ast::TraitObjectSyntax::Dyn => self.word_nbsp("dyn"),
1306 ast::TraitObjectSyntax::None => {}
1307 }
1308 self.print_type_bounds(bounds);
1309 }
1310 ast::TyKind::ImplTrait(_, bounds) => {
1311 self.word_nbsp("impl");
1312 self.print_type_bounds(bounds);
1313 }
1314 ast::TyKind::Array(ty, length) => {
1315 self.word("[");
1316 self.print_type(ty);
1317 self.word("; ");
1318 self.print_expr(&length.value, FixupContext::default());
1319 self.word("]");
1320 }
1321 ast::TyKind::Typeof(e) => {
1322 self.word("typeof(");
1323 self.print_expr(&e.value, FixupContext::default());
1324 self.word(")");
1325 }
1326 ast::TyKind::Infer => {
1327 self.word("_");
1328 }
1329 ast::TyKind::Err(_) => {
1330 self.popen();
1331 self.word("/*ERROR*/");
1332 self.pclose();
1333 }
1334 ast::TyKind::Dummy => {
1335 self.popen();
1336 self.word("/*DUMMY*/");
1337 self.pclose();
1338 }
1339 ast::TyKind::ImplicitSelf => {
1340 self.word("Self");
1341 }
1342 ast::TyKind::MacCall(m) => {
1343 self.print_mac(m);
1344 }
1345 ast::TyKind::CVarArgs => {
1346 self.word("...");
1347 }
1348 ast::TyKind::Pat(ty, pat) => {
1349 self.print_type(ty);
1350 self.word(" is ");
1351 self.print_ty_pat(pat);
1352 }
1353 }
1354 self.end(ib);
1355 }
1356
1357 fn print_trait_ref(&mut self, t: &ast::TraitRef) {
1358 self.print_path(&t.path, false, 0)
1359 }
1360
1361 fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1362 if !generic_params.is_empty() {
1363 self.word("for");
1364 self.print_generic_params(generic_params);
1365 self.nbsp();
1366 }
1367 }
1368
1369 fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
1370 self.print_formal_generic_params(&t.bound_generic_params);
1371
1372 let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1373 match constness {
1374 ast::BoundConstness::Never => {}
1375 ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1376 self.word_space(constness.as_str());
1377 }
1378 }
1379 match asyncness {
1380 ast::BoundAsyncness::Normal => {}
1381 ast::BoundAsyncness::Async(_) => {
1382 self.word_space(asyncness.as_str());
1383 }
1384 }
1385 match polarity {
1386 ast::BoundPolarity::Positive => {}
1387 ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1388 self.word(polarity.as_str());
1389 }
1390 }
1391
1392 self.print_trait_ref(&t.trait_ref)
1393 }
1394
1395 fn print_stmt(&mut self, st: &ast::Stmt) {
1396 self.maybe_print_comment(st.span.lo());
1397 match &st.kind {
1398 ast::StmtKind::Let(loc) => {
1399 self.print_outer_attributes(&loc.attrs);
1400 self.space_if_not_bol();
1401 let ib1 = self.ibox(INDENT_UNIT);
1402 if loc.super_.is_some() {
1403 self.word_nbsp("super");
1404 }
1405 self.word_nbsp("let");
1406
1407 let ib2 = self.ibox(INDENT_UNIT);
1408 self.print_local_decl(loc);
1409 self.end(ib2);
1410 if let Some((init, els)) = loc.kind.init_else_opt() {
1411 self.nbsp();
1412 self.word_space("=");
1413 self.print_expr_cond_paren(
1414 init,
1415 els.is_some() && classify::expr_trailing_brace(init).is_some(),
1416 FixupContext::default(),
1417 );
1418 if let Some(els) = els {
1419 let cb = self.cbox(INDENT_UNIT);
1420 let ib = self.ibox(INDENT_UNIT);
1421 self.word(" else ");
1422 self.print_block(els, cb, ib);
1423 }
1424 }
1425 self.word(";");
1426 self.end(ib1);
1427 }
1428 ast::StmtKind::Item(item) => self.print_item(item),
1429 ast::StmtKind::Expr(expr) => {
1430 self.space_if_not_bol();
1431 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1432 if classify::expr_requires_semi_to_be_stmt(expr) {
1433 self.word(";");
1434 }
1435 }
1436 ast::StmtKind::Semi(expr) => {
1437 self.space_if_not_bol();
1438 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1439 self.word(";");
1440 }
1441 ast::StmtKind::Empty => {
1442 self.space_if_not_bol();
1443 self.word(";");
1444 }
1445 ast::StmtKind::MacCall(mac) => {
1446 self.space_if_not_bol();
1447 self.print_outer_attributes(&mac.attrs);
1448 self.print_mac(&mac.mac);
1449 if mac.style == ast::MacStmtStyle::Semicolon {
1450 self.word(";");
1451 }
1452 }
1453 }
1454 self.maybe_print_trailing_comment(st.span, None)
1455 }
1456
1457 fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) {
1458 self.print_block_with_attrs(blk, &[], cb, ib)
1459 }
1460
1461 fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) {
1462 self.print_block_maybe_unclosed(blk, &[], None, ib)
1463 }
1464
1465 fn print_block_with_attrs(
1466 &mut self,
1467 blk: &ast::Block,
1468 attrs: &[ast::Attribute],
1469 cb: BoxMarker,
1470 ib: BoxMarker,
1471 ) {
1472 self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1473 }
1474
1475 fn print_block_maybe_unclosed(
1476 &mut self,
1477 blk: &ast::Block,
1478 attrs: &[ast::Attribute],
1479 cb: Option<BoxMarker>,
1480 ib: BoxMarker,
1481 ) {
1482 match blk.rules {
1483 BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1484 BlockCheckMode::Default => (),
1485 }
1486 self.maybe_print_comment(blk.span.lo());
1487 self.ann.pre(self, AnnNode::Block(blk));
1488 self.bopen(ib);
1489
1490 let has_attrs = self.print_inner_attributes(attrs);
1491
1492 for (i, st) in blk.stmts.iter().enumerate() {
1493 match &st.kind {
1494 ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
1495 self.maybe_print_comment(st.span.lo());
1496 self.space_if_not_bol();
1497 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1498 self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1499 }
1500 _ => self.print_stmt(st),
1501 }
1502 }
1503
1504 let no_space = !has_attrs && blk.stmts.is_empty();
1505 self.bclose_maybe_open(blk.span, no_space, cb);
1506 self.ann.post(self, AnnNode::Block(blk))
1507 }
1508
1509 fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, fixup: FixupContext) {
1535 self.word("let ");
1536 self.print_pat(pat);
1537 self.space();
1538 self.word_space("=");
1539 self.print_expr_cond_paren(
1540 expr,
1541 fixup.needs_par_as_let_scrutinee(expr),
1542 FixupContext::default(),
1543 );
1544 }
1545
1546 fn print_mac(&mut self, m: &ast::MacCall) {
1547 self.print_mac_common(
1548 Some(MacHeader::Path(&m.path)),
1549 true,
1550 None,
1551 m.args.delim,
1552 None,
1553 &m.args.tokens,
1554 true,
1555 m.span(),
1556 );
1557 }
1558
1559 fn print_inline_asm(&mut self, asm: &ast::InlineAsm) {
1560 enum AsmArg<'a> {
1561 Template(String),
1562 Operand(&'a InlineAsmOperand),
1563 ClobberAbi(Symbol),
1564 Options(InlineAsmOptions),
1565 }
1566
1567 let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
1568 args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
1569 for (abi, _) in &asm.clobber_abis {
1570 args.push(AsmArg::ClobberAbi(*abi));
1571 }
1572 if !asm.options.is_empty() {
1573 args.push(AsmArg::Options(asm.options));
1574 }
1575
1576 self.popen();
1577 self.commasep(Consistent, &args, |s, arg| match arg {
1578 AsmArg::Template(template) => s.print_string(template, ast::StrStyle::Cooked),
1579 AsmArg::Operand(op) => {
1580 let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r {
1581 InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked),
1582 InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
1583 };
1584 match op {
1585 InlineAsmOperand::In { reg, expr } => {
1586 s.word("in");
1587 s.popen();
1588 print_reg_or_class(s, reg);
1589 s.pclose();
1590 s.space();
1591 s.print_expr(expr, FixupContext::default());
1592 }
1593 InlineAsmOperand::Out { reg, late, expr } => {
1594 s.word(if *late { "lateout" } else { "out" });
1595 s.popen();
1596 print_reg_or_class(s, reg);
1597 s.pclose();
1598 s.space();
1599 match expr {
1600 Some(expr) => s.print_expr(expr, FixupContext::default()),
1601 None => s.word("_"),
1602 }
1603 }
1604 InlineAsmOperand::InOut { reg, late, expr } => {
1605 s.word(if *late { "inlateout" } else { "inout" });
1606 s.popen();
1607 print_reg_or_class(s, reg);
1608 s.pclose();
1609 s.space();
1610 s.print_expr(expr, FixupContext::default());
1611 }
1612 InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
1613 s.word(if *late { "inlateout" } else { "inout" });
1614 s.popen();
1615 print_reg_or_class(s, reg);
1616 s.pclose();
1617 s.space();
1618 s.print_expr(in_expr, FixupContext::default());
1619 s.space();
1620 s.word_space("=>");
1621 match out_expr {
1622 Some(out_expr) => s.print_expr(out_expr, FixupContext::default()),
1623 None => s.word("_"),
1624 }
1625 }
1626 InlineAsmOperand::Const { anon_const } => {
1627 s.word("const");
1628 s.space();
1629 s.print_expr(&anon_const.value, FixupContext::default());
1630 }
1631 InlineAsmOperand::Sym { sym } => {
1632 s.word("sym");
1633 s.space();
1634 if let Some(qself) = &sym.qself {
1635 s.print_qpath(&sym.path, qself, true);
1636 } else {
1637 s.print_path(&sym.path, true, 0);
1638 }
1639 }
1640 InlineAsmOperand::Label { block } => {
1641 let (cb, ib) = s.head("label");
1642 s.print_block(block, cb, ib);
1643 }
1644 }
1645 }
1646 AsmArg::ClobberAbi(abi) => {
1647 s.word("clobber_abi");
1648 s.popen();
1649 s.print_symbol(*abi, ast::StrStyle::Cooked);
1650 s.pclose();
1651 }
1652 AsmArg::Options(opts) => {
1653 s.word("options");
1654 s.popen();
1655 s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
1656 s.word(opt);
1657 });
1658 s.pclose();
1659 }
1660 });
1661 self.pclose();
1662 }
1663
1664 fn print_local_decl(&mut self, loc: &ast::Local) {
1665 self.print_pat(&loc.pat);
1666 if let Some(ty) = &loc.ty {
1667 self.word_space(":");
1668 self.print_type(ty);
1669 }
1670 }
1671
1672 fn print_name(&mut self, name: Symbol) {
1673 self.word(name.to_string());
1674 self.ann.post(self, AnnNode::Name(&name))
1675 }
1676
1677 fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
1678 self.word("<");
1679 self.print_type(&qself.ty);
1680 if qself.position > 0 {
1681 self.space();
1682 self.word_space("as");
1683 let depth = path.segments.len() - qself.position;
1684 self.print_path(path, false, depth);
1685 }
1686 self.word(">");
1687 for item_segment in &path.segments[qself.position..] {
1688 self.word("::");
1689 self.print_ident(item_segment.ident);
1690 if let Some(args) = &item_segment.args {
1691 self.print_generic_args(args, colons_before_params)
1692 }
1693 }
1694 }
1695
1696 fn print_pat(&mut self, pat: &ast::Pat) {
1697 self.maybe_print_comment(pat.span.lo());
1698 self.ann.pre(self, AnnNode::Pat(pat));
1699 match &pat.kind {
1701 PatKind::Missing => unreachable!(),
1702 PatKind::Wild => self.word("_"),
1703 PatKind::Never => self.word("!"),
1704 PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1705 if mutbl.is_mut() {
1706 self.word_nbsp("mut");
1707 }
1708 if let ByRef::Yes(rmutbl) = by_ref {
1709 self.word_nbsp("ref");
1710 if rmutbl.is_mut() {
1711 self.word_nbsp("mut");
1712 }
1713 }
1714 self.print_ident(*ident);
1715 if let Some(p) = sub {
1716 self.space();
1717 self.word_space("@");
1718 self.print_pat(p);
1719 }
1720 }
1721 PatKind::TupleStruct(qself, path, elts) => {
1722 if let Some(qself) = qself {
1723 self.print_qpath(path, qself, true);
1724 } else {
1725 self.print_path(path, true, 0);
1726 }
1727 self.popen();
1728 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1729 self.pclose();
1730 }
1731 PatKind::Or(pats) => {
1732 self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p));
1733 }
1734 PatKind::Path(None, path) => {
1735 self.print_path(path, true, 0);
1736 }
1737 PatKind::Path(Some(qself), path) => {
1738 self.print_qpath(path, qself, false);
1739 }
1740 PatKind::Struct(qself, path, fields, etc) => {
1741 if let Some(qself) = qself {
1742 self.print_qpath(path, qself, true);
1743 } else {
1744 self.print_path(path, true, 0);
1745 }
1746 self.nbsp();
1747 self.word("{");
1748 let empty = fields.is_empty() && *etc == ast::PatFieldsRest::None;
1749 if !empty {
1750 self.space();
1751 }
1752 self.commasep_cmnt(
1753 Consistent,
1754 fields,
1755 |s, f| {
1756 let cb = s.cbox(INDENT_UNIT);
1757 if !f.is_shorthand {
1758 s.print_ident(f.ident);
1759 s.word_nbsp(":");
1760 }
1761 s.print_pat(&f.pat);
1762 s.end(cb);
1763 },
1764 |f| f.pat.span,
1765 );
1766 if let ast::PatFieldsRest::Rest | ast::PatFieldsRest::Recovered(_) = etc {
1767 if !fields.is_empty() {
1768 self.word_space(",");
1769 }
1770 self.word("..");
1771 if let ast::PatFieldsRest::Recovered(_) = etc {
1772 self.word("/* recovered parse error */");
1773 }
1774 }
1775 if !empty {
1776 self.space();
1777 }
1778 self.word("}");
1779 }
1780 PatKind::Tuple(elts) => {
1781 self.popen();
1782 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1783 if elts.len() == 1 {
1784 self.word(",");
1785 }
1786 self.pclose();
1787 }
1788 PatKind::Box(inner) => {
1789 self.word("box ");
1790 self.print_pat(inner);
1791 }
1792 PatKind::Deref(inner) => {
1793 self.word("deref!");
1794 self.popen();
1795 self.print_pat(inner);
1796 self.pclose();
1797 }
1798 PatKind::Ref(inner, mutbl) => {
1799 self.word("&");
1800 if mutbl.is_mut() {
1801 self.word("mut ");
1802 }
1803 if let PatKind::Ident(ast::BindingMode::MUT, ..) = inner.kind {
1804 self.popen();
1805 self.print_pat(inner);
1806 self.pclose();
1807 } else {
1808 self.print_pat(inner);
1809 }
1810 }
1811 PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
1812 PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
1813 if let Some(e) = begin {
1814 self.print_expr(e, FixupContext::default());
1815 }
1816 match end_kind {
1817 RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
1818 RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
1819 RangeEnd::Excluded => self.word(".."),
1820 }
1821 if let Some(e) = end {
1822 self.print_expr(e, FixupContext::default());
1823 }
1824 }
1825 PatKind::Guard(subpat, condition) => {
1826 self.popen();
1827 self.print_pat(subpat);
1828 self.space();
1829 self.word_space("if");
1830 self.print_expr(condition, FixupContext::default());
1831 self.pclose();
1832 }
1833 PatKind::Slice(elts) => {
1834 self.word("[");
1835 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1836 self.word("]");
1837 }
1838 PatKind::Rest => self.word(".."),
1839 PatKind::Paren(inner) => {
1840 self.popen();
1841 self.print_pat(inner);
1842 self.pclose();
1843 }
1844 PatKind::MacCall(m) => self.print_mac(m),
1845 PatKind::Err(_) => {
1846 self.popen();
1847 self.word("/*ERROR*/");
1848 self.pclose();
1849 }
1850 }
1851 self.ann.post(self, AnnNode::Pat(pat))
1852 }
1853
1854 fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
1855 match &explicit_self.node {
1856 SelfKind::Value(m) => {
1857 self.print_mutability(*m, false);
1858 self.word("self")
1859 }
1860 SelfKind::Region(lt, m) => {
1861 self.word("&");
1862 self.print_opt_lifetime(lt);
1863 self.print_mutability(*m, false);
1864 self.word("self")
1865 }
1866 SelfKind::Pinned(lt, m) => {
1867 self.word("&");
1868 self.print_opt_lifetime(lt);
1869 self.word("pin ");
1870 self.print_mutability(*m, true);
1871 self.word("self")
1872 }
1873 SelfKind::Explicit(typ, m) => {
1874 self.print_mutability(*m, false);
1875 self.word("self");
1876 self.word_space(":");
1877 self.print_type(typ)
1878 }
1879 }
1880 }
1881
1882 fn print_coroutine_kind(&mut self, coroutine_kind: ast::CoroutineKind) {
1883 match coroutine_kind {
1884 ast::CoroutineKind::Gen { .. } => {
1885 self.word_nbsp("gen");
1886 }
1887 ast::CoroutineKind::Async { .. } => {
1888 self.word_nbsp("async");
1889 }
1890 ast::CoroutineKind::AsyncGen { .. } => {
1891 self.word_nbsp("async");
1892 self.word_nbsp("gen");
1893 }
1894 }
1895 }
1896
1897 pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
1898 let mut first = true;
1899 for bound in bounds {
1900 if first {
1901 first = false;
1902 } else {
1903 self.nbsp();
1904 self.word_space("+");
1905 }
1906
1907 match bound {
1908 GenericBound::Trait(tref) => {
1909 self.print_poly_trait_ref(tref);
1910 }
1911 GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1912 GenericBound::Use(args, _) => {
1913 self.word("use");
1914 self.word("<");
1915 self.commasep(Inconsistent, args, |s, arg| match arg {
1916 ast::PreciseCapturingArg::Arg(p, _) => s.print_path(p, false, 0),
1917 ast::PreciseCapturingArg::Lifetime(lt) => s.print_lifetime(*lt),
1918 });
1919 self.word(">")
1920 }
1921 }
1922 }
1923 }
1924
1925 fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
1926 self.print_name(lifetime.ident.name)
1927 }
1928
1929 fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
1930 for (i, bound) in bounds.iter().enumerate() {
1931 if i != 0 {
1932 self.word(" + ");
1933 }
1934 match bound {
1935 ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1936 _ => {
1937 panic!("expected a lifetime bound, found a trait bound")
1938 }
1939 }
1940 }
1941 }
1942
1943 fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1944 if generic_params.is_empty() {
1945 return;
1946 }
1947
1948 self.word("<");
1949
1950 self.commasep(Inconsistent, generic_params, |s, param| {
1951 s.print_outer_attributes_inline(¶m.attrs);
1952
1953 match ¶m.kind {
1954 ast::GenericParamKind::Lifetime => {
1955 let lt = ast::Lifetime { id: param.id, ident: param.ident };
1956 s.print_lifetime(lt);
1957 if !param.bounds.is_empty() {
1958 s.word_nbsp(":");
1959 s.print_lifetime_bounds(¶m.bounds)
1960 }
1961 }
1962 ast::GenericParamKind::Type { default } => {
1963 s.print_ident(param.ident);
1964 if !param.bounds.is_empty() {
1965 s.word_nbsp(":");
1966 s.print_type_bounds(¶m.bounds);
1967 }
1968 if let Some(default) = default {
1969 s.space();
1970 s.word_space("=");
1971 s.print_type(default)
1972 }
1973 }
1974 ast::GenericParamKind::Const { ty, default, .. } => {
1975 s.word_space("const");
1976 s.print_ident(param.ident);
1977 s.space();
1978 s.word_space(":");
1979 s.print_type(ty);
1980 if !param.bounds.is_empty() {
1981 s.word_nbsp(":");
1982 s.print_type_bounds(¶m.bounds);
1983 }
1984 if let Some(default) = default {
1985 s.space();
1986 s.word_space("=");
1987 s.print_expr(&default.value, FixupContext::default());
1988 }
1989 }
1990 }
1991 });
1992
1993 self.word(">");
1994 }
1995
1996 pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
1997 match mutbl {
1998 ast::Mutability::Mut => self.word_nbsp("mut"),
1999 ast::Mutability::Not => {
2000 if print_const {
2001 self.word_nbsp("const");
2002 }
2003 }
2004 }
2005 }
2006
2007 fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2008 self.print_mutability(mt.mutbl, print_const);
2009 self.print_type(&mt.ty)
2010 }
2011
2012 fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2013 let ib = self.ibox(INDENT_UNIT);
2014
2015 self.print_outer_attributes_inline(&input.attrs);
2016
2017 match input.ty.kind {
2018 ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2019 _ => {
2020 if let Some(eself) = input.to_self() {
2021 self.print_explicit_self(&eself);
2022 } else {
2023 if !matches!(input.pat.kind, PatKind::Missing) {
2024 self.print_pat(&input.pat);
2025 self.word(":");
2026 self.space();
2027 }
2028 self.print_type(&input.ty);
2029 }
2030 }
2031 }
2032 self.end(ib);
2033 }
2034
2035 fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2036 if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2037 self.space_if_not_bol();
2038 let ib = self.ibox(INDENT_UNIT);
2039 self.word_space("->");
2040 self.print_type(ty);
2041 self.end(ib);
2042 self.maybe_print_comment(ty.span.lo());
2043 }
2044 }
2045
2046 fn print_ty_fn(
2047 &mut self,
2048 ext: ast::Extern,
2049 safety: ast::Safety,
2050 decl: &ast::FnDecl,
2051 name: Option<Ident>,
2052 generic_params: &[ast::GenericParam],
2053 ) {
2054 let ib = self.ibox(INDENT_UNIT);
2055 self.print_formal_generic_params(generic_params);
2056 let generics = ast::Generics::default();
2057 let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
2058 self.print_fn(decl, header, name, &generics);
2059 self.end(ib);
2060 }
2061
2062 fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2063 self.print_constness(header.constness);
2064 header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
2065 self.print_safety(header.safety);
2066
2067 match header.ext {
2068 ast::Extern::None => {}
2069 ast::Extern::Implicit(_) => {
2070 self.word_nbsp("extern");
2071 }
2072 ast::Extern::Explicit(abi, _) => {
2073 self.word_nbsp("extern");
2074 self.print_token_literal(abi.as_token_lit(), abi.span);
2075 self.nbsp();
2076 }
2077 }
2078
2079 self.word("fn")
2080 }
2081
2082 fn print_safety(&mut self, s: ast::Safety) {
2083 match s {
2084 ast::Safety::Default => {}
2085 ast::Safety::Safe(_) => self.word_nbsp("safe"),
2086 ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
2087 }
2088 }
2089
2090 fn print_constness(&mut self, s: ast::Const) {
2091 match s {
2092 ast::Const::No => {}
2093 ast::Const::Yes(_) => self.word_nbsp("const"),
2094 }
2095 }
2096
2097 fn print_is_auto(&mut self, s: ast::IsAuto) {
2098 match s {
2099 ast::IsAuto::Yes => self.word_nbsp("auto"),
2100 ast::IsAuto::No => {}
2101 }
2102 }
2103
2104 fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
2105 self.print_token_literal(lit.as_token_lit(), lit.span)
2106 }
2107
2108 fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
2109 self.maybe_print_comment(span.lo());
2110 self.word(token_lit.to_string())
2111 }
2112
2113 fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
2114 self.print_string(sym.as_str(), style);
2115 }
2116
2117 fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
2118 self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
2119 }
2120
2121 fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
2122 self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
2123 }
2124
2125 fn print_attribute(&mut self, attr: &ast::Attribute) {
2126 self.print_attribute_inline(attr, false);
2127 }
2128
2129 fn print_meta_list_item(&mut self, item: &ast::MetaItemInner) {
2130 match item {
2131 ast::MetaItemInner::MetaItem(mi) => self.print_meta_item(mi),
2132 ast::MetaItemInner::Lit(lit) => self.print_meta_item_lit(lit),
2133 }
2134 }
2135
2136 fn print_meta_item(&mut self, item: &ast::MetaItem) {
2137 let ib = self.ibox(INDENT_UNIT);
2138 match &item.kind {
2139 ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
2140 ast::MetaItemKind::NameValue(value) => {
2141 self.print_path(&item.path, false, 0);
2142 self.space();
2143 self.word_space("=");
2144 self.print_meta_item_lit(value);
2145 }
2146 ast::MetaItemKind::List(items) => {
2147 self.print_path(&item.path, false, 0);
2148 self.popen();
2149 self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
2150 self.pclose();
2151 }
2152 }
2153 self.end(ib);
2154 }
2155
2156 pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
2157 Self::to_string(|s| s.print_type_bounds(bounds))
2158 }
2159
2160 pub(crate) fn where_bound_predicate_to_string(
2161 &self,
2162 where_bound_predicate: &ast::WhereBoundPredicate,
2163 ) -> String {
2164 Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
2165 }
2166
2167 pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
2168 Self::to_string(|s| {
2169 s.print_tt(tt, false);
2170 })
2171 }
2172
2173 pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
2174 Self::to_string(|s| s.print_path_segment(p, false))
2175 }
2176
2177 pub(crate) fn meta_list_item_to_string(&self, li: &ast::MetaItemInner) -> String {
2178 Self::to_string(|s| s.print_meta_list_item(li))
2179 }
2180
2181 pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
2182 Self::to_string(|s| s.print_attribute(attr))
2183 }
2184}