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.
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.
12 use rustc::{driver, middle};
13 use rustc::middle::{privacy, ty};
15 use rustc::back::link;
18 use syntax::parse::token;
21 use std::cell::RefCell;
24 use std::collections::{HashMap, HashSet};
26 use visit_ast::RustdocVisitor;
30 /// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
32 Typed(middle::ty::ctxt),
33 NotTyped(driver::session::Session)
36 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
37 (Vec<String>, clean::TypeKind)>>>;
39 pub struct DocContext {
40 pub krate: ast::Crate,
41 pub maybe_typed: MaybeTyped,
43 pub external_paths: ExternalPaths,
44 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
45 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
46 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
47 pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
51 pub fn sess<'a>(&'a self) -> &'a driver::session::Session {
52 match self.maybe_typed {
53 Typed(ref tcx) => &tcx.sess,
54 NotTyped(ref sess) => sess
58 pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt> {
59 match self.maybe_typed {
60 Typed(ref tcx) => Some(tcx),
65 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt {
66 let tcx_opt = self.tcx_opt();
67 tcx_opt.expect("tcx not present")
71 pub struct CrateAnalysis {
72 pub exported_items: privacy::ExportedItems,
73 pub public_items: privacy::PublicItems,
74 pub external_paths: ExternalPaths,
75 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
76 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
77 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
80 pub type Externs = HashMap<String, Vec<String>>;
82 /// Parses, resolves, and typechecks the given crate
83 fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>,
84 externs: Externs, triple: Option<String>)
85 -> (DocContext, CrateAnalysis) {
86 use syntax::codemap::dummy_spanned;
87 use rustc::driver::driver::{FileInput,
89 phase_2_configure_and_expand,
90 phase_3_run_analysis_passes};
91 use rustc::driver::config::build_configuration;
93 let input = FileInput(cpath.clone());
95 let warning_lint = lint::builtin::WARNINGS.name_lower();
97 let sessopts = driver::config::Options {
98 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
99 addl_lib_search_paths: RefCell::new(libs),
100 crate_types: vec!(driver::config::CrateTypeRlib),
101 lint_opts: vec!((warning_lint, lint::Allow)),
103 target_triple: triple.unwrap_or(driver::driver::host_triple().to_string()),
104 ..rustc::driver::config::basic_options().clone()
108 let codemap = syntax::codemap::CodeMap::new();
109 let diagnostic_handler = syntax::diagnostic::default_handler(syntax::diagnostic::Auto, None);
110 let span_diagnostic_handler =
111 syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
113 let sess = driver::session::build_session_(sessopts,
115 span_diagnostic_handler);
117 let mut cfg = build_configuration(&sess);
118 for cfg_ in cfgs.move_iter() {
119 let cfg_ = token::intern_and_get_ident(cfg_.as_slice());
120 cfg.push(box(GC) dummy_spanned(ast::MetaWord(cfg_)));
123 let krate = phase_1_parse_input(&sess, cfg, &input);
125 let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
129 = phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
130 .expect("phase_2_configure_and_expand aborted in rustdoc!");
132 let driver::driver::CrateAnalysis {
133 exported_items, public_items, ty_cx, ..
134 } = phase_3_run_analysis_passes(sess, &krate, ast_map, name);
136 debug!("crate: {:?}", krate);
139 maybe_typed: Typed(ty_cx),
141 external_traits: RefCell::new(Some(HashMap::new())),
142 external_typarams: RefCell::new(Some(HashMap::new())),
143 external_paths: RefCell::new(Some(HashMap::new())),
144 inlined: RefCell::new(Some(HashSet::new())),
145 populated_crate_impls: RefCell::new(HashSet::new()),
147 exported_items: exported_items,
148 public_items: public_items,
149 external_paths: RefCell::new(None),
150 external_traits: RefCell::new(None),
151 external_typarams: RefCell::new(None),
152 inlined: RefCell::new(None),
156 pub fn run_core(libs: HashSet<Path>, cfgs: Vec<String>, externs: Externs,
157 path: &Path, triple: Option<String>)
158 -> (clean::Crate, CrateAnalysis) {
159 let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs, externs, triple);
160 let ctxt = box(GC) ctxt;
161 super::ctxtkey.replace(Some(ctxt));
164 let mut v = RustdocVisitor::new(&*ctxt, Some(&analysis));
165 v.visit(&ctxt.krate);
169 let external_paths = ctxt.external_paths.borrow_mut().take();
170 *analysis.external_paths.borrow_mut() = external_paths;
171 let map = ctxt.external_traits.borrow_mut().take();
172 *analysis.external_traits.borrow_mut() = map;
173 let map = ctxt.external_typarams.borrow_mut().take();
174 *analysis.external_typarams.borrow_mut() = map;
175 let map = ctxt.inlined.borrow_mut().take();
176 *analysis.inlined.borrow_mut() = map;