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