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