]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/def.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc / middle / 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 middle::def_id::DefId;
12 use middle::privacy::LastPrivate;
13 use middle::subst::ParamSpace;
14 use util::nodemap::NodeMap;
15 use syntax::ast;
16 use rustc_front::hir;
17
18 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
19 pub enum Def {
20 Fn(DefId),
21 SelfTy(Option<DefId>, // trait id
22 Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id)
23 Mod(DefId),
24 ForeignMod(DefId),
25 Static(DefId, bool /* is_mutbl */),
26 Const(DefId),
27 AssociatedConst(DefId),
28 Local(DefId, // def id of variable
29 ast::NodeId), // node id of variable
30 Variant(DefId /* enum */, DefId /* variant */),
31 Enum(DefId),
32 TyAlias(DefId),
33 AssociatedTy(DefId /* trait */, DefId),
34 Trait(DefId),
35 PrimTy(hir::PrimTy),
36 TyParam(ParamSpace, u32, DefId, ast::Name),
37 Upvar(DefId, // def id of closed over local
38 ast::NodeId, // node id of closed over local
39 usize, // index in the freevars list of the closure
40 ast::NodeId), // expr node that creates the closure
41
42 // If Def::Struct lives in type namespace it denotes a struct item and its DefId refers
43 // to NodeId of the struct itself.
44 // If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
45 // it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
46 Struct(DefId),
47 Label(ast::NodeId),
48 Method(DefId),
49 Err,
50 }
51
52 /// The result of resolving a path.
53 /// Before type checking completes, `depth` represents the number of
54 /// trailing segments which are yet unresolved. Afterwards, if there
55 /// were no errors, all paths should be fully resolved, with `depth`
56 /// set to `0` and `base_def` representing the final resolution.
57 ///
58 /// module::Type::AssocX::AssocY::MethodOrAssocType
59 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 /// base_def depth = 3
61 ///
62 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
63 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
64 /// base_def depth = 2
65 #[derive(Copy, Clone, Debug)]
66 pub struct PathResolution {
67 pub base_def: Def,
68 pub last_private: LastPrivate,
69 pub depth: usize
70 }
71
72 impl PathResolution {
73 /// Get the definition, if fully resolved, otherwise panic.
74 pub fn full_def(&self) -> Def {
75 if self.depth != 0 {
76 panic!("path not fully resolved: {:?}", self);
77 }
78 self.base_def
79 }
80
81 /// Get the DefId, if fully resolved, otherwise panic.
82 pub fn def_id(&self) -> DefId {
83 self.full_def().def_id()
84 }
85
86 pub fn new(base_def: Def,
87 last_private: LastPrivate,
88 depth: usize)
89 -> PathResolution {
90 PathResolution {
91 base_def: base_def,
92 last_private: last_private,
93 depth: depth,
94 }
95 }
96 }
97
98 // Definition mapping
99 pub type DefMap = NodeMap<PathResolution>;
100 // This is the replacement export map. It maps a module to all of the exports
101 // within.
102 pub type ExportMap = NodeMap<Vec<Export>>;
103
104 #[derive(Copy, Clone)]
105 pub struct Export {
106 pub name: ast::Name, // The name of the target.
107 pub def_id: DefId, // The definition of the target.
108 }
109
110 impl Def {
111 pub fn var_id(&self) -> ast::NodeId {
112 match *self {
113 Def::Local(_, id) |
114 Def::Upvar(_, id, _, _) => {
115 id
116 }
117
118 Def::Fn(..) | Def::Mod(..) | Def::ForeignMod(..) | Def::Static(..) |
119 Def::Variant(..) | Def::Enum(..) | Def::TyAlias(..) | Def::AssociatedTy(..) |
120 Def::TyParam(..) | Def::Struct(..) | Def::Trait(..) |
121 Def::Method(..) | Def::Const(..) | Def::AssociatedConst(..) |
122 Def::PrimTy(..) | Def::Label(..) | Def::SelfTy(..) | Def::Err => {
123 panic!("attempted .var_id() on invalid {:?}", self)
124 }
125 }
126 }
127
128 pub fn def_id(&self) -> DefId {
129 match *self {
130 Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
131 Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
132 Def::TyParam(_, _, id, _) | Def::Struct(id) | Def::Trait(id) |
133 Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
134 Def::Local(id, _) | Def::Upvar(id, _, _, _) => {
135 id
136 }
137
138 Def::Label(..) |
139 Def::PrimTy(..) |
140 Def::SelfTy(..) |
141 Def::Err => {
142 panic!("attempted .def_id() on invalid def: {:?}", self)
143 }
144 }
145 }
146
147 pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
148 match *self {
149 Def::Variant(enum_id, var_id) => {
150 Some((enum_id, var_id))
151 }
152 _ => None
153 }
154 }
155 }