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.
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::*;
13 use rustc_driver::driver;
14 use rustc::session::{self, config};
15 use rustc::middle::{privacy, ty};
18 use rustc_trans::back::link;
19 use rustc_resolve as resolve;
21 use syntax::{ast, codemap, diagnostic};
22 use syntax::feature_gate::UnstableFeatures;
24 use std::cell::{RefCell, Cell};
25 use std::collections::{HashMap, HashSet};
27 use visit_ast::RustdocVisitor;
31 pub use rustc::session::config::Input;
32 pub use rustc::session::search_paths::SearchPaths;
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)
40 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
41 (Vec<String>, clean::TypeKind)>>>;
43 pub struct DocContext<'a, 'tcx: 'a> {
44 pub krate: &'tcx ast::Crate,
45 pub maybe_typed: MaybeTyped<'a, 'tcx>,
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>>,
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
63 pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
64 match self.maybe_typed {
65 Typed(tcx) => Some(tcx),
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")
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>,
85 pub type Externs = HashMap<String, Vec<String>>;
87 pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
88 input: Input, triple: Option<String>)
89 -> (clean::Crate, CrateAnalysis) {
91 // Parse, resolve, and typecheck the given crate.
93 let cpath = match input {
94 Input::File(ref p) => Some(p.clone()),
98 let warning_lint = lint::builtin::WARNINGS.name_lower();
100 let sessopts = config::Options {
102 search_paths: search_paths,
103 crate_types: vec!(config::CrateTypeRlib),
104 lint_opts: vec!((warning_lint, lint::Allow)),
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()
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);
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));
122 let cfg = config::build_configuration(&sess);
124 let krate = driver::phase_1_parse_input(&sess, cfg, &input);
126 let name = link::find_crate_name(Some(&sess), &krate.attrs,
129 let krate = driver::phase_2_configure_and_expand(&sess, krate, &name, None)
130 .expect("phase_2_configure_and_expand aborted in rustdoc!");
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);
136 driver::phase_3_run_analysis_passes(sess,
140 resolve::MakeGlobMap::No,
142 let ty::CrateAnalysis { exported_items, public_items, .. } = analysis;
144 let ctxt = DocContext {
145 krate: tcx.map.krate(),
146 maybe_typed: Typed(tcx),
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),
155 debug!("crate: {:?}", ctxt.krate);
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,
167 let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
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();