]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/stability.rs
auto merge of #15069 : luqmana/rust/cia, r=pcwalton
[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 middle::ty;
24 use metadata::csearch;
25
26 /// A stability index, giving the stability level for items and methods.
27 pub struct Index {
28     // stability for crate-local items; unmarked stability == no entry
29     local: NodeMap<Stability>,
30     // cache for extern-crate items; unmarked stability == entry with None
31     extern_cache: DefIdMap<Option<Stability>>
32 }
33
34 // A private tree-walker for producing an Index.
35 struct Annotator {
36     index: Index
37 }
38
39 impl Annotator {
40     // Determine the stability for a node based on its attributes and inherited
41     // stability. The stability is recorded in the index and returned.
42     fn annotate(&mut self, id: NodeId, attrs: &[Attribute],
43                 parent: Option<Stability>) -> Option<Stability> {
44         match attr::find_stability(attrs).or(parent) {
45             Some(stab) => {
46                 self.index.local.insert(id, stab.clone());
47                 Some(stab)
48             }
49             None => None
50         }
51     }
52 }
53
54 impl Visitor<Option<Stability>> for Annotator {
55     fn visit_item(&mut self, i: &Item, parent: Option<Stability>) {
56         let stab = self.annotate(i.id, i.attrs.as_slice(), parent);
57         visit::walk_item(self, i, stab)
58     }
59
60     fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block,
61                 s: Span, _: NodeId, parent: Option<Stability>) {
62         let stab = match *fk {
63             FkMethod(_, _, meth) =>
64                 self.annotate(meth.id, meth.attrs.as_slice(), parent),
65             _ => parent
66         };
67         visit::walk_fn(self, fk, fd, b, s, stab)
68     }
69
70     fn visit_trait_method(&mut self, t: &TraitMethod, parent: Option<Stability>) {
71         let stab = match *t {
72             Required(TypeMethod {attrs: ref attrs, id: id, ..}) =>
73                 self.annotate(id, attrs.as_slice(), parent),
74
75             // work around lack of pattern matching for @ types
76             Provided(method) => match *method {
77                 Method {attrs: ref attrs, id: id, ..} =>
78                     self.annotate(id, attrs.as_slice(), parent)
79             }
80         };
81         visit::walk_trait_method(self, t, stab)
82     }
83
84     fn visit_variant(&mut self, v: &Variant, g: &Generics, parent: Option<Stability>) {
85         let stab = self.annotate(v.node.id, v.node.attrs.as_slice(), parent);
86         visit::walk_variant(self, v, g, stab)
87     }
88
89     fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics,
90                         _: NodeId, parent: Option<Stability>) {
91         s.ctor_id.map(|id| self.annotate(id, &[], parent.clone()));
92         visit::walk_struct_def(self, s, parent)
93     }
94 }
95
96 impl Index {
97     /// Construct the stability index for a crate being compiled.
98     pub fn build(krate: &Crate) -> Index {
99         let mut annotator = Annotator {
100             index: Index {
101                 local: NodeMap::new(),
102                 extern_cache: DefIdMap::new()
103             }
104         };
105         visit::walk_crate(&mut annotator, krate,
106                           attr::find_stability(krate.attrs.as_slice()));
107         annotator.index
108     }
109 }
110
111 /// Lookup the stability for a node, loading external crate
112 /// metadata as necessary.
113 pub fn lookup(tcx: &ty::ctxt,  id: DefId) -> Option<Stability> {
114     // is this definition the implementation of a trait method?
115     match ty::trait_method_of_method(tcx, id) {
116         Some(trait_method_id) if trait_method_id != id => {
117             lookup(tcx, trait_method_id)
118         }
119         _ if is_local(id) => {
120             tcx.stability.borrow().local.find_copy(&id.node)
121         }
122         _ => {
123             let stab = csearch::get_stability(&tcx.sess.cstore, id);
124             let mut index = tcx.stability.borrow_mut();
125             (*index).extern_cache.insert(id, stab.clone());
126             stab
127         }
128     }
129 }