2014-05-14 15:31:30 -04:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-09-01 12:35:05 -04:00
|
|
|
use middle::def_id::DefId;
|
2015-02-17 06:44:23 +02:00
|
|
|
use middle::privacy::LastPrivate;
|
2014-05-31 18:53:13 -04:00
|
|
|
use middle::subst::ParamSpace;
|
2014-12-18 21:03:56 +02:00
|
|
|
use util::nodemap::NodeMap;
|
2014-05-14 15:31:30 -04:00
|
|
|
use syntax::ast;
|
2015-07-31 00:04:06 -07:00
|
|
|
use rustc_front::hir;
|
2014-05-14 15:31:30 -04:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-05-14 15:31:30 -04:00
|
|
|
pub enum Def {
|
2016-01-20 22:31:10 +03:00
|
|
|
Fn(DefId),
|
|
|
|
SelfTy(Option<DefId>, // trait id
|
2015-04-03 17:13:52 +13:00
|
|
|
Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id)
|
2016-01-20 22:31:10 +03:00
|
|
|
Mod(DefId),
|
|
|
|
ForeignMod(DefId),
|
|
|
|
Static(DefId, bool /* is_mutbl */),
|
|
|
|
Const(DefId),
|
|
|
|
AssociatedConst(DefId),
|
|
|
|
Local(DefId, // def id of variable
|
2015-09-09 14:23:43 -04:00
|
|
|
ast::NodeId), // node id of variable
|
2016-01-20 22:31:10 +03:00
|
|
|
Variant(DefId /* enum */, DefId /* variant */),
|
|
|
|
Enum(DefId),
|
|
|
|
TyAlias(DefId),
|
|
|
|
AssociatedTy(DefId /* trait */, DefId),
|
|
|
|
Trait(DefId),
|
|
|
|
PrimTy(hir::PrimTy),
|
|
|
|
TyParam(ParamSpace, u32, DefId, ast::Name),
|
|
|
|
Upvar(DefId, // def id of closed over local
|
2015-09-09 14:23:43 -04:00
|
|
|
ast::NodeId, // node id of closed over local
|
2015-08-18 17:54:56 -04:00
|
|
|
usize, // index in the freevars list of the closure
|
2015-01-24 22:04:41 +02:00
|
|
|
ast::NodeId), // expr node that creates the closure
|
2014-05-14 15:31:30 -04:00
|
|
|
|
2016-01-20 22:31:10 +03:00
|
|
|
// If Def::Struct lives in type namespace it denotes a struct item and its DefId refers
|
2016-01-17 22:57:54 +03:00
|
|
|
// to NodeId of the struct itself.
|
2016-01-20 22:31:10 +03:00
|
|
|
// If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
|
2016-01-17 22:57:54 +03:00
|
|
|
// it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
|
2016-01-20 22:31:10 +03:00
|
|
|
Struct(DefId),
|
|
|
|
Label(ast::NodeId),
|
|
|
|
Method(DefId),
|
|
|
|
Err,
|
2014-05-14 15:31:30 -04:00
|
|
|
}
|
|
|
|
|
2015-02-17 06:44:23 +02:00
|
|
|
/// The result of resolving a path.
|
|
|
|
/// Before type checking completes, `depth` represents the number of
|
|
|
|
/// trailing segments which are yet unresolved. Afterwards, if there
|
|
|
|
/// were no errors, all paths should be fully resolved, with `depth`
|
|
|
|
/// set to `0` and `base_def` representing the final resolution.
|
2015-02-05 13:20:48 +02:00
|
|
|
///
|
2015-02-17 06:44:23 +02:00
|
|
|
/// module::Type::AssocX::AssocY::MethodOrAssocType
|
|
|
|
/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
/// base_def depth = 3
|
2015-02-05 13:20:48 +02:00
|
|
|
///
|
2015-02-17 06:44:23 +02:00
|
|
|
/// <T as Trait>::AssocX::AssocY::MethodOrAssocType
|
|
|
|
/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
/// base_def depth = 2
|
2015-03-30 12:21:20 +13:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2015-02-17 06:44:23 +02:00
|
|
|
pub struct PathResolution {
|
|
|
|
pub base_def: Def,
|
|
|
|
pub last_private: LastPrivate,
|
|
|
|
pub depth: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathResolution {
|
|
|
|
/// Get the definition, if fully resolved, otherwise panic.
|
|
|
|
pub fn full_def(&self) -> Def {
|
|
|
|
if self.depth != 0 {
|
|
|
|
panic!("path not fully resolved: {:?}", self);
|
|
|
|
}
|
|
|
|
self.base_def
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the DefId, if fully resolved, otherwise panic.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn def_id(&self) -> DefId {
|
2015-02-17 06:44:23 +02:00
|
|
|
self.full_def().def_id()
|
|
|
|
}
|
2015-03-30 12:21:20 +13:00
|
|
|
|
|
|
|
pub fn new(base_def: Def,
|
|
|
|
last_private: LastPrivate,
|
|
|
|
depth: usize)
|
|
|
|
-> PathResolution {
|
|
|
|
PathResolution {
|
|
|
|
base_def: base_def,
|
|
|
|
last_private: last_private,
|
|
|
|
depth: depth,
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 13:20:48 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 21:03:56 +02:00
|
|
|
// Definition mapping
|
2015-11-04 00:02:22 -06:00
|
|
|
pub type DefMap = NodeMap<PathResolution>;
|
2014-12-19 00:03:00 +02:00
|
|
|
// This is the replacement export map. It maps a module to all of the exports
|
|
|
|
// within.
|
|
|
|
pub type ExportMap = NodeMap<Vec<Export>>;
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2014-12-19 00:03:00 +02:00
|
|
|
pub struct Export {
|
|
|
|
pub name: ast::Name, // The name of the target.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId, // The definition of the target.
|
2014-12-19 00:03:00 +02:00
|
|
|
}
|
2014-12-18 21:03:56 +02:00
|
|
|
|
2014-05-14 15:31:30 -04:00
|
|
|
impl Def {
|
2015-09-07 14:10:25 -04:00
|
|
|
pub fn var_id(&self) -> ast::NodeId {
|
2015-09-02 16:11:32 -04:00
|
|
|
match *self {
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Local(_, id) |
|
|
|
|
Def::Upvar(_, id, _, _) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Fn(..) | Def::Mod(..) | Def::ForeignMod(..) | Def::Static(..) |
|
|
|
|
Def::Variant(..) | Def::Enum(..) | Def::TyAlias(..) | Def::AssociatedTy(..) |
|
|
|
|
Def::TyParam(..) | Def::Struct(..) | Def::Trait(..) |
|
|
|
|
Def::Method(..) | Def::Const(..) | Def::AssociatedConst(..) |
|
|
|
|
Def::PrimTy(..) | Def::Label(..) | Def::SelfTy(..) | Def::Err => {
|
2016-01-31 20:44:10 -08:00
|
|
|
panic!("attempted .var_id() on invalid {:?}", self)
|
2015-09-02 16:11:32 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-02 04:12:25 -05:00
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn def_id(&self) -> DefId {
|
2014-05-14 15:31:30 -04:00
|
|
|
match *self {
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
|
|
|
|
Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
|
|
|
|
Def::TyParam(_, _, id, _) | Def::Struct(id) | Def::Trait(id) |
|
|
|
|
Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
|
|
|
|
Def::Local(id, _) | Def::Upvar(id, _, _, _) => {
|
2014-05-14 15:31:30 -04:00
|
|
|
id
|
|
|
|
}
|
2015-09-02 16:11:32 -04:00
|
|
|
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Label(..) |
|
|
|
|
Def::PrimTy(..) |
|
|
|
|
Def::SelfTy(..) |
|
|
|
|
Def::Err => {
|
2015-09-07 14:27:13 -04:00
|
|
|
panic!("attempted .def_id() on invalid def: {:?}", self)
|
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
|
2014-05-14 15:31:30 -04:00
|
|
|
match *self {
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Variant(enum_id, var_id) => {
|
2014-05-14 15:31:30 -04:00
|
|
|
Some((enum_id, var_id))
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|