]> git.proxmox.com Git - rustc.git/blame - src/librustc/hir/def.rs
New upstream version 1.27.2+dfsg1
[rustc.git] / src / librustc / hir / def.rs
CommitLineData
1a4d82fc
JJ
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
54a0048b 11use hir::def_id::DefId;
ea8adc8c 12use util::nodemap::{NodeMap, DefIdMap};
1a4d82fc 13use syntax::ast;
8bb4bdeb 14use syntax::ext::base::MacroKind;
cc61c64b 15use syntax_pos::Span;
54a0048b 16use hir;
ff7c6d11 17use ty;
1a4d82fc 18
c30ab7b3
SL
19#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
20pub enum CtorKind {
041b39d2 21 /// Constructor function automatically created by a tuple struct/variant.
c30ab7b3 22 Fn,
041b39d2 23 /// Constructor constant automatically created by a unit struct/variant.
c30ab7b3 24 Const,
041b39d2 25 /// Unusable name in value namespace created by a struct variant.
c30ab7b3
SL
26 Fictive,
27}
28
85aaf69f 29#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1a4d82fc 30pub enum Def {
c30ab7b3 31 // Type namespace
7453a54e 32 Mod(DefId),
c30ab7b3
SL
33 Struct(DefId), // DefId refers to NodeId of the struct itself
34 Union(DefId),
7453a54e 35 Enum(DefId),
c30ab7b3
SL
36 Variant(DefId),
37 Trait(DefId),
7453a54e 38 TyAlias(DefId),
abe05a73 39 TyForeign(DefId),
ff7c6d11 40 TraitAlias(DefId),
9e0c209e 41 AssociatedTy(DefId),
7453a54e 42 PrimTy(hir::PrimTy),
9e0c209e 43 TyParam(DefId),
c30ab7b3 44 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
1a4d82fc 45
c30ab7b3
SL
46 // Value namespace
47 Fn(DefId),
48 Const(DefId),
49 Static(DefId, bool /* is_mutbl */),
50 StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
51 VariantCtor(DefId, CtorKind),
7453a54e 52 Method(DefId),
c30ab7b3 53 AssociatedConst(DefId),
ea8adc8c
XL
54
55 Local(ast::NodeId),
56 Upvar(ast::NodeId, // node id of closed over local
c30ab7b3
SL
57 usize, // index in the freevars list of the closure
58 ast::NodeId), // expr node that creates the closure
59 Label(ast::NodeId),
60
476ff2be 61 // Macro namespace
8bb4bdeb 62 Macro(DefId, MacroKind),
476ff2be 63
cc61c64b
XL
64 GlobalAsm(DefId),
65
c30ab7b3 66 // Both namespaces
7453a54e 67 Err,
c34b1796
AL
68}
69
8bb4bdeb
XL
70/// The result of resolving a path before lowering to HIR.
71/// `base_def` is definition of resolved part of the
72/// path, `unresolved_segments` is the number of unresolved
73/// segments.
c34b1796 74///
ff7c6d11
XL
75/// ```text
76/// module::Type::AssocX::AssocY::MethodOrAssocType
77/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78/// base_def unresolved_segments = 3
79///
80/// <T as Trait>::AssocX::AssocY::MethodOrAssocType
81/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
82/// base_def unresolved_segments = 2
83/// ```
c34b1796
AL
84#[derive(Copy, Clone, Debug)]
85pub struct PathResolution {
8bb4bdeb
XL
86 base_def: Def,
87 unresolved_segments: usize,
c34b1796
AL
88}
89
90impl PathResolution {
8bb4bdeb
XL
91 pub fn new(def: Def) -> Self {
92 PathResolution { base_def: def, unresolved_segments: 0 }
93 }
94
95 pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self {
96 if def == Def::Err { unresolved_segments = 0 }
97 PathResolution { base_def: def, unresolved_segments: unresolved_segments }
98 }
99
100 #[inline]
101 pub fn base_def(&self) -> Def {
102 self.base_def
103 }
104
105 #[inline]
106 pub fn unresolved_segments(&self) -> usize {
107 self.unresolved_segments
3157f602
XL
108 }
109
3157f602 110 pub fn kind_name(&self) -> &'static str {
8bb4bdeb 111 if self.unresolved_segments != 0 {
3157f602
XL
112 "associated item"
113 } else {
114 self.base_def.kind_name()
c34b1796
AL
115 }
116 }
1a4d82fc
JJ
117}
118
041b39d2 119/// Definition mapping
92a42be0 120pub type DefMap = NodeMap<PathResolution>;
041b39d2
XL
121
122/// This is the replacement export map. It maps a module to all of the exports
123/// within.
ea8adc8c 124pub type ExportMap = DefIdMap<Vec<Export>>;
1a4d82fc 125
476ff2be 126#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
1a4d82fc 127pub struct Export {
041b39d2
XL
128 /// The name of the target.
129 pub ident: ast::Ident,
130 /// The definition of the target.
131 pub def: Def,
132 /// The span of the target definition.
133 pub span: Span,
ff7c6d11
XL
134 /// The visibility of the export.
135 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
136 pub vis: ty::Visibility,
137 /// True if from a `use` or and `extern crate`.
138 /// Used in rustdoc.
139 pub is_import: bool,
c30ab7b3
SL
140}
141
142impl CtorKind {
143 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
144 match *vdata {
145 ast::VariantData::Tuple(..) => CtorKind::Fn,
146 ast::VariantData::Unit(..) => CtorKind::Const,
147 ast::VariantData::Struct(..) => CtorKind::Fictive,
148 }
149 }
150 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
151 match *vdata {
152 hir::VariantData::Tuple(..) => CtorKind::Fn,
153 hir::VariantData::Unit(..) => CtorKind::Const,
154 hir::VariantData::Struct(..) => CtorKind::Fictive,
155 }
156 }
1a4d82fc
JJ
157}
158
1a4d82fc 159impl Def {
e9174d1e 160 pub fn def_id(&self) -> DefId {
1a4d82fc 161 match *self {
9e0c209e 162 Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
ff7c6d11
XL
163 Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
164 Def::TyAlias(id) | Def::TraitAlias(id) |
c30ab7b3
SL
165 Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
166 Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
ea8adc8c 167 Def::AssociatedConst(id) | Def::Macro(id, ..) |
abe05a73 168 Def::GlobalAsm(id) | Def::TyForeign(id) => {
1a4d82fc
JJ
169 id
170 }
1a4d82fc 171
ea8adc8c
XL
172 Def::Local(..) |
173 Def::Upvar(..) |
7453a54e
SL
174 Def::Label(..) |
175 Def::PrimTy(..) |
176 Def::SelfTy(..) |
177 Def::Err => {
54a0048b 178 bug!("attempted .def_id() on invalid def: {:?}", self)
b039eaaf 179 }
1a4d82fc
JJ
180 }
181 }
182
041b39d2 183 /// A human readable kind name
54a0048b
SL
184 pub fn kind_name(&self) -> &'static str {
185 match *self {
186 Def::Fn(..) => "function",
187 Def::Mod(..) => "module",
54a0048b
SL
188 Def::Static(..) => "static",
189 Def::Variant(..) => "variant",
c30ab7b3
SL
190 Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
191 Def::VariantCtor(.., CtorKind::Const) => "unit variant",
192 Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
54a0048b 193 Def::Enum(..) => "enum",
c30ab7b3 194 Def::TyAlias(..) => "type alias",
ff7c6d11 195 Def::TraitAlias(..) => "trait alias",
54a0048b
SL
196 Def::AssociatedTy(..) => "associated type",
197 Def::Struct(..) => "struct",
c30ab7b3
SL
198 Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
199 Def::StructCtor(.., CtorKind::Const) => "unit struct",
200 Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
9e0c209e 201 Def::Union(..) => "union",
54a0048b 202 Def::Trait(..) => "trait",
abe05a73 203 Def::TyForeign(..) => "foreign type",
54a0048b 204 Def::Method(..) => "method",
3157f602
XL
205 Def::Const(..) => "constant",
206 Def::AssociatedConst(..) => "associated constant",
54a0048b
SL
207 Def::TyParam(..) => "type parameter",
208 Def::PrimTy(..) => "builtin type",
209 Def::Local(..) => "local variable",
210 Def::Upvar(..) => "closure capture",
211 Def::Label(..) => "label",
212 Def::SelfTy(..) => "self type",
476ff2be 213 Def::Macro(..) => "macro",
cc61c64b 214 Def::GlobalAsm(..) => "global asm",
54a0048b
SL
215 Def::Err => "unresolved item",
216 }
217 }
1a4d82fc 218}