]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
ef8367dfc76132ebdba13a8ff4f654e0cc4fafc0
[rust.git] / src / librustdoc / core.rs
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.
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 rustc;
12 use rustc::{driver, middle};
13 use rustc::middle::privacy;
14 use rustc::lint;
15
16 use syntax::ast;
17 use syntax::parse::token;
18 use syntax;
19
20 use std::cell::RefCell;
21 use std::gc::GC;
22 use std::os;
23 use std::collections::{HashMap, HashSet};
24
25 use visit_ast::RustdocVisitor;
26 use clean;
27 use clean::Clean;
28
29 pub enum MaybeTyped {
30     Typed(middle::ty::ctxt),
31     NotTyped(driver::session::Session)
32 }
33
34 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
35                                                 (Vec<String>, clean::TypeKind)>>>;
36
37 pub struct DocContext {
38     pub krate: ast::Crate,
39     pub maybe_typed: MaybeTyped,
40     pub src: Path,
41     pub external_paths: ExternalPaths,
42     pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
43     pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
44     pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
45     pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
46 }
47
48 impl DocContext {
49     pub fn sess<'a>(&'a self) -> &'a driver::session::Session {
50         match self.maybe_typed {
51             Typed(ref tcx) => &tcx.sess,
52             NotTyped(ref sess) => sess
53         }
54     }
55 }
56
57 pub struct CrateAnalysis {
58     pub exported_items: privacy::ExportedItems,
59     pub public_items: privacy::PublicItems,
60     pub external_paths: ExternalPaths,
61     pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
62     pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
63     pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
64 }
65
66 /// Parses, resolves, and typechecks the given crate
67 fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)
68                        -> (DocContext, CrateAnalysis) {
69     use syntax::codemap::dummy_spanned;
70     use rustc::driver::driver::{FileInput,
71                                 phase_1_parse_input,
72                                 phase_2_configure_and_expand,
73                                 phase_3_run_analysis_passes};
74     use rustc::driver::config::build_configuration;
75
76     let input = FileInput(cpath.clone());
77
78     let warning_lint = lint::builtin::warnings.name.to_string();
79
80     let sessopts = driver::config::Options {
81         maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
82         addl_lib_search_paths: RefCell::new(libs),
83         crate_types: vec!(driver::config::CrateTypeRlib),
84         lint_opts: vec!((warning_lint, lint::Allow)),
85         ..rustc::driver::config::basic_options().clone()
86     };
87
88
89     let codemap = syntax::codemap::CodeMap::new();
90     let diagnostic_handler = syntax::diagnostic::default_handler(syntax::diagnostic::Auto);
91     let span_diagnostic_handler =
92         syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
93
94     let sess = driver::session::build_session_(sessopts,
95                                                Some(cpath.clone()),
96                                                span_diagnostic_handler);
97
98     let mut cfg = build_configuration(&sess);
99     for cfg_ in cfgs.move_iter() {
100         let cfg_ = token::intern_and_get_ident(cfg_.as_slice());
101         cfg.push(box(GC) dummy_spanned(ast::MetaWord(cfg_)));
102     }
103
104     let krate = phase_1_parse_input(&sess, cfg, &input);
105     let (krate, ast_map) = phase_2_configure_and_expand(&sess, krate,
106                                                         &from_str("rustdoc").unwrap());
107     let driver::driver::CrateAnalysis {
108         exported_items, public_items, ty_cx, ..
109     } = phase_3_run_analysis_passes(sess, &krate, ast_map);
110
111     debug!("crate: {:?}", krate);
112     (DocContext {
113         krate: krate,
114         maybe_typed: Typed(ty_cx),
115         src: cpath.clone(),
116         external_traits: RefCell::new(Some(HashMap::new())),
117         external_typarams: RefCell::new(Some(HashMap::new())),
118         external_paths: RefCell::new(Some(HashMap::new())),
119         inlined: RefCell::new(Some(HashSet::new())),
120         populated_crate_impls: RefCell::new(HashSet::new()),
121     }, CrateAnalysis {
122         exported_items: exported_items,
123         public_items: public_items,
124         external_paths: RefCell::new(None),
125         external_traits: RefCell::new(None),
126         external_typarams: RefCell::new(None),
127         inlined: RefCell::new(None),
128     })
129 }
130
131 pub fn run_core(libs: HashSet<Path>, cfgs: Vec<String>, path: &Path)
132                 -> (clean::Crate, CrateAnalysis) {
133     let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
134     let ctxt = box(GC) ctxt;
135     super::ctxtkey.replace(Some(ctxt));
136
137     let krate = {
138         let mut v = RustdocVisitor::new(&*ctxt, Some(&analysis));
139         v.visit(&ctxt.krate);
140         v.clean()
141     };
142
143     let external_paths = ctxt.external_paths.borrow_mut().take();
144     *analysis.external_paths.borrow_mut() = external_paths;
145     let map = ctxt.external_traits.borrow_mut().take();
146     *analysis.external_traits.borrow_mut() = map;
147     let map = ctxt.external_typarams.borrow_mut().take();
148     *analysis.external_typarams.borrow_mut() = map;
149     let map = ctxt.inlined.borrow_mut().take();
150     *analysis.inlined.borrow_mut() = map;
151     (krate, analysis)
152 }