2013-09-12 20:10:51 -05:00
|
|
|
// 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.
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
//! Rust AST Visitor. Extracts useful information and massages it into a form
|
|
|
|
//! usable for clean
|
|
|
|
|
|
|
|
use syntax::abi::AbiSet;
|
2013-12-22 13:23:04 -06:00
|
|
|
use syntax::ast;
|
2013-09-05 09:14:35 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
use doctree::*;
|
|
|
|
|
|
|
|
pub struct RustdocVisitor {
|
|
|
|
module: Module,
|
|
|
|
attrs: ~[ast::Attribute],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RustdocVisitor {
|
|
|
|
pub fn new() -> RustdocVisitor {
|
|
|
|
RustdocVisitor {
|
|
|
|
module: Module::new(None),
|
|
|
|
attrs: ~[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RustdocVisitor {
|
2013-12-30 19:17:45 -06:00
|
|
|
pub fn visit(&mut self, crate: &ast::Crate) {
|
2013-08-15 15:28:54 -05:00
|
|
|
self.attrs = crate.attrs.clone();
|
|
|
|
fn visit_struct_def(item: &ast::item, sd: @ast::struct_def, generics:
|
|
|
|
&ast::Generics) -> Struct {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Visiting struct");
|
2013-08-15 15:28:54 -05:00
|
|
|
let struct_type = struct_type_from_def(sd);
|
|
|
|
Struct {
|
|
|
|
id: item.id,
|
|
|
|
struct_type: struct_type,
|
|
|
|
name: item.ident,
|
|
|
|
vis: item.vis,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
generics: generics.clone(),
|
2013-11-30 10:27:25 -06:00
|
|
|
fields: sd.fields.clone(),
|
2013-08-15 15:28:54 -05:00
|
|
|
where: item.span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_enum_def(it: &ast::item, def: &ast::enum_def, params: &ast::Generics) -> Enum {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Visiting enum");
|
2013-08-15 15:28:54 -05:00
|
|
|
let mut vars: ~[Variant] = ~[];
|
|
|
|
for x in def.variants.iter() {
|
|
|
|
vars.push(Variant {
|
|
|
|
name: x.node.name,
|
|
|
|
attrs: x.node.attrs.clone(),
|
|
|
|
vis: x.node.vis,
|
|
|
|
id: x.node.id,
|
|
|
|
kind: x.node.kind.clone(),
|
|
|
|
where: x.span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Enum {
|
|
|
|
name: it.ident,
|
|
|
|
variants: vars,
|
|
|
|
vis: it.vis,
|
|
|
|
generics: params.clone(),
|
|
|
|
attrs: it.attrs.clone(),
|
|
|
|
id: it.id,
|
|
|
|
where: it.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:38:17 -05:00
|
|
|
fn visit_fn(item: &ast::item, fd: &ast::fn_decl, purity: &ast::purity,
|
2013-08-15 15:28:54 -05:00
|
|
|
_abi: &AbiSet, gen: &ast::Generics) -> Function {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Visiting fn");
|
2013-08-15 15:28:54 -05:00
|
|
|
Function {
|
|
|
|
id: item.id,
|
|
|
|
vis: item.vis,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
decl: fd.clone(),
|
|
|
|
name: item.ident,
|
|
|
|
where: item.span,
|
|
|
|
generics: gen.clone(),
|
2013-09-23 22:38:17 -05:00
|
|
|
purity: *purity,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 09:14:35 -05:00
|
|
|
fn visit_mod_contents(span: Span, attrs: ~[ast::Attribute], vis:
|
2013-12-22 13:23:04 -06:00
|
|
|
ast::visibility, id: ast::NodeId, m: &ast::_mod,
|
|
|
|
name: Option<ast::Ident>) -> Module {
|
2013-08-15 15:28:54 -05:00
|
|
|
let mut om = Module::new(name);
|
|
|
|
om.view_items = m.view_items.clone();
|
|
|
|
om.where = span;
|
|
|
|
om.attrs = attrs;
|
|
|
|
om.vis = vis;
|
|
|
|
om.id = id;
|
|
|
|
for i in m.items.iter() {
|
|
|
|
visit_item(*i, &mut om);
|
|
|
|
}
|
|
|
|
om
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(item: &ast::item, om: &mut Module) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Visiting item {:?}", item);
|
2013-08-15 15:28:54 -05:00
|
|
|
match item.node {
|
|
|
|
ast::item_mod(ref m) => {
|
|
|
|
om.mods.push(visit_mod_contents(item.span, item.attrs.clone(),
|
2013-12-22 13:23:04 -06:00
|
|
|
item.vis, item.id, m,
|
|
|
|
Some(item.ident)));
|
2013-08-15 15:28:54 -05:00
|
|
|
},
|
|
|
|
ast::item_enum(ref ed, ref gen) => om.enums.push(visit_enum_def(item, ed, gen)),
|
|
|
|
ast::item_struct(sd, ref gen) => om.structs.push(visit_struct_def(item, sd, gen)),
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::item_fn(fd, ref pur, ref abi, ref gen, _) =>
|
2013-08-15 15:28:54 -05:00
|
|
|
om.fns.push(visit_fn(item, fd, pur, abi, gen)),
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::item_ty(ty, ref gen) => {
|
2013-08-15 15:28:54 -05:00
|
|
|
let t = Typedef {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: ty,
|
2013-08-15 15:28:54 -05:00
|
|
|
gen: gen.clone(),
|
|
|
|
name: item.ident,
|
|
|
|
id: item.id,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
where: item.span,
|
|
|
|
vis: item.vis,
|
|
|
|
};
|
|
|
|
om.typedefs.push(t);
|
|
|
|
},
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::item_static(ty, ref mut_, ref exp) => {
|
2013-08-15 15:28:54 -05:00
|
|
|
let s = Static {
|
2013-11-30 16:00:39 -06:00
|
|
|
type_: ty,
|
2013-08-15 15:28:54 -05:00
|
|
|
mutability: mut_.clone(),
|
|
|
|
expr: exp.clone(),
|
|
|
|
id: item.id,
|
|
|
|
name: item.ident,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
where: item.span,
|
|
|
|
vis: item.vis,
|
|
|
|
};
|
|
|
|
om.statics.push(s);
|
|
|
|
},
|
|
|
|
ast::item_trait(ref gen, ref tr, ref met) => {
|
|
|
|
let t = Trait {
|
|
|
|
name: item.ident,
|
|
|
|
methods: met.clone(),
|
|
|
|
generics: gen.clone(),
|
|
|
|
parents: tr.clone(),
|
|
|
|
id: item.id,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
where: item.span,
|
|
|
|
vis: item.vis,
|
|
|
|
};
|
|
|
|
om.traits.push(t);
|
|
|
|
},
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::item_impl(ref gen, ref tr, ty, ref meths) => {
|
2013-08-15 15:28:54 -05:00
|
|
|
let i = Impl {
|
|
|
|
generics: gen.clone(),
|
|
|
|
trait_: tr.clone(),
|
2013-11-30 16:00:39 -06:00
|
|
|
for_: ty,
|
2013-08-15 15:28:54 -05:00
|
|
|
methods: meths.clone(),
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
id: item.id,
|
|
|
|
where: item.span,
|
|
|
|
vis: item.vis,
|
|
|
|
};
|
|
|
|
om.impls.push(i);
|
|
|
|
},
|
2013-09-26 13:57:25 -05:00
|
|
|
ast::item_foreign_mod(ref fm) => {
|
|
|
|
om.foreigns.push(fm.clone());
|
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.module = visit_mod_contents(crate.span, crate.attrs.clone(),
|
2013-12-22 13:23:04 -06:00
|
|
|
ast::public, ast::CRATE_NODE_ID,
|
|
|
|
&crate.module, None);
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
}
|