rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

IDE feature support for `asm` macro

Open Veykril opened this issue 2 years ago • 3 comments

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

Veykril avatar Mar 04 '22 20:03 Veykril

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

fs-99 avatar Mar 01 '24 06:03 fs-99

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

Veykril avatar Mar 01 '24 09:03 Veykril

Updated my comment, and I agree GSoC is the right place for this :) They should feel free to do it, sounds fun.

fs-99 avatar Mar 01 '24 10:03 fs-99

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 whatever InlineAsmOperand is.
  • We also need to a new inline asm variant on the Item rule for global_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 the rustc_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 the is_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

Veykril avatar May 22 '24 18:05 Veykril

The GSoC doesn't seem to happen so this is up for grabs given my previous instructions fwiw

Veykril avatar Jul 07 '24 09:07 Veykril