1 // Copyright 2012-2013 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 // Finds items that are externally reachable, to determine which items
12 // need to have their metadata (and possibly their AST) serialized.
13 // All items that can be referred to through an exported name are
14 // reachable, and when a reachable thing is inline or generic, it
15 // makes all other generics or inline functions that it references
22 use util::nodemap::NodeSet;
24 use std::collections::HashSet;
28 use syntax::ast_util::{is_local, PostExpansionMethod};
29 use syntax::attr::{InlineAlways, InlineHint, InlineNever, InlineNone};
31 use syntax::visit::Visitor;
34 // Returns true if the given set of attributes contains the `#[inline]`
36 fn attributes_specify_inlining(attrs: &[ast::Attribute]) -> bool {
37 match attr::find_inline_attr(attrs) {
38 InlineNone | InlineNever => false,
39 InlineAlways | InlineHint => true,
43 // Returns true if the given set of generics implies that the item it's
44 // associated with must be inlined.
45 fn generics_require_inlining(generics: &ast::Generics) -> bool {
46 !generics.ty_params.is_empty()
49 // Returns true if the given item must be inlined because it may be
50 // monomorphized or it was marked with `#[inline]`. This will only return
51 // true for functions.
52 fn item_might_be_inlined(item: &ast::Item) -> bool {
53 if attributes_specify_inlining(item.attrs[]) {
58 ast::ItemImpl(_, ref generics, _, _, _) |
59 ast::ItemFn(_, _, _, ref generics, _) => {
60 generics_require_inlining(generics)
66 fn method_might_be_inlined(tcx: &ty::ctxt, method: &ast::Method,
67 impl_src: ast::DefId) -> bool {
68 if attributes_specify_inlining(method.attrs[]) ||
69 generics_require_inlining(method.pe_generics()) {
72 if is_local(impl_src) {
74 match tcx.map.find(impl_src.node) {
75 Some(ast_map::NodeItem(item)) => {
76 item_might_be_inlined(&*item)
79 tcx.sess.span_bug(method.span, "impl did is not an item")
84 tcx.sess.span_bug(method.span, "found a foreign impl as a parent of a \
89 // Information needed while computing reachability.
90 struct ReachableContext<'a, 'tcx: 'a> {
92 tcx: &'a ty::ctxt<'tcx>,
93 // The set of items which must be exported in the linkage sense.
94 reachable_symbols: NodeSet,
95 // A worklist of item IDs. Each item ID in this worklist will be inlined
96 // and will be scanned for further references.
97 worklist: Vec<ast::NodeId>,
98 // Whether any output of this compilation is a library
102 impl<'a, 'tcx, 'v> Visitor<'v> for ReachableContext<'a, 'tcx> {
104 fn visit_expr(&mut self, expr: &ast::Expr) {
107 ast::ExprPath(_) => {
108 let def = match self.tcx.def_map.borrow().get(&expr.id) {
111 self.tcx.sess.span_bug(expr.span,
112 "def ID not in def map?!")
116 let def_id = def.def_id();
117 if is_local(def_id) {
118 if self.def_id_represents_local_inlined_item(def_id) {
119 self.worklist.push(def_id.node)
122 // If this path leads to a constant, then we need to
123 // recurse into the constant to continue finding
124 // items that are reachable.
125 def::DefConst(..) => {
126 self.worklist.push(def_id.node);
129 // If this wasn't a static, then the destination is
132 self.reachable_symbols.insert(def_id.node);
138 ast::ExprMethodCall(..) => {
139 let method_call = ty::MethodCall::expr(expr.id);
140 match (*self.tcx.method_map.borrow())[method_call].origin {
141 ty::MethodStatic(def_id) => {
142 if is_local(def_id) {
143 if self.def_id_represents_local_inlined_item(def_id) {
144 self.worklist.push(def_id.node)
146 self.reachable_symbols.insert(def_id.node);
155 visit::walk_expr(self, expr)
158 fn visit_item(&mut self, _item: &ast::Item) {
159 // Do not recurse into items. These items will be added to the worklist
160 // and recursed into manually if necessary.
164 impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
165 // Creates a new reachability computation context.
166 fn new(tcx: &'a ty::ctxt<'tcx>) -> ReachableContext<'a, 'tcx> {
167 let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
168 *ty != config::CrateTypeExecutable
172 reachable_symbols: NodeSet::new(),
173 worklist: Vec::new(),
174 any_library: any_library,
178 // Returns true if the given def ID represents a local item that is
179 // eligible for inlining and false otherwise.
180 fn def_id_represents_local_inlined_item(&self, def_id: ast::DefId) -> bool {
181 if def_id.krate != ast::LOCAL_CRATE {
185 let node_id = def_id.node;
186 match self.tcx.map.find(node_id) {
187 Some(ast_map::NodeItem(item)) => {
189 ast::ItemFn(..) => item_might_be_inlined(&*item),
193 Some(ast_map::NodeTraitItem(trait_method)) => {
194 match *trait_method {
195 ast::RequiredMethod(_) => false,
196 ast::ProvidedMethod(_) => true,
197 ast::TypeTraitItem(_) => false,
200 Some(ast_map::NodeImplItem(impl_item)) => {
202 ast::MethodImplItem(ref method) => {
203 if generics_require_inlining(method.pe_generics()) ||
204 attributes_specify_inlining(
208 let impl_did = self.tcx
210 .get_parent_did(node_id);
211 // Check the impl. If the generics on the self
212 // type of the impl require inlining, this method
214 assert!(impl_did.krate == ast::LOCAL_CRATE);
217 .expect_item(impl_did.node)
219 ast::ItemImpl(_, ref generics, _, _, _) => {
220 generics_require_inlining(generics)
226 ast::TypeImplItem(_) => false,
230 None => false // This will happen for default methods.
234 // Step 2: Mark all symbols that the symbols on the worklist touch.
235 fn propagate(&mut self) {
236 let mut scanned = HashSet::new();
238 let search_item = match self.worklist.pop() {
242 if !scanned.insert(search_item) {
246 match self.tcx.map.find(search_item) {
247 Some(ref item) => self.propagate_node(item, search_item),
248 None if search_item == ast::CRATE_NODE_ID => {}
250 self.tcx.sess.bug(format!("found unmapped ID in worklist: \
258 fn propagate_node(&mut self, node: &ast_map::Node,
259 search_item: ast::NodeId) {
260 if !self.any_library {
261 // If we are building an executable, then there's no need to flag
262 // anything as external except for `extern fn` types. These
263 // functions may still participate in some form of native interface,
264 // but all other rust-only interfaces can be private (they will not
265 // participate in linkage after this product is produced)
266 if let ast_map::NodeItem(item) = *node {
267 if let ast::ItemFn(_, _, abi, _, _) = item.node {
268 if abi != abi::Rust {
269 self.reachable_symbols.insert(search_item);
274 // If we are building a library, then reachable symbols will
275 // continue to participate in linkage after this product is
276 // produced. In this case, we traverse the ast node, recursing on
277 // all reachable nodes from this one.
278 self.reachable_symbols.insert(search_item);
282 ast_map::NodeItem(item) => {
284 ast::ItemFn(_, _, _, _, ref search_block) => {
285 if item_might_be_inlined(&*item) {
286 visit::walk_block(self, &**search_block)
290 // Reachable constants will be inlined into other crates
291 // unconditionally, so we need to make sure that their
292 // contents are also reachable.
293 ast::ItemConst(_, ref init) => {
294 self.visit_expr(&**init);
297 // These are normal, nothing reachable about these
298 // inherently and their children are already in the
299 // worklist, as determined by the privacy pass
300 ast::ItemTy(..) | ast::ItemStatic(_, _, _) |
301 ast::ItemMod(..) | ast::ItemForeignMod(..) |
302 ast::ItemImpl(..) | ast::ItemTrait(..) |
303 ast::ItemStruct(..) | ast::ItemEnum(..) => {}
306 self.tcx.sess.span_bug(item.span,
307 "found non-function item \
312 ast_map::NodeTraitItem(trait_method) => {
313 match *trait_method {
314 ast::RequiredMethod(..) => {
315 // Keep going, nothing to get exported
317 ast::ProvidedMethod(ref method) => {
318 visit::walk_block(self, &*method.pe_body());
320 ast::TypeTraitItem(_) => {}
323 ast_map::NodeImplItem(impl_item) => {
325 ast::MethodImplItem(ref method) => {
326 let did = self.tcx.map.get_parent_did(search_item);
327 if method_might_be_inlined(self.tcx, &**method, did) {
328 visit::walk_block(self, method.pe_body())
331 ast::TypeImplItem(_) => {}
334 // Nothing to recurse on for these
335 ast_map::NodeForeignItem(_) |
336 ast_map::NodeVariant(_) |
337 ast_map::NodeStructCtor(_) => {}
341 .bug(format!("found unexpected thingy in worklist: {}",
344 .node_to_string(search_item))[])
349 // Step 3: Mark all destructors as reachable.
351 // FIXME(pcwalton): This is a conservative overapproximation, but fixing
352 // this properly would result in the necessity of computing *type*
353 // reachability, which might result in a compile time loss.
354 fn mark_destructors_reachable(&mut self) {
355 for (_, destructor_def_id) in self.tcx.destructor_for_type.borrow().iter() {
356 if destructor_def_id.krate == ast::LOCAL_CRATE {
357 self.reachable_symbols.insert(destructor_def_id.node);
363 pub fn find_reachable(tcx: &ty::ctxt,
364 exported_items: &privacy::ExportedItems)
366 let mut reachable_context = ReachableContext::new(tcx);
368 // Step 1: Seed the worklist with all nodes which were found to be public as
369 // a result of the privacy pass along with all local lang items. If
370 // other crates link to us, they're going to expect to be able to
371 // use the lang items, so we need to be sure to mark them as
373 for id in exported_items.iter() {
374 reachable_context.worklist.push(*id);
376 for (_, item) in tcx.lang_items.items() {
378 Some(did) if is_local(did) => {
379 reachable_context.worklist.push(did.node);
385 // Step 2: Mark all symbols that the symbols on the worklist touch.
386 reachable_context.propagate();
388 // Step 3: Mark all destructors as reachable.
389 reachable_context.mark_destructors_reachable();
391 // Return the set of reachable symbols.
392 reachable_context.reachable_symbols