]> git.lizzy.rs Git - rust.git/blob - src/modules.rs
2f40fd33a55821314039e3dc398782ef6f03506a
[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::HashMap;
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>(krate: &'a ast::Crate,
24                       codemap: &codemap::CodeMap)
25                       -> HashMap<PathBuf, &'a ast::Mod> {
26     let mut result = HashMap::new();
27     let root_filename: PathBuf = codemap.span_to_filename(krate.span).into();
28     list_submodules(&krate.module, root_filename.parent().unwrap(), codemap, &mut result);
29     result.insert(root_filename, &krate.module);
30     result
31 }
32
33 /// Recursively list all external modules included in a module.
34 fn list_submodules<'a>(module: &'a ast::Mod,
35                        search_dir: &Path,
36                        codemap: &codemap::CodeMap,
37                        result: &mut HashMap<PathBuf, &'a ast::Mod>) {
38     debug!("list_submodules: search_dir: {:?}", search_dir);
39     for item in module.items.iter() {
40         if let ast::ItemMod(ref sub_mod) = item.node {
41             if !utils::contains_skip(&item.attrs) {
42                 let is_internal = codemap.span_to_filename(item.span) ==
43                                   codemap.span_to_filename(sub_mod.inner);
44                 let dir_path = if is_internal {
45                     search_dir.join(&item.ident.to_string())
46                 } else {
47                     let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap);
48                     let dir_path = mod_path.parent().unwrap().to_owned();
49                     result.insert(mod_path, sub_mod);
50                     dir_path
51                 };
52                 list_submodules(sub_mod, &dir_path, codemap, result);
53             }
54         }
55     }
56 }
57
58 /// Find the file corresponding to an external mod
59 fn module_file(id: ast::Ident,
60                attrs: &[ast::Attribute],
61                dir_path: &Path,
62                codemap: &codemap::CodeMap)
63                -> PathBuf {
64     if let Some(path) = parser::Parser::submod_path_from_attr(attrs, &dir_path) {
65         return path;
66     }
67
68     match parser::Parser::default_submod_path(id, &dir_path, codemap).result {
69         Ok(parser::ModulePathSuccess { path, .. }) => path,
70         Err(_) => panic!("Couldn't find module {}", id)
71     }
72 }