diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-14 18:16:48 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-14 18:16:48 +0000 |
commit | d6223253b628b279f9ddae8f83f7173d01f6b32c (patch) | |
tree | 12f2ccc5c52c1c0b67932d3a35fe18668b8e5d62 /crates/ra_hir_def/src/path/lower.rs | |
parent | 202ad1e2d9376565cb273cf085be600ed10e5a93 (diff) | |
parent | 2619950b3b405324ab1c1745876165c834b3b4b9 (diff) |
Merge #2561
2561: Split generic and non-generic paths r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/path/lower.rs')
-rw-r--r-- | crates/ra_hir_def/src/path/lower.rs | 176 |
1 files changed, 176 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..a2e995198 --- /dev/null +++ b/crates/ra_hir_def/src/path/lower.rs | |||
@@ -0,0 +1,176 @@ | |||
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 segments = Vec::new(); | ||
26 | let mut generic_args = Vec::new(); | ||
27 | loop { | ||
28 | let segment = path.segment()?; | ||
29 | |||
30 | if segment.has_colon_colon() { | ||
31 | kind = PathKind::Abs; | ||
32 | } | ||
33 | |||
34 | match segment.kind()? { | ||
35 | ast::PathSegmentKind::Name(name_ref) => { | ||
36 | // FIXME: this should just return name | ||
37 | match hygiene.name_ref_to_name(name_ref) { | ||
38 | Either::Left(name) => { | ||
39 | let args = segment | ||
40 | .type_arg_list() | ||
41 | .and_then(lower_generic_args) | ||
42 | .or_else(|| { | ||
43 | lower_generic_args_from_fn_path( | ||
44 | segment.param_list(), | ||
45 | segment.ret_type(), | ||
46 | ) | ||
47 | }) | ||
48 | .map(Arc::new); | ||
49 | segments.push(name); | ||
50 | generic_args.push(args) | ||
51 | } | ||
52 | Either::Right(crate_id) => { | ||
53 | kind = PathKind::DollarCrate(crate_id); | ||
54 | break; | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | ast::PathSegmentKind::Type { type_ref, trait_ref } => { | ||
59 | assert!(path.qualifier().is_none()); // this can only occur at the first segment | ||
60 | |||
61 | let self_type = TypeRef::from_ast(type_ref?); | ||
62 | |||
63 | match trait_ref { | ||
64 | // <T>::foo | ||
65 | None => { | ||
66 | kind = PathKind::Type(Box::new(self_type)); | ||
67 | } | ||
68 | // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo | ||
69 | Some(trait_ref) => { | ||
70 | let path = Path::from_src(trait_ref.path()?, hygiene)?; | ||
71 | kind = path.mod_path.kind; | ||
72 | |||
73 | let mut prefix_segments = path.mod_path.segments; | ||
74 | prefix_segments.reverse(); | ||
75 | segments.extend(prefix_segments); | ||
76 | |||
77 | let mut prefix_args = path.generic_args; | ||
78 | prefix_args.reverse(); | ||
79 | generic_args.extend(prefix_args); | ||
80 | |||
81 | // Insert the type reference (T in the above example) as Self parameter for the trait | ||
82 | let last_segment = generic_args.last_mut()?; | ||
83 | if last_segment.is_none() { | ||
84 | *last_segment = Some(Arc::new(GenericArgs::empty())); | ||
85 | }; | ||
86 | let args = last_segment.as_mut().unwrap(); | ||
87 | let mut args_inner = Arc::make_mut(args); | ||
88 | args_inner.has_self_type = true; | ||
89 | args_inner.args.insert(0, GenericArg::Type(self_type)); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | ast::PathSegmentKind::CrateKw => { | ||
94 | kind = PathKind::Crate; | ||
95 | break; | ||
96 | } | ||
97 | ast::PathSegmentKind::SelfKw => { | ||
98 | kind = PathKind::Self_; | ||
99 | break; | ||
100 | } | ||
101 | ast::PathSegmentKind::SuperKw => { | ||
102 | kind = PathKind::Super; | ||
103 | break; | ||
104 | } | ||
105 | } | ||
106 | path = match qualifier(&path) { | ||
107 | Some(it) => it, | ||
108 | None => break, | ||
109 | }; | ||
110 | } | ||
111 | segments.reverse(); | ||
112 | generic_args.reverse(); | ||
113 | let mod_path = ModPath { kind, segments }; | ||
114 | return Some(Path { mod_path, generic_args }); | ||
115 | |||
116 | fn qualifier(path: &ast::Path) -> Option<ast::Path> { | ||
117 | if let Some(q) = path.qualifier() { | ||
118 | return Some(q); | ||
119 | } | ||
120 | // FIXME: this bottom up traversal is not too precise. | ||
121 | // Should we handle do a top-down analysis, recording results? | ||
122 | let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; | ||
123 | let use_tree = use_tree_list.parent_use_tree(); | ||
124 | use_tree.path() | ||
125 | } | ||
126 | } | ||
127 | |||
128 | pub(super) fn lower_generic_args(node: ast::TypeArgList) -> Option<GenericArgs> { | ||
129 | let mut args = Vec::new(); | ||
130 | for type_arg in node.type_args() { | ||
131 | let type_ref = TypeRef::from_ast_opt(type_arg.type_ref()); | ||
132 | args.push(GenericArg::Type(type_ref)); | ||
133 | } | ||
134 | // lifetimes ignored for now | ||
135 | let mut bindings = Vec::new(); | ||
136 | for assoc_type_arg in node.assoc_type_args() { | ||
137 | if let Some(name_ref) = assoc_type_arg.name_ref() { | ||
138 | let name = name_ref.as_name(); | ||
139 | let type_ref = TypeRef::from_ast_opt(assoc_type_arg.type_ref()); | ||
140 | bindings.push((name, type_ref)); | ||
141 | } | ||
142 | } | ||
143 | if args.is_empty() && bindings.is_empty() { | ||
144 | None | ||
145 | } else { | ||
146 | Some(GenericArgs { args, has_self_type: false, bindings }) | ||
147 | } | ||
148 | } | ||
149 | |||
150 | /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y) | ||
151 | /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`). | ||
152 | fn lower_generic_args_from_fn_path( | ||
153 | params: Option<ast::ParamList>, | ||
154 | ret_type: Option<ast::RetType>, | ||
155 | ) -> Option<GenericArgs> { | ||
156 | let mut args = Vec::new(); | ||
157 | let mut bindings = Vec::new(); | ||
158 | if let Some(params) = params { | ||
159 | let mut param_types = Vec::new(); | ||
160 | for param in params.params() { | ||
161 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
162 | param_types.push(type_ref); | ||
163 | } | ||
164 | let arg = GenericArg::Type(TypeRef::Tuple(param_types)); | ||
165 | args.push(arg); | ||
166 | } | ||
167 | if let Some(ret_type) = ret_type { | ||
168 | let type_ref = TypeRef::from_ast_opt(ret_type.type_ref()); | ||
169 | bindings.push((name![Output], type_ref)) | ||
170 | } | ||
171 | if args.is_empty() && bindings.is_empty() { | ||
172 | None | ||
173 | } else { | ||
174 | Some(GenericArgs { args, has_self_type: false, bindings }) | ||
175 | } | ||
176 | } | ||