diff options
Diffstat (limited to 'crates/ra_hir_def/src/path/lower.rs')
-rw-r--r-- | crates/ra_hir_def/src/path/lower.rs | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/path/lower.rs b/crates/ra_hir_def/src/path/lower.rs new file mode 100644 index 000000000..62aafd508 --- /dev/null +++ b/crates/ra_hir_def/src/path/lower.rs | |||
@@ -0,0 +1,178 @@ | |||
1 | //! Transforms syntax into `Path` objects, ideally with accounting for hygiene | ||
2 | |||
3 | mod lower_use; | ||
4 | |||
5 | use std::sync::Arc; | ||
6 | |||
7 | use either::Either; | ||
8 | use hir_expand::{ | ||
9 | hygiene::Hygiene, | ||
10 | name::{name, AsName}, | ||
11 | }; | ||
12 | use ra_syntax::ast::{self, AstNode, TypeAscriptionOwner}; | ||
13 | |||
14 | use crate::{ | ||
15 | path::{GenericArg, GenericArgs, ModPath, Path, PathKind}, | ||
16 | type_ref::TypeRef, | ||
17 | }; | ||
18 | |||
19 | pub(super) use lower_use::lower_use_tree; | ||
20 | |||
21 | /// Converts an `ast::Path` to `Path`. Works with use trees. | ||
22 | /// It correctly handles `$crate` based path from macro call. | ||
23 | pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> { | ||
24 | let mut kind = PathKind::Plain; | ||
25 | let mut type_anchor = None; | ||
26 | let mut segments = Vec::new(); | ||
27 | let mut generic_args = Vec::new(); | ||
28 | loop { | ||
29 | let segment = path.segment()?; | ||
30 | |||
31 | if segment.has_colon_colon() { | ||
32 | kind = PathKind::Abs; | ||
33 | } | ||
34 | |||
35 | match segment.kind()? { | ||
36 | ast::PathSegmentKind::Name(name_ref) => { | ||
37 | // FIXME: this should just return name | ||
38 | match hygiene.name_ref_to_name(name_ref) { | ||
39 | Either::Left(name) => { | ||
40 | let args = segment | ||
41 | .type_arg_list() | ||
42 | .and_then(lower_generic_args) | ||
43 | .or_else(|| { | ||
44 | lower_generic_args_from_fn_path( | ||
45 | segment.param_list(), | ||
46 | segment.ret_type(), | ||
47 | ) | ||
48 | }) | ||
49 | .map(Arc::new); | ||
50 | segments.push(name); | ||
51 | generic_args.push(args) | ||
52 | } | ||
53 | Either::Right(crate_id) => { | ||
54 | kind = PathKind::DollarCrate(crate_id); | ||
55 | break; | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | ast::PathSegmentKind::Type { type_ref, trait_ref } => { | ||
60 | assert!(path.qualifier().is_none()); // this can only occur at the first segment | ||
61 | |||
62 | let self_type = TypeRef::from_ast(type_ref?); | ||
63 | |||
64 | match trait_ref { | ||
65 | // <T>::foo | ||
66 | None => { | ||
67 | type_anchor = Some(Box::new(self_type)); | ||
68 | kind = PathKind::Plain; | ||
69 | } | ||
70 | // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo | ||
71 | Some(trait_ref) => { | ||
72 | let path = Path::from_src(trait_ref.path()?, hygiene)?; | ||
73 | kind = path.mod_path.kind; | ||
74 | |||
75 | let mut prefix_segments = path.mod_path.segments; | ||
76 | prefix_segments.reverse(); | ||
77 | segments.extend(prefix_segments); | ||
78 | |||
79 | let mut prefix_args = path.generic_args; | ||
80 | prefix_args.reverse(); | ||
81 | generic_args.extend(prefix_args); | ||
82 | |||
83 | // Insert the type reference (T in the above example) as Self parameter for the trait | ||
84 | let last_segment = generic_args.last_mut()?; | ||
85 | if last_segment.is_none() { | ||
86 | *last_segment = Some(Arc::new(GenericArgs::empty())); | ||
87 | }; | ||
88 | let args = last_segment.as_mut().unwrap(); | ||
89 | let mut args_inner = Arc::make_mut(args); | ||
90 | args_inner.has_self_type = true; | ||
91 | args_inner.args.insert(0, GenericArg::Type(self_type)); | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | ast::PathSegmentKind::CrateKw => { | ||
96 | kind = PathKind::Crate; | ||
97 | break; | ||
98 | } | ||
99 | ast::PathSegmentKind::SelfKw => { | ||
100 | kind = PathKind::Super(0); | ||
101 | break; | ||
102 | } | ||
103 | ast::PathSegmentKind::SuperKw => { | ||
104 | kind = PathKind::Super(1); | ||
105 | break; | ||
106 | } | ||
107 | } | ||
108 | path = match qualifier(&path) { | ||
109 | Some(it) => it, | ||
110 | None => break, | ||
111 | }; | ||
112 | } | ||
113 | segments.reverse(); | ||
114 | generic_args.reverse(); | ||
115 | let mod_path = ModPath { kind, segments }; | ||
116 | return Some(Path { type_anchor, mod_path, generic_args }); | ||
117 | |||
118 | fn qualifier(path: &ast::Path) -> Option<ast::Path> { | ||
119 | if let Some(q) = path.qualifier() { | ||
120 | return Some(q); | ||
121 | } | ||
122 | // FIXME: this bottom up traversal is not too precise. | ||
123 | // Should we handle do a top-down analysis, recording results? | ||
124 | let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; | ||
125 | let use_tree = use_tree_list.parent_use_tree(); | ||
126 | use_tree.path() | ||
127 | } | ||
128 | } | ||
129 | |||
130 | pub(super) fn lower_generic_args(node: ast::TypeArgList) -> Option<GenericArgs> { | ||
131 | let mut args = Vec::new(); | ||
132 | for type_arg in node.type_args() { | ||
133 | let type_ref = TypeRef::from_ast_opt(type_arg.type_ref()); | ||
134 | args.push(GenericArg::Type(type_ref)); | ||
135 | } | ||
136 | // lifetimes ignored for now | ||
137 | let mut bindings = Vec::new(); | ||
138 | for assoc_type_arg in node.assoc_type_args() { | ||
139 | if let Some(name_ref) = assoc_type_arg.name_ref() { | ||
140 | let name = name_ref.as_name(); | ||
141 | let type_ref = TypeRef::from_ast_opt(assoc_type_arg.type_ref()); | ||
142 | bindings.push((name, type_ref)); | ||
143 | } | ||
144 | } | ||
145 | if args.is_empty() && bindings.is_empty() { | ||
146 | None | ||
147 | } else { | ||
148 | Some(GenericArgs { args, has_self_type: false, bindings }) | ||
149 | } | ||
150 | } | ||
151 | |||
152 | /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y) | ||
153 | /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`). | ||
154 | fn lower_generic_args_from_fn_path( | ||
155 | params: Option<ast::ParamList>, | ||
156 | ret_type: Option<ast::RetType>, | ||
157 | ) -> Option<GenericArgs> { | ||
158 | let mut args = Vec::new(); | ||
159 | let mut bindings = Vec::new(); | ||
160 | if let Some(params) = params { | ||
161 | let mut param_types = Vec::new(); | ||
162 | for param in params.params() { | ||
163 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
164 | param_types.push(type_ref); | ||
165 | } | ||
166 | let arg = GenericArg::Type(TypeRef::Tuple(param_types)); | ||
167 | args.push(arg); | ||
168 | } | ||
169 | if let Some(ret_type) = ret_type { | ||
170 | let type_ref = TypeRef::from_ast_opt(ret_type.type_ref()); | ||
171 | bindings.push((name![Output], type_ref)) | ||
172 | } | ||
173 | if args.is_empty() && bindings.is_empty() { | ||
174 | None | ||
175 | } else { | ||
176 | Some(GenericArgs { args, has_self_type: false, bindings }) | ||
177 | } | ||
178 | } | ||