]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/def.rs
Rollup merge of #42397 - sfackler:syncsender-sync, r=alexcrichton
[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 syntax::ext::base::MacroKind;
15 use syntax_pos::Span;
16 use hir;
17
18 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
19 pub enum CtorKind {
20     /// Constructor function automatically created by a tuple struct/variant.
21     Fn,
22     /// Constructor constant automatically created by a unit struct/variant.
23     Const,
24     /// Unusable name in value namespace created by a struct variant.
25     Fictive,
26 }
27
28 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
29 pub enum Def {
30     // Type namespace
31     Mod(DefId),
32     Struct(DefId), // DefId refers to NodeId of the struct itself
33     Union(DefId),
34     Enum(DefId),
35     Variant(DefId),
36     Trait(DefId),
37     TyAlias(DefId),
38     AssociatedTy(DefId),
39     PrimTy(hir::PrimTy),
40     TyParam(DefId),
41     SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
42
43     // Value namespace
44     Fn(DefId),
45     Const(DefId),
46     Static(DefId, bool /* is_mutbl */),
47     StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
48     VariantCtor(DefId, CtorKind),
49     Method(DefId),
50     AssociatedConst(DefId),
51     Local(DefId),
52     Upvar(DefId,        // def id of closed over local
53           usize,        // index in the freevars list of the closure
54           ast::NodeId), // expr node that creates the closure
55     Label(ast::NodeId),
56
57     // Macro namespace
58     Macro(DefId, MacroKind),
59
60     GlobalAsm(DefId),
61
62     // Both namespaces
63     Err,
64 }
65
66 /// The result of resolving a path before lowering to HIR.
67 /// `base_def` is definition of resolved part of the
68 /// path, `unresolved_segments` is the number of unresolved
69 /// segments.
70 ///     module::Type::AssocX::AssocY::MethodOrAssocType
71 ///     ^~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72 ///     base_def      unresolved_segments = 3
73 ///
74 ///     <T as Trait>::AssocX::AssocY::MethodOrAssocType
75 ///           ^~~~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~
76 ///           base_def        unresolved_segments = 2
77 #[derive(Copy, Clone, Debug)]
78 pub struct PathResolution {
79     base_def: Def,
80     unresolved_segments: usize,
81 }
82
83 impl PathResolution {
84     pub fn new(def: Def) -> Self {
85         PathResolution { base_def: def, unresolved_segments: 0 }
86     }
87
88     pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self {
89         if def == Def::Err { unresolved_segments = 0 }
90         PathResolution { base_def: def, unresolved_segments: unresolved_segments }
91     }
92
93     #[inline]
94     pub fn base_def(&self) -> Def {
95         self.base_def
96     }
97
98     #[inline]
99     pub fn unresolved_segments(&self) -> usize {
100         self.unresolved_segments
101     }
102
103     pub fn kind_name(&self) -> &'static str {
104         if self.unresolved_segments != 0 {
105             "associated item"
106         } else {
107             self.base_def.kind_name()
108         }
109     }
110 }
111
112 /// Definition mapping
113 pub type DefMap = NodeMap<PathResolution>;
114
115 /// This is the replacement export map. It maps a module to all of the exports
116 /// within.
117 pub type ExportMap = NodeMap<Vec<Export>>;
118
119 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
120 pub struct Export {
121     /// The name of the target.
122     pub ident: ast::Ident,
123     /// The definition of the target.
124     pub def: Def,
125     /// The span of the target definition.
126     pub span: Span,
127 }
128
129 impl CtorKind {
130     pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
131         match *vdata {
132             ast::VariantData::Tuple(..) => CtorKind::Fn,
133             ast::VariantData::Unit(..) => CtorKind::Const,
134             ast::VariantData::Struct(..) => CtorKind::Fictive,
135         }
136     }
137     pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
138         match *vdata {
139             hir::VariantData::Tuple(..) => CtorKind::Fn,
140             hir::VariantData::Unit(..) => CtorKind::Const,
141             hir::VariantData::Struct(..) => CtorKind::Fictive,
142         }
143     }
144 }
145
146 impl Def {
147     pub fn def_id(&self) -> DefId {
148         match *self {
149             Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
150             Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
151             Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
152             Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
153             Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) |
154             Def::GlobalAsm(id) => {
155                 id
156             }
157
158             Def::Label(..)  |
159             Def::PrimTy(..) |
160             Def::SelfTy(..) |
161             Def::Err => {
162                 bug!("attempted .def_id() on invalid def: {:?}", self)
163             }
164         }
165     }
166
167     /// A human readable kind name
168     pub fn kind_name(&self) -> &'static str {
169         match *self {
170             Def::Fn(..) => "function",
171             Def::Mod(..) => "module",
172             Def::Static(..) => "static",
173             Def::Variant(..) => "variant",
174             Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
175             Def::VariantCtor(.., CtorKind::Const) => "unit variant",
176             Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
177             Def::Enum(..) => "enum",
178             Def::TyAlias(..) => "type alias",
179             Def::AssociatedTy(..) => "associated type",
180             Def::Struct(..) => "struct",
181             Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
182             Def::StructCtor(.., CtorKind::Const) => "unit struct",
183             Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
184             Def::Union(..) => "union",
185             Def::Trait(..) => "trait",
186             Def::Method(..) => "method",
187             Def::Const(..) => "constant",
188             Def::AssociatedConst(..) => "associated constant",
189             Def::TyParam(..) => "type parameter",
190             Def::PrimTy(..) => "builtin type",
191             Def::Local(..) => "local variable",
192             Def::Upvar(..) => "closure capture",
193             Def::Label(..) => "label",
194             Def::SelfTy(..) => "self type",
195             Def::Macro(..) => "macro",
196             Def::GlobalAsm(..) => "global asm",
197             Def::Err => "unresolved item",
198         }
199     }
200 }