aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/lib.rs
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-09 00:36:06 +0100
committerJonas Schievink <[email protected]>2021-05-09 00:36:06 +0100
commit9cf8d325a21f31acf026084e0c67b3af983dddfb (patch)
tree4aaaf71f3fec62114752bc732960b7e7c3cf8ba8 /crates/hir_expand/src/lib.rs
parentcf4d4f646b6227242b2d4e216e8e30b5e111e02e (diff)
Precompute macro fragment kind
Diffstat (limited to 'crates/hir_expand/src/lib.rs')
-rw-r--r--crates/hir_expand/src/lib.rs68
1 files changed, 66 insertions, 2 deletions
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 0402640de..80ab3aeee 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -16,7 +16,9 @@ pub mod quote;
16pub mod eager; 16pub mod eager;
17 17
18use either::Either; 18use either::Either;
19
19pub use mbe::{ExpandError, ExpandResult}; 20pub use mbe::{ExpandError, ExpandResult};
21pub use parser::FragmentKind;
20 22
21use std::hash::Hash; 23use std::hash::Hash;
22use std::sync::Arc; 24use std::sync::Arc;
@@ -290,7 +292,7 @@ pub struct MacroCallLoc {
290 292
291#[derive(Debug, Clone, PartialEq, Eq, Hash)] 293#[derive(Debug, Clone, PartialEq, Eq, Hash)]
292pub enum MacroCallKind { 294pub enum MacroCallKind {
293 FnLike { ast_id: AstId<ast::MacroCall> }, 295 FnLike { ast_id: AstId<ast::MacroCall>, fragment: FragmentKind },
294 Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId }, 296 Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId },
295} 297}
296 298
@@ -324,6 +326,13 @@ impl MacroCallKind {
324 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()), 326 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()),
325 } 327 }
326 } 328 }
329
330 fn fragment_kind(&self) -> FragmentKind {
331 match self {
332 MacroCallKind::FnLike { fragment, .. } => *fragment,
333 MacroCallKind::Derive { .. } => FragmentKind::Items,
334 }
335 }
327} 336}
328 337
329impl MacroCallId { 338impl MacroCallId {
@@ -357,7 +366,6 @@ pub struct ExpansionInfo {
357} 366}
358 367
359pub use mbe::Origin; 368pub use mbe::Origin;
360use parser::FragmentKind;
361 369
362impl ExpansionInfo { 370impl ExpansionInfo {
363 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> { 371 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
@@ -562,3 +570,59 @@ impl<N: AstNode> InFile<N> {
562 self.with_value(self.value.syntax()) 570 self.with_value(self.value.syntax())
563 } 571 }
564} 572}
573
574/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
575/// FIXME: Not completed
576pub fn to_fragment_kind(call: &ast::MacroCall) -> FragmentKind {
577 use syntax::SyntaxKind::*;
578
579 let syn = call.syntax();
580
581 let parent = match syn.parent() {
582 Some(it) => it,
583 None => return FragmentKind::Statements,
584 };
585
586 match parent.kind() {
587 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
588 MACRO_STMTS => FragmentKind::Statements,
589 MACRO_PAT => FragmentKind::Pattern,
590 MACRO_TYPE => FragmentKind::Type,
591 ITEM_LIST => FragmentKind::Items,
592 LET_STMT => {
593 // FIXME: Handle LHS Pattern
594 FragmentKind::Expr
595 }
596 EXPR_STMT => FragmentKind::Statements,
597 BLOCK_EXPR => FragmentKind::Statements,
598 ARG_LIST => FragmentKind::Expr,
599 TRY_EXPR => FragmentKind::Expr,
600 TUPLE_EXPR => FragmentKind::Expr,
601 PAREN_EXPR => FragmentKind::Expr,
602 ARRAY_EXPR => FragmentKind::Expr,
603 FOR_EXPR => FragmentKind::Expr,
604 PATH_EXPR => FragmentKind::Expr,
605 CLOSURE_EXPR => FragmentKind::Expr,
606 CONDITION => FragmentKind::Expr,
607 BREAK_EXPR => FragmentKind::Expr,
608 RETURN_EXPR => FragmentKind::Expr,
609 MATCH_EXPR => FragmentKind::Expr,
610 MATCH_ARM => FragmentKind::Expr,
611 MATCH_GUARD => FragmentKind::Expr,
612 RECORD_EXPR_FIELD => FragmentKind::Expr,
613 CALL_EXPR => FragmentKind::Expr,
614 INDEX_EXPR => FragmentKind::Expr,
615 METHOD_CALL_EXPR => FragmentKind::Expr,
616 FIELD_EXPR => FragmentKind::Expr,
617 AWAIT_EXPR => FragmentKind::Expr,
618 CAST_EXPR => FragmentKind::Expr,
619 REF_EXPR => FragmentKind::Expr,
620 PREFIX_EXPR => FragmentKind::Expr,
621 RANGE_EXPR => FragmentKind::Expr,
622 BIN_EXPR => FragmentKind::Expr,
623 _ => {
624 // Unknown , Just guess it is `Items`
625 FragmentKind::Items
626 }
627 }
628}