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.
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};
25 use arena::TypedArena;
27 use visit_ast::RustdocVisitor;
31 /// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
32 pub enum MaybeTyped<'tcx> {
33 Typed(middle::ty::ctxt<'tcx>),
34 NotTyped(driver::session::Session)
37 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
38 (Vec<String>, clean::TypeKind)>>>;
40 pub struct DocContext<'tcx> {
41 pub krate: ast::Crate,
42 pub maybe_typed: MaybeTyped<'tcx>,
44 pub external_paths: ExternalPaths,
45 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
46 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
47 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
48 pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
51 impl<'tcx> DocContext<'tcx> {
52 pub fn sess<'a>(&'a self) -> &'a driver::session::Session {
53 match self.maybe_typed {
54 Typed(ref tcx) => &tcx.sess,
55 NotTyped(ref sess) => sess
59 pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
60 match self.maybe_typed {
61 Typed(ref tcx) => Some(tcx),
66 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
67 let tcx_opt = self.tcx_opt();
68 tcx_opt.expect("tcx not present")
72 pub struct CrateAnalysis {
73 pub exported_items: privacy::ExportedItems,
74 pub public_items: privacy::PublicItems,
75 pub external_paths: ExternalPaths,
76 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
77 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
78 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
81 pub type Externs = HashMap<String, Vec<String>>;
83 /// Parses, resolves, and typechecks the given crate
84 fn get_ast_and_resolve<'tcx>(cpath: &Path, libs: Vec<Path>, cfgs: Vec<String>,
85 externs: Externs, triple: Option<String>,
86 type_arena: &'tcx TypedArena<ty::t_box_>)
87 -> (DocContext<'tcx>, CrateAnalysis) {
88 use syntax::codemap::dummy_spanned;
89 use rustc::driver::driver::{FileInput,
91 phase_2_configure_and_expand,
92 phase_3_run_analysis_passes};
93 use rustc::driver::config::build_configuration;
95 let input = FileInput(cpath.clone());
97 let warning_lint = lint::builtin::WARNINGS.name_lower();
99 let sessopts = driver::config::Options {
100 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
101 addl_lib_search_paths: RefCell::new(libs),
102 crate_types: vec!(driver::config::CrateTypeRlib),
103 lint_opts: vec!((warning_lint, lint::Allow)),
105 target_triple: triple.unwrap_or(driver::driver::host_triple().to_string()),
106 ..rustc::driver::config::basic_options().clone()
110 let codemap = syntax::codemap::CodeMap::new();
111 let diagnostic_handler = syntax::diagnostic::default_handler(syntax::diagnostic::Auto, None);
112 let span_diagnostic_handler =
113 syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
115 let sess = driver::session::build_session_(sessopts,
117 span_diagnostic_handler);
119 let mut cfg = build_configuration(&sess);
120 for cfg_ in cfgs.move_iter() {
121 let cfg_ = token::intern_and_get_ident(cfg_.as_slice());
122 cfg.push(box(GC) dummy_spanned(ast::MetaWord(cfg_)));
125 let krate = phase_1_parse_input(&sess, cfg, &input);
127 let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
131 = phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
132 .expect("phase_2_configure_and_expand aborted in rustdoc!");
134 let driver::driver::CrateAnalysis {
135 exported_items, public_items, ty_cx, ..
136 } = phase_3_run_analysis_passes(sess, &krate, ast_map, type_arena, name);
138 debug!("crate: {:?}", krate);
141 maybe_typed: Typed(ty_cx),
143 external_traits: RefCell::new(Some(HashMap::new())),
144 external_typarams: RefCell::new(Some(HashMap::new())),
145 external_paths: RefCell::new(Some(HashMap::new())),
146 inlined: RefCell::new(Some(HashSet::new())),
147 populated_crate_impls: RefCell::new(HashSet::new()),
149 exported_items: exported_items,
150 public_items: public_items,
151 external_paths: RefCell::new(None),
152 external_traits: RefCell::new(None),
153 external_typarams: RefCell::new(None),
154 inlined: RefCell::new(None),
158 pub fn run_core(libs: Vec<Path>, cfgs: Vec<String>, externs: Externs,
159 path: &Path, triple: Option<String>)
160 -> (clean::Crate, CrateAnalysis) {
161 let type_arena = TypedArena::new();
162 let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs, externs,
163 triple, &type_arena);
166 let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
167 v.visit(&ctxt.krate);
171 let external_paths = ctxt.external_paths.borrow_mut().take();
172 *analysis.external_paths.borrow_mut() = external_paths;
173 let map = ctxt.external_traits.borrow_mut().take();
174 *analysis.external_traits.borrow_mut() = map;
175 let map = ctxt.external_typarams.borrow_mut().take();
176 *analysis.external_typarams.borrow_mut() = map;
177 let map = ctxt.inlined.borrow_mut().take();
178 *analysis.inlined.borrow_mut() = map;