lowering: connect to parser via function pointer instead

This commit is contained in:
Mazdak Farrokhzad 2019-10-13 23:27:18 +02:00
parent 1899432867
commit 07e946caf7
2 changed files with 12 additions and 26 deletions

View file

@ -87,7 +87,10 @@ pub struct LoweringContext<'a> {
resolver: &'a mut dyn Resolver,
parser: &'static dyn Parser,
/// HACK(Centril): there is a cyclic dependency between the parser and lowering
/// if we don't have this function pointer. To avoid that dependency so that
/// librustc is independent of the parser, we use dynamic dispatch here.
nt_to_tokenstream: NtToTokenstream,
/// The items being lowered are collected here.
items: BTreeMap<hir::HirId, hir::Item>,
@ -183,12 +186,7 @@ pub trait Resolver {
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool;
}
/// HACK(Centril): there is a cyclic dependency between the parser and lowering
/// if we don't have this trait. To avoid that dependency so that librustc is
/// independent of the parser, we use type erasure here.
pub trait Parser {
fn nt_to_tokenstream(&self, nt: &Nonterminal, sess: &ParseSess, span: Span) -> TokenStream;
}
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
/// and if so, what meaning it has.
@ -246,7 +244,7 @@ pub fn lower_crate(
dep_graph: &DepGraph,
krate: &Crate,
resolver: &mut dyn Resolver,
parser: &'static dyn Parser,
nt_to_tokenstream: NtToTokenstream,
) -> hir::Crate {
// We're constructing the HIR here; we don't care what we will
// read, since we haven't even constructed the *input* to
@ -260,7 +258,7 @@ pub fn lower_crate(
sess,
cstore,
resolver,
parser,
nt_to_tokenstream,
items: BTreeMap::new(),
trait_items: BTreeMap::new(),
impl_items: BTreeMap::new(),
@ -1034,7 +1032,7 @@ impl<'a> LoweringContext<'a> {
fn lower_token(&mut self, token: Token) -> TokenStream {
match token.kind {
token::Interpolated(nt) => {
let tts = self.parser.nt_to_tokenstream(&nt, &self.sess.parse_sess, token.span);
let tts = (self.nt_to_tokenstream)(&nt, &self.sess.parse_sess, token.span);
self.lower_token_stream(tts)
}
_ => TokenTree::Token(token).into(),

View file

@ -5,7 +5,7 @@ use crate::proc_macro_decls;
use log::{info, warn, log_enabled};
use rustc::dep_graph::DepGraph;
use rustc::hir;
use rustc::hir::lowering::{lower_crate, Parser};
use rustc::hir::lowering::lower_crate;
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc::lint;
use rustc::middle::{self, reachable, resolve_lifetime, stability};
@ -38,12 +38,9 @@ use syntax::early_buffered_lints::BufferedEarlyLint;
use syntax::ext::base::{NamedSyntaxExtension, ExtCtxt};
use syntax::mut_visit::MutVisitor;
use syntax::parse::{self, PResult};
use syntax::parse::token::Nonterminal;
use syntax::parse::ParseSess;
use syntax::tokenstream::TokenStream;
use syntax::util::node_count::NodeCounter;
use syntax::symbol::Symbol;
use syntax_pos::{FileName, Span};
use syntax_pos::FileName;
use syntax_ext;
use rustc_serialize::json;
@ -535,16 +532,6 @@ fn configure_and_expand_inner<'a>(
Ok((krate, resolver))
}
fn parser() -> &'static dyn Parser {
struct Parse;
impl Parser for Parse {
fn nt_to_tokenstream(&self, nt: &Nonterminal, sess: &ParseSess, span: Span) -> TokenStream {
syntax::parse::nt_to_tokenstream(nt, sess, span)
}
}
&Parse
}
pub fn lower_to_hir(
sess: &Session,
cstore: &CStore,
@ -554,7 +541,8 @@ pub fn lower_to_hir(
) -> Result<hir::map::Forest> {
// Lower AST to HIR.
let hir_forest = time(sess, "lowering AST -> HIR", || {
let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, parser());
let nt_to_tokenstream = syntax::parse::nt_to_tokenstream;
let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, nt_to_tokenstream);
if sess.opts.debugging_opts.hir_stats {
hir_stats::print_hir_stats(&hir_crate);