aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/builtin_macro.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src/builtin_macro.rs')
-rw-r--r--crates/hir_expand/src/builtin_macro.rs81
1 files changed, 41 insertions, 40 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index 280c25f11..94d7aecb6 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -1,7 +1,7 @@
1//! Builtin macro 1//! Builtin macro
2use crate::{ 2use crate::{
3 db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId, 3 db::AstDatabase, name, quote, AstId, CrateId, MacroCallId, MacroCallLoc, MacroDefId,
4 MacroCallLoc, MacroDefId, MacroDefKind, TextSize, 4 MacroDefKind, TextSize,
5}; 5};
6 6
7use base_db::{AnchoredPath, Edition, FileId}; 7use base_db::{AnchoredPath, Edition, FileId};
@@ -27,7 +27,7 @@ macro_rules! register_builtin {
27 pub fn expand( 27 pub fn expand(
28 &self, 28 &self,
29 db: &dyn AstDatabase, 29 db: &dyn AstDatabase,
30 id: LazyMacroId, 30 id: MacroCallId,
31 tt: &tt::Subtree, 31 tt: &tt::Subtree,
32 ) -> ExpandResult<tt::Subtree> { 32 ) -> ExpandResult<tt::Subtree> {
33 let expander = match *self { 33 let expander = match *self {
@@ -41,7 +41,7 @@ macro_rules! register_builtin {
41 pub fn expand( 41 pub fn expand(
42 &self, 42 &self,
43 db: &dyn AstDatabase, 43 db: &dyn AstDatabase,
44 arg_id: EagerMacroId, 44 arg_id: MacroCallId,
45 tt: &tt::Subtree, 45 tt: &tt::Subtree,
46 ) -> ExpandResult<Option<ExpandedEager>> { 46 ) -> ExpandResult<Option<ExpandedEager>> {
47 let expander = match *self { 47 let expander = match *self {
@@ -128,7 +128,7 @@ register_builtin! {
128 128
129fn module_path_expand( 129fn module_path_expand(
130 _db: &dyn AstDatabase, 130 _db: &dyn AstDatabase,
131 _id: LazyMacroId, 131 _id: MacroCallId,
132 _tt: &tt::Subtree, 132 _tt: &tt::Subtree,
133) -> ExpandResult<tt::Subtree> { 133) -> ExpandResult<tt::Subtree> {
134 // Just return a dummy result. 134 // Just return a dummy result.
@@ -137,7 +137,7 @@ fn module_path_expand(
137 137
138fn line_expand( 138fn line_expand(
139 _db: &dyn AstDatabase, 139 _db: &dyn AstDatabase,
140 _id: LazyMacroId, 140 _id: MacroCallId,
141 _tt: &tt::Subtree, 141 _tt: &tt::Subtree,
142) -> ExpandResult<tt::Subtree> { 142) -> ExpandResult<tt::Subtree> {
143 // dummy implementation for type-checking purposes 143 // dummy implementation for type-checking purposes
@@ -151,7 +151,7 @@ fn line_expand(
151 151
152fn stringify_expand( 152fn stringify_expand(
153 db: &dyn AstDatabase, 153 db: &dyn AstDatabase,
154 id: LazyMacroId, 154 id: MacroCallId,
155 _tt: &tt::Subtree, 155 _tt: &tt::Subtree,
156) -> ExpandResult<tt::Subtree> { 156) -> ExpandResult<tt::Subtree> {
157 let loc = db.lookup_intern_macro(id); 157 let loc = db.lookup_intern_macro(id);
@@ -176,7 +176,7 @@ fn stringify_expand(
176 176
177fn column_expand( 177fn column_expand(
178 _db: &dyn AstDatabase, 178 _db: &dyn AstDatabase,
179 _id: LazyMacroId, 179 _id: MacroCallId,
180 _tt: &tt::Subtree, 180 _tt: &tt::Subtree,
181) -> ExpandResult<tt::Subtree> { 181) -> ExpandResult<tt::Subtree> {
182 // dummy implementation for type-checking purposes 182 // dummy implementation for type-checking purposes
@@ -190,7 +190,7 @@ fn column_expand(
190 190
191fn assert_expand( 191fn assert_expand(
192 _db: &dyn AstDatabase, 192 _db: &dyn AstDatabase,
193 _id: LazyMacroId, 193 _id: MacroCallId,
194 tt: &tt::Subtree, 194 tt: &tt::Subtree,
195) -> ExpandResult<tt::Subtree> { 195) -> ExpandResult<tt::Subtree> {
196 // A hacky implementation for goto def and hover 196 // A hacky implementation for goto def and hover
@@ -214,7 +214,7 @@ fn assert_expand(
214 214
215fn file_expand( 215fn file_expand(
216 _db: &dyn AstDatabase, 216 _db: &dyn AstDatabase,
217 _id: LazyMacroId, 217 _id: MacroCallId,
218 _tt: &tt::Subtree, 218 _tt: &tt::Subtree,
219) -> ExpandResult<tt::Subtree> { 219) -> ExpandResult<tt::Subtree> {
220 // FIXME: RA purposefully lacks knowledge of absolute file names 220 // FIXME: RA purposefully lacks knowledge of absolute file names
@@ -230,7 +230,7 @@ fn file_expand(
230 230
231fn format_args_expand( 231fn format_args_expand(
232 _db: &dyn AstDatabase, 232 _db: &dyn AstDatabase,
233 _id: LazyMacroId, 233 _id: MacroCallId,
234 tt: &tt::Subtree, 234 tt: &tt::Subtree,
235) -> ExpandResult<tt::Subtree> { 235) -> ExpandResult<tt::Subtree> {
236 // We expand `format_args!("", a1, a2)` to 236 // We expand `format_args!("", a1, a2)` to
@@ -265,7 +265,7 @@ fn format_args_expand(
265 265
266fn asm_expand( 266fn asm_expand(
267 _db: &dyn AstDatabase, 267 _db: &dyn AstDatabase,
268 _id: LazyMacroId, 268 _id: MacroCallId,
269 _tt: &tt::Subtree, 269 _tt: &tt::Subtree,
270) -> ExpandResult<tt::Subtree> { 270) -> ExpandResult<tt::Subtree> {
271 // both asm and llvm_asm don't return anything, so we can expand them to nothing, 271 // both asm and llvm_asm don't return anything, so we can expand them to nothing,
@@ -278,7 +278,7 @@ fn asm_expand(
278 278
279fn global_asm_expand( 279fn global_asm_expand(
280 _db: &dyn AstDatabase, 280 _db: &dyn AstDatabase,
281 _id: LazyMacroId, 281 _id: MacroCallId,
282 _tt: &tt::Subtree, 282 _tt: &tt::Subtree,
283) -> ExpandResult<tt::Subtree> { 283) -> ExpandResult<tt::Subtree> {
284 // Expand to nothing (at item-level) 284 // Expand to nothing (at item-level)
@@ -287,7 +287,7 @@ fn global_asm_expand(
287 287
288fn cfg_expand( 288fn cfg_expand(
289 db: &dyn AstDatabase, 289 db: &dyn AstDatabase,
290 id: LazyMacroId, 290 id: MacroCallId,
291 tt: &tt::Subtree, 291 tt: &tt::Subtree,
292) -> ExpandResult<tt::Subtree> { 292) -> ExpandResult<tt::Subtree> {
293 let loc = db.lookup_intern_macro(id); 293 let loc = db.lookup_intern_macro(id);
@@ -299,7 +299,7 @@ fn cfg_expand(
299 299
300fn panic_expand( 300fn panic_expand(
301 db: &dyn AstDatabase, 301 db: &dyn AstDatabase,
302 id: LazyMacroId, 302 id: MacroCallId,
303 tt: &tt::Subtree, 303 tt: &tt::Subtree,
304) -> ExpandResult<tt::Subtree> { 304) -> ExpandResult<tt::Subtree> {
305 let loc: MacroCallLoc = db.lookup_intern_macro(id); 305 let loc: MacroCallLoc = db.lookup_intern_macro(id);
@@ -324,7 +324,7 @@ fn unquote_str(lit: &tt::Literal) -> Option<String> {
324 324
325fn compile_error_expand( 325fn compile_error_expand(
326 _db: &dyn AstDatabase, 326 _db: &dyn AstDatabase,
327 _id: EagerMacroId, 327 _id: MacroCallId,
328 tt: &tt::Subtree, 328 tt: &tt::Subtree,
329) -> ExpandResult<Option<ExpandedEager>> { 329) -> ExpandResult<Option<ExpandedEager>> {
330 let err = match &*tt.token_trees { 330 let err = match &*tt.token_trees {
@@ -345,7 +345,7 @@ fn compile_error_expand(
345 345
346fn concat_expand( 346fn concat_expand(
347 _db: &dyn AstDatabase, 347 _db: &dyn AstDatabase,
348 _arg_id: EagerMacroId, 348 _arg_id: MacroCallId,
349 tt: &tt::Subtree, 349 tt: &tt::Subtree,
350) -> ExpandResult<Option<ExpandedEager>> { 350) -> ExpandResult<Option<ExpandedEager>> {
351 let mut err = None; 351 let mut err = None;
@@ -376,7 +376,7 @@ fn concat_expand(
376 376
377fn concat_idents_expand( 377fn concat_idents_expand(
378 _db: &dyn AstDatabase, 378 _db: &dyn AstDatabase,
379 _arg_id: EagerMacroId, 379 _arg_id: MacroCallId,
380 tt: &tt::Subtree, 380 tt: &tt::Subtree,
381) -> ExpandResult<Option<ExpandedEager>> { 381) -> ExpandResult<Option<ExpandedEager>> {
382 let mut err = None; 382 let mut err = None;
@@ -427,7 +427,7 @@ fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
427 427
428fn include_expand( 428fn include_expand(
429 db: &dyn AstDatabase, 429 db: &dyn AstDatabase,
430 arg_id: EagerMacroId, 430 arg_id: MacroCallId,
431 tt: &tt::Subtree, 431 tt: &tt::Subtree,
432) -> ExpandResult<Option<ExpandedEager>> { 432) -> ExpandResult<Option<ExpandedEager>> {
433 let res = (|| { 433 let res = (|| {
@@ -457,7 +457,7 @@ fn include_expand(
457 457
458fn include_bytes_expand( 458fn include_bytes_expand(
459 _db: &dyn AstDatabase, 459 _db: &dyn AstDatabase,
460 _arg_id: EagerMacroId, 460 _arg_id: MacroCallId,
461 tt: &tt::Subtree, 461 tt: &tt::Subtree,
462) -> ExpandResult<Option<ExpandedEager>> { 462) -> ExpandResult<Option<ExpandedEager>> {
463 if let Err(e) = parse_string(tt) { 463 if let Err(e) = parse_string(tt) {
@@ -477,7 +477,7 @@ fn include_bytes_expand(
477 477
478fn include_str_expand( 478fn include_str_expand(
479 db: &dyn AstDatabase, 479 db: &dyn AstDatabase,
480 arg_id: EagerMacroId, 480 arg_id: MacroCallId,
481 tt: &tt::Subtree, 481 tt: &tt::Subtree,
482) -> ExpandResult<Option<ExpandedEager>> { 482) -> ExpandResult<Option<ExpandedEager>> {
483 let path = match parse_string(tt) { 483 let path = match parse_string(tt) {
@@ -502,14 +502,14 @@ fn include_str_expand(
502 ExpandResult::ok(Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr))) 502 ExpandResult::ok(Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)))
503} 503}
504 504
505fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> { 505fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
506 let krate = db.lookup_intern_eager_expansion(arg_id).krate; 506 let krate = db.lookup_intern_macro(arg_id).krate;
507 db.crate_graph()[krate].env.get(key) 507 db.crate_graph()[krate].env.get(key)
508} 508}
509 509
510fn env_expand( 510fn env_expand(
511 db: &dyn AstDatabase, 511 db: &dyn AstDatabase,
512 arg_id: EagerMacroId, 512 arg_id: MacroCallId,
513 tt: &tt::Subtree, 513 tt: &tt::Subtree,
514) -> ExpandResult<Option<ExpandedEager>> { 514) -> ExpandResult<Option<ExpandedEager>> {
515 let key = match parse_string(tt) { 515 let key = match parse_string(tt) {
@@ -540,7 +540,7 @@ fn env_expand(
540 540
541fn option_env_expand( 541fn option_env_expand(
542 db: &dyn AstDatabase, 542 db: &dyn AstDatabase,
543 arg_id: EagerMacroId, 543 arg_id: MacroCallId,
544 tt: &tt::Subtree, 544 tt: &tt::Subtree,
545) -> ExpandResult<Option<ExpandedEager>> { 545) -> ExpandResult<Option<ExpandedEager>> {
546 let key = match parse_string(tt) { 546 let key = match parse_string(tt) {
@@ -560,7 +560,7 @@ fn option_env_expand(
560mod tests { 560mod tests {
561 use super::*; 561 use super::*;
562 use crate::{ 562 use crate::{
563 name::AsName, test_db::TestDB, AstNode, EagerCallLoc, MacroCallId, MacroCallKind, 563 name::AsName, test_db::TestDB, AstNode, EagerCallInfo, MacroCallId, MacroCallKind,
564 MacroCallLoc, 564 MacroCallLoc,
565 }; 565 };
566 use base_db::{fixture::WithFixture, SourceDatabase}; 566 use base_db::{fixture::WithFixture, SourceDatabase};
@@ -599,6 +599,7 @@ mod tests {
599 let loc = MacroCallLoc { 599 let loc = MacroCallLoc {
600 def, 600 def,
601 krate, 601 krate,
602 eager: None,
602 kind: MacroCallKind::FnLike { 603 kind: MacroCallKind::FnLike {
603 ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)), 604 ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
604 fragment: FragmentKind::Expr, 605 fragment: FragmentKind::Expr,
@@ -620,28 +621,28 @@ mod tests {
620 let parsed_args = mbe::ast_to_token_tree(&args).0; 621 let parsed_args = mbe::ast_to_token_tree(&args).0;
621 let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)); 622 let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
622 623
623 let arg_id = db.intern_eager_expansion({ 624 let arg_id = db.intern_macro(MacroCallLoc {
624 EagerCallLoc { 625 def,
625 def, 626 krate,
626 fragment: FragmentKind::Expr, 627 eager: Some(EagerCallInfo {
627 subtree: Arc::new(parsed_args.clone()), 628 arg_or_expansion: Arc::new(parsed_args.clone()),
628 krate,
629 call: call_id,
630 included_file: None, 629 included_file: None,
631 } 630 }),
631 kind: MacroCallKind::FnLike { ast_id: call_id, fragment: FragmentKind::Expr },
632 }); 632 });
633 633
634 let expanded = expander.expand(&db, arg_id, &parsed_args).value.unwrap(); 634 let expanded = expander.expand(&db, arg_id, &parsed_args).value.unwrap();
635 let eager = EagerCallLoc { 635 let loc = MacroCallLoc {
636 def, 636 def,
637 fragment: expanded.fragment,
638 subtree: Arc::new(expanded.subtree),
639 krate, 637 krate,
640 call: call_id, 638 eager: Some(EagerCallInfo {
641 included_file: expanded.included_file, 639 arg_or_expansion: Arc::new(expanded.subtree),
640 included_file: expanded.included_file,
641 }),
642 kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment },
642 }; 643 };
643 644
644 let id: MacroCallId = db.intern_eager_expansion(eager).into(); 645 let id: MacroCallId = db.intern_macro(loc).into();
645 id.as_file() 646 id.as_file()
646 } 647 }
647 }; 648 };