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::NotNull => self.word("!null"),
1236 rustc_ast::TyPatKind::Or(variants) => {
1237 let mut first = true;
1238 for pat in variants {
1239 if first {
1240 first = false
1241 } else {
1242 self.word(" | ");
1243 }
1244 self.print_ty_pat(pat);
1245 }
1246 }
1247 rustc_ast::TyPatKind::Err(_) => {
1248 self.popen();
1249 self.word("/*ERROR*/");
1250 self.pclose();
1251 }
1252 }
1253 }
1254
1255 pub fn print_type(&mut self, ty: &ast::Ty) {
1256 self.maybe_print_comment(ty.span.lo());
1257 let ib = self.ibox(0);
1258 match &ty.kind {
1259 ast::TyKind::Slice(ty) => {
1260 self.word("[");
1261 self.print_type(ty);
1262 self.word("]");
1263 }
1264 ast::TyKind::Ptr(mt) => {
1265 self.word("*");
1266 self.print_mt(mt, true);
1267 }
1268 ast::TyKind::Ref(lifetime, mt) => {
1269 self.word("&");
1270 self.print_opt_lifetime(lifetime);
1271 self.print_mt(mt, false);
1272 }
1273 ast::TyKind::PinnedRef(lifetime, mt) => {
1274 self.word("&");
1275 self.print_opt_lifetime(lifetime);
1276 self.word("pin ");
1277 self.print_mt(mt, true);
1278 }
1279 ast::TyKind::Never => {
1280 self.word("!");
1281 }
1282 ast::TyKind::Tup(elts) => {
1283 self.popen();
1284 self.commasep(Inconsistent, elts, |s, ty| s.print_type(ty));
1285 if elts.len() == 1 {
1286 self.word(",");
1287 }
1288 self.pclose();
1289 }
1290 ast::TyKind::Paren(typ) => {
1291 self.popen();
1292 self.print_type(typ);
1293 self.pclose();
1294 }
1295 ast::TyKind::FnPtr(f) => {
1296 self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
1297 }
1298 ast::TyKind::UnsafeBinder(f) => {
1299 let ib = self.ibox(INDENT_UNIT);
1300 self.word("unsafe");
1301 self.print_generic_params(&f.generic_params);
1302 self.nbsp();
1303 self.print_type(&f.inner_ty);
1304 self.end(ib);
1305 }
1306 ast::TyKind::Path(None, path) => {
1307 self.print_path(path, false, 0);
1308 }
1309 ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false),
1310 ast::TyKind::TraitObject(bounds, syntax) => {
1311 match syntax {
1312 ast::TraitObjectSyntax::Dyn => self.word_nbsp("dyn"),
1313 ast::TraitObjectSyntax::None => {}
1314 }
1315 self.print_type_bounds(bounds);
1316 }
1317 ast::TyKind::ImplTrait(_, bounds) => {
1318 self.word_nbsp("impl");
1319 self.print_type_bounds(bounds);
1320 }
1321 ast::TyKind::Array(ty, length) => {
1322 self.word("[");
1323 self.print_type(ty);
1324 self.word("; ");
1325 self.print_expr(&length.value, FixupContext::default());
1326 self.word("]");
1327 }
1328 ast::TyKind::Typeof(e) => {
1329 self.word("typeof(");
1330 self.print_expr(&e.value, FixupContext::default());
1331 self.word(")");
1332 }
1333 ast::TyKind::Infer => {
1334 self.word("_");
1335 }
1336 ast::TyKind::Err(_) => {
1337 self.popen();
1338 self.word("/*ERROR*/");
1339 self.pclose();
1340 }
1341 ast::TyKind::Dummy => {
1342 self.popen();
1343 self.word("/*DUMMY*/");
1344 self.pclose();
1345 }
1346 ast::TyKind::ImplicitSelf => {
1347 self.word("Self");
1348 }
1349 ast::TyKind::MacCall(m) => {
1350 self.print_mac(m);
1351 }
1352 ast::TyKind::CVarArgs => {
1353 self.word("...");
1354 }
1355 ast::TyKind::Pat(ty, pat) => {
1356 self.print_type(ty);
1357 self.word(" is ");
1358 self.print_ty_pat(pat);
1359 }
1360 }
1361 self.end(ib);
1362 }
1363
1364 fn print_trait_ref(&mut self, t: &ast::TraitRef) {
1365 self.print_path(&t.path, false, 0)
1366 }
1367
1368 fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1369 if !generic_params.is_empty() {
1370 self.word("for");
1371 self.print_generic_params(generic_params);
1372 self.nbsp();
1373 }
1374 }
1375
1376 fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
1377 self.print_formal_generic_params(&t.bound_generic_params);
1378
1379 let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1380 match constness {
1381 ast::BoundConstness::Never => {}
1382 ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1383 self.word_space(constness.as_str());
1384 }
1385 }
1386 match asyncness {
1387 ast::BoundAsyncness::Normal => {}
1388 ast::BoundAsyncness::Async(_) => {
1389 self.word_space(asyncness.as_str());
1390 }
1391 }
1392 match polarity {
1393 ast::BoundPolarity::Positive => {}
1394 ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1395 self.word(polarity.as_str());
1396 }
1397 }
1398
1399 self.print_trait_ref(&t.trait_ref)
1400 }
1401
1402 fn print_stmt(&mut self, st: &ast::Stmt) {
1403 self.maybe_print_comment(st.span.lo());
1404 match &st.kind {
1405 ast::StmtKind::Let(loc) => {
1406 self.print_outer_attributes(&loc.attrs);
1407 self.space_if_not_bol();
1408 let ib1 = self.ibox(INDENT_UNIT);
1409 if loc.super_.is_some() {
1410 self.word_nbsp("super");
1411 }
1412 self.word_nbsp("let");
1413
1414 let ib2 = self.ibox(INDENT_UNIT);
1415 self.print_local_decl(loc);
1416 self.end(ib2);
1417 if let Some((init, els)) = loc.kind.init_else_opt() {
1418 self.nbsp();
1419 self.word_space("=");
1420 self.print_expr_cond_paren(
1421 init,
1422 els.is_some() && classify::expr_trailing_brace(init).is_some(),
1423 FixupContext::default(),
1424 );
1425 if let Some(els) = els {
1426 let cb = self.cbox(INDENT_UNIT);
1427 let ib = self.ibox(INDENT_UNIT);
1428 self.word(" else ");
1429 self.print_block(els, cb, ib);
1430 }
1431 }
1432 self.word(";");
1433 self.end(ib1);
1434 }
1435 ast::StmtKind::Item(item) => self.print_item(item),
1436 ast::StmtKind::Expr(expr) => {
1437 self.space_if_not_bol();
1438 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1439 if classify::expr_requires_semi_to_be_stmt(expr) {
1440 self.word(";");
1441 }
1442 }
1443 ast::StmtKind::Semi(expr) => {
1444 self.space_if_not_bol();
1445 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1446 self.word(";");
1447 }
1448 ast::StmtKind::Empty => {
1449 self.space_if_not_bol();
1450 self.word(";");
1451 }
1452 ast::StmtKind::MacCall(mac) => {
1453 self.space_if_not_bol();
1454 self.print_outer_attributes(&mac.attrs);
1455 self.print_mac(&mac.mac);
1456 if mac.style == ast::MacStmtStyle::Semicolon {
1457 self.word(";");
1458 }
1459 }
1460 }
1461 self.maybe_print_trailing_comment(st.span, None)
1462 }
1463
1464 fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) {
1465 self.print_block_with_attrs(blk, &[], cb, ib)
1466 }
1467
1468 fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) {
1469 self.print_block_maybe_unclosed(blk, &[], None, ib)
1470 }
1471
1472 fn print_block_with_attrs(
1473 &mut self,
1474 blk: &ast::Block,
1475 attrs: &[ast::Attribute],
1476 cb: BoxMarker,
1477 ib: BoxMarker,
1478 ) {
1479 self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1480 }
1481
1482 fn print_block_maybe_unclosed(
1483 &mut self,
1484 blk: &ast::Block,
1485 attrs: &[ast::Attribute],
1486 cb: Option<BoxMarker>,
1487 ib: BoxMarker,
1488 ) {
1489 match blk.rules {
1490 BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1491 BlockCheckMode::Default => (),
1492 }
1493 self.maybe_print_comment(blk.span.lo());
1494 self.ann.pre(self, AnnNode::Block(blk));
1495 self.bopen(ib);
1496
1497 let has_attrs = self.print_inner_attributes(attrs);
1498
1499 for (i, st) in blk.stmts.iter().enumerate() {
1500 match &st.kind {
1501 ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
1502 self.maybe_print_comment(st.span.lo());
1503 self.space_if_not_bol();
1504 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1505 self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1506 }
1507 _ => self.print_stmt(st),
1508 }
1509 }
1510
1511 let no_space = !has_attrs && blk.stmts.is_empty();
1512 self.bclose_maybe_open(blk.span, no_space, cb);
1513 self.ann.post(self, AnnNode::Block(blk))
1514 }
1515
1516 fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, fixup: FixupContext) {
1542 self.word("let ");
1543 self.print_pat(pat);
1544 self.space();
1545 self.word_space("=");
1546 self.print_expr_cond_paren(
1547 expr,
1548 fixup.needs_par_as_let_scrutinee(expr),
1549 FixupContext::default(),
1550 );
1551 }
1552
1553 fn print_mac(&mut self, m: &ast::MacCall) {
1554 self.print_mac_common(
1555 Some(MacHeader::Path(&m.path)),
1556 true,
1557 None,
1558 m.args.delim,
1559 None,
1560 &m.args.tokens,
1561 true,
1562 m.span(),
1563 );
1564 }
1565
1566 fn print_inline_asm(&mut self, asm: &ast::InlineAsm) {
1567 enum AsmArg<'a> {
1568 Template(String),
1569 Operand(&'a InlineAsmOperand),
1570 ClobberAbi(Symbol),
1571 Options(InlineAsmOptions),
1572 }
1573
1574 let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
1575 args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
1576 for (abi, _) in &asm.clobber_abis {
1577 args.push(AsmArg::ClobberAbi(*abi));
1578 }
1579 if !asm.options.is_empty() {
1580 args.push(AsmArg::Options(asm.options));
1581 }
1582
1583 self.popen();
1584 self.commasep(Consistent, &args, |s, arg| match arg {
1585 AsmArg::Template(template) => s.print_string(template, ast::StrStyle::Cooked),
1586 AsmArg::Operand(op) => {
1587 let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r {
1588 InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked),
1589 InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
1590 };
1591 match op {
1592 InlineAsmOperand::In { reg, expr } => {
1593 s.word("in");
1594 s.popen();
1595 print_reg_or_class(s, reg);
1596 s.pclose();
1597 s.space();
1598 s.print_expr(expr, FixupContext::default());
1599 }
1600 InlineAsmOperand::Out { reg, late, expr } => {
1601 s.word(if *late { "lateout" } else { "out" });
1602 s.popen();
1603 print_reg_or_class(s, reg);
1604 s.pclose();
1605 s.space();
1606 match expr {
1607 Some(expr) => s.print_expr(expr, FixupContext::default()),
1608 None => s.word("_"),
1609 }
1610 }
1611 InlineAsmOperand::InOut { reg, late, expr } => {
1612 s.word(if *late { "inlateout" } else { "inout" });
1613 s.popen();
1614 print_reg_or_class(s, reg);
1615 s.pclose();
1616 s.space();
1617 s.print_expr(expr, FixupContext::default());
1618 }
1619 InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
1620 s.word(if *late { "inlateout" } else { "inout" });
1621 s.popen();
1622 print_reg_or_class(s, reg);
1623 s.pclose();
1624 s.space();
1625 s.print_expr(in_expr, FixupContext::default());
1626 s.space();
1627 s.word_space("=>");
1628 match out_expr {
1629 Some(out_expr) => s.print_expr(out_expr, FixupContext::default()),
1630 None => s.word("_"),
1631 }
1632 }
1633 InlineAsmOperand::Const { anon_const } => {
1634 s.word("const");
1635 s.space();
1636 s.print_expr(&anon_const.value, FixupContext::default());
1637 }
1638 InlineAsmOperand::Sym { sym } => {
1639 s.word("sym");
1640 s.space();
1641 if let Some(qself) = &sym.qself {
1642 s.print_qpath(&sym.path, qself, true);
1643 } else {
1644 s.print_path(&sym.path, true, 0);
1645 }
1646 }
1647 InlineAsmOperand::Label { block } => {
1648 let (cb, ib) = s.head("label");
1649 s.print_block(block, cb, ib);
1650 }
1651 }
1652 }
1653 AsmArg::ClobberAbi(abi) => {
1654 s.word("clobber_abi");
1655 s.popen();
1656 s.print_symbol(*abi, ast::StrStyle::Cooked);
1657 s.pclose();
1658 }
1659 AsmArg::Options(opts) => {
1660 s.word("options");
1661 s.popen();
1662 s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
1663 s.word(opt);
1664 });
1665 s.pclose();
1666 }
1667 });
1668 self.pclose();
1669 }
1670
1671 fn print_local_decl(&mut self, loc: &ast::Local) {
1672 self.print_pat(&loc.pat);
1673 if let Some(ty) = &loc.ty {
1674 self.word_space(":");
1675 self.print_type(ty);
1676 }
1677 }
1678
1679 fn print_name(&mut self, name: Symbol) {
1680 self.word(name.to_string());
1681 self.ann.post(self, AnnNode::Name(&name))
1682 }
1683
1684 fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
1685 self.word("<");
1686 self.print_type(&qself.ty);
1687 if qself.position > 0 {
1688 self.space();
1689 self.word_space("as");
1690 let depth = path.segments.len() - qself.position;
1691 self.print_path(path, false, depth);
1692 }
1693 self.word(">");
1694 for item_segment in &path.segments[qself.position..] {
1695 self.word("::");
1696 self.print_ident(item_segment.ident);
1697 if let Some(args) = &item_segment.args {
1698 self.print_generic_args(args, colons_before_params)
1699 }
1700 }
1701 }
1702
1703 fn print_pat(&mut self, pat: &ast::Pat) {
1704 self.maybe_print_comment(pat.span.lo());
1705 self.ann.pre(self, AnnNode::Pat(pat));
1706 match &pat.kind {
1708 PatKind::Missing => unreachable!(),
1709 PatKind::Wild => self.word("_"),
1710 PatKind::Never => self.word("!"),
1711 PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1712 if mutbl.is_mut() {
1713 self.word_nbsp("mut");
1714 }
1715 if let ByRef::Yes(rmutbl) = by_ref {
1716 self.word_nbsp("ref");
1717 if rmutbl.is_mut() {
1718 self.word_nbsp("mut");
1719 }
1720 }
1721 self.print_ident(*ident);
1722 if let Some(p) = sub {
1723 self.space();
1724 self.word_space("@");
1725 self.print_pat(p);
1726 }
1727 }
1728 PatKind::TupleStruct(qself, path, elts) => {
1729 if let Some(qself) = qself {
1730 self.print_qpath(path, qself, true);
1731 } else {
1732 self.print_path(path, true, 0);
1733 }
1734 self.popen();
1735 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1736 self.pclose();
1737 }
1738 PatKind::Or(pats) => {
1739 self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p));
1740 }
1741 PatKind::Path(None, path) => {
1742 self.print_path(path, true, 0);
1743 }
1744 PatKind::Path(Some(qself), path) => {
1745 self.print_qpath(path, qself, false);
1746 }
1747 PatKind::Struct(qself, path, fields, etc) => {
1748 if let Some(qself) = qself {
1749 self.print_qpath(path, qself, true);
1750 } else {
1751 self.print_path(path, true, 0);
1752 }
1753 self.nbsp();
1754 self.word("{");
1755 let empty = fields.is_empty() && *etc == ast::PatFieldsRest::None;
1756 if !empty {
1757 self.space();
1758 }
1759 self.commasep_cmnt(
1760 Consistent,
1761 fields,
1762 |s, f| {
1763 let cb = s.cbox(INDENT_UNIT);
1764 if !f.is_shorthand {
1765 s.print_ident(f.ident);
1766 s.word_nbsp(":");
1767 }
1768 s.print_pat(&f.pat);
1769 s.end(cb);
1770 },
1771 |f| f.pat.span,
1772 );
1773 if let ast::PatFieldsRest::Rest(_) | ast::PatFieldsRest::Recovered(_) = etc {
1774 if !fields.is_empty() {
1775 self.word_space(",");
1776 }
1777 self.word("..");
1778 if let ast::PatFieldsRest::Recovered(_) = etc {
1779 self.word("/* recovered parse error */");
1780 }
1781 }
1782 if !empty {
1783 self.space();
1784 }
1785 self.word("}");
1786 }
1787 PatKind::Tuple(elts) => {
1788 self.popen();
1789 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1790 if elts.len() == 1 {
1791 self.word(",");
1792 }
1793 self.pclose();
1794 }
1795 PatKind::Box(inner) => {
1796 self.word("box ");
1797 self.print_pat(inner);
1798 }
1799 PatKind::Deref(inner) => {
1800 self.word("deref!");
1801 self.popen();
1802 self.print_pat(inner);
1803 self.pclose();
1804 }
1805 PatKind::Ref(inner, mutbl) => {
1806 self.word("&");
1807 if mutbl.is_mut() {
1808 self.word("mut ");
1809 }
1810 if let PatKind::Ident(ast::BindingMode::MUT, ..) = inner.kind {
1811 self.popen();
1812 self.print_pat(inner);
1813 self.pclose();
1814 } else {
1815 self.print_pat(inner);
1816 }
1817 }
1818 PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
1819 PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
1820 if let Some(e) = begin {
1821 self.print_expr(e, FixupContext::default());
1822 }
1823 match end_kind {
1824 RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
1825 RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
1826 RangeEnd::Excluded => self.word(".."),
1827 }
1828 if let Some(e) = end {
1829 self.print_expr(e, FixupContext::default());
1830 }
1831 }
1832 PatKind::Guard(subpat, condition) => {
1833 self.popen();
1834 self.print_pat(subpat);
1835 self.space();
1836 self.word_space("if");
1837 self.print_expr(condition, FixupContext::default());
1838 self.pclose();
1839 }
1840 PatKind::Slice(elts) => {
1841 self.word("[");
1842 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1843 self.word("]");
1844 }
1845 PatKind::Rest => self.word(".."),
1846 PatKind::Paren(inner) => {
1847 self.popen();
1848 self.print_pat(inner);
1849 self.pclose();
1850 }
1851 PatKind::MacCall(m) => self.print_mac(m),
1852 PatKind::Err(_) => {
1853 self.popen();
1854 self.word("/*ERROR*/");
1855 self.pclose();
1856 }
1857 }
1858 self.ann.post(self, AnnNode::Pat(pat))
1859 }
1860
1861 fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
1862 match &explicit_self.node {
1863 SelfKind::Value(m) => {
1864 self.print_mutability(*m, false);
1865 self.word("self")
1866 }
1867 SelfKind::Region(lt, m) => {
1868 self.word("&");
1869 self.print_opt_lifetime(lt);
1870 self.print_mutability(*m, false);
1871 self.word("self")
1872 }
1873 SelfKind::Pinned(lt, m) => {
1874 self.word("&");
1875 self.print_opt_lifetime(lt);
1876 self.word("pin ");
1877 self.print_mutability(*m, true);
1878 self.word("self")
1879 }
1880 SelfKind::Explicit(typ, m) => {
1881 self.print_mutability(*m, false);
1882 self.word("self");
1883 self.word_space(":");
1884 self.print_type(typ)
1885 }
1886 }
1887 }
1888
1889 fn print_coroutine_kind(&mut self, coroutine_kind: ast::CoroutineKind) {
1890 match coroutine_kind {
1891 ast::CoroutineKind::Gen { .. } => {
1892 self.word_nbsp("gen");
1893 }
1894 ast::CoroutineKind::Async { .. } => {
1895 self.word_nbsp("async");
1896 }
1897 ast::CoroutineKind::AsyncGen { .. } => {
1898 self.word_nbsp("async");
1899 self.word_nbsp("gen");
1900 }
1901 }
1902 }
1903
1904 pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
1905 let mut first = true;
1906 for bound in bounds {
1907 if first {
1908 first = false;
1909 } else {
1910 self.nbsp();
1911 self.word_space("+");
1912 }
1913
1914 match bound {
1915 GenericBound::Trait(tref) => {
1916 self.print_poly_trait_ref(tref);
1917 }
1918 GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1919 GenericBound::Use(args, _) => {
1920 self.word("use");
1921 self.word("<");
1922 self.commasep(Inconsistent, args, |s, arg| match arg {
1923 ast::PreciseCapturingArg::Arg(p, _) => s.print_path(p, false, 0),
1924 ast::PreciseCapturingArg::Lifetime(lt) => s.print_lifetime(*lt),
1925 });
1926 self.word(">")
1927 }
1928 }
1929 }
1930 }
1931
1932 fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
1933 self.print_name(lifetime.ident.name)
1934 }
1935
1936 fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
1937 for (i, bound) in bounds.iter().enumerate() {
1938 if i != 0 {
1939 self.word(" + ");
1940 }
1941 match bound {
1942 ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1943 _ => {
1944 panic!("expected a lifetime bound, found a trait bound")
1945 }
1946 }
1947 }
1948 }
1949
1950 fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1951 if generic_params.is_empty() {
1952 return;
1953 }
1954
1955 self.word("<");
1956
1957 self.commasep(Inconsistent, generic_params, |s, param| {
1958 s.print_outer_attributes_inline(¶m.attrs);
1959
1960 match ¶m.kind {
1961 ast::GenericParamKind::Lifetime => {
1962 let lt = ast::Lifetime { id: param.id, ident: param.ident };
1963 s.print_lifetime(lt);
1964 if !param.bounds.is_empty() {
1965 s.word_nbsp(":");
1966 s.print_lifetime_bounds(¶m.bounds)
1967 }
1968 }
1969 ast::GenericParamKind::Type { default } => {
1970 s.print_ident(param.ident);
1971 if !param.bounds.is_empty() {
1972 s.word_nbsp(":");
1973 s.print_type_bounds(¶m.bounds);
1974 }
1975 if let Some(default) = default {
1976 s.space();
1977 s.word_space("=");
1978 s.print_type(default)
1979 }
1980 }
1981 ast::GenericParamKind::Const { ty, default, .. } => {
1982 s.word_space("const");
1983 s.print_ident(param.ident);
1984 s.space();
1985 s.word_space(":");
1986 s.print_type(ty);
1987 if !param.bounds.is_empty() {
1988 s.word_nbsp(":");
1989 s.print_type_bounds(¶m.bounds);
1990 }
1991 if let Some(default) = default {
1992 s.space();
1993 s.word_space("=");
1994 s.print_expr(&default.value, FixupContext::default());
1995 }
1996 }
1997 }
1998 });
1999
2000 self.word(">");
2001 }
2002
2003 pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
2004 match mutbl {
2005 ast::Mutability::Mut => self.word_nbsp("mut"),
2006 ast::Mutability::Not => {
2007 if print_const {
2008 self.word_nbsp("const");
2009 }
2010 }
2011 }
2012 }
2013
2014 fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2015 self.print_mutability(mt.mutbl, print_const);
2016 self.print_type(&mt.ty)
2017 }
2018
2019 fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2020 let ib = self.ibox(INDENT_UNIT);
2021
2022 self.print_outer_attributes_inline(&input.attrs);
2023
2024 match input.ty.kind {
2025 ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2026 _ => {
2027 if let Some(eself) = input.to_self() {
2028 self.print_explicit_self(&eself);
2029 } else {
2030 if !matches!(input.pat.kind, PatKind::Missing) {
2031 self.print_pat(&input.pat);
2032 self.word(":");
2033 self.space();
2034 }
2035 self.print_type(&input.ty);
2036 }
2037 }
2038 }
2039 self.end(ib);
2040 }
2041
2042 fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2043 if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2044 self.space_if_not_bol();
2045 let ib = self.ibox(INDENT_UNIT);
2046 self.word_space("->");
2047 self.print_type(ty);
2048 self.end(ib);
2049 self.maybe_print_comment(ty.span.lo());
2050 }
2051 }
2052
2053 fn print_ty_fn(
2054 &mut self,
2055 ext: ast::Extern,
2056 safety: ast::Safety,
2057 decl: &ast::FnDecl,
2058 name: Option<Ident>,
2059 generic_params: &[ast::GenericParam],
2060 ) {
2061 let ib = self.ibox(INDENT_UNIT);
2062 self.print_formal_generic_params(generic_params);
2063 let generics = ast::Generics::default();
2064 let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
2065 self.print_fn(decl, header, name, &generics);
2066 self.end(ib);
2067 }
2068
2069 fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2070 self.print_constness(header.constness);
2071 header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
2072 self.print_safety(header.safety);
2073
2074 match header.ext {
2075 ast::Extern::None => {}
2076 ast::Extern::Implicit(_) => {
2077 self.word_nbsp("extern");
2078 }
2079 ast::Extern::Explicit(abi, _) => {
2080 self.word_nbsp("extern");
2081 self.print_token_literal(abi.as_token_lit(), abi.span);
2082 self.nbsp();
2083 }
2084 }
2085
2086 self.word("fn")
2087 }
2088
2089 fn print_safety(&mut self, s: ast::Safety) {
2090 match s {
2091 ast::Safety::Default => {}
2092 ast::Safety::Safe(_) => self.word_nbsp("safe"),
2093 ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
2094 }
2095 }
2096
2097 fn print_constness(&mut self, s: ast::Const) {
2098 match s {
2099 ast::Const::No => {}
2100 ast::Const::Yes(_) => self.word_nbsp("const"),
2101 }
2102 }
2103
2104 fn print_is_auto(&mut self, s: ast::IsAuto) {
2105 match s {
2106 ast::IsAuto::Yes => self.word_nbsp("auto"),
2107 ast::IsAuto::No => {}
2108 }
2109 }
2110
2111 fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
2112 self.print_token_literal(lit.as_token_lit(), lit.span)
2113 }
2114
2115 fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
2116 self.maybe_print_comment(span.lo());
2117 self.word(token_lit.to_string())
2118 }
2119
2120 fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
2121 self.print_string(sym.as_str(), style);
2122 }
2123
2124 fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
2125 self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
2126 }
2127
2128 fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
2129 self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
2130 }
2131
2132 fn print_attribute(&mut self, attr: &ast::Attribute) {
2133 self.print_attribute_inline(attr, false);
2134 }
2135
2136 fn print_meta_list_item(&mut self, item: &ast::MetaItemInner) {
2137 match item {
2138 ast::MetaItemInner::MetaItem(mi) => self.print_meta_item(mi),
2139 ast::MetaItemInner::Lit(lit) => self.print_meta_item_lit(lit),
2140 }
2141 }
2142
2143 fn print_meta_item(&mut self, item: &ast::MetaItem) {
2144 let ib = self.ibox(INDENT_UNIT);
2145 match &item.kind {
2146 ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
2147 ast::MetaItemKind::NameValue(value) => {
2148 self.print_path(&item.path, false, 0);
2149 self.space();
2150 self.word_space("=");
2151 self.print_meta_item_lit(value);
2152 }
2153 ast::MetaItemKind::List(items) => {
2154 self.print_path(&item.path, false, 0);
2155 self.popen();
2156 self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
2157 self.pclose();
2158 }
2159 }
2160 self.end(ib);
2161 }
2162
2163 pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
2164 Self::to_string(|s| s.print_type_bounds(bounds))
2165 }
2166
2167 pub(crate) fn where_bound_predicate_to_string(
2168 &self,
2169 where_bound_predicate: &ast::WhereBoundPredicate,
2170 ) -> String {
2171 Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
2172 }
2173
2174 pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
2175 Self::to_string(|s| {
2176 s.print_tt(tt, false);
2177 })
2178 }
2179
2180 pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
2181 Self::to_string(|s| s.print_path_segment(p, false))
2182 }
2183
2184 pub(crate) fn meta_list_item_to_string(&self, li: &ast::MetaItemInner) -> String {
2185 Self::to_string(|s| s.print_meta_list_item(li))
2186 }
2187
2188 pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
2189 Self::to_string(|s| s.print_attribute(attr))
2190 }
2191}