diff options
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/builtin_derive.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/builtin_macro.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/name.rs | 166 |
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; | |||
12 | use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind}; | 12 | use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind}; |
13 | 13 | ||
14 | macro_rules! register_builtin { | 14 | macro_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 | ||
46 | register_builtin! { | 46 | register_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 | ||
58 | struct BasicAdtInfo { | 58 | struct 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 | ||
46 | register_builtin! { | 46 | register_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 | ||
58 | fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize { | 58 | fn 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 | 107 | pub mod known { |
108 | pub const ISIZE: Name = Name::new_inline_ascii(b"isize"); | 108 | macro_rules! known_names { |
109 | pub const I8: Name = Name::new_inline_ascii(b"i8"); | 109 | ($($ident:ident),* $(,)?) => { |
110 | pub const I16: Name = Name::new_inline_ascii(b"i16"); | 110 | $( |
111 | pub const I32: Name = Name::new_inline_ascii(b"i32"); | 111 | #[allow(bad_style)] |
112 | pub const I64: Name = Name::new_inline_ascii(b"i64"); | 112 | pub const $ident: super::Name = |
113 | pub const I128: Name = Name::new_inline_ascii(b"i128"); | 113 | super::Name::new_inline_ascii(stringify!($ident).as_bytes()); |
114 | pub const USIZE: Name = Name::new_inline_ascii(b"usize"); | 114 | )* |
115 | pub const U8: Name = Name::new_inline_ascii(b"u8"); | 115 | }; |
116 | pub const U16: Name = Name::new_inline_ascii(b"u16"); | 116 | } |
117 | pub const U32: Name = Name::new_inline_ascii(b"u32"); | 117 | |
118 | pub const U64: Name = Name::new_inline_ascii(b"u64"); | 118 | known_names!( |
119 | pub const U128: Name = Name::new_inline_ascii(b"u128"); | 119 | // Primitives |
120 | pub const F32: Name = Name::new_inline_ascii(b"f32"); | 120 | isize, |
121 | pub const F64: Name = Name::new_inline_ascii(b"f64"); | 121 | i8, |
122 | pub const BOOL: Name = Name::new_inline_ascii(b"bool"); | 122 | i16, |
123 | pub const CHAR: Name = Name::new_inline_ascii(b"char"); | 123 | i32, |
124 | pub const STR: Name = Name::new_inline_ascii(b"str"); | 124 | i64, |
125 | 125 | i128, | |
126 | // Special names | 126 | usize, |
127 | pub const SELF_PARAM: Name = Name::new_inline_ascii(b"self"); | 127 | u8, |
128 | pub const SELF_TYPE: Name = Name::new_inline_ascii(b"Self"); | 128 | u16, |
129 | pub 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, |
132 | pub const STD: Name = Name::new_inline_ascii(b"std"); | 132 | f32, |
133 | pub const ITER: Name = Name::new_inline_ascii(b"iter"); | 133 | f64, |
134 | pub const OPS: Name = Name::new_inline_ascii(b"ops"); | 134 | bool, |
135 | pub const FUTURE: Name = Name::new_inline_ascii(b"future"); | 135 | char, |
136 | pub const RESULT: Name = Name::new_inline_ascii(b"result"); | 136 | str, |
137 | pub 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) |
140 | pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(b"IntoIterator"); | 140 | std, |
141 | pub const ITEM_TYPE: Name = Name::new_inline_ascii(b"Item"); | 141 | iter, |
142 | pub const TRY_TYPE: Name = Name::new_inline_ascii(b"Try"); | 142 | ops, |
143 | pub const OK_TYPE: Name = Name::new_inline_ascii(b"Ok"); | 143 | future, |
144 | pub const FUTURE_TYPE: Name = Name::new_inline_ascii(b"Future"); | 144 | result, |
145 | pub const RESULT_TYPE: Name = Name::new_inline_ascii(b"Result"); | 145 | boxed, |
146 | pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(b"Output"); | 146 | // Components of known path (type name) |
147 | pub const TARGET_TYPE: Name = Name::new_inline_ascii(b"Target"); | 147 | IntoIterator, |
148 | pub const BOX_TYPE: Name = Name::new_inline_ascii(b"Box"); | 148 | Item, |
149 | pub const RANGE_FROM_TYPE: Name = Name::new_inline_ascii(b"RangeFrom"); | 149 | Try, |
150 | pub const RANGE_FULL_TYPE: Name = Name::new_inline_ascii(b"RangeFull"); | 150 | Ok, |
151 | pub const RANGE_INCLUSIVE_TYPE: Name = Name::new_inline_ascii(b"RangeInclusive"); | 151 | Future, |
152 | pub const RANGE_TO_INCLUSIVE_TYPE: Name = Name::new_inline_ascii(b"RangeToInclusive"); | 152 | Result, |
153 | pub const RANGE_TO_TYPE: Name = Name::new_inline_ascii(b"RangeTo"); | 153 | Output, |
154 | pub const RANGE_TYPE: Name = Name::new_inline_ascii(b"Range"); | 154 | Target, |
155 | pub const NEG_TYPE: Name = Name::new_inline_ascii(b"Neg"); | 155 | Box, |
156 | pub const NOT_TYPE: Name = Name::new_inline_ascii(b"Not"); | 156 | RangeFrom, |
157 | 157 | RangeFull, | |
158 | // Builtin Macros | 158 | RangeInclusive, |
159 | pub const FILE_MACRO: Name = Name::new_inline_ascii(b"file"); | 159 | RangeToInclusive, |
160 | pub const COLUMN_MACRO: Name = Name::new_inline_ascii(b"column"); | 160 | RangeTo, |
161 | pub const COMPILE_ERROR_MACRO: Name = Name::new_inline_ascii(b"compile_error"); | 161 | Range, |
162 | pub const LINE_MACRO: Name = Name::new_inline_ascii(b"line"); | 162 | Neg, |
163 | pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(b"stringify"); | 163 | Not, |
164 | pub const FORMAT_ARGS_MACRO: Name = Name::new_inline_ascii(b"format_args"); | 164 | // Builtin macros |
165 | pub 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, |
168 | pub const COPY_TRAIT: Name = Name::new_inline_ascii(b"Copy"); | 168 | line, |
169 | pub const CLONE_TRAIT: Name = Name::new_inline_ascii(b"Clone"); | 169 | stringify, |
170 | pub const DEFAULT_TRAIT: Name = Name::new_inline_ascii(b"Default"); | 170 | format_args, |
171 | pub const DEBUG_TRAIT: Name = Name::new_inline_ascii(b"Debug"); | 171 | format_args_nl, |
172 | pub const HASH_TRAIT: Name = Name::new_inline_ascii(b"Hash"); | 172 | // Builtin derives |
173 | pub const ORD_TRAIT: Name = Name::new_inline_ascii(b"Ord"); | 173 | Copy, |
174 | pub const PARTIAL_ORD_TRAIT: Name = Name::new_inline_ascii(b"PartialOrd"); | 174 | Clone, |
175 | pub const EQ_TRAIT: Name = Name::new_inline_ascii(b"Eq"); | 175 | Default, |
176 | pub 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 | |||
202 | pub use crate::N; | ||