Skip to content

Commit

Permalink
rustc_resolve: use the visitor model more, remove redundant repeated …
Browse files Browse the repository at this point in the history
…lookups.
  • Loading branch information
eddyb committed Feb 24, 2015
1 parent ffb8092 commit 5809f8a
Show file tree
Hide file tree
Showing 26 changed files with 335 additions and 722 deletions.
63 changes: 22 additions & 41 deletions src/librustc/middle/astconv_util.rs
Expand Up @@ -35,55 +35,36 @@ pub fn check_path_args(tcx: &ty::ctxt,
if (flags & NO_REGIONS) != 0 {
if path.segments.iter().any(|s| s.parameters.has_lifetimes()) {
span_err!(tcx.sess, path.span, E0110,
"region parameters are not allowed on this type");
"lifetime parameters are not allowed on this type");
}
}
}

pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
-> Option<Ty<'tcx>> {
match ast_ty.node {
ast::TyPath(ref path) => {
let a_def = match tcx.def_map.borrow().get(&ast_ty.id) {
None => {
tcx.sess.span_bug(ast_ty.span,
&format!("unbound path {}",
path.repr(tcx)))
}
Some(&d) => d
};
match a_def {
def::DefPrimTy(nty) => {
match nty {
ast::TyBool => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(tcx.types.bool)
}
ast::TyChar => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(tcx.types.char)
}
ast::TyInt(it) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(ty::mk_mach_int(tcx, it))
}
ast::TyUint(uit) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(ty::mk_mach_uint(tcx, uit))
}
ast::TyFloat(ft) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(ty::mk_mach_float(tcx, ft))
}
ast::TyStr => {
Some(ty::mk_str(tcx))
}
}
}
_ => None
if let ast::TyPath(ref path) = ast_ty.node {
let def = match tcx.def_map.borrow().get(&ast_ty.id) {
None => {
tcx.sess.span_bug(ast_ty.span,
&format!("unbound path {}", path.repr(tcx)))
}
Some(&d) => d
};
if let def::DefPrimTy(nty) = def {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(match nty {
ast::TyBool => tcx.types.bool,
ast::TyChar => tcx.types.char,
ast::TyInt(it) => ty::mk_mach_int(tcx, it),
ast::TyUint(uit) => ty::mk_mach_uint(tcx, uit),
ast::TyFloat(ft) => ty::mk_mach_float(tcx, ft),
ast::TyStr => ty::mk_str(tcx)
})
} else {
None
}
_ => None
} else {
None
}
}

3 changes: 0 additions & 3 deletions src/librustc/middle/astencode.rs
Expand Up @@ -456,9 +456,6 @@ impl tr for def::Def {
}
def::DefStruct(did) => def::DefStruct(did.tr(dcx)),
def::DefRegion(nid) => def::DefRegion(dcx.tr_id(nid)),
def::DefTyParamBinder(nid) => {
def::DefTyParamBinder(dcx.tr_id(nid))
}
def::DefLabel(nid) => def::DefLabel(dcx.tr_id(nid))
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/middle/def.rs
Expand Up @@ -54,7 +54,6 @@ pub enum Def {
/// - If it's an ExprPath referring to some tuple struct, then DefMap maps
/// it to a def whose id is the StructDef.ctor_id.
DefStruct(ast::DefId),
DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
DefRegion(ast::NodeId),
DefLabel(ast::NodeId),
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
Expand Down Expand Up @@ -145,7 +144,6 @@ impl Def {
DefSelfTy(id) |
DefUpvar(id, _) |
DefRegion(id) |
DefTyParamBinder(id) |
DefLabel(id) => {
local_def(id)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Expand Up @@ -580,7 +580,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
}
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
def::DefTyParam(..) | def::DefTyParamBinder(..) | def::DefRegion(_) |
def::DefTyParam(..) | def::DefRegion(_) |
def::DefLabel(_) | def::DefSelfTy(..) |
def::DefAssociatedTy(..) | def::DefAssociatedPath(..)=> {
Ok(Rc::new(cmt_ {
Expand Down
47 changes: 11 additions & 36 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -23,18 +23,15 @@ use Namespace::{TypeNS, ValueNS};
use NameBindings;
use ParentLink::{self, ModuleParentLink, BlockParentLink};
use Resolver;
use RibKind::*;
use Shadowable;
use TypeNsDef;
use TypeParameters::HasTypeParameters;

use self::DuplicateCheckingMode::*;
use self::NamespaceError::*;

use rustc::metadata::csearch;
use rustc::metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
use rustc::middle::def::*;
use rustc::middle::subst::FnSpace;

use syntax::ast::{Block, Crate};
use syntax::ast::{DeclItem, DefId};
Expand Down Expand Up @@ -773,38 +770,25 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
}

/// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item<F>(&mut self,
foreign_item: &ForeignItem,
parent: &Rc<Module>,
f: F) where
F: FnOnce(&mut Resolver),
{
fn build_reduced_graph_for_foreign_item(&mut self,
foreign_item: &ForeignItem,
parent: &Rc<Module>) {
let name = foreign_item.ident.name;
let is_public = foreign_item.vis == ast::Public;
let modifiers = if is_public { PUBLIC } else { DefModifiers::empty() } | IMPORTABLE;
let name_bindings =
self.add_child(name, parent, ForbidDuplicateValues,
foreign_item.span);

match foreign_item.node {
ForeignItemFn(_, ref generics) => {
let def = DefFn(local_def(foreign_item.id), false);
name_bindings.define_value(def, foreign_item.span, modifiers);

self.with_type_parameter_rib(
HasTypeParameters(generics,
FnSpace,
foreign_item.id,
NormalRibKind),
f);
let def = match foreign_item.node {
ForeignItemFn(..) => {
DefFn(local_def(foreign_item.id), false)
}
ForeignItemStatic(_, m) => {
let def = DefStatic(local_def(foreign_item.id), m);
name_bindings.define_value(def, foreign_item.span, modifiers);

f(self.resolver)
DefStatic(local_def(foreign_item.id), m)
}
}
};
name_bindings.define_value(def, foreign_item.span, modifiers);
}

fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &Rc<Module>) -> Rc<Module> {
Expand Down Expand Up @@ -980,7 +964,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
}
DefLocal(..) | DefPrimTy(..) | DefTyParam(..) |
DefUse(..) | DefUpvar(..) | DefRegion(..) |
DefTyParamBinder(..) | DefLabel(..) | DefSelfTy(..) => {
DefLabel(..) | DefSelfTy(..) => {
panic!("didn't expect `{:?}`", def);
}
}
Expand Down Expand Up @@ -1241,16 +1225,7 @@ impl<'a, 'b, 'v, 'tcx> Visitor<'v> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}

fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
let parent = &self.parent;
self.builder.build_reduced_graph_for_foreign_item(foreign_item,
parent,
|r| {
let mut v = BuildReducedGraphVisitor {
builder: GraphBuilder { resolver: r },
parent: parent.clone()
};
visit::walk_foreign_item(&mut v, foreign_item);
})
self.builder.build_reduced_graph_for_foreign_item(foreign_item, &self.parent);
}

fn visit_block(&mut self, block: &Block) {
Expand Down

0 comments on commit 5809f8a

Please sign in to comment.