aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/path/lower.rs
blob: 6a0c019fdff9862530ac35d254186e742a871254 (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
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene

mod lower_use;

use std::sync::Arc;

use either::Either;
use hir_expand::{
    hygiene::Hygiene,
    name::{name, AsName},
};
use ra_syntax::ast::{self, AstNode, TypeAscriptionOwner, TypeBoundsOwner};

use super::AssociatedTypeBinding;
use crate::{
    body::LowerCtx,
    path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
    type_ref::{TypeBound, TypeRef},
};

pub(super) use lower_use::lower_use_tree;

/// Converts an `ast::Path` to `Path`. Works with use trees.
/// It correctly handles `$crate` based path from macro call.
pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
    let mut kind = PathKind::Plain;
    let mut type_anchor = None;
    let mut segments = Vec::new();
    let mut generic_args = Vec::new();
    let ctx = LowerCtx::with_hygiene(hygiene);
    loop {
        let segment = path.segment()?;

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

        match segment.kind()? {
            ast::PathSegmentKind::Name(name_ref) => {
                // FIXME: this should just return name
                match hygiene.name_ref_to_name(name_ref) {
                    Either::Left(name) => {
                        let args = segment
                            .type_arg_list()
                            .and_then(|it| lower_generic_args(&ctx, it))
                            .or_else(|| {
                                lower_generic_args_from_fn_path(
                                    &ctx,
                                    segment.param_list(),
                                    segment.ret_type(),
                                )
                            })
                            .map(Arc::new);
                        segments.push(name);
                        generic_args.push(args)
                    }
                    Either::Right(crate_id) => {
                        kind = PathKind::DollarCrate(crate_id);
                        break;
                    }
                }
            }
            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(&ctx, type_ref?);

                match trait_ref {
                    // <T>::foo
                    None => {
                        type_anchor = Some(Box::new(self_type));
                        kind = PathKind::Plain;
                    }
                    // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
                    Some(trait_ref) => {
                        let path = Path::from_src(trait_ref.path()?, hygiene)?;
                        kind = path.mod_path.kind;

                        let mut prefix_segments = path.mod_path.segments;
                        prefix_segments.reverse();
                        segments.extend(prefix_segments);

                        let mut prefix_args = path.generic_args;
                        prefix_args.reverse();
                        generic_args.extend(prefix_args);

                        // Insert the type reference (T in the above example) as Self parameter for the trait
                        let last_segment = generic_args.last_mut()?;
                        if last_segment.is_none() {
                            *last_segment = Some(Arc::new(GenericArgs::empty()));
                        };
                        let args = last_segment.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::Super(0);
                break;
            }
            ast::PathSegmentKind::SuperKw => {
                let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 };
                kind = PathKind::Super(nested_super_count + 1);
            }
        }
        path = match qualifier(&path) {
            Some(it) => it,
            None => break,
        };
    }
    segments.reverse();
    generic_args.reverse();

    // handle local_inner_macros :
    // Basically, even in rustc it is quite hacky:
    // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
    // We follow what it did anyway :)
    if segments.len() == 1 && kind == PathKind::Plain {
        if let Some(macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
            if macro_call.is_bang() {
                if let Some(crate_id) = hygiene.local_inner_macros() {
                    kind = PathKind::DollarCrate(crate_id);
                }
            }
        }
    }

    let mod_path = ModPath { kind, segments };
    return Some(Path { type_anchor, mod_path, generic_args });

    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()
    }
}

pub(super) fn lower_generic_args(
    lower_ctx: &LowerCtx,
    node: ast::TypeArgList,
) -> Option<GenericArgs> {
    let mut args = Vec::new();
    for type_arg in node.type_args() {
        let type_ref = TypeRef::from_ast_opt(lower_ctx, 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() {
        let assoc_type_arg: ast::AssocTypeArg = assoc_type_arg;
        if let Some(name_ref) = assoc_type_arg.name_ref() {
            let name = name_ref.as_name();
            let type_ref = assoc_type_arg.type_ref().map(|it| TypeRef::from_ast(lower_ctx, it));
            let bounds = if let Some(l) = assoc_type_arg.type_bound_list() {
                l.bounds().map(|it| TypeBound::from_ast(lower_ctx, it)).collect()
            } else {
                Vec::new()
            };
            bindings.push(AssociatedTypeBinding { name, type_ref, bounds });
        }
    }
    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>`).
fn lower_generic_args_from_fn_path(
    ctx: &LowerCtx,
    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(&ctx, 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(&ctx, ret_type.type_ref());
        bindings.push(AssociatedTypeBinding {
            name: name![Output],
            type_ref: Some(type_ref),
            bounds: Vec::new(),
        });
    }
    if args.is_empty() && bindings.is_empty() {
        None
    } else {
        Some(GenericArgs { args, has_self_type: false, bindings })
    }
}