]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/print.rs
fb296519d499768cc59d1abd5065b328a681ef65
[rust.git] / src / librustc / ty / print.rs
1 use crate::ty::{self, TyCtxt, TypeFoldable};
2
3 use rustc_data_structures::fx::FxHashSet;
4 use syntax::symbol::InternedString;
5
6 use std::fmt;
7
8 // FIXME(eddyb) this module uses `pub(crate)` for things used only
9 // from `ppaux` - when that is removed, they can be re-privatized.
10
11 struct LateBoundRegionNameCollector(FxHashSet<InternedString>);
12 impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
13     fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
14         match *r {
15             ty::ReLateBound(_, ty::BrNamed(_, name)) => {
16                 self.0.insert(name);
17             },
18             _ => {},
19         }
20         r.super_visit_with(self)
21     }
22 }
23
24 pub struct PrintCx<'a, 'gcx, 'tcx> {
25     pub(crate) tcx: TyCtxt<'a, 'gcx, 'tcx>,
26     pub(crate) is_debug: bool,
27     pub(crate) is_verbose: bool,
28     pub(crate) identify_regions: bool,
29     pub(crate) used_region_names: Option<FxHashSet<InternedString>>,
30     pub(crate) region_index: usize,
31     pub(crate) binder_depth: usize,
32 }
33
34 impl PrintCx<'a, 'gcx, 'tcx> {
35     pub(crate) fn with<R>(f: impl FnOnce(PrintCx<'_, '_, '_>) -> R) -> R {
36         ty::tls::with(|tcx| {
37             f(PrintCx {
38                 tcx,
39                 is_debug: false,
40                 is_verbose: tcx.sess.verbose(),
41                 identify_regions: tcx.sess.opts.debugging_opts.identify_regions,
42                 used_region_names: None,
43                 region_index: 0,
44                 binder_depth: 0,
45             })
46         })
47     }
48     pub(crate) fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
49     where T: TypeFoldable<'tcx>
50     {
51         let mut collector = LateBoundRegionNameCollector(Default::default());
52         value.visit_with(&mut collector);
53         self.used_region_names = Some(collector.0);
54         self.region_index = 0;
55     }
56 }
57
58 pub trait Print<'tcx> {
59     fn print<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, '_>) -> fmt::Result;
60     fn print_to_string(&self, cx: &mut PrintCx<'_, '_, '_>) -> String {
61         let mut result = String::new();
62         let _ = self.print(&mut result, cx);
63         result
64     }
65     fn print_display<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, '_>) -> fmt::Result {
66         let old_debug = cx.is_debug;
67         cx.is_debug = false;
68         let result = self.print(f, cx);
69         cx.is_debug = old_debug;
70         result
71     }
72     fn print_display_to_string(&self, cx: &mut PrintCx<'_, '_, '_>) -> String {
73         let mut result = String::new();
74         let _ = self.print_display(&mut result, cx);
75         result
76     }
77     fn print_debug<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, '_>) -> fmt::Result {
78         let old_debug = cx.is_debug;
79         cx.is_debug = true;
80         let result = self.print(f, cx);
81         cx.is_debug = old_debug;
82         result
83     }
84     fn print_debug_to_string(&self, cx: &mut PrintCx<'_, '_, '_>) -> String {
85         let mut result = String::new();
86         let _ = self.print_debug(&mut result, cx);
87         result
88     }
89 }