]> git.proxmox.com Git - rustc.git/blob - src/librustc/hir/def.rs
b6fce2d6ca0be1d6474c92e0a36ab43b4e04df75
[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 CtorKind {
18 // Constructor function automatically created by a tuple struct/variant.
19 Fn,
20 // Constructor constant automatically created by a unit struct/variant.
21 Const,
22 // Unusable name in value namespace created by a struct variant.
23 Fictive,
24 }
25
26 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
27 pub enum Def {
28 // Type namespace
29 Mod(DefId),
30 Struct(DefId), // DefId refers to NodeId of the struct itself
31 Union(DefId),
32 Enum(DefId),
33 Variant(DefId),
34 Trait(DefId),
35 TyAlias(DefId),
36 AssociatedTy(DefId),
37 PrimTy(hir::PrimTy),
38 TyParam(DefId),
39 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
40
41 // Value namespace
42 Fn(DefId),
43 Const(DefId),
44 Static(DefId, bool /* is_mutbl */),
45 StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
46 VariantCtor(DefId, CtorKind),
47 Method(DefId),
48 AssociatedConst(DefId),
49 Local(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
53 Label(ast::NodeId),
54
55 // Macro namespace
56 Macro(DefId),
57
58 // Both namespaces
59 Err,
60 }
61
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.
67 ///
68 /// module::Type::AssocX::AssocY::MethodOrAssocType
69 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 /// base_def depth = 3
71 ///
72 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
73 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
74 /// base_def depth = 2
75 #[derive(Copy, Clone, Debug)]
76 pub struct PathResolution {
77 pub base_def: Def,
78 pub depth: usize
79 }
80
81 impl PathResolution {
82 pub fn new(def: Def) -> PathResolution {
83 PathResolution { base_def: def, depth: 0 }
84 }
85
86 pub fn kind_name(&self) -> &'static str {
87 if self.depth != 0 {
88 "associated item"
89 } else {
90 self.base_def.kind_name()
91 }
92 }
93 }
94
95 // Definition mapping
96 pub type DefMap = NodeMap<PathResolution>;
97 // This is the replacement export map. It maps a module to all of the exports
98 // within.
99 pub type ExportMap = NodeMap<Vec<Export>>;
100
101 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
102 pub struct Export {
103 pub name: ast::Name, // The name of the target.
104 pub def: Def, // The definition of the target.
105 }
106
107 impl CtorKind {
108 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
109 match *vdata {
110 ast::VariantData::Tuple(..) => CtorKind::Fn,
111 ast::VariantData::Unit(..) => CtorKind::Const,
112 ast::VariantData::Struct(..) => CtorKind::Fictive,
113 }
114 }
115 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
116 match *vdata {
117 hir::VariantData::Tuple(..) => CtorKind::Fn,
118 hir::VariantData::Unit(..) => CtorKind::Const,
119 hir::VariantData::Struct(..) => CtorKind::Fictive,
120 }
121 }
122 }
123
124 impl Def {
125 pub fn def_id(&self) -> DefId {
126 match *self {
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) => {
132 id
133 }
134
135 Def::Label(..) |
136 Def::PrimTy(..) |
137 Def::SelfTy(..) |
138 Def::Err => {
139 bug!("attempted .def_id() on invalid def: {:?}", self)
140 }
141 }
142 }
143
144 pub fn kind_name(&self) -> &'static str {
145 match *self {
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",
173 }
174 }
175 }