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.
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.
11 //! A pass that annotates every item and method with its stability level,
12 //! propagating default levels lexically from parent to children ast nodes.
14 use util::nodemap::{NodeMap, DefIdMap};
15 use syntax::codemap::Span;
16 use syntax::{attr, visit};
17 use syntax::ast::{Attribute, Block, Crate, DefId, FnDecl, NodeId, Variant};
18 use syntax::ast::{Item, Required, Provided, TraitMethod, TypeMethod, Method};
19 use syntax::ast::{Generics, StructDef, Ident};
20 use syntax::ast_util::is_local;
21 use syntax::attr::Stability;
22 use syntax::visit::{FnKind, FkMethod, Visitor};
24 use metadata::csearch;
26 /// A stability index, giving the stability level for items and methods.
28 // stability for crate-local items; unmarked stability == no entry
29 local: NodeMap<Stability>,
30 // cache for extern-crate items; unmarked stability == entry with None
31 extern_cache: DefIdMap<Option<Stability>>
34 // A private tree-walker for producing an Index.
40 // Determine the stability for a node based on its attributes and inherited
41 // stability. The stability is recorded in the index and returned.
42 fn annotate(&mut self, id: NodeId, attrs: &[Attribute],
43 parent: Option<Stability>) -> Option<Stability> {
44 match attr::find_stability(attrs).or(parent) {
46 self.index.local.insert(id, stab.clone());
54 impl Visitor<Option<Stability>> for Annotator {
55 fn visit_item(&mut self, i: &Item, parent: Option<Stability>) {
56 let stab = self.annotate(i.id, i.attrs.as_slice(), parent);
57 visit::walk_item(self, i, stab)
60 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block,
61 s: Span, _: NodeId, parent: Option<Stability>) {
62 let stab = match *fk {
63 FkMethod(_, _, meth) =>
64 self.annotate(meth.id, meth.attrs.as_slice(), parent),
67 visit::walk_fn(self, fk, fd, b, s, stab)
70 fn visit_trait_method(&mut self, t: &TraitMethod, parent: Option<Stability>) {
72 Required(TypeMethod {attrs: ref attrs, id: id, ..}) =>
73 self.annotate(id, attrs.as_slice(), parent),
75 // work around lack of pattern matching for @ types
76 Provided(method) => match *method {
77 Method {attrs: ref attrs, id: id, ..} =>
78 self.annotate(id, attrs.as_slice(), parent)
81 visit::walk_trait_method(self, t, stab)
84 fn visit_variant(&mut self, v: &Variant, g: &Generics, parent: Option<Stability>) {
85 let stab = self.annotate(v.node.id, v.node.attrs.as_slice(), parent);
86 visit::walk_variant(self, v, g, stab)
89 fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics,
90 _: NodeId, parent: Option<Stability>) {
91 s.ctor_id.map(|id| self.annotate(id, &[], parent.clone()));
92 visit::walk_struct_def(self, s, parent)
97 /// Construct the stability index for a crate being compiled.
98 pub fn build(krate: &Crate) -> Index {
99 let mut annotator = Annotator {
101 local: NodeMap::new(),
102 extern_cache: DefIdMap::new()
105 visit::walk_crate(&mut annotator, krate,
106 attr::find_stability(krate.attrs.as_slice()));
111 /// Lookup the stability for a node, loading external crate
112 /// metadata as necessary.
113 pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
114 // is this definition the implementation of a trait method?
115 match ty::trait_method_of_method(tcx, id) {
116 Some(trait_method_id) if trait_method_id != id => {
117 lookup(tcx, trait_method_id)
119 _ if is_local(id) => {
120 tcx.stability.borrow().local.find_copy(&id.node)
123 let stab = csearch::get_stability(&tcx.sess.cstore, id);
124 let mut index = tcx.stability.borrow_mut();
125 (*index).extern_cache.insert(id, stab.clone());