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, LOCAL_CRATE};
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(ast::NodeId),
33 DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
34 DefTy(DefId, bool /* is_enum */),
35 DefAssociatedTy(DefId /* trait */, DefId),
37 DefPrimTy(hir::PrimTy),
38 DefTyParam(ParamSpace, u32, DefId, ast::Name),
40 DefUpvar(ast::NodeId, // id of closed over local
41 ast::NodeId), // expr node that creates the closure
43 /// Note that if it's a tuple struct's definition, the node id of the DefId
44 /// may either refer to the item definition's id or the StructDef.ctor_id.
46 /// The cases that I have encountered so far are (this is not exhaustive):
47 /// - If it's a ty_path referring to some tuple struct, then DefMap maps
48 /// it to a def whose id is the item definition's id.
49 /// - If it's an ExprPath referring to some tuple struct, then DefMap maps
50 /// it to a def whose id is the StructDef.ctor_id.
52 DefRegion(ast::NodeId),
53 DefLabel(ast::NodeId),
57 /// The result of resolving a path.
58 /// Before type checking completes, `depth` represents the number of
59 /// trailing segments which are yet unresolved. Afterwards, if there
60 /// were no errors, all paths should be fully resolved, with `depth`
61 /// set to `0` and `base_def` representing the final resolution.
63 /// module::Type::AssocX::AssocY::MethodOrAssocType
64 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65 /// base_def depth = 3
67 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
68 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
69 /// base_def depth = 2
70 #[derive(Copy, Clone, Debug)]
71 pub struct PathResolution {
73 pub last_private: LastPrivate,
78 /// Get the definition, if fully resolved, otherwise panic.
79 pub fn full_def(&self) -> Def {
81 panic!("path not fully resolved: {:?}", self);
86 /// Get the DefId, if fully resolved, otherwise panic.
87 pub fn def_id(&self) -> DefId {
88 self.full_def().def_id()
91 pub fn new(base_def: Def,
92 last_private: LastPrivate,
97 last_private: last_private,
103 // Definition mapping
104 pub type DefMap = RefCell<NodeMap<PathResolution>>;
105 // This is the replacement export map. It maps a module to all of the exports
107 pub type ExportMap = NodeMap<Vec<Export>>;
109 #[derive(Copy, Clone)]
111 pub name: ast::Name, // The name of the target.
112 pub def_id: DefId, // The definition of the target.
116 pub fn local_node_id(&self) -> ast::NodeId {
117 let def_id = self.def_id();
118 assert_eq!(def_id.krate, LOCAL_CRATE);
122 pub fn def_id(&self) -> DefId {
124 DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
125 DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
126 DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
127 DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
128 DefSelfTy(Some(id), None)=> {
135 DefSelfTy(_, Some((_, id))) => {
139 DefPrimTy(_) => panic!("attempted .def_id() on DefPrimTy"),
140 DefSelfTy(..) => panic!("attempted .def_id() on invalid DefSelfTy"),
144 pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
146 DefVariant(enum_id, var_id, _) => {
147 Some((enum_id, var_id))