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::*;
12 use rustc_driver::driver;
13 use rustc::session::{mod, config};
14 use rustc::session::search_paths::SearchPaths;
15 use rustc::middle::{privacy, ty};
17 use rustc_trans::back::link;
19 use syntax::{ast, ast_map, codemap, diagnostic};
21 use std::cell::RefCell;
22 use std::collections::{HashMap, HashSet};
24 use visit_ast::RustdocVisitor;
28 /// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
29 pub enum MaybeTyped<'tcx> {
30 Typed(ty::ctxt<'tcx>),
31 NotTyped(session::Session)
34 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
35 (Vec<String>, clean::TypeKind)>>>;
37 pub struct DocContext<'tcx> {
38 pub krate: &'tcx ast::Crate,
39 pub maybe_typed: MaybeTyped<'tcx>,
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>>,
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
56 pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
57 match self.maybe_typed {
58 Typed(ref tcx) => Some(tcx),
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")
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>>>,
78 pub type Externs = HashMap<String, Vec<String>>;
80 pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
81 cpath: &Path, triple: Option<String>)
82 -> (clean::Crate, CrateAnalysis) {
84 // Parse, resolve, and typecheck the given crate.
86 let input = config::Input::File(cpath.clone());
88 let warning_lint = lint::builtin::WARNINGS.name_lower();
90 let sessopts = config::Options {
92 search_paths: search_paths,
93 crate_types: vec!(config::CrateTypeRlib),
94 lint_opts: vec!((warning_lint, lint::Allow)),
96 target_triple: triple.unwrap_or(config::host_triple().to_string()),
97 cfg: config::parse_cfgspecs(cfgs),
98 ..config::basic_options().clone()
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);
107 let sess = session::build_session_(sessopts,
109 span_diagnostic_handler);
111 let cfg = config::build_configuration(&sess);
113 let krate = driver::phase_1_parse_input(&sess, cfg, &input);
115 let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
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!");
121 let mut forest = ast_map::Forest::new(krate);
122 let ast_map = driver::assign_node_ids_and_map(&sess, &mut forest);
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);
129 let ctxt = DocContext {
130 krate: ty_cx.map.krate(),
131 maybe_typed: Typed(ty_cx),
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()),
139 debug!("crate: {}", ctxt.krate);
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),
151 let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
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;