]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/stability.rs
auto merge of #15621 : sfackler/rust/attr-span, r=cmr
[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;
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};
24 use middle::ty;
25 use metadata::csearch;
26
27 /// A stability index, giving the stability level for items and methods.
28 pub struct Index {
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>>
33 }
34
35 // A private tree-walker for producing an Index.
36 struct Annotator {
37     index: Index
38 }
39
40 impl Annotator {
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) {
46             Some(stab) => {
47                 self.index.local.insert(id, stab.clone());
48                 Some(stab)
49             }
50             None => None
51         }
52     }
53 }
54
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)
59     }
60
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),
66             _ => parent
67         };
68         visit::walk_fn(self, fk, fd, b, s, stab)
69     }
70
71     fn visit_trait_method(&mut self, t: &TraitMethod, parent: Option<Stability>) {
72         let stab = match *t {
73             Required(TypeMethod {attrs: ref attrs, id: id, ..}) =>
74                 self.annotate(id, attrs.as_slice(), parent),
75
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)
80             }
81         };
82         visit::walk_trait_method(self, t, stab)
83     }
84
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)
88     }
89
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)
94     }
95
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)
99     }
100 }
101
102 impl Index {
103     /// Construct the stability index for a crate being compiled.
104     pub fn build(krate: &Crate) -> Index {
105         let mut annotator = Annotator {
106             index: Index {
107                 local: NodeMap::new(),
108                 extern_cache: DefIdMap::new()
109             }
110         };
111         let stab = annotator.annotate(ast::CRATE_NODE_ID, krate.attrs.as_slice(), None);
112         visit::walk_crate(&mut annotator, krate, stab);
113         annotator.index
114     }
115 }
116
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)
124         }
125         _ if is_local(id) => {
126             tcx.stability.borrow().local.find_copy(&id.node)
127         }
128         _ => {
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());
132             stab
133         }
134     }
135 }