aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src')
-rw-r--r--crates/hir_expand/src/builtin_macro.rs41
-rw-r--r--crates/hir_expand/src/db.rs4
-rw-r--r--crates/hir_expand/src/lib.rs2
-rw-r--r--crates/hir_expand/src/name.rs3
4 files changed, 41 insertions, 9 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index 477192a09..bd9223825 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -63,7 +63,7 @@ macro_rules! register_builtin {
63pub fn find_builtin_macro( 63pub fn find_builtin_macro(
64 ident: &name::Name, 64 ident: &name::Name,
65 krate: CrateId, 65 krate: CrateId,
66 ast_id: AstId<ast::MacroCall>, 66 ast_id: AstId<ast::MacroRules>,
67) -> Option<MacroDefId> { 67) -> Option<MacroDefId> {
68 let kind = find_by_name(ident)?; 68 let kind = find_by_name(ident)?;
69 69
@@ -88,12 +88,15 @@ register_builtin! {
88 (column, Column) => column_expand, 88 (column, Column) => column_expand,
89 (file, File) => file_expand, 89 (file, File) => file_expand,
90 (line, Line) => line_expand, 90 (line, Line) => line_expand,
91 (module_path, ModulePath) => module_path_expand,
91 (assert, Assert) => assert_expand, 92 (assert, Assert) => assert_expand,
92 (stringify, Stringify) => stringify_expand, 93 (stringify, Stringify) => stringify_expand,
93 (format_args, FormatArgs) => format_args_expand, 94 (format_args, FormatArgs) => format_args_expand,
94 // format_args_nl only differs in that it adds a newline in the end, 95 // format_args_nl only differs in that it adds a newline in the end,
95 // so we use the same stub expansion for now 96 // so we use the same stub expansion for now
96 (format_args_nl, FormatArgsNl) => format_args_expand, 97 (format_args_nl, FormatArgsNl) => format_args_expand,
98 (llvm_asm, LlvmAsm) => asm_expand,
99 (asm, Asm) => asm_expand,
97 100
98 EAGER: 101 EAGER:
99 (compile_error, CompileError) => compile_error_expand, 102 (compile_error, CompileError) => compile_error_expand,
@@ -105,6 +108,15 @@ register_builtin! {
105 (option_env, OptionEnv) => option_env_expand 108 (option_env, OptionEnv) => option_env_expand
106} 109}
107 110
111fn module_path_expand(
112 _db: &dyn AstDatabase,
113 _id: LazyMacroId,
114 _tt: &tt::Subtree,
115) -> ExpandResult<tt::Subtree> {
116 // Just return a dummy result.
117 ExpandResult::ok(quote! { "module::path" })
118}
119
108fn line_expand( 120fn line_expand(
109 _db: &dyn AstDatabase, 121 _db: &dyn AstDatabase,
110 _id: LazyMacroId, 122 _id: LazyMacroId,
@@ -261,6 +273,19 @@ fn format_args_expand(
261 ExpandResult::ok(expanded) 273 ExpandResult::ok(expanded)
262} 274}
263 275
276fn asm_expand(
277 _db: &dyn AstDatabase,
278 _id: LazyMacroId,
279 _tt: &tt::Subtree,
280) -> ExpandResult<tt::Subtree> {
281 // both asm and llvm_asm don't return anything, so we can expand them to nothing,
282 // for now
283 let expanded = quote! {
284 ()
285 };
286 ExpandResult::ok(expanded)
287}
288
264fn unquote_str(lit: &tt::Literal) -> Option<String> { 289fn unquote_str(lit: &tt::Literal) -> Option<String> {
265 let lit = ast::make::tokens::literal(&lit.to_string()); 290 let lit = ast::make::tokens::literal(&lit.to_string());
266 let token = ast::String::cast(lit)?; 291 let token = ast::String::cast(lit)?;
@@ -490,12 +515,16 @@ mod tests {
490 fn expand_builtin_macro(ra_fixture: &str) -> String { 515 fn expand_builtin_macro(ra_fixture: &str) -> String {
491 let (db, file_id) = TestDB::with_single_file(&ra_fixture); 516 let (db, file_id) = TestDB::with_single_file(&ra_fixture);
492 let parsed = db.parse(file_id); 517 let parsed = db.parse(file_id);
518 let macro_rules: Vec<_> =
519 parsed.syntax_node().descendants().filter_map(ast::MacroRules::cast).collect();
493 let macro_calls: Vec<_> = 520 let macro_calls: Vec<_> =
494 parsed.syntax_node().descendants().filter_map(ast::MacroCall::cast).collect(); 521 parsed.syntax_node().descendants().filter_map(ast::MacroCall::cast).collect();
495 522
496 let ast_id_map = db.ast_id_map(file_id.into()); 523 let ast_id_map = db.ast_id_map(file_id.into());
497 524
498 let expander = find_by_name(&macro_calls[0].name().unwrap().as_name()).unwrap(); 525 assert_eq!(macro_rules.len(), 1, "test must contain exactly 1 `macro_rules!`");
526 assert_eq!(macro_calls.len(), 1, "test must contain exactly 1 macro call");
527 let expander = find_by_name(&macro_rules[0].name().unwrap().as_name()).unwrap();
499 528
500 let krate = CrateId(0); 529 let krate = CrateId(0);
501 let file_id = match expander { 530 let file_id = match expander {
@@ -503,7 +532,7 @@ mod tests {
503 // the first one should be a macro_rules 532 // the first one should be a macro_rules
504 let def = MacroDefId { 533 let def = MacroDefId {
505 krate: Some(CrateId(0)), 534 krate: Some(CrateId(0)),
506 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))), 535 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules[0]))),
507 kind: MacroDefKind::BuiltIn(expander), 536 kind: MacroDefKind::BuiltIn(expander),
508 local_inner: false, 537 local_inner: false,
509 }; 538 };
@@ -513,7 +542,7 @@ mod tests {
513 krate, 542 krate,
514 kind: MacroCallKind::FnLike(AstId::new( 543 kind: MacroCallKind::FnLike(AstId::new(
515 file_id.into(), 544 file_id.into(),
516 ast_id_map.ast_id(&macro_calls[1]), 545 ast_id_map.ast_id(&macro_calls[0]),
517 )), 546 )),
518 }; 547 };
519 548
@@ -524,12 +553,12 @@ mod tests {
524 // the first one should be a macro_rules 553 // the first one should be a macro_rules
525 let def = MacroDefId { 554 let def = MacroDefId {
526 krate: Some(krate), 555 krate: Some(krate),
527 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))), 556 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules[0]))),
528 kind: MacroDefKind::BuiltInEager(expander), 557 kind: MacroDefKind::BuiltInEager(expander),
529 local_inner: false, 558 local_inner: false,
530 }; 559 };
531 560
532 let args = macro_calls[1].token_tree().unwrap(); 561 let args = macro_calls[0].token_tree().unwrap();
533 let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0; 562 let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0;
534 563
535 let arg_id = db.intern_eager_expansion({ 564 let arg_id = db.intern_eager_expansion({
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index ffb70f723..11b5b98c8 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -394,8 +394,8 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
394 // FIXME: Handle Pattern 394 // FIXME: Handle Pattern
395 FragmentKind::Expr 395 FragmentKind::Expr
396 } 396 }
397 // FIXME: Expand to statements in appropriate positions; HIR lowering needs to handle that 397 EXPR_STMT => FragmentKind::Statements,
398 EXPR_STMT | BLOCK_EXPR => FragmentKind::Expr, 398 BLOCK_EXPR => FragmentKind::Expr,
399 ARG_LIST => FragmentKind::Expr, 399 ARG_LIST => FragmentKind::Expr,
400 TRY_EXPR => FragmentKind::Expr, 400 TRY_EXPR => FragmentKind::Expr,
401 TUPLE_EXPR => FragmentKind::Expr, 401 TUPLE_EXPR => FragmentKind::Expr,
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 1a9428514..ae3086a95 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -228,7 +228,7 @@ pub struct MacroDefId {
228 // (which will probably require touching this code), we can instead use 228 // (which will probably require touching this code), we can instead use
229 // that (and also remove the hacks for resolving built-in derives). 229 // that (and also remove the hacks for resolving built-in derives).
230 pub krate: Option<CrateId>, 230 pub krate: Option<CrateId>,
231 pub ast_id: Option<AstId<ast::MacroCall>>, 231 pub ast_id: Option<AstId<ast::MacroRules>>,
232 pub kind: MacroDefKind, 232 pub kind: MacroDefKind,
233 233
234 pub local_inner: bool, 234 pub local_inner: bool,
diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs
index 583ed6142..69d8e6803 100644
--- a/crates/hir_expand/src/name.rs
+++ b/crates/hir_expand/src/name.rs
@@ -188,6 +188,7 @@ pub mod known {
188 column, 188 column,
189 compile_error, 189 compile_error,
190 line, 190 line,
191 module_path,
191 assert, 192 assert,
192 stringify, 193 stringify,
193 concat, 194 concat,
@@ -198,6 +199,8 @@ pub mod known {
198 format_args_nl, 199 format_args_nl,
199 env, 200 env,
200 option_env, 201 option_env,
202 llvm_asm,
203 asm,
201 // Builtin derives 204 // Builtin derives
202 Copy, 205 Copy,
203 Clone, 206 Clone,