1 //! `ItemTree` debug printer.
3 use std::fmt::{self, Write};
5 use itertools::Itertools;
9 generics::{TypeOrConstParamData, WherePredicate, WherePredicateTypeTarget},
11 type_ref::TraitBoundModifier,
12 visibility::RawVisibility,
17 pub(super) fn print_item_tree(tree: &ItemTree) -> String {
18 let mut p = Printer { tree, buf: String::new(), indent_level: 0, needs_indent: true };
20 if let Some(attrs) = tree.attrs.get(&AttrOwner::TopLevel) {
21 p.print_attrs(attrs, true);
25 for item in tree.top_level_items() {
26 p.print_mod_item(*item);
29 let mut s = p.buf.trim_end_matches('\n').to_string();
35 ($dst:expr, $($arg:tt)*) => {
36 { let _ = write!($dst, $($arg)*); }
42 { let _ = writeln!($dst); }
44 ($dst:expr, $($arg:tt)*) => {
45 { let _ = writeln!($dst, $($arg)*); }
56 impl<'a> Printer<'a> {
57 fn indented(&mut self, f: impl FnOnce(&mut Self)) {
58 self.indent_level += 1;
61 self.indent_level -= 1;
62 self.buf = self.buf.trim_end_matches('\n').to_string();
65 /// Ensures that a blank line is output before the next text.
67 let mut iter = self.buf.chars().rev().fuse();
68 match (iter.next(), iter.next()) {
69 (Some('\n'), Some('\n') | None) | (None, None) => {}
70 (Some('\n'), Some(_)) => {
77 (None, Some(_)) => unreachable!(),
81 fn whitespace(&mut self) {
82 match self.buf.chars().next_back() {
83 None | Some('\n' | ' ') => {}
84 _ => self.buf.push(' '),
88 fn print_attrs(&mut self, attrs: &RawAttrs, inner: bool) {
89 let inner = if inner { "!" } else { "" };
90 for attr in &**attrs {
96 attr.input.as_ref().map(|it| it.to_string()).unwrap_or_default(),
102 fn print_attrs_of(&mut self, of: impl Into<AttrOwner>) {
103 if let Some(attrs) = self.tree.attrs.get(&of.into()) {
104 self.print_attrs(attrs, false);
108 fn print_visibility(&mut self, vis: RawVisibilityId) {
109 match &self.tree[vis] {
110 RawVisibility::Module(path) => w!(self, "pub({}) ", path),
111 RawVisibility::Public => w!(self, "pub "),
115 fn print_fields(&mut self, fields: &Fields) {
117 Fields::Record(fields) => {
120 self.indented(|this| {
121 for field in fields.clone() {
122 let Field { visibility, name, type_ref } = &this.tree[field];
123 this.print_attrs_of(field);
124 this.print_visibility(*visibility);
125 w!(this, "{}: ", name);
126 this.print_type_ref(type_ref);
132 Fields::Tuple(fields) => {
134 self.indented(|this| {
135 for field in fields.clone() {
136 let Field { visibility, name, type_ref } = &this.tree[field];
137 this.print_attrs_of(field);
138 this.print_visibility(*visibility);
139 w!(this, "{}: ", name);
140 this.print_type_ref(type_ref);
150 fn print_fields_and_where_clause(&mut self, fields: &Fields, params: &GenericParams) {
152 Fields::Record(_) => {
153 if self.print_where_clause(params) {
156 self.print_fields(fields);
159 self.print_where_clause(params);
160 self.print_fields(fields);
162 Fields::Tuple(_) => {
163 self.print_fields(fields);
164 self.print_where_clause(params);
169 fn print_use_tree(&mut self, use_tree: &UseTree) {
170 match &use_tree.kind {
171 UseTreeKind::Single { path, alias } => {
172 w!(self, "{}", path);
173 if let Some(alias) = alias {
174 w!(self, " as {}", alias);
177 UseTreeKind::Glob { path } => {
178 if let Some(path) = path {
179 w!(self, "{}::", path);
183 UseTreeKind::Prefixed { prefix, list } => {
184 if let Some(prefix) = prefix {
185 w!(self, "{}::", prefix);
188 for (i, tree) in list.iter().enumerate() {
192 self.print_use_tree(tree);
199 fn print_mod_item(&mut self, item: ModItem) {
200 self.print_attrs_of(item);
203 ModItem::Import(it) => {
204 let Import { visibility, use_tree, ast_id: _ } = &self.tree[it];
205 self.print_visibility(*visibility);
207 self.print_use_tree(use_tree);
210 ModItem::ExternCrate(it) => {
211 let ExternCrate { name, alias, visibility, ast_id: _ } = &self.tree[it];
212 self.print_visibility(*visibility);
213 w!(self, "extern crate {}", name);
214 if let Some(alias) = alias {
215 w!(self, " as {}", alias);
219 ModItem::ExternBlock(it) => {
220 let ExternBlock { abi, ast_id: _, children } = &self.tree[it];
222 if let Some(abi) = abi {
223 w!(self, "\"{}\" ", abi);
226 self.indented(|this| {
227 for child in &**children {
228 this.print_mod_item(*child);
233 ModItem::Function(it) => {
237 explicit_generic_params,
246 wln!(self, "// flags = 0x{:X}", flags.bits);
248 self.print_visibility(*visibility);
249 if let Some(abi) = abi {
250 w!(self, "extern \"{}\" ", abi);
252 w!(self, "fn {}", name);
253 self.print_generic_params(explicit_generic_params);
255 if !params.is_empty() {
256 self.indented(|this| {
257 for param in params.clone() {
258 this.print_attrs_of(param);
259 match &this.tree[param] {
260 Param::Normal(name, ty) => {
262 Some(name) => w!(this, "{}: ", name),
263 None => w!(this, "_: "),
265 this.print_type_ref(ty);
276 self.print_type_ref(ret_type);
277 self.print_where_clause(explicit_generic_params);
280 ModItem::Struct(it) => {
281 let Struct { visibility, name, fields, generic_params, ast_id: _ } = &self.tree[it];
282 self.print_visibility(*visibility);
283 w!(self, "struct {}", name);
284 self.print_generic_params(generic_params);
285 self.print_fields_and_where_clause(fields, generic_params);
286 if matches!(fields, Fields::Record(_)) {
292 ModItem::Union(it) => {
293 let Union { name, visibility, fields, generic_params, ast_id: _ } = &self.tree[it];
294 self.print_visibility(*visibility);
295 w!(self, "union {}", name);
296 self.print_generic_params(generic_params);
297 self.print_fields_and_where_clause(fields, generic_params);
298 if matches!(fields, Fields::Record(_)) {
304 ModItem::Enum(it) => {
305 let Enum { name, visibility, variants, generic_params, ast_id: _ } = &self.tree[it];
306 self.print_visibility(*visibility);
307 w!(self, "enum {}", name);
308 self.print_generic_params(generic_params);
309 self.print_where_clause_and_opening_brace(generic_params);
310 self.indented(|this| {
311 for variant in variants.clone() {
312 let Variant { name, fields } = &this.tree[variant];
313 this.print_attrs_of(variant);
314 w!(this, "{}", name);
315 this.print_fields(fields);
321 ModItem::Const(it) => {
322 let Const { name, visibility, type_ref, ast_id: _ } = &self.tree[it];
323 self.print_visibility(*visibility);
326 Some(name) => w!(self, "{}", name),
327 None => w!(self, "_"),
330 self.print_type_ref(type_ref);
333 ModItem::Static(it) => {
334 let Static { name, visibility, mutable, type_ref, ast_id: _ } = &self.tree[it];
335 self.print_visibility(*visibility);
340 w!(self, "{}: ", name);
341 self.print_type_ref(type_ref);
345 ModItem::Trait(it) => {
355 self.print_visibility(*visibility);
362 w!(self, "trait {}", name);
363 self.print_generic_params(generic_params);
364 self.print_where_clause_and_opening_brace(generic_params);
365 self.indented(|this| {
366 for item in &**items {
367 this.print_mod_item((*item).into());
372 ModItem::Impl(it) => {
373 let Impl { target_trait, self_ty, is_negative, items, generic_params, ast_id: _ } =
376 self.print_generic_params(generic_params);
381 if let Some(tr) = target_trait {
382 self.print_path(&tr.path);
385 self.print_type_ref(self_ty);
386 self.print_where_clause_and_opening_brace(generic_params);
387 self.indented(|this| {
388 for item in &**items {
389 this.print_mod_item((*item).into());
394 ModItem::TypeAlias(it) => {
395 let TypeAlias { name, visibility, bounds, type_ref, generic_params, ast_id: _ } =
397 self.print_visibility(*visibility);
398 w!(self, "type {}", name);
399 self.print_generic_params(generic_params);
400 if !bounds.is_empty() {
402 self.print_type_bounds(bounds);
404 if let Some(ty) = type_ref {
406 self.print_type_ref(ty);
408 self.print_where_clause(generic_params);
412 ModItem::Mod(it) => {
413 let Mod { name, visibility, kind, ast_id: _ } = &self.tree[it];
414 self.print_visibility(*visibility);
415 w!(self, "mod {}", name);
417 ModKind::Inline { items } => {
419 self.indented(|this| {
420 for item in &**items {
421 this.print_mod_item(*item);
426 ModKind::Outline => {
431 ModItem::MacroCall(it) => {
432 let MacroCall { path, ast_id: _, expand_to: _ } = &self.tree[it];
433 wln!(self, "{}!(...);", path);
435 ModItem::MacroRules(it) => {
436 let MacroRules { name, ast_id: _ } = &self.tree[it];
437 wln!(self, "macro_rules! {} {{ ... }}", name);
439 ModItem::MacroDef(it) => {
440 let MacroDef { name, visibility, ast_id: _ } = &self.tree[it];
441 self.print_visibility(*visibility);
442 wln!(self, "macro {} {{ ... }}", name);
449 fn print_type_ref(&mut self, type_ref: &TypeRef) {
450 // FIXME: deduplicate with `HirDisplay` impl
452 TypeRef::Never => w!(self, "!"),
453 TypeRef::Placeholder => w!(self, "_"),
454 TypeRef::Tuple(fields) => {
456 for (i, field) in fields.iter().enumerate() {
460 self.print_type_ref(field);
464 TypeRef::Path(path) => self.print_path(path),
465 TypeRef::RawPtr(pointee, mtbl) => {
466 let mtbl = match mtbl {
467 Mutability::Shared => "*const",
468 Mutability::Mut => "*mut",
470 w!(self, "{} ", mtbl);
471 self.print_type_ref(pointee);
473 TypeRef::Reference(pointee, lt, mtbl) => {
474 let mtbl = match mtbl {
475 Mutability::Shared => "",
476 Mutability::Mut => "mut ",
479 if let Some(lt) = lt {
480 w!(self, "{} ", lt.name);
482 w!(self, "{}", mtbl);
483 self.print_type_ref(pointee);
485 TypeRef::Array(elem, len) => {
487 self.print_type_ref(elem);
488 w!(self, "; {}]", len);
490 TypeRef::Slice(elem) => {
492 self.print_type_ref(elem);
495 TypeRef::Fn(args_and_ret, varargs) => {
496 let ((_, return_type), args) =
497 args_and_ret.split_last().expect("TypeRef::Fn is missing return type");
499 for (i, (_, typeref)) in args.iter().enumerate() {
503 self.print_type_ref(typeref);
506 if !args.is_empty() {
512 self.print_type_ref(return_type);
514 TypeRef::Macro(_ast_id) => {
517 TypeRef::Error => w!(self, "{{unknown}}"),
518 TypeRef::ImplTrait(bounds) => {
520 self.print_type_bounds(bounds);
522 TypeRef::DynTrait(bounds) => {
524 self.print_type_bounds(bounds);
529 fn print_type_bounds(&mut self, bounds: &[Interned<TypeBound>]) {
530 for (i, bound) in bounds.iter().enumerate() {
535 match bound.as_ref() {
536 TypeBound::Path(path, modifier) => {
538 TraitBoundModifier::None => (),
539 TraitBoundModifier::Maybe => w!(self, "?"),
541 self.print_path(path)
543 TypeBound::ForLifetime(lifetimes, path) => {
544 w!(self, "for<{}> ", lifetimes.iter().format(", "));
545 self.print_path(path);
547 TypeBound::Lifetime(lt) => w!(self, "{}", lt.name),
548 TypeBound::Error => w!(self, "{{unknown}}"),
553 fn print_path(&mut self, path: &Path) {
554 match path.type_anchor() {
557 self.print_type_ref(anchor);
560 None => match path.kind() {
561 PathKind::Plain => {}
562 PathKind::Super(0) => w!(self, "self::"),
563 PathKind::Super(n) => {
568 PathKind::Crate => w!(self, "crate::"),
569 PathKind::Abs => w!(self, "::"),
570 PathKind::DollarCrate(_) => w!(self, "$crate::"),
574 for (i, segment) in path.segments().iter().enumerate() {
579 w!(self, "{}", segment.name);
580 if let Some(generics) = segment.args_and_bindings {
581 // NB: these are all in type position, so `::<` turbofish syntax is not necessary
583 let mut first = true;
584 let args = if generics.has_self_type {
585 let (self_ty, args) = generics.args.split_first().unwrap();
587 self.print_generic_arg(self_ty);
598 self.print_generic_arg(arg);
600 for binding in &generics.bindings {
605 w!(self, "{}", binding.name);
606 if !binding.bounds.is_empty() {
608 self.print_type_bounds(&binding.bounds);
610 if let Some(ty) = &binding.type_ref {
612 self.print_type_ref(ty);
621 fn print_generic_arg(&mut self, arg: &GenericArg) {
623 GenericArg::Type(ty) => self.print_type_ref(ty),
624 GenericArg::Const(c) => w!(self, "{}", c),
625 GenericArg::Lifetime(lt) => w!(self, "{}", lt.name),
629 fn print_generic_params(&mut self, params: &GenericParams) {
630 if params.type_or_consts.is_empty() && params.lifetimes.is_empty() {
635 let mut first = true;
636 for (_, lt) in params.lifetimes.iter() {
641 w!(self, "{}", lt.name);
643 for (idx, x) in params.type_or_consts.iter() {
649 TypeOrConstParamData::TypeParamData(ty) => match &ty.name {
650 Some(name) => w!(self, "{}", name),
651 None => w!(self, "_anon_{}", idx.into_raw()),
653 TypeOrConstParamData::ConstParamData(konst) => {
654 w!(self, "const {}: ", konst.name);
655 self.print_type_ref(&konst.ty);
662 fn print_where_clause_and_opening_brace(&mut self, params: &GenericParams) {
663 if self.print_where_clause(params) {
671 fn print_where_clause(&mut self, params: &GenericParams) -> bool {
672 if params.where_predicates.is_empty() {
677 self.indented(|this| {
678 for (i, pred) in params.where_predicates.iter().enumerate() {
683 let (target, bound) = match pred {
684 WherePredicate::TypeBound { target, bound } => (target, bound),
685 WherePredicate::Lifetime { target, bound } => {
686 wln!(this, "{}: {},", target.name, bound.name);
689 WherePredicate::ForLifetime { lifetimes, target, bound } => {
691 for (i, lt) in lifetimes.iter().enumerate() {
703 WherePredicateTypeTarget::TypeRef(ty) => this.print_type_ref(ty),
704 WherePredicateTypeTarget::TypeOrConstParam(id) => {
705 match ¶ms.type_or_consts[*id].name() {
706 Some(name) => w!(this, "{}", name),
707 None => w!(this, "_anon_{}", id.into_raw()),
712 this.print_type_bounds(std::slice::from_ref(bound));
719 impl<'a> Write for Printer<'a> {
720 fn write_str(&mut self, s: &str) -> fmt::Result {
721 for line in s.split_inclusive('\n') {
722 if self.needs_indent {
723 match self.buf.chars().last() {
724 Some('\n') | None => {}
725 _ => self.buf.push('\n'),
727 self.buf.push_str(&" ".repeat(self.indent_level));
728 self.needs_indent = false;
731 self.buf.push_str(line);
732 self.needs_indent = line.ends_with('\n');