1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Pulls type information out of the AST and attaches it to the document
23 use syntax::print::pprust;
26 pub fn mk_pass() -> Pass {
40 fold_const: fold_const,
42 fold_trait: fold_trait,
45 fold_struct: fold_struct,
46 .. fold::default_any_fold(srv)
48 (fold.fold_doc)(&fold, doc)
52 fold: &fold::Fold<astsrv::Srv>,
56 let srv = fold.ctxt.clone();
59 sig: get_fn_sig(srv, doc.id()),
64 fn get_fn_sig(srv: astsrv::Srv, fn_id: doc::AstId) -> Option<~str> {
65 do astsrv::exec(srv) |ctxt| {
66 match *ctxt.ast_map.get(&fn_id) {
67 ast_map::node_item(@ast::item {
69 node: ast::item_fn(ref decl, purity, _, ref tys, _), _
71 ast_map::node_foreign_item(@ast::foreign_item {
73 node: ast::foreign_item_fn(ref decl, purity, ref tys), _
75 Some(pprust::fun_to_str(decl, purity, ident, None, tys,
78 _ => fail!(~"get_fn_sig: fn_id not bound to a fn item")
84 fold: &fold::Fold<astsrv::Srv>,
87 let srv = fold.ctxt.clone();
92 do astsrv::exec(srv) |ctxt| {
93 match *ctxt.ast_map.get(&doc.id()) {
94 ast_map::node_item(@ast::item {
95 node: ast::item_const(ty, _), _
97 pprust::ty_to_str(ty, extract::interner())
99 _ => fail!(~"fold_const: id not bound to a const item")
107 fold: &fold::Fold<astsrv::Srv>,
110 let doc_id = doc.id();
111 let srv = fold.ctxt.clone();
114 variants: do vec::map(doc.variants) |variant| {
116 let variant = copy *variant;
117 do astsrv::exec(srv.clone()) |ctxt| {
118 match *ctxt.ast_map.get(&doc_id) {
119 ast_map::node_item(@ast::item {
120 node: ast::item_enum(ref enum_definition, _), _
123 do vec::find(enum_definition.variants) |v| {
124 to_str(v.node.name) == variant.name
127 pprust::variant_to_str(
128 ast_variant, extract::interner())
130 _ => fail!(~"enum variant not bound to an enum item")
145 fold: &fold::Fold<astsrv::Srv>,
149 methods: merge_methods(fold.ctxt.clone(), doc.id(), copy doc.methods),
157 docs: ~[doc::MethodDoc]
158 ) -> ~[doc::MethodDoc] {
159 do vec::map(docs) |doc| {
161 sig: get_method_sig(srv.clone(), item_id, copy doc.name),
172 do astsrv::exec(srv) |ctxt| {
173 match *ctxt.ast_map.get(&item_id) {
174 ast_map::node_item(@ast::item {
175 node: ast::item_trait(_, _, ref methods), _
177 match vec::find(*methods, |method| {
179 ast::required(ty_m) => to_str(ty_m.ident) == method_name,
180 ast::provided(m) => to_str(m.ident) == method_name,
185 ast::required(ty_m) => {
186 Some(pprust::fun_to_str(
190 Some(ty_m.self_ty.node),
195 ast::provided(m) => {
196 Some(pprust::fun_to_str(
200 Some(m.self_ty.node),
207 _ => fail!(~"method not found")
210 ast_map::node_item(@ast::item {
211 node: ast::item_impl(_, _, _, ref methods), _
213 match vec::find(*methods, |method| {
214 to_str(method.ident) == method_name
217 Some(pprust::fun_to_str(
221 Some(method.self_ty.node),
226 None => fail!(~"method not found")
229 _ => fail!(~"get_method_sig: item ID not bound to trait or impl")
235 fold: &fold::Fold<astsrv::Srv>,
239 let srv = fold.ctxt.clone();
241 let (bounds, trait_types, self_ty) = {
243 do astsrv::exec(srv) |ctxt| {
244 match *ctxt.ast_map.get(&doc.id()) {
245 ast_map::node_item(@ast::item {
246 node: ast::item_impl(ref generics, opt_trait_type, self_ty, _), _
248 let bounds = pprust::generics_to_str(generics, extract::interner());
249 let bounds = if bounds.is_empty() { None } else { Some(bounds) };
250 let trait_types = opt_trait_type.map_default(~[], |p| {
251 ~[pprust::path_to_str(p.path, extract::interner())]
255 Some(pprust::ty_to_str(
256 self_ty, extract::interner())))
258 _ => fail!(~"expected impl")
265 trait_types: trait_types,
267 methods: merge_methods(fold.ctxt.clone(), doc.id(), copy doc.methods),
273 fold: &fold::Fold<astsrv::Srv>,
277 let srv = fold.ctxt.clone();
282 do astsrv::exec(srv) |ctxt| {
283 match *ctxt.ast_map.get(&doc.id()) {
284 ast_map::node_item(@ast::item {
286 node: ast::item_ty(ty, ref params), _
291 pprust::generics_to_str(params,
292 extract::interner()),
293 pprust::ty_to_str(ty,
297 _ => fail!(~"expected type")
306 fold: &fold::Fold<astsrv::Srv>,
308 ) -> doc::StructDoc {
309 let srv = fold.ctxt.clone();
314 do astsrv::exec(srv) |ctxt| {
315 match *ctxt.ast_map.get(&doc.id()) {
316 ast_map::node_item(item, _) => {
317 let item = strip_struct_extra_stuff(item);
318 Some(pprust::item_to_str(item,
319 extract::interner()))
321 _ => fail!(~"not an item")
329 /// Removes various things from the struct item definition that
330 /// shouldn't be displayed in the struct signature. Probably there
331 /// should be a simple pprust::struct_to_str function that does
332 /// what I actually want
333 fn strip_struct_extra_stuff(item: @ast::item) -> @ast::item {
334 let node = match copy item.node {
335 ast::item_struct(def, tys) => ast::item_struct(def, tys),
336 _ => fail!(~"not a struct")
340 attrs: ~[], // Remove the attributes
353 fn mk_doc(source: ~str) -> doc::Doc {
354 do astsrv::from_str(copy source) |srv| {
355 let doc = extract::from_srv(srv.clone(), ~"");
356 run(srv.clone(), doc)
361 fn should_add_fn_sig() {
362 let doc = mk_doc(~"fn a<T>() -> int { }");
363 assert!(doc.cratemod().fns()[0].sig == Some(~"fn a<T>() -> int"));
367 fn should_add_foreign_fn_sig() {
368 let doc = mk_doc(~"extern mod a { fn a<T>() -> int; }");
369 assert!(doc.cratemod().nmods()[0].fns[0].sig ==
370 Some(~"fn a<T>() -> int"));
374 fn should_add_const_types() {
375 let doc = mk_doc(~"static a: bool = true;");
376 assert!(doc.cratemod().consts()[0].sig == Some(~"bool"));
380 fn should_add_variant_sigs() {
381 let doc = mk_doc(~"enum a { b(int) }");
382 assert!(doc.cratemod().enums()[0].variants[0].sig ==
387 fn should_add_trait_method_sigs() {
388 let doc = mk_doc(~"trait i { fn a<T>(&mut self) -> int; }");
389 assert!(doc.cratemod().traits()[0].methods[0].sig
390 == Some(~"fn a<T>(&mut self) -> int"));
394 fn should_add_impl_bounds() {
395 let doc = mk_doc(~"impl<T, U: Copy, V: Copy + Clone> Option<T, U, V> { }");
396 assert!(doc.cratemod().impls()[0].bounds_str == Some(~"<T, U: Copy, V: Copy + Clone>"));
400 fn should_add_impl_trait_types() {
401 let doc = mk_doc(~"impl j for int { fn a<T>() { } }");
402 assert!(doc.cratemod().impls()[0].trait_types[0] == ~"j");
406 fn should_not_add_impl_trait_types_if_none() {
407 let doc = mk_doc(~"impl int { fn a() { } }");
408 assert!(vec::len(doc.cratemod().impls()[0].trait_types) == 0);
412 fn should_add_impl_self_ty() {
413 let doc = mk_doc(~"impl int { fn a() { } }");
414 assert!(doc.cratemod().impls()[0].self_ty == Some(~"int"));
418 fn should_add_impl_method_sigs() {
419 let doc = mk_doc(~"impl int { fn a<T>(&self) -> int { fail!() } }");
420 assert!(doc.cratemod().impls()[0].methods[0].sig
421 == Some(~"fn a<T>(&self) -> int"));
425 fn should_add_type_signatures() {
426 let doc = mk_doc(~"type t<T> = int;");
427 assert!(doc.cratemod().types()[0].sig == Some(~"type t<T> = int"));
431 fn should_add_struct_defs() {
432 let doc = mk_doc(~"struct S { field: () }");
433 assert!((&doc.cratemod().structs()[0].sig).get().contains(
438 fn should_not_serialize_struct_attrs() {
439 // All we care about are the fields
440 let doc = mk_doc(~"#[wut] struct S { field: () }");
441 assert!(!(&doc.cratemod().structs()[0].sig).get().contains("wut"));