Skip to content

Commit

Permalink
lowering: connect to parser via function pointer instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 13, 2019
1 parent 1899432 commit 07e946c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
18 changes: 8 additions & 10 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
20 changes: 4 additions & 16 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 07e946c

Please sign in to comment.