]>
git.proxmox.com Git - rustc.git/blob - src/librustc/hir/def.rs
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 ty
::subst
::ParamSpace
;
13 use util
::nodemap
::NodeMap
;
17 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
20 SelfTy(Option
<DefId
>, // trait id
21 Option
<(ast
::NodeId
, ast
::NodeId
)>), // (impl id, self type id)
24 Static(DefId
, bool
/* is_mutbl */),
26 AssociatedConst(DefId
),
27 Local(DefId
, // def id of variable
28 ast
::NodeId
), // node id of variable
29 Variant(DefId
/* enum */, DefId
/* variant */),
32 AssociatedTy(DefId
/* trait */, DefId
),
35 TyParam(ParamSpace
, u32, DefId
, ast
::Name
),
36 Upvar(DefId
, // def id of closed over local
37 ast
::NodeId
, // node id of closed over local
38 usize, // index in the freevars list of the closure
39 ast
::NodeId
), // expr node that creates the closure
41 // If Def::Struct lives in type namespace it denotes a struct item and its DefId refers
42 // to NodeId of the struct itself.
43 // If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
44 // it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
51 /// The result of resolving a path.
52 /// Before type checking completes, `depth` represents the number of
53 /// trailing segments which are yet unresolved. Afterwards, if there
54 /// were no errors, all paths should be fully resolved, with `depth`
55 /// set to `0` and `base_def` representing the final resolution.
57 /// module::Type::AssocX::AssocY::MethodOrAssocType
58 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59 /// base_def depth = 3
61 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
62 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
63 /// base_def depth = 2
64 #[derive(Copy, Clone, Debug)]
65 pub struct PathResolution
{
71 /// Get the definition, if fully resolved, otherwise panic.
72 pub fn full_def(&self) -> Def
{
74 bug
!("path not fully resolved: {:?}", self);
79 /// Get the DefId, if fully resolved, otherwise panic.
80 pub fn def_id(&self) -> DefId
{
81 self.full_def().def_id()
84 pub fn new(base_def
: Def
,
95 pub type DefMap
= NodeMap
<PathResolution
>;
96 // This is the replacement export map. It maps a module to all of the exports
98 pub type ExportMap
= NodeMap
<Vec
<Export
>>;
100 #[derive(Copy, Clone)]
102 pub name
: ast
::Name
, // The name of the target.
103 pub def_id
: DefId
, // The definition of the target.
107 pub fn var_id(&self) -> ast
::NodeId
{
110 Def
::Upvar(_
, id
, _
, _
) => {
114 Def
::Fn(..) | Def
::Mod(..) | Def
::ForeignMod(..) | Def
::Static(..) |
115 Def
::Variant(..) | Def
::Enum(..) | Def
::TyAlias(..) | Def
::AssociatedTy(..) |
116 Def
::TyParam(..) | Def
::Struct(..) | Def
::Trait(..) |
117 Def
::Method(..) | Def
::Const(..) | Def
::AssociatedConst(..) |
118 Def
::PrimTy(..) | Def
::Label(..) | Def
::SelfTy(..) | Def
::Err
=> {
119 bug
!("attempted .var_id() on invalid {:?}", self)
124 pub fn def_id(&self) -> DefId
{
126 Def
::Fn(id
) | Def
::Mod(id
) | Def
::ForeignMod(id
) | Def
::Static(id
, _
) |
127 Def
::Variant(_
, id
) | Def
::Enum(id
) | Def
::TyAlias(id
) | Def
::AssociatedTy(_
, id
) |
128 Def
::TyParam(_
, _
, id
, _
) | Def
::Struct(id
) | Def
::Trait(id
) |
129 Def
::Method(id
) | Def
::Const(id
) | Def
::AssociatedConst(id
) |
130 Def
::Local(id
, _
) | Def
::Upvar(id
, _
, _
, _
) => {
138 bug
!("attempted .def_id() on invalid def: {:?}", self)
143 pub fn variant_def_ids(&self) -> Option
<(DefId
, DefId
)> {
145 Def
::Variant(enum_id
, var_id
) => {
146 Some((enum_id
, var_id
))
152 pub fn kind_name(&self) -> &'
static str {
154 Def
::Fn(..) => "function",
155 Def
::Mod(..) => "module",
156 Def
::ForeignMod(..) => "foreign module",
157 Def
::Static(..) => "static",
158 Def
::Variant(..) => "variant",
159 Def
::Enum(..) => "enum",
160 Def
::TyAlias(..) => "type",
161 Def
::AssociatedTy(..) => "associated type",
162 Def
::Struct(..) => "struct",
163 Def
::Trait(..) => "trait",
164 Def
::Method(..) => "method",
165 Def
::Const(..) => "const",
166 Def
::AssociatedConst(..) => "associated const",
167 Def
::TyParam(..) => "type parameter",
168 Def
::PrimTy(..) => "builtin type",
169 Def
::Local(..) => "local variable",
170 Def
::Upvar(..) => "closure capture",
171 Def
::Label(..) => "label",
172 Def
::SelfTy(..) => "self type",
173 Def
::Err
=> "unresolved item",