]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/core.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / librustdoc / core.rs
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.
10 pub use self::MaybeTyped::*;
11
12 use rustc_lint;
13 use rustc_driver::driver;
14 use rustc::session::{self, config};
15 use rustc::session::config::UnstableFeatures;
16 use rustc::middle::{privacy, ty};
17 use rustc::lint;
18 use rustc_trans::back::link;
19 use rustc_resolve as resolve;
20
21 use syntax::{ast, ast_map, codemap, diagnostic};
22
23 use std::cell::RefCell;
24 use std::collections::{HashMap, HashSet};
25
26 use visit_ast::RustdocVisitor;
27 use clean;
28 use clean::Clean;
29
30 pub use rustc::session::config::Input;
31 pub use rustc::session::search_paths::SearchPaths;
32
33 /// Are we generating documentation (`Typed`) or tests (`NotTyped`)?
34 pub enum MaybeTyped<'tcx> {
35 Typed(ty::ctxt<'tcx>),
36 NotTyped(session::Session)
37 }
38
39 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
40 (Vec<String>, clean::TypeKind)>>>;
41
42 pub struct DocContext<'tcx> {
43 pub krate: &'tcx ast::Crate,
44 pub maybe_typed: MaybeTyped<'tcx>,
45 pub input: Input,
46 pub external_paths: ExternalPaths,
47 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
48 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
49 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
50 pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
51 }
52
53 impl<'tcx> DocContext<'tcx> {
54 pub fn sess<'a>(&'a self) -> &'a session::Session {
55 match self.maybe_typed {
56 Typed(ref tcx) => &tcx.sess,
57 NotTyped(ref sess) => sess
58 }
59 }
60
61 pub fn tcx_opt<'a>(&'a self) -> Option<&'a ty::ctxt<'tcx>> {
62 match self.maybe_typed {
63 Typed(ref tcx) => Some(tcx),
64 NotTyped(_) => None
65 }
66 }
67
68 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
69 let tcx_opt = self.tcx_opt();
70 tcx_opt.expect("tcx not present")
71 }
72 }
73
74 pub struct CrateAnalysis {
75 pub exported_items: privacy::ExportedItems,
76 pub public_items: privacy::PublicItems,
77 pub external_paths: ExternalPaths,
78 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
79 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
80 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
81 }
82
83 pub type Externs = HashMap<String, Vec<String>>;
84
85 pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
86 input: Input, triple: Option<String>)
87 -> (clean::Crate, CrateAnalysis) {
88
89 // Parse, resolve, and typecheck the given crate.
90
91 let cpath = match input {
92 Input::File(ref p) => Some(p.clone()),
93 _ => None
94 };
95
96 let warning_lint = lint::builtin::WARNINGS.name_lower();
97
98 let sessopts = config::Options {
99 maybe_sysroot: None,
100 search_paths: search_paths,
101 crate_types: vec!(config::CrateTypeRlib),
102 lint_opts: vec!((warning_lint, lint::Allow)),
103 externs: externs,
104 target_triple: triple.unwrap_or(config::host_triple().to_string()),
105 cfg: config::parse_cfgspecs(cfgs),
106 // Ensure that rustdoc works even if rustc is feature-staged
107 unstable_features: UnstableFeatures::Default,
108 ..config::basic_options().clone()
109 };
110
111 let codemap = codemap::CodeMap::new();
112 let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None, true);
113 let span_diagnostic_handler =
114 diagnostic::mk_span_handler(diagnostic_handler, codemap);
115
116 let sess = session::build_session_(sessopts, cpath,
117 span_diagnostic_handler);
118 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
119
120 let cfg = config::build_configuration(&sess);
121
122 let krate = driver::phase_1_parse_input(&sess, cfg, &input);
123
124 let name = link::find_crate_name(Some(&sess), &krate.attrs,
125 &input);
126
127 let krate = driver::phase_2_configure_and_expand(&sess, krate, &name, None)
128 .expect("phase_2_configure_and_expand aborted in rustdoc!");
129
130 let mut forest = ast_map::Forest::new(krate);
131 let arenas = ty::CtxtArenas::new();
132 let ast_map = driver::assign_node_ids_and_map(&sess, &mut forest);
133
134 let ty::CrateAnalysis {
135 exported_items, public_items, ty_cx, ..
136 } = driver::phase_3_run_analysis_passes(sess,
137 ast_map,
138 &arenas,
139 name,
140 resolve::MakeGlobMap::No);
141
142 let ctxt = DocContext {
143 krate: ty_cx.map.krate(),
144 maybe_typed: Typed(ty_cx),
145 input: input,
146 external_traits: RefCell::new(Some(HashMap::new())),
147 external_typarams: RefCell::new(Some(HashMap::new())),
148 external_paths: RefCell::new(Some(HashMap::new())),
149 inlined: RefCell::new(Some(HashSet::new())),
150 populated_crate_impls: RefCell::new(HashSet::new()),
151 };
152 debug!("crate: {:?}", ctxt.krate);
153
154 let analysis = CrateAnalysis {
155 exported_items: exported_items,
156 public_items: public_items,
157 external_paths: RefCell::new(None),
158 external_traits: RefCell::new(None),
159 external_typarams: RefCell::new(None),
160 inlined: RefCell::new(None),
161 };
162
163 let krate = {
164 let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
165 v.visit(ctxt.krate);
166 v.clean(&ctxt)
167 };
168
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;
177 (krate, analysis)
178 }