aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs26
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs16
-rw-r--r--crates/ra_hir_expand/src/name.rs166
3 files changed, 117 insertions, 91 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs
index 574637602..d7f8ada78 100644
--- a/crates/ra_hir_expand/src/builtin_derive.rs
+++ b/crates/ra_hir_expand/src/builtin_derive.rs
@@ -12,10 +12,10 @@ use crate::db::AstDatabase;
12use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind}; 12use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind};
13 13
14macro_rules! register_builtin { 14macro_rules! register_builtin {
15 ( $(($name:ident, $kind: ident) => $expand:ident),* ) => { 15 ( $($trait:ident => $expand:ident),* ) => {
16 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 16 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17 pub enum BuiltinDeriveExpander { 17 pub enum BuiltinDeriveExpander {
18 $($kind),* 18 $($trait),*
19 } 19 }
20 20
21 impl BuiltinDeriveExpander { 21 impl BuiltinDeriveExpander {
@@ -26,7 +26,7 @@ macro_rules! register_builtin {
26 tt: &tt::Subtree, 26 tt: &tt::Subtree,
27 ) -> Result<tt::Subtree, mbe::ExpandError> { 27 ) -> Result<tt::Subtree, mbe::ExpandError> {
28 let expander = match *self { 28 let expander = match *self {
29 $( BuiltinDeriveExpander::$kind => $expand, )* 29 $( BuiltinDeriveExpander::$trait => $expand, )*
30 }; 30 };
31 expander(db, id, tt) 31 expander(db, id, tt)
32 } 32 }
@@ -34,7 +34,7 @@ macro_rules! register_builtin {
34 34
35 pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> { 35 pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> {
36 let kind = match ident { 36 let kind = match ident {
37 $( id if id == &name::$name => BuiltinDeriveExpander::$kind, )* 37 $( id if id == &name::N![$trait] => BuiltinDeriveExpander::$trait, )*
38 _ => return None, 38 _ => return None,
39 }; 39 };
40 40
@@ -44,15 +44,15 @@ macro_rules! register_builtin {
44} 44}
45 45
46register_builtin! { 46register_builtin! {
47 (COPY_TRAIT, Copy) => copy_expand, 47 Copy => copy_expand,
48 (CLONE_TRAIT, Clone) => clone_expand, 48 Clone => clone_expand,
49 (DEFAULT_TRAIT, Default) => default_expand, 49 Default => default_expand,
50 (DEBUG_TRAIT, Debug) => debug_expand, 50 Debug => debug_expand,
51 (HASH_TRAIT, Hash) => hash_expand, 51 Hash => hash_expand,
52 (ORD_TRAIT, Ord) => ord_expand, 52 Ord => ord_expand,
53 (PARTIAL_ORD_TRAIT, PartialOrd) => partial_ord_expand, 53 PartialOrd => partial_ord_expand,
54 (EQ_TRAIT, Eq) => eq_expand, 54 Eq => eq_expand,
55 (PARTIAL_EQ_TRAIT, PartialEq) => partial_eq_expand 55 PartialEq => partial_eq_expand
56} 56}
57 57
58struct BasicAdtInfo { 58struct BasicAdtInfo {
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index be5f3cbe3..e5046ad4c 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -34,7 +34,7 @@ macro_rules! register_builtin {
34 ast_id: AstId<ast::MacroCall>, 34 ast_id: AstId<ast::MacroCall>,
35 ) -> Option<MacroDefId> { 35 ) -> Option<MacroDefId> {
36 let kind = match ident { 36 let kind = match ident {
37 $( id if id == &name::$name => BuiltinFnLikeExpander::$kind, )* 37 $( id if id == &name::N![$name] => BuiltinFnLikeExpander::$kind, )*
38 _ => return None, 38 _ => return None,
39 }; 39 };
40 40
@@ -44,15 +44,15 @@ macro_rules! register_builtin {
44} 44}
45 45
46register_builtin! { 46register_builtin! {
47 (COLUMN_MACRO, Column) => column_expand, 47 (column, Column) => column_expand,
48 (COMPILE_ERROR_MACRO, CompileError) => compile_error_expand, 48 (compile_error, CompileError) => compile_error_expand,
49 (FILE_MACRO, File) => file_expand, 49 (file, File) => file_expand,
50 (LINE_MACRO, Line) => line_expand, 50 (line, Line) => line_expand,
51 (STRINGIFY_MACRO, Stringify) => stringify_expand, 51 (stringify, Stringify) => stringify_expand,
52 (FORMAT_ARGS_MACRO, FormatArgs) => format_args_expand, 52 (format_args, FormatArgs) => format_args_expand,
53 // format_args_nl only differs in that it adds a newline in the end, 53 // format_args_nl only differs in that it adds a newline in the end,
54 // so we use the same stub expansion for now 54 // so we use the same stub expansion for now
55 (FORMAT_ARGS_NL_MACRO, FormatArgsNl) => format_args_expand 55 (format_args_nl, FormatArgsNl) => format_args_expand
56} 56}
57 57
58fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize { 58fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index 9e68dd98d..8b3141d75 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -104,73 +104,99 @@ impl AsName for ra_db::Dependency {
104 } 104 }
105} 105}
106 106
107// Primitives 107pub mod known {
108pub const ISIZE: Name = Name::new_inline_ascii(b"isize"); 108 macro_rules! known_names {
109pub const I8: Name = Name::new_inline_ascii(b"i8"); 109 ($($ident:ident),* $(,)?) => {
110pub const I16: Name = Name::new_inline_ascii(b"i16"); 110 $(
111pub const I32: Name = Name::new_inline_ascii(b"i32"); 111 #[allow(bad_style)]
112pub const I64: Name = Name::new_inline_ascii(b"i64"); 112 pub const $ident: super::Name =
113pub const I128: Name = Name::new_inline_ascii(b"i128"); 113 super::Name::new_inline_ascii(stringify!($ident).as_bytes());
114pub const USIZE: Name = Name::new_inline_ascii(b"usize"); 114 )*
115pub const U8: Name = Name::new_inline_ascii(b"u8"); 115 };
116pub const U16: Name = Name::new_inline_ascii(b"u16"); 116 }
117pub const U32: Name = Name::new_inline_ascii(b"u32"); 117
118pub const U64: Name = Name::new_inline_ascii(b"u64"); 118 known_names!(
119pub const U128: Name = Name::new_inline_ascii(b"u128"); 119 // Primitives
120pub const F32: Name = Name::new_inline_ascii(b"f32"); 120 isize,
121pub const F64: Name = Name::new_inline_ascii(b"f64"); 121 i8,
122pub const BOOL: Name = Name::new_inline_ascii(b"bool"); 122 i16,
123pub const CHAR: Name = Name::new_inline_ascii(b"char"); 123 i32,
124pub const STR: Name = Name::new_inline_ascii(b"str"); 124 i64,
125 125 i128,
126// Special names 126 usize,
127pub const SELF_PARAM: Name = Name::new_inline_ascii(b"self"); 127 u8,
128pub const SELF_TYPE: Name = Name::new_inline_ascii(b"Self"); 128 u16,
129pub const MACRO_RULES: Name = Name::new_inline_ascii(b"macro_rules"); 129 u32,
130 130 u64,
131// Components of known path (value or mod name) 131 u128,
132pub const STD: Name = Name::new_inline_ascii(b"std"); 132 f32,
133pub const ITER: Name = Name::new_inline_ascii(b"iter"); 133 f64,
134pub const OPS: Name = Name::new_inline_ascii(b"ops"); 134 bool,
135pub const FUTURE: Name = Name::new_inline_ascii(b"future"); 135 char,
136pub const RESULT: Name = Name::new_inline_ascii(b"result"); 136 str,
137pub const BOXED: Name = Name::new_inline_ascii(b"boxed"); 137 // Special names
138 138 macro_rules,
139// Components of known path (type name) 139 // Components of known path (value or mod name)
140pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(b"IntoIterator"); 140 std,
141pub const ITEM_TYPE: Name = Name::new_inline_ascii(b"Item"); 141 iter,
142pub const TRY_TYPE: Name = Name::new_inline_ascii(b"Try"); 142 ops,
143pub const OK_TYPE: Name = Name::new_inline_ascii(b"Ok"); 143 future,
144pub const FUTURE_TYPE: Name = Name::new_inline_ascii(b"Future"); 144 result,
145pub const RESULT_TYPE: Name = Name::new_inline_ascii(b"Result"); 145 boxed,
146pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(b"Output"); 146 // Components of known path (type name)
147pub const TARGET_TYPE: Name = Name::new_inline_ascii(b"Target"); 147 IntoIterator,
148pub const BOX_TYPE: Name = Name::new_inline_ascii(b"Box"); 148 Item,
149pub const RANGE_FROM_TYPE: Name = Name::new_inline_ascii(b"RangeFrom"); 149 Try,
150pub const RANGE_FULL_TYPE: Name = Name::new_inline_ascii(b"RangeFull"); 150 Ok,
151pub const RANGE_INCLUSIVE_TYPE: Name = Name::new_inline_ascii(b"RangeInclusive"); 151 Future,
152pub const RANGE_TO_INCLUSIVE_TYPE: Name = Name::new_inline_ascii(b"RangeToInclusive"); 152 Result,
153pub const RANGE_TO_TYPE: Name = Name::new_inline_ascii(b"RangeTo"); 153 Output,
154pub const RANGE_TYPE: Name = Name::new_inline_ascii(b"Range"); 154 Target,
155pub const NEG_TYPE: Name = Name::new_inline_ascii(b"Neg"); 155 Box,
156pub const NOT_TYPE: Name = Name::new_inline_ascii(b"Not"); 156 RangeFrom,
157 157 RangeFull,
158// Builtin Macros 158 RangeInclusive,
159pub const FILE_MACRO: Name = Name::new_inline_ascii(b"file"); 159 RangeToInclusive,
160pub const COLUMN_MACRO: Name = Name::new_inline_ascii(b"column"); 160 RangeTo,
161pub const COMPILE_ERROR_MACRO: Name = Name::new_inline_ascii(b"compile_error"); 161 Range,
162pub const LINE_MACRO: Name = Name::new_inline_ascii(b"line"); 162 Neg,
163pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(b"stringify"); 163 Not,
164pub const FORMAT_ARGS_MACRO: Name = Name::new_inline_ascii(b"format_args"); 164 // Builtin macros
165pub const FORMAT_ARGS_NL_MACRO: Name = Name::new_inline_ascii(b"format_args_nl"); 165 file,
166 166 column,
167// Builtin derives 167 compile_error,
168pub const COPY_TRAIT: Name = Name::new_inline_ascii(b"Copy"); 168 line,
169pub const CLONE_TRAIT: Name = Name::new_inline_ascii(b"Clone"); 169 stringify,
170pub const DEFAULT_TRAIT: Name = Name::new_inline_ascii(b"Default"); 170 format_args,
171pub const DEBUG_TRAIT: Name = Name::new_inline_ascii(b"Debug"); 171 format_args_nl,
172pub const HASH_TRAIT: Name = Name::new_inline_ascii(b"Hash"); 172 // Builtin derives
173pub const ORD_TRAIT: Name = Name::new_inline_ascii(b"Ord"); 173 Copy,
174pub const PARTIAL_ORD_TRAIT: Name = Name::new_inline_ascii(b"PartialOrd"); 174 Clone,
175pub const EQ_TRAIT: Name = Name::new_inline_ascii(b"Eq"); 175 Default,
176pub const PARTIAL_EQ_TRAIT: Name = Name::new_inline_ascii(b"PartialEq"); 176 Debug,
177 Hash,
178 Ord,
179 PartialOrd,
180 Eq,
181 PartialEq,
182 );
183
184 // self/Self cannot be used as an identifier
185 pub const SELF_PARAM: super::Name = super::Name::new_inline_ascii(b"self");
186 pub const SELF_TYPE: super::Name = super::Name::new_inline_ascii(b"Self");
187
188 #[macro_export]
189 macro_rules! N {
190 (self) => {
191 $crate::name::known::SELF_PARAM
192 };
193 (Self) => {
194 $crate::name::known::SELF_TYPE
195 };
196 ($ident:ident) => {
197 $crate::name::known::$ident
198 };
199 }
200}
201
202pub use crate::N;