]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ast_util.rs
return &mut T from the arenas, not &T
[rust.git] / src / libsyntax / ast_util.rs
1 // Copyright 2012-2014 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.
4 //
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.
10
11 use abi::Abi;
12 use ast::*;
13 use ast;
14 use ast_util;
15 use codemap;
16 use codemap::Span;
17 use owned_slice::OwnedSlice;
18 use parse::token;
19 use print::pprust;
20 use ptr::P;
21 use visit::Visitor;
22 use visit;
23
24 use std::cell::Cell;
25 use std::cmp;
26 use std::u32;
27
28 pub fn path_name_i(idents: &[Ident]) -> String {
29     // FIXME: Bad copies (#2543 -- same for everything else that says "bad")
30     idents.iter().map(|i| {
31         token::get_ident(*i).get().to_string()
32     }).collect::<Vec<String>>().connect("::")
33 }
34
35 pub fn local_def(id: NodeId) -> DefId {
36     ast::DefId { krate: LOCAL_CRATE, node: id }
37 }
38
39 pub fn is_local(did: ast::DefId) -> bool { did.krate == LOCAL_CRATE }
40
41 pub fn stmt_id(s: &Stmt) -> NodeId {
42     match s.node {
43       StmtDecl(_, id) => id,
44       StmtExpr(_, id) => id,
45       StmtSemi(_, id) => id,
46       StmtMac(..) => fail!("attempted to analyze unexpanded stmt")
47     }
48 }
49
50 pub fn binop_to_string(op: BinOp) -> &'static str {
51     match op {
52         BiAdd => "+",
53         BiSub => "-",
54         BiMul => "*",
55         BiDiv => "/",
56         BiRem => "%",
57         BiAnd => "&&",
58         BiOr => "||",
59         BiBitXor => "^",
60         BiBitAnd => "&",
61         BiBitOr => "|",
62         BiShl => "<<",
63         BiShr => ">>",
64         BiEq => "==",
65         BiLt => "<",
66         BiLe => "<=",
67         BiNe => "!=",
68         BiGe => ">=",
69         BiGt => ">"
70     }
71 }
72
73 pub fn lazy_binop(b: BinOp) -> bool {
74     match b {
75       BiAnd => true,
76       BiOr => true,
77       _ => false
78     }
79 }
80
81 pub fn is_shift_binop(b: BinOp) -> bool {
82     match b {
83       BiShl => true,
84       BiShr => true,
85       _ => false
86     }
87 }
88
89 pub fn unop_to_string(op: UnOp) -> &'static str {
90     match op {
91       UnUniq => "box() ",
92       UnDeref => "*",
93       UnNot => "!",
94       UnNeg => "-",
95     }
96 }
97
98 pub fn is_path(e: P<Expr>) -> bool {
99     return match e.node { ExprPath(_) => true, _ => false };
100 }
101
102 /// Get a string representation of a signed int type, with its value.
103 /// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
104 pub fn int_ty_to_string(t: IntTy, val: Option<i64>) -> String {
105     let s = match t {
106         TyI if val.is_some() => "i",
107         TyI => "int",
108         TyI8 => "i8",
109         TyI16 => "i16",
110         TyI32 => "i32",
111         TyI64 => "i64"
112     };
113
114     match val {
115         // cast to a u64 so we can correctly print INT64_MIN. All integral types
116         // are parsed as u64, so we wouldn't want to print an extra negative
117         // sign.
118         Some(n) => format!("{}{}", n as u64, s),
119         None => s.to_string()
120     }
121 }
122
123 pub fn int_ty_max(t: IntTy) -> u64 {
124     match t {
125         TyI8 => 0x80u64,
126         TyI16 => 0x8000u64,
127         TyI | TyI32 => 0x80000000u64, // actually ni about TyI
128         TyI64 => 0x8000000000000000u64
129     }
130 }
131
132 /// Get a string representation of an unsigned int type, with its value.
133 /// We want to avoid "42uint" in favor of "42u"
134 pub fn uint_ty_to_string(t: UintTy, val: Option<u64>) -> String {
135     let s = match t {
136         TyU if val.is_some() => "u",
137         TyU => "uint",
138         TyU8 => "u8",
139         TyU16 => "u16",
140         TyU32 => "u32",
141         TyU64 => "u64"
142     };
143
144     match val {
145         Some(n) => format!("{}{}", n, s),
146         None => s.to_string()
147     }
148 }
149
150 pub fn uint_ty_max(t: UintTy) -> u64 {
151     match t {
152         TyU8 => 0xffu64,
153         TyU16 => 0xffffu64,
154         TyU | TyU32 => 0xffffffffu64, // actually ni about TyU
155         TyU64 => 0xffffffffffffffffu64
156     }
157 }
158
159 pub fn float_ty_to_string(t: FloatTy) -> String {
160     match t {
161         TyF32 => "f32".to_string(),
162         TyF64 => "f64".to_string(),
163     }
164 }
165
166 // convert a span and an identifier to the corresponding
167 // 1-segment path
168 pub fn ident_to_path(s: Span, identifier: Ident) -> Path {
169     ast::Path {
170         span: s,
171         global: false,
172         segments: vec!(
173             ast::PathSegment {
174                 identifier: identifier,
175                 lifetimes: Vec::new(),
176                 types: OwnedSlice::empty(),
177             }
178         ),
179     }
180 }
181
182 pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> {
183     P(Pat {
184         id: id,
185         node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None),
186         span: s
187     })
188 }
189
190 pub fn name_to_dummy_lifetime(name: Name) -> Lifetime {
191     Lifetime { id: DUMMY_NODE_ID,
192                span: codemap::DUMMY_SP,
193                name: name }
194 }
195
196 /// Generate a "pretty" name for an `impl` from its type and trait.
197 /// This is designed so that symbols of `impl`'d methods give some
198 /// hint of where they came from, (previously they would all just be
199 /// listed as `__extensions__::method_name::hash`, with no indication
200 /// of the type).
201 pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: &Ty) -> Ident {
202     let mut pretty = pprust::ty_to_string(ty);
203     match *trait_ref {
204         Some(ref trait_ref) => {
205             pretty.push('.');
206             pretty.push_str(pprust::path_to_string(&trait_ref.path).as_slice());
207         }
208         None => {}
209     }
210     token::gensym_ident(pretty.as_slice())
211 }
212
213 pub fn trait_method_to_ty_method(method: &Method) -> TypeMethod {
214     match method.node {
215         MethDecl(ident,
216                  ref generics,
217                  abi,
218                  ref explicit_self,
219                  fn_style,
220                  ref decl,
221                  _,
222                  vis) => {
223             TypeMethod {
224                 ident: ident,
225                 attrs: method.attrs.clone(),
226                 fn_style: fn_style,
227                 decl: (*decl).clone(),
228                 generics: generics.clone(),
229                 explicit_self: (*explicit_self).clone(),
230                 id: method.id,
231                 span: method.span,
232                 vis: vis,
233                 abi: abi,
234             }
235         },
236         MethMac(_) => fail!("expected non-macro method declaration")
237     }
238 }
239
240 /// extract a TypeMethod from a TraitItem. if the TraitItem is
241 /// a default, pull out the useful fields to make a TypeMethod
242 //
243 // NB: to be used only after expansion is complete, and macros are gone.
244 pub fn trait_item_to_ty_method(method: &TraitItem) -> TypeMethod {
245     match *method {
246         RequiredMethod(ref m) => (*m).clone(),
247         ProvidedMethod(ref m) => trait_method_to_ty_method(&**m),
248         TypeTraitItem(_) => {
249             fail!("trait_method_to_ty_method(): expected method but found \
250                    typedef")
251         }
252     }
253 }
254
255 pub fn split_trait_methods(trait_methods: &[TraitItem])
256                            -> (Vec<TypeMethod>, Vec<P<Method>> ) {
257     let mut reqd = Vec::new();
258     let mut provd = Vec::new();
259     for trt_method in trait_methods.iter() {
260         match *trt_method {
261             RequiredMethod(ref tm) => reqd.push((*tm).clone()),
262             ProvidedMethod(ref m) => provd.push((*m).clone()),
263             TypeTraitItem(_) => {}
264         }
265     };
266     (reqd, provd)
267 }
268
269 pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
270     match field.node.kind {
271         ast::NamedField(_, v) | ast::UnnamedField(v) => v
272     }
273 }
274
275 /// Maps a binary operator to its precedence
276 pub fn operator_prec(op: ast::BinOp) -> uint {
277   match op {
278       // 'as' sits here with 12
279       BiMul | BiDiv | BiRem     => 11u,
280       BiAdd | BiSub             => 10u,
281       BiShl | BiShr             =>  9u,
282       BiBitAnd                  =>  8u,
283       BiBitXor                  =>  7u,
284       BiBitOr                   =>  6u,
285       BiLt | BiLe | BiGe | BiGt =>  4u,
286       BiEq | BiNe               =>  3u,
287       BiAnd                     =>  2u,
288       BiOr                      =>  1u
289   }
290 }
291
292 /// Precedence of the `as` operator, which is a binary operator
293 /// not appearing in the prior table.
294 #[allow(non_uppercase_statics)]
295 pub static as_prec: uint = 12u;
296
297 pub fn empty_generics() -> Generics {
298     Generics {
299         lifetimes: Vec::new(),
300         ty_params: OwnedSlice::empty(),
301         where_clause: WhereClause {
302             id: DUMMY_NODE_ID,
303             predicates: Vec::new(),
304         }
305     }
306 }
307
308 // ______________________________________________________________________
309 // Enumerating the IDs which appear in an AST
310
311 #[deriving(Encodable, Decodable, Show)]
312 pub struct IdRange {
313     pub min: NodeId,
314     pub max: NodeId,
315 }
316
317 impl IdRange {
318     pub fn max() -> IdRange {
319         IdRange {
320             min: u32::MAX,
321             max: u32::MIN,
322         }
323     }
324
325     pub fn empty(&self) -> bool {
326         self.min >= self.max
327     }
328
329     pub fn add(&mut self, id: NodeId) {
330         self.min = cmp::min(self.min, id);
331         self.max = cmp::max(self.max, id + 1);
332     }
333 }
334
335 pub trait IdVisitingOperation {
336     fn visit_id(&self, node_id: NodeId);
337 }
338
339 /// A visitor that applies its operation to all of the node IDs
340 /// in a visitable thing.
341
342 pub struct IdVisitor<'a, O:'a> {
343     pub operation: &'a O,
344     pub pass_through_items: bool,
345     pub visited_outermost: bool,
346 }
347
348 impl<'a, O: IdVisitingOperation> IdVisitor<'a, O> {
349     fn visit_generics_helper(&self, generics: &Generics) {
350         for type_parameter in generics.ty_params.iter() {
351             self.operation.visit_id(type_parameter.id)
352         }
353         for lifetime in generics.lifetimes.iter() {
354             self.operation.visit_id(lifetime.lifetime.id)
355         }
356     }
357 }
358
359 impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
360     fn visit_mod(&mut self,
361                  module: &Mod,
362                  _: Span,
363                  node_id: NodeId) {
364         self.operation.visit_id(node_id);
365         visit::walk_mod(self, module)
366     }
367
368     fn visit_view_item(&mut self, view_item: &ViewItem) {
369         if !self.pass_through_items {
370             if self.visited_outermost {
371                 return;
372             } else {
373                 self.visited_outermost = true;
374             }
375         }
376         match view_item.node {
377             ViewItemExternCrate(_, _, node_id) => {
378                 self.operation.visit_id(node_id)
379             }
380             ViewItemUse(ref view_path) => {
381                 match view_path.node {
382                     ViewPathSimple(_, _, node_id) |
383                     ViewPathGlob(_, node_id) => {
384                         self.operation.visit_id(node_id)
385                     }
386                     ViewPathList(_, ref paths, node_id) => {
387                         self.operation.visit_id(node_id);
388                         for path in paths.iter() {
389                             self.operation.visit_id(path.node.id())
390                         }
391                     }
392                 }
393             }
394         }
395         visit::walk_view_item(self, view_item);
396         self.visited_outermost = false;
397     }
398
399     fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
400         self.operation.visit_id(foreign_item.id);
401         visit::walk_foreign_item(self, foreign_item)
402     }
403
404     fn visit_item(&mut self, item: &Item) {
405         if !self.pass_through_items {
406             if self.visited_outermost {
407                 return
408             } else {
409                 self.visited_outermost = true
410             }
411         }
412
413         self.operation.visit_id(item.id);
414         match item.node {
415             ItemEnum(ref enum_definition, _) => {
416                 for variant in enum_definition.variants.iter() {
417                     self.operation.visit_id(variant.node.id)
418                 }
419             }
420             _ => {}
421         }
422
423         visit::walk_item(self, item);
424
425         self.visited_outermost = false
426     }
427
428     fn visit_local(&mut self, local: &Local) {
429         self.operation.visit_id(local.id);
430         visit::walk_local(self, local)
431     }
432
433     fn visit_block(&mut self, block: &Block) {
434         self.operation.visit_id(block.id);
435         visit::walk_block(self, block)
436     }
437
438     fn visit_stmt(&mut self, statement: &Stmt) {
439         self.operation.visit_id(ast_util::stmt_id(statement));
440         visit::walk_stmt(self, statement)
441     }
442
443     fn visit_pat(&mut self, pattern: &Pat) {
444         self.operation.visit_id(pattern.id);
445         visit::walk_pat(self, pattern)
446     }
447
448     fn visit_expr(&mut self, expression: &Expr) {
449         self.operation.visit_id(expression.id);
450         visit::walk_expr(self, expression)
451     }
452
453     fn visit_ty(&mut self, typ: &Ty) {
454         self.operation.visit_id(typ.id);
455         match typ.node {
456             TyPath(_, _, id) => self.operation.visit_id(id),
457             _ => {}
458         }
459         visit::walk_ty(self, typ)
460     }
461
462     fn visit_generics(&mut self, generics: &Generics) {
463         self.visit_generics_helper(generics);
464         visit::walk_generics(self, generics)
465     }
466
467     fn visit_fn(&mut self,
468                 function_kind: visit::FnKind<'v>,
469                 function_declaration: &'v FnDecl,
470                 block: &'v Block,
471                 span: Span,
472                 node_id: NodeId) {
473         if !self.pass_through_items {
474             match function_kind {
475                 visit::FkMethod(..) if self.visited_outermost => return,
476                 visit::FkMethod(..) => self.visited_outermost = true,
477                 _ => {}
478             }
479         }
480
481         self.operation.visit_id(node_id);
482
483         match function_kind {
484             visit::FkItemFn(_, generics, _, _) |
485             visit::FkMethod(_, generics, _) => {
486                 self.visit_generics_helper(generics)
487             }
488             visit::FkFnBlock => {}
489         }
490
491         for argument in function_declaration.inputs.iter() {
492             self.operation.visit_id(argument.id)
493         }
494
495         visit::walk_fn(self,
496                         function_kind,
497                         function_declaration,
498                         block,
499                         span);
500
501         if !self.pass_through_items {
502             match function_kind {
503                 visit::FkMethod(..) => self.visited_outermost = false,
504                 _ => {}
505             }
506         }
507     }
508
509     fn visit_struct_field(&mut self, struct_field: &StructField) {
510         self.operation.visit_id(struct_field.node.id);
511         visit::walk_struct_field(self, struct_field)
512     }
513
514     fn visit_struct_def(&mut self,
515                         struct_def: &StructDef,
516                         _: ast::Ident,
517                         _: &ast::Generics,
518                         id: NodeId) {
519         self.operation.visit_id(id);
520         struct_def.ctor_id.map(|ctor_id| self.operation.visit_id(ctor_id));
521         visit::walk_struct_def(self, struct_def);
522     }
523
524     fn visit_trait_item(&mut self, tm: &ast::TraitItem) {
525         match *tm {
526             ast::RequiredMethod(ref m) => self.operation.visit_id(m.id),
527             ast::ProvidedMethod(ref m) => self.operation.visit_id(m.id),
528             ast::TypeTraitItem(ref typ) => self.operation.visit_id(typ.id),
529         }
530         visit::walk_trait_item(self, tm);
531     }
532
533     fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
534         self.operation.visit_id(lifetime.id);
535     }
536
537     fn visit_lifetime_decl(&mut self, def: &'v LifetimeDef) {
538         self.visit_lifetime_ref(&def.lifetime);
539     }
540 }
541
542 pub fn visit_ids_for_inlined_item<O: IdVisitingOperation>(item: &InlinedItem,
543                                                           operation: &O) {
544     let mut id_visitor = IdVisitor {
545         operation: operation,
546         pass_through_items: true,
547         visited_outermost: false,
548     };
549
550     visit::walk_inlined_item(&mut id_visitor, item);
551 }
552
553 struct IdRangeComputingVisitor {
554     result: Cell<IdRange>,
555 }
556
557 impl IdVisitingOperation for IdRangeComputingVisitor {
558     fn visit_id(&self, id: NodeId) {
559         let mut id_range = self.result.get();
560         id_range.add(id);
561         self.result.set(id_range)
562     }
563 }
564
565 pub fn compute_id_range_for_inlined_item(item: &InlinedItem) -> IdRange {
566     let visitor = IdRangeComputingVisitor {
567         result: Cell::new(IdRange::max())
568     };
569     visit_ids_for_inlined_item(item, &visitor);
570     visitor.result.get()
571 }
572
573 pub fn compute_id_range_for_fn_body(fk: visit::FnKind,
574                                     decl: &FnDecl,
575                                     body: &Block,
576                                     sp: Span,
577                                     id: NodeId)
578                                     -> IdRange
579 {
580     /*!
581      * Computes the id range for a single fn body,
582      * ignoring nested items.
583      */
584
585     let visitor = IdRangeComputingVisitor {
586         result: Cell::new(IdRange::max())
587     };
588     let mut id_visitor = IdVisitor {
589         operation: &visitor,
590         pass_through_items: false,
591         visited_outermost: false,
592     };
593     id_visitor.visit_fn(fk, decl, body, sp, id);
594     visitor.result.get()
595 }
596
597 pub fn walk_pat(pat: &Pat, it: |&Pat| -> bool) -> bool {
598     if !it(pat) {
599         return false;
600     }
601
602     match pat.node {
603         PatIdent(_, _, Some(ref p)) => walk_pat(&**p, it),
604         PatStruct(_, ref fields, _) => {
605             fields.iter().all(|field| walk_pat(&*field.node.pat, |p| it(p)))
606         }
607         PatEnum(_, Some(ref s)) | PatTup(ref s) => {
608             s.iter().all(|p| walk_pat(&**p, |p| it(p)))
609         }
610         PatBox(ref s) | PatRegion(ref s) => {
611             walk_pat(&**s, it)
612         }
613         PatVec(ref before, ref slice, ref after) => {
614             before.iter().all(|p| walk_pat(&**p, |p| it(p))) &&
615             slice.iter().all(|p| walk_pat(&**p, |p| it(p))) &&
616             after.iter().all(|p| walk_pat(&**p, |p| it(p)))
617         }
618         PatMac(_) => fail!("attempted to analyze unexpanded pattern"),
619         PatWild(_) | PatLit(_) | PatRange(_, _) | PatIdent(_, _, _) |
620         PatEnum(_, _) => {
621             true
622         }
623     }
624 }
625
626 pub trait EachViewItem {
627     fn each_view_item(&self, f: |&ast::ViewItem| -> bool) -> bool;
628 }
629
630 struct EachViewItemData<'a> {
631     callback: |&ast::ViewItem|: 'a -> bool,
632 }
633
634 impl<'a, 'v> Visitor<'v> for EachViewItemData<'a> {
635     fn visit_view_item(&mut self, view_item: &ast::ViewItem) {
636         let _ = (self.callback)(view_item);
637     }
638 }
639
640 impl EachViewItem for ast::Crate {
641     fn each_view_item(&self, f: |&ast::ViewItem| -> bool) -> bool {
642         let mut visit = EachViewItemData {
643             callback: f,
644         };
645         visit::walk_crate(&mut visit, self);
646         true
647     }
648 }
649
650 pub fn view_path_id(p: &ViewPath) -> NodeId {
651     match p.node {
652         ViewPathSimple(_, _, id) | ViewPathGlob(_, id)
653         | ViewPathList(_, _, id) => id
654     }
655 }
656
657 /// Returns true if the given struct def is tuple-like; i.e. that its fields
658 /// are unnamed.
659 pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool {
660     struct_def.ctor_id.is_some()
661 }
662
663 /// Returns true if the given pattern consists solely of an identifier
664 /// and false otherwise.
665 pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
666     match pat.node {
667         ast::PatIdent(..) => true,
668         _ => false,
669     }
670 }
671
672 // are two paths equal when compared unhygienically?
673 // since I'm using this to replace ==, it seems appropriate
674 // to compare the span, global, etc. fields as well.
675 pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool {
676     (a.span == b.span)
677     && (a.global == b.global)
678     && (segments_name_eq(a.segments.as_slice(), b.segments.as_slice()))
679 }
680
681 // are two arrays of segments equal when compared unhygienically?
682 pub fn segments_name_eq(a : &[ast::PathSegment], b : &[ast::PathSegment]) -> bool {
683     if a.len() != b.len() {
684         false
685     } else {
686         for (idx,seg) in a.iter().enumerate() {
687             if (seg.identifier.name != b[idx].identifier.name)
688                 // FIXME #7743: ident -> name problems in lifetime comparison?
689                 || (seg.lifetimes != b[idx].lifetimes)
690                 // can types contain idents?
691                 || (seg.types != b[idx].types) {
692                 return false;
693             }
694         }
695         true
696     }
697 }
698
699 /// Returns true if this literal is a string and false otherwise.
700 pub fn lit_is_str(lit: &Lit) -> bool {
701     match lit.node {
702         LitStr(..) => true,
703         _ => false,
704     }
705 }
706
707 /// Macro invocations are guaranteed not to occur after expansion is complete.
708 /// Extracting fields of a method requires a dynamic check to make sure that it's
709 /// not a macro invocation. This check is guaranteed to succeed, assuming
710 /// that the invocations are indeed gone.
711 pub trait PostExpansionMethod {
712     fn pe_ident(&self) -> ast::Ident;
713     fn pe_generics<'a>(&'a self) -> &'a ast::Generics;
714     fn pe_abi(&self) -> Abi;
715     fn pe_explicit_self<'a>(&'a self) -> &'a ast::ExplicitSelf;
716     fn pe_fn_style(&self) -> ast::FnStyle;
717     fn pe_fn_decl<'a>(&'a self) -> &'a ast::FnDecl;
718     fn pe_body<'a>(&'a self) -> &'a ast::Block;
719     fn pe_vis(&self) -> ast::Visibility;
720 }
721
722 macro_rules! mf_method{
723     ($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:expr) => {
724         fn $meth_name<'a>(&'a self) -> $field_ty {
725             match self.node {
726                 $field_pat => $result,
727                 MethMac(_) => {
728                     fail!("expected an AST without macro invocations");
729                 }
730             }
731         }
732     }
733 }
734
735
736 impl PostExpansionMethod for Method {
737     mf_method!(pe_ident,ast::Ident,MethDecl(ident,_,_,_,_,_,_,_),ident)
738     mf_method!(pe_generics,&'a ast::Generics,
739                MethDecl(_,ref generics,_,_,_,_,_,_),generics)
740     mf_method!(pe_abi,Abi,MethDecl(_,_,abi,_,_,_,_,_),abi)
741     mf_method!(pe_explicit_self,&'a ast::ExplicitSelf,
742                MethDecl(_,_,_,ref explicit_self,_,_,_,_),explicit_self)
743     mf_method!(pe_fn_style,ast::FnStyle,MethDecl(_,_,_,_,fn_style,_,_,_),fn_style)
744     mf_method!(pe_fn_decl,&'a ast::FnDecl,MethDecl(_,_,_,_,_,ref decl,_,_),&**decl)
745     mf_method!(pe_body,&'a ast::Block,MethDecl(_,_,_,_,_,_,ref body,_),&**body)
746     mf_method!(pe_vis,ast::Visibility,MethDecl(_,_,_,_,_,_,_,vis),vis)
747 }
748
749 #[cfg(test)]
750 mod test {
751     use ast::*;
752     use super::*;
753     use owned_slice::OwnedSlice;
754
755     fn ident_to_segment(id : &Ident) -> PathSegment {
756         PathSegment {identifier:id.clone(),
757                      lifetimes: Vec::new(),
758                      types: OwnedSlice::empty()}
759     }
760
761     #[test] fn idents_name_eq_test() {
762         assert!(segments_name_eq(
763             [Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
764                 .iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice(),
765             [Ident{name:Name(3),ctxt:104}, Ident{name:Name(78),ctxt:182}]
766                 .iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice()));
767         assert!(!segments_name_eq(
768             [Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}]
769                 .iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice(),
770             [Ident{name:Name(3),ctxt:104}, Ident{name:Name(77),ctxt:182}]
771                 .iter().map(ident_to_segment).collect::<Vec<PathSegment>>().as_slice()));
772     }
773 }