]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/def.rs
feefc43f4013e682af3628f86cb1353ae81f0397
[rust.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     /// Get the definition, if fully resolved, otherwise panic.
87     pub fn full_def(&self) -> Def {
88         if self.depth != 0 {
89             bug!("path not fully resolved: {:?}", self);
90         }
91         self.base_def
92     }
93
94     pub fn kind_name(&self) -> &'static str {
95         if self.depth != 0 {
96             "associated item"
97         } else {
98             self.base_def.kind_name()
99         }
100     }
101 }
102
103 // Definition mapping
104 pub type DefMap = NodeMap<PathResolution>;
105 // This is the replacement export map. It maps a module to all of the exports
106 // within.
107 pub type ExportMap = NodeMap<Vec<Export>>;
108
109 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
110 pub struct Export {
111     pub name: ast::Name, // The name of the target.
112     pub def: Def, // The definition of the target.
113 }
114
115 impl CtorKind {
116     pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
117         match *vdata {
118             ast::VariantData::Tuple(..) => CtorKind::Fn,
119             ast::VariantData::Unit(..) => CtorKind::Const,
120             ast::VariantData::Struct(..) => CtorKind::Fictive,
121         }
122     }
123     pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
124         match *vdata {
125             hir::VariantData::Tuple(..) => CtorKind::Fn,
126             hir::VariantData::Unit(..) => CtorKind::Const,
127             hir::VariantData::Struct(..) => CtorKind::Fictive,
128         }
129     }
130 }
131
132 impl Def {
133     pub fn def_id(&self) -> DefId {
134         match *self {
135             Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
136             Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
137             Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
138             Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
139             Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id) => {
140                 id
141             }
142
143             Def::Label(..)  |
144             Def::PrimTy(..) |
145             Def::SelfTy(..) |
146             Def::Err => {
147                 bug!("attempted .def_id() on invalid def: {:?}", self)
148             }
149         }
150     }
151
152     pub fn kind_name(&self) -> &'static str {
153         match *self {
154             Def::Fn(..) => "function",
155             Def::Mod(..) => "module",
156             Def::Static(..) => "static",
157             Def::Variant(..) => "variant",
158             Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
159             Def::VariantCtor(.., CtorKind::Const) => "unit variant",
160             Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
161             Def::Enum(..) => "enum",
162             Def::TyAlias(..) => "type alias",
163             Def::AssociatedTy(..) => "associated type",
164             Def::Struct(..) => "struct",
165             Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
166             Def::StructCtor(.., CtorKind::Const) => "unit struct",
167             Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
168             Def::Union(..) => "union",
169             Def::Trait(..) => "trait",
170             Def::Method(..) => "method",
171             Def::Const(..) => "constant",
172             Def::AssociatedConst(..) => "associated constant",
173             Def::TyParam(..) => "type parameter",
174             Def::PrimTy(..) => "builtin type",
175             Def::Local(..) => "local variable",
176             Def::Upvar(..) => "closure capture",
177             Def::Label(..) => "label",
178             Def::SelfTy(..) => "self type",
179             Def::Macro(..) => "macro",
180             Def::Err => "unresolved item",
181         }
182     }
183 }