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