2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
//! Pulls type information out of the AST and attaches it to the document
|
2012-01-17 17:44:32 -08:00
|
|
|
|
2013-05-17 15:28:44 -07:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use astsrv;
|
2012-09-18 16:48:40 -07:00
|
|
|
use doc::ItemUtils;
|
2012-12-23 17:41:37 -05:00
|
|
|
use doc;
|
|
|
|
use extract::to_str;
|
|
|
|
use extract;
|
2012-12-05 15:06:54 -08:00
|
|
|
use fold::Fold;
|
2012-12-23 17:41:37 -05:00
|
|
|
use fold;
|
2013-01-08 19:37:25 -08:00
|
|
|
use pass::Pass;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-05-24 19:35:29 -07:00
|
|
|
use core::vec;
|
2012-09-05 10:41:47 -07:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::print::pprust;
|
2013-06-04 12:21:25 -07:00
|
|
|
use syntax::parse::token;
|
2012-09-05 10:41:47 -07:00
|
|
|
use syntax::ast_map;
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-11-19 18:00:12 -08:00
|
|
|
pub fn mk_pass() -> Pass {
|
2013-01-08 14:00:45 -08:00
|
|
|
Pass {
|
2012-07-13 22:57:48 -07:00
|
|
|
name: ~"tystr",
|
2012-02-27 18:07:16 -08:00
|
|
|
f: run
|
|
|
|
}
|
2012-01-17 16:12:50 -08:00
|
|
|
}
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
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 {
|
2012-12-05 15:06:54 -08:00
|
|
|
let fold = Fold {
|
2013-01-31 20:24:03 -08:00
|
|
|
ctxt: srv.clone(),
|
2012-01-24 00:14:31 -08:00
|
|
|
fold_fn: fold_fn,
|
2012-01-25 17:22:19 -08:00
|
|
|
fold_const: fold_const,
|
2012-01-26 20:59:15 -08:00
|
|
|
fold_enum: fold_enum,
|
2012-07-03 16:30:42 -07:00
|
|
|
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,
|
2012-12-05 15:06:54 -08:00
|
|
|
.. fold::default_any_fold(srv)
|
|
|
|
};
|
2012-11-29 17:51:16 -08:00
|
|
|
(fold.fold_doc)(&fold, doc)
|
2012-01-16 15:33:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2013-01-31 20:24:03 -08:00
|
|
|
let srv = fold.ctxt.clone();
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::SimpleItemDoc {
|
2012-09-04 13:29:32 -07:00
|
|
|
sig: get_fn_sig(srv, doc.id()),
|
|
|
|
.. doc
|
2012-01-16 15:33:06 -08:00
|
|
|
}
|
2012-01-18 16:09:32 -08:00
|
|
|
}
|
2012-01-16 15:33:06 -08:00
|
|
|
|
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| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&fn_id) {
|
2013-04-16 01:09:55 +10:00
|
|
|
ast_map::node_item(@ast::item {
|
|
|
|
ident: ident,
|
|
|
|
node: ast::item_fn(ref decl, purity, _, ref tys, _), _
|
|
|
|
}, _) |
|
|
|
|
ast_map::node_foreign_item(@ast::foreign_item {
|
|
|
|
ident: ident,
|
|
|
|
node: ast::foreign_item_fn(ref decl, purity, ref tys), _
|
|
|
|
}, _, _, _) => {
|
|
|
|
Some(pprust::fun_to_str(decl, purity, ident, None, tys,
|
2013-06-04 12:21:25 -07:00
|
|
|
token::get_ident_interner()))
|
2013-04-16 01:09:55 +10:00
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("get_fn_sig: fn_id not bound to a fn item")
|
2012-01-19 18:09:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-24 00:14:31 -08:00
|
|
|
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();
|
2012-01-24 00:14:31 -08:00
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::SimpleItemDoc {
|
2013-01-10 10:59:58 -08:00
|
|
|
sig: Some({
|
|
|
|
let doc = copy doc;
|
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&doc.id()) {
|
2013-01-10 10:59:58 -08:00
|
|
|
ast_map::node_item(@ast::item {
|
2013-06-21 18:46:34 -07:00
|
|
|
node: ast::item_static(ty, _, _), _
|
2013-01-10 10:59:58 -08:00
|
|
|
}, _) => {
|
|
|
|
pprust::ty_to_str(ty, extract::interner())
|
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("fold_const: id not bound to a const item")
|
2013-01-10 10:59:58 -08:00
|
|
|
}
|
|
|
|
}}),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. doc
|
2012-01-24 00:14:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-02-26 23:47:27 -08:00
|
|
|
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
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::EnumDoc {
|
2013-01-31 20:24:03 -08:00
|
|
|
variants: do vec::map(doc.variants) |variant| {
|
2013-01-10 10:59:58 -08:00
|
|
|
let sig = {
|
|
|
|
let variant = copy *variant;
|
2013-02-17 08:54:34 +10:00
|
|
|
do astsrv::exec(srv.clone()) |ctxt| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&doc_id) {
|
2013-01-10 10:59:58 -08:00
|
|
|
ast_map::node_item(@ast::item {
|
|
|
|
node: ast::item_enum(ref enum_definition, _), _
|
|
|
|
}, _) => {
|
|
|
|
let ast_variant =
|
2013-06-19 18:56:57 -04:00
|
|
|
copy *do enum_definition.variants.iter().find_ |v| {
|
2013-01-10 10:59:58 -08:00
|
|
|
to_str(v.node.name) == variant.name
|
|
|
|
}.get();
|
|
|
|
|
|
|
|
pprust::variant_to_str(
|
2013-05-12 02:41:15 -04:00
|
|
|
&ast_variant, extract::interner())
|
2013-01-10 10:59:58 -08:00
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("enum variant not bound to an enum item")
|
2013-01-10 10:59:58 -08:00
|
|
|
}
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::VariantDoc {
|
2012-09-04 13:29:32 -07:00
|
|
|
sig: Some(sig),
|
2013-01-10 10:59:58 -08:00
|
|
|
.. 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 16:30:42 -07: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 {
|
2013-01-25 16:57:39 -08:00
|
|
|
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-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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| {
|
2013-01-25 16:57:39 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 15:44:21 -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| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&item_id) {
|
2013-04-16 01:09:55 +10:00
|
|
|
ast_map::node_item(@ast::item {
|
|
|
|
node: ast::item_trait(_, _, ref methods), _
|
|
|
|
}, _) => {
|
2013-06-19 18:56:57 -04:00
|
|
|
match methods.iter().find_(|&method| {
|
2013-04-16 01:09:55 +10:00
|
|
|
match copy *method {
|
|
|
|
ast::required(ty_m) => to_str(ty_m.ident) == method_name,
|
|
|
|
ast::provided(m) => to_str(m.ident) == method_name,
|
2012-07-10 13:44:20 -07:00
|
|
|
}
|
2013-04-16 01:09:55 +10:00
|
|
|
}) {
|
|
|
|
Some(method) => {
|
2013-06-19 18:56:57 -04:00
|
|
|
match copy *method {
|
2013-04-16 01:09:55 +10:00
|
|
|
ast::required(ty_m) => {
|
|
|
|
Some(pprust::fun_to_str(
|
|
|
|
&ty_m.decl,
|
|
|
|
ty_m.purity,
|
|
|
|
ty_m.ident,
|
2013-04-30 08:49:48 -07:00
|
|
|
Some(ty_m.explicit_self.node),
|
2013-04-16 01:09:55 +10:00
|
|
|
&ty_m.generics,
|
|
|
|
extract::interner()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
ast::provided(m) => {
|
|
|
|
Some(pprust::fun_to_str(
|
|
|
|
&m.decl,
|
|
|
|
m.purity,
|
|
|
|
m.ident,
|
2013-04-30 08:49:48 -07:00
|
|
|
Some(m.explicit_self.node),
|
2013-04-16 01:09:55 +10:00
|
|
|
&m.generics,
|
|
|
|
extract::interner()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 13:44:20 -07:00
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("method not found")
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
2013-04-16 01:09:55 +10:00
|
|
|
ast_map::node_item(@ast::item {
|
|
|
|
node: ast::item_impl(_, _, _, ref methods), _
|
|
|
|
}, _) => {
|
2013-06-19 18:56:57 -04:00
|
|
|
match methods.iter().find_(|method| {
|
2013-04-16 01:09:55 +10:00
|
|
|
to_str(method.ident) == method_name
|
|
|
|
}) {
|
|
|
|
Some(method) => {
|
|
|
|
Some(pprust::fun_to_str(
|
|
|
|
&method.decl,
|
|
|
|
method.purity,
|
|
|
|
method.ident,
|
2013-04-30 08:49:48 -07:00
|
|
|
Some(method.explicit_self.node),
|
2013-04-16 01:09:55 +10:00
|
|
|
&method.generics,
|
|
|
|
extract::interner()
|
|
|
|
))
|
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
None => fail!("method not found")
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("get_method_sig: item ID not bound to trait or impl")
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-03-25 16:11:02 -07:00
|
|
|
let (bounds, trait_types, self_ty) = {
|
2013-01-10 10:59:58 -08:00
|
|
|
let doc = copy doc;
|
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&doc.id()) {
|
2013-01-10 10:59:58 -08:00
|
|
|
ast_map::node_item(@ast::item {
|
2013-03-25 16:11:02 -07:00
|
|
|
node: ast::item_impl(ref generics, opt_trait_type, self_ty, _), _
|
2013-01-10 10:59:58 -08:00
|
|
|
}, _) => {
|
2013-03-25 16:11:02 -07:00
|
|
|
let bounds = pprust::generics_to_str(generics, extract::interner());
|
|
|
|
let bounds = if bounds.is_empty() { None } else { Some(bounds) };
|
2013-01-10 10:59:58 -08:00
|
|
|
let trait_types = opt_trait_type.map_default(~[], |p| {
|
|
|
|
~[pprust::path_to_str(p.path, extract::interner())]
|
|
|
|
});
|
2013-03-25 16:11:02 -07:00
|
|
|
(bounds,
|
|
|
|
trait_types,
|
2013-01-10 10:59:58 -08:00
|
|
|
Some(pprust::ty_to_str(
|
|
|
|
self_ty, extract::interner())))
|
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("expected impl")
|
2013-01-10 10:59:58 -08:00
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::ImplDoc {
|
2013-03-25 16:11:02 -07:00
|
|
|
bounds_str: bounds,
|
2012-07-18 09:31:53 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::SimpleItemDoc {
|
2013-01-10 10:59:58 -08:00
|
|
|
sig: {
|
|
|
|
let doc = copy doc;
|
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&doc.id()) {
|
2013-01-10 10:59:58 -08:00
|
|
|
ast_map::node_item(@ast::item {
|
|
|
|
ident: ident,
|
|
|
|
node: ast::item_ty(ty, ref params), _
|
|
|
|
}, _) => {
|
|
|
|
Some(fmt!(
|
|
|
|
"type %s%s = %s",
|
|
|
|
to_str(ident),
|
2013-02-14 21:50:03 -08:00
|
|
|
pprust::generics_to_str(params,
|
2013-01-10 10:59:58 -08:00
|
|
|
extract::interner()),
|
|
|
|
pprust::ty_to_str(ty,
|
|
|
|
extract::interner())
|
|
|
|
))
|
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("expected type")
|
2013-01-10 10:59:58 -08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::StructDoc {
|
2013-01-10 10:59:58 -08:00
|
|
|
sig: {
|
|
|
|
let doc = copy doc;
|
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2013-05-12 16:50:57 -04:00
|
|
|
match ctxt.ast_map.get_copy(&doc.id()) {
|
2013-01-10 10:59:58 -08:00
|
|
|
ast_map::node_item(item, _) => {
|
|
|
|
let item = strip_struct_extra_stuff(item);
|
|
|
|
Some(pprust::item_to_str(item,
|
|
|
|
extract::interner()))
|
|
|
|
}
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("not an item")
|
2012-09-19 14:37:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.. doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 15:14:20 -07:00
|
|
|
/// 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 {
|
2013-04-30 21:00:45 -07:00
|
|
|
ast::item_struct(def, tys) => ast::item_struct(def, tys),
|
2013-05-06 00:18:51 +02:00
|
|
|
_ => fail!("not a struct")
|
2012-09-20 14:40:55 -07:00
|
|
|
};
|
|
|
|
|
2013-01-13 13:13:41 -08:00
|
|
|
@ast::item {
|
2012-09-20 15:14:20 -07:00
|
|
|
attrs: ~[], // Remove the attributes
|
2012-09-20 14:40:55 -07:00
|
|
|
node: node,
|
2013-01-30 13:14:35 -08:00
|
|
|
.. copy *item
|
2012-09-20 14:40:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 18:32:37 -08:00
|
|
|
#[cfg(test)]
|
2013-04-16 01:09:55 +10:00
|
|
|
mod test {
|
2013-05-22 19:59:22 -07:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-29 17:38:20 -08:00
|
|
|
use astsrv;
|
|
|
|
use doc;
|
|
|
|
use extract;
|
2013-01-08 19:37:25 -08:00
|
|
|
use tystr_pass::run;
|
2012-12-29 17:38:20 -08:00
|
|
|
|
2013-04-16 01:09:55 +10:00
|
|
|
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-02-20 21:08:19 -08:00
|
|
|
}
|
2012-01-31 18:32:37 -08:00
|
|
|
}
|
2013-04-16 01:09:55 +10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_fn_sig() {
|
|
|
|
let doc = mk_doc(~"fn a<T>() -> int { }");
|
|
|
|
assert!(doc.cratemod().fns()[0].sig == Some(~"fn a<T>() -> int"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_foreign_fn_sig() {
|
2013-05-13 09:23:32 -07:00
|
|
|
let doc = mk_doc(~"extern { fn a<T>() -> int; }");
|
2013-04-16 01:09:55 +10:00
|
|
|
assert!(doc.cratemod().nmods()[0].fns[0].sig ==
|
|
|
|
Some(~"fn a<T>() -> int"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_const_types() {
|
|
|
|
let doc = mk_doc(~"static a: bool = true;");
|
|
|
|
assert!(doc.cratemod().consts()[0].sig == Some(~"bool"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_variant_sigs() {
|
|
|
|
let doc = mk_doc(~"enum a { b(int) }");
|
|
|
|
assert!(doc.cratemod().enums()[0].variants[0].sig ==
|
|
|
|
Some(~"b(int)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_trait_method_sigs() {
|
|
|
|
let doc = mk_doc(~"trait i { fn a<T>(&mut self) -> int; }");
|
|
|
|
assert!(doc.cratemod().traits()[0].methods[0].sig
|
|
|
|
== Some(~"fn a<T>(&mut self) -> int"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_bounds() {
|
|
|
|
let doc = mk_doc(~"impl<T, U: Copy, V: Copy + Clone> Option<T, U, V> { }");
|
|
|
|
assert!(doc.cratemod().impls()[0].bounds_str == Some(~"<T, U: Copy, V: Copy + Clone>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_trait_types() {
|
|
|
|
let doc = mk_doc(~"impl j for int { fn a<T>() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].trait_types[0] == ~"j");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_add_impl_trait_types_if_none() {
|
|
|
|
let doc = mk_doc(~"impl int { fn a() { } }");
|
2013-05-14 18:52:12 +09:00
|
|
|
assert!(doc.cratemod().impls()[0].trait_types.len() == 0);
|
2013-04-16 01:09:55 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_self_ty() {
|
|
|
|
let doc = mk_doc(~"impl int { fn a() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].self_ty == Some(~"int"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_method_sigs() {
|
|
|
|
let doc = mk_doc(~"impl int { fn a<T>(&self) -> int { fail!() } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].methods[0].sig
|
|
|
|
== Some(~"fn a<T>(&self) -> int"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_type_signatures() {
|
|
|
|
let doc = mk_doc(~"type t<T> = int;");
|
|
|
|
assert!(doc.cratemod().types()[0].sig == Some(~"type t<T> = int"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_struct_defs() {
|
|
|
|
let doc = mk_doc(~"struct S { field: () }");
|
2013-05-22 06:54:35 -04:00
|
|
|
assert!(doc.cratemod().structs()[0].sig.get().contains(
|
2013-04-16 01:09:55 +10:00
|
|
|
"struct S {"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_serialize_struct_attrs() {
|
|
|
|
// All we care about are the fields
|
|
|
|
let doc = mk_doc(~"#[wut] struct S { field: () }");
|
2013-05-22 06:54:35 -04:00
|
|
|
assert!(!doc.cratemod().structs()[0].sig.get().contains("wut"));
|
2013-04-16 01:09:55 +10:00
|
|
|
}
|
2012-01-30 11:52:34 +01:00
|
|
|
}
|