Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Navigating the compiler from the outside" #74

Closed
Michael-F-Bryan opened this issue Mar 4, 2018 · 7 comments · Fixed by #76
Closed

"Navigating the compiler from the outside" #74

Michael-F-Bryan opened this issue Mar 4, 2018 · 7 comments · Fixed by #76

Comments

@Michael-F-Bryan
Copy link
Contributor

I'm currently working on a project which uses rustc internals to analyse a crate so I can then generate FFI bindings. Kinda like cbindgen, except where they focus on static linking at compile time or dynamically when your program gets loaded into memory, I'm trying to load a dynamic library at runtime using dlopen (*nix) and LoadLibrary (windows).

Seeing as this is a guide on how rustc works under the hood, would it be worth adding a chapter that mentions:

  • how people can use rustc internals as a library,
  • how you ask rustc_driver to run some code after a particular phase, and
  • how you can extract information from an analysed crate?

My code is on GitLab so people in a similar situation or writing this chapter can use that as a reference.


This is slightly off topic, but I'm really impressed with how easy the compiler internals (especially the HIR and Session) are to work with once you understand how they're designed! It was initially a bit confusing because there are loads of data structures (unfortunately, not many have doc-comments) and lots of complexity, but after an hour or two of jumping around the crate docs I was able to be quite proficient in finding the information I want.

@Michael-F-Bryan
Copy link
Contributor Author

Also, I remember seeing a tutorial a while back which steps you through using rustc_driver and rustc internals to analyse a crate's AST. It was published on GitHub by someone on the core team (maybe @alexcrichton?) and I think it did something like count the number of print statements, or another seemingly trivial task.

It'd be nice to add a link to those sorts of tutorials if they aren't too outdated because they help to give you a starting point to build off of.

@mark-i-m
Copy link
Member

mark-i-m commented Mar 4, 2018

Hmm... So far the guide had been about how the compiler itself works, but I'm not opposed to adding this here. We could also add the shiny new rustdoc README...

@Michael-F-Bryan
Copy link
Contributor Author

On second thoughts I don't think it'd be necessary to write anything specific to those using rustc internals as a library, per-se. It's probably enough to talk about how rustc_driver drives the entire compilation process and the ways people are able to hook in at various stages.

We could also add the shiny new rustdoc README

Rustdoc has a new README? 😮

@mark-i-m
Copy link
Member

mark-i-m commented Mar 4, 2018

I frankly don't know that much about this topic, so I would defer to you on what would be most useful in such a chapter :)

Rustdoc has a new README? 😮

Indeed! rust-lang/rust#48283

@alexcrichton
Copy link
Member

maybe @alexcrichton?

Unfortunately not me :(

@ehuss
Copy link
Contributor

ehuss commented Mar 4, 2018

a tutorial a while back which steps you through using rustc_driver and rustc internals to analyse a crate's AST

Perhaps you are thinking of https://github.com/nrc/stupid-stats?

@Michael-F-Bryan
Copy link
Contributor Author

Perhaps you are thinking of https://github.com/nrc/stupid-stats?

Yep, that's the one! Given how the compiler is currently going from being pass-based to query-based, does anyone know if this CompilerCalls mechanism still the best way to use rustc from the outside?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants