1
1
# The HIR
2
2
3
- The HIR – "High-level IR" – is the primary IR used in most of
4
- rustc. It is a desugared version of the "abstract syntax tree" (AST)
5
- that is generated after parsing, macro expansion, and name resolution
6
- have completed. Many parts of HIR resemble Rust surface syntax quite
7
- closely, with the exception that some of Rust's expression forms have
8
- been desugared away (as an example, ` for ` loops are converted into a
9
- ` loop ` and do not appear in the HIR).
3
+ The HIR – "High-level IR" – is the primary IR used in most of rustc.
4
+ It is a desugared version of the "abstract syntax tree" (AST) that is generated
5
+ after parsing, macro expansion, and name resolution have completed. Many parts
6
+ of HIR resemble Rust surface syntax quite closely, with the exception that some
7
+ of Rust's expression forms have been desugared away (as an example, ` for ` loops
8
+ are converted into a ` loop ` and do not appear in the HIR).
10
9
11
10
This chapter covers the main concepts of the HIR.
12
11
@@ -21,34 +20,34 @@ serve to organize the content of the crate for easier access.
21
20
22
21
For example, the contents of individual items (e.g., modules,
23
22
functions, traits, impls, etc) in the HIR are not immediately
24
- accessible in the parents. So, for example, if had a module item ` foo `
25
- containing a function ` bar() ` :
23
+ accessible in the parents. So, for example, if there is a module item
24
+ ` foo ` containing a function ` bar() ` :
26
25
27
26
```
28
27
mod foo {
29
28
fn bar() { }
30
29
}
31
30
```
32
31
33
- Then in the HIR the representation of module ` foo ` (the ` Mod `
34
- stuct) would have only the ** ` ItemId ` ** ` I ` of ` bar() ` . To get the
32
+ then in the HIR the representation of module ` foo ` (the ` Mod `
33
+ stuct) would only have the ** ` ItemId ` ** ` I ` of ` bar() ` . To get the
35
34
details of the function ` bar() ` , we would lookup ` I ` in the
36
35
` items ` map.
37
36
38
37
One nice result from this representation is that one can iterate
39
38
over all items in the crate by iterating over the key-value pairs
40
- in these maps (without the need to trawl through the IR in total ).
39
+ in these maps (without the need to trawl through the whole HIR ).
41
40
There are similar maps for things like trait items and impl items,
42
41
as well as "bodies" (explained below).
43
42
44
- The other reason to setup the representation this way is for better
43
+ The other reason to set up the representation this way is for better
45
44
integration with incremental compilation. This way, if you gain access
46
- to a ` &hir::Item ` (e.g. for the mod ` foo ` ), you do not immediately
45
+ to an ` &hir::Item ` (e.g. for the mod ` foo ` ), you do not immediately
47
46
gain access to the contents of the function ` bar() ` . Instead, you only
48
47
gain access to the ** id** for ` bar() ` , and you must invoke some
49
- function to lookup the contents of ` bar() ` given its id; this gives us
50
- a chance to observe that you accessed the data for ` bar() ` and record
51
- the dependency.
48
+ function to lookup the contents of ` bar() ` given its id; this gives the
49
+ compiler a chance to observe that you accessed the data for ` bar() ` ,
50
+ and then record the dependency.
52
51
53
52
### Identifiers in the HIR
54
53
@@ -57,37 +56,35 @@ carry around references into the HIR, but rather to carry around
57
56
* identifier numbers* (or just "ids"). Right now, you will find four
58
57
sorts of identifiers in active use:
59
58
60
- - ` DefId ` , which primarily names "definitions" or top-level items.
61
- - You can think of a ` DefId ` as being shorthand for a very explicit
62
- and complete path, like ` std::collections::HashMap ` . However,
63
- these paths are able to name things that are not nameable in
64
- normal Rust (e.g., impls), and they also include extra information
65
- about the crate (such as its version number, as two versions of
66
- the same crate can co-exist).
67
- - A ` DefId ` really consists of two parts, a ` CrateNum ` (which
68
- identifies the crate) and a ` DefIndex ` (which indixes into a list
69
- of items that is maintained per crate).
70
- - ` HirId ` , which combines the index of a particular item with an
71
- offset within that item.
72
- - the key point of a ` HirId ` is that it is * relative* to some item (which is named
73
- via a ` DefId ` ).
74
- - ` BodyId ` , this is an absolute identifier that refers to a specific
75
- body (definition of a function or constant) in the crate. It is currently
76
- effectively a "newtype'd" ` NodeId ` .
77
- - ` NodeId ` , which is an absolute id that identifies a single node in the HIR tree.
59
+ - ` DefId ` – primarily names "definitions" or top-level items.
60
+ - You can think of a ` DefId ` as shorthand for a very explicit and complete
61
+ path, like ` std::collections::HashMap ` . However, these paths are able to
62
+ name things that are not nameable in normal Rust (e.g. impls), and they also
63
+ include extra information about the crate (such as its version number, since
64
+ two versions of the same crate can co-exist).
65
+ - A ` DefId ` really consists of two parts, a ` CrateNum ` (which identifies the
66
+ crate) and a ` DefIndex ` (which indexes into a list of items that is
67
+ maintained per crate).
68
+ - ` HirId ` – combines the index of a particular item with an offset within
69
+ that item.
70
+ - The key point of an ` HirId ` is that it is * relative* to some item (which is
71
+ named via a ` DefId ` ).
72
+ - ` BodyId ` – an absolute identifier that refers to a specific body (definition
73
+ of a function or constant) in the crate. It is currently effectively a
74
+ "newtype'd" ` NodeId ` .
75
+ - ` NodeId ` – an absolute ID that identifies a single node in the HIR tree.
78
76
- While these are still in common use, ** they are being slowly phased out** .
79
- - Since they are absolute within the crate, adding a new node
80
- anywhere in the tree causes the node-ids of all subsequent code in
81
- the crate to change. This is terrible for incremental compilation,
82
- as you can perhaps imagine.
77
+ - Since they are absolute within the crate, adding a new node anywhere in the
78
+ tree causes the ` NodeId ` s of all subsequent code in the crate to change.
79
+ This is terrible for incremental compilation, as you can perhaps imagine.
83
80
84
- ### HIR Map
81
+ ### The HIR Map
85
82
86
83
Most of the time when you are working with the HIR, you will do so via
87
84
the ** HIR Map** , accessible in the tcx via ` tcx.hir ` (and defined in
88
85
the ` hir::map ` module). The HIR map contains a number of methods to
89
- convert between ids of various kinds and to lookup data associated
90
- with a HIR node.
86
+ convert between IDs of various kinds and to lookup data associated
87
+ with an HIR node.
91
88
92
89
For example, if you have a ` DefId ` , and you would like to convert it
93
90
to a ` NodeId ` , you can use ` tcx.hir.as_local_node_id(def_id) ` . This
@@ -100,7 +97,7 @@ Similarly, you can use `tcx.hir.find(n)` to lookup the node for a
100
97
` NodeId ` . This returns a ` Option<Node<'tcx>> ` , where ` Node ` is an enum
101
98
defined in the map; by matching on this you can find out what sort of
102
99
node the node-id referred to and also get a pointer to the data
103
- itself. Often, you know what sort of node ` n ` is – e.g., if you know
100
+ itself. Often, you know what sort of node ` n ` is – e.g. if you know
104
101
that ` n ` must be some HIR expression, you can do
105
102
` tcx.hir.expect_expr(n) ` , which will extract and return the
106
103
` &hir::Expr ` , panicking if ` n ` is not in fact an expression.
@@ -113,7 +110,7 @@ calls like `tcx.hir.get_parent_node(n)`.
113
110
A ** body** represents some kind of executable code, such as the body
114
111
of a function/closure or the definition of a constant. Bodies are
115
112
associated with an ** owner** , which is typically some kind of item
116
- (e.g., a ` fn() ` or ` const ` ), but could also be a closure expression
117
- (e.g., ` |x, y| x + y ` ). You can use the HIR map to find the body
118
- associated with a given def-id (` maybe_body_owned_by() ` ) or to find
113
+ (e.g. an ` fn() ` or ` const ` ), but could also be a closure expression
114
+ (e.g. ` |x, y| x + y ` ). You can use the HIR map to find the body
115
+ associated with a given ` DefId ` (` maybe_body_owned_by() ` ) or to find
119
116
the owner of a body (` body_owner_def_id() ` ).
0 commit comments