2014-05-14 14:31:30 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::Def::*;
|
|
|
|
pub use self::MethodProvenance::*;
|
2014-12-18 16:17:58 -06:00
|
|
|
pub use self::TraitItemKind::*;
|
2014-11-06 02:05:53 -06:00
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
use middle::subst::ParamSpace;
|
2014-12-18 16:17:58 -06:00
|
|
|
use middle::ty::{ExplicitSelfCategory, StaticExplicitSelfCategory};
|
2014-12-18 13:03:56 -06:00
|
|
|
use util::nodemap::NodeMap;
|
2014-05-14 14:31:30 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_util::local_def;
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-05-14 14:31:30 -05:00
|
|
|
pub enum Def {
|
2014-10-15 23:44:24 -05:00
|
|
|
DefFn(ast::DefId, bool /* is_ctor */),
|
|
|
|
DefStaticMethod(/* method */ ast::DefId, MethodProvenance),
|
2014-05-14 14:31:30 -05:00
|
|
|
DefSelfTy(/* trait id */ ast::NodeId),
|
|
|
|
DefMod(ast::DefId),
|
|
|
|
DefForeignMod(ast::DefId),
|
|
|
|
DefStatic(ast::DefId, bool /* is_mutbl */),
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
DefConst(ast::DefId),
|
2014-09-17 10:17:09 -05:00
|
|
|
DefLocal(ast::NodeId),
|
2014-05-14 14:31:30 -05:00
|
|
|
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
|
2014-09-15 16:13:00 -05:00
|
|
|
DefTy(ast::DefId, bool /* is_enum */),
|
2015-01-30 04:16:59 -06:00
|
|
|
DefAssociatedTy(ast::DefId /* trait */, ast::DefId),
|
2014-12-04 13:58:52 -06:00
|
|
|
// A partially resolved path to an associated type `T::U` where `T` is a concrete
|
|
|
|
// type (indicated by the DefId) which implements a trait which has an associated
|
|
|
|
// type `U` (indicated by the Ident).
|
2014-12-29 10:00:56 -06:00
|
|
|
// FIXME(#20301) -- should use Name
|
2014-12-04 13:58:52 -06:00
|
|
|
DefAssociatedPath(TyParamProvenance, ast::Ident),
|
2015-02-23 23:45:34 -06:00
|
|
|
DefTrait(ast::DefId),
|
2014-05-14 14:31:30 -05:00
|
|
|
DefPrimTy(ast::PrimTy),
|
2014-12-27 18:42:27 -06:00
|
|
|
DefTyParam(ParamSpace, u32, ast::DefId, ast::Name),
|
2014-05-14 14:31:30 -05:00
|
|
|
DefUse(ast::DefId),
|
2014-09-14 16:40:45 -05:00
|
|
|
DefUpvar(ast::NodeId, // id of closed over local
|
2015-01-24 14:04:41 -06:00
|
|
|
ast::NodeId), // expr node that creates the closure
|
2014-05-14 14:31:30 -05:00
|
|
|
|
|
|
|
/// Note that if it's a tuple struct's definition, the node id of the ast::DefId
|
|
|
|
/// may either refer to the item definition's id or the StructDef.ctor_id.
|
|
|
|
///
|
|
|
|
/// The cases that I have encountered so far are (this is not exhaustive):
|
|
|
|
/// - If it's a ty_path referring to some tuple struct, then DefMap maps
|
|
|
|
/// it to a def whose id is the item definition's id.
|
|
|
|
/// - If it's an ExprPath referring to some tuple struct, then DefMap maps
|
|
|
|
/// it to a def whose id is the StructDef.ctor_id.
|
|
|
|
DefStruct(ast::DefId),
|
|
|
|
DefRegion(ast::NodeId),
|
|
|
|
DefLabel(ast::NodeId),
|
2014-10-14 19:33:20 -05:00
|
|
|
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
|
2014-05-14 14:31:30 -05:00
|
|
|
}
|
|
|
|
|
2014-12-18 13:03:56 -06:00
|
|
|
// Definition mapping
|
|
|
|
pub type DefMap = RefCell<NodeMap<Def>>;
|
2014-12-18 16:03:00 -06:00
|
|
|
// This is the replacement export map. It maps a module to all of the exports
|
|
|
|
// within.
|
|
|
|
pub type ExportMap = NodeMap<Vec<Export>>;
|
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Copy)]
|
2014-12-18 16:03:00 -06:00
|
|
|
pub struct Export {
|
|
|
|
pub name: ast::Name, // The name of the target.
|
|
|
|
pub def_id: ast::DefId, // The definition of the target.
|
|
|
|
}
|
2014-12-18 13:03:56 -06:00
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-05-14 14:31:30 -05:00
|
|
|
pub enum MethodProvenance {
|
|
|
|
FromTrait(ast::DefId),
|
|
|
|
FromImpl(ast::DefId),
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-12-04 13:58:52 -06:00
|
|
|
pub enum TyParamProvenance {
|
|
|
|
FromSelf(ast::DefId),
|
|
|
|
FromParam(ast::DefId),
|
|
|
|
}
|
|
|
|
|
2014-11-01 20:49:48 -05:00
|
|
|
impl MethodProvenance {
|
2014-12-08 19:26:43 -06:00
|
|
|
pub fn map<F>(self, f: F) -> MethodProvenance where
|
|
|
|
F: FnOnce(ast::DefId) -> ast::DefId,
|
|
|
|
{
|
2014-11-01 20:49:48 -05:00
|
|
|
match self {
|
|
|
|
FromTrait(did) => FromTrait(f(did)),
|
|
|
|
FromImpl(did) => FromImpl(f(did))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:58:52 -06:00
|
|
|
impl TyParamProvenance {
|
|
|
|
pub fn def_id(&self) -> ast::DefId {
|
|
|
|
match *self {
|
|
|
|
TyParamProvenance::FromSelf(ref did) => did.clone(),
|
|
|
|
TyParamProvenance::FromParam(ref did) => did.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
2014-12-18 16:17:58 -06:00
|
|
|
pub enum TraitItemKind {
|
|
|
|
NonstaticMethodTraitItemKind,
|
|
|
|
StaticMethodTraitItemKind,
|
|
|
|
TypeTraitItemKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitItemKind {
|
|
|
|
pub fn from_explicit_self_category(explicit_self_category:
|
|
|
|
ExplicitSelfCategory)
|
|
|
|
-> TraitItemKind {
|
|
|
|
if explicit_self_category == StaticExplicitSelfCategory {
|
|
|
|
StaticMethodTraitItemKind
|
|
|
|
} else {
|
|
|
|
NonstaticMethodTraitItemKind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
impl Def {
|
2015-01-02 03:12:25 -06:00
|
|
|
pub fn local_node_id(&self) -> ast::NodeId {
|
|
|
|
let def_id = self.def_id();
|
|
|
|
assert_eq!(def_id.krate, ast::LOCAL_CRATE);
|
|
|
|
def_id.node
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
pub fn def_id(&self) -> ast::DefId {
|
|
|
|
match *self {
|
2014-10-15 23:44:24 -05:00
|
|
|
DefFn(id, _) | DefStaticMethod(id, _) | DefMod(id) |
|
2014-05-14 14:31:30 -05:00
|
|
|
DefForeignMod(id) | DefStatic(id, _) |
|
2015-01-30 04:16:59 -06:00
|
|
|
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
|
2015-02-23 23:45:34 -06:00
|
|
|
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
|
2014-12-04 13:58:52 -06:00
|
|
|
DefMethod(id, _, _) | DefConst(id) |
|
|
|
|
DefAssociatedPath(TyParamProvenance::FromSelf(id), _) |
|
|
|
|
DefAssociatedPath(TyParamProvenance::FromParam(id), _) => {
|
2014-05-14 14:31:30 -05:00
|
|
|
id
|
|
|
|
}
|
2014-09-17 10:17:09 -05:00
|
|
|
DefLocal(id) |
|
2014-05-14 14:31:30 -05:00
|
|
|
DefSelfTy(id) |
|
2015-01-24 14:04:41 -06:00
|
|
|
DefUpvar(id, _) |
|
2014-05-14 14:31:30 -05:00
|
|
|
DefRegion(id) |
|
|
|
|
DefLabel(id) => {
|
|
|
|
local_def(id)
|
|
|
|
}
|
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
DefPrimTy(_) => panic!()
|
2014-05-14 14:31:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn variant_def_ids(&self) -> Option<(ast::DefId, ast::DefId)> {
|
|
|
|
match *self {
|
|
|
|
DefVariant(enum_id, var_id, _) => {
|
|
|
|
Some((enum_id, var_id))
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|