]> git.lizzy.rs Git - rust.git/blob - src/modules.rs
5b5050d51c9641576029b8731246c6d8e006228d
[rust.git] / src / modules.rs
1 // Copyright 2015 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 use utils;
12
13 use std::path::{Path, PathBuf};
14 use std::collections::BTreeMap;
15
16 use syntax::ast;
17 use syntax::codemap;
18 use syntax::parse::parser;
19
20
21 /// List all the files containing modules of a crate.
22 /// If a file is used twice in a crate, it appears only once.
23 pub fn list_files<'a>(
24     krate: &'a ast::Crate,
25     codemap: &codemap::CodeMap,
26 ) -> BTreeMap<PathBuf, &'a ast::Mod> {
27     let mut result = BTreeMap::new(); // Enforce file order determinism
28     let root_filename: PathBuf = codemap.span_to_filename(krate.span).into();
29     list_submodules(
30         &krate.module,
31         root_filename.parent().unwrap(),
32         codemap,
33         &mut result,
34     );
35     result.insert(root_filename, &krate.module);
36     result
37 }
38
39 /// Recursively list all external modules included in a module.
40 fn list_submodules<'a>(
41     module: &'a ast::Mod,
42     search_dir: &Path,
43     codemap: &codemap::CodeMap,
44     result: &mut BTreeMap<PathBuf, &'a ast::Mod>,
45 ) {
46     debug!("list_submodules: search_dir: {:?}", search_dir);
47     for item in &module.items {
48         if let ast::ItemKind::Mod(ref sub_mod) = item.node {
49             if !utils::contains_skip(&item.attrs) {
50                 let is_internal = codemap.span_to_filename(item.span) ==
51                     codemap.span_to_filename(sub_mod.inner);
52                 let dir_path = if is_internal {
53                     search_dir.join(&item.ident.to_string())
54                 } else {
55                     let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap);
56                     let dir_path = mod_path.parent().unwrap().to_owned();
57                     result.insert(mod_path, sub_mod);
58                     dir_path
59                 };
60                 list_submodules(sub_mod, &dir_path, codemap, result);
61             }
62         }
63     }
64 }
65
66 /// Find the file corresponding to an external mod
67 fn module_file(
68     id: ast::Ident,
69     attrs: &[ast::Attribute],
70     dir_path: &Path,
71     codemap: &codemap::CodeMap,
72 ) -> PathBuf {
73     if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
74         return path;
75     }
76
77     match parser::Parser::default_submod_path(id, dir_path, codemap).result {
78         Ok(parser::ModulePathSuccess { path, .. }) => path,
79         Err(_) => panic!("Couldn't find module {}", id),
80     }
81 }