]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/stability.rs
auto merge of #14963 : w3ln4/rust/master, r=alexcrichton
[rust.git] / src / librustc / middle / stability.rs
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.
4 //
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.
10
11 //! A pass that annotates every item and method with its stability level,
12 //! propagating default levels lexically from parent to children ast nodes.
13
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};
24
25 /// A stability index, giving the stability level for items and methods.
26 pub struct Index {
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>>
31 }
32
33 // A private tree-walker for producing an Index.
34 struct Annotator {
35     index: Index
36 }
37
38 impl Annotator {
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) {
44             Some(stab) => {
45                 self.index.local.insert(id, stab.clone());
46                 Some(stab)
47             }
48             None => None
49         }
50     }
51 }
52
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)
57     }
58
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),
64             _ => parent
65         };
66         visit::walk_fn(self, fk, fd, b, s, stab)
67     }
68
69     fn visit_trait_method(&mut self, t: &TraitMethod, parent: Option<Stability>) {
70         let stab = match *t {
71             Required(TypeMethod {attrs: ref attrs, id: id, ..}) =>
72                 self.annotate(id, attrs.as_slice(), parent),
73
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)
78             }
79         };
80         visit::walk_trait_method(self, t, stab)
81     }
82
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)
86     }
87
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)
92     }
93 }
94
95 impl Index {
96     /// Construct the stability index for a crate being compiled.
97     pub fn build(krate: &Crate) -> Index {
98         let mut annotator = Annotator {
99             index: Index {
100                 local: NodeMap::new(),
101                 extern_cache: DefIdMap::new()
102             }
103         };
104         visit::walk_crate(&mut annotator, krate,
105                           attr::find_stability(krate.attrs.as_slice()));
106         annotator.index
107     }
108
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> {
112         if is_local(id) {
113             self.lookup_local(id.node)
114         } else {
115             let stab = csearch::get_stability(cstore, id);
116             self.extern_cache.insert(id, stab.clone());
117             stab
118         }
119     }
120
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)
124     }
125 }