]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_save_analysis/src/dumper.rs
Rollup merge of #107097 - tmiasko:ssa, r=cjgillot
[rust.git] / compiler / rustc_save_analysis / src / dumper.rs
1 use rls_data::config::Config;
2 use rls_data::{
3     self, Analysis, CompilationOptions, CratePreludeData, Def, DefKind, Impl, Import, MacroRef,
4     Ref, RefKind, Relation,
5 };
6 use rls_span::{Column, Row};
7
8 #[derive(Debug)]
9 pub struct Access {
10     pub reachable: bool,
11     pub public: bool,
12 }
13
14 pub struct Dumper {
15     result: Analysis,
16     config: Config,
17 }
18
19 impl Dumper {
20     pub fn new(config: Config) -> Dumper {
21         Dumper { config: config.clone(), result: Analysis::new(config) }
22     }
23
24     pub fn analysis(&self) -> &Analysis {
25         &self.result
26     }
27 }
28
29 impl Dumper {
30     pub fn crate_prelude(&mut self, data: CratePreludeData) {
31         self.result.prelude = Some(data)
32     }
33
34     pub fn compilation_opts(&mut self, data: CompilationOptions) {
35         self.result.compilation = Some(data);
36     }
37
38     pub fn _macro_use(&mut self, data: MacroRef) {
39         if self.config.pub_only || self.config.reachable_only {
40             return;
41         }
42         self.result.macro_refs.push(data);
43     }
44
45     pub fn import(&mut self, access: &Access, import: Import) {
46         if !access.public && self.config.pub_only || !access.reachable && self.config.reachable_only
47         {
48             return;
49         }
50         self.result.imports.push(import);
51     }
52
53     pub fn dump_ref(&mut self, data: Ref) {
54         if self.config.pub_only || self.config.reachable_only {
55             return;
56         }
57         self.result.refs.push(data);
58     }
59
60     pub fn dump_def(&mut self, access: &Access, mut data: Def) {
61         if !access.public && self.config.pub_only || !access.reachable && self.config.reachable_only
62         {
63             return;
64         }
65         if data.kind == DefKind::Mod && data.span.file_name.to_str().unwrap() != data.value {
66             // If the module is an out-of-line definition, then we'll make the
67             // definition the first character in the module's file and turn
68             // the declaration into a reference to it.
69             let rf = Ref { kind: RefKind::Mod, span: data.span, ref_id: data.id };
70             self.result.refs.push(rf);
71             data.span = rls_data::SpanData {
72                 file_name: data.value.clone().into(),
73                 byte_start: 0,
74                 byte_end: 0,
75                 line_start: Row::new_one_indexed(1),
76                 line_end: Row::new_one_indexed(1),
77                 column_start: Column::new_one_indexed(1),
78                 column_end: Column::new_one_indexed(1),
79             }
80         }
81         self.result.defs.push(data);
82     }
83
84     pub fn dump_relation(&mut self, data: Relation) {
85         self.result.relations.push(data);
86     }
87
88     pub fn dump_impl(&mut self, data: Impl) {
89         self.result.impls.push(data);
90     }
91 }