2014-08-31 12:07:27 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-12 20:10:51 -05: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 02:05:53 -06:00
|
|
|
pub use self::MaybeTyped::*;
|
2013-09-12 20:10:51 -05:00
|
|
|
|
2014-11-15 19:30:33 -06:00
|
|
|
use rustc_trans::driver::driver;
|
|
|
|
use rustc::session::{mod, config};
|
2014-06-26 13:37:39 -05:00
|
|
|
use rustc::middle::{privacy, ty};
|
2014-06-01 17:58:06 -05:00
|
|
|
use rustc::lint;
|
2014-11-15 19:30:33 -06:00
|
|
|
use rustc_trans::back::link;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
use syntax::{ast, ast_map, codemap, diagnostic};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-12-21 16:28:04 -06:00
|
|
|
use std::cell::RefCell;
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2014-09-06 11:13:40 -05:00
|
|
|
use arena::TypedArena;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
use visit_ast::RustdocVisitor;
|
|
|
|
use clean;
|
|
|
|
use clean::Clean;
|
|
|
|
|
2014-06-26 13:37:39 -05:00
|
|
|
/// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
|
2014-09-06 11:13:40 -05:00
|
|
|
pub enum MaybeTyped<'tcx> {
|
2014-05-18 08:56:13 -05:00
|
|
|
Typed(ty::ctxt<'tcx>),
|
|
|
|
NotTyped(session::Session)
|
2014-03-05 08:36:01 -06:00
|
|
|
}
|
|
|
|
|
2014-05-09 15:52:17 -05:00
|
|
|
pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
|
2014-05-22 18:57:53 -05:00
|
|
|
(Vec<String>, clean::TypeKind)>>>;
|
2014-05-09 15:52:17 -05:00
|
|
|
|
2014-09-06 11:13:40 -05:00
|
|
|
pub struct DocContext<'tcx> {
|
2014-05-18 08:56:13 -05:00
|
|
|
pub krate: &'tcx ast::Crate,
|
2014-09-06 11:13:40 -05:00
|
|
|
pub maybe_typed: MaybeTyped<'tcx>,
|
2014-05-02 18:15:12 -05:00
|
|
|
pub src: Path,
|
2014-05-09 15:52:17 -05:00
|
|
|
pub external_paths: ExternalPaths,
|
2014-05-03 04:08:58 -05:00
|
|
|
pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
|
2014-05-22 18:57:53 -05:00
|
|
|
pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
|
2014-05-26 23:51:59 -05:00
|
|
|
pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
|
2014-05-29 03:35:44 -05:00
|
|
|
pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
|
2014-03-05 08:36:01 -06:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:40 -05:00
|
|
|
impl<'tcx> DocContext<'tcx> {
|
2014-05-18 08:56:13 -05:00
|
|
|
pub fn sess<'a>(&'a self) -> &'a session::Session {
|
2014-03-05 08:36:01 -06:00
|
|
|
match self.maybe_typed {
|
|
|
|
Typed(ref tcx) => &tcx.sess,
|
|
|
|
NotTyped(ref sess) => sess
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 13:37:39 -05:00
|
|
|
|
2014-09-06 11:13:40 -05:00
|
|
|
pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
|
2014-06-26 13:37:39 -05:00
|
|
|
match self.maybe_typed {
|
|
|
|
Typed(ref tcx) => Some(tcx),
|
|
|
|
NotTyped(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:40 -05:00
|
|
|
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
|
2014-06-26 13:37:39 -05:00
|
|
|
let tcx_opt = self.tcx_opt();
|
|
|
|
tcx_opt.expect("tcx not present")
|
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
pub struct CrateAnalysis {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub exported_items: privacy::ExportedItems,
|
|
|
|
pub public_items: privacy::PublicItems,
|
2014-05-09 15:52:17 -05:00
|
|
|
pub external_paths: ExternalPaths,
|
2014-05-03 04:08:58 -05:00
|
|
|
pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
|
2014-05-22 18:57:53 -05:00
|
|
|
pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
|
2014-05-26 23:51:59 -05:00
|
|
|
pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
|
|
|
|
2014-07-20 01:02:14 -05:00
|
|
|
pub type Externs = HashMap<String, Vec<String>>;
|
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
pub fn run_core(libs: Vec<Path>, cfgs: Vec<String>, externs: Externs,
|
|
|
|
cpath: &Path, triple: Option<String>)
|
|
|
|
-> (clean::Crate, CrateAnalysis) {
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
// Parse, resolve, and typecheck the given crate.
|
|
|
|
|
2014-11-27 06:21:26 -06:00
|
|
|
let input = config::Input::File(cpath.clone());
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-06-13 15:13:05 -05:00
|
|
|
let warning_lint = lint::builtin::WARNINGS.name_lower();
|
2014-06-04 16:35:58 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
let sessopts = config::Options {
|
2014-11-18 17:56:12 -06:00
|
|
|
maybe_sysroot: None,
|
2014-03-09 07:24:58 -05:00
|
|
|
addl_lib_search_paths: RefCell::new(libs),
|
2014-05-18 08:56:13 -05:00
|
|
|
crate_types: vec!(config::CrateTypeRlib),
|
2014-06-04 16:35:58 -05:00
|
|
|
lint_opts: vec!((warning_lint, lint::Allow)),
|
2014-07-20 01:02:14 -05:00
|
|
|
externs: externs,
|
2014-11-15 19:30:33 -06:00
|
|
|
target_triple: triple.unwrap_or(config::host_triple().to_string()),
|
2014-10-06 21:39:01 -05:00
|
|
|
cfg: config::parse_cfgspecs(cfgs),
|
2014-05-18 08:56:13 -05:00
|
|
|
..config::basic_options().clone()
|
2013-08-15 15:28:54 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
let codemap = codemap::CodeMap::new();
|
|
|
|
let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None);
|
2013-08-15 15:28:54 -05:00
|
|
|
let span_diagnostic_handler =
|
2014-05-18 08:56:13 -05:00
|
|
|
diagnostic::mk_span_handler(diagnostic_handler, codemap);
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
let sess = session::build_session_(sessopts,
|
|
|
|
Some(cpath.clone()),
|
|
|
|
span_diagnostic_handler);
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-10-06 21:39:01 -05:00
|
|
|
let cfg = config::build_configuration(&sess);
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
|
2014-06-06 15:21:18 -05:00
|
|
|
|
|
|
|
let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
|
2014-07-05 14:37:43 -05:00
|
|
|
&input);
|
2014-06-06 15:21:18 -05:00
|
|
|
|
2014-05-18 08:56:13 -05: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 16:03:19 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
let type_arena = TypedArena::new();
|
2014-11-27 06:21:26 -06:00
|
|
|
let ty::CrateAnalysis {
|
2014-01-07 20:46:16 -06:00
|
|
|
exported_items, public_items, ty_cx, ..
|
2014-05-18 08:56:13 -05:00
|
|
|
} = driver::phase_3_run_analysis_passes(sess, ast_map, &type_arena, name);
|
2013-10-12 16:40:41 -05:00
|
|
|
|
2014-05-18 08:56:13 -05:00
|
|
|
let ctxt = DocContext {
|
|
|
|
krate: ty_cx.map.krate(),
|
2014-05-02 18:15:12 -05:00
|
|
|
maybe_typed: Typed(ty_cx),
|
|
|
|
src: cpath.clone(),
|
2014-05-03 04:08:58 -05:00
|
|
|
external_traits: RefCell::new(Some(HashMap::new())),
|
|
|
|
external_typarams: RefCell::new(Some(HashMap::new())),
|
2014-05-09 15:52:17 -05:00
|
|
|
external_paths: RefCell::new(Some(HashMap::new())),
|
2014-05-26 23:51:59 -05:00
|
|
|
inlined: RefCell::new(Some(HashSet::new())),
|
2014-05-29 03:35:44 -05:00
|
|
|
populated_crate_impls: RefCell::new(HashSet::new()),
|
2014-05-18 08:56:13 -05:00
|
|
|
};
|
2014-10-15 02:20:39 -05:00
|
|
|
debug!("crate: {}", ctxt.krate);
|
2014-05-18 08:56:13 -05:00
|
|
|
|
|
|
|
let analysis = CrateAnalysis {
|
2014-03-05 08:36:01 -06:00
|
|
|
exported_items: exported_items,
|
|
|
|
public_items: public_items,
|
2014-05-09 15:52:17 -05:00
|
|
|
external_paths: RefCell::new(None),
|
2014-05-03 04:08:58 -05:00
|
|
|
external_traits: RefCell::new(None),
|
|
|
|
external_typarams: RefCell::new(None),
|
2014-05-26 23:51:59 -05:00
|
|
|
inlined: RefCell::new(None),
|
2014-05-18 08:56:13 -05:00
|
|
|
};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
let krate = {
|
2014-09-06 11:13:40 -05:00
|
|
|
let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
|
2014-05-18 08:56:13 -05:00
|
|
|
v.visit(ctxt.krate);
|
2014-09-06 11:13:40 -05:00
|
|
|
v.clean(&ctxt)
|
2014-01-07 20:46:16 -06:00
|
|
|
};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-09 15:52:17 -05:00
|
|
|
let external_paths = ctxt.external_paths.borrow_mut().take();
|
|
|
|
*analysis.external_paths.borrow_mut() = external_paths;
|
2014-05-03 04:08:58 -05: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 23:51:59 -05:00
|
|
|
let map = ctxt.inlined.borrow_mut().take();
|
|
|
|
*analysis.inlined.borrow_mut() = map;
|
2014-02-05 15:15:24 -06:00
|
|
|
(krate, analysis)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|