rust-analyzer
rust-analyzer copied to clipboard
IDE feature support for `asm` macro
We should be able to support asm macro calls with more features like name resolution for locals passed to it, completions, semantic highlighting etc
cc https://github.com/rust-analyzer/rust-analyzer/issues/6031
I would love to see all the sym
usage sites in the references view for a function.
Imagine a kernel function getting called from assembly called kernel_init
. The references view for kernel_init
should lead me to the asm block sym
where I use it.
Where would I have to start to implement this without implementing everything else too? :smile:
edited
Could you elaborate on the project you are referring to? 😅
Also someone might be picking this up as a GSoC project: https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/GSoC.20Proposal.20for.20Rust.20Analyzer
Updated my comment, and I agree GSoC is the right place for this :) They should feel free to do it, sounds fun.
Quoting myself from zulip:
Alright, basically the asm macro (like the format_args
one) expands to a special placeholder syntax internally (builtin#asm(template_string0, template_string1, ..., fmt_arg0, fmt_arg1, ...)
). The template strings behave like the format_arg!
one mostly, so a lot of similar stuff can be done there, and if all you want for now is get completions in the templates working you can probably copy a lot from it there.
The first thing that needs to be done is adjust the ungram grammar file and parser to parse that correctly, then lowering needs to be done, and finally you can add the completion stuff and whatever other IDE features you'd like:
- Adjust https://github.com/rust-analyzer/rust-analyzer/blob/760ad445e2037868ef40dd3cddb4e1f66bdc7e71/crates/syntax/rust.ungram#L384-L385 to somethign that resembles the macro inputs more correctly. I don't knoiw the concrete grammar it takes, but its probably gonna look something like
Attr* 'builtin' '#' 'asm' '(' Expr (',' Expr)* (',' InlineAsmOperand)* ')'
+ (a) new rule(s) for whateverInlineAsmOperand
is. - We also need to a new inline asm variant on the
Item
rule forglobal_asm
, though you can skip that for now, getting that working afterwords should be trivial. - Now run
cargo codegen grammar
once to update the syntax tree definitions - Then adjust the parsing here https://github.com/rust-lang/rust-analyzer/blob/760ad445e2037868ef40dd3cddb4e1f66bdc7e71/crates/parser/src/grammar/expressions/atom.rs#L274-L280 to accomodate your changes
- Now its time to lower the AST to our intermediate representation, the entry point for that is here https://github.com/rust-lang/rust-analyzer/blob/760ad445e2037868ef40dd3cddb4e1f66bdc7e71/crates/hir-def/src/body/lower.rs#L648-L651. This is where I'd recommend reading through what the
format_args
lowering does 2 match arms down. For reference for how rust-lang/rust uses therustc_parse_format
crate for the asm expansion you can take a look here https://github.com/rust-lang/rust/blob/4c5e95494f9844130921deb5fa06c5c1b27f4d25/compiler/rustc_builtin_macros/src/asm.rs#L46. Note that they do the expansion on the AST (in the acutal macro expansion) where as we do the expansion on the ast to hir lowering. - Compeltions for
format_args
template strings are done here https://github.com/rust-lang/rust-analyzer/blob/760ad445e2037868ef40dd3cddb4e1f66bdc7e71/crates/ide-completion/src/completions/format_string.rs, you'll likely only need to adjust theis_format_string
check to accomodate ofr the asm macro as well
An aside, test fixtures in the IDE layer, like those in the completions crate do not have access to the standard library, the special first line //- minicore: fmt
gives your test fixture a stripped down core
library, https://github.com/rust-lang/rust-analyzer/blob/760ad445e2037868ef40dd3cddb4e1f66bdc7e71/crates/test-utils/src/minicore.rs the comment at the top explains that
The GSoC doesn't seem to happen so this is up for grabs given my previous instructions fwiw