use std::collections::BTreeMap;
use std::iter;
use syntax::ast::*;
+use syntax::errors;
use syntax::ptr::P;
use syntax::codemap::{respan, Spanned};
use syntax::parse::token;
pub struct LoweringContext<'a> {
crate_root: Option<&'static str>,
// Use to assign ids to hir nodes that do not directly correspond to an ast node
- id_assigner: &'a NodeIdAssigner,
+ sess: Option<&'a Session>,
// As we walk the AST we must keep track of the current 'parent' def id (in
// the form of a DefIndex) so that if we create a new node which introduces
// a definition, then we can properly create the def id.
pub fn lower_crate(sess: &Session,
krate: &Crate,
- id_assigner: &NodeIdAssigner,
resolver: &mut Resolver)
-> hir::Crate {
// We're constructing the HIR here; we don't care what we will
} else {
Some("std")
},
- id_assigner: id_assigner,
+ sess: Some(sess),
parent_def: None,
resolver: resolver,
}.lower_crate(krate)
}
impl<'a> LoweringContext<'a> {
- pub fn testing_context(id_assigner: &'a NodeIdAssigner, resolver: &'a mut Resolver) -> Self {
+ pub fn testing_context(resolver: &'a mut Resolver) -> Self {
LoweringContext {
crate_root: None,
- id_assigner: id_assigner,
+ sess: None,
parent_def: None,
resolver: resolver,
}
}
fn next_id(&self) -> NodeId {
- self.id_assigner.next_node_id()
+ self.sess.map(Session::next_node_id).unwrap_or(0)
+ }
+
+ fn diagnostic(&self) -> &errors::Handler {
+ self.sess.map(Session::diagnostic)
+ .unwrap_or_else(|| panic!("this lowerer cannot emit diagnostics"))
}
fn str_to_ident(&self, s: &'static str) -> Name {
if let Some(SelfKind::Explicit(..)) = sig.decl.get_self().map(|eself| eself.node) {
match hir_sig.decl.get_self().map(|eself| eself.node) {
Some(hir::SelfKind::Value(..)) | Some(hir::SelfKind::Region(..)) => {
- self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
+ self.diagnostic().span_err(sig.decl.inputs[0].ty.span,
"the type placeholder `_` is not allowed within types on item signatures");
}
_ => {}
make_struct(self, e, &["RangeInclusive", "NonEmpty"],
&[("start", e1), ("end", e2)]),
- _ => panic!(self.id_assigner.diagnostic()
+ _ => panic!(self.diagnostic()
.span_fatal(e.span, "inclusive range with no end")),
};
}
use util::nodemap::{NodeMap, FnvHashMap};
use mir::transform as mir_pass;
-use syntax::ast::{NodeId, NodeIdAssigner, Name};
+use syntax::ast::{NodeId, Name};
use errors::{self, DiagnosticBuilder};
use errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
use syntax::json::JsonEmitter;
id
}
+ pub fn next_node_id(&self) -> NodeId {
+ self.reserve_node_ids(1)
+ }
pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler {
&self.parse_sess.span_diagnostic
}
}
}
-impl NodeIdAssigner for Session {
- fn next_node_id(&self) -> NodeId {
- self.reserve_node_ids(1)
- }
-
- fn peek_node_id(&self) -> NodeId {
- self.next_node_id.get().checked_add(1).unwrap()
- }
-
- fn diagnostic(&self) -> &errors::Handler {
- self.diagnostic()
- }
-}
-
fn split_msg_into_multilines(msg: &str) -> Option<String> {
// Conditions for enabling multi-line errors:
if !msg.contains("mismatched types") &&
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
-use syntax::ast::{self, NodeIdAssigner};
+use syntax::{ast, diagnostics, visit};
use syntax::attr::{self, AttrMetaMethods};
-use syntax::diagnostics;
use syntax::fold::Folder;
use syntax::parse::{self, PResult, token};
use syntax::util::node_count::NodeCounter;
-use syntax::visit;
use syntax;
use syntax_ext;
// Lower ast -> hir.
let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || {
- hir_map::Forest::new(lower_crate(sess, krate, sess, &mut resolver), dep_graph)
+ hir_map::Forest::new(lower_crate(sess, krate, &mut resolver), dep_graph)
});
(ty::CrateAnalysis {
use rustc::ty::{self, Ty, TyCtxt};
use syntax::ast;
-use syntax::ast::NodeIdAssigner;
use syntax::ptr::P;
use syntax_pos;
#[cfg(test)] use std::io::Cursor;
#[cfg(test)] use syntax::parse;
-#[cfg(test)] use syntax::ast::NodeId;
#[cfg(test)] use rustc::hir::print as pprust;
#[cfg(test)] use rustc::hir::lowering::{LoweringContext, DummyResolver};
fn parse_sess(&self) -> &parse::ParseSess { self }
}
-#[cfg(test)]
-struct FakeNodeIdAssigner;
-
-#[cfg(test)]
-// It should go without saying that this may give unexpected results. Avoid
-// lowering anything which needs new nodes.
-impl NodeIdAssigner for FakeNodeIdAssigner {
- fn next_node_id(&self) -> NodeId {
- 0
- }
-
- fn peek_node_id(&self) -> NodeId {
- 0
- }
-}
-
#[cfg(test)]
fn mk_ctxt() -> parse::ParseSess {
parse::ParseSess::new()
#[cfg(test)]
fn with_testing_context<T, F: FnOnce(&mut LoweringContext) -> T>(f: F) -> T {
- let assigner = FakeNodeIdAssigner;
let mut resolver = DummyResolver;
- let mut lcx = LoweringContext::testing_context(&assigner, &mut resolver);
+ let mut lcx = LoweringContext::testing_context(&mut resolver);
f(&mut lcx)
}
use syntax_pos::{mk_sp, Span, DUMMY_SP, ExpnId};
use codemap::{respan, Spanned};
use abi::Abi;
-use errors;
use parse::token::{self, keywords, InternedString};
use print::pprust;
use ptr::P;
/// small, positive ids.
pub const DUMMY_NODE_ID: NodeId = !0;
-pub trait NodeIdAssigner {
- fn next_node_id(&self) -> NodeId;
- fn peek_node_id(&self) -> NodeId;
-
- fn diagnostic(&self) -> &errors::Handler {
- panic!("this ID assigner cannot emit diagnostics")
- }
-}
-
/// The AST represents all type param bounds as types.
/// typeck::collect::compute_bounds matches these against
/// the "special" built-in traits (see middle::lang_items) and