1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use hir
::def_id
::DefId
;
12 use util
::nodemap
::NodeMap
;
16 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
19 SelfTy(Option
<DefId
> /* trait */, Option
<DefId
> /* impl */),
21 Static(DefId
, bool
/* is_mutbl */),
23 AssociatedConst(DefId
),
32 Upvar(DefId
, // def id of closed over local
33 usize, // index in the freevars list of the closure
34 ast
::NodeId
), // expr node that creates the closure
36 // If Def::Struct lives in type namespace it denotes a struct item and its DefId refers
37 // to NodeId of the struct itself.
38 // If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
39 // it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
47 /// The result of resolving a path.
48 /// Before type checking completes, `depth` represents the number of
49 /// trailing segments which are yet unresolved. Afterwards, if there
50 /// were no errors, all paths should be fully resolved, with `depth`
51 /// set to `0` and `base_def` representing the final resolution.
53 /// module::Type::AssocX::AssocY::MethodOrAssocType
54 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 /// base_def depth = 3
57 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
58 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
59 /// base_def depth = 2
60 #[derive(Copy, Clone, Debug)]
61 pub struct PathResolution
{
67 pub fn new(def
: Def
) -> PathResolution
{
68 PathResolution { base_def: def, depth: 0 }
71 /// Get the definition, if fully resolved, otherwise panic.
72 pub fn full_def(&self) -> Def
{
74 bug
!("path not fully resolved: {:?}", self);
79 pub fn kind_name(&self) -> &'
static str {
83 self.base_def
.kind_name()
89 pub type DefMap
= NodeMap
<PathResolution
>;
90 // This is the replacement export map. It maps a module to all of the exports
92 pub type ExportMap
= NodeMap
<Vec
<Export
>>;
94 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
96 pub name
: ast
::Name
, // The name of the target.
97 pub def_id
: DefId
, // The definition of the target.
101 pub fn def_id(&self) -> DefId
{
103 Def
::Fn(id
) | Def
::Mod(id
) | Def
::Static(id
, _
) |
104 Def
::Variant(id
) | Def
::Enum(id
) | Def
::TyAlias(id
) | Def
::AssociatedTy(id
) |
105 Def
::TyParam(id
) | Def
::Struct(id
) | Def
::Union(id
) | Def
::Trait(id
) |
106 Def
::Method(id
) | Def
::Const(id
) | Def
::AssociatedConst(id
) |
107 Def
::Local(id
) | Def
::Upvar(id
, ..) => {
115 bug
!("attempted .def_id() on invalid def: {:?}", self)
120 pub fn kind_name(&self) -> &'
static str {
122 Def
::Fn(..) => "function",
123 Def
::Mod(..) => "module",
124 Def
::Static(..) => "static",
125 Def
::Variant(..) => "variant",
126 Def
::Enum(..) => "enum",
127 Def
::TyAlias(..) => "type",
128 Def
::AssociatedTy(..) => "associated type",
129 Def
::Struct(..) => "struct",
130 Def
::Union(..) => "union",
131 Def
::Trait(..) => "trait",
132 Def
::Method(..) => "method",
133 Def
::Const(..) => "constant",
134 Def
::AssociatedConst(..) => "associated constant",
135 Def
::TyParam(..) => "type parameter",
136 Def
::PrimTy(..) => "builtin type",
137 Def
::Local(..) => "local variable",
138 Def
::Upvar(..) => "closure capture",
139 Def
::Label(..) => "label",
140 Def
::SelfTy(..) => "self type",
141 Def
::Err
=> "unresolved item",