aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/path.rs
blob: a61161b63ac697c10584b756dd0e4b1b0fe25699 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use std::{iter, sync::Arc};

use ra_syntax::{
    ast::{self, NameOwner, TypeAscriptionOwner},
    AstNode,
};

use crate::{name, type_ref::TypeRef, AsName, Name};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Path {
    pub kind: PathKind,
    pub segments: Vec<PathSegment>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PathSegment {
    pub name: Name,
    pub args_and_bindings: Option<Arc<GenericArgs>>,
}

/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
/// can (in the future) also include bindings of associated types, like in
/// `Iterator<Item = Foo>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericArgs {
    pub args: Vec<GenericArg>,
    /// This specifies whether the args contain a Self type as the first
    /// element. This is the case for path segments like `<T as Trait>`, where
    /// `T` is actually a type parameter for the path `Trait` specifying the
    /// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
    /// is left out.
    pub has_self_type: bool,
    /// Associated type bindings like in `Iterator<Item = T>`.
    pub bindings: Vec<(Name, TypeRef)>,
}

/// A single generic argument.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GenericArg {
    Type(TypeRef),
    // or lifetime...
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathKind {
    Plain,
    Self_,
    Super,
    Crate,
    // Absolute path
    Abs,
    // Type based path like `<T>::foo`
    Type(Box<TypeRef>),
}

impl Path {
    /// Calls `cb` with all paths, represented by this use item.
    pub fn expand_use_item(
        item: &ast::UseItem,
        mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
    ) {
        if let Some(tree) = item.use_tree() {
            expand_use_tree(None, tree, &mut cb);
        }
    }

    pub fn from_simple_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> Path {
        Path {
            kind,
            segments: segments
                .into_iter()
                .map(|name| PathSegment { name, args_and_bindings: None })
                .collect(),
        }
    }

    /// Converts an `ast::Path` to `Path`. Works with use trees.
    pub fn from_ast(mut path: ast::Path) -> Option<Path> {
        let mut kind = PathKind::Plain;
        let mut segments = Vec::new();
        loop {
            let segment = path.segment()?;

            if segment.has_colon_colon() {
                kind = PathKind::Abs;
            }

            match segment.kind()? {
                ast::PathSegmentKind::Name(name) => {
                    let args = segment
                        .type_arg_list()
                        .and_then(GenericArgs::from_ast)
                        .or_else(|| {
                            GenericArgs::from_fn_like_path_ast(
                                segment.param_list(),
                                segment.ret_type(),
                            )
                        })
                        .map(Arc::new);
                    let segment = PathSegment { name: name.as_name(), args_and_bindings: args };
                    segments.push(segment);
                }
                ast::PathSegmentKind::Type { type_ref, trait_ref } => {
                    assert!(path.qualifier().is_none()); // this can only occur at the first segment

                    let self_type = TypeRef::from_ast(type_ref?);

                    match trait_ref {
                        // <T>::foo
                        None => {
                            kind = PathKind::Type(Box::new(self_type));
                        }
                        // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
                        Some(trait_ref) => {
                            let path = Path::from_ast(trait_ref.path()?)?;
                            kind = path.kind;
                            let mut prefix_segments = path.segments;
                            prefix_segments.reverse();
                            segments.extend(prefix_segments);
                            // Insert the type reference (T in the above example) as Self parameter for the trait
                            let mut last_segment = segments.last_mut()?;
                            if last_segment.args_and_bindings.is_none() {
                                last_segment.args_and_bindings =
                                    Some(Arc::new(GenericArgs::empty()));
                            };
                            let args = last_segment.args_and_bindings.as_mut().unwrap();
                            let mut args_inner = Arc::make_mut(args);
                            args_inner.has_self_type = true;
                            args_inner.args.insert(0, GenericArg::Type(self_type));
                        }
                    }
                }
                ast::PathSegmentKind::CrateKw => {
                    kind = PathKind::Crate;
                    break;
                }
                ast::PathSegmentKind::SelfKw => {
                    kind = PathKind::Self_;
                    break;
                }
                ast::PathSegmentKind::SuperKw => {
                    kind = PathKind::Super;
                    break;
                }
            }
            path = match qualifier(&path) {
                Some(it) => it,
                None => break,
            };
        }
        segments.reverse();
        return Some(Path { kind, segments });

        fn qualifier(path: &ast::Path) -> Option<ast::Path> {
            if let Some(q) = path.qualifier() {
                return Some(q);
            }
            // FIXME: this bottom up traversal is not too precise.
            // Should we handle do a top-down analysis, recording results?
            let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
            let use_tree = use_tree_list.parent_use_tree();
            use_tree.path()
        }
    }

    /// Converts an `ast::NameRef` into a single-identifier `Path`.
    pub fn from_name_ref(name_ref: &ast::NameRef) -> Path {
        name_ref.as_name().into()
    }

    /// `true` is this path is a single identifier, like `foo`
    pub fn is_ident(&self) -> bool {
        self.kind == PathKind::Plain && self.segments.len() == 1
    }

    /// `true` if this path is just a standalone `self`
    pub fn is_self(&self) -> bool {
        self.kind == PathKind::Self_ && self.segments.is_empty()
    }

