about summary refs log tree commit diff
path: root/src/librustdoc/core.rs
blob: 69d9f8f5946d75fd126424777e914d00f7ec02f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rustc;
use rustc::{driver, middle};
use rustc::middle::privacy;

use syntax::ast;
use syntax::ast_util::is_local;
use syntax::diagnostic;
use syntax::parse;
use syntax;

use std::os;
use std::local_data;
use std::hashmap::HashMap;

use visit_ast::RustdocVisitor;
use clean;
use clean::Clean;

pub struct DocContext {
    crate: ast::Crate,
    tycx: middle::ty::ctxt,
    sess: driver::session::Session
}

pub struct CrateAnalysis {
    exported_items: privacy::ExportedItems,
    reexports: HashMap<ast::NodeId, ~[ast::NodeId]>,
}

/// Parses, resolves, and typechecks the given crate
fn get_ast_and_resolve(cpath: &Path,
                       libs: ~[Path]) -> (DocContext, CrateAnalysis) {
    use syntax::codemap::dummy_spanned;
    use rustc::driver::driver::{file_input, build_configuration,
                                phase_1_parse_input,
                                phase_2_configure_and_expand,
                                phase_3_run_analysis_passes};

    let parsesess = parse::new_parse_sess(None);
    let input = file_input(cpath.clone());

    let sessopts = @driver::session::options {
        binary: @"rustdoc",
        maybe_sysroot: Some(@os::self_exe_path().unwrap().pop()),
        addl_lib_search_paths: @mut libs,
        .. (*rustc::driver::session::basic_options()).clone()
    };


    let diagnostic_handler = syntax::diagnostic::mk_handler(None);
    let span_diagnostic_handler =
        syntax::diagnostic::mk_span_handler(diagnostic_handler, parsesess.cm);

    let sess = driver::driver::build_session_(sessopts,
                                              parsesess.cm,
                                              @diagnostic::DefaultEmitter as
                                                @diagnostic::Emitter,
                                              span_diagnostic_handler);

    let mut cfg = build_configuration(sess);
    cfg.push(@dummy_spanned(ast::MetaWord(@"stage2")));

    let mut crate = phase_1_parse_input(sess, cfg.clone(), &input);
    crate = phase_2_configure_and_expand(sess, cfg, crate);
    let driver::driver::CrateAnalysis {
        exported_items, ty_cx, exp_map2, _
    } = phase_3_run_analysis_passes(sess, &crate);

    let mut reexports = HashMap::new();
    for (&module, nodes) in exp_map2.iter() {
        reexports.insert(module, nodes.iter()
                                      .filter(|e| e.reexport && is_local(e.def_id))
                                      .map(|e| e.def_id.node)
                                      .to_owned_vec());
    }

    debug2!("crate: {:?}", crate);
    return (DocContext { crate: crate, tycx: ty_cx, sess: sess },
            CrateAnalysis { reexports: reexports, exported_items: exported_items });
}

pub fn run_core (libs: ~[Path], path: &Path) -> (clean::Crate, CrateAnalysis) {
    let (ctxt, analysis) = get_ast_and_resolve(path, libs);
    let ctxt = @ctxt;
    debug2!("defmap:");
    for (k, v) in ctxt.tycx.def_map.iter() {
        debug2!("{:?}: {:?}", k, v);
    }
    local_data::set(super::ctxtkey, ctxt);

    let v = @mut RustdocVisitor::new();
    v.visit(&ctxt.crate);

    (v.clean(), analysis)
}