]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/def.rs
auto merge of #16377 : pcwalton/rust/associated-items, r=nikomatsakis
[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 middle::subst::ParamSpace;
12 use syntax::ast;
13 use syntax::ast_util::local_def;
14
15 use std::gc::Gc;
16
17 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
18 pub enum Def {
19     DefFn(ast::DefId, ast::FnStyle),
20     DefStaticMethod(/* method */ ast::DefId, MethodProvenance, ast::FnStyle),
21     DefSelfTy(/* trait id */ ast::NodeId),
22     DefMod(ast::DefId),
23     DefForeignMod(ast::DefId),
24     DefStatic(ast::DefId, bool /* is_mutbl */),
25     DefArg(ast::NodeId, ast::BindingMode),
26     DefLocal(ast::NodeId, ast::BindingMode),
27     DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
28     DefTy(ast::DefId, bool /* is_enum */),
29     DefAssociatedTy(ast::DefId),
30     DefTrait(ast::DefId),
31     DefPrimTy(ast::PrimTy),
32     DefTyParam(ParamSpace, ast::DefId, uint),
33     DefBinding(ast::NodeId, ast::BindingMode),
34     DefUse(ast::DefId),
35     DefUpvar(ast::NodeId,  // id of closed over var
36              Gc<Def>,     // closed over def
37              ast::NodeId,  // expr node that creates the closure
38              ast::NodeId), // id for the block/body of the closure expr
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 */),
53 }
54
55 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
56 pub enum MethodProvenance {
57     FromTrait(ast::DefId),
58     FromImpl(ast::DefId),
59 }
60
61 impl Def {
62     pub fn def_id(&self) -> ast::DefId {
63         match *self {
64             DefFn(id, _) | DefStaticMethod(id, _, _) | DefMod(id) |
65             DefForeignMod(id) | DefStatic(id, _) |
66             DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
67             DefTyParam(_, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
68             DefMethod(id, _) => {
69                 id
70             }
71             DefArg(id, _) |
72             DefLocal(id, _) |
73             DefSelfTy(id) |
74             DefUpvar(id, _, _, _) |
75             DefBinding(id, _) |
76             DefRegion(id) |
77             DefTyParamBinder(id) |
78             DefLabel(id) => {
79                 local_def(id)
80             }
81
82             DefPrimTy(_) => fail!()
83         }
84     }
85
86     pub fn variant_def_ids(&self) -> Option<(ast::DefId, ast::DefId)> {
87         match *self {
88             DefVariant(enum_id, var_id, _) => {
89                 Some((enum_id, var_id))
90             }
91             _ => None
92         }
93     }
94 }
95