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};
23 use metadata::{cstore, csearch};
25 /// A stability index, giving the stability level for items and methods.
27 // stability for crate-local items; unmarked stability == no entry
28 local: NodeMap<Stability>,
29 // cache for extern-crate items; unmarked stability == entry with None
30 extern_cache: DefIdMap<Option<Stability>>
33 // A private tree-walker for producing an Index.
39 // Determine the stability for a node based on its attributes and inherited
40 // stability. The stability is recorded in the index and returned.
41 fn annotate(&mut self, id: NodeId, attrs: &[Attribute],
42 parent: Option<Stability>) -> Option<Stability> {
43 match attr::find_stability(attrs).or(parent) {
45 self.index.local.insert(id, stab.clone());
53 impl Visitor<Option<Stability>> for Annotator {
54 fn visit_item(&mut self, i: &Item, parent: Option<Stability>) {
55 let stab = self.annotate(i.id, i.attrs.as_slice(), parent);
56 visit::walk_item(self, i, stab)
59 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block,
60 s: Span, _: NodeId, parent: Option<Stability>) {
61 let stab = match *fk {
62 FkMethod(_, _, meth) =>
63 self.annotate(meth.id, meth.attrs.as_slice(), parent),
66 visit::walk_fn(self, fk, fd, b, s, stab)
69 fn visit_trait_method(&mut self, t: &TraitMethod, parent: Option<Stability>) {
71 Required(TypeMethod {attrs: ref attrs, id: id, ..}) =>
72 self.annotate(id, attrs.as_slice(), parent),
74 // work around lack of pattern matching for @ types
75 Provided(method) => match *method {
76 Method {attrs: ref attrs, id: id, ..} =>
77 self.annotate(id, attrs.as_slice(), parent)
80 visit::walk_trait_method(self, t, stab)
83 fn visit_variant(&mut self, v: &Variant, g: &Generics, parent: Option<Stability>) {
84 let stab = self.annotate(v.node.id, v.node.attrs.as_slice(), parent);
85 visit::walk_variant(self, v, g, stab)
88 fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics,
89 _: NodeId, parent: Option<Stability>) {
90 s.ctor_id.map(|id| self.annotate(id, &[], parent.clone()));
91 visit::walk_struct_def(self, s, parent)
96 /// Construct the stability index for a crate being compiled.
97 pub fn build(krate: &Crate) -> Index {
98 let mut annotator = Annotator {
100 local: NodeMap::new(),
101 extern_cache: DefIdMap::new()
104 visit::walk_crate(&mut annotator, krate,
105 attr::find_stability(krate.attrs.as_slice()));
109 /// Lookup the stability for a node, loading external crate
110 /// metadata as necessary.
111 pub fn lookup(&mut self, cstore: &cstore::CStore, id: DefId) -> Option<Stability> {
113 self.lookup_local(id.node)
115 let stab = csearch::get_stability(cstore, id);
116 self.extern_cache.insert(id, stab.clone());
121 /// Lookup the stability for a local node without loading any external crates
122 pub fn lookup_local(&self, id: NodeId) -> Option<Stability> {
123 self.local.find_copy(&id)