]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/stability.rs
rollup merge of #20080: seanmonstar/new-show-syntax
[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, 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};
24 use middle::ty;
25 use metadata::csearch;
26
27 use std::mem::replace;
28
29 /// A stability index, giving the stability level for items and methods.
30 pub struct Index {
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>>
35 }
36
37 // A private tree-walker for producing an Index.
38 struct Annotator {
39     index: Index,
40     parent: Option<Stability>
41 }
42
43 impl Annotator {
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, use_parent: bool,
47                    attrs: &Vec<Attribute>, f: F) where
48         F: FnOnce(&mut Annotator),
49     {
50         match attr::find_stability(attrs.as_slice()) {
51             Some(stab) => {
52                 self.index.local.insert(id, stab.clone());
53
54                 // Don't inherit #[stable]
55                 if stab.level != attr::Stable {
56                     let parent = replace(&mut self.parent, Some(stab));
57                     f(self);
58                     self.parent = parent;
59                 } else {
60                     f(self);
61                 }
62             }
63             None => {
64                 if use_parent {
65                     self.parent.clone().map(|stab| self.index.local.insert(id, stab));
66                 }
67                 f(self);
68             }
69         }
70     }
71 }
72
73 impl<'v> Visitor<'v> for Annotator {
74     fn visit_item(&mut self, i: &Item) {
75         // FIXME (#18969): the following is a hack around the fact
76         // that we cannot currently annotate the stability of
77         // `deriving`.  Basically, we do *not* allow stability
78         // inheritance on trait implementations, so that derived
79         // implementations appear to be unannotated. This then allows
80         // derived implementations to be automatically tagged with the
81         // stability of the trait. This is WRONG, but expedient to get
82         // libstd stabilized for the 1.0 release.
83         let use_parent = match i.node {
84             ast::ItemImpl(_, _, Some(_), _, _) => false,
85             _ => true,
86         };
87
88         self.annotate(i.id, use_parent, &i.attrs, |v| visit::walk_item(v, i));
89
90         if let ast::ItemStruct(ref sd, _) = i.node {
91             sd.ctor_id.map(|id| {
92                 self.annotate(id, true, &i.attrs, |_| {})
93             });
94         }
95     }
96
97     fn visit_fn(&mut self, fk: FnKind<'v>, _: &'v FnDecl,
98                 _: &'v Block, _: Span, _: NodeId) {
99         if let FkMethod(_, _, meth) = fk {
100             // Methods are not already annotated, so we annotate it
101             self.annotate(meth.id, true, &meth.attrs, |_| {});
102         }
103         // Items defined in a function body have no reason to have
104         // a stability attribute, so we don't recurse.
105     }
106
107     fn visit_trait_item(&mut self, t: &TraitItem) {
108         let (id, attrs) = match *t {
109             RequiredMethod(TypeMethod {id, ref attrs, ..}) => (id, attrs),
110
111             // work around lack of pattern matching for @ types
112             ProvidedMethod(ref method) => {
113                 match **method {
114                     Method {ref attrs, id, ..} => (id, attrs),
115                 }
116             }
117
118             TypeTraitItem(ref typedef) => (typedef.ty_param.id, &typedef.attrs),
119         };
120         self.annotate(id, true, attrs, |v| visit::walk_trait_item(v, t));
121     }
122
123     fn visit_variant(&mut self, var: &Variant, g: &'v Generics) {
124         self.annotate(var.node.id, true, &var.node.attrs,
125                       |v| visit::walk_variant(v, var, g))
126     }
127
128     fn visit_struct_field(&mut self, s: &StructField) {
129         self.annotate(s.node.id, true, &s.node.attrs,
130                       |v| visit::walk_struct_field(v, s));
131     }
132 }
133
134 impl Index {
135     /// Construct the stability index for a crate being compiled.
136     pub fn build(krate: &Crate) -> Index {
137         let mut annotator = Annotator {
138             index: Index {
139                 local: NodeMap::new(),
140                 extern_cache: DefIdMap::new()
141             },
142             parent: None
143         };
144         annotator.annotate(ast::CRATE_NODE_ID, true, &krate.attrs,
145                            |v| visit::walk_crate(v, krate));
146         annotator.index
147     }
148 }
149
150 /// Lookup the stability for a node, loading external crate
151 /// metadata as necessary.
152 pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
153     // is this definition the implementation of a trait method?
154     match ty::trait_item_of_item(tcx, id) {
155         Some(ty::MethodTraitItemId(trait_method_id))
156                 if trait_method_id != id => {
157             return lookup(tcx, trait_method_id)
158         }
159         _ => {}
160     }
161
162     let item_stab = if is_local(id) {
163         tcx.stability.borrow().local.get(&id.node).cloned()
164     } else {
165         let stab = csearch::get_stability(&tcx.sess.cstore, id);
166         let mut index = tcx.stability.borrow_mut();
167         (*index).extern_cache.insert(id, stab.clone());
168         stab
169     };
170
171     item_stab.or_else(|| {
172         if let Some(trait_id) = ty::trait_id_of_impl(tcx, id) {
173             // FIXME (#18969): for the time being, simply use the
174             // stability of the trait to determine the stability of any
175             // unmarked impls for it. See FIXME above for more details.
176
177             lookup(tcx, trait_id)
178         } else {
179             None
180         }
181     })
182 }