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)]
18 // Constructor function automatically created by a tuple struct/variant.
20 // Constructor constant automatically created by a unit struct/variant.
22 // Unusable name in value namespace created by a struct variant.
26 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
30 Struct(DefId
), // DefId refers to NodeId of the struct itself
39 SelfTy(Option
<DefId
> /* trait */, Option
<DefId
> /* impl */),
44 Static(DefId
, bool
/* is_mutbl */),
45 StructCtor(DefId
, CtorKind
), // DefId refers to NodeId of the struct's constructor
46 VariantCtor(DefId
, CtorKind
),
48 AssociatedConst(DefId
),
50 Upvar(DefId
, // def id of closed over local
51 usize, // index in the freevars list of the closure
52 ast
::NodeId
), // expr node that creates the closure
62 /// The result of resolving a path.
63 /// Before type checking completes, `depth` represents the number of
64 /// trailing segments which are yet unresolved. Afterwards, if there
65 /// were no errors, all paths should be fully resolved, with `depth`
66 /// set to `0` and `base_def` representing the final resolution.
68 /// module::Type::AssocX::AssocY::MethodOrAssocType
69 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 /// base_def depth = 3
72 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
73 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
74 /// base_def depth = 2
75 #[derive(Copy, Clone, Debug)]
76 pub struct PathResolution
{
82 pub fn new(def
: Def
) -> PathResolution
{
83 PathResolution { base_def: def, depth: 0 }
86 pub fn kind_name(&self) -> &'
static str {
90 self.base_def
.kind_name()
96 pub type DefMap
= NodeMap
<PathResolution
>;
97 // This is the replacement export map. It maps a module to all of the exports
99 pub type ExportMap
= NodeMap
<Vec
<Export
>>;
101 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
103 pub name
: ast
::Name
, // The name of the target.
104 pub def
: Def
, // The definition of the target.
108 pub fn from_ast(vdata
: &ast
::VariantData
) -> CtorKind
{
110 ast
::VariantData
::Tuple(..) => CtorKind
::Fn
,
111 ast
::VariantData
::Unit(..) => CtorKind
::Const
,
112 ast
::VariantData
::Struct(..) => CtorKind
::Fictive
,
115 pub fn from_hir(vdata
: &hir
::VariantData
) -> CtorKind
{
117 hir
::VariantData
::Tuple(..) => CtorKind
::Fn
,
118 hir
::VariantData
::Unit(..) => CtorKind
::Const
,
119 hir
::VariantData
::Struct(..) => CtorKind
::Fictive
,
125 pub fn def_id(&self) -> DefId
{
127 Def
::Fn(id
) | Def
::Mod(id
) | Def
::Static(id
, _
) |
128 Def
::Variant(id
) | Def
::VariantCtor(id
, ..) | Def
::Enum(id
) | Def
::TyAlias(id
) |
129 Def
::AssociatedTy(id
) | Def
::TyParam(id
) | Def
::Struct(id
) | Def
::StructCtor(id
, ..) |
130 Def
::Union(id
) | Def
::Trait(id
) | Def
::Method(id
) | Def
::Const(id
) |
131 Def
::AssociatedConst(id
) | Def
::Local(id
) | Def
::Upvar(id
, ..) | Def
::Macro(id
) => {
139 bug
!("attempted .def_id() on invalid def: {:?}", self)
144 pub fn kind_name(&self) -> &'
static str {
146 Def
::Fn(..) => "function",
147 Def
::Mod(..) => "module",
148 Def
::Static(..) => "static",
149 Def
::Variant(..) => "variant",
150 Def
::VariantCtor(.., CtorKind
::Fn
) => "tuple variant",
151 Def
::VariantCtor(.., CtorKind
::Const
) => "unit variant",
152 Def
::VariantCtor(.., CtorKind
::Fictive
) => "struct variant",
153 Def
::Enum(..) => "enum",
154 Def
::TyAlias(..) => "type alias",
155 Def
::AssociatedTy(..) => "associated type",
156 Def
::Struct(..) => "struct",
157 Def
::StructCtor(.., CtorKind
::Fn
) => "tuple struct",
158 Def
::StructCtor(.., CtorKind
::Const
) => "unit struct",
159 Def
::StructCtor(.., CtorKind
::Fictive
) => bug
!("impossible struct constructor"),
160 Def
::Union(..) => "union",
161 Def
::Trait(..) => "trait",
162 Def
::Method(..) => "method",
163 Def
::Const(..) => "constant",
164 Def
::AssociatedConst(..) => "associated constant",
165 Def
::TyParam(..) => "type parameter",
166 Def
::PrimTy(..) => "builtin type",
167 Def
::Local(..) => "local variable",
168 Def
::Upvar(..) => "closure capture",
169 Def
::Label(..) => "label",
170 Def
::SelfTy(..) => "self type",
171 Def
::Macro(..) => "macro",
172 Def
::Err
=> "unresolved item",