Skip to content

Commit 069a244

Browse files
committed
Add exprs map to crate, collect item blocks there
1 parent 1c44857 commit 069a244

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/librustc/hir/lowering.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ use hir::def_id::{DefIndex, DefId};
4747
use hir::def::{Def, PathResolution};
4848
use session::Session;
4949
use util::nodemap::NodeMap;
50+
use rustc_data_structures::fnv::FnvHashMap;
5051

5152
use std::collections::BTreeMap;
5253
use std::iter;
54+
use std::mem;
55+
5356
use syntax::ast::*;
5457
use syntax::errors;
5558
use syntax::ptr::P;
@@ -68,6 +71,7 @@ pub struct LoweringContext<'a> {
6871
// the form of a DefIndex) so that if we create a new node which introduces
6972
// a definition, then we can properly create the def id.
7073
parent_def: Option<DefIndex>,
74+
exprs: FnvHashMap<hir::ExprId, hir::Expr>,
7175
resolver: &'a mut Resolver,
7276

7377
/// The items being lowered are collected here.
@@ -104,6 +108,7 @@ pub fn lower_crate(sess: &Session,
104108
crate_root: std_inject::injected_crate_name(krate),
105109
sess: sess,
106110
parent_def: None,
111+
exprs: FnvHashMap(),
107112
resolver: resolver,
108113
items: BTreeMap::new(),
109114
impl_items: BTreeMap::new(),
@@ -120,6 +125,23 @@ enum ParamMode {
120125

121126
impl<'a> LoweringContext<'a> {
122127
fn lower_crate(mut self, c: &Crate) -> hir::Crate {
128+
self.lower_items(c);
129+
let module = self.lower_mod(&c.module);
130+
let attrs = self.lower_attrs(&c.attrs);
131+
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
132+
133+
hir::Crate {
134+
module: module,
135+
attrs: attrs,
136+
span: c.span,
137+
exported_macros: exported_macros,
138+
items: self.items,
139+
impl_items: self.impl_items,
140+
exprs: mem::replace(&mut self.exprs, FnvHashMap()),
141+
}
142+
}
143+
144+
fn lower_items(&mut self, c: &Crate) {
123145
struct ItemLowerer<'lcx, 'interner: 'lcx> {
124146
lctx: &'lcx mut LoweringContext<'interner>,
125147
}
@@ -139,16 +161,14 @@ impl<'a> LoweringContext<'a> {
139161
}
140162
}
141163

142-
visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c);
164+
let mut item_lowerer = ItemLowerer { lctx: self };
165+
visit::walk_crate(&mut item_lowerer, c);
166+
}
143167

144-
hir::Crate {
145-
module: self.lower_mod(&c.module),
146-
attrs: self.lower_attrs(&c.attrs),
147-
span: c.span,
148-
exported_macros: c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect(),
149-
items: self.items,
150-
impl_items: self.impl_items,
151-
}
168+
fn record_expr(&mut self, expr: hir::Expr) -> hir::ExprId {
169+
let id = hir::ExprId(expr.id);
170+
self.exprs.insert(id, expr);
171+
id
152172
}
153173

154174
fn next_id(&self) -> NodeId {

src/librustc/hir/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub use self::PathParameters::*;
3333
use hir::def::Def;
3434
use hir::def_id::DefId;
3535
use util::nodemap::{NodeMap, FxHashSet};
36+
use rustc_data_structures::fnv::FnvHashMap;
3637

3738
use syntax_pos::{mk_sp, Span, ExpnId, DUMMY_SP};
3839
use syntax::codemap::{self, respan, Spanned};
@@ -428,6 +429,7 @@ pub struct Crate {
428429
pub items: BTreeMap<NodeId, Item>,
429430

430431
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
432+
pub exprs: FnvHashMap<ExprId, Expr>,
431433
}
432434

433435
impl Crate {
@@ -846,6 +848,15 @@ pub enum UnsafeSource {
846848
UserProvided,
847849
}
848850

851+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
852+
pub struct ExprId(NodeId);
853+
854+
impl ExprId {
855+
pub fn node_id(self) -> NodeId {
856+
self.0
857+
}
858+
}
859+
849860
/// An expression
850861
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
851862
pub struct Expr {

0 commit comments

Comments
 (0)