]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / librustdoc / core.rs
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.
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 pub use self::MaybeTyped::*;
11
12 use rustc_driver::driver;
13 use rustc::session::{self, config};
14 use rustc::session::search_paths::SearchPaths;
15 use rustc::middle::{privacy, ty};
16 use rustc::lint;
17 use rustc_trans::back::link;
18
19 use syntax::{ast, ast_map, codemap, diagnostic};
20
21 use std::cell::RefCell;
22 use std::collections::{HashMap, HashSet};
23
24 use visit_ast::RustdocVisitor;
25 use clean;
26 use clean::Clean;
27
28 /// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
29 pub enum MaybeTyped<'tcx> {
30     Typed(ty::ctxt<'tcx>),
31     NotTyped(session::Session)
32 }
33
34 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
35                                                 (Vec<String>, clean::TypeKind)>>>;
36
37 pub struct DocContext<'tcx> {
38     pub krate: &'tcx ast::Crate,
39     pub maybe_typed: MaybeTyped<'tcx>,
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<'tcx> DocContext<'tcx> {
49     pub fn sess<'a>(&'a self) -> &'a session::Session {
50         match self.maybe_typed {
51             Typed(ref tcx) => &tcx.sess,
52             NotTyped(ref sess) => sess
53         }
54     }
55
56     pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
57         match self.maybe_typed {
58             Typed(ref tcx) => Some(tcx),
59             NotTyped(_) => None
60         }
61     }
62
63     pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
64         let tcx_opt = self.tcx_opt();
65         tcx_opt.expect("tcx not present")
66     }
67 }
68
69 pub struct CrateAnalysis {
70     pub exported_items: privacy::ExportedItems,
71     pub public_items: privacy::PublicItems,
72     pub external_paths: ExternalPaths,
73     pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
74     pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
75     pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
76 }
77
78 pub type Externs = HashMap<String, Vec<String>>;
79
80 pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
81                 cpath: &Path, triple: Option<String>)
82                 -> (clean::Crate, CrateAnalysis) {
83
84     // Parse, resolve, and typecheck the given crate.
85
86     let input = config::Input::File(cpath.clone());
87
88     let warning_lint = lint::builtin::WARNINGS.name_lower();
89
90     let sessopts = config::Options {
91         maybe_sysroot: None,
92         search_paths: search_paths,
93         crate_types: vec!(config::CrateTypeRlib),
94         lint_opts: vec!((warning_lint, lint::Allow)),
95         externs: externs,
96         target_triple: triple.unwrap_or(config::host_triple().to_string()),
97         cfg: config::parse_cfgspecs(cfgs),
98         ..config::basic_options().clone()
99     };
100
101
102     let codemap = codemap::CodeMap::new();
103     let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None);
104     let span_diagnostic_handler =
105         diagnostic::mk_span_handler(diagnostic_handler, codemap);
106
107     let sess = session::build_session_(sessopts,
108                                        Some(cpath.clone()),
109                                        span_diagnostic_handler);
110
111     let cfg = config::build_configuration(&sess);
112
113     let krate = driver::phase_1_parse_input(&sess, cfg, &input);
114
115     let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
116                                      &input);
117
118     let krate = driver::phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
119                     .expect("phase_2_configure_and_expand aborted in rustdoc!");
120
121     let mut forest = ast_map::Forest::new(krate);
122     let ast_map = driver::assign_node_ids_and_map(&sess, &mut forest);
123
124     let arenas = ty::CtxtArenas::new();
125     let ty::CrateAnalysis {
126         exported_items, public_items, ty_cx, ..
127     } = driver::phase_3_run_analysis_passes(sess, ast_map, &arenas, name);
128
129     let ctxt = DocContext {
130         krate: ty_cx.map.krate(),
131         maybe_typed: Typed(ty_cx),
132         src: cpath.clone(),
133         external_traits: RefCell::new(Some(HashMap::new())),
134         external_typarams: RefCell::new(Some(HashMap::new())),
135         external_paths: RefCell::new(Some(HashMap::new())),
136         inlined: RefCell::new(Some(HashSet::new())),
137         populated_crate_impls: RefCell::new(HashSet::new()),
138     };
139     debug!("crate: {}", ctxt.krate);
140
141     let analysis = CrateAnalysis {
142         exported_items: exported_items,
143         public_items: public_items,
144         external_paths: RefCell::new(None),
145         external_traits: RefCell::new(None),
146         external_typarams: RefCell::new(None),
147         inlined: RefCell::new(None),
148     };
149
150     let krate = {
151         let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
152         v.visit(ctxt.krate);
153         v.clean(&ctxt)
154     };
155
156     let external_paths = ctxt.external_paths.borrow_mut().take();
157     *analysis.external_paths.borrow_mut() = external_paths;
158     let map = ctxt.external_traits.borrow_mut().take();
159     *analysis.external_traits.borrow_mut() = map;
160     let map = ctxt.external_typarams.borrow_mut().take();
161     *analysis.external_typarams.borrow_mut() = map;
162     let map = ctxt.inlined.borrow_mut().take();
163     *analysis.inlined.borrow_mut() = map;
164     (krate, analysis)
165 }