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};
18 use syntax::ast::{Attribute, Block, Crate, DefId, FnDecl, NodeId, Variant};
19 use syntax::ast::{Item, Required, Provided, TraitMethod, TypeMethod, Method};
20 use syntax::ast::{Generics, StructDef, StructField, Ident};
21 use syntax::ast_util::is_local;
22 use syntax::attr::Stability;
23 use syntax::visit::{FnKind, FkMethod, Visitor};
25 use metadata::csearch;
27 /// A stability index, giving the stability level for items and methods.
29 // stability for crate-local items; unmarked stability == no entry
30 local: NodeMap<Stability>,
31 // cache for extern-crate items; unmarked stability == entry with None
32 extern_cache: DefIdMap<Option<Stability>>
35 // A private tree-walker for producing an Index.
41 // Determine the stability for a node based on its attributes and inherited
42 // stability. The stability is recorded in the index and returned.
43 fn annotate(&mut self, id: NodeId, attrs: &[Attribute],
44 parent: Option<Stability>) -> Option<Stability> {
45 match attr::find_stability(attrs).or(parent) {
47 self.index.local.insert(id, stab.clone());
55 impl Visitor<Option<Stability>> for Annotator {
56 fn visit_item(&mut self, i: &Item, parent: Option<Stability>) {
57 let stab = self.annotate(i.id, i.attrs.as_slice(), parent);
58 visit::walk_item(self, i, stab)
61 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block,
62 s: Span, _: NodeId, parent: Option<Stability>) {
63 let stab = match *fk {
64 FkMethod(_, _, meth) =>
65 self.annotate(meth.id, meth.attrs.as_slice(), parent),
68 visit::walk_fn(self, fk, fd, b, s, stab)
71 fn visit_trait_method(&mut self, t: &TraitMethod, parent: Option<Stability>) {
73 Required(TypeMethod {attrs: ref attrs, id: id, ..}) =>
74 self.annotate(id, attrs.as_slice(), parent),
76 // work around lack of pattern matching for @ types
77 Provided(method) => match *method {
78 Method {attrs: ref attrs, id: id, ..} =>
79 self.annotate(id, attrs.as_slice(), parent)
82 visit::walk_trait_method(self, t, stab)
85 fn visit_variant(&mut self, v: &Variant, g: &Generics, parent: Option<Stability>) {
86 let stab = self.annotate(v.node.id, v.node.attrs.as_slice(), parent);
87 visit::walk_variant(self, v, g, stab)
90 fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics,
91 _: NodeId, parent: Option<Stability>) {
92 s.ctor_id.map(|id| self.annotate(id, &[], parent.clone()));
93 visit::walk_struct_def(self, s, parent)
96 fn visit_struct_field(&mut self, s: &StructField, parent: Option<Stability>) {
97 let stab = self.annotate(s.node.id, s.node.attrs.as_slice(), parent);
98 visit::walk_struct_field(self, s, stab)
103 /// Construct the stability index for a crate being compiled.
104 pub fn build(krate: &Crate) -> Index {
105 let mut annotator = Annotator {
107 local: NodeMap::new(),
108 extern_cache: DefIdMap::new()
111 let stab = annotator.annotate(ast::CRATE_NODE_ID, krate.attrs.as_slice(), None);
112 visit::walk_crate(&mut annotator, krate, stab);
117 /// Lookup the stability for a node, loading external crate
118 /// metadata as necessary.
119 pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
120 // is this definition the implementation of a trait method?
121 match ty::trait_method_of_method(tcx, id) {
122 Some(trait_method_id) if trait_method_id != id => {
123 lookup(tcx, trait_method_id)
125 _ if is_local(id) => {
126 tcx.stability.borrow().local.find_copy(&id.node)
129 let stab = csearch::get_stability(&tcx.sess.cstore, id);
130 let mut index = tcx.stability.borrow_mut();
131 (*index).extern_cache.insert(id, stab.clone());