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