]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/core.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustdoc / core.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10pub use self::MaybeTyped::*;
11
c34b1796 12use rustc_lint;
7453a54e
SL
13use rustc_driver::{driver, target_features, abort_on_err};
14use rustc::dep_graph::DepGraph;
1a4d82fc 15use rustc::session::{self, config};
54a0048b 16use rustc::hir::def_id::DefId;
92a42be0 17use rustc::middle::privacy::AccessLevels;
54a0048b
SL
18use rustc::ty::{self, TyCtxt};
19use rustc::hir::map as hir_map;
1a4d82fc
JJ
20use rustc::lint;
21use rustc_trans::back::link;
85aaf69f 22use rustc_resolve as resolve;
54a0048b 23use rustc::hir::lowering::{lower_crate, LoweringContext};
92a42be0 24use rustc_metadata::cstore::CStore;
1a4d82fc 25
9cc50fc6
SL
26use syntax::{ast, codemap, errors};
27use syntax::errors::emitter::ColorConfig;
62682a34 28use syntax::feature_gate::UnstableFeatures;
92a42be0 29use syntax::parse::token;
1a4d82fc 30
d9579d0f 31use std::cell::{RefCell, Cell};
1a4d82fc 32use std::collections::{HashMap, HashSet};
92a42be0 33use std::rc::Rc;
1a4d82fc
JJ
34
35use visit_ast::RustdocVisitor;
36use clean;
37use clean::Clean;
38
85aaf69f
SL
39pub use rustc::session::config::Input;
40pub use rustc::session::search_paths::SearchPaths;
41
1a4d82fc 42/// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
62682a34 43pub enum MaybeTyped<'a, 'tcx: 'a> {
54a0048b 44 Typed(&'a TyCtxt<'tcx>),
b039eaaf 45 NotTyped(&'a session::Session)
1a4d82fc
JJ
46}
47
e9174d1e 48pub type ExternalPaths = RefCell<Option<HashMap<DefId,
1a4d82fc
JJ
49 (Vec<String>, clean::TypeKind)>>>;
50
62682a34 51pub struct DocContext<'a, 'tcx: 'a> {
b039eaaf 52 pub map: &'a hir_map::Map<'tcx>,
62682a34 53 pub maybe_typed: MaybeTyped<'a, 'tcx>,
85aaf69f 54 pub input: Input,
1a4d82fc 55 pub external_paths: ExternalPaths,
e9174d1e
SL
56 pub external_traits: RefCell<Option<HashMap<DefId, clean::Trait>>>,
57 pub external_typarams: RefCell<Option<HashMap<DefId, String>>>,
58 pub inlined: RefCell<Option<HashSet<DefId>>>,
54a0048b 59 pub all_crate_impls: RefCell<HashMap<ast::CrateNum, Vec<clean::Item>>>,
e9174d1e 60 pub deref_trait_did: Cell<Option<DefId>>,
1a4d82fc
JJ
61}
62
62682a34 63impl<'b, 'tcx> DocContext<'b, 'tcx> {
1a4d82fc
JJ
64 pub fn sess<'a>(&'a self) -> &'a session::Session {
65 match self.maybe_typed {
62682a34 66 Typed(tcx) => &tcx.sess,
1a4d82fc
JJ
67 NotTyped(ref sess) => sess
68 }
69 }
70
54a0048b 71 pub fn tcx_opt<'a>(&'a self) -> Option<&'a TyCtxt<'tcx>> {
1a4d82fc 72 match self.maybe_typed {
62682a34 73 Typed(tcx) => Some(tcx),
1a4d82fc
JJ
74 NotTyped(_) => None
75 }
76 }
77
54a0048b 78 pub fn tcx<'a>(&'a self) -> &'a TyCtxt<'tcx> {
1a4d82fc
JJ
79 let tcx_opt = self.tcx_opt();
80 tcx_opt.expect("tcx not present")
81 }
82}
83
84pub struct CrateAnalysis {
92a42be0 85 pub access_levels: AccessLevels<DefId>,
1a4d82fc 86 pub external_paths: ExternalPaths,
e9174d1e
SL
87 pub external_typarams: RefCell<Option<HashMap<DefId, String>>>,
88 pub inlined: RefCell<Option<HashSet<DefId>>>,
89 pub deref_trait_did: Option<DefId>,
1a4d82fc
JJ
90}
91
92pub type Externs = HashMap<String, Vec<String>>;
93
94pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
85aaf69f 95 input: Input, triple: Option<String>)
1a4d82fc
JJ
96 -> (clean::Crate, CrateAnalysis) {
97
98 // Parse, resolve, and typecheck the given crate.
99
85aaf69f
SL
100 let cpath = match input {
101 Input::File(ref p) => Some(p.clone()),
102 _ => None
103 };
1a4d82fc
JJ
104
105 let warning_lint = lint::builtin::WARNINGS.name_lower();
106
107 let sessopts = config::Options {
108 maybe_sysroot: None,
109 search_paths: search_paths,
110 crate_types: vec!(config::CrateTypeRlib),
111 lint_opts: vec!((warning_lint, lint::Allow)),
92a42be0 112 lint_cap: Some(lint::Allow),
1a4d82fc
JJ
113 externs: externs,
114 target_triple: triple.unwrap_or(config::host_triple().to_string()),
115 cfg: config::parse_cfgspecs(cfgs),
116 // Ensure that rustdoc works even if rustc is feature-staged
62682a34 117 unstable_features: UnstableFeatures::Allow,
1a4d82fc
JJ
118 ..config::basic_options().clone()
119 };
120
9cc50fc6
SL
121 let codemap = Rc::new(codemap::CodeMap::new());
122 let diagnostic_handler = errors::Handler::with_tty_emitter(ColorConfig::Auto,
123 None,
124 true,
125 false,
126 codemap.clone());
1a4d82fc 127
92a42be0 128 let cstore = Rc::new(CStore::new(token::get_ident_interner()));
9cc50fc6
SL
129 let sess = session::build_session_(sessopts, cpath, diagnostic_handler,
130 codemap, cstore.clone());
c34b1796 131 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
1a4d82fc 132
e9174d1e
SL
133 let mut cfg = config::build_configuration(&sess);
134 target_features::add_configuration(&mut cfg, &sess);
1a4d82fc 135
54a0048b 136 let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
1a4d82fc 137
85aaf69f 138 let name = link::find_crate_name(Some(&sess), &krate.attrs,
1a4d82fc
JJ
139 &input);
140
92a42be0 141 let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, &name, None)
1a4d82fc
JJ
142 .expect("phase_2_configure_and_expand aborted in rustdoc!");
143
e9174d1e
SL
144 let krate = driver::assign_node_ids(&sess, krate);
145 // Lower ast -> hir.
b039eaaf 146 let lcx = LoweringContext::new(&sess, Some(&krate));
7453a54e 147 let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), DepGraph::new(false));
85aaf69f 148 let arenas = ty::CtxtArenas::new();
e9174d1e 149 let hir_map = driver::make_map(&sess, &mut hir_forest);
1a4d82fc 150
7453a54e
SL
151 let krate_and_analysis = abort_on_err(driver::phase_3_run_analysis_passes(&sess,
152 &cstore,
153 hir_map,
154 &arenas,
155 &name,
156 resolve::MakeGlobMap::No,
157 |tcx, _, analysis, result| {
158 // Return if the driver hit an err (in `result`)
159 if let Err(_) = result {
160 return None
161 }
162
9cc50fc6 163 let _ignore = tcx.dep_graph.in_ignore();
92a42be0 164 let ty::CrateAnalysis { access_levels, .. } = analysis;
62682a34 165
b039eaaf
SL
166 // Convert from a NodeId set to a DefId set since we don't always have easy access
167 // to the map from defid -> nodeid
92a42be0
SL
168 let access_levels = AccessLevels {
169 map: access_levels.map.into_iter()
170 .map(|(k, v)| (tcx.map.local_def_id(k), v))
171 .collect()
172 };
b039eaaf 173
62682a34 174 let ctxt = DocContext {
b039eaaf 175 map: &tcx.map,
62682a34
SL
176 maybe_typed: Typed(tcx),
177 input: input,
178 external_traits: RefCell::new(Some(HashMap::new())),
179 external_typarams: RefCell::new(Some(HashMap::new())),
180 external_paths: RefCell::new(Some(HashMap::new())),
181 inlined: RefCell::new(Some(HashSet::new())),
54a0048b 182 all_crate_impls: RefCell::new(HashMap::new()),
62682a34
SL
183 deref_trait_did: Cell::new(None),
184 };
b039eaaf 185 debug!("crate: {:?}", ctxt.map.krate());
62682a34
SL
186
187 let mut analysis = CrateAnalysis {
92a42be0 188 access_levels: access_levels,
62682a34
SL
189 external_paths: RefCell::new(None),
190 external_typarams: RefCell::new(None),
191 inlined: RefCell::new(None),
192 deref_trait_did: None,
193 };
194
195 let krate = {
196 let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
b039eaaf 197 v.visit(ctxt.map.krate());
62682a34
SL
198 v.clean(&ctxt)
199 };
200
201 let external_paths = ctxt.external_paths.borrow_mut().take();
202 *analysis.external_paths.borrow_mut() = external_paths;
7453a54e 203
62682a34
SL
204 let map = ctxt.external_typarams.borrow_mut().take();
205 *analysis.external_typarams.borrow_mut() = map;
7453a54e 206
62682a34
SL
207 let map = ctxt.inlined.borrow_mut().take();
208 *analysis.inlined.borrow_mut() = map;
7453a54e 209
62682a34 210 analysis.deref_trait_did = ctxt.deref_trait_did.get();
7453a54e
SL
211
212 Some((krate, analysis))
213 }), &sess);
214
215 krate_and_analysis.unwrap()
1a4d82fc 216}