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.
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.
13 use middle::def_id::DefId;
14 use middle::privacy::LastPrivate;
15 use middle::subst::ParamSpace;
16 use util::nodemap::NodeMap;
20 use std::cell::RefCell;
22 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
24 DefFn(DefId, bool /* is_ctor */),
25 DefSelfTy(Option<DefId>, // trait id
26 Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id)
29 DefStatic(DefId, bool /* is_mutbl */),
31 DefAssociatedConst(DefId),
32 DefLocal(DefId, // def id of variable
33 ast::NodeId), // node id of variable
34 DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
35 DefTy(DefId, bool /* is_enum */),
36 DefAssociatedTy(DefId /* trait */, DefId),
38 DefPrimTy(hir::PrimTy),
39 DefTyParam(ParamSpace, u32, DefId, ast::Name),
41 DefUpvar(DefId, // def id of closed over local
42 ast::NodeId, // node id of closed over local
43 usize, // index in the freevars list of the closure
44 ast::NodeId), // expr node that creates the closure
46 /// Note that if it's a tuple struct's definition, the node id of the DefId
47 /// may either refer to the item definition's id or the StructDef.ctor_id.
49 /// The cases that I have encountered so far are (this is not exhaustive):
50 /// - If it's a ty_path referring to some tuple struct, then DefMap maps
51 /// it to a def whose id is the item definition's id.
52 /// - If it's an ExprPath referring to some tuple struct, then DefMap maps
53 /// it to a def whose id is the StructDef.ctor_id.
55 DefLabel(ast::NodeId),
59 /// The result of resolving a path.
60 /// Before type checking completes, `depth` represents the number of
61 /// trailing segments which are yet unresolved. Afterwards, if there
62 /// were no errors, all paths should be fully resolved, with `depth`
63 /// set to `0` and `base_def` representing the final resolution.
65 /// module::Type::AssocX::AssocY::MethodOrAssocType
66 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67 /// base_def depth = 3
69 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
70 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
71 /// base_def depth = 2
72 #[derive(Copy, Clone, Debug)]
73 pub struct PathResolution {
75 pub last_private: LastPrivate,
80 /// Get the definition, if fully resolved, otherwise panic.
81 pub fn full_def(&self) -> Def {
83 panic!("path not fully resolved: {:?}", self);
88 /// Get the DefId, if fully resolved, otherwise panic.
89 pub fn def_id(&self) -> DefId {
90 self.full_def().def_id()
93 pub fn new(base_def: Def,
94 last_private: LastPrivate,
99 last_private: last_private,
105 // Definition mapping
106 pub type DefMap = RefCell<NodeMap<PathResolution>>;
107 // This is the replacement export map. It maps a module to all of the exports
109 pub type ExportMap = NodeMap<Vec<Export>>;
111 #[derive(Copy, Clone)]
113 pub name: ast::Name, // The name of the target.
114 pub def_id: DefId, // The definition of the target.
118 pub fn var_id(&self) -> ast::NodeId {
121 DefUpvar(_, id, _, _) => {
125 DefFn(..) | DefMod(..) | DefForeignMod(..) | DefStatic(..) |
126 DefVariant(..) | DefTy(..) | DefAssociatedTy(..) |
127 DefTyParam(..) | DefUse(..) | DefStruct(..) | DefTrait(..) |
128 DefMethod(..) | DefConst(..) | DefAssociatedConst(..) |
129 DefPrimTy(..) | DefLabel(..) | DefSelfTy(..) => {
130 panic!("attempted .def_id() on invalid {:?}", self)
135 pub fn def_id(&self) -> DefId {
137 DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
138 DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
139 DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
140 DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
141 DefLocal(id, _) | DefUpvar(id, _, _, _) => {
148 panic!("attempted .def_id() on invalid def: {:?}", self)
153 pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
155 DefVariant(enum_id, var_id, _) => {
156 Some((enum_id, var_id))