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