aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src/lib.rs')
-rw-r--r--crates/hir_expand/src/lib.rs89
1 files changed, 83 insertions, 6 deletions
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 0402640de..88cb16ca4 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -14,9 +14,12 @@ pub mod builtin_macro;
14pub mod proc_macro; 14pub mod proc_macro;
15pub mod quote; 15pub mod quote;
16pub mod eager; 16pub mod eager;
17mod input;
17 18
18use either::Either; 19use either::Either;
20
19pub use mbe::{ExpandError, ExpandResult}; 21pub use mbe::{ExpandError, ExpandResult};
22pub use parser::FragmentKind;
20 23
21use std::hash::Hash; 24use std::hash::Hash;
22use std::sync::Arc; 25use std::sync::Arc;
@@ -269,6 +272,10 @@ impl MacroDefId {
269 }; 272 };
270 Either::Left(*id) 273 Either::Left(*id)
271 } 274 }
275
276 pub fn is_proc_macro(&self) -> bool {
277 matches!(self.kind, MacroDefKind::ProcMacro(..))
278 }
272} 279}
273 280
274#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 281#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -290,13 +297,21 @@ pub struct MacroCallLoc {
290 297
291#[derive(Debug, Clone, PartialEq, Eq, Hash)] 298#[derive(Debug, Clone, PartialEq, Eq, Hash)]
292pub enum MacroCallKind { 299pub enum MacroCallKind {
293 FnLike { ast_id: AstId<ast::MacroCall> }, 300 FnLike {
294 Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId }, 301 ast_id: AstId<ast::MacroCall>,
302 fragment: FragmentKind,
303 },
304 Derive {
305 ast_id: AstId<ast::Item>,
306 derive_name: String,
307 /// Syntactical index of the invoking `#[derive]` attribute.
308 ///
309 /// Outer attributes are counted first, then inner attributes. This does not support
310 /// out-of-line modules, which may have attributes spread across 2 files!
311 derive_attr_index: u32,
312 },
295} 313}
296 314
297#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
298pub struct AttrId(pub u32);
299
300impl MacroCallKind { 315impl MacroCallKind {
301 fn file_id(&self) -> HirFileId { 316 fn file_id(&self) -> HirFileId {
302 match self { 317 match self {
@@ -324,6 +339,13 @@ impl MacroCallKind {
324 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()), 339 MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()),
325 } 340 }
326 } 341 }
342
343 fn fragment_kind(&self) -> FragmentKind {
344 match self {
345 MacroCallKind::FnLike { fragment, .. } => *fragment,
346 MacroCallKind::Derive { .. } => FragmentKind::Items,
347 }
348 }
327} 349}
328 350
329impl MacroCallId { 351impl MacroCallId {
@@ -357,7 +379,6 @@ pub struct ExpansionInfo {
357} 379}
358 380
359pub use mbe::Origin; 381pub use mbe::Origin;
360use parser::FragmentKind;
361 382
362impl ExpansionInfo { 383impl ExpansionInfo {
363 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> { 384 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
@@ -562,3 +583,59 @@ impl<N: AstNode> InFile<N> {
562 self.with_value(self.value.syntax()) 583 self.with_value(self.value.syntax())
563 } 584 }
564} 585}
586
587/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
588/// FIXME: Not completed
589pub fn to_fragment_kind(call: &ast::MacroCall) -> FragmentKind {
590 use syntax::SyntaxKind::*;
591
592 let syn = call.syntax();
593
594 let parent = match syn.parent() {
595 Some(it) => it,
596 None => return FragmentKind::Statements,
597 };
598
599 match parent.kind() {
600 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
601 MACRO_STMTS => FragmentKind::Statements,
602 MACRO_PAT => FragmentKind::Pattern,
603 MACRO_TYPE => FragmentKind::Type,
604 ITEM_LIST => FragmentKind::Items,
605 LET_STMT => {
606 // FIXME: Handle LHS Pattern
607 FragmentKind::Expr
608 }
609 EXPR_STMT => FragmentKind::Statements,
610 BLOCK_EXPR => FragmentKind::Statements,
611 ARG_LIST => FragmentKind::Expr,
612 TRY_EXPR => FragmentKind::Expr,
613 TUPLE_EXPR => FragmentKind::Expr,
614 PAREN_EXPR => FragmentKind::Expr,
615 ARRAY_EXPR => FragmentKind::Expr,
616 FOR_EXPR => FragmentKind::Expr,
617 PATH_EXPR => FragmentKind::Expr,
618 CLOSURE_EXPR => FragmentKind::Expr,
619 CONDITION => FragmentKind::Expr,
620 BREAK_EXPR => FragmentKind::Expr,
621 RETURN_EXPR => FragmentKind::Expr,
622 MATCH_EXPR => FragmentKind::Expr,
623 MATCH_ARM => FragmentKind::Expr,
624 MATCH_GUARD => FragmentKind::Expr,
625 RECORD_EXPR_FIELD => FragmentKind::Expr,
626 CALL_EXPR => FragmentKind::Expr,
627 INDEX_EXPR => FragmentKind::Expr,
628 METHOD_CALL_EXPR => FragmentKind::Expr,
629 FIELD_EXPR => FragmentKind::Expr,
630 AWAIT_EXPR => FragmentKind::Expr,
631 CAST_EXPR => FragmentKind::Expr,
632 REF_EXPR => FragmentKind::Expr,
633 PREFIX_EXPR => FragmentKind::Expr,
634 RANGE_EXPR => FragmentKind::Expr,
635 BIN_EXPR => FragmentKind::Expr,
636 _ => {
637 // Unknown , Just guess it is `Items`
638 FragmentKind::Items
639 }
640 }
641}