]> git.proxmox.com Git - rustc.git/blob - src/librustc/hir/def.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / 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.
4 //
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.
10
11 use hir::def_id::DefId;
12 use util::nodemap::NodeMap;
13 use syntax::ast;
14 use hir;
15
16 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
17 pub enum Def {
18 Fn(DefId),
19 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
20 Mod(DefId),
21 Static(DefId, bool /* is_mutbl */),
22 Const(DefId),
23 AssociatedConst(DefId),
24 Local(DefId),
25 Variant(DefId),
26 Enum(DefId),
27 TyAlias(DefId),
28 AssociatedTy(DefId),
29 Trait(DefId),
30 PrimTy(hir::PrimTy),
31 TyParam(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
35
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.
40 Struct(DefId),
41 Union(DefId),
42 Label(ast::NodeId),
43 Method(DefId),
44 Err,
45 }
46
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.
52 ///
53 /// module::Type::AssocX::AssocY::MethodOrAssocType
54 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 /// base_def depth = 3
56 ///
57 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
58 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
59 /// base_def depth = 2
60 #[derive(Copy, Clone, Debug)]
61 pub struct PathResolution {
62 pub base_def: Def,
63 pub depth: usize
64 }
65
66 impl PathResolution {
67 pub fn new(def: Def) -> PathResolution {
68 PathResolution { base_def: def, depth: 0 }
69 }
70
71 /// Get the definition, if fully resolved, otherwise panic.
72 pub fn full_def(&self) -> Def {
73 if self.depth != 0 {
74 bug!("path not fully resolved: {:?}", self);
75 }
76 self.base_def
77 }
78
79 pub fn kind_name(&self) -> &'static str {
80 if self.depth != 0 {
81 "associated item"
82 } else {
83 self.base_def.kind_name()
84 }
85 }
86 }
87
88 // Definition mapping
89 pub type DefMap = NodeMap<PathResolution>;
90 // This is the replacement export map. It maps a module to all of the exports
91 // within.
92 pub type ExportMap = NodeMap<Vec<Export>>;
93
94 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
95 pub struct Export {
96 pub name: ast::Name, // The name of the target.
97 pub def_id: DefId, // The definition of the target.
98 }
99
100 impl Def {
101 pub fn def_id(&self) -> DefId {
102 match *self {
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, ..) => {
108 id
109 }
110
111 Def::Label(..) |
112 Def::PrimTy(..) |
113 Def::SelfTy(..) |
114 Def::Err => {
115 bug!("attempted .def_id() on invalid def: {:?}", self)
116 }
117 }
118 }
119
120 pub fn kind_name(&self) -> &'static str {
121 match *self {
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",
142 }
143 }
144 }