aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/attr.rs91
-rw-r--r--crates/ra_hir/src/code_model.rs30
-rw-r--r--crates/ra_hir/src/db.rs17
-rw-r--r--crates/ra_hir/src/either.rs55
-rw-r--r--crates/ra_hir/src/from_source.rs42
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir/src/marks.rs1
-rw-r--r--crates/ra_hir/src/mock.rs1
-rw-r--r--crates/ra_hir/src/name.rs143
-rw-r--r--crates/ra_hir/src/nameres.rs11
-rw-r--r--crates/ra_hir/src/nameres/collector.rs3
-rw-r--r--crates/ra_hir/src/nameres/tests/mod_resolution.rs2
-rw-r--r--crates/ra_hir/src/path.rs423
-rw-r--r--crates/ra_hir/src/type_ref.rs163
-rw-r--r--crates/ra_hir_def/Cargo.toml7
-rw-r--r--crates/ra_hir_def/src/attr.rs91
-rw-r--r--crates/ra_hir_def/src/db.rs18
-rw-r--r--crates/ra_hir_def/src/either.rs54
-rw-r--r--crates/ra_hir_def/src/lib.rs95
-rw-r--r--crates/ra_hir_def/src/name.rs142
-rw-r--r--crates/ra_hir_def/src/nameres.rs1
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs (renamed from crates/ra_hir/src/nameres/raw.rs)62
-rw-r--r--crates/ra_hir_def/src/path.rs423
-rw-r--r--crates/ra_hir_def/src/type_ref.rs162
-rw-r--r--crates/ra_hir_expand/src/lib.rs8
25 files changed, 1052 insertions, 998 deletions
diff --git a/crates/ra_hir/src/attr.rs b/crates/ra_hir/src/attr.rs
index bd159a566..988a671b8 100644
--- a/crates/ra_hir/src/attr.rs
+++ b/crates/ra_hir/src/attr.rs
@@ -1,90 +1 @@
1//! A higher level attributes based on TokenTree, with also some shortcuts. pub use hir_def::attr::*;
2
3use std::sync::Arc;
4
5use mbe::ast_to_token_tree;
6use ra_cfg::CfgOptions;
7use ra_syntax::{
8 ast::{self, AstNode, AttrsOwner},
9 SmolStr,
10};
11use tt::Subtree;
12
13use crate::{db::AstDatabase, path::Path, HirFileId, Source};
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub(crate) struct Attr {
17 pub(crate) path: Path,
18 pub(crate) input: Option<AttrInput>,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum AttrInput {
23 Literal(SmolStr),
24 TokenTree(Subtree),
25}
26
27impl Attr {
28 pub(crate) fn from_src(
29 Source { file_id, ast }: Source<ast::Attr>,
30 db: &impl AstDatabase,
31 ) -> Option<Attr> {
32 let path = Path::from_src(Source { file_id, ast: ast.path()? }, db)?;
33 let input = match ast.input() {
34 None => None,
35 Some(ast::AttrInput::Literal(lit)) => {
36 // FIXME: escape? raw string?
37 let value = lit.syntax().first_token()?.text().trim_matches('"').into();
38 Some(AttrInput::Literal(value))
39 }
40 Some(ast::AttrInput::TokenTree(tt)) => {
41 Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
42 }
43 };
44
45 Some(Attr { path, input })
46 }
47
48 pub(crate) fn from_attrs_owner(
49 file_id: HirFileId,
50 owner: &dyn AttrsOwner,
51 db: &impl AstDatabase,
52 ) -> Option<Arc<[Attr]>> {
53 let mut attrs = owner.attrs().peekable();
54 if attrs.peek().is_none() {
55 // Avoid heap allocation
56 return None;
57 }
58 Some(attrs.flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect())
59 }
60
61 pub(crate) fn is_simple_atom(&self, name: &str) -> bool {
62 // FIXME: Avoid cloning
63 self.path.as_ident().map_or(false, |s| s.to_string() == name)
64 }
65
66 // FIXME: handle cfg_attr :-)
67 pub(crate) fn as_cfg(&self) -> Option<&Subtree> {
68 if !self.is_simple_atom("cfg") {
69 return None;
70 }
71 match &self.input {
72 Some(AttrInput::TokenTree(subtree)) => Some(subtree),
73 _ => None,
74 }
75 }
76
77 pub(crate) fn as_path(&self) -> Option<&SmolStr> {
78 if !self.is_simple_atom("path") {
79 return None;
80 }
81 match &self.input {
82 Some(AttrInput::Literal(it)) => Some(it),
83 _ => None,
84 }
85 }
86
87 pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Option<bool> {
88 cfg_options.is_cfg_enabled(self.as_cfg()?)
89 }
90}
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 1a790b2f3..de1377aa4 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -6,7 +6,7 @@ pub(crate) mod docs;
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use hir_def::{CrateModuleId, ModuleId}; 8use hir_def::{CrateModuleId, ModuleId};
9use ra_db::{CrateId, Edition, FileId}; 9use ra_db::{CrateId, Edition};
10use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 10use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
11 11
12use crate::{ 12use crate::{
@@ -33,7 +33,7 @@ use crate::{
33 }, 33 },
34 type_ref::Mutability, 34 type_ref::Mutability,
35 type_ref::TypeRef, 35 type_ref::TypeRef,
36 AsName, AstId, Either, HasSource, Name, Ty, 36 AsName, Either, HasSource, Name, Ty,
37}; 37};
38 38
39/// hir::Crate describes a single crate. It's the main interface with which 39/// hir::Crate describes a single crate. It's the main interface with which
@@ -147,31 +147,7 @@ impl_froms!(
147 BuiltinType 147 BuiltinType
148); 148);
149 149
150pub enum ModuleSource { 150pub use hir_def::ModuleSource;
151 SourceFile(ast::SourceFile),
152 Module(ast::Module),
153}
154
155impl ModuleSource {
156 pub(crate) fn new(
157 db: &(impl DefDatabase + AstDatabase),
158 file_id: Option<FileId>,
159 decl_id: Option<AstId<ast::Module>>,
160 ) -> ModuleSource {
161 match (file_id, decl_id) {
162 (Some(file_id), _) => {
163 let source_file = db.parse(file_id).tree();
164 ModuleSource::SourceFile(source_file)
165 }
166 (None, Some(item_id)) => {
167 let module = item_id.to_node(db);
168 assert!(module.item_list().is_some(), "expected inline module");
169 ModuleSource::Module(module)
170 }
171 (None, None) => panic!(),
172 }
173 }
174}
175 151
176impl Module { 152impl Module {
177 pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module { 153 pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module {
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 8f6cb2da7..142d7338d 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -12,15 +12,15 @@ use crate::{
12 ids, 12 ids,
13 impl_block::{ImplBlock, ImplSourceMap, ModuleImplBlocks}, 13 impl_block::{ImplBlock, ImplSourceMap, ModuleImplBlocks},
14 lang_item::{LangItemTarget, LangItems}, 14 lang_item::{LangItemTarget, LangItems},
15 nameres::{CrateDefMap, ImportSourceMap, Namespace, RawItems}, 15 nameres::{CrateDefMap, Namespace},
16 traits::TraitData, 16 traits::TraitData,
17 ty::{ 17 ty::{
18 method_resolution::CrateImplBlocks, traits::Impl, CallableDef, FnSig, GenericPredicate, 18 method_resolution::CrateImplBlocks, traits::Impl, CallableDef, FnSig, GenericPredicate,
19 InferenceResult, Substs, Ty, TypableDef, TypeCtor, 19 InferenceResult, Substs, Ty, TypableDef, TypeCtor,
20 }, 20 },
21 type_alias::TypeAliasData, 21 type_alias::TypeAliasData,
22 Const, ConstData, Crate, DefWithBody, Enum, ExprScopes, FnData, Function, HirFileId, Module, 22 Const, ConstData, Crate, DefWithBody, Enum, ExprScopes, FnData, Function, Module, Static,
23 Static, Struct, StructField, Trait, TypeAlias, 23 Struct, StructField, Trait, TypeAlias,
24}; 24};
25 25
26pub use hir_def::db::{InternDatabase, InternDatabaseStorage}; 26pub use hir_def::db::{InternDatabase, InternDatabaseStorage};
@@ -32,7 +32,7 @@ pub use hir_expand::db::{
32// This database uses `AstDatabase` internally, 32// This database uses `AstDatabase` internally,
33#[salsa::query_group(DefDatabaseStorage)] 33#[salsa::query_group(DefDatabaseStorage)]
34#[salsa::requires(AstDatabase)] 34#[salsa::requires(AstDatabase)]
35pub trait DefDatabase: InternDatabase + HirDebugDatabase + AstDatabase { 35pub trait DefDatabase: HirDebugDatabase + hir_def::db::DefDatabase2 {
36 #[salsa::invoke(crate::adt::StructData::struct_data_query)] 36 #[salsa::invoke(crate::adt::StructData::struct_data_query)]
37 fn struct_data(&self, s: Struct) -> Arc<StructData>; 37 fn struct_data(&self, s: Struct) -> Arc<StructData>;
38 38
@@ -45,15 +45,6 @@ pub trait DefDatabase: InternDatabase + HirDebugDatabase + AstDatabase {
45 #[salsa::invoke(crate::traits::TraitItemsIndex::trait_items_index)] 45 #[salsa::invoke(crate::traits::TraitItemsIndex::trait_items_index)]
46 fn trait_items_index(&self, module: Module) -> crate::traits::TraitItemsIndex; 46 fn trait_items_index(&self, module: Module) -> crate::traits::TraitItemsIndex;
47 47
48 #[salsa::invoke(RawItems::raw_items_with_source_map_query)]
49 fn raw_items_with_source_map(
50 &self,
51 file_id: HirFileId,
52 ) -> (Arc<RawItems>, Arc<ImportSourceMap>);
53
54 #[salsa::invoke(RawItems::raw_items_query)]
55 fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
56
57 #[salsa::invoke(CrateDefMap::crate_def_map_query)] 48 #[salsa::invoke(CrateDefMap::crate_def_map_query)]
58 fn crate_def_map(&self, krate: Crate) -> Arc<CrateDefMap>; 49 fn crate_def_map(&self, krate: Crate) -> Arc<CrateDefMap>;
59 50
diff --git a/crates/ra_hir/src/either.rs b/crates/ra_hir/src/either.rs
index 83583ef8b..44498dd38 100644
--- a/crates/ra_hir/src/either.rs
+++ b/crates/ra_hir/src/either.rs
@@ -1,54 +1 @@
1//! FIXME: write short doc here pub use hir_def::either::*;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub enum Either<A, B> {
5 A(A),
6 B(B),
7}
8
9impl<A, B> Either<A, B> {
10 pub fn either<R, F1, F2>(self, f1: F1, f2: F2) -> R
11 where
12 F1: FnOnce(A) -> R,
13 F2: FnOnce(B) -> R,
14 {
15 match self {
16 Either::A(a) => f1(a),
17 Either::B(b) => f2(b),
18 }
19 }
20 pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V>
21 where
22 F1: FnOnce(A) -> U,
23 F2: FnOnce(B) -> V,
24 {
25 match self {
26 Either::A(a) => Either::A(f1(a)),
27 Either::B(b) => Either::B(f2(b)),
28 }
29 }
30 pub fn map_a<U, F>(self, f: F) -> Either<U, B>
31 where
32 F: FnOnce(A) -> U,
33 {
34 self.map(f, |it| it)
35 }
36 pub fn a(self) -> Option<A> {
37 match self {
38 Either::A(it) => Some(it),
39 Either::B(_) => None,
40 }
41 }
42 pub fn b(self) -> Option<B> {
43 match self {
44 Either::A(_) => None,
45 Either::B(it) => Some(it),
46 }
47 }
48 pub fn as_ref(&self) -> Either<&A, &B> {
49 match self {
50 Either::A(it) => Either::A(it),
51 Either::B(it) => Either::B(it),
52 }
53 }
54}
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 93713bb14..697c8dc84 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -1,11 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_db::{FileId, FilePosition}; 3use ra_syntax::ast::{self, AstNode, NameOwner};
4use ra_syntax::{
5 algo::find_node_at_offset,
6 ast::{self, AstNode, NameOwner},
7 SyntaxNode,
8};
9 4
10use crate::{ 5use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 6 db::{AstDatabase, DefDatabase, HirDatabase},
@@ -129,41 +124,6 @@ impl FromSource for StructField {
129 } 124 }
130} 125}
131 126
132// FIXME: simplify it
133impl ModuleSource {
134 pub fn from_position(
135 db: &(impl DefDatabase + AstDatabase),
136 position: FilePosition,
137 ) -> ModuleSource {
138 let parse = db.parse(position.file_id);
139 match &find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
140 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
141 _ => {
142 let source_file = parse.tree();
143 ModuleSource::SourceFile(source_file)
144 }
145 }
146 }
147
148 pub fn from_child_node(
149 db: &(impl DefDatabase + AstDatabase),
150 file_id: FileId,
151 child: &SyntaxNode,
152 ) -> ModuleSource {
153 if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) {
154 ModuleSource::Module(m)
155 } else {
156 let source_file = db.parse(file_id).tree();
157 ModuleSource::SourceFile(source_file)
158 }
159 }
160
161 pub fn from_file_id(db: &(impl DefDatabase + AstDatabase), file_id: FileId) -> ModuleSource {
162 let source_file = db.parse(file_id).tree();
163 ModuleSource::SourceFile(source_file)
164 }
165}
166
167impl Module { 127impl Module {
168 pub fn from_declaration(db: &impl HirDatabase, src: Source<ast::Module>) -> Option<Self> { 128 pub fn from_declaration(db: &impl HirDatabase, src: Source<ast::Module>) -> Option<Self> {
169 let src_parent = Source { 129 let src_parent = Source {
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 0f2d233bb..b49f615bf 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -59,10 +59,7 @@ pub mod from_source;
59#[cfg(test)] 59#[cfg(test)]
60mod marks; 60mod marks;
61 61
62use hir_expand::{ 62use hir_expand::AstId;
63 ast_id_map::{AstIdMap, FileAstId},
64 AstId,
65};
66 63
67use crate::{ids::MacroFileKind, name::AsName, resolve::Resolver}; 64use crate::{ids::MacroFileKind, name::AsName, resolve::Resolver};
68 65
diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs
index 79af24b20..b423489a1 100644
--- a/crates/ra_hir/src/marks.rs
+++ b/crates/ra_hir/src/marks.rs
@@ -2,6 +2,7 @@
2 2
3test_utils::marks!( 3test_utils::marks!(
4 bogus_paths 4 bogus_paths
5 // FIXME: restore this mark once hir is split
5 name_res_works_for_broken_modules 6 name_res_works_for_broken_modules
6 can_import_enum_variant 7 can_import_enum_variant
7 type_var_cycles_resolve_completely 8 type_var_cycles_resolve_completely
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 0b278deb3..bb2d78abe 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -17,6 +17,7 @@ use crate::{db, debug::HirDebugHelper, diagnostics::DiagnosticSink};
17pub const WORKSPACE: SourceRootId = SourceRootId(0); 17pub const WORKSPACE: SourceRootId = SourceRootId(0);
18 18
19#[salsa::database( 19#[salsa::database(
20 hir_def::db::DefDatabase2Storage,
20 ra_db::SourceDatabaseExtStorage, 21 ra_db::SourceDatabaseExtStorage,
21 ra_db::SourceDatabaseStorage, 22 ra_db::SourceDatabaseStorage,
22 db::InternDatabaseStorage, 23 db::InternDatabaseStorage,
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
index 1e0b8c350..cf66f88ad 100644
--- a/crates/ra_hir/src/name.rs
+++ b/crates/ra_hir/src/name.rs
@@ -1,142 +1 @@
1//! FIXME: write short doc here pub use hir_def::name::*;
2
3use std::fmt;
4
5use ra_syntax::{ast, SmolStr};
6
7/// `Name` is a wrapper around string, which is used in hir for both references
8/// and declarations. In theory, names should also carry hygiene info, but we are
9/// not there yet!
10#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
11pub struct Name(Repr);
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
14enum Repr {
15 Text(SmolStr),
16 TupleField(usize),
17}
18
19impl fmt::Display for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 match &self.0 {
22 Repr::Text(text) => fmt::Display::fmt(&text, f),
23 Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
24 }
25 }
26}
27
28impl Name {
29 /// Note: this is private to make creating name from random string hard.
30 /// Hopefully, this should allow us to integrate hygiene cleaner in the
31 /// future, and to switch to interned representation of names.
32 const fn new_text(text: SmolStr) -> Name {
33 Name(Repr::Text(text))
34 }
35
36 pub(crate) fn new_tuple_field(idx: usize) -> Name {
37 Name(Repr::TupleField(idx))
38 }
39
40 /// Shortcut to create inline plain text name
41 const fn new_inline_ascii(len: usize, text: &[u8]) -> Name {
42 Name::new_text(SmolStr::new_inline_from_ascii(len, text))
43 }
44
45 /// Resolve a name from the text of token.
46 fn resolve(raw_text: &SmolStr) -> Name {
47 let raw_start = "r#";
48 if raw_text.as_str().starts_with(raw_start) {
49 Name::new_text(SmolStr::new(&raw_text[raw_start.len()..]))
50 } else {
51 Name::new_text(raw_text.clone())
52 }
53 }
54
55 pub(crate) fn missing() -> Name {
56 Name::new_text("[missing name]".into())
57 }
58
59 pub(crate) fn as_tuple_index(&self) -> Option<usize> {
60 match self.0 {
61 Repr::TupleField(idx) => Some(idx),
62 _ => None,
63 }
64 }
65}
66
67pub(crate) trait AsName {
68 fn as_name(&self) -> Name;
69}
70
71impl AsName for ast::NameRef {
72 fn as_name(&self) -> Name {
73 match self.as_tuple_field() {
74 Some(idx) => Name::new_tuple_field(idx),
75 None => Name::resolve(self.text()),
76 }
77 }
78}
79
80impl AsName for ast::Name {
81 fn as_name(&self) -> Name {
82 Name::resolve(self.text())
83 }
84}
85
86impl AsName for ast::FieldKind {
87 fn as_name(&self) -> Name {
88 match self {
89 ast::FieldKind::Name(nr) => nr.as_name(),
90 ast::FieldKind::Index(idx) => Name::new_tuple_field(idx.text().parse().unwrap()),
91 }
92 }
93}
94
95impl AsName for ra_db::Dependency {
96 fn as_name(&self) -> Name {
97 Name::new_text(self.name.clone())
98 }
99}
100
101// Primitives
102pub(crate) const ISIZE: Name = Name::new_inline_ascii(5, b"isize");
103pub(crate) const I8: Name = Name::new_inline_ascii(2, b"i8");
104pub(crate) const I16: Name = Name::new_inline_ascii(3, b"i16");
105pub(crate) const I32: Name = Name::new_inline_ascii(3, b"i32");
106pub(crate) const I64: Name = Name::new_inline_ascii(3, b"i64");
107pub(crate) const I128: Name = Name::new_inline_ascii(4, b"i128");
108pub(crate) const USIZE: Name = Name::new_inline_ascii(5, b"usize");
109pub(crate) const U8: Name = Name::new_inline_ascii(2, b"u8");
110pub(crate) const U16: Name = Name::new_inline_ascii(3, b"u16");
111pub(crate) const U32: Name = Name::new_inline_ascii(3, b"u32");
112pub(crate) const U64: Name = Name::new_inline_ascii(3, b"u64");
113pub(crate) const U128: Name = Name::new_inline_ascii(4, b"u128");
114pub(crate) const F32: Name = Name::new_inline_ascii(3, b"f32");
115pub(crate) const F64: Name = Name::new_inline_ascii(3, b"f64");
116pub(crate) const BOOL: Name = Name::new_inline_ascii(4, b"bool");
117pub(crate) const CHAR: Name = Name::new_inline_ascii(4, b"char");
118pub(crate) const STR: Name = Name::new_inline_ascii(3, b"str");
119
120// Special names
121pub(crate) const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self");
122pub(crate) const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self");
123pub(crate) const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules");
124
125// Components of known path (value or mod name)
126pub(crate) const STD: Name = Name::new_inline_ascii(3, b"std");
127pub(crate) const ITER: Name = Name::new_inline_ascii(4, b"iter");
128pub(crate) const OPS: Name = Name::new_inline_ascii(3, b"ops");
129pub(crate) const FUTURE: Name = Name::new_inline_ascii(6, b"future");
130pub(crate) const RESULT: Name = Name::new_inline_ascii(6, b"result");
131pub(crate) const BOXED: Name = Name::new_inline_ascii(5, b"boxed");
132
133// Components of known path (type name)
134pub(crate) const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator");
135pub(crate) const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item");
136pub(crate) const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try");
137pub(crate) const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok");
138pub(crate) const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future");
139pub(crate) const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result");
140pub(crate) const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
141pub(crate) const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
142pub(crate) const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index b325979f5..39f585b44 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -48,7 +48,6 @@
48//! on the result 48//! on the result
49 49
50mod per_ns; 50mod per_ns;
51mod raw;
52mod collector; 51mod collector;
53mod mod_resolution; 52mod mod_resolution;
54#[cfg(test)] 53#[cfg(test)]
@@ -74,12 +73,9 @@ use crate::{
74 Trait, 73 Trait,
75}; 74};
76 75
77pub(crate) use self::raw::{ImportSourceMap, RawItems}; 76pub use self::per_ns::{Namespace, PerNs};
78 77
79pub use self::{ 78pub use hir_def::nameres::raw::ImportId;
80 per_ns::{Namespace, PerNs},
81 raw::ImportId,
82};
83 79
84/// Contains all top-level defs from a macro-expanded crate 80/// Contains all top-level defs from a macro-expanded crate
85#[derive(Debug, PartialEq, Eq)] 81#[derive(Debug, PartialEq, Eq)]
@@ -328,7 +324,8 @@ impl CrateDefMap {
328 ) -> ResolvePathResult { 324 ) -> ResolvePathResult {
329 let mut segments = path.segments.iter().enumerate(); 325 let mut segments = path.segments.iter().enumerate();
330 let mut curr_per_ns: PerNs = match path.kind { 326 let mut curr_per_ns: PerNs = match path.kind {
331 PathKind::DollarCrate(krate) => { 327 PathKind::DollarCrate(crate_id) => {
328 let krate = Crate { crate_id };
332 if krate == self.krate { 329 if krate == self.krate {
333 tested_by!(macro_dollar_crate_self); 330 tested_by!(macro_dollar_crate_self);
334 PerNs::types(Module::new(self.krate, self.root).into()) 331 PerNs::types(Module::new(self.krate, self.root).into())
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index a94a0554c..2dd0a5877 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -1,5 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::nameres::raw;
3use ra_cfg::CfgOptions; 4use ra_cfg::CfgOptions;
4use ra_db::FileId; 5use ra_db::FileId;
5use ra_syntax::{ast, SmolStr}; 6use ra_syntax::{ast, SmolStr};
@@ -12,7 +13,7 @@ use crate::{
12 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, 13 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
13 name::MACRO_RULES, 14 name::MACRO_RULES,
14 nameres::{ 15 nameres::{
15 diagnostics::DefDiagnostic, mod_resolution::ModDir, raw, Crate, CrateDefMap, CrateModuleId, 16 diagnostics::DefDiagnostic, mod_resolution::ModDir, Crate, CrateDefMap, CrateModuleId,
16 ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode, 17 ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode,
17 }, 18 },
18 Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, 19 Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static,
diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs
index f569aacdc..abfe8b1c3 100644
--- a/crates/ra_hir/src/nameres/tests/mod_resolution.rs
+++ b/crates/ra_hir/src/nameres/tests/mod_resolution.rs
@@ -2,7 +2,7 @@ use super::*;
2 2
3#[test] 3#[test]
4fn name_res_works_for_broken_modules() { 4fn name_res_works_for_broken_modules() {
5 covers!(name_res_works_for_broken_modules); 5 // covers!(name_res_works_for_broken_modules);
6 let map = def_map( 6 let map = def_map(
7 " 7 "
8 //- /lib.rs 8 //- /lib.rs
diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs
index bbe536bcb..7f0ff4bfc 100644
--- a/crates/ra_hir/src/path.rs
+++ b/crates/ra_hir/src/path.rs
@@ -1,422 +1 @@
1//! FIXME: write short doc here pub use hir_def::path::*;
2
3use std::{iter, sync::Arc};
4
5use ra_syntax::{
6 ast::{self, NameOwner, TypeAscriptionOwner},
7 AstNode,
8};
9
10use crate::{db::AstDatabase, name, type_ref::TypeRef, AsName, Crate, Name, Source};
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub struct Path {
14 pub kind: PathKind,
15 pub segments: Vec<PathSegment>,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub struct PathSegment {
20 pub name: Name,
21 pub args_and_bindings: Option<Arc<GenericArgs>>,
22}
23
24/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
25/// can (in the future) also include bindings of associated types, like in
26/// `Iterator<Item = Foo>`.
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct GenericArgs {
29 pub args: Vec<GenericArg>,
30 /// This specifies whether the args contain a Self type as the first
31 /// element. This is the case for path segments like `<T as Trait>`, where
32 /// `T` is actually a type parameter for the path `Trait` specifying the
33 /// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
34 /// is left out.
35 pub has_self_type: bool,
36 /// Associated type bindings like in `Iterator<Item = T>`.
37 pub bindings: Vec<(Name, TypeRef)>,
38}
39
40/// A single generic argument.
41#[derive(Debug, Clone, PartialEq, Eq, Hash)]
42pub enum GenericArg {
43 Type(TypeRef),
44 // or lifetime...
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, Hash)]
48pub enum PathKind {
49 Plain,
50 Self_,
51 Super,
52 Crate,
53 // Absolute path
54 Abs,
55 // Type based path like `<T>::foo`
56 Type(Box<TypeRef>),
57 // `$crate` from macro expansion
58 DollarCrate(Crate),
59}
60
61impl Path {
62 /// Calls `cb` with all paths, represented by this use item.
63 pub fn expand_use_item(
64 item_src: Source<ast::UseItem>,
65 db: &impl AstDatabase,
66 mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
67 ) {
68 if let Some(tree) = item_src.ast.use_tree() {
69 expand_use_tree(
70 None,
71 tree,
72 &|| item_src.file_id.macro_crate(db).map(|crate_id| Crate { crate_id }),
73 &mut cb,
74 );
75 }
76 }
77
78 pub fn from_simple_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> Path {
79 Path {
80 kind,
81 segments: segments
82 .into_iter()
83 .map(|name| PathSegment { name, args_and_bindings: None })
84 .collect(),
85 }
86 }
87
88 /// Converts an `ast::Path` to `Path`. Works with use trees.
89 /// DEPRECATED: It does not handle `$crate` from macro call.
90 pub fn from_ast(path: ast::Path) -> Option<Path> {
91 Path::parse(path, &|| None)
92 }
93
94 /// Converts an `ast::Path` to `Path`. Works with use trees.
95 /// It correctly handles `$crate` based path from macro call.
96 pub fn from_src(source: Source<ast::Path>, db: &impl AstDatabase) -> Option<Path> {
97 let file_id = source.file_id;
98 Path::parse(source.ast, &|| file_id.macro_crate(db).map(|crate_id| Crate { crate_id }))
99 }
100
101 fn parse(mut path: ast::Path, macro_crate: &impl Fn() -> Option<Crate>) -> Option<Path> {
102 let mut kind = PathKind::Plain;
103 let mut segments = Vec::new();
104 loop {
105 let segment = path.segment()?;
106
107 if segment.has_colon_colon() {
108 kind = PathKind::Abs;
109 }
110
111 match segment.kind()? {
112 ast::PathSegmentKind::Name(name) => {
113 if name.text() == "$crate" {
114 if let Some(macro_crate) = macro_crate() {
115 kind = PathKind::DollarCrate(macro_crate);
116 break;
117 }
118 }
119
120 let args = segment
121 .type_arg_list()
122 .and_then(GenericArgs::from_ast)
123 .or_else(|| {
124 GenericArgs::from_fn_like_path_ast(
125 segment.param_list(),
126 segment.ret_type(),
127 )
128 })
129 .map(Arc::new);
130 let segment = PathSegment { name: name.as_name(), args_and_bindings: args };
131 segments.push(segment);
132 }
133 ast::PathSegmentKind::Type { type_ref, trait_ref } => {
134 assert!(path.qualifier().is_none()); // this can only occur at the first segment
135
136 let self_type = TypeRef::from_ast(type_ref?);
137
138 match trait_ref {
139 // <T>::foo
140 None => {
141 kind = PathKind::Type(Box::new(self_type));
142 }
143 // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
144 Some(trait_ref) => {
145 let path = Path::parse(trait_ref.path()?, macro_crate)?;
146 kind = path.kind;
147 let mut prefix_segments = path.segments;
148 prefix_segments.reverse();
149 segments.extend(prefix_segments);
150 // Insert the type reference (T in the above example) as Self parameter for the trait
151 let mut last_segment = segments.last_mut()?;
152 if last_segment.args_and_bindings.is_none() {
153 last_segment.args_and_bindings =
154 Some(Arc::new(GenericArgs::empty()));
155 };
156 let args = last_segment.args_and_bindings.as_mut().unwrap();
157 let mut args_inner = Arc::make_mut(args);
158 args_inner.has_self_type = true;
159 args_inner.args.insert(0, GenericArg::Type(self_type));
160 }
161 }
162 }
163 ast::PathSegmentKind::CrateKw => {
164 kind = PathKind::Crate;
165 break;
166 }
167 ast::PathSegmentKind::SelfKw => {
168 kind = PathKind::Self_;
169 break;
170 }
171 ast::PathSegmentKind::SuperKw => {
172 kind = PathKind::Super;
173 break;
174 }
175 }
176 path = match qualifier(&path) {
177 Some(it) => it,
178 None => break,
179 };
180 }
181 segments.reverse();
182 return Some(Path { kind, segments });
183
184 fn qualifier(path: &ast::Path) -> Option<ast::Path> {
185 if let Some(q) = path.qualifier() {
186 return Some(q);
187 }
188 // FIXME: this bottom up traversal is not too precise.
189 // Should we handle do a top-down analysis, recording results?
190 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
191 let use_tree = use_tree_list.parent_use_tree();
192 use_tree.path()
193 }
194 }
195
196 /// Converts an `ast::NameRef` into a single-identifier `Path`.
197 pub fn from_name_ref(name_ref: &ast::NameRef) -> Path {
198 name_ref.as_name().into()
199 }
200
201 /// `true` is this path is a single identifier, like `foo`
202 pub fn is_ident(&self) -> bool {
203 self.kind == PathKind::Plain && self.segments.len() == 1
204 }
205
206 /// `true` if this path is just a standalone `self`
207 pub fn is_self(&self) -> bool {
208 self.kind == PathKind::Self_ && self.segments.is_empty()
209 }
210
211 /// If this path is a single identifier, like `foo`, return its name.
212 pub fn as_ident(&self) -> Option<&Name> {
213 if self.kind != PathKind::Plain || self.segments.len() > 1 {
214 return None;
215 }
216 self.segments.first().map(|s| &s.name)
217 }
218
219 pub fn expand_macro_expr(&self) -> Option<Name> {
220 self.as_ident().and_then(|name| Some(name.clone()))
221 }
222
223 pub fn is_type_relative(&self) -> bool {
224 match self.kind {
225 PathKind::Type(_) => true,
226 _ => false,
227 }
228 }
229}
230
231impl GenericArgs {
232 pub(crate) fn from_ast(node: ast::TypeArgList) -> Option<GenericArgs> {
233 let mut args = Vec::new();
234 for type_arg in node.type_args() {
235 let type_ref = TypeRef::from_ast_opt(type_arg.type_ref());
236 args.push(GenericArg::Type(type_ref));
237 }
238 // lifetimes ignored for now
239 let mut bindings = Vec::new();
240 for assoc_type_arg in node.assoc_type_args() {
241 if let Some(name_ref) = assoc_type_arg.name_ref() {
242 let name = name_ref.as_name();
243 let type_ref = TypeRef::from_ast_opt(assoc_type_arg.type_ref());
244 bindings.push((name, type_ref));
245 }
246 }
247 if args.is_empty() && bindings.is_empty() {
248 None
249 } else {
250 Some(GenericArgs { args, has_self_type: false, bindings })
251 }
252 }
253
254 /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
255 /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`).
256 pub(crate) fn from_fn_like_path_ast(
257 params: Option<ast::ParamList>,
258 ret_type: Option<ast::RetType>,
259 ) -> Option<GenericArgs> {
260 let mut args = Vec::new();
261 let mut bindings = Vec::new();
262 if let Some(params) = params {
263 let mut param_types = Vec::new();
264 for param in params.params() {
265 let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
266 param_types.push(type_ref);
267 }
268 let arg = GenericArg::Type(TypeRef::Tuple(param_types));
269 args.push(arg);
270 }
271 if let Some(ret_type) = ret_type {
272 let type_ref = TypeRef::from_ast_opt(ret_type.type_ref());
273 bindings.push((name::OUTPUT_TYPE, type_ref))
274 }
275 if args.is_empty() && bindings.is_empty() {
276 None
277 } else {
278 Some(GenericArgs { args, has_self_type: false, bindings })
279 }
280 }
281
282 pub(crate) fn empty() -> GenericArgs {
283 GenericArgs { args: Vec::new(), has_self_type: false, bindings: Vec::new() }
284 }
285}
286
287impl From<Name> for Path {
288 fn from(name: Name) -> Path {
289 Path::from_simple_segments(PathKind::Plain, iter::once(name))
290 }
291}
292
293fn expand_use_tree(
294 prefix: Option<Path>,
295 tree: ast::UseTree,
296 macro_crate: &impl Fn() -> Option<Crate>,
297 cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
298) {
299 if let Some(use_tree_list) = tree.use_tree_list() {
300 let prefix = match tree.path() {
301 // E.g. use something::{{{inner}}};
302 None => prefix,
303 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
304 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
305 Some(path) => match convert_path(prefix, path, macro_crate) {
306 Some(it) => Some(it),
307 None => return, // FIXME: report errors somewhere
308 },
309 };
310 for child_tree in use_tree_list.use_trees() {
311 expand_use_tree(prefix.clone(), child_tree, macro_crate, cb);
312 }
313 } else {
314 let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
315 if let Some(ast_path) = tree.path() {
316 // Handle self in a path.
317 // E.g. `use something::{self, <...>}`
318 if ast_path.qualifier().is_none() {
319 if let Some(segment) = ast_path.segment() {
320 if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
321 if let Some(prefix) = prefix {
322 cb(prefix, &tree, false, alias);
323 return;
324 }
325 }
326 }
327 }
328 if let Some(path) = convert_path(prefix, ast_path, macro_crate) {
329 let is_glob = tree.has_star();
330 cb(path, &tree, is_glob, alias)
331 }
332 // FIXME: report errors somewhere
333 // We get here if we do
334 }
335 }
336}
337
338fn convert_path(
339 prefix: Option<Path>,
340 path: ast::Path,
341 macro_crate: &impl Fn() -> Option<Crate>,
342) -> Option<Path> {
343 let prefix = if let Some(qual) = path.qualifier() {
344 Some(convert_path(prefix, qual, macro_crate)?)
345 } else {
346 prefix
347 };
348
349 let segment = path.segment()?;
350 let res = match segment.kind()? {
351 ast::PathSegmentKind::Name(name) => {
352 if name.text() == "$crate" {
353 if let Some(krate) = macro_crate() {
354 return Some(Path::from_simple_segments(
355 PathKind::DollarCrate(krate),
356 iter::empty(),
357 ));
358 }
359 }
360
361 // no type args in use
362 let mut res = prefix
363 .unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
364 res.segments.push(PathSegment {
365 name: name.as_name(),
366 args_and_bindings: None, // no type args in use
367 });
368 res
369 }
370 ast::PathSegmentKind::CrateKw => {
371 if prefix.is_some() {
372 return None;
373 }
374 Path::from_simple_segments(PathKind::Crate, iter::empty())
375 }
376 ast::PathSegmentKind::SelfKw => {
377 if prefix.is_some() {
378 return None;
379 }
380 Path::from_simple_segments(PathKind::Self_, iter::empty())
381 }
382 ast::PathSegmentKind::SuperKw => {
383 if prefix.is_some() {
384 return None;
385 }
386 Path::from_simple_segments(PathKind::Super, iter::empty())
387 }
388 ast::PathSegmentKind::Type { .. } => {
389 // not allowed in imports
390 return None;
391 }
392 };
393 Some(res)
394}
395
396pub mod known {
397 use super::{Path, PathKind};
398 use crate::name;
399
400 pub fn std_iter_into_iterator() -> Path {
401 Path::from_simple_segments(
402 PathKind::Abs,
403 vec![name::STD, name::ITER, name::INTO_ITERATOR_TYPE],
404 )
405 }
406
407 pub fn std_ops_try() -> Path {
408 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE])
409 }
410
411 pub fn std_result_result() -> Path {
412 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE])
413 }
414
415 pub fn std_future_future() -> Path {
416 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::FUTURE, name::FUTURE_TYPE])
417 }
418
419 pub fn std_boxed_box() -> Path {
420 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::BOXED, name::BOX_TYPE])
421 }
422}
diff --git a/crates/ra_hir/src/type_ref.rs b/crates/ra_hir/src/type_ref.rs
index 2cf06b250..bd56ddbe6 100644
--- a/crates/ra_hir/src/type_ref.rs
+++ b/crates/ra_hir/src/type_ref.rs
@@ -1,162 +1 @@
1//! HIR for references to types. Paths in these are not yet resolved. They can pub use hir_def::type_ref::*;
2//! be directly created from an ast::TypeRef, without further queries.
3
4use ra_syntax::ast::{self, TypeAscriptionOwner, TypeBoundsOwner};
5
6use crate::Path;
7
8#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
9pub enum Mutability {
10 Shared,
11 Mut,
12}
13
14impl Mutability {
15 pub fn from_mutable(mutable: bool) -> Mutability {
16 if mutable {
17 Mutability::Mut
18 } else {
19 Mutability::Shared
20 }
21 }
22
23 pub fn as_keyword_for_ref(self) -> &'static str {
24 match self {
25 Mutability::Shared => "",
26 Mutability::Mut => "mut ",
27 }
28 }
29
30 pub fn as_keyword_for_ptr(self) -> &'static str {
31 match self {
32 Mutability::Shared => "const ",
33 Mutability::Mut => "mut ",
34 }
35 }
36}
37
38/// Compare ty::Ty
39#[derive(Clone, PartialEq, Eq, Hash, Debug)]
40pub enum TypeRef {
41 Never,
42 Placeholder,
43 Tuple(Vec<TypeRef>),
44 Path(Path),
45 RawPtr(Box<TypeRef>, Mutability),
46 Reference(Box<TypeRef>, Mutability),
47 Array(Box<TypeRef> /*, Expr*/),
48 Slice(Box<TypeRef>),
49 /// A fn pointer. Last element of the vector is the return type.
50 Fn(Vec<TypeRef>),
51 // For
52 ImplTrait(Vec<TypeBound>),
53 DynTrait(Vec<TypeBound>),
54 Error,
55}
56
57#[derive(Clone, PartialEq, Eq, Hash, Debug)]
58pub enum TypeBound {
59 Path(Path),
60 // also for<> bounds
61 // also Lifetimes
62 Error,
63}
64
65impl TypeRef {
66 /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
67 pub(crate) fn from_ast(node: ast::TypeRef) -> Self {
68 match node {
69 ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
70 ast::TypeRef::TupleType(inner) => {
71 TypeRef::Tuple(inner.fields().map(TypeRef::from_ast).collect())
72 }
73 ast::TypeRef::NeverType(..) => TypeRef::Never,
74 ast::TypeRef::PathType(inner) => {
75 // FIXME: Use `Path::from_src`
76 inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error)
77 }
78 ast::TypeRef::PointerType(inner) => {
79 let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
80 let mutability = Mutability::from_mutable(inner.is_mut());
81 TypeRef::RawPtr(Box::new(inner_ty), mutability)
82 }
83 ast::TypeRef::ArrayType(inner) => {
84 TypeRef::Array(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
85 }
86 ast::TypeRef::SliceType(inner) => {
87 TypeRef::Slice(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
88 }
89 ast::TypeRef::ReferenceType(inner) => {
90 let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
91 let mutability = Mutability::from_mutable(inner.is_mut());
92 TypeRef::Reference(Box::new(inner_ty), mutability)
93 }
94 ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,
95 ast::TypeRef::FnPointerType(inner) => {
96 let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref()));
97 let mut params = if let Some(pl) = inner.param_list() {
98 pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect()
99 } else {
100 Vec::new()
101 };
102 params.push(ret_ty);
103 TypeRef::Fn(params)
104 }
105 // for types are close enough for our purposes to the inner type for now...
106 ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
107 ast::TypeRef::ImplTraitType(inner) => {
108 TypeRef::ImplTrait(type_bounds_from_ast(inner.type_bound_list()))
109 }
110 ast::TypeRef::DynTraitType(inner) => {
111 TypeRef::DynTrait(type_bounds_from_ast(inner.type_bound_list()))
112 }
113 }
114 }
115
116 pub(crate) fn from_ast_opt(node: Option<ast::TypeRef>) -> Self {
117 if let Some(node) = node {
118 TypeRef::from_ast(node)
119 } else {
120 TypeRef::Error
121 }
122 }
123
124 pub fn unit() -> TypeRef {
125 TypeRef::Tuple(Vec::new())
126 }
127}
128
129pub(crate) fn type_bounds_from_ast(type_bounds_opt: Option<ast::TypeBoundList>) -> Vec<TypeBound> {
130 if let Some(type_bounds) = type_bounds_opt {
131 type_bounds.bounds().map(TypeBound::from_ast).collect()
132 } else {
133 vec![]
134 }
135}
136
137impl TypeBound {
138 pub(crate) fn from_ast(node: ast::TypeBound) -> Self {
139 match node.kind() {
140 ast::TypeBoundKind::PathType(path_type) => {
141 let path = match path_type.path() {
142 Some(p) => p,
143 None => return TypeBound::Error,
144 };
145 // FIXME: Use `Path::from_src`
146 let path = match Path::from_ast(path) {
147 Some(p) => p,
148 None => return TypeBound::Error,
149 };
150 TypeBound::Path(path)
151 }
152 ast::TypeBoundKind::ForType(_) | ast::TypeBoundKind::Lifetime(_) => TypeBound::Error,
153 }
154 }
155
156 pub fn as_path(&self) -> Option<&Path> {
157 match self {
158 TypeBound::Path(p) => Some(p),
159 _ => None,
160 }
161 }
162}
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml
index 75e93f254..746c907e8 100644
--- a/crates/ra_hir_def/Cargo.toml
+++ b/crates/ra_hir_def/Cargo.toml
@@ -6,9 +6,16 @@ authors = ["rust-analyzer developers"]
6 6
7[dependencies] 7[dependencies]
8log = "0.4.5" 8log = "0.4.5"
9once_cell = "1.0.1"
10relative-path = "1.0.0"
11rustc-hash = "1.0"
9 12
10ra_arena = { path = "../ra_arena" } 13ra_arena = { path = "../ra_arena" }
11ra_db = { path = "../ra_db" } 14ra_db = { path = "../ra_db" }
12ra_syntax = { path = "../ra_syntax" } 15ra_syntax = { path = "../ra_syntax" }
13ra_prof = { path = "../ra_prof" } 16ra_prof = { path = "../ra_prof" }
14hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } 17hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
18test_utils = { path = "../test_utils" }
19mbe = { path = "../ra_mbe", package = "ra_mbe" }
20ra_cfg = { path = "../ra_cfg" }
21tt = { path = "../ra_tt", package = "ra_tt" }
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
new file mode 100644
index 000000000..248f03cdf
--- /dev/null
+++ b/crates/ra_hir_def/src/attr.rs
@@ -0,0 +1,91 @@
1//! A higher level attributes based on TokenTree, with also some shortcuts.
2
3use std::sync::Arc;
4
5use hir_expand::db::AstDatabase;
6use mbe::ast_to_token_tree;
7use ra_cfg::CfgOptions;
8use ra_syntax::{
9 ast::{self, AstNode, AttrsOwner},
10 SmolStr,
11};
12use tt::Subtree;
13
14use crate::{path::Path, HirFileId, Source};
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Attr {
18 pub(crate) path: Path,
19 pub(crate) input: Option<AttrInput>,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum AttrInput {
24 Literal(SmolStr),
25 TokenTree(Subtree),
26}
27
28impl Attr {
29 pub(crate) fn from_src(
30 Source { file_id, ast }: Source<ast::Attr>,
31 db: &impl AstDatabase,
32 ) -> Option<Attr> {
33 let path = Path::from_src(Source { file_id, ast: ast.path()? }, db)?;
34 let input = match ast.input() {
35 None => None,
36 Some(ast::AttrInput::Literal(lit)) => {
37 // FIXME: escape? raw string?
38 let value = lit.syntax().first_token()?.text().trim_matches('"').into();
39 Some(AttrInput::Literal(value))
40 }
41 Some(ast::AttrInput::TokenTree(tt)) => {
42 Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
43 }
44 };
45
46 Some(Attr { path, input })
47 }
48
49 pub fn from_attrs_owner(
50 file_id: HirFileId,
51 owner: &dyn AttrsOwner,
52 db: &impl AstDatabase,
53 ) -> Option<Arc<[Attr]>> {
54 let mut attrs = owner.attrs().peekable();
55 if attrs.peek().is_none() {
56 // Avoid heap allocation
57 return None;
58 }
59 Some(attrs.flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect())
60 }
61
62 pub fn is_simple_atom(&self, name: &str) -> bool {
63 // FIXME: Avoid cloning
64 self.path.as_ident().map_or(false, |s| s.to_string() == name)
65 }
66
67 // FIXME: handle cfg_attr :-)
68 pub fn as_cfg(&self) -> Option<&Subtree> {
69 if !self.is_simple_atom("cfg") {
70 return None;
71 }
72 match &self.input {
73 Some(AttrInput::TokenTree(subtree)) => Some(subtree),
74 _ => None,
75 }
76 }
77
78 pub fn as_path(&self) -> Option<&SmolStr> {
79 if !self.is_simple_atom("path") {
80 return None;
81 }
82 match &self.input {
83 Some(AttrInput::Literal(it)) => Some(it),
84 _ => None,
85 }
86 }
87
88 pub fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Option<bool> {
89 cfg_options.is_cfg_enabled(self.as_cfg()?)
90 }
91}
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index f6f976c86..b271636b0 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -1,8 +1,12 @@
1//! Defines database & queries for name resolution. 1//! Defines database & queries for name resolution.
2use std::sync::Arc;
2 3
4use hir_expand::{db::AstDatabase, HirFileId};
3use ra_db::{salsa, SourceDatabase}; 5use ra_db::{salsa, SourceDatabase};
4use ra_syntax::ast; 6use ra_syntax::ast;
5 7
8use crate::nameres::raw::{ImportSourceMap, RawItems};
9
6#[salsa::query_group(InternDatabaseStorage)] 10#[salsa::query_group(InternDatabaseStorage)]
7pub trait InternDatabase: SourceDatabase { 11pub trait InternDatabase: SourceDatabase {
8 #[salsa::interned] 12 #[salsa::interned]
@@ -10,6 +14,8 @@ pub trait InternDatabase: SourceDatabase {
10 #[salsa::interned] 14 #[salsa::interned]
11 fn intern_struct(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::StructId; 15 fn intern_struct(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::StructId;
12 #[salsa::interned] 16 #[salsa::interned]
17 fn intern_union(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::UnionId;
18 #[salsa::interned]
13 fn intern_enum(&self, loc: crate::ItemLoc<ast::EnumDef>) -> crate::EnumId; 19 fn intern_enum(&self, loc: crate::ItemLoc<ast::EnumDef>) -> crate::EnumId;
14 #[salsa::interned] 20 #[salsa::interned]
15 fn intern_const(&self, loc: crate::ItemLoc<ast::ConstDef>) -> crate::ConstId; 21 fn intern_const(&self, loc: crate::ItemLoc<ast::ConstDef>) -> crate::ConstId;
@@ -20,3 +26,15 @@ pub trait InternDatabase: SourceDatabase {
20 #[salsa::interned] 26 #[salsa::interned]
21 fn intern_type_alias(&self, loc: crate::ItemLoc<ast::TypeAliasDef>) -> crate::TypeAliasId; 27 fn intern_type_alias(&self, loc: crate::ItemLoc<ast::TypeAliasDef>) -> crate::TypeAliasId;
22} 28}
29
30#[salsa::query_group(DefDatabase2Storage)]
31pub trait DefDatabase2: InternDatabase + AstDatabase {
32 #[salsa::invoke(RawItems::raw_items_with_source_map_query)]
33 fn raw_items_with_source_map(
34 &self,
35 file_id: HirFileId,
36 ) -> (Arc<RawItems>, Arc<ImportSourceMap>);
37
38 #[salsa::invoke(RawItems::raw_items_query)]
39 fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
40}
diff --git a/crates/ra_hir_def/src/either.rs b/crates/ra_hir_def/src/either.rs
new file mode 100644
index 000000000..83583ef8b
--- /dev/null
+++ b/crates/ra_hir_def/src/either.rs
@@ -0,0 +1,54 @@
1//! FIXME: write short doc here
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub enum Either<A, B> {
5 A(A),
6 B(B),
7}
8
9impl<A, B> Either<A, B> {
10 pub fn either<R, F1, F2>(self, f1: F1, f2: F2) -> R
11 where
12 F1: FnOnce(A) -> R,
13 F2: FnOnce(B) -> R,
14 {
15 match self {
16 Either::A(a) => f1(a),
17 Either::B(b) => f2(b),
18 }
19 }
20 pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V>
21 where
22 F1: FnOnce(A) -> U,
23 F2: FnOnce(B) -> V,
24 {
25 match self {
26 Either::A(a) => Either::A(f1(a)),
27 Either::B(b) => Either::B(f2(b)),
28 }
29 }
30 pub fn map_a<U, F>(self, f: F) -> Either<U, B>
31 where
32 F: FnOnce(A) -> U,
33 {
34 self.map(f, |it| it)
35 }
36 pub fn a(self) -> Option<A> {
37 match self {
38 Either::A(it) => Some(it),
39 Either::B(_) => None,
40 }
41 }
42 pub fn b(self) -> Option<B> {
43 match self {
44 Either::A(_) => None,
45 Either::B(it) => Some(it),
46 }
47 }
48 pub fn as_ref(&self) -> Either<&A, &B> {
49 match self {
50 Either::A(it) => Either::A(it),
51 Either::B(it) => Either::B(it),
52 }
53 }
54}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 4d6b9db03..95d503325 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -8,12 +8,20 @@
8//! actually true. 8//! actually true.
9 9
10pub mod db; 10pub mod db;
11pub mod either;
12pub mod attr;
13pub mod name;
14pub mod path;
15pub mod type_ref;
16
17// FIXME: this should be private
18pub mod nameres;
11 19
12use std::hash::{Hash, Hasher}; 20use std::hash::{Hash, Hasher};
13 21
14use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId}; 22use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId};
15use ra_arena::{impl_arena_id, RawId}; 23use ra_arena::{impl_arena_id, RawId};
16use ra_db::{salsa, CrateId}; 24use ra_db::{salsa, CrateId, FileId};
17use ra_syntax::{ast, AstNode, SyntaxNode}; 25use ra_syntax::{ast, AstNode, SyntaxNode};
18 26
19use crate::db::InternDatabase; 27use crate::db::InternDatabase;
@@ -24,6 +32,68 @@ pub struct Source<T> {
24 pub ast: T, 32 pub ast: T,
25} 33}
26 34
35pub enum ModuleSource {
36 SourceFile(ast::SourceFile),
37 Module(ast::Module),
38}
39
40impl ModuleSource {
41 pub fn new(
42 db: &impl db::DefDatabase2,
43 file_id: Option<FileId>,
44 decl_id: Option<AstId<ast::Module>>,
45 ) -> ModuleSource {
46 match (file_id, decl_id) {
47 (Some(file_id), _) => {
48 let source_file = db.parse(file_id).tree();
49 ModuleSource::SourceFile(source_file)
50 }
51 (None, Some(item_id)) => {
52 let module = item_id.to_node(db);
53 assert!(module.item_list().is_some(), "expected inline module");
54 ModuleSource::Module(module)
55 }
56 (None, None) => panic!(),
57 }
58 }
59
60 // FIXME: this methods do not belong here
61 pub fn from_position(
62 db: &impl db::DefDatabase2,
63 position: ra_db::FilePosition,
64 ) -> ModuleSource {
65 let parse = db.parse(position.file_id);
66 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
67 parse.tree().syntax(),
68 position.offset,
69 ) {
70 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
71 _ => {
72 let source_file = parse.tree();
73 ModuleSource::SourceFile(source_file)
74 }
75 }
76 }
77
78 pub fn from_child_node(
79 db: &impl db::DefDatabase2,
80 file_id: FileId,
81 child: &SyntaxNode,
82 ) -> ModuleSource {
83 if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) {
84 ModuleSource::Module(m)
85 } else {
86 let source_file = db.parse(file_id).tree();
87 ModuleSource::SourceFile(source_file)
88 }
89 }
90
91 pub fn from_file_id(db: &impl db::DefDatabase2, file_id: FileId) -> ModuleSource {
92 let source_file = db.parse(file_id).tree();
93 ModuleSource::SourceFile(source_file)
94 }
95}
96
27impl<T> Source<T> { 97impl<T> Source<T> {
28 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { 98 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
29 Source { file_id: self.file_id, ast: f(self.ast) } 99 Source { file_id: self.file_id, ast: f(self.ast) }
@@ -156,6 +226,18 @@ impl AstItemDef<ast::StructDef> for StructId {
156} 226}
157 227
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 228#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
229pub struct UnionId(salsa::InternId);
230impl_intern_key!(UnionId);
231impl AstItemDef<ast::StructDef> for UnionId {
232 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
233 db.intern_union(loc)
234 }
235 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
236 db.lookup_intern_union(self)
237 }
238}
239
240#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
159pub struct EnumId(salsa::InternId); 241pub struct EnumId(salsa::InternId);
160impl_intern_key!(EnumId); 242impl_intern_key!(EnumId);
161impl AstItemDef<ast::EnumDef> for EnumId { 243impl AstItemDef<ast::EnumDef> for EnumId {
@@ -167,6 +249,17 @@ impl AstItemDef<ast::EnumDef> for EnumId {
167 } 249 }
168} 250}
169 251
252// FIXME: rename to `VariantId`, only enums can ave variants
253#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254pub struct EnumVariantId {
255 parent: EnumId,
256 local_id: LocalEnumVariantId,
257}
258
259#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
260pub(crate) struct LocalEnumVariantId(RawId);
261impl_arena_id!(LocalEnumVariantId);
262
170#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 263#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
171pub struct ConstId(salsa::InternId); 264pub struct ConstId(salsa::InternId);
172impl_intern_key!(ConstId); 265impl_intern_key!(ConstId);
diff --git a/crates/ra_hir_def/src/name.rs b/crates/ra_hir_def/src/name.rs
new file mode 100644
index 000000000..720896ee8
--- /dev/null
+++ b/crates/ra_hir_def/src/name.rs
@@ -0,0 +1,142 @@
1//! FIXME: write short doc here
2
3use std::fmt;
4
5use ra_syntax::{ast, SmolStr};
6
7/// `Name` is a wrapper around string, which is used in hir for both references
8/// and declarations. In theory, names should also carry hygiene info, but we are
9/// not there yet!
10#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
11pub struct Name(Repr);
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
14enum Repr {
15 Text(SmolStr),
16 TupleField(usize),
17}
18
19impl fmt::Display for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 match &self.0 {
22 Repr::Text(text) => fmt::Display::fmt(&text, f),
23 Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
24 }
25 }
26}
27
28impl Name {
29 /// Note: this is private to make creating name from random string hard.
30 /// Hopefully, this should allow us to integrate hygiene cleaner in the
31 /// future, and to switch to interned representation of names.
32 const fn new_text(text: SmolStr) -> Name {
33 Name(Repr::Text(text))
34 }
35
36 pub fn new_tuple_field(idx: usize) -> Name {
37 Name(Repr::TupleField(idx))
38 }
39
40 /// Shortcut to create inline plain text name
41 const fn new_inline_ascii(len: usize, text: &[u8]) -> Name {
42 Name::new_text(SmolStr::new_inline_from_ascii(len, text))
43 }
44
45 /// Resolve a name from the text of token.
46 fn resolve(raw_text: &SmolStr) -> Name {
47 let raw_start = "r#";
48 if raw_text.as_str().starts_with(raw_start) {
49 Name::new_text(SmolStr::new(&raw_text[raw_start.len()..]))
50 } else {
51 Name::new_text(raw_text.clone())
52 }
53 }
54
55 pub fn missing() -> Name {
56 Name::new_text("[missing name]".into())
57 }
58
59 pub fn as_tuple_index(&self) -> Option<usize> {
60 match self.0 {
61 Repr::TupleField(idx) => Some(idx),
62 _ => None,
63 }
64 }
65}
66
67pub trait AsName {
68 fn as_name(&self) -> Name;
69}
70
71impl AsName for ast::NameRef {
72 fn as_name(&self) -> Name {
73 match self.as_tuple_field() {
74 Some(idx) => Name::new_tuple_field(idx),
75 None => Name::resolve(self.text()),
76 }
77 }
78}
79
80impl AsName for ast::Name {
81 fn as_name(&self) -> Name {
82 Name::resolve(self.text())
83 }
84}
85
86impl AsName for ast::FieldKind {
87 fn as_name(&self) -> Name {
88 match self {
89 ast::FieldKind::Name(nr) => nr.as_name(),
90 ast::FieldKind::Index(idx) => Name::new_tuple_field(idx.text().parse().unwrap()),
91 }
92 }
93}
94
95impl AsName for ra_db::Dependency {
96 fn as_name(&self) -> Name {
97 Name::new_text(self.name.clone())
98 }
99}
100
101// Primitives
102pub const ISIZE: Name = Name::new_inline_ascii(5, b"isize");
103pub const I8: Name = Name::new_inline_ascii(2, b"i8");
104pub const I16: Name = Name::new_inline_ascii(3, b"i16");
105pub const I32: Name = Name::new_inline_ascii(3, b"i32");
106pub const I64: Name = Name::new_inline_ascii(3, b"i64");
107pub const I128: Name = Name::new_inline_ascii(4, b"i128");
108pub const USIZE: Name = Name::new_inline_ascii(5, b"usize");
109pub const U8: Name = Name::new_inline_ascii(2, b"u8");
110pub const U16: Name = Name::new_inline_ascii(3, b"u16");
111pub const U32: Name = Name::new_inline_ascii(3, b"u32");
112pub const U64: Name = Name::new_inline_ascii(3, b"u64");
113pub const U128: Name = Name::new_inline_ascii(4, b"u128");
114pub const F32: Name = Name::new_inline_ascii(3, b"f32");
115pub const F64: Name = Name::new_inline_ascii(3, b"f64");
116pub const BOOL: Name = Name::new_inline_ascii(4, b"bool");
117pub const CHAR: Name = Name::new_inline_ascii(4, b"char");
118pub const STR: Name = Name::new_inline_ascii(3, b"str");
119
120// Special names
121pub const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self");
122pub const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self");
123pub const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules");
124
125// Components of known path (value or mod name)
126pub const STD: Name = Name::new_inline_ascii(3, b"std");
127pub const ITER: Name = Name::new_inline_ascii(4, b"iter");
128pub const OPS: Name = Name::new_inline_ascii(3, b"ops");
129pub const FUTURE: Name = Name::new_inline_ascii(6, b"future");
130pub const RESULT: Name = Name::new_inline_ascii(6, b"result");
131pub const BOXED: Name = Name::new_inline_ascii(5, b"boxed");
132
133// Components of known path (type name)
134pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator");
135pub const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item");
136pub const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try");
137pub const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok");
138pub const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future");
139pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result");
140pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
141pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
142pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
new file mode 100644
index 000000000..1a3f7667d
--- /dev/null
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -0,0 +1 @@
pub mod raw;
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index 57f2929c3..13b9fbf48 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -2,6 +2,7 @@
2 2
3use std::{ops::Index, sync::Arc}; 3use std::{ops::Index, sync::Arc};
4 4
5use hir_expand::{ast_id_map::AstIdMap, db::AstDatabase};
5use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; 6use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
6use ra_syntax::{ 7use ra_syntax::{
7 ast::{self, AttrsOwner, NameOwner}, 8 ast::{self, AttrsOwner, NameOwner},
@@ -11,8 +12,11 @@ use test_utils::tested_by;
11 12
12use crate::{ 13use crate::{
13 attr::Attr, 14 attr::Attr,
14 db::{AstDatabase, DefDatabase}, 15 db::DefDatabase2,
15 AsName, AstIdMap, Either, FileAstId, HirFileId, ModuleSource, Name, Path, Source, 16 either::Either,
17 name::{AsName, Name},
18 path::Path,
19 FileAstId, HirFileId, ModuleSource, Source,
16}; 20};
17 21
18/// `RawItems` is a set of top-level items in a file (except for impls). 22/// `RawItems` is a set of top-level items in a file (except for impls).
@@ -48,7 +52,7 @@ impl ImportSourceMap {
48 self.map.insert(import, ptr) 52 self.map.insert(import, ptr)
49 } 53 }
50 54
51 pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource { 55 pub fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource {
52 let file = match source { 56 let file = match source {
53 ModuleSource::SourceFile(file) => file.clone(), 57 ModuleSource::SourceFile(file) => file.clone(),
54 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), 58 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
@@ -60,14 +64,14 @@ impl ImportSourceMap {
60 64
61impl RawItems { 65impl RawItems {
62 pub(crate) fn raw_items_query( 66 pub(crate) fn raw_items_query(
63 db: &(impl DefDatabase + AstDatabase), 67 db: &(impl DefDatabase2 + AstDatabase),
64 file_id: HirFileId, 68 file_id: HirFileId,
65 ) -> Arc<RawItems> { 69 ) -> Arc<RawItems> {
66 db.raw_items_with_source_map(file_id).0 70 db.raw_items_with_source_map(file_id).0
67 } 71 }
68 72
69 pub(crate) fn raw_items_with_source_map_query( 73 pub(crate) fn raw_items_with_source_map_query(
70 db: &(impl DefDatabase + AstDatabase), 74 db: &(impl DefDatabase2 + AstDatabase),
71 file_id: HirFileId, 75 file_id: HirFileId,
72 ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { 76 ) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
73 let mut collector = RawItemsCollector { 77 let mut collector = RawItemsCollector {
@@ -87,7 +91,7 @@ impl RawItems {
87 (Arc::new(collector.raw_items), Arc::new(collector.source_map)) 91 (Arc::new(collector.raw_items), Arc::new(collector.source_map))
88 } 92 }
89 93
90 pub(super) fn items(&self) -> &[RawItem] { 94 pub fn items(&self) -> &[RawItem] {
91 &self.items 95 &self.items
92 } 96 }
93} 97}
@@ -124,19 +128,19 @@ impl Index<Macro> for RawItems {
124type Attrs = Option<Arc<[Attr]>>; 128type Attrs = Option<Arc<[Attr]>>;
125 129
126#[derive(Debug, PartialEq, Eq, Clone)] 130#[derive(Debug, PartialEq, Eq, Clone)]
127pub(super) struct RawItem { 131pub struct RawItem {
128 attrs: Attrs, 132 attrs: Attrs,
129 pub(super) kind: RawItemKind, 133 pub kind: RawItemKind,
130} 134}
131 135
132impl RawItem { 136impl RawItem {
133 pub(super) fn attrs(&self) -> &[Attr] { 137 pub fn attrs(&self) -> &[Attr] {
134 self.attrs.as_ref().map_or(&[], |it| &*it) 138 self.attrs.as_ref().map_or(&[], |it| &*it)
135 } 139 }
136} 140}
137 141
138#[derive(Debug, PartialEq, Eq, Clone, Copy)] 142#[derive(Debug, PartialEq, Eq, Clone, Copy)]
139pub(super) enum RawItemKind { 143pub enum RawItemKind {
140 Module(Module), 144 Module(Module),
141 Import(ImportId), 145 Import(ImportId),
142 Def(Def), 146 Def(Def),
@@ -144,11 +148,11 @@ pub(super) enum RawItemKind {
144} 148}
145 149
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 150#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
147pub(super) struct Module(RawId); 151pub struct Module(RawId);
148impl_arena_id!(Module); 152impl_arena_id!(Module);
149 153
150#[derive(Debug, PartialEq, Eq)] 154#[derive(Debug, PartialEq, Eq)]
151pub(super) enum ModuleData { 155pub enum ModuleData {
152 Declaration { name: Name, ast_id: FileAstId<ast::Module> }, 156 Declaration { name: Name, ast_id: FileAstId<ast::Module> },
153 Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> }, 157 Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> },
154} 158}
@@ -159,26 +163,26 @@ impl_arena_id!(ImportId);
159 163
160#[derive(Debug, Clone, PartialEq, Eq)] 164#[derive(Debug, Clone, PartialEq, Eq)]
161pub struct ImportData { 165pub struct ImportData {
162 pub(super) path: Path, 166 pub path: Path,
163 pub(super) alias: Option<Name>, 167 pub alias: Option<Name>,
164 pub(super) is_glob: bool, 168 pub is_glob: bool,
165 pub(super) is_prelude: bool, 169 pub is_prelude: bool,
166 pub(super) is_extern_crate: bool, 170 pub is_extern_crate: bool,
167 pub(super) is_macro_use: bool, 171 pub is_macro_use: bool,
168} 172}
169 173
170#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 174#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
171pub(super) struct Def(RawId); 175pub struct Def(RawId);
172impl_arena_id!(Def); 176impl_arena_id!(Def);
173 177
174#[derive(Debug, PartialEq, Eq)] 178#[derive(Debug, PartialEq, Eq)]
175pub(super) struct DefData { 179pub struct DefData {
176 pub(super) name: Name, 180 pub name: Name,
177 pub(super) kind: DefKind, 181 pub kind: DefKind,
178} 182}
179 183
180#[derive(Debug, PartialEq, Eq, Clone, Copy)] 184#[derive(Debug, PartialEq, Eq, Clone, Copy)]
181pub(super) enum DefKind { 185pub enum DefKind {
182 Function(FileAstId<ast::FnDef>), 186 Function(FileAstId<ast::FnDef>),
183 Struct(FileAstId<ast::StructDef>), 187 Struct(FileAstId<ast::StructDef>),
184 Union(FileAstId<ast::StructDef>), 188 Union(FileAstId<ast::StructDef>),
@@ -190,15 +194,15 @@ pub(super) enum DefKind {
190} 194}
191 195
192#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 196#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
193pub(super) struct Macro(RawId); 197pub struct Macro(RawId);
194impl_arena_id!(Macro); 198impl_arena_id!(Macro);
195 199
196#[derive(Debug, PartialEq, Eq)] 200#[derive(Debug, PartialEq, Eq)]
197pub(super) struct MacroData { 201pub struct MacroData {
198 pub(super) ast_id: FileAstId<ast::MacroCall>, 202 pub ast_id: FileAstId<ast::MacroCall>,
199 pub(super) path: Path, 203 pub path: Path,
200 pub(super) name: Option<Name>, 204 pub name: Option<Name>,
201 pub(super) export: bool, 205 pub export: bool,
202} 206}
203 207
204struct RawItemsCollector<DB> { 208struct RawItemsCollector<DB> {
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
new file mode 100644
index 000000000..fe060437d
--- /dev/null
+++ b/crates/ra_hir_def/src/path.rs
@@ -0,0 +1,423 @@
1//! FIXME: write short doc here
2
3use std::{iter, sync::Arc};
4
5use hir_expand::db::AstDatabase;
6use ra_db::CrateId;
7use ra_syntax::{
8 ast::{self, NameOwner, TypeAscriptionOwner},
9 AstNode,
10};
11
12use crate::{
13 name::{self, AsName, Name},
14 type_ref::TypeRef,
15 Source,
16};
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub struct Path {
20 pub kind: PathKind,
21 pub segments: Vec<PathSegment>,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Hash)]
25pub struct PathSegment {
26 pub name: Name,
27 pub args_and_bindings: Option<Arc<GenericArgs>>,
28}
29
30/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
31/// can (in the future) also include bindings of associated types, like in
32/// `Iterator<Item = Foo>`.
33#[derive(Debug, Clone, PartialEq, Eq, Hash)]
34pub struct GenericArgs {
35 pub args: Vec<GenericArg>,
36 /// This specifies whether the args contain a Self type as the first
37 /// element. This is the case for path segments like `<T as Trait>`, where
38 /// `T` is actually a type parameter for the path `Trait` specifying the
39 /// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
40 /// is left out.
41 pub has_self_type: bool,
42 /// Associated type bindings like in `Iterator<Item = T>`.
43 pub bindings: Vec<(Name, TypeRef)>,
44}
45
46/// A single generic argument.
47#[derive(Debug, Clone, PartialEq, Eq, Hash)]
48pub enum GenericArg {
49 Type(TypeRef),
50 // or lifetime...
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54pub enum PathKind {
55 Plain,
56 Self_,
57 Super,
58 Crate,
59 // Absolute path
60 Abs,
61 // Type based path like `<T>::foo`
62 Type(Box<TypeRef>),
63 // `$crate` from macro expansion
64 DollarCrate(CrateId),
65}
66
67impl Path {
68 /// Calls `cb` with all paths, represented by this use item.
69 pub fn expand_use_item(
70 item_src: Source<ast::UseItem>,
71 db: &impl AstDatabase,
72 mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
73 ) {
74 if let Some(tree) = item_src.ast.use_tree() {
75 expand_use_tree(None, tree, &|| item_src.file_id.macro_crate(db), &mut cb);
76 }
77 }
78
79 pub fn from_simple_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> Path {
80 Path {
81 kind,
82 segments: segments
83 .into_iter()
84 .map(|name| PathSegment { name, args_and_bindings: None })
85 .collect(),
86 }
87 }
88
89 /// Converts an `ast::Path` to `Path`. Works with use trees.
90 /// DEPRECATED: It does not handle `$crate` from macro call.
91 pub fn from_ast(path: ast::Path) -> Option<Path> {
92 Path::parse(path, &|| None)
93 }
94
95 /// Converts an `ast::Path` to `Path`. Works with use trees.
96 /// It correctly handles `$crate` based path from macro call.
97 pub fn from_src(source: Source<ast::Path>, db: &impl AstDatabase) -> Option<Path> {
98 let file_id = source.file_id;
99 Path::parse(source.ast, &|| file_id.macro_crate(db))
100 }
101
102 fn parse(mut path: ast::Path, macro_crate: &impl Fn() -> Option<CrateId>) -> Option<Path> {
103 let mut kind = PathKind::Plain;
104 let mut segments = Vec::new();
105 loop {
106 let segment = path.segment()?;
107
108 if segment.has_colon_colon() {
109 kind = PathKind::Abs;
110 }
111
112 match segment.kind()? {
113 ast::PathSegmentKind::Name(name) => {
114 if name.text() == "$crate" {
115 if let Some(macro_crate) = macro_crate() {
116 kind = PathKind::DollarCrate(macro_crate);
117 break;
118 }
119 }
120
121 let args = segment
122 .type_arg_list()
123 .and_then(GenericArgs::from_ast)
124 .or_else(|| {
125 GenericArgs::from_fn_like_path_ast(
126 segment.param_list(),
127 segment.ret_type(),
128 )
129 })
130 .map(Arc::new);
131 let segment = PathSegment { name: name.as_name(), args_and_bindings: args };
132 segments.push(segment);
133 }
134 ast::PathSegmentKind::Type { type_ref, trait_ref } => {
135 assert!(path.qualifier().is_none()); // this can only occur at the first segment
136
137 let self_type = TypeRef::from_ast(type_ref?);
138
139 match trait_ref {
140 // <T>::foo
141 None => {
142 kind = PathKind::Type(Box::new(self_type));
143 }
144 // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
145 Some(trait_ref) => {
146 let path = Path::parse(trait_ref.path()?, macro_crate)?;
147 kind = path.kind;
148 let mut prefix_segments = path.segments;
149 prefix_segments.reverse();
150 segments.extend(prefix_segments);
151 // Insert the type reference (T in the above example) as Self parameter for the trait
152 let mut last_segment = segments.last_mut()?;
153 if last_segment.args_and_bindings.is_none() {
154 last_segment.args_and_bindings =
155 Some(Arc::new(GenericArgs::empty()));
156 };
157 let args = last_segment.args_and_bindings.as_mut().unwrap();
158 let mut args_inner = Arc::make_mut(args);
159 args_inner.has_self_type = true;
160 args_inner.args.insert(0, GenericArg::Type(self_type));
161 }
162 }
163 }
164 ast::PathSegmentKind::CrateKw => {
165 kind = PathKind::Crate;
166 break;
167 }
168 ast::PathSegmentKind::SelfKw => {
169 kind = PathKind::Self_;
170 break;
171 }
172 ast::PathSegmentKind::SuperKw => {
173 kind = PathKind::Super;
174 break;
175 }
176 }
177 path = match qualifier(&path) {
178 Some(it) => it,
179 None => break,
180 };
181 }
182 segments.reverse();
183 return Some(Path { kind, segments });
184
185 fn qualifier(path: &ast::Path) -> Option<ast::Path> {
186 if let Some(q) = path.qualifier() {
187 return Some(q);
188 }
189 // FIXME: this bottom up traversal is not too precise.
190 // Should we handle do a top-down analysis, recording results?
191 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
192 let use_tree = use_tree_list.parent_use_tree();
193 use_tree.path()
194 }
195 }
196
197 /// Converts an `ast::NameRef` into a single-identifier `Path`.
198 pub fn from_name_ref(name_ref: &ast::NameRef) -> Path {
199 name_ref.as_name().into()
200 }
201
202 /// `true` is this path is a single identifier, like `foo`
203 pub fn is_ident(&self) -> bool {
204 self.kind == PathKind::Plain && self.segments.len() == 1
205 }
206
207 /// `true` if this path is just a standalone `self`
208 pub fn is_self(&self) -> bool {
209 self.kind == PathKind::Self_ && self.segments.is_empty()
210 }
211
212 /// If this path is a single identifier, like `foo`, return its name.
213 pub fn as_ident(&self) -> Option<&Name> {
214 if self.kind != PathKind::Plain || self.segments.len() > 1 {
215 return None;
216 }
217 self.segments.first().map(|s| &s.name)
218 }
219
220 pub fn expand_macro_expr(&self) -> Option<Name> {
221 self.as_ident().and_then(|name| Some(name.clone()))
222 }
223
224 pub fn is_type_relative(&self) -> bool {
225 match self.kind {
226 PathKind::Type(_) => true,
227 _ => false,
228 }
229 }
230}
231
232impl GenericArgs {
233 pub fn from_ast(node: ast::TypeArgList) -> Option<GenericArgs> {
234 let mut args = Vec::new();
235 for type_arg in node.type_args() {
236 let type_ref = TypeRef::from_ast_opt(type_arg.type_ref());
237 args.push(GenericArg::Type(type_ref));
238 }
239 // lifetimes ignored for now
240 let mut bindings = Vec::new();
241 for assoc_type_arg in node.assoc_type_args() {
242 if let Some(name_ref) = assoc_type_arg.name_ref() {
243 let name = name_ref.as_name();
244 let type_ref = TypeRef::from_ast_opt(assoc_type_arg.type_ref());
245 bindings.push((name, type_ref));
246 }
247 }
248 if args.is_empty() && bindings.is_empty() {
249 None
250 } else {
251 Some(GenericArgs { args, has_self_type: false, bindings })
252 }
253 }
254
255 /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
256 /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`).
257 pub(crate) fn from_fn_like_path_ast(
258 params: Option<ast::ParamList>,
259 ret_type: Option<ast::RetType>,
260 ) -> Option<GenericArgs> {
261 let mut args = Vec::new();
262 let mut bindings = Vec::new();
263 if let Some(params) = params {
264 let mut param_types = Vec::new();
265 for param in params.params() {
266 let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
267 param_types.push(type_ref);
268 }
269 let arg = GenericArg::Type(TypeRef::Tuple(param_types));
270 args.push(arg);
271 }
272 if let Some(ret_type) = ret_type {
273 let type_ref = TypeRef::from_ast_opt(ret_type.type_ref());
274 bindings.push((name::OUTPUT_TYPE, type_ref))
275 }
276 if args.is_empty() && bindings.is_empty() {
277 None
278 } else {
279 Some(GenericArgs { args, has_self_type: false, bindings })
280 }
281 }
282
283 pub(crate) fn empty() -> GenericArgs {
284 GenericArgs { args: Vec::new(), has_self_type: false, bindings: Vec::new() }
285 }
286}
287
288impl From<Name> for Path {
289 fn from(name: Name) -> Path {
290 Path::from_simple_segments(PathKind::Plain, iter::once(name))
291 }
292}
293
294fn expand_use_tree(
295 prefix: Option<Path>,
296 tree: ast::UseTree,
297 macro_crate: &impl Fn() -> Option<CrateId>,
298 cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
299) {
300 if let Some(use_tree_list) = tree.use_tree_list() {
301 let prefix = match tree.path() {
302 // E.g. use something::{{{inner}}};
303 None => prefix,
304 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
305 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
306 Some(path) => match convert_path(prefix, path, macro_crate) {
307 Some(it) => Some(it),
308 None => return, // FIXME: report errors somewhere
309 },
310 };
311 for child_tree in use_tree_list.use_trees() {
312 expand_use_tree(prefix.clone(), child_tree, macro_crate, cb);
313 }
314 } else {
315 let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
316 if let Some(ast_path) = tree.path() {
317 // Handle self in a path.
318 // E.g. `use something::{self, <...>}`
319 if ast_path.qualifier().is_none() {
320 if let Some(segment) = ast_path.segment() {
321 if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
322 if let Some(prefix) = prefix {
323 cb(prefix, &tree, false, alias);
324 return;
325 }
326 }
327 }
328 }
329 if let Some(path) = convert_path(prefix, ast_path, macro_crate) {
330 let is_glob = tree.has_star();
331 cb(path, &tree, is_glob, alias)
332 }
333 // FIXME: report errors somewhere
334 // We get here if we do
335 }
336 }
337}
338
339fn convert_path(
340 prefix: Option<Path>,
341 path: ast::Path,
342 macro_crate: &impl Fn() -> Option<CrateId>,
343) -> Option<Path> {
344 let prefix = if let Some(qual) = path.qualifier() {
345 Some(convert_path(prefix, qual, macro_crate)?)
346 } else {
347 prefix
348 };
349
350 let segment = path.segment()?;
351 let res = match segment.kind()? {
352 ast::PathSegmentKind::Name(name) => {
353 if name.text() == "$crate" {
354 if let Some(krate) = macro_crate() {
355 return Some(Path::from_simple_segments(
356 PathKind::DollarCrate(krate),
357 iter::empty(),
358 ));
359 }
360 }
361
362 // no type args in use
363 let mut res = prefix
364 .unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
365 res.segments.push(PathSegment {
366 name: name.as_name(),
367 args_and_bindings: None, // no type args in use
368 });
369 res
370 }
371 ast::PathSegmentKind::CrateKw => {
372 if prefix.is_some() {
373 return None;
374 }
375 Path::from_simple_segments(PathKind::Crate, iter::empty())
376 }
377 ast::PathSegmentKind::SelfKw => {
378 if prefix.is_some() {
379 return None;
380 }
381 Path::from_simple_segments(PathKind::Self_, iter::empty())
382 }
383 ast::PathSegmentKind::SuperKw => {
384 if prefix.is_some() {
385 return None;
386 }
387 Path::from_simple_segments(PathKind::Super, iter::empty())
388 }
389 ast::PathSegmentKind::Type { .. } => {
390 // not allowed in imports
391 return None;
392 }
393 };
394 Some(res)
395}
396
397pub mod known {
398 use super::{Path, PathKind};
399 use crate::name;
400
401 pub fn std_iter_into_iterator() -> Path {
402 Path::from_simple_segments(
403 PathKind::Abs,
404 vec![name::STD, name::ITER, name::INTO_ITERATOR_TYPE],
405 )
406 }
407
408 pub fn std_ops_try() -> Path {
409 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE])
410 }
411
412 pub fn std_result_result() -> Path {
413 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE])
414 }
415
416 pub fn std_future_future() -> Path {
417 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::FUTURE, name::FUTURE_TYPE])
418 }
419
420 pub fn std_boxed_box() -> Path {
421 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::BOXED, name::BOX_TYPE])
422 }
423}
diff --git a/crates/ra_hir_def/src/type_ref.rs b/crates/ra_hir_def/src/type_ref.rs
new file mode 100644
index 000000000..8af061116
--- /dev/null
+++ b/crates/ra_hir_def/src/type_ref.rs
@@ -0,0 +1,162 @@
1//! HIR for references to types. Paths in these are not yet resolved. They can
2//! be directly created from an ast::TypeRef, without further queries.
3
4use ra_syntax::ast::{self, TypeAscriptionOwner, TypeBoundsOwner};
5
6use crate::path::Path;
7
8#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
9pub enum Mutability {
10 Shared,
11 Mut,
12}
13
14impl Mutability {
15 pub fn from_mutable(mutable: bool) -> Mutability {
16 if mutable {
17 Mutability::Mut
18 } else {
19 Mutability::Shared
20 }
21 }
22
23 pub fn as_keyword_for_ref(self) -> &'static str {
24 match self {
25 Mutability::Shared => "",
26 Mutability::Mut => "mut ",
27 }
28 }
29
30 pub fn as_keyword_for_ptr(self) -> &'static str {
31 match self {
32 Mutability::Shared => "const ",
33 Mutability::Mut => "mut ",
34 }
35 }
36}
37
38/// Compare ty::Ty
39#[derive(Clone, PartialEq, Eq, Hash, Debug)]
40pub enum TypeRef {
41 Never,
42 Placeholder,
43 Tuple(Vec<TypeRef>),
44 Path(Path),
45 RawPtr(Box<TypeRef>, Mutability),
46 Reference(Box<TypeRef>, Mutability),
47 Array(Box<TypeRef> /*, Expr*/),
48 Slice(Box<TypeRef>),
49 /// A fn pointer. Last element of the vector is the return type.
50 Fn(Vec<TypeRef>),
51 // For
52 ImplTrait(Vec<TypeBound>),
53 DynTrait(Vec<TypeBound>),
54 Error,
55}
56
57#[derive(Clone, PartialEq, Eq, Hash, Debug)]
58pub enum TypeBound {
59 Path(Path),
60 // also for<> bounds
61 // also Lifetimes
62 Error,
63}
64
65impl TypeRef {
66 /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
67 pub fn from_ast(node: ast::TypeRef) -> Self {
68 match node {
69 ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
70 ast::TypeRef::TupleType(inner) => {
71 TypeRef::Tuple(inner.fields().map(TypeRef::from_ast).collect())
72 }
73 ast::TypeRef::NeverType(..) => TypeRef::Never,
74 ast::TypeRef::PathType(inner) => {
75 // FIXME: Use `Path::from_src`
76 inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error)
77 }
78 ast::TypeRef::PointerType(inner) => {
79 let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
80 let mutability = Mutability::from_mutable(inner.is_mut());
81 TypeRef::RawPtr(Box::new(inner_ty), mutability)
82 }
83 ast::TypeRef::ArrayType(inner) => {
84 TypeRef::Array(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
85 }
86 ast::TypeRef::SliceType(inner) => {
87 TypeRef::Slice(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
88 }
89 ast::TypeRef::ReferenceType(inner) => {
90 let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
91 let mutability = Mutability::from_mutable(inner.is_mut());
92 TypeRef::Reference(Box::new(inner_ty), mutability)
93 }
94 ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,
95 ast::TypeRef::FnPointerType(inner) => {
96 let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref()));
97 let mut params = if let Some(pl) = inner.param_list() {
98 pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect()
99 } else {
100 Vec::new()
101 };
102 params.push(ret_ty);
103 TypeRef::Fn(params)
104 }
105 // for types are close enough for our purposes to the inner type for now...
106 ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
107 ast::TypeRef::ImplTraitType(inner) => {
108 TypeRef::ImplTrait(type_bounds_from_ast(inner.type_bound_list()))
109 }
110 ast::TypeRef::DynTraitType(inner) => {
111 TypeRef::DynTrait(type_bounds_from_ast(inner.type_bound_list()))
112 }
113 }
114 }
115
116 pub fn from_ast_opt(node: Option<ast::TypeRef>) -> Self {
117 if let Some(node) = node {
118 TypeRef::from_ast(node)
119 } else {
120 TypeRef::Error
121 }
122 }
123
124 pub fn unit() -> TypeRef {
125 TypeRef::Tuple(Vec::new())
126 }
127}
128
129pub(crate) fn type_bounds_from_ast(type_bounds_opt: Option<ast::TypeBoundList>) -> Vec<TypeBound> {
130 if let Some(type_bounds) = type_bounds_opt {
131 type_bounds.bounds().map(TypeBound::from_ast).collect()
132 } else {
133 vec![]
134 }
135}
136
137impl TypeBound {
138 pub fn from_ast(node: ast::TypeBound) -> Self {
139 match node.kind() {
140 ast::TypeBoundKind::PathType(path_type) => {
141 let path = match path_type.path() {
142 Some(p) => p,
143 None => return TypeBound::Error,
144 };
145 // FIXME: Use `Path::from_src`
146 let path = match Path::from_ast(path) {
147 Some(p) => p,
148 None => return TypeBound::Error,
149 };
150 TypeBound::Path(path)
151 }
152 ast::TypeBoundKind::ForType(_) | ast::TypeBoundKind::Lifetime(_) => TypeBound::Error,
153 }
154 }
155
156 pub fn as_path(&self) -> Option<&Path> {
157 match self {
158 TypeBound::Path(p) => Some(p),
159 _ => None,
160 }
161 }
162}
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 6b3538673..3c0ef8f1c 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -12,7 +12,7 @@ use std::hash::{Hash, Hasher};
12use ra_db::{salsa, CrateId, FileId}; 12use ra_db::{salsa, CrateId, FileId};
13use ra_syntax::ast::{self, AstNode}; 13use ra_syntax::ast::{self, AstNode};
14 14
15use crate::{ast_id_map::FileAstId, db::AstDatabase}; 15use crate::ast_id_map::FileAstId;
16 16
17/// Input to the analyzer is a set of files, where each file is identified by 17/// Input to the analyzer is a set of files, where each file is identified by
18/// `FileId` and contains source code. However, another source of source code in 18/// `FileId` and contains source code. However, another source of source code in
@@ -50,7 +50,7 @@ impl From<MacroFile> for HirFileId {
50impl HirFileId { 50impl HirFileId {
51 /// For macro-expansion files, returns the file original source file the 51 /// For macro-expansion files, returns the file original source file the
52 /// expansion originated from. 52 /// expansion originated from.
53 pub fn original_file(self, db: &dyn AstDatabase) -> FileId { 53 pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId {
54 match self.0 { 54 match self.0 {
55 HirFileIdRepr::FileId(file_id) => file_id, 55 HirFileIdRepr::FileId(file_id) => file_id,
56 HirFileIdRepr::MacroFile(macro_file) => { 56 HirFileIdRepr::MacroFile(macro_file) => {
@@ -61,7 +61,7 @@ impl HirFileId {
61 } 61 }
62 62
63 /// Get the crate which the macro lives in, if it is a macro file. 63 /// Get the crate which the macro lives in, if it is a macro file.
64 pub fn macro_crate(self, db: &dyn AstDatabase) -> Option<CrateId> { 64 pub fn macro_crate(self, db: &dyn db::AstDatabase) -> Option<CrateId> {
65 match self.0 { 65 match self.0 {
66 HirFileIdRepr::FileId(_) => None, 66 HirFileIdRepr::FileId(_) => None,
67 HirFileIdRepr::MacroFile(macro_file) => { 67 HirFileIdRepr::MacroFile(macro_file) => {
@@ -154,7 +154,7 @@ impl<N: AstNode> AstId<N> {
154 self.file_id 154 self.file_id
155 } 155 }
156 156
157 pub fn to_node(&self, db: &dyn AstDatabase) -> N { 157 pub fn to_node(&self, db: &dyn db::AstDatabase) -> N {
158 let root = db.parse_or_expand(self.file_id).unwrap(); 158 let root = db.parse_or_expand(self.file_id).unwrap();
159 db.ast_id_map(self.file_id).get(self.file_ast_id).to_node(&root) 159 db.ast_id_map(self.file_id).get(self.file_ast_id).to_node(&root)
160 } 160 }