]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/def.rs
2930dd67f45bab8c08f12789798e590c2783a14d
[rust.git] / src / librustc / middle / 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 pub use self::Def::*;
12
13 use middle::def_id::{DefId, LOCAL_CRATE};
14 use middle::privacy::LastPrivate;
15 use middle::subst::ParamSpace;
16 use util::nodemap::NodeMap;
17 use syntax::ast;
18 use rustc_front::hir;
19
20 use std::cell::RefCell;
21
22 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
23 pub enum Def {
24     DefFn(DefId, bool /* is_ctor */),
25     DefSelfTy(Option<DefId>,                    // trait id
26               Option<(ast::NodeId, ast::NodeId)>),   // (impl id, self type id)
27     DefMod(DefId),
28     DefForeignMod(DefId),
29     DefStatic(DefId, bool /* is_mutbl */),
30     DefConst(DefId),
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),
36     DefTrait(DefId),
37     DefPrimTy(hir::PrimTy),
38     DefTyParam(ParamSpace, u32, DefId, ast::Name),
39     DefUse(DefId),
40     DefUpvar(ast::NodeId,  // id of closed over local
41              ast::NodeId), // expr node that creates the closure
42
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.
45     ///
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.
51     DefStruct(DefId),
52     DefRegion(ast::NodeId),
53     DefLabel(ast::NodeId),
54     DefMethod(DefId),
55 }
56
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.
62 ///
63 ///     module::Type::AssocX::AssocY::MethodOrAssocType
64 ///     ^~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65 ///     base_def      depth = 3
66 ///
67 ///     <T as Trait>::AssocX::AssocY::MethodOrAssocType
68 ///           ^~~~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~
69 ///           base_def        depth = 2
70 #[derive(Copy, Clone, Debug)]
71 pub struct PathResolution {
72     pub base_def: Def,
73     pub last_private: LastPrivate,
74     pub depth: usize
75 }
76
77 impl PathResolution {
78     /// Get the definition, if fully resolved, otherwise panic.
79     pub fn full_def(&self) -> Def {
80         if self.depth != 0 {
81             panic!("path not fully resolved: {:?}", self);
82         }
83         self.base_def
84     }
85
86     /// Get the DefId, if fully resolved, otherwise panic.
87     pub fn def_id(&self) -> DefId {
88         self.full_def().def_id()
89     }
90
91     pub fn new(base_def: Def,
92                last_private: LastPrivate,
93                depth: usize)
94                -> PathResolution {
95         PathResolution {
96             base_def: base_def,
97             last_private: last_private,
98             depth: depth,
99         }
100     }
101 }
102
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
106 // within.
107 pub type ExportMap = NodeMap<Vec<Export>>;
108
109 #[derive(Copy, Clone)]
110 pub struct Export {
111     pub name: ast::Name,    // The name of the target.
112     pub def_id: DefId, // The definition of the target.
113 }
114
115 impl Def {
116     pub fn local_node_id(&self) -> ast::NodeId {
117         let def_id = self.def_id();
118         assert_eq!(def_id.krate, LOCAL_CRATE);
119         def_id.node
120     }
121
122     pub fn def_id(&self) -> DefId {
123         match *self {
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)=> {
129                 id
130             }
131             DefLocal(id) |
132             DefUpvar(id, _) |
133             DefRegion(id) |
134             DefLabel(id)  |
135             DefSelfTy(_, Some((_, id))) => {
136                 DefId::local(id)
137             }
138
139             DefPrimTy(_) => panic!("attempted .def_id() on DefPrimTy"),
140             DefSelfTy(..) => panic!("attempted .def_id() on invalid DefSelfTy"),
141         }
142     }
143
144     pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
145         match *self {
146             DefVariant(enum_id, var_id, _) => {
147                 Some((enum_id, var_id))
148             }
149             _ => None
150         }
151     }
152 }