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