cl_ast/desugar/
path_absoluter.rs1use crate::{ast::*, ast_visitor::Fold};
2
3pub struct NormalizePaths {
5 path: Path,
6}
7
8impl NormalizePaths {
9 pub fn new() -> Self {
10 Self { path: Path { absolute: true, parts: vec![] } }
11 }
12 pub fn in_path(path: Path) -> Self {
14 Self { path }
15 }
16}
17
18impl Default for NormalizePaths {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl Fold for NormalizePaths {
25 fn fold_module(&mut self, m: Module) -> Module {
26 let Module { name, file } = m;
27 self.path.push(PathPart::Ident(name));
28
29 let name = self.fold_sym(name);
30 let file = file.map(|f| self.fold_file(f));
31
32 self.path.pop();
33 Module { name, file }
34 }
35
36 fn fold_path(&mut self, p: Path) -> Path {
37 if p.absolute {
38 p
39 } else {
40 self.path.clone().concat(&p)
41 }
42 }
43
44 fn fold_use(&mut self, u: Use) -> Use {
45 let Use { absolute, mut tree } = u;
46
47 if !absolute {
48 for segment in self.path.parts.iter().rev() {
49 tree = UseTree::Path(segment.clone(), Box::new(tree))
50 }
51 }
52
53 Use { absolute: true, tree: self.fold_use_tree(tree) }
54 }
55}