Skip to content

Commit

Permalink
resolve: Refactor away DefModifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Apr 17, 2016
1 parent ae33aa7 commit 2c978dc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 75 deletions.
1 change: 0 additions & 1 deletion src/librustc_resolve/Cargo.toml
Expand Up @@ -12,5 +12,4 @@ crate-type = ["dylib"]
log = { path = "../liblog" }
syntax = { path = "../libsyntax" }
rustc = { path = "../librustc" }
rustc_bitflags = { path = "../librustc_bitflags" }
arena = { path = "../libarena" }
48 changes: 18 additions & 30 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -13,7 +13,6 @@
//! Here we build the "reduced graph": the graph of the module tree without
//! any imports resolved.

use DefModifiers;
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
use Module;
use Namespace::{self, TypeNS, ValueNS};
Expand Down Expand Up @@ -53,10 +52,9 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
}
}

impl<'a> ToNameBinding<'a> for (Def, Span, DefModifiers, ty::Visibility) {
impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
fn to_name_binding(self) -> NameBinding<'a> {
let kind = NameBindingKind::Def(self.0);
NameBinding { modifiers: self.2, kind: kind, span: Some(self.1), vis: self.3 }
NameBinding { kind: NameBindingKind::Def(self.0), span: Some(self.1), vis: self.2 }
}
}

Expand Down Expand Up @@ -105,7 +103,6 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
let parent = *parent_ref;
let name = item.name;
let sp = item.span;
let modifiers = DefModifiers::IMPORTABLE;
self.current_module = parent;
let vis = self.resolve_visibility(&item.vis);

Expand Down Expand Up @@ -268,21 +265,21 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
ItemStatic(_, m, _) => {
let mutbl = m == hir::MutMutable;
let def = Def::Static(self.ast_map.local_def_id(item.id), mutbl);
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
self.define(parent, name, ValueNS, (def, sp, vis));
}
ItemConst(_, _) => {
let def = Def::Const(self.ast_map.local_def_id(item.id));
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
self.define(parent, name, ValueNS, (def, sp, vis));
}
ItemFn(_, _, _, _, _, _) => {
let def = Def::Fn(self.ast_map.local_def_id(item.id));
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
self.define(parent, name, ValueNS, (def, sp, vis));
}

// These items live in the type namespace.
ItemTy(..) => {
let def = Def::TyAlias(self.ast_map.local_def_id(item.id));
self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
self.define(parent, name, TypeNS, (def, sp, vis));
}

ItemEnum(ref enum_definition, _) => {
Expand All @@ -301,13 +298,13 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
ItemStruct(ref struct_def, _) => {
// Define a name in the type namespace.
let def = Def::Struct(self.ast_map.local_def_id(item.id));
self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
self.define(parent, name, TypeNS, (def, sp, vis));

// If this is a newtype or unit-like struct, define a name
// in the value namespace as well
if !struct_def.is_struct() {
let def = Def::Struct(self.ast_map.local_def_id(struct_def.id()));
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
self.define(parent, name, ValueNS, (def, sp, vis));
}

// Record the def ID and fields of this struct.
Expand Down Expand Up @@ -339,8 +336,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
hir::TypeTraitItem(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
};

let modifiers = DefModifiers::empty(); // NB: not DefModifiers::IMPORTABLE
self.define(module_parent, item.name, ns, (def, item.span, modifiers, vis));
self.define(module_parent, item.name, ns, (def, item.span, vis));

self.trait_item_map.insert((item.name, def_id), item_def_id);
}
Expand All @@ -363,19 +359,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {

// Variants are always treated as importable to allow them to be glob used.
// All variants are defined in both type and value namespaces as future-proofing.
let modifiers = DefModifiers::IMPORTABLE;
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));

self.define(parent, name, ValueNS, (def, variant.span, modifiers, parent.vis));
self.define(parent, name, TypeNS, (def, variant.span, modifiers, parent.vis));
self.define(parent, name, ValueNS, (def, variant.span, parent.vis));
self.define(parent, name, TypeNS, (def, variant.span, parent.vis));
}

/// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item(&mut self,
foreign_item: &ForeignItem,
parent: Module<'b>) {
let name = foreign_item.name;
let modifiers = DefModifiers::IMPORTABLE;

let def = match foreign_item.node {
ForeignItemFn(..) => {
Expand All @@ -387,7 +380,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
};
self.current_module = parent;
let vis = self.resolve_visibility(&foreign_item.vis);
self.define(parent, name, ValueNS, (def, foreign_item.span, modifiers, vis));
self.define(parent, name, ValueNS, (def, foreign_item.span, vis));
}

fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &mut Module<'b>) {
Expand Down Expand Up @@ -422,10 +415,6 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {

let name = xcdef.name;
let vis = if parent.is_trait() { ty::Visibility::Public } else { xcdef.vis };
let modifiers = match parent.is_normal() {
true => DefModifiers::IMPORTABLE,
false => DefModifiers::empty(),
};

match def {
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
Expand All @@ -439,9 +428,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
debug!("(building reduced graph for external crate) building variant {}", name);
// Variants are always treated as importable to allow them to be glob used.
// All variants are defined in both type and value namespaces as future-proofing.
let modifiers = DefModifiers::IMPORTABLE;
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
// Not adding fields for variants as they are not accessed with a self receiver
self.structs.insert(variant_id, Vec::new());
Expand All @@ -454,7 +442,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
Def::Method(..) => {
debug!("(building reduced graph for external crate) building value (fn/static) {}",
name);
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}
Def::Trait(def_id) => {
debug!("(building reduced graph for external crate) building type {}", name);
Expand All @@ -480,16 +468,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
}
Def::TyAlias(..) | Def::AssociatedTy(..) => {
debug!("(building reduced graph for external crate) building type {}", name);
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
}
Def::Struct(def_id)
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
debug!("(building reduced graph for external crate) building type and value for {}",
name);
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
let def = Def::Struct(ctor_def_id);
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}

// Record the def ID and fields of this struct.
Expand Down
46 changes: 22 additions & 24 deletions src/librustc_resolve/lib.rs
Expand Up @@ -29,9 +29,6 @@ extern crate log;
extern crate syntax;
extern crate arena;
#[macro_use]
#[no_link]
extern crate rustc_bitflags;
#[macro_use]
extern crate rustc;

use self::PatternBindingMode::*;
Expand Down Expand Up @@ -915,18 +912,9 @@ impl<'a> fmt::Debug for ModuleS<'a> {
}
}

bitflags! {
#[derive(Debug)]
flags DefModifiers: u8 {
const IMPORTABLE = 1 << 1,
const GLOB_IMPORTED = 1 << 3,
}
}

// Records a possibly-private value, type, or module definition.
#[derive(Clone, Debug)]
pub struct NameBinding<'a> {
modifiers: DefModifiers,
kind: NameBindingKind<'a>,
span: Option<Span>,
vis: ty::Visibility,
Expand All @@ -938,7 +926,7 @@ enum NameBindingKind<'a> {
Module(Module<'a>),
Import {
binding: &'a NameBinding<'a>,
id: NodeId,
directive: &'a ImportDirective<'a>,
// Some(error) if using this imported name causes the import to be a privacy error
privacy_error: Option<Box<PrivacyError<'a>>>,
},
Expand All @@ -950,7 +938,6 @@ struct PrivacyError<'a>(Span, Name, &'a NameBinding<'a>);
impl<'a> NameBinding<'a> {
fn create_from_module(module: Module<'a>, span: Option<Span>) -> Self {
NameBinding {
modifiers: DefModifiers::IMPORTABLE,
kind: NameBindingKind::Module(module),
span: span,
vis: module.vis,
Expand All @@ -973,10 +960,6 @@ impl<'a> NameBinding<'a> {
}
}

fn defined_with(&self, modifiers: DefModifiers) -> bool {
self.modifiers.contains(modifiers)
}

fn is_pseudo_public(&self) -> bool {
self.pseudo_vis() == ty::Visibility::Public
}
Expand All @@ -1003,6 +986,20 @@ impl<'a> NameBinding<'a> {
_ => false,
}
}

fn is_glob_import(&self) -> bool {
match self.kind {
NameBindingKind::Import { directive, .. } => directive.is_glob(),
_ => false,
}
}

fn is_importable(&self) -> bool {
match self.def().unwrap() {
Def::AssociatedConst(..) | Def::Method(..) | Def::AssociatedTy(..) => false,
_ => true,
}
}
}

/// Interns the names of the primitive types.
Expand Down Expand Up @@ -1228,27 +1225,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.used_crates.insert(krate);
}

let (import_id, privacy_error) = match binding.kind {
NameBindingKind::Import { id, ref privacy_error, .. } => (id, privacy_error),
let (directive, privacy_error) = match binding.kind {
NameBindingKind::Import { directive, ref privacy_error, .. } =>
(directive, privacy_error),
_ => return,
};

self.used_imports.insert((import_id, ns));
self.used_imports.insert((directive.id, ns));
if let Some(error) = privacy_error.as_ref() {
self.privacy_errors.push((**error).clone());
}

if !self.make_glob_map {
return;
}
if self.glob_map.contains_key(&import_id) {
self.glob_map.get_mut(&import_id).unwrap().insert(name);
if self.glob_map.contains_key(&directive.id) {
self.glob_map.get_mut(&directive.id).unwrap().insert(name);
return;
}

let mut new_set = FnvHashSet();
new_set.insert(name);
self.glob_map.insert(import_id, new_set);
self.glob_map.insert(directive.id, new_set);
}

fn get_trait_name(&self, did: DefId) -> Name {
Expand Down

0 comments on commit 2c978dc

Please sign in to comment.