rust/src/librustdoc/tystr_pass.rs

447 lines
13 KiB
Rust
Raw Normal View History

// Copyright 2012 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.
//! Pulls type information out of the AST and attaches it to the document
2012-01-17 17:44:32 -08:00
use core::prelude::*;
use astsrv;
2012-09-18 16:48:40 -07:00
use doc::ItemUtils;
use doc;
use extract::to_str;
use extract;
use fold::Fold;
use fold;
use pass::Pass;
use core::vec;
2012-09-05 10:41:47 -07:00
use syntax::ast;
use syntax::print::pprust;
use syntax::ast_map;
2012-11-19 18:00:12 -08:00
pub fn mk_pass() -> Pass {
Pass {
name: ~"tystr",
2012-02-27 18:07:16 -08:00
f: run
}
}
pub fn run(
2012-09-18 16:48:40 -07:00
srv: astsrv::Srv,
2013-01-30 19:32:36 -08:00
doc: doc::Doc
2012-09-18 16:48:40 -07:00
) -> doc::Doc {
let fold = Fold {
2013-01-31 20:24:03 -08:00
ctxt: srv.clone(),
fold_fn: fold_fn,
2012-01-25 17:22:19 -08:00
fold_const: fold_const,
fold_enum: fold_enum,
fold_trait: fold_trait,
2012-02-01 22:41:41 -08:00
fold_impl: fold_impl,
2012-09-04 13:29:32 -07:00
fold_type: fold_type,
2012-09-19 14:37:43 -07:00
fold_struct: fold_struct,
.. fold::default_any_fold(srv)
};
(fold.fold_doc)(&fold, doc)
}
fn fold_fn(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::FnDoc
2012-09-18 16:48:40 -07:00
) -> doc::FnDoc {
2013-01-31 20:24:03 -08:00
let srv = fold.ctxt.clone();
doc::SimpleItemDoc {
2012-09-04 13:29:32 -07:00
sig: get_fn_sig(srv, doc.id()),
.. doc
}
}
2012-09-18 16:48:40 -07:00
fn get_fn_sig(srv: astsrv::Srv, fn_id: doc::AstId) -> Option<~str> {
2012-06-30 16:19:07 -07:00
do astsrv::exec(srv) |ctxt| {
match ctxt.ast_map.get(&fn_id) {
2013-01-13 13:13:41 -08:00
ast_map::node_item(@ast::item {
ident: ident,
2013-01-30 13:14:35 -08:00
node: ast::item_fn(ref decl, _, ref tys, _), _
}, _) |
2013-01-13 12:02:16 -08:00
ast_map::node_foreign_item(@ast::foreign_item {
ident: ident,
2013-01-30 13:14:35 -08:00
node: ast::foreign_item_fn(ref decl, _, ref tys), _
}, _, _, _) => {
Some(pprust::fun_to_str(decl, ident, None, tys,
2013-01-30 13:14:35 -08:00
extract::interner()))
}
_ => fail!(~"get_fn_sig: fn_id not bound to a fn item")
}
}
}
#[test]
fn should_add_fn_sig() {
let doc = test::mk_doc(~"fn a<T>() -> int { }");
fail_unless!(doc.cratemod().fns()[0].sig == Some(~"fn a<T>() -> int"));
}
#[test]
fn should_add_foreign_fn_sig() {
let doc = test::mk_doc(~"extern mod a { fn a<T>() -> int; }");
2013-03-06 19:09:17 -08:00
fail_unless!(doc.cratemod().nmods()[0].fns[0].sig ==
Some(~"fn a<T>() -> int"));
}
fn fold_const(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::ConstDoc
2012-09-18 16:48:40 -07:00
) -> doc::ConstDoc {
2013-01-31 20:24:03 -08:00
let srv = fold.ctxt.clone();
doc::SimpleItemDoc {
sig: Some({
let doc = copy doc;
do astsrv::exec(srv) |ctxt| {
match ctxt.ast_map.get(&doc.id()) {
ast_map::node_item(@ast::item {
node: ast::item_const(ty, _), _
}, _) => {
pprust::ty_to_str(ty, extract::interner())
}
_ => fail!(~"fold_const: id not bound to a const item")
}
}}),
2012-09-04 13:29:32 -07:00
.. doc
}
}
#[test]
fn should_add_const_types() {
let doc = test::mk_doc(~"static a: bool = true;");
fail_unless!(doc.cratemod().consts()[0].sig == Some(~"bool"));
2012-01-25 17:22:19 -08:00
}
fn fold_enum(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::EnumDoc
2012-09-18 16:48:40 -07:00
) -> doc::EnumDoc {
let doc_id = doc.id();
2013-01-31 20:24:03 -08:00
let srv = fold.ctxt.clone();
2012-01-25 17:22:19 -08:00
doc::EnumDoc {
2013-01-31 20:24:03 -08:00
variants: do vec::map(doc.variants) |variant| {
let sig = {
let variant = copy *variant;
2013-02-17 08:54:34 +10:00
do astsrv::exec(srv.clone()) |ctxt| {
match ctxt.ast_map.get(&doc_id) {
ast_map::node_item(@ast::item {
node: ast::item_enum(ref enum_definition, _), _
}, _) => {
let ast_variant =
do vec::find(enum_definition.variants) |v| {
to_str(v.node.name) == variant.name
}.get();
pprust::variant_to_str(
ast_variant, extract::interner())
}
_ => fail!(~"enum variant not bound to an enum item")
}
2012-01-25 17:22:19 -08:00
}
};
doc::VariantDoc {
2012-09-04 13:29:32 -07:00
sig: Some(sig),
.. copy *variant
2012-01-25 17:22:19 -08:00
}
2012-09-04 13:29:32 -07:00
},
.. doc
2012-01-25 17:22:19 -08:00
}
}
#[test]
fn should_add_variant_sigs() {
let doc = test::mk_doc(~"enum a { b(int) }");
2013-03-06 19:09:17 -08:00
fail_unless!(doc.cratemod().enums()[0].variants[0].sig ==
Some(~"b(int)"));
2012-01-25 17:22:19 -08:00
}
fn fold_trait(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::TraitDoc
2012-09-18 16:48:40 -07:00
) -> doc::TraitDoc {
doc::TraitDoc {
2013-01-31 20:24:03 -08:00
methods: merge_methods(fold.ctxt.clone(), doc.id(), copy doc.methods),
2012-09-04 13:29:32 -07:00
.. doc
}
}
2012-01-31 16:35:54 -08:00
fn merge_methods(
2012-09-18 16:48:40 -07:00
srv: astsrv::Srv,
item_id: doc::AstId,
2013-01-30 19:32:36 -08:00
docs: ~[doc::MethodDoc]
2012-09-18 16:48:40 -07:00
) -> ~[doc::MethodDoc] {
2013-01-31 20:24:03 -08:00
do vec::map(docs) |doc| {
doc::MethodDoc {
2013-01-31 20:24:03 -08:00
sig: get_method_sig(srv.clone(), item_id, copy doc.name),
2013-01-30 13:14:35 -08:00
.. copy *doc
2012-01-31 16:35:54 -08:00
}
}
}
fn get_method_sig(
2012-09-18 16:48:40 -07:00
srv: astsrv::Srv,
item_id: doc::AstId,
2013-01-30 19:32:36 -08:00
method_name: ~str
2012-08-20 12:23:37 -07:00
) -> Option<~str> {
2013-02-17 08:54:34 +10:00
do astsrv::exec(srv) |ctxt| {
match ctxt.ast_map.get(&item_id) {
2013-01-13 13:13:41 -08:00
ast_map::node_item(@ast::item {
2013-01-30 13:14:35 -08:00
node: ast::item_trait(_, _, ref methods), _
2012-08-03 19:59:04 -07:00
}, _) => {
2013-01-30 13:14:35 -08:00
match vec::find(*methods, |method| {
match copy *method {
2012-07-18 16:18:02 -07:00
ast::required(ty_m) => to_str(ty_m.ident) == method_name,
ast::provided(m) => to_str(m.ident) == method_name,
}
}) {
2012-08-20 12:23:37 -07:00
Some(method) => {
2012-08-06 12:34:08 -07:00
match method {
2012-08-03 19:59:04 -07:00
ast::required(ty_m) => {
2012-08-20 12:23:37 -07:00
Some(pprust::fun_to_str(
&ty_m.decl,
ty_m.ident,
Some(ty_m.self_ty.node),
&ty_m.generics,
2012-07-18 16:18:02 -07:00
extract::interner()
))
}
2012-08-03 19:59:04 -07:00
ast::provided(m) => {
2012-08-20 12:23:37 -07:00
Some(pprust::fun_to_str(
&m.decl,
m.ident,
Some(m.self_ty.node),
&m.generics,
2012-07-18 16:18:02 -07:00
extract::interner()
))
}
}
}
_ => fail!(~"method not found")
2012-01-31 16:35:54 -08:00
}
}
2013-01-13 13:13:41 -08:00
ast_map::node_item(@ast::item {
2013-01-30 13:14:35 -08:00
node: ast::item_impl(_, _, _, ref methods), _
2012-08-03 19:59:04 -07:00
}, _) => {
2013-01-30 13:14:35 -08:00
match vec::find(*methods, |method| {
to_str(method.ident) == method_name
}) {
Some(method) => {
Some(pprust::fun_to_str(
&method.decl,
method.ident,
Some(method.self_ty.node),
&method.generics,
extract::interner()
))
2012-01-31 16:35:54 -08:00
}
None => fail!(~"method not found")
}
}
_ => fail!(~"get_method_sig: item ID not bound to trait or impl")
}
}
}
#[test]
fn should_add_trait_method_sigs() {
2013-03-17 11:45:22 +10:00
let doc = test::mk_doc(~"trait i { fn a<T>(&mut self) -> int; }");
fail_unless!(doc.cratemod().traits()[0].methods[0].sig
2013-03-17 11:45:22 +10:00
== Some(~"fn a<T>(&mut self) -> int"));
}
2012-01-31 16:35:54 -08:00
fn fold_impl(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::ImplDoc
2012-09-18 16:48:40 -07:00
) -> doc::ImplDoc {
2012-01-31 16:35:54 -08:00
2013-01-31 20:24:03 -08:00
let srv = fold.ctxt.clone();
2012-01-31 16:35:54 -08:00
let (trait_types, self_ty) = {
let doc = copy doc;
do astsrv::exec(srv) |ctxt| {
match ctxt.ast_map.get(&doc.id()) {
ast_map::node_item(@ast::item {
node: ast::item_impl(_, opt_trait_type, self_ty, _), _
}, _) => {
let trait_types = opt_trait_type.map_default(~[], |p| {
~[pprust::path_to_str(p.path, extract::interner())]
});
(trait_types,
Some(pprust::ty_to_str(
self_ty, extract::interner())))
}
_ => fail!(~"expected impl")
}
2012-01-31 16:35:54 -08:00
}
};
doc::ImplDoc {
trait_types: trait_types,
2012-01-31 16:35:54 -08:00
self_ty: self_ty,
2013-01-31 20:24:03 -08:00
methods: merge_methods(fold.ctxt.clone(), doc.id(), copy doc.methods),
2012-09-04 13:29:32 -07:00
.. doc
2012-01-31 16:35:54 -08:00
}
}
#[test]
fn should_add_impl_trait_types() {
let doc = test::mk_doc(~"impl j for int { fn a<T>() { } }");
fail_unless!(doc.cratemod().impls()[0].trait_types[0] == ~"j");
2012-01-31 16:35:54 -08:00
}
#[test]
fn should_not_add_impl_trait_types_if_none() {
2012-08-08 17:19:06 -07:00
let doc = test::mk_doc(~"impl int { fn a() { } }");
fail_unless!(vec::len(doc.cratemod().impls()[0].trait_types) == 0);
2012-01-31 16:35:54 -08:00
}
#[test]
fn should_add_impl_self_ty() {
2012-08-08 17:19:06 -07:00
let doc = test::mk_doc(~"impl int { fn a() { } }");
fail_unless!(doc.cratemod().impls()[0].self_ty == Some(~"int"));
2012-01-31 16:35:54 -08:00
}
#[test]
fn should_add_impl_method_sigs() {
2013-03-17 11:45:22 +10:00
let doc = test::mk_doc(~"impl int { fn a<T>(&self) -> int { fail!() } }");
fail_unless!(doc.cratemod().impls()[0].methods[0].sig
2013-03-17 11:45:22 +10:00
== Some(~"fn a<T>(&self) -> int"));
2012-01-31 16:35:54 -08:00
}
2012-02-01 22:41:41 -08:00
fn fold_type(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::TyDoc
2012-09-18 16:48:40 -07:00
) -> doc::TyDoc {
2012-02-01 22:41:41 -08:00
2013-01-31 20:24:03 -08:00
let srv = fold.ctxt.clone();
2012-02-01 22:41:41 -08:00
doc::SimpleItemDoc {
sig: {
let doc = copy doc;
do astsrv::exec(srv) |ctxt| {
match ctxt.ast_map.get(&doc.id()) {
ast_map::node_item(@ast::item {
ident: ident,
node: ast::item_ty(ty, ref params), _
}, _) => {
Some(fmt!(
"type %s%s = %s",
to_str(ident),
pprust::generics_to_str(params,
extract::interner()),
pprust::ty_to_str(ty,
extract::interner())
))
}
_ => fail!(~"expected type")
}
2012-02-01 22:41:41 -08:00
}
2012-09-04 13:29:32 -07:00
},
.. doc
2012-02-01 22:41:41 -08:00
}
}
#[test]
fn should_add_type_signatures() {
let doc = test::mk_doc(~"type t<T> = int;");
fail_unless!(doc.cratemod().types()[0].sig == Some(~"type t<T> = int"));
2012-02-01 22:41:41 -08:00
}
2012-09-19 14:37:43 -07:00
fn fold_struct(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
2013-01-30 19:32:36 -08:00
doc: doc::StructDoc
2012-09-19 14:37:43 -07:00
) -> doc::StructDoc {
2013-01-31 20:24:03 -08:00
let srv = fold.ctxt.clone();
2012-09-19 14:37:43 -07:00
doc::StructDoc {
sig: {
let doc = copy doc;
do astsrv::exec(srv) |ctxt| {
match ctxt.ast_map.get(&doc.id()) {
ast_map::node_item(item, _) => {
let item = strip_struct_extra_stuff(item);
Some(pprust::item_to_str(item,
extract::interner()))
}
_ => fail!(~"not an item")
2012-09-19 14:37:43 -07:00
}
}
},
.. doc
}
}
/// Removes various things from the struct item definition that
/// shouldn't be displayed in the struct signature. Probably there
/// should be a simple pprust::struct_to_str function that does
/// what I actually want
fn strip_struct_extra_stuff(item: @ast::item) -> @ast::item {
2013-01-30 13:14:35 -08:00
let node = match copy item.node {
ast::item_struct(def, tys) => {
2013-01-13 13:45:57 -08:00
let def = @ast::struct_def {
dtor: None, // Remove the drop { } block
2013-01-30 13:14:35 -08:00
.. copy *def
};
ast::item_struct(def, tys)
}
_ => fail!(~"not a struct")
};
2013-01-13 13:13:41 -08:00
@ast::item {
attrs: ~[], // Remove the attributes
node: node,
2013-01-30 13:14:35 -08:00
.. copy *item
}
}
2012-09-19 14:37:43 -07:00
#[test]
fn should_add_struct_defs() {
let doc = test::mk_doc(~"struct S { field: () }");
2013-03-06 19:09:17 -08:00
fail_unless!((&doc.cratemod().structs()[0].sig).get().contains(
"struct S {"));
2012-09-19 14:37:43 -07:00
}
#[test]
fn should_not_serialize_struct_drop_blocks() {
// All we care about are the fields
let doc = test::mk_doc(~"struct S { field: (), drop { } }");
fail_unless!(!(&doc.cratemod().structs()[0].sig).get().contains("drop"));
}
#[test]
fn should_not_serialize_struct_attrs() {
// All we care about are the fields
let doc = test::mk_doc(~"#[wut] struct S { field: () }");
fail_unless!(!(&doc.cratemod().structs()[0].sig).get().contains("wut"));
}
2012-01-31 18:32:37 -08:00
#[cfg(test)]
pub mod test {
use astsrv;
use doc;
use extract;
use tystr_pass::run;
2013-01-30 19:32:36 -08:00
pub fn mk_doc(source: ~str) -> doc::Doc {
2013-01-30 13:14:35 -08:00
do astsrv::from_str(copy source) |srv| {
2013-01-31 20:24:03 -08:00
let doc = extract::from_srv(srv.clone(), ~"");
run(srv.clone(), doc)
}
2012-01-31 18:32:37 -08:00
}
}