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