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, RequiredMethod, ProvidedMethod, TraitItem};
20 use syntax::ast::{TypeMethod, Method, Generics, StructField, TypeTraitItem};
21 use syntax::ast_util::is_local;
22 use syntax::attr::Stability;
23 use syntax::visit::{FnKind, FkMethod, Visitor};
25 use metadata::csearch;
27 use std::mem::replace;
29 /// A stability index, giving the stability level for items and methods.
31 // stability for crate-local items; unmarked stability == no entry
32 local: NodeMap<Stability>,
33 // cache for extern-crate items; unmarked stability == entry with None
34 extern_cache: DefIdMap<Option<Stability>>
37 // A private tree-walker for producing an Index.
40 parent: Option<Stability>
44 // Determine the stability for a node based on its attributes and inherited
45 // stability. The stability is recorded in the index and used as the parent.
46 fn annotate<F>(&mut self, id: NodeId, attrs: &Vec<Attribute>, f: F) where
47 F: FnOnce(&mut Annotator),
49 match attr::find_stability(attrs.as_slice()) {
51 self.index.local.insert(id, stab.clone());
53 // Don't inherit #[stable]
54 if stab.level != attr::Stable {
55 let parent = replace(&mut self.parent, Some(stab));
63 self.parent.clone().map(|stab| self.index.local.insert(id, stab));
70 impl<'v> Visitor<'v> for Annotator {
71 fn visit_item(&mut self, i: &Item) {
72 self.annotate(i.id, &i.attrs, |v| visit::walk_item(v, i));
74 if let ast::ItemStruct(ref sd, _) = i.node {
76 self.annotate(id, &i.attrs, |_| {})
81 fn visit_fn(&mut self, fk: FnKind<'v>, _: &'v FnDecl,
82 _: &'v Block, _: Span, _: NodeId) {
83 if let FkMethod(_, _, meth) = fk {
84 // Methods are not already annotated, so we annotate it
85 self.annotate(meth.id, &meth.attrs, |_| {});
87 // Items defined in a function body have no reason to have
88 // a stability attribute, so we don't recurse.
91 fn visit_trait_item(&mut self, t: &TraitItem) {
92 let (id, attrs) = match *t {
93 RequiredMethod(TypeMethod {id, ref attrs, ..}) => (id, attrs),
95 // work around lack of pattern matching for @ types
96 ProvidedMethod(ref method) => {
98 Method {ref attrs, id, ..} => (id, attrs),
102 TypeTraitItem(ref typedef) => (typedef.ty_param.id, &typedef.attrs),
104 self.annotate(id, attrs, |v| visit::walk_trait_item(v, t));
107 fn visit_variant(&mut self, var: &Variant, g: &'v Generics) {
108 self.annotate(var.node.id, &var.node.attrs, |v| visit::walk_variant(v, var, g))
111 fn visit_struct_field(&mut self, s: &StructField) {
112 self.annotate(s.node.id, &s.node.attrs, |v| visit::walk_struct_field(v, s));
115 fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
116 self.annotate(i.id, &i.attrs, |_| {});
121 /// Construct the stability index for a crate being compiled.
122 pub fn build(krate: &Crate) -> Index {
123 let mut annotator = Annotator {
125 local: NodeMap::new(),
126 extern_cache: DefIdMap::new()
130 annotator.annotate(ast::CRATE_NODE_ID, &krate.attrs, |v| visit::walk_crate(v, krate));
135 /// Lookup the stability for a node, loading external crate
136 /// metadata as necessary.
137 pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
138 // is this definition the implementation of a trait method?
139 match ty::trait_item_of_item(tcx, id) {
140 Some(ty::MethodTraitItemId(trait_method_id))
141 if trait_method_id != id => {
142 lookup(tcx, trait_method_id)
144 _ if is_local(id) => {
145 tcx.stability.borrow().local.get(&id.node).cloned()
148 let stab = csearch::get_stability(&tcx.sess.cstore, id);
149 let mut index = tcx.stability.borrow_mut();
150 (*index).extern_cache.insert(id, stab.clone());