1 // Copyright 2012-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.
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.
14 // This pass simply determines what all "export" keywords refer to and
15 // writes the results into the export map.
17 // FIXME #4953 This pass will be removed once exports change to per-item.
18 // Then this operation can simply be performed as part of item (or import)
21 use {Module, NameBindings, Resolver};
22 use Namespace::{mod, TypeNS, ValueNS};
24 use rustc::middle::def::Export;
26 use syntax::parse::token;
30 struct ExportRecorder<'a, 'b:'a, 'tcx:'b> {
31 resolver: &'a mut Resolver<'b, 'tcx>
34 // Deref and DerefMut impls allow treating ExportRecorder as Resolver.
35 impl<'a, 'b, 'tcx:'b> Deref<Resolver<'b, 'tcx>> for ExportRecorder<'a, 'b, 'tcx> {
36 fn deref<'c>(&'c self) -> &'c Resolver<'b, 'tcx> {
41 impl<'a, 'b, 'tcx:'b> DerefMut<Resolver<'b, 'tcx>> for ExportRecorder<'a, 'b, 'tcx> {
42 fn deref_mut<'c>(&'c mut self) -> &'c mut Resolver<'b, 'tcx> {
47 impl<'a, 'b, 'tcx> ExportRecorder<'a, 'b, 'tcx> {
48 fn record_exports_for_module_subtree(&mut self,
49 module_: Rc<Module>) {
50 // If this isn't a local krate, then bail out. We don't need to record
51 // exports for nonlocal crates.
53 match module_.def_id.get() {
54 Some(def_id) if def_id.krate == ast::LOCAL_CRATE => {
56 debug!("(recording exports for module subtree) recording \
57 exports for local module `{}`",
58 self.module_to_string(&*module_));
61 // Record exports for the root module.
62 debug!("(recording exports for module subtree) recording \
63 exports for root module `{}`",
64 self.module_to_string(&*module_));
68 debug!("(recording exports for module subtree) not recording \
70 self.module_to_string(&*module_));
75 self.record_exports_for_module(&*module_);
76 self.populate_module_if_necessary(&module_);
78 for (_, child_name_bindings) in module_.children.borrow().iter() {
79 match child_name_bindings.get_module_if_available() {
83 Some(child_module) => {
84 self.record_exports_for_module_subtree(child_module);
89 for (_, child_module) in module_.anonymous_children.borrow().iter() {
90 self.record_exports_for_module_subtree(child_module.clone());
94 fn record_exports_for_module(&mut self, module_: &Module) {
95 let mut exports = Vec::new();
97 self.add_exports_for_module(&mut exports, module_);
98 match module_.def_id.get() {
100 self.export_map.insert(def_id.node, exports);
101 debug!("(computing exports) writing exports for {} (some)",
108 fn add_exports_of_namebindings(&mut self,
109 exports: &mut Vec<Export>,
111 namebindings: &NameBindings,
113 match namebindings.def_for_namespace(ns) {
115 debug!("(computing exports) YES: export '{}' => {}",
117 exports.push(Export {
123 debug!("(computing exports) NO: {}", d_opt);
128 fn add_exports_for_module(&mut self,
129 exports: &mut Vec<Export>,
131 for (name, importresolution) in module_.import_resolutions.borrow().iter() {
132 if !importresolution.is_public {
135 let xs = [TypeNS, ValueNS];
136 for &ns in xs.iter() {
137 match importresolution.target_for_namespace(ns) {
139 debug!("(computing exports) maybe export '{}'",
140 token::get_name(*name));
141 self.add_exports_of_namebindings(exports,
153 pub fn record(resolver: &mut Resolver) {
154 let mut recorder = ExportRecorder { resolver: resolver };
155 let root_module = recorder.graph_root.get_module();
156 recorder.record_exports_for_module_subtree(root_module);