diff options
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 15 | ||||
| -rw-r--r-- | src/libsyntax/ast_map/blocks.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ast_util.rs | 23 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/generic/mod.rs | 14 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 17 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 33 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 39 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 107 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 20 |
9 files changed, 109 insertions, 161 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index bc1767fa3a4..0a08b0b1207 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1058,10 +1058,10 @@ pub struct TypeField { pub span: Span, } -/// Represents a required method in a trait declaration, -/// one without a default implementation +/// Represents a method's signature in a trait declaration, +/// or in an implementation. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] -pub struct TypeMethod { +pub struct MethodSig { pub unsafety: Unsafety, pub abi: Abi, pub decl: P<FnDecl>, @@ -1084,7 +1084,7 @@ pub struct TraitItem { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum TraitItem_ { - RequiredMethod(TypeMethod), + RequiredMethod(MethodSig), ProvidedMethod(Method), TypeTraitItem(TyParamBounds, Option<P<Ty>>), } @@ -1419,12 +1419,7 @@ pub type ExplicitSelf = Spanned<ExplicitSelf_>; #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Method { /// Represents a method declaration - MethDecl(Generics, - Abi, - ExplicitSelf, - Unsafety, - P<FnDecl>, - P<Block>), + MethDecl(MethodSig, P<Block>), /// Represents a macro in method position MethMac(Mac), } diff --git a/src/libsyntax/ast_map/blocks.rs b/src/libsyntax/ast_map/blocks.rs index 053c3153340..345ccf902cd 100644 --- a/src/libsyntax/ast_map/blocks.rs +++ b/src/libsyntax/ast_map/blocks.rs @@ -162,7 +162,7 @@ impl<'a> FnLikeNode<'a> { pub fn decl(self) -> &'a FnDecl { self.handle(|i: ItemFnParts<'a>| &*i.decl, - |_, _, m: &'a ast::Method, _| m.pe_fn_decl(), + |_, _, m: &'a ast::Method, _| &m.pe_sig().decl, |c: ClosureParts<'a>| c.decl) } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 673ea4ac431..91ddc8beec8 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use abi::Abi; use ast::*; use ast; use ast_util; @@ -461,7 +460,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { self.visit_generics_helper(generics) } visit::FkMethod(_, m) => { - self.visit_generics_helper(m.pe_generics()) + self.visit_generics_helper(&m.pe_sig().generics) } visit::FkFnBlock => {} } @@ -653,11 +652,7 @@ pub fn lit_is_str(lit: &Lit) -> bool { /// not a macro invocation. This check is guaranteed to succeed, assuming /// that the invocations are indeed gone. pub trait PostExpansionMethod { - fn pe_generics<'a>(&'a self) -> &'a ast::Generics; - fn pe_abi(&self) -> Abi; - fn pe_explicit_self<'a>(&'a self) -> &'a ast::ExplicitSelf; - fn pe_unsafety(&self) -> ast::Unsafety; - fn pe_fn_decl<'a>(&'a self) -> &'a ast::FnDecl; + fn pe_sig<'a>(&'a self) -> &'a ast::MethodSig; fn pe_body<'a>(&'a self) -> &'a ast::Block; } @@ -676,18 +671,8 @@ macro_rules! mf_method{ impl PostExpansionMethod for Method { - mf_method! { - pe_generics,&'a ast::Generics, - MethDecl(ref generics,_,_,_,_,_),generics - } - mf_method! { pe_abi,Abi,MethDecl(_,abi,_,_,_,_),abi } - mf_method! { - pe_explicit_self,&'a ast::ExplicitSelf, - MethDecl(_,_,ref explicit_self,_,_,_),explicit_self - } - mf_method! { pe_unsafety,ast::Unsafety,MethDecl(_,_,_,unsafety,_,_),unsafety } - mf_method! { pe_fn_decl,&'a ast::FnDecl,MethDecl(_,_,_,_,ref decl,_),&**decl } - mf_method! { pe_body,&'a ast::Block,MethDecl(_,_,_,_,_,ref body),&**body } + mf_method! { pe_sig, &'a ast::MethodSig,MethDecl(ref sig, _), sig } + mf_method! { pe_body, &'a ast::Block,MethDecl(_, ref body), body } } #[cfg(test)] diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index a4962afff3c..d4b0f7d1dcb 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -724,13 +724,13 @@ impl<'a> MethodDef<'a> { span: trait_.span, vis: ast::Inherited, ident: method_ident, - node: ast::MethodImplItem( - ast::MethDecl(fn_generics, - abi, - explicit_self, - ast::Unsafety::Normal, - fn_decl, - body_block)) + node: ast::MethodImplItem(ast::MethDecl(ast::MethodSig { + generics: fn_generics, + abi: abi, + explicit_self: explicit_self, + unsafety: ast::Unsafety::Normal, + decl: fn_decl + }, body_block)) }) } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 5fb0126cdd0..fa0b747f45b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1393,15 +1393,16 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> { fn fold_method(&mut self, m: ast::Method) -> ast::Method { match m { - ast::MethDecl(generics, abi, explicit_self, fn_style, decl, body) => { + ast::MethDecl(sig, body) => { let (rewritten_fn_decl, rewritten_body) - = expand_and_rename_fn_decl_and_block(decl, body, self); - ast::MethDecl(self.fold_generics(generics), - abi, - self.fold_explicit_self(explicit_self), - fn_style, - rewritten_fn_decl, - rewritten_body) + = expand_and_rename_fn_decl_and_block(sig.decl, body, self); + ast::MethDecl(ast::MethodSig { + generics: self.fold_generics(sig.generics), + abi: sig.abi, + explicit_self: self.fold_explicit_self(sig.explicit_self), + unsafety: sig.unsafety, + decl: rewritten_fn_decl + }, rewritten_body) } ast::MethMac(mac) => ast::MethMac(mac) } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index d99f1600cb3..d7982ef8399 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -977,15 +977,7 @@ pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>, folder: &mut T) ident: folder.fold_ident(ident), attrs: fold_attrs(attrs, folder), node: match node { - RequiredMethod(TypeMethod { unsafety, abi, decl, generics, explicit_self }) => { - RequiredMethod(TypeMethod { - unsafety: unsafety, - abi: abi, - decl: folder.fold_fn_decl(decl), - generics: folder.fold_generics(generics), - explicit_self: folder.fold_explicit_self(explicit_self) - }) - } + RequiredMethod(sig) => RequiredMethod(noop_fold_method_sig(sig, folder)), ProvidedMethod(m) => ProvidedMethod(folder.fold_method(m)), TypeTraitItem(bounds, default) => { TypeTraitItem(folder.fold_bounds(bounds), @@ -1110,23 +1102,24 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> // Default fold over a method. pub fn noop_fold_method<T: Folder>(method: Method, folder: &mut T) -> Method { match method { - MethDecl(generics, - abi, - explicit_self, - unsafety, - decl, - body) => { - MethDecl(folder.fold_generics(generics), - abi, - folder.fold_explicit_self(explicit_self), - unsafety, - folder.fold_fn_decl(decl), + MethDecl(sig, body) => { + MethDecl(noop_fold_method_sig(sig, folder), folder.fold_block(body)) }, MethMac(mac) => MethMac(folder.fold_mac(mac)) } } +pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig { + MethodSig { + generics: folder.fold_generics(sig.generics), + abi: sig.abi, + explicit_self: folder.fold_explicit_self(sig.explicit_self), + unsafety: sig.unsafety, + decl: folder.fold_fn_decl(sig.decl) + } +} + pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> { p.map(|Pat {id, node, span}| Pat { id: folder.new_id(id), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a38508d2cf5..2e77bd6df18 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -51,8 +51,7 @@ use ast::{SelfExplicit, SelfRegion, SelfStatic, SelfValue}; use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef}; use ast::{TtDelimited, TtSequence, TtToken}; use ast::{TupleVariantKind, Ty, Ty_, TypeBinding}; -use ast::{TyFixedLengthVec, TyBareFn}; -use ast::{TyTypeof, TyInfer, TypeMethod}; +use ast::{TyFixedLengthVec, TyBareFn, TyTypeof, TyInfer}; use ast::{TyParam, TyParamBound, TyParen, TyPath, TyPolyTraitRef, TyPtr}; use ast::{TyRptr, TyTup, TyU32, TyVec, UnUniq}; use ast::{TypeImplItem, TypeTraitItem}; @@ -1341,31 +1340,27 @@ impl<'a> Parser<'a> { }); p.parse_where_clause(&mut generics); + let sig = ast::MethodSig { + unsafety: style, + decl: d, + generics: generics, + abi: abi, + explicit_self: explicit_self, + }; let hi = p.last_span.hi; let node = match p.token { token::Semi => { p.bump(); debug!("parse_trait_methods(): parsing required method"); - RequiredMethod(TypeMethod { - unsafety: style, - decl: d, - generics: generics, - abi: abi, - explicit_self: explicit_self, - }) + RequiredMethod(sig) } token::OpenDelim(token::Brace) => { debug!("parse_trait_methods(): parsing provided method"); let (inner_attrs, body) = p.parse_inner_attrs_and_block(); attrs.push_all(&inner_attrs[..]); - ProvidedMethod(ast::MethDecl(generics, - abi, - explicit_self, - style, - d, - body)) + ProvidedMethod(ast::MethDecl(sig, body)) } _ => { @@ -4758,13 +4753,13 @@ impl<'a> Parser<'a> { let body_span = body.span; let mut new_attrs = attrs; new_attrs.push_all(&inner_attrs[..]); - (ast::MethDecl(generics, - abi, - explicit_self, - unsafety, - decl, - body), - body_span.hi, new_attrs, ident) + (ast::MethDecl(ast::MethodSig { + generics: generics, + abi: abi, + explicit_self: explicit_self, + unsafety: unsafety, + decl: decl + }, body), body_span.hi, new_attrs, ident) } }; P(ImplItem { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a457355698d..20c8df42993 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -375,8 +375,9 @@ pub fn fun_to_string(decl: &ast::FnDecl, unsafety: ast::Unsafety, name: ast::Ide opt_explicit_self: Option<&ast::ExplicitSelf_>, generics: &ast::Generics) -> String { $to_string(|s| { - try!(s.print_fn(decl, Some(unsafety), abi::Rust, - name, generics, opt_explicit_self, ast::Inherited)); + try!(s.head("")); + try!(s.print_fn(decl, unsafety, abi::Rust, Some(name), + generics, opt_explicit_self, ast::Inherited)); try!(s.end()); // Close the head box s.end() // Close the outer box }) @@ -759,8 +760,10 @@ impl<'a> State<'a> { try!(self.print_outer_attributes(&item.attrs)); match item.node { ast::ForeignItemFn(ref decl, ref generics) => { - try!(self.print_fn(&**decl, None, abi::Rust, item.ident, generics, - None, item.vis)); + try!(self.head("")); + try!(self.print_fn(&**decl, ast::Unsafety::Normal, + abi::Rust, Some(item.ident), + generics, None, item.vis)); try!(self.end()); // end head-ibox try!(word(&mut self.s, ";")); self.end() // end the outer fn box @@ -861,11 +864,12 @@ impl<'a> State<'a> { try!(self.end()); // end the outer cbox } ast::ItemFn(ref decl, unsafety, abi, ref typarams, ref body) => { + try!(self.head("")); try!(self.print_fn( - &**decl, - Some(unsafety), + decl, + unsafety, abi, - item.ident, + Some(item.ident), typarams, None, item.vis @@ -1227,17 +1231,18 @@ impl<'a> State<'a> { } } - pub fn print_ty_method(&mut self, - ident: ast::Ident, - m: &ast::TypeMethod) - -> io::Result<()> { - try!(self.print_ty_fn(m.abi, - m.unsafety, - &*m.decl, - Some(ident), - &m.generics, - Some(&m.explicit_self.node))); - word(&mut self.s, ";") + pub fn print_method_sig(&mut self, + ident: ast::Ident, + m: &ast::MethodSig, + vis: ast::Visibility) + -> io::Result<()> { + self.print_fn(&m.decl, + m.unsafety, + m.abi, + Some(ident), + &m.generics, + Some(&m.explicit_self.node), + vis) } pub fn print_trait_item(&mut self, ti: &ast::TraitItem) @@ -1246,8 +1251,9 @@ impl<'a> State<'a> { try!(self.maybe_print_comment(ti.span.lo)); try!(self.print_outer_attributes(&ti.attrs)); match ti.node { - ast::RequiredMethod(ref ty_m) => { - self.print_ty_method(ti.ident, ty_m) + ast::RequiredMethod(ref sig) => { + try!(self.print_method_sig(ti.ident, sig, ast::Inherited)); + word(&mut self.s, ";") } ast::ProvidedMethod(ref m) => { self.print_method(ti.ident, &ti.attrs, ast::Inherited, m) @@ -1280,20 +1286,10 @@ impl<'a> State<'a> { meth: &ast::Method) -> io::Result<()> { match *meth { - ast::MethDecl(ref generics, - abi, - ref explicit_self, - unsafety, - ref decl, - ref body) => { - try!(self.print_fn(&**decl, - Some(unsafety), - abi, - ident, - generics, - Some(&explicit_self.node), - vis)); - try!(word(&mut self.s, " ")); + ast::MethDecl(ref sig, ref body) => { + try!(self.head("")); + try!(self.print_method_sig(ident, sig, vis)); + try!(self.nbsp()); self.print_block_with_attrs(&**body, attrs) }, ast::MethMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), @@ -2328,16 +2324,18 @@ impl<'a> State<'a> { pub fn print_fn(&mut self, decl: &ast::FnDecl, - unsafety: Option<ast::Unsafety>, + unsafety: ast::Unsafety, abi: abi::Abi, - name: ast::Ident, + name: Option<ast::Ident>, generics: &ast::Generics, opt_explicit_self: Option<&ast::ExplicitSelf_>, vis: ast::Visibility) -> io::Result<()> { - try!(self.head("")); try!(self.print_fn_header_info(unsafety, abi, vis)); - try!(self.nbsp()); - try!(self.print_ident(name)); + + if let Some(name) = name { + try!(self.nbsp()); + try!(self.print_ident(name)); + } try!(self.print_generics(generics)); try!(self.print_fn_args_and_ret(decl, opt_explicit_self)); self.print_where_clause(generics) @@ -2704,25 +2702,14 @@ impl<'a> State<'a> { abi: abi::Abi, unsafety: ast::Unsafety, decl: &ast::FnDecl, - id: Option<ast::Ident>, + name: Option<ast::Ident>, generics: &ast::Generics, opt_explicit_self: Option<&ast::ExplicitSelf_>) -> io::Result<()> { try!(self.ibox(indent_unit)); - try!(self.print_fn_header_info(Some(unsafety), abi, ast::Inherited)); - - match id { - Some(id) => { - try!(word(&mut self.s, " ")); - try!(self.print_ident(id)); - } - _ => () - } - - try!(self.print_generics(generics)); - try!(zerobreak(&mut self.s)); - try!(self.print_fn_args_and_ret(decl, opt_explicit_self)); - try!(self.print_where_clause(generics)); + try!(self.print_fn(decl, unsafety, abi, name, + generics, opt_explicit_self, + ast::Inherited)); self.end() } @@ -2944,14 +2931,6 @@ impl<'a> State<'a> { } } - pub fn print_opt_unsafety(&mut self, - opt_unsafety: Option<ast::Unsafety>) -> io::Result<()> { - match opt_unsafety { - Some(unsafety) => self.print_unsafety(unsafety), - None => Ok(()) - } - } - pub fn print_opt_abi_and_extern_if_nondefault(&mut self, opt_abi: Option<abi::Abi>) -> io::Result<()> { @@ -2977,11 +2956,11 @@ impl<'a> State<'a> { } pub fn print_fn_header_info(&mut self, - opt_unsafety: Option<ast::Unsafety>, + unsafety: ast::Unsafety, abi: abi::Abi, vis: ast::Visibility) -> io::Result<()> { try!(word(&mut self.s, &visibility_qualified(vis, ""))); - try!(self.print_opt_unsafety(opt_unsafety)); + try!(self.print_unsafety(unsafety)); if abi != abi::Rust { try!(self.word_nbsp("extern")); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 867f98f6077..4375e17ce0b 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -602,11 +602,11 @@ fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, method: &'v Method) { match *method { - MethDecl(_, _, _, _, ref decl, ref body) => { + MethDecl(ref sig, ref body) => { visitor.visit_ident(span, ident); visitor.visit_fn(FkMethod(ident, method), - &**decl, - &**body, + &sig.decl, + body, span, id); }, @@ -627,9 +627,9 @@ pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V, } FkMethod(_, method) => { match *method { - MethDecl(ref generics, _, ref explicit_self, _, _, _) => { - visitor.visit_generics(generics); - visitor.visit_explicit_self(explicit_self); + MethDecl(ref sig, _) => { + visitor.visit_generics(&sig.generics); + visitor.visit_explicit_self(&sig.explicit_self); } MethMac(ref mac) => visitor.visit_mac(mac) } @@ -646,10 +646,10 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai visitor.visit_attribute(attr); } match trait_item.node { - RequiredMethod(ref method_type) => { - visitor.visit_explicit_self(&method_type.explicit_self); - visitor.visit_generics(&method_type.generics); - walk_fn_decl(visitor, &method_type.decl); + RequiredMethod(ref sig) => { + visitor.visit_explicit_self(&sig.explicit_self); + visitor.visit_generics(&sig.generics); + walk_fn_decl(visitor, &sig.decl); } ProvidedMethod(ref method) => { walk_method_helper(visitor, |
