1 use crate::pp::Breaks::Inconsistent;
2 use crate::pprust::state::{AnnNode, PrintState, State};
5 use rustc_ast::GenericBound;
6 use rustc_ast::ModKind;
7 use rustc_span::symbol::Ident;
9 fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
10 format!("{}{}", State::to_string(|s| s.print_visibility(vis)), s)
14 fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[ast::Attribute]) {
15 self.print_inner_attributes(attrs);
16 for item in &nmod.items {
17 self.print_foreign_item(item);
21 fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
22 let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
23 self.ann.pre(self, AnnNode::SubItem(id));
24 self.hardbreak_if_not_bol();
25 self.maybe_print_comment(span.lo());
26 self.print_outer_attributes(attrs);
28 ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
29 self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
31 ast::ForeignItemKind::Static(ty, mutbl, body) => {
32 let def = ast::Defaultness::Final;
33 self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
35 ast::ForeignItemKind::TyAlias(box ast::TyAlias {
41 self.print_associated_type(
50 ast::ForeignItemKind::MacCall(m) => {
52 if m.args.need_semicolon() {
57 self.ann.post(self, AnnNode::SubItem(id))
63 mutbl: Option<ast::Mutability>,
65 body: Option<&ast::Expr>,
66 vis: &ast::Visibility,
67 defaultness: ast::Defaultness,
70 self.print_visibility(vis);
71 self.print_defaultness(defaultness);
72 let leading = match mutbl {
74 Some(ast::Mutability::Not) => "static",
75 Some(ast::Mutability::Mut) => "static mut",
77 self.word_space(leading);
78 self.print_ident(ident);
84 self.end(); // end the head-ibox
85 if let Some(body) = body {
87 self.print_expr(body);
90 self.end(); // end the outer cbox
93 fn print_associated_type(
96 generics: &ast::Generics,
97 bounds: &ast::GenericBounds,
99 vis: &ast::Visibility,
100 defaultness: ast::Defaultness,
103 self.print_visibility(vis);
104 self.print_defaultness(defaultness);
105 self.word_space("type");
106 self.print_ident(ident);
107 self.print_generic_params(&generics.params);
108 self.print_type_bounds(":", bounds);
109 self.print_where_clause(&generics.where_clause);
110 if let Some(ty) = ty {
112 self.word_space("=");
116 self.end(); // end inner head-block
117 self.end(); // end outer head-block
120 /// Pretty-prints an item.
121 crate fn print_item(&mut self, item: &ast::Item) {
122 self.hardbreak_if_not_bol();
123 self.maybe_print_comment(item.span.lo());
124 self.print_outer_attributes(&item.attrs);
125 self.ann.pre(self, AnnNode::Item(item));
127 ast::ItemKind::ExternCrate(orig_name) => {
128 self.head(visibility_qualified(&item.vis, "extern crate"));
129 if let Some(orig_name) = orig_name {
130 self.print_name(orig_name);
135 self.print_ident(item.ident);
137 self.end(); // end inner head-block
138 self.end(); // end outer head-block
140 ast::ItemKind::Use(ref tree) => {
141 self.head(visibility_qualified(&item.vis, "use"));
142 self.print_use_tree(tree);
144 self.end(); // end inner head-block
145 self.end(); // end outer head-block
147 ast::ItemKind::Static(ref ty, mutbl, ref body) => {
148 let def = ast::Defaultness::Final;
149 self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
151 ast::ItemKind::Const(def, ref ty, ref body) => {
152 self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
154 ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => {
155 let body = body.as_deref();
166 ast::ItemKind::Mod(unsafety, ref mod_kind) => {
167 self.head(Self::to_string(|s| {
168 s.print_visibility(&item.vis);
169 s.print_unsafety(unsafety);
172 self.print_ident(item.ident);
175 ModKind::Loaded(items, ..) => {
178 self.print_inner_attributes(&item.attrs);
180 self.print_item(item);
182 let empty = item.attrs.is_empty() && items.is_empty();
183 self.bclose(item.span, empty);
185 ModKind::Unloaded => {
187 self.end(); // end inner head-block
188 self.end(); // end outer head-block
192 ast::ItemKind::ForeignMod(ref nmod) => {
193 self.head(Self::to_string(|s| {
194 s.print_unsafety(nmod.unsafety);
197 if let Some(abi) = nmod.abi {
198 self.print_literal(&abi.as_lit());
202 self.print_foreign_mod(nmod, &item.attrs);
203 let empty = item.attrs.is_empty() && nmod.items.is_empty();
204 self.bclose(item.span, empty);
206 ast::ItemKind::GlobalAsm(ref asm) => {
207 self.head(visibility_qualified(&item.vis, "global_asm!"));
208 self.print_inline_asm(asm);
211 ast::ItemKind::TyAlias(box ast::TyAlias {
217 let ty = ty.as_deref();
218 self.print_associated_type(
227 ast::ItemKind::Enum(ref enum_definition, ref params) => {
228 self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
230 ast::ItemKind::Struct(ref struct_def, ref generics) => {
231 self.head(visibility_qualified(&item.vis, "struct"));
232 self.print_struct(struct_def, generics, item.ident, item.span, true);
234 ast::ItemKind::Union(ref struct_def, ref generics) => {
235 self.head(visibility_qualified(&item.vis, "union"));
236 self.print_struct(struct_def, generics, item.ident, item.span, true);
238 ast::ItemKind::Impl(box ast::Impl {
249 self.print_visibility(&item.vis);
250 self.print_defaultness(defaultness);
251 self.print_unsafety(unsafety);
254 if generics.params.is_empty() {
257 self.print_generic_params(&generics.params);
261 self.print_constness(constness);
263 if let ast::ImplPolarity::Negative(_) = polarity {
267 if let Some(ref t) = *of_trait {
268 self.print_trait_ref(t);
270 self.word_space("for");
273 self.print_type(self_ty);
274 self.print_where_clause(&generics.where_clause);
278 self.print_inner_attributes(&item.attrs);
279 for impl_item in items {
280 self.print_assoc_item(impl_item);
282 let empty = item.attrs.is_empty() && items.is_empty();
283 self.bclose(item.span, empty);
285 ast::ItemKind::Trait(box ast::Trait {
294 self.print_visibility(&item.vis);
295 self.print_unsafety(unsafety);
296 self.print_is_auto(is_auto);
297 self.word_nbsp("trait");
298 self.print_ident(item.ident);
299 self.print_generic_params(&generics.params);
300 let mut real_bounds = Vec::with_capacity(bounds.len());
301 for b in bounds.iter() {
302 if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
304 self.word_space("for ?");
305 self.print_trait_ref(&ptr.trait_ref);
307 real_bounds.push(b.clone());
310 self.print_type_bounds(":", &real_bounds);
311 self.print_where_clause(&generics.where_clause);
314 self.print_inner_attributes(&item.attrs);
315 for trait_item in items {
316 self.print_assoc_item(trait_item);
318 let empty = item.attrs.is_empty() && items.is_empty();
319 self.bclose(item.span, empty);
321 ast::ItemKind::TraitAlias(ref generics, ref bounds) => {
322 self.head(visibility_qualified(&item.vis, "trait"));
323 self.print_ident(item.ident);
324 self.print_generic_params(&generics.params);
325 let mut real_bounds = Vec::with_capacity(bounds.len());
326 // FIXME(durka) this seems to be some quite outdated syntax
327 for b in bounds.iter() {
328 if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
330 self.word_space("for ?");
331 self.print_trait_ref(&ptr.trait_ref);
333 real_bounds.push(b.clone());
337 self.print_type_bounds("=", &real_bounds);
338 self.print_where_clause(&generics.where_clause);
340 self.end(); // end inner head-block
341 self.end(); // end outer head-block
343 ast::ItemKind::MacCall(ref mac) => {
345 if mac.args.need_semicolon() {
349 ast::ItemKind::MacroDef(ref macro_def) => {
350 self.print_mac_def(macro_def, &item.ident, item.span, |state| {
351 state.print_visibility(&item.vis)
355 self.ann.post(self, AnnNode::Item(item))
360 enum_definition: &ast::EnumDef,
361 generics: &ast::Generics,
363 span: rustc_span::Span,
364 visibility: &ast::Visibility,
366 self.head(visibility_qualified(visibility, "enum"));
367 self.print_ident(ident);
368 self.print_generic_params(&generics.params);
369 self.print_where_clause(&generics.where_clause);
371 self.print_variants(&enum_definition.variants, span)
374 fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) {
377 self.space_if_not_bol();
378 self.maybe_print_comment(v.span.lo());
379 self.print_outer_attributes(&v.attrs);
381 self.print_variant(v);
384 self.maybe_print_trailing_comment(v.span, None);
386 let empty = variants.is_empty();
387 self.bclose(span, empty)
390 crate fn print_visibility(&mut self, vis: &ast::Visibility) {
392 ast::VisibilityKind::Public => self.word_nbsp("pub"),
393 ast::VisibilityKind::Crate(sugar) => match sugar {
394 ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"),
395 ast::CrateSugar::JustCrate => self.word_nbsp("crate"),
397 ast::VisibilityKind::Restricted { ref path, .. } => {
398 let path = Self::to_string(|s| s.print_path(path, false, 0));
399 if path == "self" || path == "super" {
400 self.word_nbsp(format!("pub({})", path))
402 self.word_nbsp(format!("pub(in {})", path))
405 ast::VisibilityKind::Inherited => {}
409 fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
410 if let ast::Defaultness::Default(_) = defaultness {
411 self.word_nbsp("default");
415 fn print_record_struct_body(&mut self, fields: &[ast::FieldDef], span: rustc_span::Span) {
419 let empty = fields.is_empty();
421 self.hardbreak_if_not_bol();
423 for field in fields {
424 self.hardbreak_if_not_bol();
425 self.maybe_print_comment(field.span.lo());
426 self.print_outer_attributes(&field.attrs);
427 self.print_visibility(&field.vis);
428 self.print_ident(field.ident.unwrap());
430 self.print_type(&field.ty);
435 self.bclose(span, empty);
440 struct_def: &ast::VariantData,
441 generics: &ast::Generics,
443 span: rustc_span::Span,
444 print_finalizer: bool,
446 self.print_ident(ident);
447 self.print_generic_params(&generics.params);
449 ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
450 if let ast::VariantData::Tuple(..) = struct_def {
452 self.commasep(Inconsistent, struct_def.fields(), |s, field| {
453 s.maybe_print_comment(field.span.lo());
454 s.print_outer_attributes(&field.attrs);
455 s.print_visibility(&field.vis);
456 s.print_type(&field.ty)
460 self.print_where_clause(&generics.where_clause);
465 self.end(); // Close the outer-box.
467 ast::VariantData::Struct(ref fields, ..) => {
468 self.print_where_clause(&generics.where_clause);
469 self.print_record_struct_body(fields, span);
474 crate fn print_variant(&mut self, v: &ast::Variant) {
476 self.print_visibility(&v.vis);
477 let generics = ast::Generics::default();
478 self.print_struct(&v.data, &generics, v.ident, v.span, false);
479 if let Some(ref d) = v.disr_expr {
481 self.word_space("=");
482 self.print_expr(&d.value)
486 fn print_assoc_item(&mut self, item: &ast::AssocItem) {
487 let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
488 self.ann.pre(self, AnnNode::SubItem(id));
489 self.hardbreak_if_not_bol();
490 self.maybe_print_comment(span.lo());
491 self.print_outer_attributes(attrs);
493 ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
494 self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
496 ast::AssocItemKind::Const(def, ty, body) => {
497 self.print_item_const(ident, None, ty, body.as_deref(), vis, *def);
499 ast::AssocItemKind::TyAlias(box ast::TyAlias { defaultness, generics, bounds, ty }) => {
500 self.print_associated_type(
509 ast::AssocItemKind::MacCall(m) => {
511 if m.args.need_semicolon() {
516 self.ann.post(self, AnnNode::SubItem(id))
523 generics: &ast::Generics,
524 vis: &ast::Visibility,
525 defaultness: ast::Defaultness,
526 body: Option<&ast::Block>,
527 attrs: &[ast::Attribute],
532 self.print_visibility(vis);
533 self.print_defaultness(defaultness);
534 self.print_fn(&sig.decl, sig.header, Some(name), generics);
535 if let Some(body) = body {
537 self.print_block_with_attrs(body, attrs);
546 header: ast::FnHeader,
548 generics: &ast::Generics,
550 self.print_fn_header_info(header);
551 if let Some(name) = name {
553 self.print_ident(name);
555 self.print_generic_params(&generics.params);
556 self.print_fn_params_and_ret(decl, false);
557 self.print_where_clause(&generics.where_clause)
560 crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
561 let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
563 self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
565 self.print_fn_ret_ty(&decl.output)
568 fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
569 if where_clause.predicates.is_empty() && !where_clause.has_where_token {
574 self.word_space("where");
576 for (i, predicate) in where_clause.predicates.iter().enumerate() {
578 self.word_space(",");
581 self.print_where_predicate(predicate);
585 pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
587 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
588 bound_generic_params,
593 self.print_formal_generic_params(bound_generic_params);
594 self.print_type(bounded_ty);
595 self.print_type_bounds(":", bounds);
597 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
602 self.print_lifetime_bounds(*lifetime, bounds);
604 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
605 self.print_type(lhs_ty);
607 self.word_space("=");
608 self.print_type(rhs_ty);
613 fn print_use_tree(&mut self, tree: &ast::UseTree) {
615 ast::UseTreeKind::Simple(rename, ..) => {
616 self.print_path(&tree.prefix, false, 0);
617 if let Some(rename) = rename {
619 self.word_space("as");
620 self.print_ident(rename);
623 ast::UseTreeKind::Glob => {
624 if !tree.prefix.segments.is_empty() {
625 self.print_path(&tree.prefix, false, 0);
630 ast::UseTreeKind::Nested(ref items) => {
631 if tree.prefix.segments.is_empty() {
634 self.print_path(&tree.prefix, false, 0);
637 self.commasep(Inconsistent, &items, |this, &(ref tree, _)| {
638 this.print_use_tree(tree)