diff options
Diffstat (limited to 'crates/hir_expand')
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 37 | ||||
-rw-r--r-- | crates/hir_expand/src/db.rs | 18 | ||||
-rw-r--r-- | crates/hir_expand/src/diagnostics.rs | 110 | ||||
-rw-r--r-- | crates/hir_expand/src/eager.rs | 5 | ||||
-rw-r--r-- | crates/hir_expand/src/hygiene.rs | 2 | ||||
-rw-r--r-- | crates/hir_expand/src/lib.rs | 16 |
6 files changed, 36 insertions, 152 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 94d7aecb6..0b310ba2f 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -8,7 +8,6 @@ use base_db::{AnchoredPath, Edition, FileId}; | |||
8 | use cfg::CfgExpr; | 8 | use cfg::CfgExpr; |
9 | use either::Either; | 9 | use either::Either; |
10 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; | 10 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; |
11 | use parser::FragmentKind; | ||
12 | use syntax::ast::{self, AstToken}; | 11 | use syntax::ast::{self, AstToken}; |
13 | 12 | ||
14 | macro_rules! register_builtin { | 13 | macro_rules! register_builtin { |
@@ -47,7 +46,7 @@ macro_rules! register_builtin { | |||
47 | let expander = match *self { | 46 | let expander = match *self { |
48 | $( EagerExpander::$e_kind => $e_expand, )* | 47 | $( EagerExpander::$e_kind => $e_expand, )* |
49 | }; | 48 | }; |
50 | expander(db,arg_id,tt) | 49 | expander(db, arg_id, tt) |
51 | } | 50 | } |
52 | } | 51 | } |
53 | 52 | ||
@@ -64,14 +63,13 @@ macro_rules! register_builtin { | |||
64 | #[derive(Debug)] | 63 | #[derive(Debug)] |
65 | pub struct ExpandedEager { | 64 | pub struct ExpandedEager { |
66 | pub(crate) subtree: tt::Subtree, | 65 | pub(crate) subtree: tt::Subtree, |
67 | pub(crate) fragment: FragmentKind, | ||
68 | /// The included file ID of the include macro. | 66 | /// The included file ID of the include macro. |
69 | pub(crate) included_file: Option<FileId>, | 67 | pub(crate) included_file: Option<FileId>, |
70 | } | 68 | } |
71 | 69 | ||
72 | impl ExpandedEager { | 70 | impl ExpandedEager { |
73 | fn new(subtree: tt::Subtree, fragment: FragmentKind) -> Self { | 71 | fn new(subtree: tt::Subtree) -> Self { |
74 | ExpandedEager { subtree, fragment, included_file: None } | 72 | ExpandedEager { subtree, included_file: None } |
75 | } | 73 | } |
76 | } | 74 | } |
77 | 75 | ||
@@ -340,7 +338,7 @@ fn compile_error_expand( | |||
340 | _ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()), | 338 | _ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()), |
341 | }; | 339 | }; |
342 | 340 | ||
343 | ExpandResult { value: Some(ExpandedEager::new(quote! {}, FragmentKind::Items)), err: Some(err) } | 341 | ExpandResult { value: Some(ExpandedEager::new(quote! {})), err: Some(err) } |
344 | } | 342 | } |
345 | 343 | ||
346 | fn concat_expand( | 344 | fn concat_expand( |
@@ -371,7 +369,7 @@ fn concat_expand( | |||
371 | } | 369 | } |
372 | } | 370 | } |
373 | } | 371 | } |
374 | ExpandResult { value: Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)), err } | 372 | ExpandResult { value: Some(ExpandedEager::new(quote!(#text))), err } |
375 | } | 373 | } |
376 | 374 | ||
377 | fn concat_idents_expand( | 375 | fn concat_idents_expand( |
@@ -393,7 +391,7 @@ fn concat_idents_expand( | |||
393 | } | 391 | } |
394 | } | 392 | } |
395 | let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() }; | 393 | let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() }; |
396 | ExpandResult { value: Some(ExpandedEager::new(quote!(#ident), FragmentKind::Expr)), err } | 394 | ExpandResult { value: Some(ExpandedEager::new(quote!(#ident))), err } |
397 | } | 395 | } |
398 | 396 | ||
399 | fn relative_file( | 397 | fn relative_file( |
@@ -442,14 +440,7 @@ fn include_expand( | |||
442 | 440 | ||
443 | match res { | 441 | match res { |
444 | Ok((subtree, file_id)) => { | 442 | Ok((subtree, file_id)) => { |
445 | // FIXME: | 443 | ExpandResult::ok(Some(ExpandedEager { subtree, included_file: Some(file_id) })) |
446 | // Handle include as expression | ||
447 | |||
448 | ExpandResult::ok(Some(ExpandedEager { | ||
449 | subtree, | ||
450 | fragment: FragmentKind::Items, | ||
451 | included_file: Some(file_id), | ||
452 | })) | ||
453 | } | 444 | } |
454 | Err(e) => ExpandResult::only_err(e), | 445 | Err(e) => ExpandResult::only_err(e), |
455 | } | 446 | } |
@@ -472,7 +463,7 @@ fn include_bytes_expand( | |||
472 | id: tt::TokenId::unspecified(), | 463 | id: tt::TokenId::unspecified(), |
473 | }))], | 464 | }))], |
474 | }; | 465 | }; |
475 | ExpandResult::ok(Some(ExpandedEager::new(res, FragmentKind::Expr))) | 466 | ExpandResult::ok(Some(ExpandedEager::new(res))) |
476 | } | 467 | } |
477 | 468 | ||
478 | fn include_str_expand( | 469 | fn include_str_expand( |
@@ -492,14 +483,14 @@ fn include_str_expand( | |||
492 | let file_id = match relative_file(db, arg_id.into(), &path, true) { | 483 | let file_id = match relative_file(db, arg_id.into(), &path, true) { |
493 | Ok(file_id) => file_id, | 484 | Ok(file_id) => file_id, |
494 | Err(_) => { | 485 | Err(_) => { |
495 | return ExpandResult::ok(Some(ExpandedEager::new(quote!(""), FragmentKind::Expr))); | 486 | return ExpandResult::ok(Some(ExpandedEager::new(quote!("")))); |
496 | } | 487 | } |
497 | }; | 488 | }; |
498 | 489 | ||
499 | let text = db.file_text(file_id); | 490 | let text = db.file_text(file_id); |
500 | let text = &*text; | 491 | let text = &*text; |
501 | 492 | ||
502 | ExpandResult::ok(Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr))) | 493 | ExpandResult::ok(Some(ExpandedEager::new(quote!(#text)))) |
503 | } | 494 | } |
504 | 495 | ||
505 | fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> { | 496 | fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> { |
@@ -535,7 +526,7 @@ fn env_expand( | |||
535 | }); | 526 | }); |
536 | let expanded = quote! { #s }; | 527 | let expanded = quote! { #s }; |
537 | 528 | ||
538 | ExpandResult { value: Some(ExpandedEager::new(expanded, FragmentKind::Expr)), err } | 529 | ExpandResult { value: Some(ExpandedEager::new(expanded)), err } |
539 | } | 530 | } |
540 | 531 | ||
541 | fn option_env_expand( | 532 | fn option_env_expand( |
@@ -553,7 +544,7 @@ fn option_env_expand( | |||
553 | Some(s) => quote! { std::option::Some(#s) }, | 544 | Some(s) => quote! { std::option::Some(#s) }, |
554 | }; | 545 | }; |
555 | 546 | ||
556 | ExpandResult::ok(Some(ExpandedEager::new(expanded, FragmentKind::Expr))) | 547 | ExpandResult::ok(Some(ExpandedEager::new(expanded))) |
557 | } | 548 | } |
558 | 549 | ||
559 | #[cfg(test)] | 550 | #[cfg(test)] |
@@ -565,6 +556,7 @@ mod tests { | |||
565 | }; | 556 | }; |
566 | use base_db::{fixture::WithFixture, SourceDatabase}; | 557 | use base_db::{fixture::WithFixture, SourceDatabase}; |
567 | use expect_test::{expect, Expect}; | 558 | use expect_test::{expect, Expect}; |
559 | use parser::FragmentKind; | ||
568 | use std::sync::Arc; | 560 | use std::sync::Arc; |
569 | use syntax::ast::NameOwner; | 561 | use syntax::ast::NameOwner; |
570 | 562 | ||
@@ -617,6 +609,7 @@ mod tests { | |||
617 | local_inner: false, | 609 | local_inner: false, |
618 | }; | 610 | }; |
619 | 611 | ||
612 | let fragment = crate::to_fragment_kind(¯o_call); | ||
620 | let args = macro_call.token_tree().unwrap(); | 613 | let args = macro_call.token_tree().unwrap(); |
621 | let parsed_args = mbe::ast_to_token_tree(&args).0; | 614 | let parsed_args = mbe::ast_to_token_tree(&args).0; |
622 | let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)); | 615 | let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)); |
@@ -639,7 +632,7 @@ mod tests { | |||
639 | arg_or_expansion: Arc::new(expanded.subtree), | 632 | arg_or_expansion: Arc::new(expanded.subtree), |
640 | included_file: expanded.included_file, | 633 | included_file: expanded.included_file, |
641 | }), | 634 | }), |
642 | kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment }, | 635 | kind: MacroCallKind::FnLike { ast_id: call_id, fragment }, |
643 | }; | 636 | }; |
644 | 637 | ||
645 | let id: MacroCallId = db.intern_macro(loc).into(); | 638 | let id: MacroCallId = db.intern_macro(loc).into(); |
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 5c769c1bf..e8f4af309 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs | |||
@@ -131,15 +131,15 @@ pub trait AstDatabase: SourceDatabase { | |||
131 | /// used for completion, where we want to see what 'would happen' if we insert a | 131 | /// used for completion, where we want to see what 'would happen' if we insert a |
132 | /// token. The `token_to_map` mapped down into the expansion, with the mapped | 132 | /// token. The `token_to_map` mapped down into the expansion, with the mapped |
133 | /// token returned. | 133 | /// token returned. |
134 | pub fn expand_hypothetical( | 134 | pub fn expand_speculative( |
135 | db: &dyn AstDatabase, | 135 | db: &dyn AstDatabase, |
136 | actual_macro_call: MacroCallId, | 136 | actual_macro_call: MacroCallId, |
137 | hypothetical_args: &ast::TokenTree, | 137 | speculative_args: &ast::TokenTree, |
138 | token_to_map: SyntaxToken, | 138 | token_to_map: SyntaxToken, |
139 | ) -> Option<(SyntaxNode, SyntaxToken)> { | 139 | ) -> Option<(SyntaxNode, SyntaxToken)> { |
140 | let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()); | 140 | let (tt, tmap_1) = mbe::syntax_node_to_token_tree(speculative_args.syntax()); |
141 | let range = | 141 | let range = |
142 | token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?; | 142 | token_to_map.text_range().checked_sub(speculative_args.syntax().text_range().start())?; |
143 | let token_id = tmap_1.token_by_range(range)?; | 143 | let token_id = tmap_1.token_by_range(range)?; |
144 | 144 | ||
145 | let macro_def = { | 145 | let macro_def = { |
@@ -147,15 +147,15 @@ pub fn expand_hypothetical( | |||
147 | db.macro_def(loc.def)? | 147 | db.macro_def(loc.def)? |
148 | }; | 148 | }; |
149 | 149 | ||
150 | let hypothetical_expansion = macro_def.expand(db, actual_macro_call, &tt); | 150 | let speculative_expansion = macro_def.expand(db, actual_macro_call, &tt); |
151 | 151 | ||
152 | let fragment_kind = macro_fragment_kind(db, actual_macro_call); | 152 | let fragment_kind = macro_fragment_kind(db, actual_macro_call); |
153 | 153 | ||
154 | let (node, tmap_2) = | 154 | let (node, tmap_2) = |
155 | mbe::token_tree_to_syntax_node(&hypothetical_expansion.value, fragment_kind).ok()?; | 155 | mbe::token_tree_to_syntax_node(&speculative_expansion.value, fragment_kind).ok()?; |
156 | 156 | ||
157 | let token_id = macro_def.map_id_down(token_id); | 157 | let token_id = macro_def.map_id_down(token_id); |
158 | let range = tmap_2.range_by_token(token_id)?.by_kind(token_to_map.kind())?; | 158 | let range = tmap_2.range_by_token(token_id, token_to_map.kind())?; |
159 | let token = node.syntax_node().covering_element(range).into_token()?; | 159 | let token = node.syntax_node().covering_element(range).into_token()?; |
160 | Some((node.syntax_node(), token)) | 160 | Some((node.syntax_node(), token)) |
161 | } | 161 | } |
@@ -186,7 +186,7 @@ fn parse_macro_expansion( | |||
186 | // The final goal we would like to make all parse_macro success, | 186 | // The final goal we would like to make all parse_macro success, |
187 | // such that the following log will not call anyway. | 187 | // such that the following log will not call anyway. |
188 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 188 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
189 | let node = loc.kind.node(db); | 189 | let node = loc.kind.to_node(db); |
190 | 190 | ||
191 | // collect parent information for warning log | 191 | // collect parent information for warning log |
192 | let parents = | 192 | let parents = |
@@ -325,7 +325,7 @@ fn macro_expand_with_arg( | |||
325 | if let Some(eager) = &loc.eager { | 325 | if let Some(eager) = &loc.eager { |
326 | if arg.is_some() { | 326 | if arg.is_some() { |
327 | return ExpandResult::str_err( | 327 | return ExpandResult::str_err( |
328 | "hypothetical macro expansion not implemented for eager macro".to_owned(), | 328 | "speculative macro expansion not implemented for eager macro".to_owned(), |
329 | ); | 329 | ); |
330 | } else { | 330 | } else { |
331 | return ExpandResult { | 331 | return ExpandResult { |
diff --git a/crates/hir_expand/src/diagnostics.rs b/crates/hir_expand/src/diagnostics.rs deleted file mode 100644 index bf0b85ce9..000000000 --- a/crates/hir_expand/src/diagnostics.rs +++ /dev/null | |||
@@ -1,110 +0,0 @@ | |||
1 | //! Semantic errors and warnings. | ||
2 | //! | ||
3 | //! The `Diagnostic` trait defines a trait object which can represent any | ||
4 | //! diagnostic. | ||
5 | //! | ||
6 | //! `DiagnosticSink` struct is used as an emitter for diagnostic. When creating | ||
7 | //! a `DiagnosticSink`, you supply a callback which can react to a `dyn | ||
8 | //! Diagnostic` or to any concrete diagnostic (downcasting is used internally). | ||
9 | //! | ||
10 | //! Because diagnostics store file offsets, it's a bad idea to store them | ||
11 | //! directly in salsa. For this reason, every hir subsytem defines it's own | ||
12 | //! strongly-typed closed set of diagnostics which use hir ids internally, are | ||
13 | //! stored in salsa and do *not* implement the `Diagnostic` trait. Instead, a | ||
14 | //! subsystem provides a separate, non-query-based API which can walk all stored | ||
15 | //! values and transform them into instances of `Diagnostic`. | ||
16 | |||
17 | use std::{any::Any, fmt}; | ||
18 | |||
19 | use syntax::SyntaxNodePtr; | ||
20 | |||
21 | use crate::InFile; | ||
22 | |||
23 | #[derive(Copy, Clone, Debug, PartialEq)] | ||
24 | pub struct DiagnosticCode(pub &'static str); | ||
25 | |||
26 | impl DiagnosticCode { | ||
27 | pub fn as_str(&self) -> &str { | ||
28 | self.0 | ||
29 | } | ||
30 | } | ||
31 | |||
32 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | ||
33 | fn code(&self) -> DiagnosticCode; | ||
34 | fn message(&self) -> String; | ||
35 | /// Source element that triggered the diagnostics. | ||
36 | /// | ||
37 | /// Note that this should reflect "semantics", rather than specific span we | ||
38 | /// want to highlight. When rendering the diagnostics into an error message, | ||
39 | /// the IDE will fetch the `SyntaxNode` and will narrow the span | ||
40 | /// appropriately. | ||
41 | fn display_source(&self) -> InFile<SyntaxNodePtr>; | ||
42 | fn as_any(&self) -> &(dyn Any + Send + 'static); | ||
43 | fn is_experimental(&self) -> bool { | ||
44 | false | ||
45 | } | ||
46 | } | ||
47 | |||
48 | pub struct DiagnosticSink<'a> { | ||
49 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
50 | filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, | ||
51 | default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>, | ||
52 | } | ||
53 | |||
54 | impl<'a> DiagnosticSink<'a> { | ||
55 | pub fn push(&mut self, d: impl Diagnostic) { | ||
56 | let d: &dyn Diagnostic = &d; | ||
57 | self._push(d); | ||
58 | } | ||
59 | |||
60 | fn _push(&mut self, d: &dyn Diagnostic) { | ||
61 | for filter in &mut self.filters { | ||
62 | if !filter(d) { | ||
63 | return; | ||
64 | } | ||
65 | } | ||
66 | for cb in &mut self.callbacks { | ||
67 | match cb(d) { | ||
68 | Ok(()) => return, | ||
69 | Err(()) => (), | ||
70 | } | ||
71 | } | ||
72 | (self.default_callback)(d) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | pub struct DiagnosticSinkBuilder<'a> { | ||
77 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
78 | filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, | ||
79 | } | ||
80 | |||
81 | impl<'a> DiagnosticSinkBuilder<'a> { | ||
82 | pub fn new() -> Self { | ||
83 | Self { callbacks: Vec::new(), filters: Vec::new() } | ||
84 | } | ||
85 | |||
86 | pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self { | ||
87 | self.filters.push(Box::new(cb)); | ||
88 | self | ||
89 | } | ||
90 | |||
91 | pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self { | ||
92 | let cb = move |diag: &dyn Diagnostic| match diag.as_any().downcast_ref::<D>() { | ||
93 | Some(d) => { | ||
94 | cb(d); | ||
95 | Ok(()) | ||
96 | } | ||
97 | None => Err(()), | ||
98 | }; | ||
99 | self.callbacks.push(Box::new(cb)); | ||
100 | self | ||
101 | } | ||
102 | |||
103 | pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> { | ||
104 | DiagnosticSink { | ||
105 | callbacks: self.callbacks, | ||
106 | filters: self.filters, | ||
107 | default_callback: Box::new(default_callback), | ||
108 | } | ||
109 | } | ||
110 | } | ||
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index e165b9c5f..14af628a1 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs | |||
@@ -113,6 +113,7 @@ pub fn expand_eager_macro( | |||
113 | 113 | ||
114 | let ast_map = db.ast_id_map(macro_call.file_id); | 114 | let ast_map = db.ast_id_map(macro_call.file_id); |
115 | let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value)); | 115 | let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value)); |
116 | let fragment = crate::to_fragment_kind(¯o_call.value); | ||
116 | 117 | ||
117 | // Note: | 118 | // Note: |
118 | // When `lazy_expand` is called, its *parent* file must be already exists. | 119 | // When `lazy_expand` is called, its *parent* file must be already exists. |
@@ -152,7 +153,7 @@ pub fn expand_eager_macro( | |||
152 | arg_or_expansion: Arc::new(expanded.subtree), | 153 | arg_or_expansion: Arc::new(expanded.subtree), |
153 | included_file: expanded.included_file, | 154 | included_file: expanded.included_file, |
154 | }), | 155 | }), |
155 | kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment }, | 156 | kind: MacroCallKind::FnLike { ast_id: call_id, fragment }, |
156 | }; | 157 | }; |
157 | 158 | ||
158 | Ok(db.intern_macro(loc)) | 159 | Ok(db.intern_macro(loc)) |
@@ -197,7 +198,7 @@ fn eager_macro_recur( | |||
197 | macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, | 198 | macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, |
198 | mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), | 199 | mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), |
199 | ) -> Result<SyntaxNode, ErrorEmitted> { | 200 | ) -> Result<SyntaxNode, ErrorEmitted> { |
200 | let original = curr.value.clone().clone_for_update(); | 201 | let original = curr.value.clone_for_update(); |
201 | 202 | ||
202 | let children = original.descendants().filter_map(ast::MacroCall::cast); | 203 | let children = original.descendants().filter_map(ast::MacroCall::cast); |
203 | let mut replacements = Vec::new(); | 204 | let mut replacements = Vec::new(); |
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs index 38e09fdd4..d98913907 100644 --- a/crates/hir_expand/src/hygiene.rs +++ b/crates/hir_expand/src/hygiene.rs | |||
@@ -154,7 +154,7 @@ impl HygieneInfo { | |||
154 | }, | 154 | }, |
155 | }; | 155 | }; |
156 | 156 | ||
157 | let range = token_map.range_by_token(token_id)?.by_kind(SyntaxKind::IDENT)?; | 157 | let range = token_map.range_by_token(token_id, SyntaxKind::IDENT)?; |
158 | Some((tt.with_value(range + tt.value), origin)) | 158 | Some((tt.with_value(range + tt.value), origin)) |
159 | } | 159 | } |
160 | } | 160 | } |
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 92c679dd2..90d8ae240 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -8,7 +8,6 @@ pub mod db; | |||
8 | pub mod ast_id_map; | 8 | pub mod ast_id_map; |
9 | pub mod name; | 9 | pub mod name; |
10 | pub mod hygiene; | 10 | pub mod hygiene; |
11 | pub mod diagnostics; | ||
12 | pub mod builtin_derive; | 11 | pub mod builtin_derive; |
13 | pub mod builtin_macro; | 12 | pub mod builtin_macro; |
14 | pub mod proc_macro; | 13 | pub mod proc_macro; |
@@ -16,6 +15,7 @@ pub mod quote; | |||
16 | pub mod eager; | 15 | pub mod eager; |
17 | mod input; | 16 | mod input; |
18 | 17 | ||
18 | use base_db::ProcMacroKind; | ||
19 | use either::Either; | 19 | use either::Either; |
20 | 20 | ||
21 | pub use mbe::{ExpandError, ExpandResult}; | 21 | pub use mbe::{ExpandError, ExpandResult}; |
@@ -108,7 +108,7 @@ impl HirFileId { | |||
108 | HirFileIdRepr::FileId(_) => None, | 108 | HirFileIdRepr::FileId(_) => None, |
109 | HirFileIdRepr::MacroFile(macro_file) => { | 109 | HirFileIdRepr::MacroFile(macro_file) => { |
110 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 110 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
111 | Some(loc.kind.node(db)) | 111 | Some(loc.kind.to_node(db)) |
112 | } | 112 | } |
113 | } | 113 | } |
114 | } | 114 | } |
@@ -153,7 +153,7 @@ impl HirFileId { | |||
153 | HirFileIdRepr::MacroFile(macro_file) => { | 153 | HirFileIdRepr::MacroFile(macro_file) => { |
154 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 154 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
155 | let item = match loc.def.kind { | 155 | let item = match loc.def.kind { |
156 | MacroDefKind::BuiltInDerive(..) => loc.kind.node(db), | 156 | MacroDefKind::BuiltInDerive(..) => loc.kind.to_node(db), |
157 | _ => return None, | 157 | _ => return None, |
158 | }; | 158 | }; |
159 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) | 159 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) |
@@ -208,7 +208,7 @@ impl MacroDefId { | |||
208 | MacroDefKind::BuiltIn(_, id) => id, | 208 | MacroDefKind::BuiltIn(_, id) => id, |
209 | MacroDefKind::BuiltInDerive(_, id) => id, | 209 | MacroDefKind::BuiltInDerive(_, id) => id, |
210 | MacroDefKind::BuiltInEager(_, id) => id, | 210 | MacroDefKind::BuiltInEager(_, id) => id, |
211 | MacroDefKind::ProcMacro(_, id) => return Either::Right(*id), | 211 | MacroDefKind::ProcMacro(.., id) => return Either::Right(*id), |
212 | }; | 212 | }; |
213 | Either::Left(*id) | 213 | Either::Left(*id) |
214 | } | 214 | } |
@@ -225,7 +225,7 @@ pub enum MacroDefKind { | |||
225 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander | 225 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander |
226 | BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>), | 226 | BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>), |
227 | BuiltInEager(EagerExpander, AstId<ast::Macro>), | 227 | BuiltInEager(EagerExpander, AstId<ast::Macro>), |
228 | ProcMacro(ProcMacroExpander, AstId<ast::Fn>), | 228 | ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>), |
229 | } | 229 | } |
230 | 230 | ||
231 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 231 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -269,7 +269,7 @@ impl MacroCallKind { | |||
269 | } | 269 | } |
270 | } | 270 | } |
271 | 271 | ||
272 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { | 272 | pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { |
273 | match self { | 273 | match self { |
274 | MacroCallKind::FnLike { ast_id, .. } => { | 274 | MacroCallKind::FnLike { ast_id, .. } => { |
275 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) | 275 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) |
@@ -329,7 +329,7 @@ impl ExpansionInfo { | |||
329 | let token_id = self.macro_arg.1.token_by_range(range)?; | 329 | let token_id = self.macro_arg.1.token_by_range(range)?; |
330 | let token_id = self.macro_def.map_id_down(token_id); | 330 | let token_id = self.macro_def.map_id_down(token_id); |
331 | 331 | ||
332 | let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?; | 332 | let range = self.exp_map.range_by_token(token_id, token.value.kind())?; |
333 | 333 | ||
334 | let token = self.expanded.value.covering_element(range).into_token()?; | 334 | let token = self.expanded.value.covering_element(range).into_token()?; |
335 | 335 | ||
@@ -354,7 +354,7 @@ impl ExpansionInfo { | |||
354 | }, | 354 | }, |
355 | }; | 355 | }; |
356 | 356 | ||
357 | let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; | 357 | let range = token_map.range_by_token(token_id, token.value.kind())?; |
358 | let token = | 358 | let token = |
359 | tt.value.covering_element(range + tt.value.text_range().start()).into_token()?; | 359 | tt.value.covering_element(range + tt.value.text_range().start()).into_token()?; |
360 | Some((tt.with_value(token), origin)) | 360 | Some((tt.with_value(token), origin)) |