aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/path
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/path')
-rw-r--r--crates/ra_hir_def/src/path/lower.rs176
-rw-r--r--crates/ra_hir_def/src/path/lower/lower_use.rs (renamed from crates/ra_hir_def/src/path/lower_use.rs)23
2 files changed, 186 insertions, 13 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
3mod lower_use;
4
5use std::sync::Arc;
6
7use either::Either;
8use hir_expand::{
9 hygiene::Hygiene,
10 name::{name, AsName},
11};
12use ra_syntax::ast::{self, AstNode, TypeAscriptionOwner};
13
14use crate::{
15 path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
16 type_ref::TypeRef,
17};
18
19pub(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.
23pub(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
128pub(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>`).
152fn 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}
diff --git a/crates/ra_hir_def/src/path/lower_use.rs b/crates/ra_hir_def/src/path/lower/lower_use.rs
index e2e1f716d..ea3fdb56c 100644
--- a/crates/ra_hir_def/src/path/lower_use.rs
+++ b/crates/ra_hir_def/src/path/lower/lower_use.rs
@@ -10,13 +10,13 @@ use hir_expand::{
10}; 10};
11use ra_syntax::ast::{self, NameOwner}; 11use ra_syntax::ast::{self, NameOwner};
12 12
13use crate::path::{Path, PathKind, PathSegment}; 13use crate::path::{ModPath, PathKind};
14 14
15pub(crate) fn lower_use_tree( 15pub(crate) fn lower_use_tree(
16 prefix: Option<Path>, 16 prefix: Option<ModPath>,
17 tree: ast::UseTree, 17 tree: ast::UseTree,
18 hygiene: &Hygiene, 18 hygiene: &Hygiene,
19 cb: &mut dyn FnMut(Path, &ast::UseTree, bool, Option<Name>), 19 cb: &mut dyn FnMut(ModPath, &ast::UseTree, bool, Option<Name>),
20) { 20) {
21 if let Some(use_tree_list) = tree.use_tree_list() { 21 if let Some(use_tree_list) = tree.use_tree_list() {
22 let prefix = match tree.path() { 22 let prefix = match tree.path() {
@@ -57,7 +57,7 @@ pub(crate) fn lower_use_tree(
57 } 57 }
58} 58}
59 59
60fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Option<Path> { 60fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
61 let prefix = if let Some(qual) = path.qualifier() { 61 let prefix = if let Some(qual) = path.qualifier() {
62 Some(convert_path(prefix, qual, hygiene)?) 62 Some(convert_path(prefix, qual, hygiene)?)
63 } else { 63 } else {
@@ -70,18 +70,15 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
70 match hygiene.name_ref_to_name(name_ref) { 70 match hygiene.name_ref_to_name(name_ref) {
71 Either::Left(name) => { 71 Either::Left(name) => {
72 // no type args in use 72 // no type args in use
73 let mut res = prefix.unwrap_or_else(|| Path { 73 let mut res = prefix.unwrap_or_else(|| ModPath {
74 kind: PathKind::Plain, 74 kind: PathKind::Plain,
75 segments: Vec::with_capacity(1), 75 segments: Vec::with_capacity(1),
76 }); 76 });
77 res.segments.push(PathSegment { 77 res.segments.push(name);
78 name,
79 args_and_bindings: None, // no type args in use
80 });
81 res 78 res
82 } 79 }
83 Either::Right(crate_id) => { 80 Either::Right(crate_id) => {
84 return Some(Path::from_simple_segments( 81 return Some(ModPath::from_simple_segments(
85 PathKind::DollarCrate(crate_id), 82 PathKind::DollarCrate(crate_id),
86 iter::empty(), 83 iter::empty(),
87 )) 84 ))
@@ -92,19 +89,19 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
92 if prefix.is_some() { 89 if prefix.is_some() {
93 return None; 90 return None;
94 } 91 }
95 Path::from_simple_segments(PathKind::Crate, iter::empty()) 92 ModPath::from_simple_segments(PathKind::Crate, iter::empty())
96 } 93 }
97 ast::PathSegmentKind::SelfKw => { 94 ast::PathSegmentKind::SelfKw => {
98 if prefix.is_some() { 95 if prefix.is_some() {
99 return None; 96 return None;
100 } 97 }
101 Path::from_simple_segments(PathKind::Self_, iter::empty()) 98 ModPath::from_simple_segments(PathKind::Self_, iter::empty())
102 } 99 }
103 ast::PathSegmentKind::SuperKw => { 100 ast::PathSegmentKind::SuperKw => {
104 if prefix.is_some() { 101 if prefix.is_some() {
105 return None; 102 return None;
106 } 103 }
107 Path::from_simple_segments(PathKind::Super, iter::empty()) 104 ModPath::from_simple_segments(PathKind::Super, iter::empty())
108 } 105 }
109 ast::PathSegmentKind::Type { .. } => { 106 ast::PathSegmentKind::Type { .. } => {
110 // not allowed in imports 107 // not allowed in imports