    /// If this path is a single identifier, like `foo`, return its name.
    pub fn as_ident(&self) -> Option<&Name> {
        if self.kind != PathKind::Plain || self.segments.len() > 1 {
            return None;
        }
        self.segments.first().map(|s| &s.name)
    }

    pub fn expand_macro_expr(&self) -> Option<Name> {
        self.as_ident().and_then(|name| Some(name.clone()))
    }
}

impl GenericArgs {
    pub(crate) fn from_ast(node: ast::TypeArgList) -> Option<GenericArgs> {
        let mut args = Vec::new();
        for type_arg in node.type_args() {
            let type_ref = TypeRef::from_ast_opt(type_arg.type_ref());
            args.push(GenericArg::Type(type_ref));
        }
        // lifetimes ignored for now
        let mut bindings = Vec::new();
        for assoc_type_arg in node.assoc_type_args() {
            if let Some(name_ref) = assoc_type_arg.name_ref() {
                let name = name_ref.as_name();
                let type_ref = TypeRef::from_ast_opt(assoc_type_arg.type_ref());
                bindings.push((name, type_ref));
            }
        }
        if args.is_empty() && bindings.is_empty() {
            None
        } else {
            Some(GenericArgs { args, has_self_type: false, bindings })
        }
    }

    /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
    /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`).
    pub(crate) fn from_fn_like_path_ast(
        params: Option<ast::ParamList>,
        ret_type: Option<ast::RetType>,
    ) -> Option<GenericArgs> {
        let mut args = Vec::new();
        let mut bindings = Vec::new();
        if let Some(params) = params {
            let mut param_types = Vec::new();
            for param in params.params() {
                let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
                param_types.push(type_ref);
            }
            let arg = GenericArg::Type(TypeRef::Tuple(param_types));
            args.push(arg);
        }
        if let Some(ret_type) = ret_type {
            let type_ref = TypeRef::from_ast_opt(ret_type.type_ref());
            bindings.push((name::OUTPUT_TYPE, type_ref))
        }
        if args.is_empty() && bindings.is_empty() {
            None
        } else {
            Some(GenericArgs { args, has_self_type: false, bindings })
        }
    }

    pub(crate) fn empty() -> GenericArgs {
        GenericArgs { args: Vec::new(), has_self_type: false, bindings: Vec::new() }
    }
}

impl From<Name> for Path {
    fn from(name: Name) -> Path {
        Path::from_simple_segments(PathKind::Plain, iter::once(name))
    }
}

fn expand_use_tree(
    prefix: Option<Path>,
    tree: ast::UseTree,
    cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
) {
    if let Some(use_tree_list) = tree.use_tree_list() {
        let prefix = match tree.path() {
            // E.g. use something::{{{inner}}};
            None => prefix,
            // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
            // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
            Some(path) => match convert_path(prefix, path) {
                Some(it) => Some(it),
                None => return, // FIXME: report errors somewhere
            },
        };
        for child_tree in use_tree_list.use_trees() {
            expand_use_tree(prefix.clone(), child_tree, cb);
        }
    } else {
        let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
        if let Some(ast_path) = tree.path() {
            // Handle self in a path.
            // E.g. `use something::{self, <...>}`
            if ast_path.qualifier().is_none() {
                if let Some(segment) = ast_path.segment() {
                    if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
                        if let Some(prefix) = prefix {
                            cb(prefix, &tree, false, alias);
                            return;
                        }
                    }
                }
            }
            if let Some(path) = convert_path(prefix, ast_path) {
                let is_glob = tree.has_star();
                cb(path, &tree, is_glob, alias)
            }
            // FIXME: report errors somewhere
            // We get here if we do
        }
    }
}

fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
    let prefix =
        if let Some(qual) = path.qualifier() { Some(convert_path(prefix, qual)?) } else { prefix };
    let segment = path.segment()?;
    let res = match segment.kind()? {
        ast::PathSegmentKind::Name(name) => {
            // no type args in use
            let mut res = prefix
                .unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
            res.segments.push(PathSegment {
                name: name.as_name(),
                args_and_bindings: None, // no type args in use
            });
            res
        }
        ast::PathSegmentKind::CrateKw => {
            if prefix.is_some() {
                return None;
            }
            Path::from_simple_segments(PathKind::Crate, iter::empty())
        }
        ast::PathSegmentKind::SelfKw => {
            if prefix.is_some() {
                return None;
            }
            Path::from_simple_segments(PathKind::Self_, iter::empty())
        }
        ast::PathSegmentKind::SuperKw => {
            if prefix.is_some() {
                return None;
            }
            Path::from_simple_segments(PathKind::Super, iter::empty())
        }
        ast::PathSegmentKind::Type { .. } => {
            // not allowed in imports
            return None;
        }
    };
    Some(res)
}

pub mod known {
    use super::{Path, PathKind};
    use crate::name;

    pub fn std_iter_into_iterator() -> Path {
        Path::from_simple_segments(
            PathKind::Abs,
            vec![name::STD, name::ITER, name::INTO_ITERATOR_TYPE],
        )
    }

    pub fn std_ops_try() -> Path {
        Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE])
    }

    pub fn std_result_result() -> Path {
        Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE])
    }

    pub fn std_future_future() -> Path {
        Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::FUTURE, name::FUTURE_TYPE])
    }

    pub fn std_boxed_box() -> Path {
        Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::BOXED, name::BOX_TYPE])
    }
}