about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/crates/syntax
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2024-09-05 12:40:41 +0200
committerLukas Wirth <lukastw97@gmail.com>2024-09-05 12:40:48 +0200
commitf3b6965f90e764fc29ff627a6965ccfc68d38ead (patch)
tree25dc9463dc851470d0651309d8028c92475e75e4 /src/tools/rust-analyzer/crates/syntax
parent304f54e023cf5784163a319b22316950c71c58f2 (diff)
Give InlineAsmOperand a HIR representation
Diffstat (limited to 'src/tools/rust-analyzer/crates/syntax')
-rw-r--r--src/tools/rust-analyzer/crates/syntax/rust.ungram8
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs109
2 files changed, 89 insertions, 28 deletions
diff --git a/src/tools/rust-analyzer/crates/syntax/rust.ungram b/src/tools/rust-analyzer/crates/syntax/rust.ungram
index 5dace66c32d..52ad439e4de 100644
--- a/src/tools/rust-analyzer/crates/syntax/rust.ungram
+++ b/src/tools/rust-analyzer/crates/syntax/rust.ungram
@@ -395,7 +395,7 @@ OffsetOfExpr =
 // global_asm := "global_asm!(" format_string *("," format_string) *("," operand) [","] ")"
 // format_string := STRING_LITERAL / RAW_STRING_LITERAL
 AsmExpr =
-  Attr* 'builtin' '#' 'asm' '(' template:(Expr (',' Expr)*) (AsmOperand (',' AsmOperand)*)? ','? ')'
+  Attr* 'builtin' '#' 'asm' '(' template:(Expr (',' Expr)*) (AsmPiece (',' AsmPiece)*)? ','? ')'
 
 // operand_expr := expr / "_" / expr "=>" expr / expr "=>" "_"
 AsmOperandExpr = in_expr:Expr ('=>' out_expr:Expr)?
@@ -404,7 +404,7 @@ AsmDirSpec = 'in' | 'out' | 'lateout' | 'inout' | 'inlateout'
 // reg_spec := <register class> / "\"" <explicit register> "\""
 AsmRegSpec = '@string' | NameRef
 // reg_operand := [ident "="] dir_spec "(" reg_spec ")" operand_expr
-AsmRegOperand = (Name '=')? AsmDirSpec '(' AsmRegSpec ')' AsmOperandExpr
+AsmRegOperand = AsmDirSpec '(' AsmRegSpec ')' AsmOperandExpr
 // clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
 AsmClobberAbi = 'clobber_abi' '(' ('@string' (',' '@string')* ','?) ')'
 // option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" / "raw"
@@ -415,7 +415,9 @@ AsmLabel = 'label' BlockExpr
 AsmSym = 'sym' Path
 AsmConst = 'const' Expr
 // operand := reg_operand / clobber_abi / options
