2013-11-11 22:17:47 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2013-06-14 20:21:47 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Finds items that are externally reachable, to determine which items
|
|
|
|
// need to have their metadata (and possibly their AST) serialized.
|
|
|
|
// All items that can be referred to through an exported name are
|
|
|
|
// reachable, and when a reachable thing is inline or generic, it
|
|
|
|
// makes all other generics or inline functions that it references
|
|
|
|
// reachable as well.
|
|
|
|
|
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck;
|
2013-10-09 23:16:51 -05:00
|
|
|
use middle::privacy;
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2013-12-20 22:14:51 -06:00
|
|
|
use std::cell::RefCell;
|
2014-02-19 21:29:58 -06:00
|
|
|
use collections::HashSet;
|
2013-10-21 12:37:36 -05:00
|
|
|
use syntax::ast;
|
2013-06-14 20:21:47 -05:00
|
|
|
use syntax::ast_map;
|
2013-11-20 17:15:34 -06:00
|
|
|
use syntax::ast_util::{def_id_of_def, is_local};
|
2013-06-14 20:21:47 -05:00
|
|
|
use syntax::attr;
|
2013-08-13 06:53:13 -05:00
|
|
|
use syntax::visit::Visitor;
|
|
|
|
use syntax::visit;
|
2013-06-14 20:21:47 -05:00
|
|
|
|
|
|
|
// Returns true if the given set of attributes contains the `#[inline]`
|
|
|
|
// attribute.
|
2013-10-21 12:37:36 -05:00
|
|
|
fn attributes_specify_inlining(attrs: &[ast::Attribute]) -> bool {
|
2013-07-19 06:51:37 -05:00
|
|
|
attr::contains_name(attrs, "inline")
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the given set of generics implies that the item it's
|
|
|
|
// associated with must be inlined.
|
2013-10-21 12:37:36 -05:00
|
|
|
fn generics_require_inlining(generics: &ast::Generics) -> bool {
|
2013-06-14 20:21:47 -05:00
|
|
|
!generics.ty_params.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the given item must be inlined because it may be
|
|
|
|
// monomorphized or it was marked with `#[inline]`. This will only return
|
|
|
|
// true for functions.
|
2014-01-09 07:05:33 -06:00
|
|
|
fn item_might_be_inlined(item: &ast::Item) -> bool {
|
2013-06-14 20:21:47 -05:00
|
|
|
if attributes_specify_inlining(item.attrs) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemImpl(ref generics, _, _, _) |
|
|
|
|
ast::ItemFn(_, _, _, ref generics, _) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
generics_require_inlining(generics)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn method_might_be_inlined(tcx: ty::ctxt, method: &ast::Method,
|
2013-10-31 22:47:23 -05:00
|
|
|
impl_src: ast::DefId) -> bool {
|
|
|
|
if attributes_specify_inlining(method.attrs) ||
|
|
|
|
generics_require_inlining(&method.generics) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if is_local(impl_src) {
|
2013-12-27 18:09:29 -06:00
|
|
|
{
|
2014-02-13 23:07:09 -06:00
|
|
|
match tcx.map.find(impl_src.node) {
|
|
|
|
Some(ast_map::NodeItem(item)) => {
|
2013-12-27 18:09:29 -06:00
|
|
|
item_might_be_inlined(item)
|
|
|
|
}
|
|
|
|
Some(..) | None => {
|
|
|
|
tcx.sess.span_bug(method.span, "impl did is not an item")
|
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tcx.sess.span_bug(method.span, "found a foreign impl as a parent of a \
|
|
|
|
local method")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
// Information needed while computing reachability.
|
|
|
|
struct ReachableContext {
|
|
|
|
// The type context.
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
// The method map, which links node IDs of method call expressions to the
|
|
|
|
// methods they've been resolved to.
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
// The set of items which must be exported in the linkage sense.
|
2013-12-20 22:14:51 -06:00
|
|
|
reachable_symbols: @RefCell<HashSet<ast::NodeId>>,
|
2013-06-14 20:21:47 -05:00
|
|
|
// A worklist of item IDs. Each item ID in this worklist will be inlined
|
|
|
|
// and will be scanned for further references.
|
2013-12-22 16:24:09 -06:00
|
|
|
worklist: @RefCell<~[ast::NodeId]>,
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MarkSymbolVisitor {
|
2013-12-22 16:24:09 -06:00
|
|
|
worklist: @RefCell<~[ast::NodeId]>,
|
2013-08-13 06:53:13 -05:00
|
|
|
method_map: typeck::method_map,
|
|
|
|
tcx: ty::ctxt,
|
2013-12-20 22:14:51 -06:00
|
|
|
reachable_symbols: @RefCell<HashSet<ast::NodeId>>,
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<()> for MarkSymbolVisitor {
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_expr(&mut self, expr: &ast::Expr, _: ()) {
|
2013-08-13 06:53:13 -05:00
|
|
|
|
2013-10-31 22:47:23 -05:00
|
|
|
match expr.node {
|
|
|
|
ast::ExprPath(_) => {
|
2013-12-23 13:15:16 -06:00
|
|
|
let def_map = self.tcx.def_map.borrow();
|
|
|
|
let def = match def_map.get().find(&expr.id) {
|
2013-10-31 22:47:23 -05:00
|
|
|
Some(&def) => def,
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.span_bug(expr.span,
|
|
|
|
"def ID not in def map?!")
|
|
|
|
}
|
|
|
|
};
|
2013-08-13 06:53:13 -05:00
|
|
|
|
2013-10-31 22:47:23 -05:00
|
|
|
let def_id = def_id_of_def(def);
|
2013-12-01 19:56:55 -06:00
|
|
|
if is_local(def_id) {
|
|
|
|
if ReachableContext::
|
|
|
|
def_id_represents_local_inlined_item(self.tcx, def_id) {
|
2013-12-22 16:24:09 -06:00
|
|
|
{
|
|
|
|
let mut worklist = self.worklist.borrow_mut();
|
|
|
|
worklist.get().push(def_id.node)
|
|
|
|
}
|
2013-12-01 19:56:55 -06:00
|
|
|
} else {
|
|
|
|
match def {
|
|
|
|
// If this path leads to a static, then we may have
|
|
|
|
// to do some work to figure out whether the static
|
|
|
|
// is indeed reachable (address_insignificant
|
|
|
|
// statics are *never* reachable).
|
|
|
|
ast::DefStatic(..) => {
|
2013-12-22 16:24:09 -06:00
|
|
|
let mut worklist = self.worklist.borrow_mut();
|
|
|
|
worklist.get().push(def_id.node);
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this wasn't a static, then this destination is
|
|
|
|
// surely reachable.
|
|
|
|
_ => {
|
2013-12-20 22:14:51 -06:00
|
|
|
let mut reachable_symbols =
|
|
|
|
self.reachable_symbols.borrow_mut();
|
|
|
|
reachable_symbols.get().insert(def_id.node);
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprMethodCall(..) => {
|
2013-12-21 19:04:42 -06:00
|
|
|
let method_map = self.method_map.borrow();
|
|
|
|
match method_map.get().find(&expr.id) {
|
2014-02-19 14:11:45 -06:00
|
|
|
Some(&typeck::method_static(def_id)) => {
|
2013-12-01 19:56:55 -06:00
|
|
|
if is_local(def_id) {
|
|
|
|
if ReachableContext::
|
|
|
|
def_id_represents_local_inlined_item(
|
|
|
|
self.tcx,
|
|
|
|
def_id) {
|
2013-12-22 16:24:09 -06:00
|
|
|
{
|
|
|
|
let mut worklist = self.worklist
|
|
|
|
.borrow_mut();
|
|
|
|
worklist.get().push(def_id.node)
|
|
|
|
}
|
2013-12-20 22:14:51 -06:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut reachable_symbols =
|
|
|
|
self.reachable_symbols.borrow_mut();
|
|
|
|
reachable_symbols.get().insert(def_id.node);
|
|
|
|
}
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
Some(_) => {}
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.span_bug(expr.span,
|
2014-02-19 14:11:45 -06:00
|
|
|
"method call expression not in method map?!")
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2013-08-13 06:53:13 -05:00
|
|
|
|
2013-10-31 22:47:23 -05:00
|
|
|
visit::walk_expr(self, expr, ())
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_item(&mut self, _item: &ast::Item, _: ()) {
|
2013-10-31 22:47:23 -05:00
|
|
|
// Do not recurse into items. These items will be added to the worklist
|
|
|
|
// and recursed into manually if necessary.
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReachableContext {
|
|
|
|
// Creates a new reachability computation context.
|
2013-11-11 22:17:47 -06:00
|
|
|
fn new(tcx: ty::ctxt, method_map: typeck::method_map) -> ReachableContext {
|
2013-08-13 06:53:13 -05:00
|
|
|
ReachableContext {
|
|
|
|
tcx: tcx,
|
|
|
|
method_map: method_map,
|
2013-12-20 22:14:51 -06:00
|
|
|
reachable_symbols: @RefCell::new(HashSet::new()),
|
2013-12-22 16:24:09 -06:00
|
|
|
worklist: @RefCell::new(~[]),
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
// Returns true if the given def ID represents a local item that is
|
|
|
|
// eligible for inlining and false otherwise.
|
2013-10-21 12:37:36 -05:00
|
|
|
fn def_id_represents_local_inlined_item(tcx: ty::ctxt, def_id: ast::DefId)
|
2013-06-14 20:21:47 -05:00
|
|
|
-> bool {
|
2014-02-05 15:15:24 -06:00
|
|
|
if def_id.krate != ast::LOCAL_CRATE {
|
2013-06-14 20:21:47 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let node_id = def_id.node;
|
2014-02-13 23:07:09 -06:00
|
|
|
match tcx.map.find(node_id) {
|
|
|
|
Some(ast_map::NodeItem(item)) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemFn(..) => item_might_be_inlined(item),
|
2013-06-14 20:21:47 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
Some(ast_map::NodeTraitMethod(trait_method)) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
match *trait_method {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Required(_) => false,
|
|
|
|
ast::Provided(_) => true,
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
Some(ast_map::NodeMethod(method)) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
if generics_require_inlining(&method.generics) ||
|
|
|
|
attributes_specify_inlining(method.attrs) {
|
|
|
|
true
|
|
|
|
} else {
|
2014-02-13 23:07:09 -06:00
|
|
|
let impl_did = tcx.map.get_parent_did(node_id);
|
2013-06-14 20:21:47 -05:00
|
|
|
// Check the impl. If the generics on the self type of the
|
|
|
|
// impl require inlining, this method does too.
|
2014-02-05 15:15:24 -06:00
|
|
|
assert!(impl_did.krate == ast::LOCAL_CRATE);
|
2014-02-13 23:07:09 -06:00
|
|
|
match tcx.map.expect_item(impl_did.node).node {
|
|
|
|
ast::ItemImpl(ref generics, _, _, _) => {
|
|
|
|
generics_require_inlining(generics)
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
_ => false
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => false,
|
2013-06-18 11:39:16 -05:00
|
|
|
None => false // This will happen for default methods.
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to set up a visitor for `propagate()` below.
|
2013-08-13 06:53:13 -05:00
|
|
|
fn init_visitor(&self) -> MarkSymbolVisitor {
|
2013-06-14 20:21:47 -05:00
|
|
|
let (worklist, method_map) = (self.worklist, self.method_map);
|
|
|
|
let (tcx, reachable_symbols) = (self.tcx, self.reachable_symbols);
|
|
|
|
|
2013-08-13 06:53:13 -05:00
|
|
|
MarkSymbolVisitor {
|
|
|
|
worklist: worklist,
|
|
|
|
method_map: method_map,
|
|
|
|
tcx: tcx,
|
|
|
|
reachable_symbols: reachable_symbols,
|
|
|
|
}
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Mark all symbols that the symbols on the worklist touch.
|
|
|
|
fn propagate(&self) {
|
2013-08-13 06:53:13 -05:00
|
|
|
let mut visitor = self.init_visitor();
|
2013-06-14 20:21:47 -05:00
|
|
|
let mut scanned = HashSet::new();
|
2013-12-22 16:24:09 -06:00
|
|
|
loop {
|
|
|
|
let search_item = {
|
|
|
|
let mut worklist = self.worklist.borrow_mut();
|
|
|
|
if worklist.get().len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
2013-12-23 09:20:52 -06:00
|
|
|
let search_item = worklist.get().pop().unwrap();
|
2013-12-22 16:24:09 -06:00
|
|
|
if scanned.contains(&search_item) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
search_item
|
|
|
|
};
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
scanned.insert(search_item);
|
2014-02-13 23:07:09 -06:00
|
|
|
match self.tcx.map.find(search_item) {
|
2014-01-17 06:23:09 -06:00
|
|
|
Some(ref item) => self.propagate_node(item, search_item,
|
2013-11-18 01:19:44 -06:00
|
|
|
&mut visitor),
|
|
|
|
None if search_item == ast::CRATE_NODE_ID => {}
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.bug(format!("found unmapped ID in worklist: \
|
|
|
|
{}",
|
|
|
|
search_item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn propagate_node(&self, node: &ast_map::Node,
|
2013-11-18 01:19:44 -06:00
|
|
|
search_item: ast::NodeId,
|
|
|
|
visitor: &mut MarkSymbolVisitor) {
|
2013-12-22 16:40:03 -06:00
|
|
|
if !self.tcx.sess.building_library.get() {
|
2013-11-18 01:19:44 -06:00
|
|
|
// If we are building an executable, then there's no need to flag
|
|
|
|
// anything as external except for `extern fn` types. These
|
|
|
|
// functions may still participate in some form of native interface,
|
|
|
|
// but all other rust-only interfaces can be private (they will not
|
|
|
|
// participate in linkage after this product is produced)
|
|
|
|
match *node {
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeItem(item) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemFn(_, ast::ExternFn, _, _, _) => {
|
2013-12-20 22:14:51 -06:00
|
|
|
let mut reachable_symbols =
|
|
|
|
self.reachable_symbols.borrow_mut();
|
|
|
|
reachable_symbols.get().insert(search_item);
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we are building a library, then reachable symbols will
|
|
|
|
// continue to participate in linkage after this product is
|
|
|
|
// produced. In this case, we traverse the ast node, recursing on
|
|
|
|
// all reachable nodes from this one.
|
2013-12-20 22:14:51 -06:00
|
|
|
let mut reachable_symbols = self.reachable_symbols.borrow_mut();
|
|
|
|
reachable_symbols.get().insert(search_item);
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
|
2013-11-18 01:19:44 -06:00
|
|
|
match *node {
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeItem(item) => {
|
2013-11-18 01:19:44 -06:00
|
|
|
match item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemFn(_, _, _, _, search_block) => {
|
2013-11-18 01:19:44 -06:00
|
|
|
if item_might_be_inlined(item) {
|
|
|
|
visit::walk_block(visitor, search_block, ())
|
2013-10-21 12:37:36 -05:00
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2013-10-21 12:37:36 -05:00
|
|
|
|
2013-12-01 19:56:55 -06:00
|
|
|
// Statics with insignificant addresses are not reachable
|
|
|
|
// because they're inlined specially into all other crates.
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemStatic(..) => {
|
2013-12-01 19:56:55 -06:00
|
|
|
if attr::contains_name(item.attrs,
|
|
|
|
"address_insignificant") {
|
2013-12-20 22:14:51 -06:00
|
|
|
let mut reachable_symbols =
|
|
|
|
self.reachable_symbols.borrow_mut();
|
|
|
|
reachable_symbols.get().remove(&search_item);
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 01:19:44 -06:00
|
|
|
// These are normal, nothing reachable about these
|
|
|
|
// inherently and their children are already in the
|
2013-11-20 17:15:34 -06:00
|
|
|
// worklist, as determined by the privacy pass
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemTy(..) |
|
|
|
|
ast::ItemMod(..) | ast::ItemForeignMod(..) |
|
|
|
|
ast::ItemImpl(..) | ast::ItemTrait(..) |
|
|
|
|
ast::ItemStruct(..) | ast::ItemEnum(..) => {}
|
2013-10-21 12:37:36 -05:00
|
|
|
|
2013-11-18 01:19:44 -06:00
|
|
|
_ => {
|
|
|
|
self.tcx.sess.span_bug(item.span,
|
|
|
|
"found non-function item \
|
|
|
|
in worklist?!")
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeTraitMethod(trait_method) => {
|
2013-11-18 01:19:44 -06:00
|
|
|
match *trait_method {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Required(..) => {
|
2013-11-18 01:19:44 -06:00
|
|
|
// Keep going, nothing to get exported
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Provided(ref method) => {
|
2013-11-30 16:00:39 -06:00
|
|
|
visit::walk_block(visitor, method.body, ())
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeMethod(method) => {
|
|
|
|
let did = self.tcx.map.get_parent_did(search_item);
|
2013-11-18 01:19:44 -06:00
|
|
|
if method_might_be_inlined(self.tcx, method, did) {
|
2013-11-30 16:00:39 -06:00
|
|
|
visit::walk_block(visitor, method.body, ())
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
// Nothing to recurse on for these
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeForeignItem(_) |
|
|
|
|
ast_map::NodeVariant(_) |
|
|
|
|
ast_map::NodeStructCtor(_) => {}
|
2013-11-18 01:19:44 -06:00
|
|
|
_ => {
|
|
|
|
self.tcx.sess.bug(format!("found unexpected thingy in \
|
|
|
|
worklist: {}",
|
2014-02-13 23:07:09 -06:00
|
|
|
self.tcx.map.node_to_str(search_item)))
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: Mark all destructors as reachable.
|
|
|
|
//
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME(pcwalton): This is a conservative overapproximation, but fixing
|
2013-06-14 20:21:47 -05:00
|
|
|
// this properly would result in the necessity of computing *type*
|
|
|
|
// reachability, which might result in a compile time loss.
|
|
|
|
fn mark_destructors_reachable(&self) {
|
2013-12-19 20:47:16 -06:00
|
|
|
let destructor_for_type = self.tcx.destructor_for_type.borrow();
|
|
|
|
for (_, destructor_def_id) in destructor_for_type.get().iter() {
|
2014-02-05 15:15:24 -06:00
|
|
|
if destructor_def_id.krate == ast::LOCAL_CRATE {
|
2013-12-20 22:14:51 -06:00
|
|
|
let mut reachable_symbols = self.reachable_symbols
|
|
|
|
.borrow_mut();
|
|
|
|
reachable_symbols.get().insert(destructor_def_id.node);
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_reachable(tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
2013-10-09 23:16:51 -05:00
|
|
|
exported_items: &privacy::ExportedItems)
|
2013-12-20 22:14:51 -06:00
|
|
|
-> @RefCell<HashSet<ast::NodeId>> {
|
2013-11-11 22:17:47 -06:00
|
|
|
let reachable_context = ReachableContext::new(tcx, method_map);
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2013-10-09 23:16:51 -05:00
|
|
|
// Step 1: Seed the worklist with all nodes which were found to be public as
|
2014-01-16 12:12:31 -06:00
|
|
|
// a result of the privacy pass along with all local lang items. If
|
|
|
|
// other crates link to us, they're going to expect to be able to
|
|
|
|
// use the lang items, so we need to be sure to mark them as
|
|
|
|
// exported.
|
|
|
|
let mut worklist = reachable_context.worklist.borrow_mut();
|
2013-10-09 23:16:51 -05:00
|
|
|
for &id in exported_items.iter() {
|
2013-12-22 16:24:09 -06:00
|
|
|
worklist.get().push(id);
|
2013-10-09 23:16:51 -05:00
|
|
|
}
|
2014-01-16 12:12:31 -06:00
|
|
|
for (_, item) in tcx.lang_items.items() {
|
|
|
|
match *item {
|
|
|
|
Some(did) if is_local(did) => {
|
|
|
|
worklist.get().push(did.node);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drop(worklist);
|
2013-06-14 20:21:47 -05:00
|
|
|
|
|
|
|
// Step 2: Mark all symbols that the symbols on the worklist touch.
|
|
|
|
reachable_context.propagate();
|
|
|
|
|
|
|
|
// Step 3: Mark all destructors as reachable.
|
|
|
|
reachable_context.mark_destructors_reachable();
|
|
|
|
|
|
|
|
// Return the set of reachable symbols.
|
|
|
|
reachable_context.reachable_symbols
|
|
|
|
}
|