Skip to content

Commit

Permalink
cfg_eval: Configure everything through mutable visitor methods
Browse files Browse the repository at this point in the history
This is simpler and mirrors what invocation collector does
  • Loading branch information
petrochenkov committed Mar 6, 2021
1 parent f9019b7 commit 10ed08f
Showing 1 changed file with 27 additions and 70 deletions.
97 changes: 27 additions & 70 deletions compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::util::check_builtin_macro_attribute;
use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::ptr::P;
use rustc_ast::{self as ast, AstLike};
use rustc_data_structures::map_in_place::MapInPlace;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_expand::config::StripUnconfigured;
use rustc_expand::configure;
Expand Down Expand Up @@ -43,57 +42,6 @@ impl CfgEval<'_> {
self.cfg.configure(node)
}

fn configure_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
let ast::ForeignMod { unsafety: _, abi: _, items } = foreign_mod;
items.flat_map_in_place(|item| self.configure(item));
}

fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
match vdata {
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) => {
fields.flat_map_in_place(|field| self.configure(field))
}
ast::VariantData::Unit(_) => {}
}
}

fn configure_item_kind(&mut self, item: &mut ast::ItemKind) {
match item {
ast::ItemKind::Struct(def, _generics) | ast::ItemKind::Union(def, _generics) => {
self.configure_variant_data(def)
}
ast::ItemKind::Enum(ast::EnumDef { variants }, _generics) => {
variants.flat_map_in_place(|variant| self.configure(variant));
for variant in variants {
self.configure_variant_data(&mut variant.data);
}
}
_ => {}
}
}

fn configure_expr_kind(&mut self, expr_kind: &mut ast::ExprKind) {
match expr_kind {
ast::ExprKind::Match(_m, arms) => {
arms.flat_map_in_place(|arm| self.configure(arm));
}
ast::ExprKind::Struct(_path, fields, _base) => {
fields.flat_map_in_place(|field| self.configure(field));
}
_ => {}
}
}

fn configure_pat(&mut self, pat: &mut P<ast::Pat>) {
if let ast::PatKind::Struct(_path, fields, _etc) = &mut pat.kind {
fields.flat_map_in_place(|field| self.configure(field));
}
}

fn configure_fn_decl(&mut self, fn_decl: &mut ast::FnDecl) {
fn_decl.inputs.flat_map_in_place(|arg| self.configure(arg));
}

crate fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
// Since the item itself has already been configured by the InvocationCollector,
// we know that fold result vector will contain exactly one element
Expand Down Expand Up @@ -139,25 +87,13 @@ impl CfgEval<'_> {
}

impl MutVisitor for CfgEval<'_> {
fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
self.configure_foreign_mod(foreign_mod);
mut_visit::noop_visit_foreign_mod(foreign_mod, self);
}

fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
self.configure_item_kind(item);
mut_visit::noop_visit_item_kind(item, self);
}

fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
self.cfg.configure_expr(expr);
self.configure_expr_kind(&mut expr.kind);
mut_visit::noop_visit_expr(expr, self);
}

fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut expr = configure!(self, expr);
self.configure_expr_kind(&mut expr.kind);
mut_visit::noop_visit_expr(&mut expr, self);
Some(expr)
}
Expand Down Expand Up @@ -185,13 +121,34 @@ impl MutVisitor for CfgEval<'_> {
mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
}

fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
self.configure_pat(pat);
mut_visit::noop_visit_pat(pat, self)
fn flat_map_foreign_item(
&mut self,
foreign_item: P<ast::ForeignItem>,
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
}

fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
mut_visit::noop_flat_map_arm(configure!(self, arm), self)
}

fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
mut_visit::noop_flat_map_field(configure!(self, field), self)
}

fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
mut_visit::noop_flat_map_field_pattern(configure!(self, fp), self)
}

fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
mut_visit::noop_flat_map_param(configure!(self, p), self)
}

fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
mut_visit::noop_flat_map_struct_field(configure!(self, sf), self)
}

fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
self.configure_fn_decl(&mut fn_decl);
mut_visit::noop_visit_fn_decl(fn_decl, self);
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
mut_visit::noop_flat_map_variant(configure!(self, variant), self)
}
}

0 comments on commit 10ed08f

Please sign in to comment.