]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/def.rs
auto merge of #19648 : mquandalle/rust/patch-1, r=alexcrichton
[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 pub use self::MethodProvenance::*;
13
14 use middle::subst::ParamSpace;
15 use syntax::ast;
16 use syntax::ast_util::local_def;
17
18 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
19 pub enum Def {
20     DefFn(ast::DefId, bool /* is_ctor */),
21     DefStaticMethod(/* method */ ast::DefId, MethodProvenance),
22     DefSelfTy(/* trait id */ ast::NodeId),
23     DefMod(ast::DefId),
24     DefForeignMod(ast::DefId),
25     DefStatic(ast::DefId, bool /* is_mutbl */),
26     DefConst(ast::DefId),
27     DefLocal(ast::NodeId),
28     DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
29     DefTy(ast::DefId, bool /* is_enum */),
30     DefAssociatedTy(ast::DefId),
31     DefTrait(ast::DefId),
32     DefPrimTy(ast::PrimTy),
33     DefTyParam(ParamSpace, ast::DefId, uint),
34     DefUse(ast::DefId),
35     DefUpvar(ast::NodeId,  // id of closed over local
36              ast::NodeId,  // expr node that creates the closure
37              ast::NodeId), // block node for the closest enclosing proc
38                            // or unboxed closure, DUMMY_NODE_ID otherwise
39
40     /// Note that if it's a tuple struct's definition, the node id of the ast::DefId
41     /// may either refer to the item definition's id or the StructDef.ctor_id.
42     ///
43     /// The cases that I have encountered so far are (this is not exhaustive):
44     /// - If it's a ty_path referring to some tuple struct, then DefMap maps
45     ///   it to a def whose id is the item definition's id.
46     /// - If it's an ExprPath referring to some tuple struct, then DefMap maps
47     ///   it to a def whose id is the StructDef.ctor_id.
48     DefStruct(ast::DefId),
49     DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
50     DefRegion(ast::NodeId),
51     DefLabel(ast::NodeId),
52     DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
53 }
54
55 impl Copy for Def {}
56
57 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
58 pub enum MethodProvenance {
59     FromTrait(ast::DefId),
60     FromImpl(ast::DefId),
61 }
62
63 impl MethodProvenance {
64     pub fn map(self, f: |ast::DefId| -> ast::DefId) -> MethodProvenance {
65         match self {
66             FromTrait(did) => FromTrait(f(did)),
67             FromImpl(did) => FromImpl(f(did))
68         }
69     }
70 }
71
72 impl Copy for MethodProvenance {}
73
74 impl Def {
75     pub fn def_id(&self) -> ast::DefId {
76         match *self {
77             DefFn(id, _) | DefStaticMethod(id, _) | DefMod(id) |
78             DefForeignMod(id) | DefStatic(id, _) |
79             DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
80             DefTyParam(_, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
81             DefMethod(id, _, _) | DefConst(id) => {
82                 id
83             }
84             DefLocal(id) |
85             DefSelfTy(id) |
86             DefUpvar(id, _, _) |
87             DefRegion(id) |
88             DefTyParamBinder(id) |
89             DefLabel(id) => {
90                 local_def(id)
91             }
92
93             DefPrimTy(_) => panic!()
94         }
95     }
96
97     pub fn variant_def_ids(&self) -> Option<(ast::DefId, ast::DefId)> {
98         match *self {
99             DefVariant(enum_id, var_id, _) => {
100                 Some((enum_id, var_id))
101             }
102             _ => None
103         }
104     }
105 }
106