2014-08-31 19:07:27 +02:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-12 21:10:51 -04:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::MaybeTyped::*;
|
2013-09-12 21:10:51 -04:00
|
|
|
|
2014-11-27 09:57:47 -05:00
|
|
|
use rustc_driver::driver;
|
2014-11-15 20:30:33 -05:00
|
|
|
use rustc::session::{mod, config};
|
2014-12-16 14:32:02 -08:00
|
|
|
use rustc::session::search_paths::SearchPaths;
|
2014-06-26 11:37:39 -07:00
|
|
|
use rustc::middle::{privacy, ty};
|
2014-06-01 15:58:06 -07:00
|
|
|
use rustc::lint;
|
2014-11-15 20:30:33 -05:00
|
|
|
use rustc_trans::back::link;
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
use syntax::{ast, ast_map, codemap, diagnostic};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2013-12-21 14:28:04 -08:00
|
|
|
use std::cell::RefCell;
|
2014-05-29 19:03:06 -07:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
|
|
|
use visit_ast::RustdocVisitor;
|
|
|
|
use clean;
|
|
|
|
use clean::Clean;
|
|
|
|
|
2014-06-26 11:37:39 -07:00
|
|
|
/// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
|
2014-09-06 19:13:40 +03:00
|
|
|
pub enum MaybeTyped<'tcx> {
|
2014-05-18 16:56:13 +03:00
|
|
|
Typed(ty::ctxt<'tcx>),
|
|
|
|
NotTyped(session::Session)
|
2014-03-05 16:36:01 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 13:52:17 -07:00
|
|
|
pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
|
2014-05-22 16:57:53 -07:00
|
|
|
(Vec<String>, clean::TypeKind)>>>;
|
2014-05-09 13:52:17 -07:00
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
pub struct DocContext<'tcx> {
|
2014-05-18 16:56:13 +03:00
|
|
|
pub krate: &'tcx ast::Crate,
|
2014-09-06 19:13:40 +03:00
|
|
|
pub maybe_typed: MaybeTyped<'tcx>,
|
2014-05-02 16:15:12 -07:00
|
|
|
pub src: Path,
|
2014-05-09 13:52:17 -07:00
|
|
|
pub external_paths: ExternalPaths,
|
2014-05-03 02:08:58 -07:00
|
|
|
pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
|
2014-05-22 16:57:53 -07:00
|
|
|
pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
|
2014-05-26 21:51:59 -07:00
|
|
|
pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
|
2014-05-29 01:35:44 -07:00
|
|
|
pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
|
2014-03-05 16:36:01 +02:00
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
impl<'tcx> DocContext<'tcx> {
|
2014-05-18 16:56:13 +03:00
|
|
|
pub fn sess<'a>(&'a self) -> &'a session::Session {
|
2014-03-05 16:36:01 +02:00
|
|
|
match self.maybe_typed {
|
|
|
|
Typed(ref tcx) => &tcx.sess,
|
|
|
|
NotTyped(ref sess) => sess
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 11:37:39 -07:00
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
|
2014-06-26 11:37:39 -07:00
|
|
|
match self.maybe_typed {
|
|
|
|
Typed(ref tcx) => Some(tcx),
|
|
|
|
NotTyped(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
|
2014-06-26 11:37:39 -07:00
|
|
|
let tcx_opt = self.tcx_opt();
|
|
|
|
tcx_opt.expect("tcx not present")
|
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
2013-10-12 14:40:41 -07:00
|
|
|
pub struct CrateAnalysis {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub exported_items: privacy::ExportedItems,
|
|
|
|
pub public_items: privacy::PublicItems,
|
2014-05-09 13:52:17 -07:00
|
|
|
pub external_paths: ExternalPaths,
|
2014-05-03 02:08:58 -07:00
|
|
|
pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
|
2014-05-22 16:57:53 -07:00
|
|
|
pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
|
2014-05-26 21:51:59 -07:00
|
|
|
pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
|
2013-10-12 14:40:41 -07:00
|
|
|
}
|
|
|
|
|
2014-07-19 23:02:14 -07:00
|
|
|
pub type Externs = HashMap<String, Vec<String>>;
|
|
|
|
|
2014-12-16 14:32:02 -08:00
|
|
|
pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
|
2014-05-18 16:56:13 +03:00
|
|
|
cpath: &Path, triple: Option<String>)
|
|
|
|
-> (clean::Crate, CrateAnalysis) {
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
// Parse, resolve, and typecheck the given crate.
|
|
|
|
|
2014-11-27 07:21:26 -05:00
|
|
|
let input = config::Input::File(cpath.clone());
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-06-13 13:13:05 -07:00
|
|
|
let warning_lint = lint::builtin::WARNINGS.name_lower();
|
2014-06-04 14:35:58 -07:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
let sessopts = config::Options {
|
2014-11-18 18:56:12 -05:00
|
|
|
maybe_sysroot: None,
|
2014-12-16 14:32:02 -08:00
|
|
|
search_paths: search_paths,
|
2014-05-18 16:56:13 +03:00
|
|
|
crate_types: vec!(config::CrateTypeRlib),
|
2014-06-04 14:35:58 -07:00
|
|
|
lint_opts: vec!((warning_lint, lint::Allow)),
|
2014-07-19 23:02:14 -07:00
|
|
|
externs: externs,
|
2014-11-15 20:30:33 -05:00
|
|
|
target_triple: triple.unwrap_or(config::host_triple().to_string()),
|
2014-10-06 19:39:01 -07:00
|
|
|
cfg: config::parse_cfgspecs(cfgs),
|
2014-05-18 16:56:13 +03:00
|
|
|
..config::basic_options().clone()
|
2013-08-15 16:28:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
let codemap = codemap::CodeMap::new();
|
|
|
|
let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None);
|
2013-08-15 16:28:54 -04:00
|
|
|
let span_diagnostic_handler =
|
2014-05-18 16:56:13 +03:00
|
|
|
diagnostic::mk_span_handler(diagnostic_handler, codemap);
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
let sess = session::build_session_(sessopts,
|
|
|
|
Some(cpath.clone()),
|
|
|
|
span_diagnostic_handler);
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-10-06 19:39:01 -07:00
|
|
|
let cfg = config::build_configuration(&sess);
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
|
2014-06-06 13:21:18 -07:00
|
|
|
|
|
|
|
let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
|
2014-07-05 12:37:43 -07:00
|
|
|
&input);
|
2014-06-06 13:21:18 -07:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
let krate = driver::phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
|
|
|
|
.expect("phase_2_configure_and_expand aborted in rustdoc!");
|
|
|
|
|
|
|
|
let mut forest = ast_map::Forest::new(krate);
|
|
|
|
let ast_map = driver::assign_node_ids_and_map(&sess, &mut forest);
|
2014-06-10 14:03:19 -07:00
|
|
|
|
2014-12-04 16:25:29 -08:00
|
|
|
let arenas = ty::CtxtArenas::new();
|
2014-11-27 07:21:26 -05:00
|
|
|
let ty::CrateAnalysis {
|
2014-01-07 18:46:16 -08:00
|
|
|
exported_items, public_items, ty_cx, ..
|
2014-12-04 16:25:29 -08:00
|
|
|
} = driver::phase_3_run_analysis_passes(sess, ast_map, &arenas, name);
|
2013-10-12 14:40:41 -07:00
|
|
|
|
2014-05-18 16:56:13 +03:00
|
|
|
let ctxt = DocContext {
|
|
|
|
krate: ty_cx.map.krate(),
|
2014-05-02 16:15:12 -07:00
|
|
|
maybe_typed: Typed(ty_cx),
|
|
|
|
src: cpath.clone(),
|
2014-05-03 02:08:58 -07:00
|
|
|
external_traits: RefCell::new(Some(HashMap::new())),
|
|
|
|
external_typarams: RefCell::new(Some(HashMap::new())),
|
2014-05-09 13:52:17 -07:00
|
|
|
external_paths: RefCell::new(Some(HashMap::new())),
|
2014-05-26 21:51:59 -07:00
|
|
|
inlined: RefCell::new(Some(HashSet::new())),
|
2014-05-29 01:35:44 -07:00
|
|
|
populated_crate_impls: RefCell::new(HashSet::new()),
|
2014-05-18 16:56:13 +03:00
|
|
|
};
|
2014-10-15 03:20:39 -04:00
|
|
|
debug!("crate: {}", ctxt.krate);
|
2014-05-18 16:56:13 +03:00
|
|
|
|
|
|
|
let analysis = CrateAnalysis {
|
2014-03-05 16:36:01 +02:00
|
|
|
exported_items: exported_items,
|
|
|
|
public_items: public_items,
|
2014-05-09 13:52:17 -07:00
|
|
|
external_paths: RefCell::new(None),
|
2014-05-03 02:08:58 -07:00
|
|
|
external_traits: RefCell::new(None),
|
|
|
|
external_typarams: RefCell::new(None),
|
2014-05-26 21:51:59 -07:00
|
|
|
inlined: RefCell::new(None),
|
2014-05-18 16:56:13 +03:00
|
|
|
};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-02-05 22:15:24 +01:00
|
|
|
let krate = {
|
2014-09-06 19:13:40 +03:00
|
|
|
let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
|
2014-05-18 16:56:13 +03:00
|
|
|
v.visit(ctxt.krate);
|
2014-09-06 19:13:40 +03:00
|
|
|
v.clean(&ctxt)
|
2014-01-07 18:46:16 -08:00
|
|
|
};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2014-05-09 13:52:17 -07:00
|
|
|
let external_paths = ctxt.external_paths.borrow_mut().take();
|
|
|
|
*analysis.external_paths.borrow_mut() = external_paths;
|
2014-05-03 02:08:58 -07:00
|
|
|
let map = ctxt.external_traits.borrow_mut().take();
|
|
|
|
*analysis.external_traits.borrow_mut() = map;
|
|
|
|
let map = ctxt.external_typarams.borrow_mut().take();
|
|
|
|
*analysis.external_typarams.borrow_mut() = map;
|
2014-05-26 21:51:59 -07:00
|
|
|
let map = ctxt.inlined.borrow_mut().take();
|
|
|
|
*analysis.inlined.borrow_mut() = map;
|
2014-02-05 22:15:24 +01:00
|
|
|
(krate, analysis)
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|