Skip to content

Add a HIR to the compiler #1191

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

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions text/0000-hir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
- Feature Name: N/A
- Start Date: 2015-07-06
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)


# Summary

Add a high-level intermediate representation (HIR) to the compiler. This is
basically a new (and additional) AST more suited for use by the compiler.

This is purely an implementation detail of the compiler. It has no effect on the
language.

Note that adding a HIR does not preclude adding a MIR or LIR in the future.


# Motivation

Currently the AST is used by libsyntax for syntactic operations, by the compiler
for pretty much everything, and in syntax extensions. I propose splitting the
AST into a libsyntax version that is specialised for syntactic operation and
will eventually be stabilised for use by syntax extensions and tools, and the
HIR which is entirely internal to the compiler.

The benefit of this split is that each AST can be specialised to its task and we
can separate the interface to the compiler (the AST) from its implementation
(the HIR). Specific changes I see that could happen are more ids and spans in
the AST, the AST adhering more closely to the surface syntax, the HIR becoming
more abstract (e.g., combining structs and enums), and using resolved names in
the HIR (i.e., performing name resolution as part of the AST->HIR lowering).

Not using the AST in the compiler means we can work to stabilise it for syntax
extensions and tools: it will become part of the interface to the compiler.

I also envisage all syntactic expansion of language constructs (e.g., `for`
loops, `if let`) moving to the lowering step from AST to HIR, rather than being
AST manipulations. That should make both error messages and tool support better
for such constructs. It would be nice to move lifetime elision to the lowering
step too, in order to make the HIR as explicit as possible.


# Detailed design

Initially, the HIR will be an (almost) identical copy of the AST and the
lowering step will simply be a copy operation. Since some constructs (macros,
`for` loops, etc.) are expanded away in libsyntax, these will not be part of the
HIR. Tools such as the AST visitor will need to be duplicated.

The compiler will be changed to use the HIR throughout (this should mostly be a
matter of change the imports). Incrementally, I expect to move expansion of
language constructs to the lowering step. Further in the future, the HIR should
get more abstract and compact, and the AST should get closer to the surface
syntax.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This RFC needs a kind of road-map of steps that need to be done (and each step should get an issue). It's too general right now.

  1. copy the AST to HIR after all AST transforms have been applied and have all other passes operate on the HIR
  2. remove some ast transforms
    • remove if let ast transform and apply it during the AST -> HIR step
    • remove while let ast transform and apply it during the AST -> HIR step
    • remove for ast transform and apply it during the AST -> HIR step
    • cfg-attribute application step?
    • erase all unresolved names from HIR and resolve names in the AST -> HIR step
    • elide lifetimes in the AST -> HIR step and erase unresolved lifetimes from the HIR
  3. things that can be moved from tables to HIR
    • i have no clue ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generalness is kind of deliberate. There's a lot we could do, what we actually do is down to prioritisation. Its also not clear how some of those steps tie into proposed work on reforming name resolution and syntax extensions. I hope this RFC spells out the first step and motivates it. Major stuff can get more RFCs (or some other kind of discussion) later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that makes sense. Maybe that statement belongs into the RFC itself? :)


# Drawbacks

Potentially slower compilations and higher memory use. However, this should be
offset in the long run by making improvements to the compiler easier by having a
more appropriate data structure.


# Alternatives

Leave things as they are.

Skip the HIR and lower straight to a MIR later in compilation. This has
advantages which adding a HIR does not have, however, it is a far more complex
refactoring and also misses some benefits of the HIR, notably being able to
stabilise the AST for tools and syntax extensions without locking in the
compiler.


# Unresolved questions

How to deal with spans and source code. We could keep the AST around and
reference back to it from the HIR. Or we could copy span information to the HIR
(I plan on doing this initially). Possibly some other solution like keeping the
span info in a side table (note that we need less span info in the compiler than
we do in libsyntax, which is in turn less than tools want).