aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/builtin_macro.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src/builtin_macro.rs')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index 97fb0cb55..9628666d4 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -11,6 +11,7 @@ use crate::quote;
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum BuiltinExpander { 12pub enum BuiltinExpander {
13 Line, 13 Line,
14 Stringify,
14} 15}
15 16
16impl BuiltinExpander { 17impl BuiltinExpander {
@@ -22,6 +23,7 @@ impl BuiltinExpander {
22 ) -> Result<tt::Subtree, mbe::ExpandError> { 23 ) -> Result<tt::Subtree, mbe::ExpandError> {
23 match self { 24 match self {
24 BuiltinExpander::Line => line_expand(db, id, tt), 25 BuiltinExpander::Line => line_expand(db, id, tt),
26 BuiltinExpander::Stringify => stringify_expand(db, id, tt),
25 } 27 }
26 } 28 }
27} 29}
@@ -34,6 +36,8 @@ pub fn find_builtin_macro(
34 // FIXME: Better registering method 36 // FIXME: Better registering method
35 if ident == &name::LINE_MACRO { 37 if ident == &name::LINE_MACRO {
36 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) 38 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
39 } else if ident == &name::STRINGIFY_MACRO {
40 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) })
37 } else { 41 } else {
38 None 42 None
39 } 43 }
@@ -78,3 +82,26 @@ fn line_expand(
78 82
79 Ok(expanded) 83 Ok(expanded)
80} 84}
85
86fn stringify_expand(
87 db: &dyn AstDatabase,
88 id: MacroCallId,
89 _tt: &tt::Subtree,
90) -> Result<tt::Subtree, mbe::ExpandError> {
91 let loc = db.lookup_intern_macro(id);
92 let macro_call = loc.ast_id.to_node(db);
93
94 let macro_content = {
95 let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
96 let macro_args = arg.syntax().clone();
97 let text = macro_args.text();
98 let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
99 text.slice(without_parens).to_string()
100 };
101
102 let expanded = quote! {
103 #macro_content
104 };
105
106 Ok(expanded)
107}