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.
|
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def_id::DefId;
|
2014-12-18 21:03:56 +02:00
|
|
|
use util::nodemap::NodeMap;
|
2014-05-14 15:31:30 -04:00
|
|
|
use syntax::ast;
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir;
|
2014-05-14 15:31:30 -04:00
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum CtorKind {
|
|
|
|
// Constructor function automatically created by a tuple struct/variant.
|
|
|
|
Fn,
|
|
|
|
// Constructor constant automatically created by a unit struct/variant.
|
|
|
|
Const,
|
|
|
|
// Unusable name in value namespace created by a struct variant.
|
|
|
|
Fictive,
|
|
|
|
}
|
|
|
|
|
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),
|
2016-08-31 14:08:22 +03:00
|
|
|
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
|
2016-01-20 22:31:10 +03:00
|
|
|
Mod(DefId),
|
|
|
|
Static(DefId, bool /* is_mutbl */),
|
|
|
|
Const(DefId),
|
|
|
|
AssociatedConst(DefId),
|
2016-08-31 14:08:22 +03:00
|
|
|
Local(DefId),
|
2016-09-08 19:05:50 +03:00
|
|
|
Variant(DefId),
|
2016-09-15 00:51:46 +03:00
|
|
|
VariantCtor(DefId, CtorKind),
|
2016-01-20 22:31:10 +03:00
|
|
|
Enum(DefId),
|
|
|
|
TyAlias(DefId),
|
2016-09-08 19:05:50 +03:00
|
|
|
AssociatedTy(DefId),
|
2016-01-20 22:31:10 +03:00
|
|
|
Trait(DefId),
|
|
|
|
PrimTy(hir::PrimTy),
|
2016-08-15 01:07:09 +03:00
|
|
|
TyParam(DefId),
|
2016-01-20 22:31:10 +03:00
|
|
|
Upvar(DefId, // def id of closed over local
|
2016-09-15 00:51:46 +03:00
|
|
|
usize, // index in the freevars list of the closure
|
|
|
|
ast::NodeId), // expr node that creates the closure
|
|
|
|
Struct(DefId), // DefId refers to NodeId of the struct itself
|
|
|
|
StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
|
2016-08-06 21:56:02 +03:00
|
|
|
Union(DefId),
|
2016-01-20 22:31:10 +03:00
|
|
|
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 depth: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathResolution {
|
2016-06-03 23:15:00 +03:00
|
|
|
pub fn new(def: Def) -> PathResolution {
|
|
|
|
PathResolution { base_def: def, depth: 0 }
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:44:23 +02:00
|
|
|
/// Get the definition, if fully resolved, otherwise panic.
|
|
|
|
pub fn full_def(&self) -> Def {
|
|
|
|
if self.depth != 0 {
|
2016-03-26 19:59:04 +01:00
|
|
|
bug!("path not fully resolved: {:?}", self);
|
2015-02-17 06:44:23 +02:00
|
|
|
}
|
|
|
|
self.base_def
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:15:00 +03:00
|
|
|
pub fn kind_name(&self) -> &'static str {
|
|
|
|
if self.depth != 0 {
|
|
|
|
"associated item"
|
|
|
|
} else {
|
|
|
|
self.base_def.kind_name()
|
|
|
|
}
|
|
|
|
}
|
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>>;
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
|
2014-12-19 00:03:00 +02:00
|
|
|
pub struct Export {
|
2016-09-15 00:51:46 +03:00
|
|
|
pub name: ast::Name, // The name of the target.
|
|
|
|
pub def: Def, // The definition of the target.
|
2014-12-19 00:03:00 +02:00
|
|
|
}
|
2014-12-18 21:03:56 +02:00
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
impl CtorKind {
|
|
|
|
pub fn from_vdata(vdata: &ast::VariantData) -> CtorKind {
|
|
|
|
match *vdata {
|
|
|
|
ast::VariantData::Tuple(..) => CtorKind::Fn,
|
|
|
|
ast::VariantData::Unit(..) => CtorKind::Const,
|
|
|
|
ast::VariantData::Struct(..) => CtorKind::Fictive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 15:31:30 -04:00
|
|
|
impl Def {
|
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-09-08 19:05:50 +03:00
|
|
|
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
|
|
|
|
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
|
|
|
|
Def::Union(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 => {
|
2016-03-26 19:59:04 +01:00
|
|
|
bug!("attempted .def_id() on invalid def: {:?}", self)
|
2015-09-07 14:27:13 -04:00
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 01:55:54 +00:00
|
|
|
pub fn kind_name(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Def::Fn(..) => "function",
|
|
|
|
Def::Mod(..) => "module",
|
|
|
|
Def::Static(..) => "static",
|
|
|
|
Def::Variant(..) => "variant",
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
|
|
|
|
Def::VariantCtor(.., CtorKind::Const) => "unit variant",
|
|
|
|
Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
|
2016-02-25 01:55:54 +00:00
|
|
|
Def::Enum(..) => "enum",
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::TyAlias(..) => "type alias",
|
2016-02-25 01:55:54 +00:00
|
|
|
Def::AssociatedTy(..) => "associated type",
|
|
|
|
Def::Struct(..) => "struct",
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
|
|
|
|
Def::StructCtor(.., CtorKind::Const) => "unit struct",
|
|
|
|
Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
|
2016-08-06 21:56:02 +03:00
|
|
|
Def::Union(..) => "union",
|
2016-02-25 01:55:54 +00:00
|
|
|
Def::Trait(..) => "trait",
|
|
|
|
Def::Method(..) => "method",
|
2016-06-03 23:15:00 +03:00
|
|
|
Def::Const(..) => "constant",
|
|
|
|
Def::AssociatedConst(..) => "associated constant",
|
2016-02-25 01:55:54 +00:00
|
|
|
Def::TyParam(..) => "type parameter",
|
|
|
|
Def::PrimTy(..) => "builtin type",
|
|
|
|
Def::Local(..) => "local variable",
|
|
|
|
Def::Upvar(..) => "closure capture",
|
|
|
|
Def::Label(..) => "label",
|
|
|
|
Def::SelfTy(..) => "self type",
|
|
|
|
Def::Err => "unresolved item",
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
}
|