aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-30 15:56:20 +0000
committerAleksey Kladov <[email protected]>2019-10-30 15:56:20 +0000
commit872ac566bfc6cf43ac55354cf5223b962dbc1d92 (patch)
tree187da15a51e06cd3936d3f8c8e823f70c363a555 /crates/ra_hir_def
parentb05d6e53fb0e9a008dc2e1220b1201818e63ed2d (diff)
push name down to hir_expand
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/hygiene.rs10
-rw-r--r--crates/ra_hir_def/src/lib.rs1
-rw-r--r--crates/ra_hir_def/src/name.rs142
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs15
-rw-r--r--crates/ra_hir_def/src/path.rs15
5 files changed, 21 insertions, 162 deletions
diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs
index f51c46fcb..94de2c57c 100644
--- a/crates/ra_hir_def/src/hygiene.rs
+++ b/crates/ra_hir_def/src/hygiene.rs
@@ -4,13 +4,15 @@
4//! this moment, this is horribly incomplete and handles only `$crate`. 4//! this moment, this is horribly incomplete and handles only `$crate`.
5// Should this be moved to `hir_expand`? Seems like it. 5// Should this be moved to `hir_expand`? Seems like it.
6 6
7use hir_expand::either::Either; 7use hir_expand::{
8use hir_expand::{db::AstDatabase, HirFileId}; 8 db::AstDatabase,
9 either::Either,
10 name::{AsName, Name},
11 HirFileId,
12};
9use ra_db::CrateId; 13use ra_db::CrateId;
10use ra_syntax::ast; 14use ra_syntax::ast;
11 15
12use crate::name::{AsName, Name};
13
14#[derive(Debug)] 16#[derive(Debug)]
15pub struct Hygiene { 17pub struct Hygiene {
16 // This is what `$crate` expands to 18 // This is what `$crate` expands to
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 0de728dc1..5135dda56 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -9,7 +9,6 @@
9 9
10pub mod db; 10pub mod db;
11pub mod attr; 11pub mod attr;
12pub mod name;
13pub mod path; 12pub mod path;
14pub mod type_ref; 13pub mod type_ref;
15pub mod hygiene; 14pub mod hygiene;
diff --git a/crates/ra_hir_def/src/name.rs b/crates/ra_hir_def/src/name.rs
deleted file mode 100644
index 720896ee8..000000000
--- a/crates/ra_hir_def/src/name.rs
+++ /dev/null
@@ -1,142 +0,0 @@
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/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index f1896c0cc..56831e409 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -2,7 +2,12 @@
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, either::Either}; 5use hir_expand::{
6 ast_id_map::AstIdMap,
7 db::AstDatabase,
8 either::Either,
9 name::{AsName, Name},
10};
6use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; 11use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
7use ra_syntax::{ 12use ra_syntax::{
8 ast::{self, AttrsOwner, NameOwner}, 13 ast::{self, AttrsOwner, NameOwner},
@@ -10,12 +15,8 @@ use ra_syntax::{
10}; 15};
11 16
12use crate::{ 17use crate::{
13 attr::Attr, 18 attr::Attr, db::DefDatabase2, hygiene::Hygiene, path::Path, FileAstId, HirFileId, ModuleSource,
14 db::DefDatabase2, 19 Source,
15 hygiene::Hygiene,
16 name::{AsName, Name},
17 path::Path,
18 FileAstId, HirFileId, ModuleSource, Source,
19}; 20};
20 21
21/// `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).
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 8d57e7761..d0b842a6b 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -2,19 +2,17 @@
2 2
3use std::{iter, sync::Arc}; 3use std::{iter, sync::Arc};
4 4
5use hir_expand::either::Either; 5use hir_expand::{
6 either::Either,
7 name::{self, AsName, Name},
8};
6use ra_db::CrateId; 9use ra_db::CrateId;
7use ra_syntax::{ 10use ra_syntax::{
8 ast::{self, NameOwner, TypeAscriptionOwner}, 11 ast::{self, NameOwner, TypeAscriptionOwner},
9 AstNode, 12 AstNode,
10}; 13};
11 14
12use crate::{ 15use crate::{hygiene::Hygiene, type_ref::TypeRef, Source};
13 hygiene::Hygiene,
14 name::{self, AsName, Name},
15 type_ref::TypeRef,
16 Source,
17};
18 16
19#[derive(Debug, Clone, PartialEq, Eq, Hash)] 17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20pub struct Path { 18pub struct Path {
@@ -392,8 +390,9 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
392} 390}
393 391
394pub mod known { 392pub mod known {
393 use hir_expand::name;
394
395 use super::{Path, PathKind}; 395 use super::{Path, PathKind};
396 use crate::name;
397 396
398 pub fn std_iter_into_iterator() -> Path { 397 pub fn std_iter_into_iterator() -> Path {
399 Path::from_simple_segments( 398 Path::from_simple_segments(