-AsmOperand = AsmRegOperand | AsmClobberAbi | AsmOptions | AsmLabel | AsmSym | AsmConst
+AsmOperand = AsmRegOperand | AsmLabel | AsmSym | AsmConst
+AsmOperandNamed = (Name '=')? AsmOperand
+AsmPiece = AsmOperandNamed | AsmClobberAbi | AsmOptions
 
 FormatArgsExpr =
   Attr* 'builtin' '#' 'format_args' '('
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs
index 01d47c34bbd..c81a19f3bda 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs
@@ -118,7 +118,7 @@ pub struct AsmExpr {
 impl ast::HasAttrs for AsmExpr {}
 impl AsmExpr {
     #[inline]
-    pub fn asm_operands(&self) -> AstChildren<AsmOperand> { support::children(&self.syntax) }
+    pub fn asm_pieces(&self) -> AstChildren<AsmPiece> { support::children(&self.syntax) }
     #[inline]
     pub fn template(&self) -> AstChildren<Expr> { support::children(&self.syntax) }
     #[inline]
@@ -160,6 +160,18 @@ impl AsmOperandExpr {
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct AsmOperandNamed {
+    pub(crate) syntax: SyntaxNode,
+}
+impl ast::HasName for AsmOperandNamed {}
+impl AsmOperandNamed {
+    #[inline]
+    pub fn asm_operand(&self) -> Option<AsmOperand> { support::child(&self.syntax) }
+    #[inline]
+    pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct AsmOption {
     pub(crate) syntax: SyntaxNode,
 }
@@ -217,7 +229,6 @@ impl AsmOptions {
 pub struct AsmRegOperand {
     pub(crate) syntax: SyntaxNode,
 }
-impl ast::HasName for AsmRegOperand {}
 impl AsmRegOperand {
     #[inline]
     pub fn asm_dir_spec(&self) -> Option<AsmDirSpec> { support::child(&self.syntax) }
@@ -229,8 +240,6 @@ impl AsmRegOperand {
     pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
     #[inline]
     pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
-    #[inline]
-    pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -2224,15 +2233,20 @@ impl ast::HasVisibility for Adt {}
 
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub enum AsmOperand {
-    AsmClobberAbi(AsmClobberAbi),
     AsmConst(AsmConst),
     AsmLabel(AsmLabel),
-    AsmOptions(AsmOptions),
     AsmRegOperand(AsmRegOperand),
     AsmSym(AsmSym),
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum AsmPiece {
+    AsmClobberAbi(AsmClobberAbi),
+    AsmOperandNamed(AsmOperandNamed),
+    AsmOptions(AsmOptions),
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub enum AssocItem {
     Const(Const),
     Fn(Fn),
@@ -2581,6 +2595,20 @@ impl AstNode for AsmOperandExpr {
     #[inline]
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
+impl AstNode for AsmOperandNamed {
+    #[inline]
+    fn can_cast(kind: SyntaxKind) -> bool { kind == ASM_OPERAND_NAMED }
+    #[inline]
+    fn cast(syntax: SyntaxNode) -> Option<Self> {
+        if Self::can_cast(syntax.kind()) {
+            Some(Self { syntax })
+        } else {
+            None
+        }
+    }
+    #[inline]
+    fn syntax(&self) -> &SyntaxNode { &self.syntax }
+}
 impl AstNode for AsmOption {
     #[inline]
     fn can_cast(kind: SyntaxKind) -> bool { kind == ASM_OPTION }
@@ -4589,10 +4617,6 @@ impl AstNode for Adt {
         }
     }
 }
-impl From<AsmClobberAbi> for AsmOperand {
-    #[inline]
-    fn from(node: AsmClobberAbi) -> AsmOperand { AsmOperand::AsmClobberAbi(node) }
-}
 impl From<AsmConst> for AsmOperand {
     #[inline]
     fn from(node: AsmConst) -> AsmOperand { AsmOperand::AsmConst(node) }
@@ -4601,10 +4625,6 @@ impl From<AsmLabel> for AsmOperand {
     #[inline]
     fn from(node: AsmLabel) -> AsmOperand { AsmOperand::AsmLabel(node) }
 }
-impl From<AsmOptions> for AsmOperand {
-    #[inline]
-    fn from(node: AsmOptions) -> AsmOperand { AsmOperand::AsmOptions(node) }
-}
 impl From<AsmRegOperand> for AsmOperand {
     #[inline]
     fn from(node: AsmRegOperand) -> AsmOperand { AsmOperand::AsmRegOperand(node) }
@@ -4616,18 +4636,13 @@ impl From<AsmSym> for AsmOperand {
 impl AstNode for AsmOperand {
     #[inline]
     fn can_cast(kind: SyntaxKind) -> bool {
-        matches!(
-            kind,
-            ASM_CLOBBER_ABI | ASM_CONST | ASM_LABEL | ASM_OPTIONS | ASM_REG_OPERAND | ASM_SYM
-        )
+        matches!(kind, ASM_CONST | ASM_LABEL | ASM_REG_OPERAND | ASM_SYM)
     }
     #[inline]
     fn cast(syntax: SyntaxNode) -> Option<Self> {
         let res = match syntax.kind() {
-            ASM_CLOBBER_ABI => AsmOperand::AsmClobberAbi(AsmClobberAbi { syntax }),
             ASM_CONST => AsmOperand::AsmConst(AsmConst { syntax }),
             ASM_LABEL => AsmOperand::AsmLabel(AsmLabel { syntax }),
-            ASM_OPTIONS => AsmOperand::AsmOptions(AsmOptions { syntax }),
             ASM_REG_OPERAND => AsmOperand::AsmRegOperand(AsmRegOperand { syntax }),
             ASM_SYM => AsmOperand::AsmSym(AsmSym { syntax }),
             _ => return None,
@@ -4637,15 +4652,49 @@ impl AstNode for AsmOperand {
     #[inline]
     fn syntax(&self) -> &SyntaxNode {
         match self {
-            AsmOperand::AsmClobberAbi(it) => &it.syntax,
             AsmOperand::AsmConst(it) => &it.syntax,
             AsmOperand::AsmLabel(it) => &it.syntax,
-            AsmOperand::AsmOptions(it) => &it.syntax,
             AsmOperand::AsmRegOperand(it) => &it.syntax,
             AsmOperand::AsmSym(it) => &it.syntax,
         }
     }
 }
+impl From<AsmClobberAbi> for AsmPiece {
+    #[inline]
+    fn from(node: AsmClobberAbi) -> AsmPiece { AsmPiece::AsmClobberAbi(node) }
+}
+impl From<AsmOperandNamed> for AsmPiece {
+    #[inline]
+    fn from(node: AsmOperandNamed) -> AsmPiece { AsmPiece::AsmOperandNamed(node) }
+}
+impl From<AsmOptions> for AsmPiece {
+    #[inline]
+    fn from(node: AsmOptions) -> AsmPiece { AsmPiece::AsmOptions(node) }
+}
+impl AstNode for AsmPiece {
+    #[inline]
+    fn can_cast(kind: SyntaxKind) -> bool {
+        matches!(kind, ASM_CLOBBER_ABI | ASM_OPERAND_NAMED | ASM_OPTIONS)
+    }
+    #[inline]
+    fn cast(syntax: SyntaxNode) -> Option<Self> {
+        let res = match syntax.kind() {
+            ASM_CLOBBER_ABI => AsmPiece::AsmClobberAbi(AsmClobberAbi { syntax }),
+            ASM_OPERAND_NAMED => AsmPiece::AsmOperandNamed(AsmOperandNamed { syntax }),
+            ASM_OPTIONS => AsmPiece::AsmOptions(AsmOptions { syntax }),
+            _ => return None,
+        };
+        Some(res)
+    }
+    #[inline]
+    fn syntax(&self) -> &SyntaxNode {
+        match self {
+            AsmPiece::AsmClobberAbi(it) => &it.syntax,
+            AsmPiece::AsmOperandNamed(it) => &it.syntax,
+            AsmPiece::AsmOptions(it) => &it.syntax,
+        }
+    }
+}
 impl From<Const> for AssocItem {
     #[inline]
     fn from(node: Const) -> AssocItem { AssocItem::Const(node) }
@@ -6181,7 +6230,7 @@ impl AstNode for AnyHasName {
     fn can_cast(kind: SyntaxKind) -> bool {
         matches!(
             kind,
-            ASM_REG_OPERAND
+            ASM_OPERAND_NAMED
                 | CONST
                 | CONST_PARAM
                 | ENUM
@@ -6211,9 +6260,9 @@ impl AstNode for AnyHasName {
     #[inline]
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
-impl From<AsmRegOperand> for AnyHasName {
+impl From<AsmOperandNamed> for AnyHasName {
     #[inline]
-    fn from(node: AsmRegOperand) -> AnyHasName { AnyHasName { syntax: node.syntax } }
+    fn from(node: AsmOperandNamed) -> AnyHasName { AnyHasName { syntax: node.syntax } }
 }
 impl From<Const> for AnyHasName {
     #[inline]
@@ -6460,6 +6509,11 @@ impl std::fmt::Display for AsmOperand {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
+impl std::fmt::Display for AsmPiece {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        std::fmt::Display::fmt(self.syntax(), f)
+    }
+}
 impl std::fmt::Display for AssocItem {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
@@ -6560,6 +6614,11 @@ impl std::fmt::Display for AsmOperandExpr {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
+impl std::fmt::Display for AsmOperandNamed {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        std::fmt::Display::fmt(self.syntax(), f)
+    }
+}
 impl std::fmt::Display for AsmOption {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)