aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres/crate_def_map.rs
blob: ea9b4fb50f99cb929175358bd11ff6eba0ec2269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/// This module implements new import-resolution/macro expansion algorithm.
///
/// The result of this module is `CrateDefMap`: a datastructure which contains:
///
///   * a tree of modules for the crate
///   * for each module, a set of items visible in the module (directly declared
///     or imported)
///
/// Note that `CrateDefMap` contains fully macro expanded code.
///
/// Computing `CrateDefMap` can be partitioned into several logically
/// independent "phases". The phases are mutually recursive though, there's no
/// stric ordering.
///
/// ## Collecting RawItems
///
///  This happens in the `raw` module, which parses a single source file into a
///  set of top-level items. Nested importa are desugared to flat imports in
///  this phase. Macro calls are represented as a triple of (Path, Option<Name>,
///  TokenTree).
///
/// ## Collecting Modules
///
/// This happens in the `collector` module. In this phase, we recursively walk
/// tree of modules, collect raw items from submodules, populate module scopes
/// with defined items (so, we assign item ids in this phase) and record the set
/// of unresovled imports and macros.
///
/// While we walk tree of modules, we also record macro_rules defenitions and
/// expand calls to macro_rules defined macros.
///
/// ## Resolving Imports
///
/// TBD
///
/// ## Resolving Macros
///
/// While macro_rules from the same crate use a global mutable namespace, macros
/// from other crates (including proc-macros) can be used with `foo::bar!`
/// syntax.
///
/// TBD;
mod raw;
mod collector;

use rustc_hash::FxHashMap;
use ra_arena::{Arena};

use crate::{
    Name,
    module_tree::ModuleId,
    nameres::ModuleScope,
};

#[derive(Default, Debug)]
struct ModuleData {
    parent: Option<ModuleId>,
    children: FxHashMap<Name, ModuleId>,
    scope: ModuleScope,
}

/// Contans all top-level defs from a macro-expanded crate
#[derive(Debug)]
pub(crate) struct CrateDefMap {
    root: ModuleId,
    modules: Arena<ModuleId, ModuleData>,
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use ra_db::SourceDatabase;
    use insta::assert_snapshot_matches;

    use crate::{Crate, mock::MockDatabase, nameres::Resolution};

    use super::*;

    fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
        let db = MockDatabase::with_files(fixture);
        let crate_id = db.crate_graph().iter().next().unwrap();
        let krate = Crate { crate_id };
        collector::crate_def_map_query(&db, krate)
    }

    fn render_crate_def_map(map: &CrateDefMap) -> String {
        let mut buf = String::new();
        go(&mut buf, map, "\ncrate", map.root);
        return buf;

        fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: ModuleId) {
            *buf += path;
            *buf += "\n";
            for (name, res) in map.modules[module].scope.items.iter() {
                *buf += &format!("{}: {}\n", name, dump_resolution(res))
            }
            for (name, child) in map.modules[module].children.iter() {
                let path = path.to_string() + &format!("::{}", name);
                go(buf, map, &path, *child);
            }
        }

        fn dump_resolution(resolution: &Resolution) -> &'static str {
            match (resolution.def.types.is_some(), resolution.def.values.is_some()) {
                (true, true) => "t v",
                (true, false) => "t",
                (false, true) => "v",
                (false, false) => "_",
            }
        }
    }

    fn def_map(fixtute: &str) -> String {
        let dm = compute_crate_def_map(fixtute);
        render_crate_def_map(&dm)
    }

    #[test]
    fn crate_def_map_smoke_test() {
        let map = def_map(
            "
            //- /lib.rs
            mod foo;
            struct S;

            //- /foo/mod.rs
            pub mod bar;
            fn f() {}

            //- /foo/bar.rs
            pub struct Baz;
            enum E { V }
        ",
        );
        assert_snapshot_matches!(
        map,
            @r###"
crate
S: t v

crate::foo
f: v

crate::foo::bar
Baz: t v
E: t
"###
        )
    }

    #[test]
    fn macro_rules_are_globally_visible() {
        let map = def_map(
            "
            //- /lib.rs
            macro_rules! structs {
                ($($i:ident),*) => {
                    $(struct $i { field: u32 } )*
                }
            }
            structs!(Foo);
            mod nested;

            //- /nested.rs
            structs!(Bar, Baz);
        ",
        );
        assert_snapshot_matches!(map, @r###"
crate
Foo: t v

crate::nested
Bar: t v
Baz: t v
"###);
    }

    #[test]
    fn macro_rules_can_define_modules() {
        let map = def_map(
            "
            //- /lib.rs
            macro_rules! m {
                ($name:ident) => { mod $name;  }
            }
            m!(n1);

            //- /n1.rs
            m!(n2)
            //- /n1/n2.rs
            struct X;
        ",
        );
        assert_snapshot_matches!(map, @r###"
crate

crate::n1

crate::n1::n2
X: t v
"###);
    }
}