2012-01-17 17:44:32 -08:00
|
|
|
#[doc =
|
|
|
|
"Pulls type information out of the AST and attaches it to the document"];
|
|
|
|
|
2012-01-16 15:33:06 -08:00
|
|
|
import rustc::syntax::ast;
|
2012-01-16 18:21:34 -08:00
|
|
|
import rustc::syntax::print::pprust;
|
|
|
|
import rustc::middle::ast_map;
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-01-17 16:12:50 -08:00
|
|
|
export mk_pass;
|
|
|
|
|
|
|
|
fn mk_pass() -> pass {
|
|
|
|
run
|
|
|
|
}
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-01-16 15:44:10 -08:00
|
|
|
fn run(
|
2012-01-17 16:12:31 -08:00
|
|
|
srv: astsrv::srv,
|
2012-01-16 18:21:34 -08:00
|
|
|
doc: doc::cratedoc
|
2012-01-16 15:44:10 -08:00
|
|
|
) -> doc::cratedoc {
|
2012-01-16 15:33:06 -08:00
|
|
|
let fold = fold::fold({
|
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-01-30 15:44:21 -08:00
|
|
|
fold_res: fold_res,
|
2012-01-31 16:35:54 -08:00
|
|
|
fold_iface: fold_iface,
|
2012-02-01 22:41:41 -08:00
|
|
|
fold_impl: fold_impl,
|
|
|
|
fold_type: fold_type
|
2012-01-16 18:21:34 -08:00
|
|
|
with *fold::default_seq_fold(srv)
|
2012-01-16 15:33:06 -08:00
|
|
|
});
|
|
|
|
fold.fold_crate(fold, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_fn(
|
2012-01-17 16:12:31 -08:00
|
|
|
fold: fold::fold<astsrv::srv>,
|
2012-01-16 15:33:06 -08:00
|
|
|
doc: doc::fndoc
|
|
|
|
) -> doc::fndoc {
|
|
|
|
|
2012-01-16 18:21:34 -08:00
|
|
|
let srv = fold.ctxt;
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-02-17 14:46:30 -08:00
|
|
|
args: merge_arg_tys(srv, doc.id(), doc.args),
|
|
|
|
return: merge_ret_ty(srv, doc.id(), doc.return),
|
|
|
|
sig: get_fn_sig(srv, doc.id())
|
2012-01-30 13:05:25 -08:00
|
|
|
with 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-01-19 18:09:19 -08:00
|
|
|
fn get_fn_sig(srv: astsrv::srv, fn_id: doc::ast_id) -> option<str> {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check ctxt.ast_map.get(fn_id) {
|
2012-01-19 18:09:19 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
ident: ident,
|
|
|
|
node: ast::item_fn(decl, _, blk), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-19 18:09:19 -08:00
|
|
|
some(pprust::fun_to_str(decl, ident, []))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_fn_sig() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("fn a() -> int { }");
|
2012-01-29 12:43:21 -08:00
|
|
|
assert doc.topmod.fns()[0].sig == some("fn a() -> int");
|
2012-01-19 18:09:19 -08:00
|
|
|
}
|
|
|
|
|
2012-01-18 16:09:32 -08:00
|
|
|
fn merge_ret_ty(
|
2012-01-18 17:05:03 -08:00
|
|
|
srv: astsrv::srv,
|
|
|
|
fn_id: doc::ast_id,
|
2012-01-19 19:14:12 -08:00
|
|
|
doc: doc::retdoc
|
|
|
|
) -> doc::retdoc {
|
|
|
|
alt get_ret_ty(srv, fn_id) {
|
|
|
|
some(ty) {
|
|
|
|
{
|
|
|
|
ty: some(ty)
|
|
|
|
with doc
|
2012-01-18 21:50:35 -08:00
|
|
|
}
|
2012-01-18 16:09:32 -08:00
|
|
|
}
|
2012-01-19 19:14:12 -08:00
|
|
|
none { doc }
|
2012-01-16 15:33:06 -08:00
|
|
|
}
|
|
|
|
}
|
2012-01-18 16:09:32 -08:00
|
|
|
|
2012-01-19 18:09:19 -08:00
|
|
|
fn get_ret_ty(srv: astsrv::srv, fn_id: doc::ast_id) -> option<str> {
|
2012-01-18 17:05:03 -08:00
|
|
|
astsrv::exec(srv) {|ctxt|
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check ctxt.ast_map.get(fn_id) {
|
2012-01-18 17:05:03 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_fn(decl, _, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-30 15:44:21 -08:00
|
|
|
ret_ty_to_str(decl)
|
2012-01-18 17:05:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 15:44:21 -08:00
|
|
|
fn ret_ty_to_str(decl: ast::fn_decl) -> option<str> {
|
|
|
|
if decl.output.node != ast::ty_nil {
|
|
|
|
some(pprust::ty_to_str(decl.output))
|
|
|
|
} else {
|
|
|
|
// Nil-typed return values are not interesting
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-18 16:09:32 -08:00
|
|
|
#[test]
|
|
|
|
fn should_add_fn_ret_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("fn a() -> int { }");
|
2012-01-29 12:43:21 -08:00
|
|
|
assert doc.topmod.fns()[0].return.ty == some("int");
|
2012-01-18 16:09:32 -08:00
|
|
|
}
|
|
|
|
|
2012-01-18 21:50:35 -08:00
|
|
|
#[test]
|
|
|
|
fn should_not_add_nil_ret_type() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("fn a() { }");
|
2012-01-29 12:43:21 -08:00
|
|
|
assert doc.topmod.fns()[0].return.ty == none;
|
2012-01-18 21:50:35 -08:00
|
|
|
}
|
|
|
|
|
2012-01-18 17:05:03 -08:00
|
|
|
fn merge_arg_tys(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
fn_id: doc::ast_id,
|
|
|
|
args: [doc::argdoc]
|
|
|
|
) -> [doc::argdoc] {
|
|
|
|
let tys = get_arg_tys(srv, fn_id);
|
|
|
|
vec::map2(args, tys) {|arg, ty|
|
|
|
|
// Sanity check that we're talking about the same args
|
|
|
|
assert arg.name == tuple::first(ty);
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-01-18 17:05:03 -08:00
|
|
|
ty: some(tuple::second(ty))
|
2012-01-30 13:05:25 -08:00
|
|
|
with arg
|
2012-01-18 17:05:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_arg_tys(srv: astsrv::srv, fn_id: doc::ast_id) -> [(str, str)] {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check ctxt.ast_map.get(fn_id) {
|
2012-01-18 17:05:03 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_fn(decl, _, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) |
|
2012-01-26 22:45:28 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_res(decl, _, _, _, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-30 15:44:21 -08:00
|
|
|
decl_arg_tys(decl)
|
2012-01-18 17:05:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 15:44:21 -08:00
|
|
|
fn decl_arg_tys(decl: ast::fn_decl) -> [(str, str)] {
|
|
|
|
vec::map(decl.inputs) {|arg|
|
|
|
|
(arg.ident, pprust::ty_to_str(arg.ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-18 17:05:03 -08:00
|
|
|
#[test]
|
|
|
|
fn should_add_arg_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("fn a(b: int, c: bool) { }");
|
2012-01-29 12:43:21 -08:00
|
|
|
let fn_ = doc.topmod.fns()[0];
|
2012-01-18 17:05:03 -08:00
|
|
|
assert fn_.args[0].ty == some("int");
|
|
|
|
assert fn_.args[1].ty == some("bool");
|
2012-01-24 00:14:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_const(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::constdoc
|
|
|
|
) -> doc::constdoc {
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-01-24 00:14:31 -08:00
|
|
|
ty: some(astsrv::exec(srv) {|ctxt|
|
2012-02-17 14:46:30 -08:00
|
|
|
alt check ctxt.ast_map.get(doc.id()) {
|
2012-01-24 00:14:31 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_const(ty, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-24 00:14:31 -08:00
|
|
|
pprust::ty_to_str(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2012-01-30 13:05:25 -08:00
|
|
|
with doc
|
2012-01-24 00:14:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_const_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("const a: bool = true;");
|
2012-01-28 15:45:19 -08:00
|
|
|
assert doc.topmod.consts()[0].ty == some("bool");
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_enum(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::enumdoc
|
|
|
|
) -> doc::enumdoc {
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-01-25 17:22:19 -08:00
|
|
|
variants: vec::map(doc.variants) {|variant|
|
|
|
|
let sig = astsrv::exec(srv) {|ctxt|
|
2012-02-17 14:46:30 -08:00
|
|
|
alt check ctxt.ast_map.get(doc.id()) {
|
2012-01-25 17:22:19 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_enum(ast_variants, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-25 17:22:19 -08:00
|
|
|
let ast_variant = option::get(
|
|
|
|
vec::find(ast_variants) {|v|
|
|
|
|
v.node.name == variant.name
|
|
|
|
});
|
|
|
|
|
|
|
|
pprust::variant_to_str(ast_variant)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-01-25 17:22:19 -08:00
|
|
|
sig: some(sig)
|
2012-01-30 13:05:25 -08:00
|
|
|
with variant
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
}
|
2012-01-30 13:05:25 -08:00
|
|
|
with doc
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_variant_sigs() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("enum a { b(int) }");
|
2012-01-28 15:04:36 -08:00
|
|
|
assert doc.topmod.enums()[0].variants[0].sig == some("b(int)");
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
2012-01-26 20:59:15 -08:00
|
|
|
|
|
|
|
fn fold_res(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::resdoc
|
|
|
|
) -> doc::resdoc {
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-02-17 14:46:30 -08:00
|
|
|
args: merge_arg_tys(srv, doc.id(), doc.args),
|
2012-01-26 20:59:15 -08:00
|
|
|
sig: some(astsrv::exec(srv) {|ctxt|
|
2012-02-17 14:46:30 -08:00
|
|
|
alt check ctxt.ast_map.get(doc.id()) {
|
2012-01-26 20:59:15 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_res(decl, _, _, _, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-02-17 14:46:30 -08:00
|
|
|
pprust::res_to_str(decl, doc.name(), [])
|
2012-01-26 20:59:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2012-01-30 13:05:25 -08:00
|
|
|
with doc
|
2012-01-26 20:59:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_resource_sigs() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("resource r(b: bool) { }");
|
2012-01-28 14:36:35 -08:00
|
|
|
assert doc.topmod.resources()[0].sig == some("resource r(b: bool)");
|
2012-01-26 20:59:15 -08:00
|
|
|
}
|
2012-01-26 22:45:28 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_resource_arg_tys() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("resource r(a: bool) { }");
|
2012-01-28 14:36:35 -08:00
|
|
|
assert doc.topmod.resources()[0].args[0].ty == some("bool");
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_iface(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::ifacedoc
|
|
|
|
) -> doc::ifacedoc {
|
|
|
|
{
|
2012-02-17 14:46:30 -08:00
|
|
|
methods: merge_methods(fold.ctxt, doc.id(), doc.methods)
|
2012-01-30 15:44:21 -08:00
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 16:35:54 -08:00
|
|
|
fn merge_methods(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_id: doc::ast_id,
|
|
|
|
docs: [doc::methoddoc]
|
|
|
|
) -> [doc::methoddoc] {
|
|
|
|
vec::map(docs) {|doc|
|
|
|
|
{
|
|
|
|
args: merge_method_arg_tys(
|
|
|
|
srv,
|
|
|
|
item_id,
|
|
|
|
doc.args,
|
|
|
|
doc.name),
|
|
|
|
return: merge_method_ret_ty(
|
|
|
|
srv,
|
|
|
|
item_id,
|
|
|
|
doc.return,
|
|
|
|
doc.name),
|
|
|
|
sig: get_method_sig(srv, item_id, doc.name)
|
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 15:44:21 -08:00
|
|
|
fn merge_method_ret_ty(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_id: doc::ast_id,
|
|
|
|
doc: doc::retdoc,
|
|
|
|
method_name: str
|
|
|
|
) -> doc::retdoc {
|
|
|
|
alt get_method_ret_ty(srv, item_id, method_name) {
|
|
|
|
some(ty) {
|
|
|
|
{
|
|
|
|
ty: some(ty)
|
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
none { doc }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_method_ret_ty(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_id: doc::ast_id,
|
|
|
|
method_name: str
|
|
|
|
) -> option<str> {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
|
|
|
alt ctxt.ast_map.get(item_id) {
|
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_iface(_, methods), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check vec::find(methods) {|method|
|
2012-01-30 15:44:21 -08:00
|
|
|
method.ident == method_name
|
|
|
|
} {
|
|
|
|
some(method) {
|
|
|
|
ret_ty_to_str(method.decl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_impl(_, _, _, methods), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check vec::find(methods) {|method|
|
2012-01-31 16:35:54 -08:00
|
|
|
method.ident == method_name
|
|
|
|
} {
|
|
|
|
some(method) {
|
|
|
|
ret_ty_to_str(method.decl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { fail }
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_method_sig(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_id: doc::ast_id,
|
|
|
|
method_name: str
|
|
|
|
) -> option<str> {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check ctxt.ast_map.get(item_id) {
|
2012-01-30 15:44:21 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_iface(_, methods), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check vec::find(methods) {|method|
|
2012-01-30 15:44:21 -08:00
|
|
|
method.ident == method_name
|
|
|
|
} {
|
|
|
|
some(method) {
|
|
|
|
some(pprust::fun_to_str(method.decl, method.ident, []))
|
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_impl(_, _, _, methods), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-02-15 15:42:35 +01:00
|
|
|
alt check vec::find(methods) {|method|
|
2012-01-31 16:35:54 -08:00
|
|
|
method.ident == method_name
|
|
|
|
} {
|
|
|
|
some(method) {
|
|
|
|
some(pprust::fun_to_str(method.decl, method.ident, []))
|
|
|
|
}
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_method_arg_tys(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_id: doc::ast_id,
|
|
|
|
args: [doc::argdoc],
|
|
|
|
method_name: str
|
|
|
|
) -> [doc::argdoc] {
|
|
|
|
let tys = get_method_arg_tys(srv, item_id, method_name);
|
|
|
|
vec::map2(args, tys) {|arg, ty|
|
|
|
|
assert arg.name == tuple::first(ty);
|
|
|
|
{
|
|
|
|
ty: some(tuple::second(ty))
|
|
|
|
with arg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_method_arg_tys(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_id: doc::ast_id,
|
|
|
|
method_name: str
|
|
|
|
) -> [(str, str)] {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
|
|
|
alt ctxt.ast_map.get(item_id) {
|
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_iface(_, methods), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-30 15:44:21 -08:00
|
|
|
alt vec::find(methods) {|method|
|
|
|
|
method.ident == method_name
|
|
|
|
} {
|
|
|
|
some(method) {
|
|
|
|
decl_arg_tys(method.decl)
|
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
_ { fail "get_method_arg_tys: expected method"; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_impl(_, _, _, methods), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-31 16:35:54 -08:00
|
|
|
alt vec::find(methods) {|method|
|
|
|
|
method.ident == method_name
|
|
|
|
} {
|
|
|
|
some(method) {
|
|
|
|
decl_arg_tys(method.decl)
|
|
|
|
}
|
|
|
|
_ { fail "get_method_arg_tys: expected method"; }
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
_ { fail }
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_iface_method_sigs() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("iface i { fn a() -> int; }");
|
2012-01-30 15:44:21 -08:00
|
|
|
assert doc.topmod.ifaces()[0].methods[0].sig == some("fn a() -> int");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_iface_method_ret_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("iface i { fn a() -> int; }");
|
2012-01-30 15:44:21 -08:00
|
|
|
assert doc.topmod.ifaces()[0].methods[0].return.ty == some("int");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_add_iface_method_nil_ret_type() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("iface i { fn a(); }");
|
2012-01-30 15:44:21 -08:00
|
|
|
assert doc.topmod.ifaces()[0].methods[0].return.ty == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_iface_method_arg_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("iface i { fn a(b: int, c: bool); }");
|
2012-01-30 15:44:21 -08:00
|
|
|
let fn_ = doc.topmod.ifaces()[0].methods[0];
|
|
|
|
assert fn_.args[0].ty == some("int");
|
|
|
|
assert fn_.args[1].ty == some("bool");
|
|
|
|
}
|
2012-01-31 16:35:54 -08:00
|
|
|
|
|
|
|
fn fold_impl(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::impldoc
|
|
|
|
) -> doc::impldoc {
|
|
|
|
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
|
|
|
let (iface_ty, self_ty) = astsrv::exec(srv) {|ctxt|
|
2012-02-17 14:46:30 -08:00
|
|
|
alt ctxt.ast_map.get(doc.id()) {
|
2012-01-31 16:35:54 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_impl(_, iface_ty, self_ty, _), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-01-31 16:35:54 -08:00
|
|
|
let iface_ty = option::map(iface_ty) {|iface_ty|
|
|
|
|
pprust::ty_to_str(iface_ty)
|
|
|
|
};
|
|
|
|
(iface_ty, some(pprust::ty_to_str(self_ty)))
|
|
|
|
}
|
|
|
|
_ { fail "expected impl" }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
iface_ty: iface_ty,
|
|
|
|
self_ty: self_ty,
|
2012-02-17 14:46:30 -08:00
|
|
|
methods: merge_methods(fold.ctxt, doc.id(), doc.methods)
|
2012-01-31 16:35:54 -08:00
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_iface_ty() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i of j for int { fn a() { } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
assert doc.topmod.impls()[0].iface_ty == some("j");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_add_impl_iface_ty_if_none() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i for int { fn a() { } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
assert doc.topmod.impls()[0].iface_ty == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_self_ty() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i for int { fn a() { } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
assert doc.topmod.impls()[0].self_ty == some("int");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_method_sigs() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i for int { fn a() -> int { fail } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
assert doc.topmod.impls()[0].methods[0].sig == some("fn a() -> int");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_method_ret_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i for int { fn a() -> int { fail } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
assert doc.topmod.impls()[0].methods[0].return.ty == some("int");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_add_impl_method_nil_ret_type() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i for int { fn a() { } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
assert doc.topmod.impls()[0].methods[0].return.ty == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_method_arg_types() {
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = test::mk_doc("impl i for int { fn a(b: int, c: bool) { } }");
|
2012-01-31 16:35:54 -08:00
|
|
|
let fn_ = doc.topmod.impls()[0].methods[0];
|
|
|
|
assert fn_.args[0].ty == some("int");
|
|
|
|
assert fn_.args[1].ty == some("bool");
|
|
|
|
}
|
2012-01-31 18:32:37 -08:00
|
|
|
|
2012-02-01 22:41:41 -08:00
|
|
|
fn fold_type(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::tydoc
|
|
|
|
) -> doc::tydoc {
|
|
|
|
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
|
|
|
{
|
|
|
|
sig: astsrv::exec(srv) {|ctxt|
|
2012-02-17 14:46:30 -08:00
|
|
|
alt ctxt.ast_map.get(doc.id()) {
|
2012-02-01 22:41:41 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
ident: ident,
|
|
|
|
node: ast::item_ty(ty, params), _
|
2012-02-03 09:53:37 +01:00
|
|
|
}, _) {
|
2012-02-01 22:41:41 -08:00
|
|
|
some(#fmt(
|
|
|
|
"type %s%s = %s",
|
|
|
|
ident,
|
|
|
|
pprust::typarams_to_str(params),
|
|
|
|
pprust::ty_to_str(ty)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ { fail "expected type" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_type_signatures() {
|
|
|
|
let doc = test::mk_doc("type t<T> = int;");
|
|
|
|
assert doc.topmod.types()[0].sig == some("type t<T> = int");
|
|
|
|
}
|
|
|
|
|
2012-01-31 18:32:37 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
fn mk_doc(source: str) -> doc::cratedoc {
|
2012-02-20 20:44:33 -08:00
|
|
|
let srv = astsrv::from_str(source);
|
2012-01-31 18:32:37 -08:00
|
|
|
let doc = extract::from_srv(srv, "");
|
|
|
|
run(srv, doc)
|
|
|
|
}
|
2012-01-30 11:52:34 +01:00
|
|
|
}
|