diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2014-09-13 19:06:01 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2014-09-14 03:39:36 +0300 |
| commit | ccd8498afbb371939b7decdbee712f726ccbded3 (patch) | |
| tree | 8939c9dba98ee7a2f624e82c3c72dcf33576d350 | |
| parent | d6fb338d01864e3801cab9f76d608f204d11fc27 (diff) | |
| download | rust-ccd8498afbb371939b7decdbee712f726ccbded3.tar.gz rust-ccd8498afbb371939b7decdbee712f726ccbded3.zip | |
syntax: fix fallout from using ptr::P.
45 files changed, 1529 insertions, 1688 deletions
diff --git a/src/libsyntax/ast_map/blocks.rs b/src/libsyntax/ast_map/blocks.rs index b00c6ce14b5..1400e494917 100644 --- a/src/libsyntax/ast_map/blocks.rs +++ b/src/libsyntax/ast_map/blocks.rs @@ -22,7 +22,7 @@ //! for the `Code` associated with a particular NodeId. use abi; -use ast::{P, Block, FnDecl, NodeId}; +use ast::{Block, FnDecl, NodeId}; use ast; use ast_map::{Node}; use ast_map; @@ -39,7 +39,7 @@ use visit; /// - The default implementation for a trait method. /// /// To construct one, use the `Code::from_node` function. -pub struct FnLikeNode { node: ast_map::Node } +pub struct FnLikeNode<'a> { node: ast_map::Node<'a> } /// MaybeFnLike wraps a method that indicates if an object /// corresponds to some FnLikeNode. @@ -47,8 +47,8 @@ pub trait MaybeFnLike { fn is_fn_like(&self) -> bool; } /// Components shared by fn-like things (fn items, methods, closures). pub struct FnParts<'a> { - pub decl: P<FnDecl>, - pub body: P<Block>, + pub decl: &'a FnDecl, + pub body: &'a Block, pub kind: visit::FnKind<'a>, pub span: Span, pub id: NodeId, @@ -78,12 +78,12 @@ impl MaybeFnLike for ast::Expr { /// Carries either an FnLikeNode or a Block, as these are the two /// constructs that correspond to "code" (as in, something from which /// we can construct a control-flow graph). -pub enum Code { - FnLikeCode(FnLikeNode), - BlockCode(P<Block>), +pub enum Code<'a> { + FnLikeCode(FnLikeNode<'a>), + BlockCode(&'a Block), } -impl Code { +impl<'a> Code<'a> { pub fn id(&self) -> ast::NodeId { match *self { FnLikeCode(node) => node.id(), @@ -115,32 +115,32 @@ impl Code { /// use when implementing FnLikeNode operations. struct ItemFnParts<'a> { ident: ast::Ident, - decl: P<ast::FnDecl>, + decl: &'a ast::FnDecl, style: ast::FnStyle, abi: abi::Abi, generics: &'a ast::Generics, - body: P<Block>, + body: &'a Block, id: ast::NodeId, span: Span } /// These are all the components one can extract from a closure expr /// for use when implementing FnLikeNode operations. -struct ClosureParts { - decl: P<FnDecl>, - body: P<Block>, +struct ClosureParts<'a> { + decl: &'a FnDecl, + body: &'a Block, id: NodeId, span: Span } -impl ClosureParts { - fn new(d: P<FnDecl>, b: P<Block>, id: NodeId, s: Span) -> ClosureParts { +impl<'a> ClosureParts<'a> { + fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span) -> ClosureParts<'a> { ClosureParts { decl: d, body: b, id: id, span: s } } } -impl FnLikeNode { - pub fn to_fn_parts<'a>(&'a self) -> FnParts<'a> { +impl<'a> FnLikeNode<'a> { + pub fn to_fn_parts(self) -> FnParts<'a> { FnParts { decl: self.decl(), body: self.body(), @@ -150,31 +150,31 @@ impl FnLikeNode { } } - pub fn body<'a>(&'a self) -> P<Block> { - self.handle(|i: ItemFnParts| i.body, - |m: &'a ast::Method| m.pe_body(), - |c: ClosureParts| c.body) + pub fn body(self) -> &'a Block { + self.handle(|i: ItemFnParts<'a>| &*i.body, + |m: &'a ast::Method| m.pe_body(), + |c: ClosureParts<'a>| c.body) } - pub fn decl<'a>(&'a self) -> P<FnDecl> { - self.handle(|i: ItemFnParts| i.decl, - |m: &'a ast::Method| m.pe_fn_decl(), - |c: ClosureParts| c.decl) + pub fn decl(self) -> &'a FnDecl { + self.handle(|i: ItemFnParts<'a>| &*i.decl, + |m: &'a ast::Method| m.pe_fn_decl(), + |c: ClosureParts<'a>| c.decl) } - pub fn span<'a>(&'a self) -> Span { + pub fn span(self) -> Span { self.handle(|i: ItemFnParts| i.span, |m: &'a ast::Method| m.span, |c: ClosureParts| c.span) } - pub fn id<'a>(&'a self) -> NodeId { + pub fn id(self) -> NodeId { self.handle(|i: ItemFnParts| i.id, |m: &'a ast::Method| m.id, |c: ClosureParts| c.id) } - pub fn kind<'a>(&'a self) -> visit::FnKind<'a> { + pub fn kind(self) -> visit::FnKind<'a> { let item = |p: ItemFnParts<'a>| -> visit::FnKind<'a> { visit::FkItemFn(p.ident, p.generics, p.style, p.abi) }; @@ -187,33 +187,33 @@ impl FnLikeNode { self.handle(item, method, closure) } - fn handle<'a, A>(&'a self, - item_fn: |ItemFnParts<'a>| -> A, - method: |&'a ast::Method| -> A, - closure: |ClosureParts| -> A) -> A { + fn handle<A>(self, + item_fn: |ItemFnParts<'a>| -> A, + method: |&'a ast::Method| -> A, + closure: |ClosureParts<'a>| -> A) -> A { match self.node { - ast_map::NodeItem(ref i) => match i.node { - ast::ItemFn(decl, style, abi, ref generics, block) => + ast_map::NodeItem(i) => match i.node { + ast::ItemFn(ref decl, style, abi, ref generics, ref block) => item_fn(ItemFnParts{ - ident: i.ident, decl: decl, style: style, body: block, + ident: i.ident, decl: &**decl, style: style, body: &**block, generics: generics, abi: abi, id: i.id, span: i.span }), _ => fail!("item FnLikeNode that is not fn-like"), }, - ast_map::NodeTraitItem(ref t) => match **t { + ast_map::NodeTraitItem(t) => match *t { ast::ProvidedMethod(ref m) => method(&**m), _ => fail!("trait method FnLikeNode that is not fn-like"), }, - ast_map::NodeImplItem(ref ii) => { - match **ii { + ast_map::NodeImplItem(ii) => { + match *ii { ast::MethodImplItem(ref m) => method(&**m), } } - ast_map::NodeExpr(ref e) => match e.node { + ast_map::NodeExpr(e) => match e.node { ast::ExprFnBlock(_, ref decl, ref block) => - closure(ClosureParts::new(*decl, *block, e.id, e.span)), + closure(ClosureParts::new(&**decl, &**block, e.id, e.span)), ast::ExprProc(ref decl, ref block) => - closure(ClosureParts::new(*decl, *block, e.id, e.span)), + closure(ClosureParts::new(&**decl, &**block, e.id, e.span)), _ => fail!("expr FnLikeNode that is not fn-like"), }, _ => fail!("other FnLikeNode that is not fn-like"), diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 2a76bbf4e3b..becfe715f29 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -19,12 +19,12 @@ use codemap::Span; use owned_slice::OwnedSlice; use parse::token; use print::pprust; +use ptr::P; use visit::Visitor; use visit; use std::cell::Cell; use std::cmp; -use std::gc::{Gc, GC}; use std::u32; pub fn path_name_i(idents: &[Ident]) -> String { @@ -98,7 +98,7 @@ pub fn unop_to_string(op: UnOp) -> &'static str { } } -pub fn is_path(e: Gc<Expr>) -> bool { +pub fn is_path(e: P<Expr>) -> bool { return match e.node { ExprPath(_) => true, _ => false }; } @@ -166,21 +166,6 @@ pub fn float_ty_to_string(t: FloatTy) -> String { } } -pub fn is_call_expr(e: Gc<Expr>) -> bool { - match e.node { ExprCall(..) => true, _ => false } -} - -pub fn block_from_expr(e: Gc<Expr>) -> P<Block> { - P(Block { - view_items: Vec::new(), - stmts: Vec::new(), - expr: Some(e), - id: e.id, - rules: DefaultBlock, - span: e.span - }) -} - // convert a span and an identifier to the corresponding // 1-segment path pub fn ident_to_path(s: Span, identifier: Ident) -> Path { @@ -197,10 +182,12 @@ pub fn ident_to_path(s: Span, identifier: Ident) -> Path { } } -pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> Gc<Pat> { - box(GC) ast::Pat { id: id, - node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None), - span: s } +pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> { + P(Pat { + id: id, + node: PatIdent(BindByValue(MutImmutable), codemap::Spanned{span:s, node:i}, None), + span: s + }) } pub fn name_to_dummy_lifetime(name: Name) -> Lifetime { @@ -226,57 +213,6 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: &Ty) -> Ident { token::gensym_ident(pretty.as_slice()) } -pub fn trait_method_to_ty_method(method: &Method) -> TypeMethod { - match method.node { - MethDecl(ident, - ref generics, - abi, - explicit_self, - fn_style, - decl, - _, - vis) => { - TypeMethod { - ident: ident, - attrs: method.attrs.clone(), - fn_style: fn_style, - decl: decl, - generics: generics.clone(), - explicit_self: explicit_self, - id: method.id, - span: method.span, - vis: vis, - abi: abi, - } - }, - MethMac(_) => fail!("expected non-macro method declaration") - } -} - -/// extract a TypeMethod from a TraitItem. if the TraitItem is -/// a default, pull out the useful fields to make a TypeMethod -// -// NB: to be used only after expansion is complete, and macros are gone. -pub fn trait_item_to_ty_method(method: &TraitItem) -> TypeMethod { - match *method { - RequiredMethod(ref m) => (*m).clone(), - ProvidedMethod(ref m) => trait_method_to_ty_method(&**m), - } -} - -pub fn split_trait_methods(trait_methods: &[TraitItem]) - -> (Vec<TypeMethod> , Vec<Gc<Method>> ) { - let mut reqd = Vec::new(); - let mut provd = Vec::new(); - for trt_method in trait_methods.iter() { - match *trt_method { - RequiredMethod(ref tm) => reqd.push((*tm).clone()), - ProvidedMethod(m) => provd.push(m) - } - }; - (reqd, provd) -} - pub fn struct_field_visibility(field: ast::StructField) -> Visibility { match field.node.kind { ast::NamedField(_, v) | ast::UnnamedField(v) => v @@ -603,13 +539,6 @@ pub fn compute_id_range_for_fn_body(fk: visit::FnKind, visitor.result.get() } -pub fn is_item_impl(item: Gc<ast::Item>) -> bool { - match item.node { - ItemImpl(..) => true, - _ => false - } -} - pub fn walk_pat(pat: &Pat, it: |&Pat| -> bool) -> bool { if !it(pat) { return false; @@ -678,7 +607,7 @@ pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool { /// Returns true if the given pattern consists solely of an identifier /// and false otherwise. -pub fn pat_is_ident(pat: Gc<ast::Pat>) -> bool { +pub fn pat_is_ident(pat: P<ast::Pat>) -> bool { match pat.node { ast::PatIdent(..) => true, _ => false, @@ -713,28 +642,13 @@ pub fn segments_name_eq(a : &[ast::PathSegment], b : &[ast::PathSegment]) -> boo } /// Returns true if this literal is a string and false otherwise. -pub fn lit_is_str(lit: Gc<Lit>) -> bool { +pub fn lit_is_str(lit: &Lit) -> bool { match lit.node { LitStr(..) => true, _ => false, } } -pub fn get_inner_tys(ty: P<Ty>) -> Vec<P<Ty>> { - match ty.node { - ast::TyRptr(_, mut_ty) | ast::TyPtr(mut_ty) => { - vec!(mut_ty.ty) - } - ast::TyBox(ty) - | ast::TyVec(ty) - | ast::TyUniq(ty) - | ast::TyFixedLengthVec(ty, _) => vec!(ty), - ast::TyTup(ref tys) => tys.clone(), - ast::TyParen(ty) => get_inner_tys(ty), - _ => Vec::new() - } -} - /// Returns true if the static with the given mutability and attributes /// has a significant address and false otherwise. pub fn static_has_significant_address(mutbl: ast::Mutability, @@ -757,13 +671,13 @@ pub trait PostExpansionMethod { fn pe_abi(&self) -> Abi; fn pe_explicit_self<'a>(&'a self) -> &'a ast::ExplicitSelf; fn pe_fn_style(&self) -> ast::FnStyle; - fn pe_fn_decl(&self) -> P<ast::FnDecl>; - fn pe_body(&self) -> P<ast::Block>; + fn pe_fn_decl<'a>(&'a self) -> &'a ast::FnDecl; + fn pe_body<'a>(&'a self) -> &'a ast::Block; fn pe_vis(&self) -> ast::Visibility; } macro_rules! mf_method{ - ($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:ident) => { + ($meth_name:ident, $field_ty:ty, $field_pat:pat, $result:expr) => { fn $meth_name<'a>(&'a self) -> $field_ty { match self.node { $field_pat => $result, @@ -784,8 +698,8 @@ impl PostExpansionMethod for Method { mf_method!(pe_explicit_self,&'a ast::ExplicitSelf, MethDecl(_,_,_,ref explicit_self,_,_,_,_),explicit_self) mf_method!(pe_fn_style,ast::FnStyle,MethDecl(_,_,_,_,fn_style,_,_,_),fn_style) - mf_method!(pe_fn_decl,P<ast::FnDecl>,MethDecl(_,_,_,_,_,decl,_,_),decl) - mf_method!(pe_body,P<ast::Block>,MethDecl(_,_,_,_,_,_,body,_),body) + 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_vis,ast::Visibility,MethDecl(_,_,_,_,_,_,_,vis),vis) } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index dd422d02149..80e4d148bde 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -18,10 +18,10 @@ use diagnostic::SpanHandler; use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use parse::token::InternedString; use parse::token; +use ptr::P; use std::collections::HashSet; use std::collections::BitvSet; -use std::gc::{Gc, GC}; local_data_key!(used_attrs: BitvSet) @@ -50,7 +50,7 @@ pub trait AttrMetaMethods { /// containing a string, otherwise None. fn value_str(&self) -> Option<InternedString>; /// Gets a list of inner meta items from a list MetaItem type. - fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<MetaItem>]>; + fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]>; } impl AttrMetaMethods for Attribute { @@ -65,7 +65,7 @@ impl AttrMetaMethods for Attribute { fn value_str(&self) -> Option<InternedString> { self.meta().value_str() } - fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<MetaItem>]> { + fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]> { self.node.value.meta_item_list() } } @@ -91,7 +91,7 @@ impl AttrMetaMethods for MetaItem { } } - fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<MetaItem>]> { + fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]> { match self.node { MetaList(_, ref l) => Some(l.as_slice()), _ => None @@ -100,30 +100,30 @@ impl AttrMetaMethods for MetaItem { } // Annoying, but required to get test_cfg to work -impl AttrMetaMethods for Gc<MetaItem> { +impl AttrMetaMethods for P<MetaItem> { fn name(&self) -> InternedString { (**self).name() } fn value_str(&self) -> Option<InternedString> { (**self).value_str() } - fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<MetaItem>]> { + fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]> { (**self).meta_item_list() } } pub trait AttributeMethods { - fn meta(&self) -> Gc<MetaItem>; - fn desugar_doc(&self) -> Attribute; + fn meta<'a>(&'a self) -> &'a MetaItem; + fn with_desugared_doc<T>(&self, f: |&Attribute| -> T) -> T; } impl AttributeMethods for Attribute { /// Extract the MetaItem from inside this Attribute. - fn meta(&self) -> Gc<MetaItem> { - self.node.value + fn meta<'a>(&'a self) -> &'a MetaItem { + &*self.node.value } /// Convert self to a normal #[doc="foo"] comment, if it is a /// comment like `///` or `/** */`. (Returns self unchanged for /// non-sugared doc attributes.) - fn desugar_doc(&self) -> Attribute { + fn with_desugared_doc<T>(&self, f: |&Attribute| -> T) -> T { if self.node.is_sugared_doc { let comment = self.value_str().unwrap(); let meta = mk_name_value_item_str( @@ -131,12 +131,12 @@ impl AttributeMethods for Attribute { token::intern_and_get_ident(strip_doc_comment_decoration( comment.get()).as_slice())); if self.node.style == ast::AttrOuter { - mk_attr_outer(self.node.id, meta) + f(&mk_attr_outer(self.node.id, meta)) } else { - mk_attr_inner(self.node.id, meta) + f(&mk_attr_inner(self.node.id, meta)) } } else { - *self + f(self) } } } @@ -144,23 +144,22 @@ impl AttributeMethods for Attribute { /* Constructors */ pub fn mk_name_value_item_str(name: InternedString, value: InternedString) - -> Gc<MetaItem> { + -> P<MetaItem> { let value_lit = dummy_spanned(ast::LitStr(value, ast::CookedStr)); mk_name_value_item(name, value_lit) } pub fn mk_name_value_item(name: InternedString, value: ast::Lit) - -> Gc<MetaItem> { - box(GC) dummy_spanned(MetaNameValue(name, value)) + -> P<MetaItem> { + P(dummy_spanned(MetaNameValue(name, value))) } -pub fn mk_list_item(name: InternedString, - items: Vec<Gc<MetaItem>>) -> Gc<MetaItem> { - box(GC) dummy_spanned(MetaList(name, items)) +pub fn mk_list_item(name: InternedString, items: Vec<P<MetaItem>>) -> P<MetaItem> { + P(dummy_spanned(MetaList(name, items))) } -pub fn mk_word_item(name: InternedString) -> Gc<MetaItem> { - box(GC) dummy_spanned(MetaWord(name)) +pub fn mk_word_item(name: InternedString) -> P<MetaItem> { + P(dummy_spanned(MetaWord(name))) } local_data_key!(next_attr_id: uint) @@ -172,7 +171,7 @@ pub fn mk_attr_id() -> AttrId { } /// Returns an inner attribute with the given value. -pub fn mk_attr_inner(id: AttrId, item: Gc<MetaItem>) -> Attribute { +pub fn mk_attr_inner(id: AttrId, item: P<MetaItem>) -> Attribute { dummy_spanned(Attribute_ { id: id, style: ast::AttrInner, @@ -182,7 +181,7 @@ pub fn mk_attr_inner(id: AttrId, item: Gc<MetaItem>) -> Attribute { } /// Returns an outer attribute with the given value. -pub fn mk_attr_outer(id: AttrId, item: Gc<MetaItem>) -> Attribute { +pub fn mk_attr_outer(id: AttrId, item: P<MetaItem>) -> Attribute { dummy_spanned(Attribute_ { id: id, style: ast::AttrOuter, @@ -199,8 +198,8 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos, let attr = Attribute_ { id: id, style: style, - value: box(GC) spanned(lo, hi, MetaNameValue(InternedString::new("doc"), - lit)), + value: P(spanned(lo, hi, MetaNameValue(InternedString::new("doc"), + lit))), is_sugared_doc: true }; spanned(lo, hi, attr) @@ -210,8 +209,7 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos, /// Check if `needle` occurs in `haystack` by a structural /// comparison. This is slightly subtle, and relies on ignoring the /// span included in the `==` comparison a plain MetaItem. -pub fn contains(haystack: &[Gc<ast::MetaItem>], - needle: Gc<ast::MetaItem>) -> bool { +pub fn contains(haystack: &[P<MetaItem>], needle: &MetaItem) -> bool { debug!("attr::contains (name={})", needle.name()); haystack.iter().any(|item| { debug!(" testing: {}", item.name()); @@ -234,7 +232,7 @@ pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) .and_then(|at| at.value_str()) } -pub fn last_meta_item_value_str_by_name(items: &[Gc<MetaItem>], name: &str) +pub fn last_meta_item_value_str_by_name(items: &[P<MetaItem>], name: &str) -> Option<InternedString> { items.iter() .rev() @@ -244,28 +242,25 @@ pub fn last_meta_item_value_str_by_name(items: &[Gc<MetaItem>], name: &str) /* Higher-level applications */ -pub fn sort_meta_items(items: &[Gc<MetaItem>]) -> Vec<Gc<MetaItem>> { +pub fn sort_meta_items(items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> { // This is sort of stupid here, but we need to sort by // human-readable strings. - let mut v = items.iter() - .map(|&mi| (mi.name(), mi)) - .collect::<Vec<(InternedString, Gc<MetaItem>)> >(); + let mut v = items.move_iter() + .map(|mi| (mi.name(), mi)) + .collect::<Vec<(InternedString, P<MetaItem>)>>(); v.sort_by(|&(ref a, _), &(ref b, _)| a.cmp(b)); // There doesn't seem to be a more optimal way to do this - v.move_iter().map(|(_, m)| { - match m.node { - MetaList(ref n, ref mis) => { - box(GC) Spanned { - node: MetaList((*n).clone(), - sort_meta_items(mis.as_slice())), - .. /*bad*/ (*m).clone() - } - } - _ => m + v.move_iter().map(|(_, m)| m.map(|Spanned {node, span}| { + Spanned { + node: match node { + MetaList(n, mis) => MetaList(n, sort_meta_items(mis)), + _ => node + }, + span: span } - }).collect() + })).collect() } pub fn find_crate_name(attrs: &[Attribute]) -> Option<InternedString> { @@ -318,8 +313,8 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool { /// test_cfg(`[foo="a", bar]`, `[cfg(not(bar))]`) == false /// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="a")]`) == true /// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="b")]`) == false -pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>> - (cfg: &[Gc<MetaItem>], mut metas: It) -> bool { +pub fn test_cfg<'a, AM: AttrMetaMethods, It: Iterator<&'a AM>> + (cfg: &[P<MetaItem>], mut metas: It) -> bool { // having no #[cfg(...)] attributes counts as matching. let mut no_cfgs = true; @@ -344,10 +339,10 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>> // not match. !not_cfgs.iter().all(|mi| { debug!("cfg(not({}[...]))", mi.name()); - contains(cfg, *mi) + contains(cfg, &**mi) }) } - _ => contains(cfg, *cfg_mi) + _ => contains(cfg, &**cfg_mi) } }) } @@ -397,7 +392,7 @@ pub fn find_stability_generic<'a, }; return Some((Stability { - level: level, + level: level, text: attr.value_str() }, attr)); } @@ -412,7 +407,7 @@ pub fn find_stability(attrs: &[Attribute]) -> Option<Stability> { }) } -pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[Gc<MetaItem>]) { +pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[P<MetaItem>]) { let mut set = HashSet::new(); for meta in metas.iter() { let name = meta.name(); diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 78d3d86b296..faa3946b74d 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -543,10 +543,9 @@ fn print_macro_backtrace(w: &mut EmitterWriter, Ok(()) } -pub fn expect<T:Clone>(diag: &SpanHandler, opt: Option<T>, msg: || -> String) - -> T { +pub fn expect<T>(diag: &SpanHandler, opt: Option<T>, msg: || -> String) -> T { match opt { - Some(ref t) => (*t).clone(), - None => diag.handler().bug(msg().as_slice()), + Some(t) => t, + None => diag.handler().bug(msg().as_slice()), } } diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index 25a6a4c01bd..132b59c89b2 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -10,13 +10,13 @@ use std::cell::RefCell; use std::collections::HashMap; -use std::gc::Gc; use ast; use ast::{Ident, Name, TokenTree}; use codemap::Span; use ext::base::{ExtCtxt, MacExpr, MacItem, MacResult}; use ext::build::AstBuilder; use parse::token; +use ptr::P; local_data_key!(registered_diagnostics: RefCell<HashMap<Name, Option<Name>>>) local_data_key!(used_diagnostics: RefCell<HashMap<Name, Span>>) @@ -116,7 +116,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt, let (count, expr) = with_used_diagnostics(|diagnostics_in_use| { with_registered_diagnostics(|diagnostics| { - let descriptions: Vec<Gc<ast::Expr>> = diagnostics + let descriptions: Vec<P<ast::Expr>> = diagnostics .iter().filter_map(|(code, description)| { if !diagnostics_in_use.contains_key(code) { ecx.span_warn(span, format!( diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index 8028d51a7b5..4b8c3376cad 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -18,8 +18,7 @@ use ext::base; use ext::base::*; use parse::token::InternedString; use parse::token; - -use std::gc::GC; +use ptr::P; enum State { Asm, @@ -199,7 +198,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) } } - MacExpr::new(box(GC) ast::Expr { + MacExpr::new(P(ast::Expr { id: ast::DUMMY_NODE_ID, node: ast::ExprInlineAsm(ast::InlineAsm { asm: token::intern_and_get_ident(asm.get()), @@ -212,5 +211,5 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) dialect: dialect }), span: sp - }) + })) } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 4976e68cc64..6e25b6b73ad 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -18,6 +18,7 @@ use parse; use parse::parser; use parse::token; use parse::token::{InternedString, intern, str_to_ident}; +use ptr::P; use util::small_vector::SmallVector; use ext::mtwt; use fold::Folder; @@ -43,18 +44,18 @@ pub trait ItemDecorator { fn expand(&self, ecx: &mut ExtCtxt, sp: Span, - meta_item: Gc<ast::MetaItem>, - item: Gc<ast::Item>, - push: |Gc<ast::Item>|); + meta_item: &ast::MetaItem, + item: &ast::Item, + push: |P<ast::Item>|); } -impl ItemDecorator for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|) { +impl ItemDecorator for fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, |P<ast::Item>|) { fn expand(&self, ecx: &mut ExtCtxt, sp: Span, - meta_item: Gc<ast::MetaItem>, - item: Gc<ast::Item>, - push: |Gc<ast::Item>|) { + meta_item: &ast::MetaItem, + item: &ast::Item, + push: |P<ast::Item>|) { (*self)(ecx, sp, meta_item, item, push) } } @@ -63,18 +64,18 @@ pub trait ItemModifier { fn expand(&self, ecx: &mut ExtCtxt, span: Span, - meta_item: Gc<ast::MetaItem>, - item: Gc<ast::Item>) - -> Gc<ast::Item>; + meta_item: &ast::MetaItem, + item: P<ast::Item>) + -> P<ast::Item>; } -impl ItemModifier for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item> { +impl ItemModifier for fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item> { fn expand(&self, ecx: &mut ExtCtxt, span: Span, - meta_item: Gc<ast::MetaItem>, - item: Gc<ast::Item>) - -> Gc<ast::Item> { + meta_item: &ast::MetaItem, + item: P<ast::Item>) + -> P<ast::Item> { (*self)(ecx, span, meta_item, item) } } @@ -128,29 +129,29 @@ impl IdentMacroExpander for IdentMacroExpanderFn { /// methods are spliced into the AST at the callsite of the macro (or /// just into the compiler's internal macro table, for `make_def`). pub trait MacResult { - /// Define a new macro. + /// Attempt to define a new macro. // this should go away; the idea that a macro might expand into // either a macro definition or an expression, depending on what // the context wants, is kind of silly. - fn make_def(&self) -> Option<MacroDef> { + fn make_def(&mut self) -> Option<MacroDef> { None } /// Create an expression. - fn make_expr(&self) -> Option<Gc<ast::Expr>> { + fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> { None } /// Create zero or more items. - fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> { + fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> { None } /// Create zero or more methods. - fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> { + fn make_methods(self: Box<Self>) -> Option<SmallVector<P<ast::Method>>> { None } /// Create a pattern. - fn make_pat(&self) -> Option<Gc<ast::Pat>> { + fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> { None } @@ -158,69 +159,69 @@ pub trait MacResult { /// /// By default this attempts to create an expression statement, /// returning None if that fails. - fn make_stmt(&self) -> Option<Gc<ast::Stmt>> { + fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> { self.make_expr() - .map(|e| box(GC) codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID))) + .map(|e| P(codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID)))) } } /// A convenience type for macros that return a single expression. pub struct MacExpr { - e: Gc<ast::Expr>, + e: P<ast::Expr> } impl MacExpr { - pub fn new(e: Gc<ast::Expr>) -> Box<MacResult+'static> { + pub fn new(e: P<ast::Expr>) -> Box<MacResult+'static> { box MacExpr { e: e } as Box<MacResult+'static> } } impl MacResult for MacExpr { - fn make_expr(&self) -> Option<Gc<ast::Expr>> { + fn make_expr(self: Box<MacExpr>) -> Option<P<ast::Expr>> { Some(self.e) } - fn make_pat(&self) -> Option<Gc<ast::Pat>> { + fn make_pat(self: Box<MacExpr>) -> Option<P<ast::Pat>> { match self.e.node { - ast::ExprLit(_) => Some(box(GC) ast::Pat { + ast::ExprLit(_) => Some(P(ast::Pat { id: ast::DUMMY_NODE_ID, - node: ast::PatLit(self.e), - span: self.e.span - }), + span: self.e.span, + node: ast::PatLit(self.e) + })), _ => None } } } /// A convenience type for macros that return a single pattern. pub struct MacPat { - p: Gc<ast::Pat>, + p: P<ast::Pat> } impl MacPat { - pub fn new(p: Gc<ast::Pat>) -> Box<MacResult+'static> { + pub fn new(p: P<ast::Pat>) -> Box<MacResult+'static> { box MacPat { p: p } as Box<MacResult+'static> } } impl MacResult for MacPat { - fn make_pat(&self) -> Option<Gc<ast::Pat>> { + fn make_pat(self: Box<MacPat>) -> Option<P<ast::Pat>> { Some(self.p) } } /// A convenience type for macros that return a single item. pub struct MacItem { - i: Gc<ast::Item> + i: P<ast::Item> } impl MacItem { - pub fn new(i: Gc<ast::Item>) -> Box<MacResult+'static> { + pub fn new(i: P<ast::Item>) -> Box<MacResult+'static> { box MacItem { i: i } as Box<MacResult+'static> } } impl MacResult for MacItem { - fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> { + fn make_items(self: Box<MacItem>) -> Option<SmallVector<P<ast::Item>>> { Some(SmallVector::one(self.i)) } - fn make_stmt(&self) -> Option<Gc<ast::Stmt>> { - Some(box(GC) codemap::respan( + fn make_stmt(self: Box<MacItem>) -> Option<P<ast::Stmt>> { + Some(P(codemap::respan( self.i.span, ast::StmtDecl( - box(GC) codemap::respan(self.i.span, ast::DeclItem(self.i)), - ast::DUMMY_NODE_ID))) + P(codemap::respan(self.i.span, ast::DeclItem(self.i))), + ast::DUMMY_NODE_ID)))) } } @@ -250,17 +251,17 @@ impl DummyResult { } /// A plain dummy expression. - pub fn raw_expr(sp: Span) -> Gc<ast::Expr> { - box(GC) ast::Expr { + pub fn raw_expr(sp: Span) -> P<ast::Expr> { + P(ast::Expr { id: ast::DUMMY_NODE_ID, - node: ast::ExprLit(box(GC) codemap::respan(sp, ast::LitNil)), + node: ast::ExprLit(P(codemap::respan(sp, ast::LitNil))), span: sp, - } + }) } /// A plain dummy pattern. - pub fn raw_pat(sp: Span) -> Gc<ast::Pat> { - box(GC) ast::Pat { + pub fn raw_pat(sp: Span) -> ast::Pat { + ast::Pat { id: ast::DUMMY_NODE_ID, node: ast::PatWild(ast::PatWildSingle), span: sp, @@ -270,13 +271,13 @@ impl DummyResult { } impl MacResult for DummyResult { - fn make_expr(&self) -> Option<Gc<ast::Expr>> { + fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> { Some(DummyResult::raw_expr(self.span)) } - fn make_pat(&self) -> Option<Gc<ast::Pat>> { - Some(DummyResult::raw_pat(self.span)) + fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> { + Some(P(DummyResult::raw_pat(self.span))) } - fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> { + fn make_items(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Item>>> { // this code needs a comment... why not always just return the Some() ? if self.expr_only { None @@ -284,17 +285,17 @@ impl MacResult for DummyResult { Some(SmallVector::zero()) } } - fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> { + fn make_methods(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Method>>> { if self.expr_only { None } else { Some(SmallVector::zero()) } } - fn make_stmt(&self) -> Option<Gc<ast::Stmt>> { - Some(box(GC) codemap::respan(self.span, - ast::StmtExpr(DummyResult::raw_expr(self.span), - ast::DUMMY_NODE_ID))) + fn make_stmt(self: Box<DummyResult>) -> Option<P<ast::Stmt>> { + Some(P(codemap::respan(self.span, + ast::StmtExpr(DummyResult::raw_expr(self.span), + ast::DUMMY_NODE_ID)))) } } @@ -461,7 +462,7 @@ pub struct ExtCtxt<'a> { pub mod_path: Vec<ast::Ident> , pub trace_mac: bool, - pub exported_macros: Vec<Gc<ast::Item>>, + pub exported_macros: Vec<P<ast::Item>>, pub syntax_env: SyntaxEnv, } @@ -482,7 +483,7 @@ impl<'a> ExtCtxt<'a> { } #[deprecated = "Replaced with `expander().fold_expr()`"] - pub fn expand_expr(&mut self, e: Gc<ast::Expr>) -> Gc<ast::Expr> { + pub fn expand_expr(&mut self, e: P<ast::Expr>) -> P<ast::Expr> { self.expander().fold_expr(e) } @@ -595,12 +596,12 @@ impl<'a> ExtCtxt<'a> { /// Extract a string literal from the macro expanded version of `expr`, /// emitting `err_msg` if `expr` is not a string literal. This does not stop /// compilation on error, merely emits a non-fatal error and returns None. -pub fn expr_to_string(cx: &mut ExtCtxt, expr: Gc<ast::Expr>, err_msg: &str) - -> Option<(InternedString, ast::StrStyle)> { +pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str) + -> Option<(InternedString, ast::StrStyle)> { // we want to be able to handle e.g. concat("foo", "bar") let expr = cx.expander().fold_expr(expr); match expr.node { - ast::ExprLit(l) => match l.node { + ast::ExprLit(ref l) => match l.node { ast::LitStr(ref s, style) => return Some(((*s).clone(), style)), _ => cx.span_err(l.span, err_msg) }, @@ -651,7 +652,7 @@ pub fn get_single_str_from_tts(cx: &ExtCtxt, /// parsing error, emit a non-fatal error and return None. pub fn get_exprs_from_tts(cx: &mut ExtCtxt, sp: Span, - tts: &[ast::TokenTree]) -> Option<Vec<Gc<ast::Expr>>> { + tts: &[ast::TokenTree]) -> Option<Vec<P<ast::Expr>>> { let mut p = cx.new_parser_from_tts(tts); let mut es = Vec::new(); while p.token != token::EOF { diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 6bd1fba4b58..eda373c4fb8 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -9,19 +9,18 @@ // except according to those terms. use abi; -use ast::{P, Ident, Generics, NodeId, Expr}; +use ast::{Ident, Generics, Expr}; use ast; use ast_util; use attr; use codemap::{Span, respan, Spanned, DUMMY_SP, Pos}; use ext::base::ExtCtxt; -use fold::Folder; use owned_slice::OwnedSlice; use parse::token::special_idents; use parse::token::InternedString; use parse::token; +use ptr::P; -use std::gc::{Gc, GC}; // Transitional reexports so qquote can find the paths it is looking for mod syntax { @@ -64,7 +63,6 @@ pub trait AstBuilder { fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ; fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ; fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField; - fn strip_bounds(&self, bounds: &Generics) -> Generics; fn typaram(&self, span: Span, @@ -83,140 +81,130 @@ pub trait AstBuilder { -> ast::LifetimeDef; // statements - fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt>; - fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, - ex: Gc<ast::Expr>) -> Gc<ast::Stmt>; + fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt>; + fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> P<ast::Stmt>; fn stmt_let_typed(&self, sp: Span, mutbl: bool, ident: ast::Ident, typ: P<ast::Ty>, - ex: Gc<ast::Expr>) - -> Gc<ast::Stmt>; - fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt>; + ex: P<ast::Expr>) + -> P<ast::Stmt>; + fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt>; // blocks - fn block(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>, - expr: Option<Gc<ast::Expr>>) -> P<ast::Block>; - fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block>; + fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>, + expr: Option<P<ast::Expr>>) -> P<ast::Block>; + fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>; fn block_all(&self, span: Span, - view_items: Vec<ast::ViewItem> , - stmts: Vec<Gc<ast::Stmt>> , - expr: Option<Gc<ast::Expr>>) -> P<ast::Block>; + view_items: Vec<ast::ViewItem>, + stmts: Vec<P<ast::Stmt>>, + expr: Option<P<ast::Expr>>) -> P<ast::Block>; // expressions - fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr>; - fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr>; - fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr>; + fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>; + fn expr_path(&self, path: ast::Path) -> P<ast::Expr>; + fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>; - fn expr_self(&self, span: Span) -> Gc<ast::Expr>; + fn expr_self(&self, span: Span) -> P<ast::Expr>; fn expr_binary(&self, sp: Span, op: ast::BinOp, - lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr>; - - fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_field_access(&self, span: Span, expr: Gc<ast::Expr>, - ident: ast::Ident) -> Gc<ast::Expr>; - fn expr_tup_field_access(&self, sp: Span, expr: Gc<ast::Expr>, - idx: uint) -> Gc<ast::Expr>; - fn expr_call(&self, span: Span, expr: Gc<ast::Expr>, - args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>; - fn expr_call_ident(&self, span: Span, id: ast::Ident, - args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>; - fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> , - args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>; + lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>; + fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>; + fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>; + + fn expr_managed(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>; + fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>; + fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>; + fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>; + fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, + idx: uint) -> P<ast::Expr>; + fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>; + fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>; + fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>, + args: Vec<P<ast::Expr>> ) -> P<ast::Expr>; fn expr_method_call(&self, span: Span, - expr: Gc<ast::Expr>, ident: ast::Ident, - args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>; - fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr>; - fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, - ty: P<ast::Ty>) -> Gc<ast::Expr>; - - fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field; - fn expr_struct(&self, span: Span, path: ast::Path, - fields: Vec<ast::Field> ) -> Gc<ast::Expr>; + expr: P<ast::Expr>, ident: ast::Ident, + args: Vec<P<ast::Expr>> ) -> P<ast::Expr>; + fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>; + fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>; + + fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field; + fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>; fn expr_struct_ident(&self, span: Span, id: ast::Ident, - fields: Vec<ast::Field> ) -> Gc<ast::Expr>; + fields: Vec<ast::Field>) -> P<ast::Expr>; - fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr>; + fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>; - fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr>; - fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr>; - fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr>; - fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr>; + fn expr_uint(&self, span: Span, i: uint) -> P<ast::Expr>; + fn expr_int(&self, sp: Span, i: int) -> P<ast::Expr>; + fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>; + fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>; - fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>; - fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr>; - fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>; - fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr>; + fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>; + fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>; + fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>; + fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>; - fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_none(&self, sp: Span) -> Gc<ast::Expr>; + fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>; + fn expr_none(&self, sp: Span) -> P<ast::Expr>; - fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>; + fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>; - fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr>; - fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr>; + fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>; + fn expr_unreachable(&self, span: Span) -> P<ast::Expr>; - fn expr_ok(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_err(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn expr_try(&self, span: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr>; + fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>; + fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>; + fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>; - fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat>; - fn pat_wild(&self, span: Span) -> Gc<ast::Pat>; - fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat>; - fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat>; + fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>; + fn pat_wild(&self, span: Span) -> P<ast::Pat>; + fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>; + fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>; fn pat_ident_binding_mode(&self, span: Span, ident: ast::Ident, - bm: ast::BindingMode) -> Gc<ast::Pat>; - fn pat_enum(&self, span: Span, path: ast::Path, - subpats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>; + bm: ast::BindingMode) -> P<ast::Pat>; + fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>; fn pat_struct(&self, span: Span, - path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat>; - fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>; + path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> P<ast::Pat>; + fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>; - fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>; - fn pat_none(&self, span: Span) -> Gc<ast::Pat>; + fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>; + fn pat_none(&self, span: Span) -> P<ast::Pat>; - fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>; - fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>; + fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>; + fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>; - fn arm(&self, span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm; + fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm; fn arm_unreachable(&self, span: Span) -> ast::Arm; - fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, arms: Vec<ast::Arm> ) -> Gc<ast::Expr>; + fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>; fn expr_if(&self, span: Span, - cond: Gc<ast::Expr>, then: Gc<ast::Expr>, - els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr>; - fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr>; + cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>; + fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>; fn lambda_fn_decl(&self, span: Span, - fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr>; + fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>; - fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr>; - fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr>; - fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr>; + fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>; + fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>; + fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>; - fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>; - fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr>; + fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>; + fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>; + fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>; fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>, - blk: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>; - fn lambda_stmts_0(&self, span: Span, - stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>; - fn lambda_stmts_1(&self, span: Span, - stmts: Vec<Gc<ast::Stmt>>, ident: ast::Ident) -> Gc<ast::Expr>; + blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>; + fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>; + fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>, + ident: ast::Ident) -> P<ast::Expr>; // items fn item(&self, span: Span, - name: Ident, attrs: Vec<ast::Attribute>, - node: ast::Item_) -> Gc<ast::Item>; + name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>; fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg; // FIXME unused self @@ -228,67 +216,64 @@ pub trait AstBuilder { inputs: Vec<ast::Arg> , output: P<ast::Ty>, generics: Generics, - body: P<ast::Block>) -> Gc<ast::Item>; + body: P<ast::Block>) -> P<ast::Item>; fn item_fn(&self, span: Span, name: Ident, inputs: Vec<ast::Arg> , output: P<ast::Ty>, - body: P<ast::Block>) -> Gc<ast::Item>; + body: P<ast::Block>) -> P<ast::Item>; fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant; fn item_enum_poly(&self, span: Span, name: Ident, enum_definition: ast::EnumDef, - generics: Generics) -> Gc<ast::Item>; - fn item_enum(&self, span: Span, name: Ident, - enum_def: ast::EnumDef) -> Gc<ast::Item>; + generics: Generics) -> P<ast::Item>; + fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>; fn item_struct_poly(&self, span: Span, name: Ident, struct_def: ast::StructDef, - generics: Generics) -> Gc<ast::Item>; - fn item_struct(&self, span: Span, name: Ident, - struct_def: ast::StructDef) -> Gc<ast::Item>; + generics: Generics) -> P<ast::Item>; + fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> P<ast::Item>; fn item_mod(&self, span: Span, inner_span: Span, name: Ident, attrs: Vec<ast::Attribute>, - vi: Vec<ast::ViewItem>, - items: Vec<Gc<ast::Item>>) -> Gc<ast::Item>; + vi: Vec<ast::ViewItem> , items: Vec<P<ast::Item>> ) -> P<ast::Item>; fn item_static(&self, span: Span, name: Ident, ty: P<ast::Ty>, mutbl: ast::Mutability, - expr: Gc<ast::Expr>) - -> Gc<ast::Item>; + expr: P<ast::Expr>) + -> P<ast::Item>; fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>, - generics: Generics) -> Gc<ast::Item>; - fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item>; + generics: Generics) -> P<ast::Item>; + fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>; - fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute; + fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute; - fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem>; + fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>; fn meta_list(&self, sp: Span, name: InternedString, - mis: Vec<Gc<ast::MetaItem>>) - -> Gc<ast::MetaItem>; + mis: Vec<P<ast::MetaItem>> ) + -> P<ast::MetaItem>; fn meta_name_value(&self, sp: Span, name: InternedString, value: ast::Lit_) - -> Gc<ast::MetaItem>; + -> P<ast::MetaItem>; fn view_use(&self, sp: Span, - vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem; + vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem; fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem; fn view_use_simple_(&self, sp: Span, vis: ast::Visibility, ident: ast::Ident, path: ast::Path) -> ast::ViewItem; @@ -447,16 +432,6 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect() } - fn strip_bounds(&self, generics: &Generics) -> Generics { - let new_params = generics.ty_params.map(|ty_param| { - ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param } - }); - Generics { - ty_params: new_params, - .. (*generics).clone() - } - } - fn trait_ref(&self, path: ast::Path) -> ast::TraitRef { ast::TraitRef { path: path, @@ -483,27 +458,27 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } } - fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt> { - box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)) + fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> { + P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))) } fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, - ex: Gc<ast::Expr>) -> Gc<ast::Stmt> { + ex: P<ast::Expr>) -> P<ast::Stmt> { let pat = if mutbl { self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable)) } else { self.pat_ident(sp, ident) }; - let local = box(GC) ast::Local { + let local = P(ast::Local { ty: self.ty_infer(sp), pat: pat, init: Some(ex), id: ast::DUMMY_NODE_ID, span: sp, source: ast::LocalLet, - }; + }); let decl = respan(sp, ast::DeclLocal(local)); - box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID)) + P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) } fn stmt_let_typed(&self, @@ -511,46 +486,43 @@ impl<'a> AstBuilder for ExtCtxt<'a> { mutbl: bool, ident: ast::Ident, typ: P<ast::Ty>, - ex: Gc<ast::Expr>) - -> Gc<ast::Stmt> { + ex: P<ast::Expr>) + -> P<ast::Stmt> { let pat = if mutbl { self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable)) } else { self.pat_ident(sp, ident) }; - let local = box(GC) ast::Local { + let local = P(ast::Local { ty: typ, pat: pat, init: Some(ex), id: ast::DUMMY_NODE_ID, span: sp, source: ast::LocalLet, - }; + }); let decl = respan(sp, ast::DeclLocal(local)); - box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID)) + P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) } - fn block(&self, - span: Span, - stmts: Vec<Gc<ast::Stmt>>, - expr: Option<Gc<Expr>>) - -> P<ast::Block> { + fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>, + expr: Option<P<Expr>>) -> P<ast::Block> { self.block_all(span, Vec::new(), stmts, expr) } - fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt> { + fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> { let decl = respan(sp, ast::DeclItem(item)); - box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID)) + P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) } - fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block> { + fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> { self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr)) } fn block_all(&self, span: Span, - view_items: Vec<ast::ViewItem> , - stmts: Vec<Gc<ast::Stmt>>, - expr: Option<Gc<ast::Expr>>) -> P<ast::Block> { + view_items: Vec<ast::ViewItem>, + stmts: Vec<P<ast::Stmt>>, + expr: Option<P<ast::Expr>>) -> P<ast::Block> { P(ast::Block { view_items: view_items, stmts: stmts, @@ -561,42 +533,42 @@ impl<'a> AstBuilder for ExtCtxt<'a> { }) } - fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr> { - box(GC) ast::Expr { + fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> { + P(ast::Expr { id: ast::DUMMY_NODE_ID, node: node, span: span, - } + }) } - fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr> { + fn expr_path(&self, path: ast::Path) -> P<ast::Expr> { self.expr(path.span, ast::ExprPath(path)) } - fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr> { + fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> { self.expr_path(self.path_ident(span, id)) } - fn expr_self(&self, span: Span) -> Gc<ast::Expr> { + fn expr_self(&self, span: Span) -> P<ast::Expr> { self.expr_ident(span, special_idents::self_) } fn expr_binary(&self, sp: Span, op: ast::BinOp, - lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr> { + lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> { self.expr(sp, ast::ExprBinary(op, lhs, rhs)) } - fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> { self.expr_unary(sp, ast::UnDeref, e) } - fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> { self.expr(sp, ast::ExprUnary(op, e)) } - fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_managed(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> { self.expr_unary(sp, ast::UnBox, e) } - fn expr_field_access(&self, sp: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> { + fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> { let field_name = token::get_ident(ident); let field_span = Span { lo: sp.lo - Pos::from_uint(field_name.get().len()), @@ -607,7 +579,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { let id = Spanned { node: ident, span: field_span }; self.expr(sp, ast::ExprField(expr, id, Vec::new())) } - fn expr_tup_field_access(&self, sp: Span, expr: Gc<ast::Expr>, idx: uint) -> Gc<ast::Expr> { + fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: uint) -> P<ast::Expr> { let field_span = Span { lo: sp.lo - Pos::from_uint(idx.to_string().len()), hi: sp.hi, @@ -617,68 +589,67 @@ impl<'a> AstBuilder for ExtCtxt<'a> { let id = Spanned { node: idx, span: field_span }; self.expr(sp, ast::ExprTupField(expr, id, Vec::new())) } - fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> { self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e)) } - fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> { self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e)) } - fn expr_call(&self, span: Span, expr: Gc<ast::Expr>, - args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> { + fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> { self.expr(span, ast::ExprCall(expr, args)) } fn expr_call_ident(&self, span: Span, id: ast::Ident, - args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> { + args: Vec<P<ast::Expr>>) -> P<ast::Expr> { self.expr(span, ast::ExprCall(self.expr_ident(span, id), args)) } fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> , - args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> { + args: Vec<P<ast::Expr>> ) -> P<ast::Expr> { let pathexpr = self.expr_path(self.path_global(sp, fn_path)); self.expr_call(sp, pathexpr, args) } fn expr_method_call(&self, span: Span, - expr: Gc<ast::Expr>, + expr: P<ast::Expr>, ident: ast::Ident, - mut args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> { + mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> { let id = Spanned { node: ident, span: span }; args.unshift(expr); self.expr(span, ast::ExprMethodCall(id, Vec::new(), args)) } - fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr> { + fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> { self.expr(b.span, ast::ExprBlock(b)) } - fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field { + fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field { ast::Field { ident: respan(span, name), expr: e, span: span } } - fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field> ) -> Gc<ast::Expr> { + fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> { self.expr(span, ast::ExprStruct(path, fields, None)) } fn expr_struct_ident(&self, span: Span, - id: ast::Ident, fields: Vec<ast::Field> ) -> Gc<ast::Expr> { + id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> { self.expr_struct(span, self.path_ident(span, id), fields) } - fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr> { - self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit))) + fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> { + self.expr(sp, ast::ExprLit(P(respan(sp, lit)))) } - fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> { + fn expr_uint(&self, span: Span, i: uint) -> P<ast::Expr> { self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyU))) } - fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> { + fn expr_int(&self, sp: Span, i: int) -> P<ast::Expr> { self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyI, ast::Sign::new(i)))) } - fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> { + fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> { self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8))) } - fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> { + fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> { self.expr_lit(sp, ast::LitBool(value)) } - fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> { + fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> { self.expr(sp, ast::ExprVec(exprs)) } - fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr> { + fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> { self.expr_call_global(sp, vec!(self.ident_of("std"), self.ident_of("vec"), @@ -686,19 +657,19 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.ident_of("new")), Vec::new()) } - fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> { + fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> { self.expr_addr_of(sp, self.expr_vec(sp, exprs)) } - fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> { + fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> { self.expr_lit(sp, ast::LitStr(s, ast::CookedStr)) } - fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, ty: P<ast::Ty>) -> Gc<ast::Expr> { + fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> { self.expr(sp, ast::ExprCast(expr, ty)) } - fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> { let some = vec!( self.ident_of("std"), self.ident_of("option"), @@ -706,7 +677,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr_call_global(sp, some, vec!(expr)) } - fn expr_none(&self, sp: Span) -> Gc<ast::Expr> { + fn expr_none(&self, sp: Span) -> P<ast::Expr> { let none = self.path_global(sp, vec!( self.ident_of("std"), self.ident_of("option"), @@ -714,11 +685,11 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr_path(none) } - fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> { + fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> { self.expr(sp, ast::ExprTup(exprs)) } - fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr> { + fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> { let loc = self.codemap().lookup_char_pos(span.lo); let expr_file = self.expr_str(span, token::intern_and_get_ident(loc.file @@ -738,13 +709,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> { expr_file_line_ptr)) } - fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr> { + fn expr_unreachable(&self, span: Span) -> P<ast::Expr> { self.expr_fail(span, InternedString::new( "internal error: entered unreachable code")) } - fn expr_ok(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> { let ok = vec!( self.ident_of("std"), self.ident_of("result"), @@ -752,7 +723,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr_call_global(sp, ok, vec!(expr)) } - fn expr_err(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> { let err = vec!( self.ident_of("std"), self.ident_of("result"), @@ -760,7 +731,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr_call_global(sp, err, vec!(expr)) } - fn expr_try(&self, sp: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> { let ok = self.ident_of("Ok"); let ok_path = self.path_ident(sp, ok); let err = self.ident_of("Err"); @@ -771,11 +742,11 @@ impl<'a> AstBuilder for ExtCtxt<'a> { let binding_expr = self.expr_ident(sp, binding_variable); // Ok(__try_var) pattern - let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat)); + let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone())); // Err(__try_var) (pattern and expression resp.) let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat)); - let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr)); + let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr.clone())); // return Err(__try_var) let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr))); @@ -789,41 +760,41 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } - fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat> { - box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span } + fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> { + P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }) } - fn pat_wild(&self, span: Span) -> Gc<ast::Pat> { + fn pat_wild(&self, span: Span) -> P<ast::Pat> { self.pat(span, ast::PatWild(ast::PatWildSingle)) } - fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat> { + fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> { self.pat(span, ast::PatLit(expr)) } - fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat> { + fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> { self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable)) } fn pat_ident_binding_mode(&self, span: Span, ident: ast::Ident, - bm: ast::BindingMode) -> Gc<ast::Pat> { + bm: ast::BindingMode) -> P<ast::Pat> { let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None); self.pat(span, pat) } - fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<Gc<ast::Pat>> ) -> Gc<ast::Pat> { + fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> { let pat = ast::PatEnum(path, Some(subpats)); self.pat(span, pat) } fn pat_struct(&self, span: Span, - path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat> { + path: ast::Path, field_pats: Vec<ast::FieldPat>) -> P<ast::Pat> { let pat = ast::PatStruct(path, field_pats, false); self.pat(span, pat) } - fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat> { + fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> { let pat = ast::PatTup(pats); self.pat(span, pat) } - fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> { + fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> { let some = vec!( self.ident_of("std"), self.ident_of("option"), @@ -832,7 +803,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.pat_enum(span, path, vec!(pat)) } - fn pat_none(&self, span: Span) -> Gc<ast::Pat> { + fn pat_none(&self, span: Span) -> P<ast::Pat> { let some = vec!( self.ident_of("std"), self.ident_of("option"), @@ -841,7 +812,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.pat_enum(span, path, vec!()) } - fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> { + fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> { let some = vec!( self.ident_of("std"), self.ident_of("result"), @@ -850,7 +821,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.pat_enum(span, path, vec!(pat)) } - fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> { + fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> { let some = vec!( self.ident_of("std"), self.ident_of("result"), @@ -859,7 +830,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.pat_enum(span, path, vec!(pat)) } - fn arm(&self, _span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm { + fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm { ast::Arm { attrs: vec!(), pats: pats, @@ -872,64 +843,62 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span)) } - fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, - arms: Vec<ast::Arm>) -> Gc<Expr> { + fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> { self.expr(span, ast::ExprMatch(arg, arms)) } - fn expr_if(&self, span: Span, - cond: Gc<ast::Expr>, then: Gc<ast::Expr>, - els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr> { + fn expr_if(&self, span: Span, cond: P<ast::Expr>, + then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> { let els = els.map(|x| self.expr_block(self.block_expr(x))); self.expr(span, ast::ExprIf(cond, self.block_expr(then), els)) } - fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr> { + fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> { self.expr(span, ast::ExprLoop(block, None)) } fn lambda_fn_decl(&self, span: Span, - fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr> { + fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> { self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk)) } - fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr> { + fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> { let fn_decl = self.fn_decl( ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(), self.ty_infer(span)); self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk)) } - fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr> { + fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> { self.lambda(span, Vec::new(), blk) } - fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr> { + fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> { self.lambda(span, vec!(ident), blk) } - fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>, + expr: P<ast::Expr>) -> P<ast::Expr> { self.lambda(span, ids, self.block_expr(expr)) } - fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> { self.lambda0(span, self.block_expr(expr)) } - fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> { + fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> { self.lambda1(span, self.block_expr(expr), ident) } fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>, - stmts: Vec<Gc<ast::Stmt>>) - -> Gc<ast::Expr> { + stmts: Vec<P<ast::Stmt>>) + -> P<ast::Expr> { self.lambda(span, ids, self.block(span, stmts, None)) } - fn lambda_stmts_0(&self, span: Span, - stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr> { + fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> { self.lambda0(span, self.block(span, stmts, None)) } - fn lambda_stmts_1(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>, - ident: ast::Ident) -> Gc<ast::Expr> { + fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>, + ident: ast::Ident) -> P<ast::Expr> { self.lambda1(span, self.block(span, stmts, None), ident) } @@ -952,17 +921,18 @@ impl<'a> AstBuilder for ExtCtxt<'a> { }) } - fn item(&self, span: Span, - name: Ident, attrs: Vec<ast::Attribute>, - node: ast::Item_) -> Gc<ast::Item> { + fn item(&self, span: Span, name: Ident, + attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> { // FIXME: Would be nice if our generated code didn't violate // Rust coding conventions - box(GC) ast::Item { ident: name, - attrs: attrs, - id: ast::DUMMY_NODE_ID, - node: node, - vis: ast::Inherited, - span: span } + P(ast::Item { + ident: name, + attrs: attrs, + id: ast::DUMMY_NODE_ID, + node: node, + vis: ast::Inherited, + span: span + }) } fn item_fn_poly(&self, @@ -971,7 +941,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { inputs: Vec<ast::Arg> , output: P<ast::Ty>, generics: Generics, - body: P<ast::Block>) -> Gc<ast::Item> { + body: P<ast::Block>) -> P<ast::Item> { self.item(span, name, Vec::new(), @@ -988,7 +958,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { inputs: Vec<ast::Arg> , output: P<ast::Ty>, body: P<ast::Block> - ) -> Gc<ast::Item> { + ) -> P<ast::Item> { self.item_fn_poly( span, name, @@ -1016,18 +986,18 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn item_enum_poly(&self, span: Span, name: Ident, enum_definition: ast::EnumDef, - generics: Generics) -> Gc<ast::Item> { + generics: Generics) -> P<ast::Item> { self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics)) } fn item_enum(&self, span: Span, name: Ident, - enum_definition: ast::EnumDef) -> Gc<ast::Item> { + enum_definition: ast::EnumDef) -> P<ast::Item> { self.item_enum_poly(span, name, enum_definition, ast_util::empty_generics()) } fn item_struct(&self, span: Span, name: Ident, - struct_def: ast::StructDef) -> Gc<ast::Item> { + struct_def: ast::StructDef) -> P<ast::Item> { self.item_struct_poly( span, name, @@ -1037,14 +1007,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } fn item_struct_poly(&self, span: Span, name: Ident, - struct_def: ast::StructDef, generics: Generics) -> Gc<ast::Item> { - self.item(span, name, Vec::new(), ast::ItemStruct(box(GC) struct_def, generics)) + struct_def: ast::StructDef, generics: Generics) -> P<ast::Item> { + self.item(span, name, Vec::new(), ast::ItemStruct(P(struct_def), generics)) } fn item_mod(&self, span: Span, inner_span: Span, name: Ident, attrs: Vec<ast::Attribute> , vi: Vec<ast::ViewItem> , - items: Vec<Gc<ast::Item>>) -> Gc<ast::Item> { + items: Vec<P<ast::Item>> ) -> P<ast::Item> { self.item( span, name, @@ -1062,21 +1032,21 @@ impl<'a> AstBuilder for ExtCtxt<'a> { name: Ident, ty: P<ast::Ty>, mutbl: ast::Mutability, - expr: Gc<ast::Expr>) - -> Gc<ast::Item> { + expr: P<ast::Expr>) + -> P<ast::Item> { self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr)) } fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>, - generics: Generics) -> Gc<ast::Item> { + generics: Generics) -> P<ast::Item> { self.item(span, name, Vec::new(), ast::ItemTy(ty, generics)) } - fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item> { + fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> { self.item_ty_poly(span, name, ty, ast_util::empty_generics()) } - fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute { + fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute { respan(sp, ast::Attribute_ { id: attr::mk_attr_id(), style: ast::AttrOuter, @@ -1085,26 +1055,26 @@ impl<'a> AstBuilder for ExtCtxt<'a> { }) } - fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem> { - box(GC) respan(sp, ast::MetaWord(w)) + fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> { + P(respan(sp, ast::MetaWord(w))) } fn meta_list(&self, sp: Span, name: InternedString, - mis: Vec<Gc<ast::MetaItem>> ) - -> Gc<ast::MetaItem> { - box(GC) respan(sp, ast::MetaList(name, mis)) + mis: Vec<P<ast::MetaItem>> ) + -> P<ast::MetaItem> { + P(respan(sp, ast::MetaList(name, mis))) } fn meta_name_value(&self, sp: Span, name: InternedString, value: ast::Lit_) - -> Gc<ast::MetaItem> { - box(GC) respan(sp, ast::MetaNameValue(name, respan(sp, value))) + -> P<ast::MetaItem> { + P(respan(sp, ast::MetaNameValue(name, respan(sp, value)))) } fn view_use(&self, sp: Span, - vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem { + vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem { ast::ViewItem { node: ast::ViewItemUse(vp), attrs: Vec::new(), @@ -1121,10 +1091,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn view_use_simple_(&self, sp: Span, vis: ast::Visibility, ident: ast::Ident, path: ast::Path) -> ast::ViewItem { self.view_use(sp, vis, - box(GC) respan(sp, - ast::ViewPathSimple(ident, - path, - ast::DUMMY_NODE_ID))) + P(respan(sp, + ast::ViewPathSimple(ident, + path, + ast::DUMMY_NODE_ID)))) } fn view_use_list(&self, sp: Span, vis: ast::Visibility, @@ -1134,41 +1104,16 @@ impl<'a> AstBuilder for ExtCtxt<'a> { }).collect(); self.view_use(sp, vis, - box(GC) respan(sp, - ast::ViewPathList(self.path(sp, path), - imports, - ast::DUMMY_NODE_ID))) + P(respan(sp, + ast::ViewPathList(self.path(sp, path), + imports, + ast::DUMMY_NODE_ID)))) } fn view_use_glob(&self, sp: Span, vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem { self.view_use(sp, vis, - box(GC) respan(sp, - ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID))) - } -} - -struct Duplicator<'a>; - -impl<'a> Folder for Duplicator<'a> { - fn new_id(&mut self, _: NodeId) -> NodeId { - ast::DUMMY_NODE_ID - } -} - -pub trait Duplicate { - // - // Duplication functions - // - // These functions just duplicate AST nodes. - // - - fn duplicate(&self, cx: &ExtCtxt) -> Self; -} - -impl Duplicate for Gc<ast::Expr> { - fn duplicate(&self, _: &ExtCtxt) -> Gc<ast::Expr> { - let mut folder = Duplicator; - folder.fold_expr(*self) + P(respan(sp, + ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))) } } diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs index 18367511495..3e0f340ad7f 100644 --- a/src/libsyntax/ext/bytes.rs +++ b/src/libsyntax/ext/bytes.rs @@ -40,7 +40,7 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt, for expr in exprs.iter() { match expr.node { // expression is a literal - ast::ExprLit(lit) => match lit.node { + ast::ExprLit(ref lit) => match lit.node { // string literal, push each byte to vector expression ast::LitStr(ref s, _) => { for byte in s.get().bytes() { diff --git a/src/libsyntax/ext/cfg.rs b/src/libsyntax/ext/cfg.rs index 0c3a951c982..79cb47fee7b 100644 --- a/src/libsyntax/ext/cfg.rs +++ b/src/libsyntax/ext/cfg.rs @@ -40,10 +40,10 @@ pub fn expand_cfg<'cx>(cx: &mut ExtCtxt, } // test_cfg searches for meta items looking like `cfg(foo, ...)` - let in_cfg = &[cx.meta_list(sp, InternedString::new("cfg"), cfgs)]; + let in_cfg = Some(cx.meta_list(sp, InternedString::new("cfg"), cfgs)); let matches_cfg = attr::test_cfg(cx.cfg().as_slice(), - in_cfg.iter().map(|&x| x)); + in_cfg.iter()); let e = cx.expr_bool(sp, matches_cfg); MacExpr::new(e) } diff --git a/src/libsyntax/ext/concat.rs b/src/libsyntax/ext/concat.rs index ea7a4d061c0..455148bfedd 100644 --- a/src/libsyntax/ext/concat.rs +++ b/src/libsyntax/ext/concat.rs @@ -27,7 +27,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, let mut accumulator = String::new(); for e in es.move_iter() { match e.node { - ast::ExprLit(lit) => { + ast::ExprLit(ref lit) => { match lit.node { ast::LitStr(ref s, _) | ast::LitFloat(ref s, _) | diff --git a/src/libsyntax/ext/concat_idents.rs b/src/libsyntax/ext/concat_idents.rs index 0ac26a3a904..145412caa0b 100644 --- a/src/libsyntax/ext/concat_idents.rs +++ b/src/libsyntax/ext/concat_idents.rs @@ -15,8 +15,7 @@ use ext::base; use owned_slice::OwnedSlice; use parse::token; use parse::token::{str_to_ident}; - -use std::gc::GC; +use ptr::P; pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> Box<base::MacResult+'cx> { @@ -44,7 +43,7 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree] } let res = str_to_ident(res_str.as_slice()); - let e = box(GC) ast::Expr { + let e = P(ast::Expr { id: ast::DUMMY_NODE_ID, node: ast::ExprPath( ast::Path { @@ -60,6 +59,6 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree] } ), span: sp, - }; + }); MacExpr::new(e) } diff --git a/src/libsyntax/ext/deriving/bounds.rs b/src/libsyntax/ext/deriving/bounds.rs index 7cff6e8ff3c..0595b0bc7f4 100644 --- a/src/libsyntax/ext/deriving/bounds.rs +++ b/src/libsyntax/ext/deriving/bounds.rs @@ -13,14 +13,13 @@ use codemap::Span; use ext::base::ExtCtxt; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_bound(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let name = match mitem.node { MetaWord(ref tname) => { diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index bbe96018f4b..64607ffd5d4 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -15,14 +15,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_clone(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let inline = cx.meta_word(span, InternedString::new("inline")); let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { @@ -52,12 +51,12 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, fn cs_clone( name: &str, cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { + substr: &Substructure) -> P<Expr> { let clone_ident = substr.method_ident; let ctor_ident; let all_fields; let subcall = |field: &FieldInfo| - cx.expr_method_call(field.span, field.self_, clone_ident, Vec::new()); + cx.expr_method_call(field.span, field.self_.clone(), clone_ident, Vec::new()); match *substr.fields { Struct(ref af) => { diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs index 19a979a5655..a27016fde61 100644 --- a/src/libsyntax/ext/deriving/cmp/eq.rs +++ b/src/libsyntax/ext/deriving/cmp/eq.rs @@ -15,21 +15,20 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_eq(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { // structures are equal if all fields are equal, and non equal, if // any fields are not equal or if the enum variants are different - fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> Gc<Expr> { + fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> { cs_and(|cx, span, _, _| cx.expr_bool(span, false), cx, span, substr) } - fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> Gc<Expr> { + fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> { cs_or(|cx, span, _, _| cx.expr_bool(span, true), cx, span, substr) } diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index dcf59ba820e..7cb61d295c0 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -16,14 +16,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_ord(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { macro_rules! md ( ($name:expr, $op:expr, $equal:expr) => { { let inline = cx.meta_word(span, InternedString::new("inline")); @@ -87,7 +86,7 @@ pub enum OrderingOp { pub fn some_ordering_collapsed(cx: &mut ExtCtxt, span: Span, op: OrderingOp, - self_arg_tags: &[ast::Ident]) -> Gc<ast::Expr> { + self_arg_tags: &[ast::Ident]) -> P<ast::Expr> { let lft = cx.expr_ident(span, self_arg_tags[0]); let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1])); let op_str = match op { @@ -99,7 +98,7 @@ pub fn some_ordering_collapsed(cx: &mut ExtCtxt, } pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, - substr: &Substructure) -> Gc<Expr> { + substr: &Substructure) -> P<Expr> { let test_id = cx.ident_of("__test"); let ordering = cx.path_global(span, vec!(cx.ident_of("std"), @@ -159,8 +158,8 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, } /// Strict inequality. -fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt, span: Span, - substr: &Substructure) -> Gc<Expr> { +fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt, + span: Span, substr: &Substructure) -> P<Expr> { let op = if less {ast::BiLt} else {ast::BiGt}; cs_fold( false, // need foldr, @@ -183,14 +182,14 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt, span: Span, layers of pointers, if the type includes pointers. */ let other_f = match other_fs { - [o_f] => o_f, + [ref o_f] => o_f, _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`") }; - let cmp = cx.expr_binary(span, op, self_f, other_f); + let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone()); let not_cmp = cx.expr_unary(span, ast::UnNot, - cx.expr_binary(span, op, other_f, self_f)); + cx.expr_binary(span, op, other_f.clone(), self_f)); let and = cx.expr_binary(span, ast::BiAnd, not_cmp, subexpr); cx.expr_binary(span, ast::BiOr, cmp, and) diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs index 42365936c9d..98c8885f7fa 100644 --- a/src/libsyntax/ext/deriving/cmp/totaleq.rs +++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs @@ -15,16 +15,14 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_totaleq(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { - fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, - substr: &Substructure) -> Gc<Expr> { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { + fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> { cs_same_method(|cx, span, exprs| { // create `a.<method>(); b.<method>(); c.<method>(); ...` // (where method is `assert_receiver_is_total_eq`) diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs index e010b635fe4..9ef463f9c63 100644 --- a/src/libsyntax/ext/deriving/cmp/totalord.rs +++ b/src/libsyntax/ext/deriving/cmp/totalord.rs @@ -16,14 +16,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_totalord(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let inline = cx.meta_word(span, InternedString::new("inline")); let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { @@ -53,14 +52,14 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt, pub fn ordering_collapsed(cx: &mut ExtCtxt, span: Span, - self_arg_tags: &[ast::Ident]) -> Gc<ast::Expr> { + self_arg_tags: &[ast::Ident]) -> P<ast::Expr> { let lft = cx.expr_ident(span, self_arg_tags[0]); let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1])); cx.expr_method_call(span, lft, cx.ident_of("cmp"), vec![rgt]) } pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, - substr: &Substructure) -> Gc<Expr> { + substr: &Substructure) -> P<Expr> { let test_id = cx.ident_of("__test"); let equals_path = cx.path_global(span, vec!(cx.ident_of("std"), diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index d909ffd2b49..fd24f5e35a4 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -21,14 +21,13 @@ use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; use parse::token; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_decodable(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let trait_def = TraitDef { span: span, attributes: Vec::new(), @@ -64,15 +63,15 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt, } fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { - let decoder = substr.nonself_args[0]; + substr: &Substructure) -> P<Expr> { + let decoder = substr.nonself_args[0].clone(); let recurse = vec!(cx.ident_of("serialize"), cx.ident_of("Decodable"), cx.ident_of("decode")); // throw an underscore in front to suppress unused variable warnings let blkarg = cx.ident_of("_d"); let blkdecoder = cx.expr_ident(trait_span, blkarg); - let calldecode = cx.expr_call_global(trait_span, recurse, vec!(blkdecoder)); + let calldecode = cx.expr_call_global(trait_span, recurse, vec!(blkdecoder.clone())); let lambdadecode = cx.lambda_expr_1(trait_span, calldecode, blkarg); return match *substr.fields { @@ -89,10 +88,10 @@ fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span, summary, |cx, span, name, field| { cx.expr_try(span, - cx.expr_method_call(span, blkdecoder, read_struct_field, + cx.expr_method_call(span, blkdecoder.clone(), read_struct_field, vec!(cx.expr_str(span, name), cx.expr_uint(span, field), - lambdadecode))) + lambdadecode.clone()))) }); let result = cx.expr_ok(trait_span, result); cx.expr_method_call(trait_span, @@ -121,8 +120,8 @@ fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span, |cx, span, _, field| { let idx = cx.expr_uint(span, field); cx.expr_try(span, - cx.expr_method_call(span, blkdecoder, rvariant_arg, - vec!(idx, lambdadecode))) + cx.expr_method_call(span, blkdecoder.clone(), rvariant_arg, + vec!(idx, lambdadecode.clone()))) }); arms.push(cx.arm(v_span, @@ -159,8 +158,8 @@ fn decode_static_fields(cx: &mut ExtCtxt, trait_span: Span, outer_pat_ident: Ident, fields: &StaticFields, - getarg: |&mut ExtCtxt, Span, InternedString, uint| -> Gc<Expr>) - -> Gc<Expr> { + getarg: |&mut ExtCtxt, Span, InternedString, uint| -> P<Expr>) + -> P<Expr> { match *fields { Unnamed(ref fields) => { if fields.is_empty() { diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs index f7d0308e1bd..f4a66414d89 100644 --- a/src/libsyntax/ext/deriving/default.rs +++ b/src/libsyntax/ext/deriving/default.rs @@ -15,14 +15,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_default(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let inline = cx.meta_word(span, InternedString::new("inline")); let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { @@ -47,8 +46,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt, trait_def.expand(cx, mitem, item, push) } -fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { +fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> { let default_ident = vec!( cx.ident_of("std"), cx.ident_of("default"), diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 02a748eed8e..103253560df 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -86,14 +86,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_encodable(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let trait_def = TraitDef { span: span, attributes: Vec::new(), @@ -131,8 +130,8 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt, } fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { - let encoder = substr.nonself_args[0]; + substr: &Substructure) -> P<Expr> { + let encoder = substr.nonself_args[0].clone(); // throw an underscore in front to suppress unused variable warnings let blkarg = cx.ident_of("_e"); let blkencoder = cx.expr_ident(trait_span, blkarg); @@ -145,7 +144,7 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span, let last = fields.len() - 1; for (i, &FieldInfo { name, - self_, + ref self_, span, .. }) in fields.iter().enumerate() { @@ -156,9 +155,10 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span, i).as_slice()) } }; - let enc = cx.expr_method_call(span, self_, encode, vec!(blkencoder)); + let enc = cx.expr_method_call(span, self_.clone(), + encode, vec!(blkencoder.clone())); let lambda = cx.lambda_expr_1(span, enc, blkarg); - let call = cx.expr_method_call(span, blkencoder, + let call = cx.expr_method_call(span, blkencoder.clone(), emit_struct_field, vec!(cx.expr_str(span, name), cx.expr_uint(span, i), @@ -202,10 +202,11 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span, let emit_variant_arg = cx.ident_of("emit_enum_variant_arg"); let mut stmts = Vec::new(); let last = fields.len() - 1; - for (i, &FieldInfo { self_, span, .. }) in fields.iter().enumerate() { - let enc = cx.expr_method_call(span, self_, encode, vec!(blkencoder)); + for (i, &FieldInfo { ref self_, span, .. }) in fields.iter().enumerate() { + let enc = cx.expr_method_call(span, self_.clone(), + encode, vec!(blkencoder.clone())); let lambda = cx.lambda_expr_1(span, enc, blkarg); - let call = cx.expr_method_call(span, blkencoder, + let call = cx.expr_method_call(span, blkencoder.clone(), emit_variant_arg, vec!(cx.expr_uint(span, i), lambda)); diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 50bdc296aad..53af5a86ed2 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -181,12 +181,13 @@ //! ~~~ use std::cell::RefCell; -use std::gc::{Gc, GC}; +use std::gc::GC; +use std::vec; use abi::Abi; use abi; use ast; -use ast::{P, EnumDef, Expr, Ident, Generics, StructDef}; +use ast::{EnumDef, Expr, Ident, Generics, StructDef}; use ast_util; use attr; use attr::AttrMetaMethods; @@ -194,9 +195,11 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use codemap; use codemap::Span; +use fold::MoveMap; use owned_slice::OwnedSlice; use parse::token::InternedString; use parse::token::special_idents; +use ptr::P; use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self, Ty}; @@ -251,9 +254,9 @@ pub struct Substructure<'a> { /// ident of the method pub method_ident: Ident, /// dereferenced access to any Self or Ptr(Self, _) arguments - pub self_args: &'a [Gc<Expr>], + pub self_args: &'a [P<Expr>], /// verbatim access to any other arguments - pub nonself_args: &'a [Gc<Expr>], + pub nonself_args: &'a [P<Expr>], pub fields: &'a SubstructureFields<'a> } @@ -265,10 +268,10 @@ pub struct FieldInfo { pub name: Option<Ident>, /// The expression corresponding to this field of `self` /// (specifically, a reference to it). - pub self_: Gc<Expr>, + pub self_: P<Expr>, /// The expressions corresponding to references to this field in /// the other Self arguments. - pub other: Vec<Gc<Expr>>, + pub other: Vec<P<Expr>>, } /// Fields for a static method @@ -298,7 +301,7 @@ pub enum SubstructureFields<'a> { Idents bound to the variant index values for each of the actual input Self arguments. */ - EnumNonMatchingCollapsed(Vec<Ident>, &'a [Gc<ast::Variant>], &'a [Ident]), + EnumNonMatchingCollapsed(Vec<Ident>, &'a [P<ast::Variant>], &'a [Ident]), /// A static method where Self is a struct. StaticStruct(&'a ast::StructDef, StaticFields), @@ -313,7 +316,7 @@ Combine the values of all the fields together. The last argument is all the fields of all the structures, see above for details. */ pub type CombineSubstructureFunc<'a> = - |&mut ExtCtxt, Span, &Substructure|: 'a -> Gc<Expr>; + |&mut ExtCtxt, Span, &Substructure|: 'a -> P<Expr>; /** Deal with non-matching enum variants. The tuple is a list of @@ -324,10 +327,10 @@ last argument is all the non-Self args of the method being derived. */ pub type EnumNonMatchCollapsedFunc<'a> = |&mut ExtCtxt, - Span, - (&[Ident], &[Ident]), - &[Gc<Expr>]|: 'a - -> Gc<Expr>; + Span, + (&[Ident], &[Ident]), + &[P<Expr>]|: 'a + -> P<Expr>; pub fn combine_substructure<'a>(f: CombineSubstructureFunc<'a>) -> RefCell<CombineSubstructureFunc<'a>> { @@ -338,9 +341,9 @@ pub fn combine_substructure<'a>(f: CombineSubstructureFunc<'a>) impl<'a> TraitDef<'a> { pub fn expand(&self, cx: &mut ExtCtxt, - _mitem: Gc<ast::MetaItem>, - item: Gc<ast::Item>, - push: |Gc<ast::Item>|) { + _mitem: &ast::MetaItem, + item: &ast::Item, + push: |P<ast::Item>|) { let newitem = match item.node { ast::ItemStruct(ref struct_def, ref generics) => { self.expand_struct_def(cx, @@ -365,10 +368,10 @@ impl<'a> TraitDef<'a> { _ => false, } }).map(|a| a.clone())); - push(box(GC) ast::Item { + push(P(ast::Item { attrs: attrs, ..(*newitem).clone() - }) + })) } /** @@ -387,7 +390,7 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt, type_ident: Ident, generics: &Generics, - methods: Vec<Gc<ast::Method>> ) -> Gc<ast::Item> { + methods: Vec<P<ast::Method>>) -> P<ast::Item> { let trait_path = self.path.to_path(cx, self.span, type_ident, generics); let Generics { mut lifetimes, ty_params, where_clause: _ } = @@ -475,7 +478,7 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt, struct_def: &StructDef, type_ident: Ident, - generics: &Generics) -> Gc<ast::Item> { + generics: &Generics) -> P<ast::Item> { let methods = self.methods.iter().map(|method_def| { let (explicit_self, self_args, nonself_args, tys) = method_def.split_self_nonself_args( @@ -515,7 +518,7 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt, enum_def: &EnumDef, type_ident: Ident, - generics: &Generics) -> Gc<ast::Item> { + generics: &Generics) -> P<ast::Item> { let methods = self.methods.iter().map(|method_def| { let (explicit_self, self_args, nonself_args, tys) = method_def.split_self_nonself_args(cx, self, @@ -534,7 +537,7 @@ impl<'a> TraitDef<'a> { self, enum_def, type_ident, - self_args.as_slice(), + self_args, nonself_args.as_slice()) }; @@ -553,7 +556,7 @@ impl<'a> TraitDef<'a> { } fn variant_to_pat(cx: &mut ExtCtxt, sp: Span, variant: &ast::Variant) - -> Gc<ast::Pat> { + -> P<ast::Pat> { let ident = cx.path_ident(sp, variant.node.name); cx.pat(sp, match variant.node.kind { ast::TupleVariantKind(..) => ast::PatEnum(ident, None), @@ -566,10 +569,10 @@ impl<'a> MethodDef<'a> { cx: &mut ExtCtxt, trait_: &TraitDef, type_ident: Ident, - self_args: &[Gc<Expr>], - nonself_args: &[Gc<Expr>], + self_args: &[P<Expr>], + nonself_args: &[P<Expr>], fields: &SubstructureFields) - -> Gc<Expr> { + -> P<Expr> { let substructure = Substructure { type_ident: type_ident, method_ident: cx.ident_of(self.name), @@ -600,8 +603,7 @@ impl<'a> MethodDef<'a> { trait_: &TraitDef, type_ident: Ident, generics: &Generics) - -> (ast::ExplicitSelf, Vec<Gc<Expr>>, Vec<Gc<Expr>>, - Vec<(Ident, P<ast::Ty>)>) { + -> (ast::ExplicitSelf, Vec<P<Expr>>, Vec<P<Expr>>, Vec<(Ident, P<ast::Ty>)>) { let mut self_args = Vec::new(); let mut nonself_args = Vec::new(); @@ -654,8 +656,7 @@ impl<'a> MethodDef<'a> { abi: Abi, explicit_self: ast::ExplicitSelf, arg_types: Vec<(Ident, P<ast::Ty>)> , - body: Gc<Expr>) - -> Gc<ast::Method> { + body: P<Expr>) -> P<ast::Method> { // create the generics that aren't for Self let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics); @@ -678,7 +679,7 @@ impl<'a> MethodDef<'a> { let body_block = cx.block_expr(body); // Create the method. - box(GC) ast::Method { + P(ast::Method { attrs: self.attributes.clone(), id: ast::DUMMY_NODE_ID, span: trait_.span, @@ -690,7 +691,7 @@ impl<'a> MethodDef<'a> { fn_decl, body_block, ast::Inherited) - } + }) } /** @@ -719,9 +720,9 @@ impl<'a> MethodDef<'a> { trait_: &TraitDef, struct_def: &StructDef, type_ident: Ident, - self_args: &[Gc<Expr>], - nonself_args: &[Gc<Expr>]) - -> Gc<Expr> { + self_args: &[P<Expr>], + nonself_args: &[P<Expr>]) + -> P<Expr> { let mut raw_fields = Vec::new(); // ~[[fields of self], // [fields of next Self arg], [etc]] @@ -740,20 +741,20 @@ impl<'a> MethodDef<'a> { // transpose raw_fields let fields = if raw_fields.len() > 0 { - raw_fields.get(0) - .iter() - .enumerate() - .map(|(i, &(span, opt_id, field))| { - let other_fields = raw_fields.tail().iter().map(|l| { - match l.get(i) { - &(_, _, ex) => ex - } - }).collect(); + let mut raw_fields = raw_fields.move_iter().map(|v| v.move_iter()); + let first_field = raw_fields.next().unwrap(); + let mut other_fields: Vec<vec::MoveItems<(Span, Option<Ident>, P<Expr>)>> + = raw_fields.collect(); + first_field.map(|(span, opt_id, field)| { FieldInfo { span: span, name: opt_id, self_: field, - other: other_fields + other: other_fields.mut_iter().map(|l| { + match l.next().unwrap() { + (_, _, ex) => ex + } + }).collect() } }).collect() } else { @@ -774,9 +775,9 @@ impl<'a> MethodDef<'a> { // make a series of nested matches, to destructure the // structs. This is actually right-to-left, but it shouldn't // matter. - for (&arg_expr, &pat) in self_args.iter().zip(patterns.iter()) { - body = cx.expr_match(trait_.span, arg_expr, - vec!( cx.arm(trait_.span, vec!(pat), body) )) + for (arg_expr, pat) in self_args.iter().zip(patterns.iter()) { + body = cx.expr_match(trait_.span, arg_expr.clone(), + vec!( cx.arm(trait_.span, vec!(pat.clone()), body) )) } body } @@ -786,9 +787,9 @@ impl<'a> MethodDef<'a> { trait_: &TraitDef, struct_def: &StructDef, type_ident: Ident, - self_args: &[Gc<Expr>], - nonself_args: &[Gc<Expr>]) - -> Gc<Expr> { + self_args: &[P<Expr>], + nonself_args: &[P<Expr>]) + -> P<Expr> { let summary = trait_.summarise_struct(cx, struct_def); self.call_substructure_method(cx, @@ -834,9 +835,9 @@ impl<'a> MethodDef<'a> { trait_: &TraitDef, enum_def: &EnumDef, type_ident: Ident, - self_args: &[Gc<Expr>], - nonself_args: &[Gc<Expr>]) - -> Gc<Expr> { + self_args: Vec<P<Expr>>, + nonself_args: &[P<Expr>]) + -> P<Expr> { self.build_enum_match_tuple( cx, trait_, enum_def, type_ident, self_args, nonself_args) } @@ -875,8 +876,8 @@ impl<'a> MethodDef<'a> { trait_: &TraitDef, enum_def: &EnumDef, type_ident: Ident, - self_args: &[Gc<Expr>], - nonself_args: &[Gc<Expr>]) -> Gc<Expr> { + self_args: Vec<P<Expr>>, + nonself_args: &[P<Expr>]) -> P<Expr> { let sp = trait_.span; let variants = &enum_def.variants; @@ -898,7 +899,7 @@ impl<'a> MethodDef<'a> { // The `vi_idents` will be bound, solely in the catch-all, to // a series of let statements mapping each self_arg to a uint // corresponding to its variant index. - let vi_idents : Vec<ast::Ident> = self_arg_names.iter() + let vi_idents: Vec<ast::Ident> = self_arg_names.iter() .map(|name| { let vi_suffix = format!("{:s}_vi", name.as_slice()); cx.ident_of(vi_suffix.as_slice()) }) .collect::<Vec<ast::Ident>>(); @@ -914,24 +915,29 @@ impl<'a> MethodDef<'a> { // (Variant2, Variant2, ...) => Body2 // ... // where each tuple has length = self_args.len() - let mut match_arms : Vec<ast::Arm> = variants.iter().enumerate() - .map(|(index, &variant)| { - - // These self_pats have form Variant1, Variant2, ... - let self_pats : Vec<(Gc<ast::Pat>, - Vec<(Span, Option<Ident>, Gc<Expr>)>)>; - self_pats = self_arg_names.iter() - .map(|self_arg_name| - trait_.create_enum_variant_pattern( - cx, &*variant, self_arg_name.as_slice(), - ast::MutImmutable)) - .collect(); + let mut match_arms: Vec<ast::Arm> = variants.iter().enumerate() + .map(|(index, variant)| { + let mk_self_pat = |cx: &mut ExtCtxt, self_arg_name: &str| { + let (p, idents) = trait_.create_enum_variant_pattern(cx, &**variant, + self_arg_name, + ast::MutImmutable); + (cx.pat(sp, ast::PatRegion(p)), idents) + }; // A single arm has form (&VariantK, &VariantK, ...) => BodyK // (see "Final wrinkle" note below for why.) - let subpats = self_pats.iter() - .map(|&(p, ref _idents)| cx.pat(sp, ast::PatRegion(p))) - .collect::<Vec<Gc<ast::Pat>>>(); + let mut subpats = Vec::with_capacity(self_arg_names.len()); + let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1); + let first_self_pat_idents = { + let (p, idents) = mk_self_pat(cx, self_arg_names[0].as_slice()); + subpats.push(p); + idents + }; + for self_arg_name in self_arg_names.tail().iter() { + let (p, idents) = mk_self_pat(cx, self_arg_name.as_slice()); + subpats.push(p); + self_pats_idents.push(idents); + } // Here is the pat = `(&VariantK, &VariantK, ...)` let single_pat = cx.pat(sp, ast::PatTup(subpats)); @@ -941,39 +947,33 @@ impl<'a> MethodDef<'a> { // we are in. // All of the Self args have the same variant in these - // cases. So we transpose the info in self_pats to - // gather the getter expressions together, in the form - // that EnumMatching expects. + // cases. So we transpose the info in self_pats_idents + // to gather the getter expressions together, in the + // form that EnumMatching expects. // The transposition is driven by walking across the // arg fields of the variant for the first self pat. - let &(_, ref self_arg_fields) = self_pats.get(0); - - let field_tuples : Vec<FieldInfo>; - - field_tuples = self_arg_fields.iter().enumerate() + let field_tuples = first_self_pat_idents.move_iter().enumerate() // For each arg field of self, pull out its getter expr ... - .map(|(field_index, &(sp, opt_ident, self_getter_expr))| { + .map(|(field_index, (sp, opt_ident, self_getter_expr))| { // ... but FieldInfo also wants getter expr // for matching other arguments of Self type; - // so walk across the *other* self_pats and - // pull out getter for same field in each of - // them (using `field_index` tracked above). + // so walk across the *other* self_pats_idents + // and pull out getter for same field in each + // of them (using `field_index` tracked above). // That is the heart of the transposition. - let others = self_pats.tail().iter() - .map(|&(_pat, ref fields)| { + let others = self_pats_idents.iter().map(|fields| { + let &(_, _opt_ident, ref other_getter_expr) = + fields.get(field_index); - let &(_, _opt_ident, other_getter_expr) = - fields.get(field_index); + // All Self args have same variant, so + // opt_idents are the same. (Assert + // here to make it self-evident that + // it is okay to ignore `_opt_ident`.) + assert!(opt_ident == _opt_ident); - // All Self args have same variant, so - // opt_idents are the same. (Assert - // here to make it self-evident that - // it is okay to ignore `_opt_ident`.) - assert!(opt_ident == _opt_ident); - - other_getter_expr - }).collect::<Vec<Gc<Expr>>>(); + other_getter_expr.clone() + }).collect::<Vec<P<Expr>>>(); FieldInfo { span: sp, name: opt_ident, @@ -987,10 +987,10 @@ impl<'a> MethodDef<'a> { // Self arg, assuming all are instances of VariantK. // Build up code associated with such a case. let substructure = EnumMatching(index, - &*variant, + &**variant, field_tuples); let arm_expr = self.call_substructure_method( - cx, trait_, type_ident, self_args, nonself_args, + cx, trait_, type_ident, self_args.as_slice(), nonself_args, &substructure); cx.arm(sp, vec![single_pat], arm_expr) @@ -1012,9 +1012,9 @@ impl<'a> MethodDef<'a> { // unreachable-pattern error. // if variants.len() > 1 && self_args.len() > 1 { - let arms : Vec<ast::Arm> = variants.iter().enumerate() - .map(|(index, &variant)| { - let pat = variant_to_pat(cx, sp, &*variant); + let arms: Vec<ast::Arm> = variants.iter().enumerate() + .map(|(index, variant)| { + let pat = variant_to_pat(cx, sp, &**variant); let lit = ast::LitInt(index as u64, ast::UnsignedIntLit(ast::TyU)); cx.arm(sp, vec![pat], cx.expr_lit(sp, lit)) }).collect(); @@ -1035,15 +1035,15 @@ impl<'a> MethodDef<'a> { // A => 0u, B(..) => 1u, C(..) => 2u // }; // ``` - let mut index_let_stmts : Vec<Gc<ast::Stmt>> = Vec::new(); - for (&ident, &self_arg) in vi_idents.iter().zip(self_args.iter()) { - let variant_idx = cx.expr_match(sp, self_arg, arms.clone()); + let mut index_let_stmts: Vec<P<ast::Stmt>> = Vec::new(); + for (&ident, self_arg) in vi_idents.iter().zip(self_args.iter()) { + let variant_idx = cx.expr_match(sp, self_arg.clone(), arms.clone()); let let_stmt = cx.stmt_let(sp, false, ident, variant_idx); index_let_stmts.push(let_stmt); } let arm_expr = self.call_substructure_method( - cx, trait_, type_ident, self_args, nonself_args, + cx, trait_, type_ident, self_args.as_slice(), nonself_args, &catch_all_substructure); // Builds the expression: @@ -1124,9 +1124,7 @@ impl<'a> MethodDef<'a> { // them when they are fed as r-values into a tuple // expression; here add a layer of borrowing, turning // `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`. - let borrowed_self_args = self_args.iter() - .map(|&self_arg| cx.expr_addr_of(sp, self_arg)) - .collect::<Vec<Gc<ast::Expr>>>(); + let borrowed_self_args = self_args.move_map(|self_arg| cx.expr_addr_of(sp, self_arg)); let match_arg = cx.expr(sp, ast::ExprTup(borrowed_self_args)); cx.expr_match(sp, match_arg, match_arms) } @@ -1136,9 +1134,9 @@ impl<'a> MethodDef<'a> { trait_: &TraitDef, enum_def: &EnumDef, type_ident: Ident, - self_args: &[Gc<Expr>], - nonself_args: &[Gc<Expr>]) - -> Gc<Expr> { + self_args: &[P<Expr>], + nonself_args: &[P<Expr>]) + -> P<Expr> { let summary = enum_def.variants.iter().map(|v| { let ident = v.node.name; let summary = match v.node.kind { @@ -1210,11 +1208,11 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt, field_paths: Vec<ast::SpannedIdent> , mutbl: ast::Mutability) - -> Vec<Gc<ast::Pat>> { + -> Vec<P<ast::Pat>> { field_paths.iter().map(|path| { cx.pat(path.span, ast::PatIdent(ast::BindByRef(mutbl), (*path).clone(), None)) - }).collect() + }).collect() } fn create_struct_pattern(&self, @@ -1223,7 +1221,7 @@ impl<'a> TraitDef<'a> { struct_def: &StructDef, prefix: &str, mutbl: ast::Mutability) - -> (Gc<ast::Pat>, Vec<(Span, Option<Ident>, Gc<Expr>)>) { + -> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>)>) { if struct_def.fields.is_empty() { return ( cx.pat_ident_binding_mode( @@ -1266,7 +1264,7 @@ impl<'a> TraitDef<'a> { // struct_type is definitely not Unknown, since struct_def.fields // must be nonempty to reach here let pattern = if struct_type == Record { - let field_pats = subpats.iter().zip(ident_expr.iter()).map(|(&pat, &(_, id, _))| { + let field_pats = subpats.move_iter().zip(ident_expr.iter()).map(|(pat, &(_, id, _))| { // id is guaranteed to be Some ast::FieldPat { ident: id.unwrap(), pat: pat } }).collect(); @@ -1283,7 +1281,7 @@ impl<'a> TraitDef<'a> { variant: &ast::Variant, prefix: &str, mutbl: ast::Mutability) - -> (Gc<ast::Pat>, Vec<(Span, Option<Ident>, Gc<Expr>)> ) { + -> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>)>) { let variant_ident = variant.node.name; match variant.node.kind { ast::TupleVariantKind(ref variant_args) => { @@ -1327,13 +1325,13 @@ Fold the fields. `use_foldl` controls whether this is done left-to-right (`true`) or right-to-left (`false`). */ pub fn cs_fold(use_foldl: bool, - f: |&mut ExtCtxt, Span, Gc<Expr>, Gc<Expr>, &[Gc<Expr>]| -> Gc<Expr>, - base: Gc<Expr>, + f: |&mut ExtCtxt, Span, P<Expr>, P<Expr>, &[P<Expr>]| -> P<Expr>, + base: P<Expr>, enum_nonmatch_f: EnumNonMatchCollapsedFunc, cx: &mut ExtCtxt, trait_span: Span, substructure: &Substructure) - -> Gc<Expr> { + -> P<Expr> { match *substructure.fields { EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => { if use_foldl { @@ -1341,7 +1339,7 @@ pub fn cs_fold(use_foldl: bool, f(cx, field.span, old, - field.self_, + field.self_.clone(), field.other.as_slice()) }) } else { @@ -1349,7 +1347,7 @@ pub fn cs_fold(use_foldl: bool, f(cx, field.span, old, - field.self_, + field.self_.clone(), field.other.as_slice()) }) } @@ -1374,21 +1372,21 @@ f(cx, span, ~[self_1.method(__arg_1_1, __arg_2_1), ~~~ */ #[inline] -pub fn cs_same_method(f: |&mut ExtCtxt, Span, Vec<Gc<Expr>>| -> Gc<Expr>, +pub fn cs_same_method(f: |&mut ExtCtxt, Span, Vec<P<Expr>>| -> P<Expr>, enum_nonmatch_f: EnumNonMatchCollapsedFunc, cx: &mut ExtCtxt, trait_span: Span, substructure: &Substructure) - -> Gc<Expr> { + -> P<Expr> { match *substructure.fields { EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => { // call self_n.method(other_1_n, other_2_n, ...) let called = all_fields.iter().map(|field| { cx.expr_method_call(field.span, - field.self_, + field.self_.clone(), substructure.method_ident, field.other.iter() - .map(|e| cx.expr_addr_of(field.span, *e)) + .map(|e| cx.expr_addr_of(field.span, e.clone())) .collect()) }).collect(); @@ -1410,21 +1408,21 @@ fields. `use_foldl` controls whether this is done left-to-right */ #[inline] pub fn cs_same_method_fold(use_foldl: bool, - f: |&mut ExtCtxt, Span, Gc<Expr>, Gc<Expr>| -> Gc<Expr>, - base: Gc<Expr>, + f: |&mut ExtCtxt, Span, P<Expr>, P<Expr>| -> P<Expr>, + base: P<Expr>, enum_nonmatch_f: EnumNonMatchCollapsedFunc, cx: &mut ExtCtxt, trait_span: Span, substructure: &Substructure) - -> Gc<Expr> { + -> P<Expr> { cs_same_method( |cx, span, vals| { if use_foldl { - vals.iter().fold(base, |old, &new| { + vals.move_iter().fold(base.clone(), |old, new| { f(cx, span, old, new) }) } else { - vals.iter().rev().fold(base, |old, &new| { + vals.move_iter().rev().fold(base.clone(), |old, new| { f(cx, span, old, new) }) } @@ -1438,10 +1436,10 @@ Use a given binop to combine the result of calling the derived method on all the fields. */ #[inline] -pub fn cs_binop(binop: ast::BinOp, base: Gc<Expr>, +pub fn cs_binop(binop: ast::BinOp, base: P<Expr>, enum_nonmatch_f: EnumNonMatchCollapsedFunc, cx: &mut ExtCtxt, trait_span: Span, - substructure: &Substructure) -> Gc<Expr> { + substructure: &Substructure) -> P<Expr> { cs_same_method_fold( true, // foldl is good enough |cx, span, old, new| { @@ -1459,7 +1457,7 @@ pub fn cs_binop(binop: ast::BinOp, base: Gc<Expr>, #[inline] pub fn cs_or(enum_nonmatch_f: EnumNonMatchCollapsedFunc, cx: &mut ExtCtxt, span: Span, - substructure: &Substructure) -> Gc<Expr> { + substructure: &Substructure) -> P<Expr> { cs_binop(ast::BiOr, cx.expr_bool(span, false), enum_nonmatch_f, cx, span, substructure) @@ -1469,7 +1467,7 @@ pub fn cs_or(enum_nonmatch_f: EnumNonMatchCollapsedFunc, #[inline] pub fn cs_and(enum_nonmatch_f: EnumNonMatchCollapsedFunc, cx: &mut ExtCtxt, span: Span, - substructure: &Substructure) -> Gc<Expr> { + substructure: &Substructure) -> P<Expr> { cs_binop(ast::BiAnd, cx.expr_bool(span, true), enum_nonmatch_f, cx, span, substructure) diff --git a/src/libsyntax/ext/deriving/generic/ty.rs b/src/libsyntax/ext/deriving/generic/ty.rs index 8b4a9c51cf0..a90618a30b6 100644 --- a/src/libsyntax/ext/deriving/generic/ty.rs +++ b/src/libsyntax/ext/deriving/generic/ty.rs @@ -14,14 +14,13 @@ explicit `Self` type to use when specifying impls to be derived. */ use ast; -use ast::{P,Expr,Generics,Ident}; +use ast::{Expr,Generics,Ident}; use ext::base::ExtCtxt; use ext::build::AstBuilder; use codemap::{Span,respan}; use owned_slice::OwnedSlice; use parse::token::special_idents; - -use std::gc::Gc; +use ptr::P; /// The types of pointers #[deriving(Clone)] @@ -260,7 +259,7 @@ impl<'a> LifetimeBounds<'a> { } pub fn get_explicit_self(cx: &ExtCtxt, span: Span, self_ptr: &Option<PtrTy>) - -> (Gc<Expr>, ast::ExplicitSelf) { + -> (P<Expr>, ast::ExplicitSelf) { // this constructs a fresh `self` path, which will match the fresh `self` binding // created below. let self_path = cx.expr_self(span); diff --git a/src/libsyntax/ext/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs index f469139177a..b7f11c25825 100644 --- a/src/libsyntax/ext/deriving/hash.rs +++ b/src/libsyntax/ext/deriving/hash.rs @@ -15,14 +15,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_hash(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let (path, generics, args) = if cx.ecfg.deriving_hash_type_parameter { (Path::new_(vec!("std", "hash", "Hash"), None, @@ -64,15 +63,14 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, hash_trait_def.expand(cx, mitem, item, push); } -fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { +fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> { let state_expr = match substr.nonself_args { - [state_expr] => state_expr, + [ref state_expr] => state_expr, _ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(Hash)`") }; let hash_ident = substr.method_ident; let call_hash = |span, thing_expr| { - let expr = cx.expr_method_call(span, thing_expr, hash_ident, vec!(state_expr)); + let expr = cx.expr_method_call(span, thing_expr, hash_ident, vec!(state_expr.clone())); cx.stmt_expr(expr) }; let mut stmts = Vec::new(); @@ -83,7 +81,7 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, // Determine the discriminant. We will feed this value to the byte // iteration function. let discriminant = match variant.node.disr_expr { - Some(d) => d, + Some(ref d) => d.clone(), None => cx.expr_uint(trait_span, index) }; @@ -94,8 +92,8 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, _ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`") }; - for &FieldInfo { self_, span, .. } in fields.iter() { - stmts.push(call_hash(span, self_)); + for &FieldInfo { ref self_, span, .. } in fields.iter() { + stmts.push(call_hash(span, self_.clone())); } if stmts.len() == 0 { diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index a9b5c8a4134..b8cebd8ea20 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -21,8 +21,7 @@ library. use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord}; use ext::base::ExtCtxt; use codemap::Span; - -use std::gc::Gc; +use ptr::P; pub mod bounds; pub mod clone; @@ -49,9 +48,9 @@ pub mod generic; pub fn expand_meta_deriving(cx: &mut ExtCtxt, _span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { match mitem.node { MetaNameValue(_, ref l) => { cx.span_err(l.span, "unexpected value in `deriving`"); @@ -63,13 +62,13 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt, cx.span_warn(mitem.span, "empty trait list in `deriving`"); } MetaList(_, ref titems) => { - for &titem in titems.iter().rev() { + for titem in titems.iter().rev() { match titem.node { MetaNameValue(ref tname, _) | MetaList(ref tname, _) | MetaWord(ref tname) => { macro_rules! expand(($func:path) => ($func(cx, titem.span, - titem, item, + &**titem, item, |i| push(i)))); match tname.get() { "Clone" => expand!(clone::expand_deriving_clone), diff --git a/src/libsyntax/ext/deriving/primitive.rs b/src/libsyntax/ext/deriving/primitive.rs index 30dd8e9683a..044a2812c00 100644 --- a/src/libsyntax/ext/deriving/primitive.rs +++ b/src/libsyntax/ext/deriving/primitive.rs @@ -16,14 +16,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let inline = cx.meta_word(span, InternedString::new("inline")); let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { @@ -70,10 +69,9 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt, trait_def.expand(cx, mitem, item, push) } -fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { +fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> { let n = match substr.nonself_args { - [n] => n, + [ref n] => n, _ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(FromPrimitive)`") }; @@ -106,8 +104,8 @@ fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, // expr for `$n == $variant as $name` let variant = cx.expr_ident(span, variant.node.name); let ty = cx.ty_ident(span, cx.ident_of(name)); - let cast = cx.expr_cast(span, variant, ty); - let guard = cx.expr_binary(span, ast::BiEq, n, cast); + let cast = cx.expr_cast(span, variant.clone(), ty); + let guard = cx.expr_binary(span, ast::BiEq, n.clone(), cast); // expr for `Some($variant)` let body = cx.expr_some(span, variant); @@ -141,7 +139,7 @@ fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, }; arms.push(arm); - cx.expr_match(trait_span, n, arms) + cx.expr_match(trait_span, n.clone(), arms) } _ => cx.span_bug(trait_span, "expected StaticEnum in deriving(FromPrimitive)") } diff --git a/src/libsyntax/ext/deriving/rand.rs b/src/libsyntax/ext/deriving/rand.rs index c652b5a5bed..584645bb306 100644 --- a/src/libsyntax/ext/deriving/rand.rs +++ b/src/libsyntax/ext/deriving/rand.rs @@ -15,14 +15,13 @@ use ext::base::ExtCtxt; use ext::build::{AstBuilder}; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_rand(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let trait_def = TraitDef { span: span, attributes: Vec::new(), @@ -54,10 +53,9 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt, trait_def.expand(cx, mitem, item, push) } -fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { +fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> { let rng = match substr.nonself_args { - [rng] => vec!( rng ), + [ref rng] => rng, _ => cx.bug("Incorrect number of arguments to `rand` in `deriving(Rand)`") }; let rand_ident = vec!( @@ -69,7 +67,7 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, let rand_call = |cx: &mut ExtCtxt, span| { cx.expr_call_global(span, rand_ident.clone(), - vec!( *rng.get(0) )) + vec!(rng.clone())) }; return match *substr.fields { @@ -95,7 +93,7 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, // ::rand::Rand::rand(rng) let rv_call = cx.expr_call(trait_span, rand_name, - vec!( *rng.get(0) )); + vec!(rng.clone())); // need to specify the uint-ness of the random number let uint_ty = cx.ty_ident(trait_span, cx.ident_of("uint")); @@ -136,8 +134,8 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, trait_span: Span, ctor_ident: Ident, summary: &StaticFields, - rand_call: |&mut ExtCtxt, Span| -> Gc<Expr>) - -> Gc<Expr> { + rand_call: |&mut ExtCtxt, Span| -> P<Expr>) + -> P<Expr> { match *summary { Unnamed(ref fields) => { if fields.is_empty() { diff --git a/src/libsyntax/ext/deriving/show.rs b/src/libsyntax/ext/deriving/show.rs index e0dfbb232f5..16ce264fe71 100644 --- a/src/libsyntax/ext/deriving/show.rs +++ b/src/libsyntax/ext/deriving/show.rs @@ -9,7 +9,7 @@ // except according to those terms. use ast; -use ast::{MetaItem, Item, Expr}; +use ast::{MetaItem, Item, Expr,}; use codemap::Span; use ext::format; use ext::base::ExtCtxt; @@ -17,16 +17,15 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token; +use ptr::P; use std::collections::HashMap; -use std::string::String; -use std::gc::Gc; pub fn expand_deriving_show(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { // &mut ::std::fmt::Formatter let fmtr = Ptr(box Literal(Path::new(vec!("std", "fmt", "Formatter"))), Borrowed(None, ast::MutMutable)); @@ -57,7 +56,7 @@ pub fn expand_deriving_show(cx: &mut ExtCtxt, /// We construct a format string and then defer to std::fmt, since that /// knows what's up with formatting and so on. fn show_substructure(cx: &mut ExtCtxt, span: Span, - substr: &Substructure) -> Gc<Expr> { + substr: &Substructure) -> P<Expr> { // build `<name>`, `<name>({}, {}, ...)` or `<name> { <field>: {}, // <field>: {}, ... }` based on the "shape". // @@ -91,7 +90,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, format_string.push_str("{}"); - exprs.push(field.self_); + exprs.push(field.self_.clone()); } format_string.push_str(")"); @@ -108,7 +107,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, format_string.push_str(name.get()); format_string.push_str(": {}"); - exprs.push(field.self_); + exprs.push(field.self_.clone()); } format_string.push_str(" }}"); @@ -123,7 +122,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, // format_arg_method!(fmt, write_fmt, "<format_string>", exprs...) // // but doing it directly via ext::format. - let formatter = substr.nonself_args[0]; + let formatter = substr.nonself_args[0].clone(); let meth = cx.ident_of("write_fmt"); let s = token::intern_and_get_ident(format_string.as_slice()); diff --git a/src/libsyntax/ext/deriving/zero.rs b/src/libsyntax/ext/deriving/zero.rs index 973f9d518cd..7f265b529ff 100644 --- a/src/libsyntax/ext/deriving/zero.rs +++ b/src/libsyntax/ext/deriving/zero.rs @@ -15,14 +15,13 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use ext::deriving::generic::ty::*; use parse::token::InternedString; - -use std::gc::Gc; +use ptr::P; pub fn expand_deriving_zero(cx: &mut ExtCtxt, span: Span, - mitem: Gc<MetaItem>, - item: Gc<Item>, - push: |Gc<Item>|) { + mitem: &MetaItem, + item: &Item, + push: |P<Item>|) { let inline = cx.meta_word(span, InternedString::new("inline")); let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { @@ -63,8 +62,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt, trait_def.expand(cx, mitem, item, push) } -fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> Gc<Expr> { +fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> { let zero_ident = vec!( cx.ident_of("std"), cx.ident_of("num"), diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs index aae92ae85fc..69574ee6696 100644 --- a/src/libsyntax/ext/env.rs +++ b/src/libsyntax/ext/env.rs @@ -61,38 +61,42 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> Box<base::MacResult+'cx> { - let exprs = match get_exprs_from_tts(cx, sp, tts) { + let mut exprs = match get_exprs_from_tts(cx, sp, tts) { Some(ref exprs) if exprs.len() == 0 => { cx.span_err(sp, "env! takes 1 or 2 arguments"); return DummyResult::expr(sp); } None => return DummyResult::expr(sp), - Some(exprs) => exprs + Some(exprs) => exprs.move_iter() }; let var = match expr_to_string(cx, - *exprs.get(0), + exprs.next().unwrap(), "expected string literal") { None => return DummyResult::expr(sp), Some((v, _style)) => v }; - let msg = match exprs.len() { - 1 => { + let msg = match exprs.next() { + None => { token::intern_and_get_ident(format!("environment variable `{}` \ not defined", var).as_slice()) } - 2 => { - match expr_to_string(cx, *exprs.get(1), "expected string literal") { + Some(second) => { + match expr_to_string(cx, second, "expected string literal") { None => return DummyResult::expr(sp), Some((s, _style)) => s } } - _ => { + }; + + match exprs.next() { + None => {} + Some(_) => { cx.span_err(sp, "env! takes 1 or 2 arguments"); return DummyResult::expr(sp); } - }; + } let e = match os::getenv(var.get()) { None => { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index d15d6b3f8f1..db28872de37 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{P, Block, Crate, DeclLocal, ExprMac, PatMac}; +use ast::{Block, Crate, DeclLocal, ExprMac, PatMac}; use ast::{Local, Ident, MacInvocTT}; use ast::{ItemMac, Mrk, Stmt, StmtDecl, StmtMac, StmtExpr, StmtSemi}; use ast::TokenTree; @@ -25,103 +25,106 @@ use fold::*; use parse; use parse::token::{fresh_mark, fresh_name, intern}; use parse::token; +use ptr::P; +use util::small_vector::SmallVector; use visit; use visit::Visitor; -use util::small_vector::SmallVector; -use std::gc::{Gc, GC}; +use std::gc::Gc; enum Either<L,R> { Left(L), Right(R) } -fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> { - match e.node { +pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> { + e.and_then(|ast::Expr {id, node, span}| match node { // expr_mac should really be expr_ext or something; it's the // entry-point for all syntax extensions. - ExprMac(ref mac) => { - let expanded_expr = match expand_mac_invoc(mac,&e.span, - |r|{r.make_expr()}, - |expr,fm|{mark_expr(expr,fm)}, - fld) { + ExprMac(mac) => { + let expanded_expr = match expand_mac_invoc(mac, span, + |r| r.make_expr(), + mark_expr, fld) { Some(expr) => expr, None => { - return DummyResult::raw_expr(e.span); + return DummyResult::raw_expr(span); } }; // Keep going, outside-in. // - // FIXME(pcwalton): Is it necessary to clone the - // node here? - let fully_expanded = - fld.fold_expr(expanded_expr).node.clone(); + let fully_expanded = fld.fold_expr(expanded_expr); fld.cx.bt_pop(); - box(GC) ast::Expr { + fully_expanded.map(|e| ast::Expr { id: ast::DUMMY_NODE_ID, - node: fully_expanded, - span: e.span, - } + node: e.node, + span: span, + }) } ast::ExprWhile(cond, body, opt_ident) => { let cond = fld.fold_expr(cond); let (body, opt_ident) = expand_loop_block(body, opt_ident, fld); - fld.cx.expr(e.span, ast::ExprWhile(cond, body, opt_ident)) + fld.cx.expr(span, ast::ExprWhile(cond, body, opt_ident)) } ast::ExprLoop(loop_block, opt_ident) => { let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld); - fld.cx.expr(e.span, ast::ExprLoop(loop_block, opt_ident)) + fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident)) } ast::ExprForLoop(pat, head, body, opt_ident) => { let pat = fld.fold_pat(pat); let head = fld.fold_expr(head); let (body, opt_ident) = expand_loop_block(body, opt_ident, fld); - fld.cx.expr(e.span, ast::ExprForLoop(pat, head, body, opt_ident)) + fld.cx.expr(span, ast::ExprForLoop(pat, head, body, opt_ident)) } ast::ExprFnBlock(capture_clause, fn_decl, block) => { let (rewritten_fn_decl, rewritten_block) - = expand_and_rename_fn_decl_and_block(&*fn_decl, block, fld); + = expand_and_rename_fn_decl_and_block(fn_decl, block, fld); let new_node = ast::ExprFnBlock(capture_clause, rewritten_fn_decl, rewritten_block); - box(GC) ast::Expr{id:e.id, node: new_node, span: fld.new_span(e.span)} + P(ast::Expr{id:id, node: new_node, span: fld.new_span(span)}) } ast::ExprProc(fn_decl, block) => { let (rewritten_fn_decl, rewritten_block) - = expand_and_rename_fn_decl_and_block(&*fn_decl, block, fld); + = expand_and_rename_fn_decl_and_block(fn_decl, block, fld); let new_node = ast::ExprProc(rewritten_fn_decl, rewritten_block); - box(GC) ast::Expr{id:e.id, node: new_node, span: fld.new_span(e.span)} + P(ast::Expr{id:id, node: new_node, span: fld.new_span(span)}) } - _ => noop_fold_expr(e, fld) - } + _ => { + P(noop_fold_expr(ast::Expr { + id: id, + node: node, + span: span + }, fld)) + } + }) } /// Expand a (not-ident-style) macro invocation. Returns the result /// of expansion and the mark which must be applied to the result. /// Our current interface doesn't allow us to apply the mark to the /// result until after calling make_expr, make_items, etc. -fn expand_mac_invoc<T>(mac: &ast::Mac, span: &codemap::Span, +fn expand_mac_invoc<T>(mac: ast::Mac, span: codemap::Span, parse_thunk: |Box<MacResult>|->Option<T>, mark_thunk: |T,Mrk|->T, fld: &mut MacroExpander) -> Option<T> { - match (*mac).node { + match mac.node { // it would almost certainly be cleaner to pass the whole // macro invocation in, rather than pulling it apart and // marking the tts and the ctxt separately. This also goes // for the other three macro invocation chunks of code // in this file. // Token-tree macros: - MacInvocTT(ref pth, ref tts, _) => { + MacInvocTT(pth, tts, _) => { if pth.segments.len() > 1u { fld.cx.span_err(pth.span, "expected macro name without module \ @@ -144,7 +147,7 @@ fn expand_mac_invoc<T>(mac: &ast::Mac, span: &codemap::Span, Some(rc) => match *rc { NormalTT(ref expandfun, exp_span) => { fld.cx.bt_push(ExpnInfo { - call_site: *span, + call_site: span, callee: NameAndSpan { name: extnamestr.get().to_string(), format: MacroBang, @@ -218,7 +221,7 @@ fn expand_loop_block(loop_block: P<Block>, // in a block enclosed by loop head. fld.cx.syntax_env.push_frame(); fld.cx.syntax_env.info().pending_renames.push(rename); - let expanded_block = expand_block_elts(&*loop_block, fld); + let expanded_block = expand_block_elts(loop_block, fld); fld.cx.syntax_env.pop_frame(); (expanded_block, Some(renamed_ident)) @@ -240,8 +243,8 @@ macro_rules! with_exts_frame ( ) // When we enter a module, record it, for the sake of `module!` -fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander) - -> SmallVector<Gc<ast::Item>> { +pub fn expand_item(it: P<ast::Item>, fld: &mut MacroExpander) + -> SmallVector<P<ast::Item>> { let it = expand_item_modifiers(it, fld); let mut decorator_items = SmallVector::zero(); @@ -265,8 +268,9 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander) // we'd ideally decorator_items.push_all(expand_item(item, fld)), // but that double-mut-borrows fld - let mut items: SmallVector<Gc<ast::Item>> = SmallVector::zero(); - dec.expand(fld.cx, attr.span, attr.node.value, it, |item| items.push(item)); + let mut items: SmallVector<P<ast::Item>> = SmallVector::zero(); + dec.expand(fld.cx, attr.span, &*attr.node.value, &*it, + |item| items.push(item)); decorator_items.extend(items.move_iter() .flat_map(|item| expand_item(item, fld).move_iter())); @@ -285,17 +289,16 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander) let macro_escape = contains_macro_escape(new_attrs.as_slice()); let result = with_exts_frame!(fld.cx.syntax_env, macro_escape, - noop_fold_item(&*it, fld)); + noop_fold_item(it, fld)); fld.cx.mod_pop(); result }, _ => { - let it = box(GC) ast::Item { + let it = P(ast::Item { attrs: new_attrs, ..(*it).clone() - - }; - noop_fold_item(&*it, fld) + }); + noop_fold_item(it, fld) } }; @@ -303,8 +306,8 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander) new_items } -fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander) - -> Gc<ast::Item> { +fn expand_item_modifiers(mut it: P<ast::Item>, fld: &mut MacroExpander) + -> P<ast::Item> { // partition the attributes into ItemModifiers and others let (modifiers, other_attrs) = it.attrs.partitioned(|attr| { match fld.cx.syntax_env.find(&intern(attr.name().get())) { @@ -313,10 +316,10 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander) } }); // update the attrs, leave everything else alone. Is this mutation really a good idea? - it = box(GC) ast::Item { + it = P(ast::Item { attrs: other_attrs, ..(*it).clone() - }; + }); if modifiers.is_empty() { return it; @@ -337,7 +340,7 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander) span: None, } }); - it = mac.expand(fld.cx, attr.span, attr.node.value, it); + it = mac.expand(fld.cx, attr.span, &*attr.node.value, it); fld.cx.bt_pop(); } _ => unreachable!() @@ -351,15 +354,15 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander) } /// Expand item_underscore -fn expand_item_underscore(item: &ast::Item_, fld: &mut MacroExpander) -> ast::Item_ { - match *item { - ast::ItemFn(decl, fn_style, abi, ref generics, body) => { +fn expand_item_underscore(item: ast::Item_, fld: &mut MacroExpander) -> ast::Item_ { + match item { + ast::ItemFn(decl, fn_style, abi, generics, body) => { let (rewritten_fn_decl, rewritten_body) - = expand_and_rename_fn_decl_and_block(&*decl, body, fld); + = expand_and_rename_fn_decl_and_block(decl, body, fld); let expanded_generics = fold::noop_fold_generics(generics,fld); ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body) } - _ => noop_fold_item_underscore(&*item, fld) + _ => noop_fold_item_underscore(item, fld) } } @@ -370,26 +373,24 @@ fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool { // Support for item-position macro invocations, exactly the same // logic as for expression-position macro invocations. -fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander) - -> SmallVector<Gc<ast::Item>> -{ - let (pth, tts) = match it.node { +pub fn expand_item_mac(it: P<ast::Item>, fld: &mut MacroExpander) + -> SmallVector<P<ast::Item>> { + let (extname, path_span, tts) = match it.node { ItemMac(codemap::Spanned { node: MacInvocTT(ref pth, ref tts, _), .. }) => { - (pth, (*tts).clone()) + (pth.segments.get(0).identifier, pth.span, (*tts).clone()) } _ => fld.cx.span_bug(it.span, "invalid item macro invocation") }; - let extname = pth.segments.get(0).identifier; let extnamestr = token::get_ident(extname); let fm = fresh_mark(); let def_or_items = { - let expanded = match fld.cx.syntax_env.find(&extname.name) { + let mut expanded = match fld.cx.syntax_env.find(&extname.name) { None => { - fld.cx.span_err(pth.span, + fld.cx.span_err(path_span, format!("macro undefined: '{}!'", extnamestr).as_slice()); // let compilation continue @@ -400,7 +401,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander) NormalTT(ref expander, span) => { if it.ident.name != parse::token::special_idents::invalid.name { fld.cx - .span_err(pth.span, + .span_err(path_span, format!("macro {}! expects no ident argument, \ given '{}'", extnamestr, @@ -421,7 +422,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander) } IdentTT(ref expander, span) => { if it.ident.name == parse::token::special_idents::invalid.name { - fld.cx.span_err(pth.span, + fld.cx.span_err(path_span, format!("macro {}! expects an ident argument", extnamestr.get()).as_slice()); return SmallVector::zero(); @@ -440,7 +441,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander) } LetSyntaxTT(ref expander, span) => { if it.ident.name == parse::token::special_idents::invalid.name { - fld.cx.span_err(pth.span, + fld.cx.span_err(path_span, format!("macro {}! expects an ident argument", extnamestr.get()).as_slice()); return SmallVector::zero(); @@ -490,7 +491,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander) .collect() } Right(None) => { - fld.cx.span_err(pth.span, + fld.cx.span_err(path_span, format!("non-item macro in item position: {}", extnamestr.get()).as_slice()); return SmallVector::zero(); @@ -498,24 +499,21 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander) }; fld.cx.bt_pop(); - return items; + items } /// Expand a stmt // // I don't understand why this returns a vector... it looks like we're // half done adding machinery to allow macros to expand into multiple statements. -fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<Gc<Stmt>> { +fn expand_stmt(s: Stmt, fld: &mut MacroExpander) -> SmallVector<P<Stmt>> { let (mac, semi) = match s.node { - StmtMac(ref mac, semi) => (mac, semi), + StmtMac(mac, semi) => (mac, semi), _ => return expand_non_macro_stmt(s, fld) }; - let expanded_stmt = match expand_mac_invoc(mac,&s.span, - |r|{r.make_stmt()}, - |sts,mrk| { - mark_stmt(&*sts,mrk) - }, - fld) { + let expanded_stmt = match expand_mac_invoc(mac, s.span, + |r| r.make_stmt(), + mark_stmt, fld) { Some(stmt) => stmt, None => { return SmallVector::zero(); @@ -523,46 +521,34 @@ fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<Gc<Stmt>> { }; // Keep going, outside-in. - let fully_expanded = fld.fold_stmt(&*expanded_stmt); + let fully_expanded = fld.fold_stmt(expanded_stmt); fld.cx.bt_pop(); - let fully_expanded: SmallVector<Gc<Stmt>> = fully_expanded.move_iter() - .map(|s| box(GC) Spanned { span: s.span, node: s.node.clone() }) - .collect(); - - fully_expanded.move_iter().map(|s| { - match s.node { - StmtExpr(e, stmt_id) if semi => { - box(GC) Spanned { - span: s.span, - node: StmtSemi(e, stmt_id) - } + + if semi { + fully_expanded.move_iter().map(|s| s.map(|Spanned {node, span}| { + Spanned { + node: match node { + StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id), + _ => node /* might already have a semi */ + }, + span: span } - _ => s /* might already have a semi */ - } - }).collect() + })).collect() + } else { + fully_expanded + } } // expand a non-macro stmt. this is essentially the fallthrough for // expand_stmt, above. -fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander) - -> SmallVector<Gc<Stmt>> { +fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroExpander) + -> SmallVector<P<Stmt>> { // is it a let? - match s.node { - StmtDecl(decl, node_id) => { - match *decl { - Spanned { - node: DeclLocal(ref local), - span: stmt_span - } => { - // take it apart: - let Local { - ty: ty, - pat: pat, - init: init, - id: id, - span: span, - source: source, - } = **local; + match node { + StmtDecl(decl, node_id) => decl.and_then(|Spanned {node: decl, span}| match decl { + DeclLocal(local) => { + // take it apart: + let rewritten_local = local.map(|Local {id, pat, ty, init, source, span}| { // expand the ty since TyFixedLengthVec contains an Expr // and thus may have a macro use let expanded_ty = fld.fold_ty(ty); @@ -585,57 +571,66 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander) }; // add them to the existing pending renames: fld.cx.syntax_env.info().pending_renames.push_all_move(new_pending_renames); - // also, don't forget to expand the init: - let new_init_opt = init.map(|e| fld.fold_expr(e)); - let rewritten_local = - box(GC) Local { - ty: expanded_ty, - pat: rewritten_pat, - init: new_init_opt, - id: id, - span: span, - source: source - }; - SmallVector::one(box(GC) Spanned { - node: StmtDecl(box(GC) Spanned { - node: DeclLocal(rewritten_local), - span: stmt_span - }, - node_id), + Local { + id: id, + ty: expanded_ty, + pat: rewritten_pat, + // also, don't forget to expand the init: + init: init.map(|e| fld.fold_expr(e)), + source: source, span: span - }) - } - _ => noop_fold_stmt(s, fld), + } + }); + SmallVector::one(P(Spanned { + node: StmtDecl(P(Spanned { + node: DeclLocal(rewritten_local), + span: span + }), + node_id), + span: stmt_span + })) } - }, - _ => noop_fold_stmt(s, fld), + _ => { + noop_fold_stmt(Spanned { + node: StmtDecl(P(Spanned { + node: decl, + span: span + }), + node_id), + span: stmt_span + }, fld) + } + }), + _ => { + noop_fold_stmt(Spanned { + node: node, + span: stmt_span + }, fld) + } } } // expand the arm of a 'match', renaming for macro hygiene -fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm { +fn expand_arm(arm: ast::Arm, fld: &mut MacroExpander) -> ast::Arm { // expand pats... they might contain macro uses: - let expanded_pats : Vec<Gc<ast::Pat>> = arm.pats.iter().map(|pat| fld.fold_pat(*pat)).collect(); + let expanded_pats = arm.pats.move_map(|pat| fld.fold_pat(pat)); if expanded_pats.len() == 0 { fail!("encountered match arm with 0 patterns"); } // all of the pats must have the same set of bindings, so use the // first one to extract them and generate new names: - let first_pat = expanded_pats.get(0); - let idents = pattern_bindings(&**first_pat); - let new_renames = - idents.iter().map(|id| (*id,fresh_name(id))).collect(); + let idents = pattern_bindings(&**expanded_pats.get(0)); + let new_renames = idents.move_iter().map(|id| (id, fresh_name(&id))).collect(); // apply the renaming, but only to the PatIdents: let mut rename_pats_fld = PatIdentRenamer{renames:&new_renames}; - let rewritten_pats = - expanded_pats.iter().map(|pat| rename_pats_fld.fold_pat(*pat)).collect(); + let rewritten_pats = expanded_pats.move_map(|pat| rename_pats_fld.fold_pat(pat)); // apply renaming and then expansion to the guard and the body: let mut rename_fld = IdentRenamer{renames:&new_renames}; let rewritten_guard = arm.guard.map(|g| fld.fold_expr(rename_fld.fold_expr(g))); let rewritten_body = fld.fold_expr(rename_fld.fold_expr(arm.body)); ast::Arm { - attrs: arm.attrs.iter().map(|x| fld.fold_attribute(*x)).collect(), + attrs: arm.attrs.move_map(|x| fld.fold_attribute(x)), pats: rewritten_pats, guard: rewritten_guard, body: rewritten_body, @@ -683,121 +678,126 @@ fn fn_decl_arg_bindings(fn_decl: &ast::FnDecl) -> Vec<ast::Ident> { } // expand a block. pushes a new exts_frame, then calls expand_block_elts -fn expand_block(blk: &Block, fld: &mut MacroExpander) -> P<Block> { +pub fn expand_block(blk: P<Block>, fld: &mut MacroExpander) -> P<Block> { // see note below about treatment of exts table with_exts_frame!(fld.cx.syntax_env,false, expand_block_elts(blk, fld)) } // expand the elements of a block. -fn expand_block_elts(b: &Block, fld: &mut MacroExpander) -> P<Block> { - let new_view_items = b.view_items.iter().map(|x| fld.fold_view_item(x)).collect(); - let new_stmts = - b.stmts.iter().flat_map(|x| { +pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> { + b.map(|Block {id, view_items, stmts, expr, rules, span}| { + let new_view_items = view_items.move_iter().map(|x| fld.fold_view_item(x)).collect(); + let new_stmts = stmts.move_iter().flat_map(|x| { // perform all pending renames let renamed_stmt = { let pending_renames = &mut fld.cx.syntax_env.info().pending_renames; let mut rename_fld = IdentRenamer{renames:pending_renames}; - rename_fld.fold_stmt(&**x).expect_one("rename_fold didn't return one value") + rename_fld.fold_stmt(x).expect_one("rename_fold didn't return one value") }; // expand macros in the statement - fld.fold_stmt(&*renamed_stmt).move_iter() + fld.fold_stmt(renamed_stmt).move_iter() }).collect(); - let new_expr = b.expr.map(|x| { - let expr = { - let pending_renames = &mut fld.cx.syntax_env.info().pending_renames; - let mut rename_fld = IdentRenamer{renames:pending_renames}; - rename_fld.fold_expr(x) - }; - fld.fold_expr(expr) - }); - P(Block { - view_items: new_view_items, - stmts: new_stmts, - expr: new_expr, - id: fld.new_id(b.id), - rules: b.rules, - span: b.span, + let new_expr = expr.map(|x| { + let expr = { + let pending_renames = &mut fld.cx.syntax_env.info().pending_renames; + let mut rename_fld = IdentRenamer{renames:pending_renames}; + rename_fld.fold_expr(x) + }; + fld.fold_expr(expr) + }); + Block { + id: fld.new_id(id), + view_items: new_view_items, + stmts: new_stmts, + expr: new_expr, + rules: rules, + span: span + } }) } -fn expand_pat(p: Gc<ast::Pat>, fld: &mut MacroExpander) -> Gc<ast::Pat> { - let (pth, tts) = match p.node { - PatMac(ref mac) => { - match mac.node { - MacInvocTT(ref pth, ref tts, _) => { - (pth, (*tts).clone()) - } - } - } - _ => return noop_fold_pat(p, fld), - }; - if pth.segments.len() > 1u { - fld.cx.span_err(pth.span, "expected macro name without module separators"); - return DummyResult::raw_pat(p.span); +fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> { + match p.node { + PatMac(_) => {} + _ => return noop_fold_pat(p, fld) } - let extname = pth.segments.get(0).identifier; - let extnamestr = token::get_ident(extname); - let marked_after = match fld.cx.syntax_env.find(&extname.name) { - None => { - fld.cx.span_err(pth.span, - format!("macro undefined: '{}!'", - extnamestr).as_slice()); - // let compilation continue - return DummyResult::raw_pat(p.span); + p.map(|ast::Pat {node, span, ..}| { + let (pth, tts) = match node { + PatMac(mac) => match mac.node { + MacInvocTT(pth, tts, _) => { + (pth, tts) + } + }, + _ => unreachable!() + }; + if pth.segments.len() > 1u { + fld.cx.span_err(pth.span, "expected macro name without module separators"); + return DummyResult::raw_pat(span); } + let extname = pth.segments.get(0).identifier; + let extnamestr = token::get_ident(extname); + let marked_after = match fld.cx.syntax_env.find(&extname.name) { + None => { + fld.cx.span_err(pth.span, + format!("macro undefined: '{}!'", + extnamestr).as_slice()); + // let compilation continue + return DummyResult::raw_pat(span); + } - Some(rc) => match *rc { - NormalTT(ref expander, span) => { - fld.cx.bt_push(ExpnInfo { - call_site: p.span, - callee: NameAndSpan { - name: extnamestr.get().to_string(), - format: MacroBang, - span: span - } - }); + Some(rc) => match *rc { + NormalTT(ref expander, tt_span) => { + fld.cx.bt_push(ExpnInfo { + call_site: span, + callee: NameAndSpan { + name: extnamestr.get().to_string(), + format: MacroBang, + span: tt_span + } + }); - let fm = fresh_mark(); - let marked_before = mark_tts(tts.as_slice(), fm); - let mac_span = original_span(fld.cx); - let expanded = match expander.expand(fld.cx, - mac_span.call_site, - marked_before.as_slice()).make_pat() { - Some(e) => e, - None => { - fld.cx.span_err( - pth.span, - format!( - "non-pattern macro in pattern position: {}", - extnamestr.get() - ).as_slice() - ); - return DummyResult::raw_pat(p.span); - } - }; + let fm = fresh_mark(); + let marked_before = mark_tts(tts.as_slice(), fm); + let mac_span = original_span(fld.cx); + let expanded = match expander.expand(fld.cx, + mac_span.call_site, + marked_before.as_slice()).make_pat() { + Some(e) => e, + None => { + fld.cx.span_err( + pth.span, + format!( + "non-pattern macro in pattern position: {}", + extnamestr.get() + ).as_slice() + ); + return DummyResult::raw_pat(span); + } + }; - // mark after: - mark_pat(expanded,fm) - } - _ => { - fld.cx.span_err(p.span, - format!("{}! is not legal in pattern position", - extnamestr.get()).as_slice()); - return DummyResult::raw_pat(p.span); + // mark after: + mark_pat(expanded,fm) + } + _ => { + fld.cx.span_err(span, + format!("{}! is not legal in pattern position", + extnamestr.get()).as_slice()); + return DummyResult::raw_pat(span); + } } - } - }; + }; - let fully_expanded = - fld.fold_pat(marked_after).node.clone(); - fld.cx.bt_pop(); + let fully_expanded = + fld.fold_pat(marked_after).node.clone(); + fld.cx.bt_pop(); - box(GC) ast::Pat { - id: ast::DUMMY_NODE_ID, - node: fully_expanded, - span: p.span, - } + ast::Pat { + id: ast::DUMMY_NODE_ID, + node: fully_expanded, + span: span + } + }) } /// A tree-folder that applies every rename in its (mutable) list @@ -814,7 +814,7 @@ impl<'a> Folder for IdentRenamer<'a> { ctxt: mtwt::apply_renames(self.renames, id.ctxt), } } - fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac { + fn fold_mac(&mut self, macro: ast::Mac) -> ast::Mac { fold::noop_fold_mac(macro, self) } } @@ -828,45 +828,50 @@ pub struct PatIdentRenamer<'a> { } impl<'a> Folder for PatIdentRenamer<'a> { - fn fold_pat(&mut self, pat: Gc<ast::Pat>) -> Gc<ast::Pat> { + fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> { match pat.node { - ast::PatIdent(binding_mode, Spanned{span: ref sp, node: id}, ref sub) => { - let new_ident = Ident{name: id.name, - ctxt: mtwt::apply_renames(self.renames, id.ctxt)}; + ast::PatIdent(..) => {}, + _ => return noop_fold_pat(pat, self) + } + + pat.map(|ast::Pat {id, node, span}| match node { + ast::PatIdent(binding_mode, Spanned{span: sp, node: ident}, sub) => { + let new_ident = Ident{name: ident.name, + ctxt: mtwt::apply_renames(self.renames, ident.ctxt)}; let new_node = ast::PatIdent(binding_mode, - Spanned{span: self.new_span(*sp), node: new_ident}, + Spanned{span: self.new_span(sp), node: new_ident}, sub.map(|p| self.fold_pat(p))); - box(GC) ast::Pat { - id: pat.id, - span: self.new_span(pat.span), + ast::Pat { + id: id, node: new_node, + span: self.new_span(span) } }, - _ => noop_fold_pat(pat, self) - } + _ => unreachable!() + }) } - fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac { + fn fold_mac(&mut self, macro: ast::Mac) -> ast::Mac { fold::noop_fold_mac(macro, self) } } // expand a method -fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast::Method>> { - let id = fld.new_id(m.id); - match m.node { +fn expand_method(m: P<ast::Method>, fld: &mut MacroExpander) -> SmallVector<P<ast::Method>> { + m.and_then(|m| match m.node { ast::MethDecl(ident, - ref generics, + generics, abi, - ref explicit_self, + explicit_self, fn_style, decl, body, vis) => { + let id = fld.new_id(m.id); let (rewritten_fn_decl, rewritten_body) - = expand_and_rename_fn_decl_and_block(&*decl,body,fld); - SmallVector::one(box(GC) ast::Method { - attrs: m.attrs.iter().map(|a| fld.fold_attribute(*a)).collect(), + = expand_and_rename_fn_decl_and_block(decl,body,fld); + SmallVector::one(P(ast::Method { + attrs: m.attrs.move_map(|a| fld.fold_attribute(a)), id: id, span: fld.new_span(m.span), node: ast::MethDecl(fld.fold_ident(ident), @@ -877,15 +882,13 @@ fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast rewritten_fn_decl, rewritten_body, vis) - }) + })) }, - ast::MethMac(ref mac) => { + ast::MethMac(mac) => { let maybe_new_methods = - expand_mac_invoc(mac, &m.span, - |r|{r.make_methods()}, - |meths,mark|{ - meths.move_iter().map(|m|{mark_method(m,mark)}) - .collect()}, + expand_mac_invoc(mac, m.span, + |r| r.make_methods(), + |meths, mark| meths.move_map(|m| mark_method(m, mark)), fld); let new_methods = match maybe_new_methods { @@ -896,22 +899,22 @@ fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast // expand again if necessary new_methods.move_iter().flat_map(|m| fld.fold_method(m).move_iter()).collect() } - } + }) } /// Given a fn_decl and a block and a MacroExpander, expand the fn_decl, then use the /// PatIdents in its arguments to perform renaming in the FnDecl and /// the block, returning both the new FnDecl and the new Block. -fn expand_and_rename_fn_decl_and_block(fn_decl: &ast::FnDecl, block: Gc<ast::Block>, +fn expand_and_rename_fn_decl_and_block(fn_decl: P<ast::FnDecl>, block: P<ast::Block>, fld: &mut MacroExpander) - -> (Gc<ast::FnDecl>, Gc<ast::Block>) { + -> (P<ast::FnDecl>, P<ast::Block>) { let expanded_decl = fld.fold_fn_decl(fn_decl); let idents = fn_decl_arg_bindings(&*expanded_decl); let renames = idents.iter().map(|id : &ast::Ident| (*id,fresh_name(id))).collect(); // first, a renamer for the PatIdents, for the fn_decl: let mut rename_pat_fld = PatIdentRenamer{renames: &renames}; - let rewritten_fn_decl = rename_pat_fld.fold_fn_decl(&*expanded_decl); + let rewritten_fn_decl = rename_pat_fld.fold_fn_decl(expanded_decl); // now, a renamer for *all* idents, for the body: let mut rename_fld = IdentRenamer{renames: &renames}; let rewritten_body = fld.fold_block(rename_fld.fold_block(block)); @@ -924,36 +927,36 @@ pub struct MacroExpander<'a, 'b:'a> { } impl<'a, 'b> Folder for MacroExpander<'a, 'b> { - fn fold_expr(&mut self, expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> { expand_expr(expr, self) } - fn fold_pat(&mut self, pat: Gc<ast::Pat>) -> Gc<ast::Pat> { + fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> { expand_pat(pat, self) } - fn fold_item(&mut self, item: Gc<ast::Item>) -> SmallVector<Gc<ast::Item>> { + fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> { expand_item(item, self) } - fn fold_item_underscore(&mut self, item: &ast::Item_) -> ast::Item_ { + fn fold_item_underscore(&mut self, item: ast::Item_) -> ast::Item_ { expand_item_underscore(item, self) } - fn fold_stmt(&mut self, stmt: &ast::Stmt) -> SmallVector<Gc<ast::Stmt>> { - expand_stmt(stmt, self) + fn fold_stmt(&mut self, stmt: P<ast::Stmt>) -> SmallVector<P<ast::Stmt>> { + stmt.and_then(|stmt| expand_stmt(stmt, self)) } fn fold_block(&mut self, block: P<Block>) -> P<Block> { - expand_block(&*block, self) + expand_block(block, self) } - fn fold_arm(&mut self, arm: &ast::Arm) -> ast::Arm { + fn fold_arm(&mut self, arm: ast::Arm) -> ast::Arm { expand_arm(arm, self) } - fn fold_method(&mut self, method: Gc<ast::Method>) -> SmallVector<Gc<ast::Method>> { - expand_method(&*method, self) + fn fold_method(&mut self, method: P<ast::Method>) -> SmallVector<P<ast::Method>> { + expand_method(method, self) } fn new_span(&mut self, span: Span) -> Span { @@ -1033,17 +1036,16 @@ impl Folder for Marker { ctxt: mtwt::apply_mark(self.mark, id.ctxt) } } - fn fold_mac(&mut self, m: &ast::Mac) -> ast::Mac { - let macro = match m.node { - MacInvocTT(ref path, ref tts, ctxt) => { - MacInvocTT(self.fold_path(path), - self.fold_tts(tts.as_slice()), - mtwt::apply_mark(self.mark, ctxt)) - } - }; + fn fold_mac(&mut self, Spanned {node, span}: ast::Mac) -> ast::Mac { Spanned { - node: macro, - span: m.span, + node: match node { + MacInvocTT(path, tts, ctxt) => { + MacInvocTT(self.fold_path(path), + self.fold_tts(tts.as_slice()), + mtwt::apply_mark(self.mark, ctxt)) + } + }, + span: span, } } } @@ -1054,29 +1056,29 @@ fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> { } // apply a given mark to the given expr. Used following the expansion of a macro. -fn mark_expr(expr: Gc<ast::Expr>, m: Mrk) -> Gc<ast::Expr> { +fn mark_expr(expr: P<ast::Expr>, m: Mrk) -> P<ast::Expr> { Marker{mark:m}.fold_expr(expr) } // apply a given mark to the given pattern. Used following the expansion of a macro. -fn mark_pat(pat: Gc<ast::Pat>, m: Mrk) -> Gc<ast::Pat> { +fn mark_pat(pat: P<ast::Pat>, m: Mrk) -> P<ast::Pat> { Marker{mark:m}.fold_pat(pat) } // apply a given mark to the given stmt. Used following the expansion of a macro. -fn mark_stmt(expr: &ast::Stmt, m: Mrk) -> Gc<ast::Stmt> { +fn mark_stmt(expr: P<ast::Stmt>, m: Mrk) -> P<ast::Stmt> { Marker{mark:m}.fold_stmt(expr) .expect_one("marking a stmt didn't return exactly one stmt") } // apply a given mark to the given item. Used following the expansion of a macro. -fn mark_item(expr: Gc<ast::Item>, m: Mrk) -> Gc<ast::Item> { +fn mark_item(expr: P<ast::Item>, m: Mrk) -> P<ast::Item> { Marker{mark:m}.fold_item(expr) .expect_one("marking an item didn't return exactly one item") } // apply a given mark to the given item. Used following the expansion of a macro. -fn mark_method(expr: Gc<ast::Method>, m: Mrk) -> Gc<ast::Method> { +fn mark_method(expr: P<ast::Method>, m: Mrk) -> P<ast::Method> { Marker{mark:m}.fold_method(expr) .expect_one("marking an item didn't return exactly one method") } @@ -1133,8 +1135,6 @@ mod test { use visit; use visit::Visitor; - use std::gc::GC; - // a visitor that extracts the paths // from a given thingy and puts them in a mutable // array (passed in to the traversal) @@ -1252,10 +1252,10 @@ mod test { node: Attribute_ { id: attr::mk_attr_id(), style: AttrOuter, - value: box(GC) Spanned { + value: P(Spanned { node: MetaWord(token::intern_and_get_ident(s)), span: codemap::DUMMY_SP, - }, + }), is_sugared_doc: false, } } diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 0bb32c73ca2..271a5137bbf 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -9,7 +9,6 @@ // except according to those terms. use ast; -use ast::P; use codemap::{Span, respan}; use ext::base::*; use ext::base; @@ -17,9 +16,9 @@ use ext::build::AstBuilder; use fmt_macros as parse; use parse::token::InternedString; use parse::token; +use ptr::P; use std::collections::HashMap; -use std::gc::{Gc, GC}; #[deriving(PartialEq)] enum ArgumentType { @@ -39,13 +38,13 @@ struct Context<'a, 'b:'a> { /// Parsed argument expressions and the types that we've found so far for /// them. - args: Vec<Gc<ast::Expr>>, + args: Vec<P<ast::Expr>>, arg_types: Vec<Option<ArgumentType>>, /// Parsed named expressions and the types that we've found for them so far. /// Note that we keep a side-array of the ordering of the named arguments /// found to be sure that we can translate them in the same order that they /// were declared in. - names: HashMap<String, Gc<ast::Expr>>, + names: HashMap<String, P<ast::Expr>>, name_types: HashMap<String, ArgumentType>, name_ordering: Vec<String>, @@ -53,14 +52,14 @@ struct Context<'a, 'b:'a> { literal: String, /// Collection of the compiled `rt::Argument` structures - pieces: Vec<Gc<ast::Expr>>, + pieces: Vec<P<ast::Expr>>, /// Collection of string literals - str_pieces: Vec<Gc<ast::Expr>>, + str_pieces: Vec<P<ast::Expr>>, /// Stays `true` if all formatting parameters are default (as in "{}{}"). all_pieces_simple: bool, name_positions: HashMap<String, uint>, - method_statics: Vec<Gc<ast::Item>>, + method_statics: Vec<P<ast::Item>>, /// Updated as arguments are consumed or methods are entered nest_level: uint, @@ -68,8 +67,8 @@ struct Context<'a, 'b:'a> { } pub enum Invocation { - Call(Gc<ast::Expr>), - MethodCall(Gc<ast::Expr>, ast::Ident), + Call(P<ast::Expr>), + MethodCall(P<ast::Expr>, ast::Ident), } /// Parses the arguments from the given list of tokens, returning None @@ -82,10 +81,10 @@ pub enum Invocation { /// named arguments)) fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool, tts: &[ast::TokenTree]) - -> (Invocation, Option<(Gc<ast::Expr>, Vec<Gc<ast::Expr>>, Vec<String>, - HashMap<String, Gc<ast::Expr>>)>) { + -> (Invocation, Option<(P<ast::Expr>, Vec<P<ast::Expr>>, Vec<String>, + HashMap<String, P<ast::Expr>>)>) { let mut args = Vec::new(); - let mut names = HashMap::<String, Gc<ast::Expr>>::new(); + let mut names = HashMap::<String, P<ast::Expr>>::new(); let mut order = Vec::new(); let mut p = ecx.new_parser_from_tts(tts); @@ -323,44 +322,44 @@ impl<'a, 'b> Context<'a, 'b> { /// These attributes are applied to all statics that this syntax extension /// will generate. - fn static_attrs(&self) -> Vec<ast::Attribute> { + fn static_attrs(ecx: &ExtCtxt, fmtsp: Span) -> Vec<ast::Attribute> { // Flag statics as `inline` so LLVM can merge duplicate globals as much // as possible (which we're generating a whole lot of). - let unnamed = self.ecx.meta_word(self.fmtsp, InternedString::new("inline")); - let unnamed = self.ecx.attribute(self.fmtsp, unnamed); + let unnamed = ecx.meta_word(fmtsp, InternedString::new("inline")); + let unnamed = ecx.attribute(fmtsp, unnamed); // Do not warn format string as dead code - let dead_code = self.ecx.meta_word(self.fmtsp, - InternedString::new("dead_code")); - let allow_dead_code = self.ecx.meta_list(self.fmtsp, - InternedString::new("allow"), - vec!(dead_code)); - let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code); - return vec!(unnamed, allow_dead_code); + let dead_code = ecx.meta_word(fmtsp, InternedString::new("dead_code")); + let allow_dead_code = ecx.meta_list(fmtsp, + InternedString::new("allow"), + vec![dead_code]); + let allow_dead_code = ecx.attribute(fmtsp, allow_dead_code); + vec![unnamed, allow_dead_code] } - fn rtpath(&self, s: &str) -> Vec<ast::Ident> { - vec!(self.ecx.ident_of("std"), self.ecx.ident_of("fmt"), - self.ecx.ident_of("rt"), self.ecx.ident_of(s)) + fn rtpath(ecx: &ExtCtxt, s: &str) -> Vec<ast::Ident> { + vec![ecx.ident_of("std"), ecx.ident_of("fmt"), ecx.ident_of("rt"), ecx.ident_of(s)] } - fn trans_count(&self, c: parse::Count) -> Gc<ast::Expr> { + fn trans_count(&self, c: parse::Count) -> P<ast::Expr> { let sp = self.fmtsp; match c { parse::CountIs(i) => { - self.ecx.expr_call_global(sp, self.rtpath("CountIs"), + self.ecx.expr_call_global(sp, Context::rtpath(self.ecx, "CountIs"), vec!(self.ecx.expr_uint(sp, i))) } parse::CountIsParam(i) => { - self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"), + self.ecx.expr_call_global(sp, Context::rtpath(self.ecx, "CountIsParam"), vec!(self.ecx.expr_uint(sp, i))) } parse::CountImplied => { - let path = self.ecx.path_global(sp, self.rtpath("CountImplied")); + let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, + "CountImplied")); self.ecx.expr_path(path) } parse::CountIsNextParam => { - let path = self.ecx.path_global(sp, self.rtpath("CountIsNextParam")); + let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, + "CountIsNextParam")); self.ecx.expr_path(path) } parse::CountIsName(n) => { @@ -369,14 +368,14 @@ impl<'a, 'b> Context<'a, 'b> { None => 0, // error already emitted elsewhere }; let i = i + self.args.len(); - self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"), + self.ecx.expr_call_global(sp, Context::rtpath(self.ecx, "CountIsParam"), vec!(self.ecx.expr_uint(sp, i))) } } } /// Translate the accumulated string literals to a literal expression - fn trans_literal_string(&mut self) -> Gc<ast::Expr> { + fn trans_literal_string(&mut self) -> P<ast::Expr> { let sp = self.fmtsp; let s = token::intern_and_get_ident(self.literal.as_slice()); self.literal.clear(); @@ -385,7 +384,7 @@ impl<'a, 'b> Context<'a, 'b> { /// Translate a `parse::Piece` to a static `rt::Argument` or append /// to the `literal` string. - fn trans_piece(&mut self, piece: &parse::Piece) -> Option<Gc<ast::Expr>> { + fn trans_piece(&mut self, piece: &parse::Piece) -> Option<P<ast::Expr>> { let sp = self.fmtsp; match *piece { parse::String(s) => { @@ -397,12 +396,12 @@ impl<'a, 'b> Context<'a, 'b> { let pos = match arg.position { // These two have a direct mapping parse::ArgumentNext => { - let path = self.ecx.path_global(sp, - self.rtpath("ArgumentNext")); + let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, + "ArgumentNext")); self.ecx.expr_path(path) } parse::ArgumentIs(i) => { - self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"), + self.ecx.expr_call_global(sp, Context::rtpath(self.ecx, "ArgumentIs"), vec!(self.ecx.expr_uint(sp, i))) } // Named arguments are converted to positional arguments at @@ -413,7 +412,7 @@ impl<'a, 'b> Context<'a, 'b> { None => 0, // error already emitted elsewhere }; let i = i + self.args.len(); - self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"), + self.ecx.expr_call_global(sp, Context::rtpath(self.ecx, "ArgumentIs"), vec!(self.ecx.expr_uint(sp, i))) } }; @@ -440,23 +439,23 @@ impl<'a, 'b> Context<'a, 'b> { let fill = self.ecx.expr_lit(sp, ast::LitChar(fill)); let align = match arg.format.align { parse::AlignLeft => { - self.ecx.path_global(sp, self.rtpath("AlignLeft")) + self.ecx.path_global(sp, Context::rtpath(self.ecx, "AlignLeft")) } parse::AlignRight => { - self.ecx.path_global(sp, self.rtpath("AlignRight")) + self.ecx.path_global(sp, Context::rtpath(self.ecx, "AlignRight")) } parse::AlignCenter => { - self.ecx.path_global(sp, self.rtpath("AlignCenter")) + self.ecx.path_global(sp, Context::rtpath(self.ecx, "AlignCenter")) } parse::AlignUnknown => { - self.ecx.path_global(sp, self.rtpath("AlignUnknown")) + self.ecx.path_global(sp, Context::rtpath(self.ecx, "AlignUnknown")) } }; let align = self.ecx.expr_path(align); let flags = self.ecx.expr_uint(sp, arg.format.flags); let prec = self.trans_count(arg.format.precision); let width = self.trans_count(arg.format.width); - let path = self.ecx.path_global(sp, self.rtpath("FormatSpec")); + let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "FormatSpec")); let fmt = self.ecx.expr_struct(sp, path, vec!( self.ecx.field_imm(sp, self.ecx.ident_of("fill"), fill), self.ecx.field_imm(sp, self.ecx.ident_of("align"), align), @@ -464,7 +463,7 @@ impl<'a, 'b> Context<'a, 'b> { self.ecx.field_imm(sp, self.ecx.ident_of("precision"), prec), self.ecx.field_imm(sp, self.ecx.ident_of("width"), width))); - let path = self.ecx.path_global(sp, self.rtpath("Argument")); + let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "Argument")); Some(self.ecx.expr_struct(sp, path, vec!( self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos), self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt)))) @@ -472,29 +471,28 @@ impl<'a, 'b> Context<'a, 'b> { } } - fn item_static_array(&self, + fn item_static_array(ecx: &mut ExtCtxt, name: ast::Ident, - piece_ty: Gc<ast::Ty>, - pieces: Vec<Gc<ast::Expr>>) - -> ast::Stmt - { - let pieces_len = self.ecx.expr_uint(self.fmtsp, pieces.len()); - let fmt = self.ecx.expr_vec(self.fmtsp, pieces); + piece_ty: P<ast::Ty>, + pieces: Vec<P<ast::Expr>>) + -> P<ast::Stmt> { + let fmtsp = piece_ty.span; + let pieces_len = ecx.expr_uint(fmtsp, pieces.len()); + let fmt = ecx.expr_vec(fmtsp, pieces); let ty = ast::TyFixedLengthVec( piece_ty, pieces_len ); - let ty = self.ecx.ty(self.fmtsp, ty); + let ty = ecx.ty(fmtsp, ty); let st = ast::ItemStatic(ty, ast::MutImmutable, fmt); - let item = self.ecx.item(self.fmtsp, name, - self.static_attrs(), st); - let decl = respan(self.fmtsp, ast::DeclItem(item)); - respan(self.fmtsp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID)) + let item = ecx.item(fmtsp, name, Context::static_attrs(ecx, fmtsp), st); + let decl = respan(fmtsp, ast::DeclItem(item)); + P(respan(fmtsp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) } /// Actually builds the expression which the iformat! block will be expanded /// to - fn to_expr(&self, invocation: Invocation) -> Gc<ast::Expr> { + fn to_expr(mut self, invocation: Invocation) -> P<ast::Expr> { let mut lets = Vec::new(); let mut locals = Vec::new(); let mut names = Vec::from_fn(self.name_positions.len(), |_| None); @@ -502,10 +500,10 @@ impl<'a, 'b> Context<'a, 'b> { let mut heads = Vec::new(); // First, declare all of our methods that are statics - for &method in self.method_statics.iter() { + for method in self.method_statics.move_iter() { let decl = respan(self.fmtsp, ast::DeclItem(method)); - lets.push(box(GC) respan(self.fmtsp, - ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))); + lets.push(P(respan(self.fmtsp, + ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))); } // Next, build up the static array which will become our precompiled @@ -517,9 +515,10 @@ impl<'a, 'b> Context<'a, 'b> { self.ecx.ty_ident(self.fmtsp, self.ecx.ident_of("str")), Some(static_lifetime), ast::MutImmutable); - lets.push(box(GC) self.item_static_array(static_str_name, - piece_ty, - self.str_pieces.clone())); + lets.push(Context::item_static_array(self.ecx, + static_str_name, + piece_ty, + self.str_pieces)); // Then, build up the static array which will store our precompiled // nonstandard placeholders, if there are any. @@ -527,13 +526,14 @@ impl<'a, 'b> Context<'a, 'b> { if !self.all_pieces_simple { let piece_ty = self.ecx.ty_path(self.ecx.path_all( self.fmtsp, - true, self.rtpath("Argument"), + true, Context::rtpath(self.ecx, "Argument"), vec![static_lifetime], vec![] ), None); - lets.push(box(GC) self.item_static_array(static_args_name, - piece_ty, - self.pieces.clone())); + lets.push(Context::item_static_array(self.ecx, + static_args_name, + piece_ty, + self.pieces)); } // Right now there is a bug such that for the expression: @@ -543,31 +543,35 @@ impl<'a, 'b> Context<'a, 'b> { // format! string are shoved into locals. Furthermore, we shove the address // of each variable because we don't want to move out of the arguments // passed to this function. - for (i, &e) in self.args.iter().enumerate() { - if self.arg_types.get(i).is_none() { - continue // error already generated - } + for (i, e) in self.args.move_iter().enumerate() { + let arg_ty = match self.arg_types.get(i).as_ref() { + Some(ty) => ty, + None => continue // error already generated + }; let name = self.ecx.ident_of(format!("__arg{}", i).as_slice()); pats.push(self.ecx.pat_ident(e.span, name)); + locals.push(Context::format_arg(self.ecx, e.span, arg_ty, + self.ecx.expr_ident(e.span, name))); heads.push(self.ecx.expr_addr_of(e.span, e)); - locals.push(self.format_arg(e.span, Exact(i), - self.ecx.expr_ident(e.span, name))); } for name in self.name_ordering.iter() { - let e = match self.names.find(name) { - Some(&e) if self.name_types.contains_key(name) => e, - Some(..) | None => continue + let e = match self.names.pop(name) { + Some(e) => e, + None => continue + }; + let arg_ty = match self.name_types.find(name) { + Some(ty) => ty, + None => continue }; let lname = self.ecx.ident_of(format!("__arg{}", *name).as_slice()); pats.push(self.ecx.pat_ident(e.span, lname)); - heads.push(self.ecx.expr_addr_of(e.span, e)); *names.get_mut(*self.name_positions.get(name)) = - Some(self.format_arg(e.span, - Named((*name).clone()), - self.ecx.expr_ident(e.span, lname))); + Some(Context::format_arg(self.ecx, e.span, arg_ty, + self.ecx.expr_ident(e.span, lname))); + heads.push(self.ecx.expr_addr_of(e.span, e)); } // Now create a vector containing all the arguments @@ -611,12 +615,14 @@ impl<'a, 'b> Context<'a, 'b> { let res = self.ecx.expr_ident(self.fmtsp, resname); let result = match invocation { Call(e) => { - self.ecx.expr_call(e.span, e, - vec!(self.ecx.expr_addr_of(e.span, res))) + let span = e.span; + self.ecx.expr_call(span, e, + vec!(self.ecx.expr_addr_of(span, res))) } MethodCall(e, m) => { - self.ecx.expr_method_call(e.span, e, m, - vec!(self.ecx.expr_addr_of(e.span, res))) + let span = e.span; + self.ecx.expr_method_call(span, e, m, + vec!(self.ecx.expr_addr_of(span, res))) } }; let body = self.ecx.expr_block(self.ecx.block(self.fmtsp, lets, @@ -655,13 +661,9 @@ impl<'a, 'b> Context<'a, 'b> { self.ecx.expr_match(self.fmtsp, head, vec!(arm)) } - fn format_arg(&self, sp: Span, argno: Position, arg: Gc<ast::Expr>) - -> Gc<ast::Expr> { - let ty = match argno { - Exact(ref i) => self.arg_types.get(*i).get_ref(), - Named(ref s) => self.name_types.get(s) - }; - + fn format_arg(ecx: &ExtCtxt, sp: Span, + ty: &ArgumentType, arg: P<ast::Expr>) + -> P<ast::Expr> { let (krate, fmt_fn) = match *ty { Known(ref tyname) => { match tyname.as_slice() { @@ -681,36 +683,35 @@ impl<'a, 'b> Context<'a, 'b> { "x" => ("std", "secret_lower_hex"), "X" => ("std", "secret_upper_hex"), _ => { - self.ecx - .span_err(sp, - format!("unknown format trait `{}`", - *tyname).as_slice()); + ecx.span_err(sp, + format!("unknown format trait `{}`", + *tyname).as_slice()); ("std", "dummy") } } } String => { - return self.ecx.expr_call_global(sp, vec!( - self.ecx.ident_of("std"), - self.ecx.ident_of("fmt"), - self.ecx.ident_of("argumentstr")), vec!(arg)) + return ecx.expr_call_global(sp, vec![ + ecx.ident_of("std"), + ecx.ident_of("fmt"), + ecx.ident_of("argumentstr")], vec![arg]) } Unsigned => { - return self.ecx.expr_call_global(sp, vec!( - self.ecx.ident_of("std"), - self.ecx.ident_of("fmt"), - self.ecx.ident_of("argumentuint")), vec!(arg)) + return ecx.expr_call_global(sp, vec![ + ecx.ident_of("std"), + ecx.ident_of("fmt"), + ecx.ident_of("argumentuint")], vec![arg]) } }; - let format_fn = self.ecx.path_global(sp, vec!( - self.ecx.ident_of(krate), - self.ecx.ident_of("fmt"), - self.ecx.ident_of(fmt_fn))); - self.ecx.expr_call_global(sp, vec!( - self.ecx.ident_of("std"), - self.ecx.ident_of("fmt"), - self.ecx.ident_of("argument")), vec!(self.ecx.expr_path(format_fn), arg)) + let format_fn = ecx.path_global(sp, vec![ + ecx.ident_of(krate), + ecx.ident_of("fmt"), + ecx.ident_of(fmt_fn)]); + ecx.expr_call_global(sp, vec![ + ecx.ident_of("std"), + ecx.ident_of("fmt"), + ecx.ident_of("argument")], vec![ecx.expr_path(format_fn), arg]) } } @@ -744,12 +745,11 @@ pub fn expand_format_args_method<'cx>(ecx: &'cx mut ExtCtxt, sp: Span, /// expression. pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span, invocation: Invocation, - efmt: Gc<ast::Expr>, - args: Vec<Gc<ast::Expr>>, + efmt: P<ast::Expr>, + args: Vec<P<ast::Expr>>, name_ordering: Vec<String>, - names: HashMap<String, Gc<ast::Expr>>) - -> Gc<ast::Expr> -{ + names: HashMap<String, P<ast::Expr>>) + -> P<ast::Expr> { let arg_types = Vec::from_fn(args.len(), |_| None); let mut cx = Context { ecx: ecx, @@ -796,7 +796,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span, } match parser.errors.shift() { Some(error) => { - cx.ecx.span_err(efmt.span, + cx.ecx.span_err(cx.fmtsp, format!("invalid format string: {}", error).as_slice()); return DummyResult::raw_expr(sp); diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 808e671f868..6f13a2e6a51 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -15,8 +15,7 @@ use ext::base; use ext::build::AstBuilder; use parse::token::*; use parse::token; - -use std::gc::Gc; +use ptr::P; /** * @@ -36,14 +35,13 @@ pub mod rt { use parse::token; use parse; use print::pprust; + use ptr::P; use ast::{TokenTree, Generics, Expr}; pub use parse::new_parser_from_tts; pub use codemap::{BytePos, Span, dummy_spanned}; - use std::gc::Gc; - pub trait ToTokens { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> ; } @@ -107,13 +105,13 @@ pub mod rt { } macro_rules! impl_to_source( - (Gc<$t:ty>, $pp:ident) => ( - impl ToSource for Gc<$t> { + (P<$t:ty>, $pp:ident) => ( + impl ToSource for P<$t> { fn to_source(&self) -> String { pprust::$pp(&**self) } } - impl ToSourceWithHygiene for Gc<$t> { + impl ToSourceWithHygiene for P<$t> { fn to_source_with_hygiene(&self) -> String { pprust::with_hygiene::$pp(&**self) } @@ -182,18 +180,18 @@ pub mod rt { impl_to_source!(ast::Block, block_to_string) impl_to_source!(ast::Arg, arg_to_string) impl_to_source!(Generics, generics_to_string) - impl_to_source!(Gc<ast::Item>, item_to_string) - impl_to_source!(Gc<ast::Method>, method_to_string) - impl_to_source!(Gc<ast::Stmt>, stmt_to_string) - impl_to_source!(Gc<ast::Expr>, expr_to_string) - impl_to_source!(Gc<ast::Pat>, pat_to_string) + impl_to_source!(P<ast::Item>, item_to_string) + impl_to_source!(P<ast::Method>, method_to_string) + impl_to_source!(P<ast::Stmt>, stmt_to_string) + impl_to_source!(P<ast::Expr>, expr_to_string) + impl_to_source!(P<ast::Pat>, pat_to_string) impl_to_source!(ast::Arm, arm_to_string) impl_to_source_slice!(ast::Ty, ", ") - impl_to_source_slice!(Gc<ast::Item>, "\n\n") + impl_to_source_slice!(P<ast::Item>, "\n\n") impl ToSource for ast::Attribute_ { fn to_source(&self) -> String { - pprust::attribute_to_string(&dummy_spanned(*self)) + pprust::attribute_to_string(&dummy_spanned(self.clone())) } } impl ToSourceWithHygiene for ast::Attribute_ { @@ -315,16 +313,16 @@ pub mod rt { ) impl_to_tokens!(ast::Ident) - impl_to_tokens!(Gc<ast::Item>) - impl_to_tokens!(Gc<ast::Pat>) + impl_to_tokens!(P<ast::Item>) + impl_to_tokens!(P<ast::Pat>) impl_to_tokens!(ast::Arm) - impl_to_tokens!(Gc<ast::Method>) - impl_to_tokens_lifetime!(&'a [Gc<ast::Item>]) + impl_to_tokens!(P<ast::Method>) + impl_to_tokens_lifetime!(&'a [P<ast::Item>]) impl_to_tokens!(ast::Ty) impl_to_tokens_lifetime!(&'a [ast::Ty]) impl_to_tokens!(Generics) - impl_to_tokens!(Gc<ast::Stmt>) - impl_to_tokens!(Gc<ast::Expr>) + impl_to_tokens!(P<ast::Stmt>) + impl_to_tokens!(P<ast::Expr>) impl_to_tokens!(ast::Block) impl_to_tokens!(ast::Arg) impl_to_tokens!(ast::Attribute_) @@ -344,9 +342,9 @@ pub mod rt { impl_to_tokens!(u64) pub trait ExtParseUtils { - fn parse_item(&self, s: String) -> Gc<ast::Item>; - fn parse_expr(&self, s: String) -> Gc<ast::Expr>; - fn parse_stmt(&self, s: String) -> Gc<ast::Stmt>; + fn parse_item(&self, s: String) -> P<ast::Item>; + fn parse_expr(&self, s: String) -> P<ast::Expr>; + fn parse_stmt(&self, s: String) -> P<ast::Stmt>; fn parse_tts(&self, s: String) -> Vec<ast::TokenTree>; } @@ -358,7 +356,7 @@ pub mod rt { impl<'a> ExtParseUtils for ExtCtxt<'a> { - fn parse_item(&self, s: String) -> Gc<ast::Item> { + fn parse_item(&self, s: String) -> P<ast::Item> { let res = parse::parse_item_from_source_str( "<quote expansion>".to_string(), s, @@ -373,7 +371,7 @@ pub mod rt { } } - fn parse_stmt(&self, s: String) -> Gc<ast::Stmt> { + fn parse_stmt(&self, s: String) -> P<ast::Stmt> { parse::parse_stmt_from_source_str("<quote expansion>".to_string(), s, self.cfg(), @@ -381,7 +379,7 @@ pub mod rt { self.parse_sess()) } - fn parse_expr(&self, s: String) -> Gc<ast::Expr> { + fn parse_expr(&self, s: String) -> P<ast::Expr> { parse::parse_expr_from_source_str("<quote expansion>".to_string(), s, self.cfg(), @@ -491,7 +489,7 @@ fn id_ext(str: &str) -> ast::Ident { } // Lift an ident to the expr that evaluates to that ident. -fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> { +fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> { let e_str = cx.expr_str(sp, token::get_ident(ident)); cx.expr_method_call(sp, cx.expr_ident(sp, id_ext("ext_cx")), @@ -500,7 +498,7 @@ fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> { } // Lift a name to the expr that evaluates to that name -fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> { +fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> { let e_str = cx.expr_str(sp, token::get_ident(ident)); cx.expr_method_call(sp, cx.expr_ident(sp, id_ext("ext_cx")), @@ -508,17 +506,17 @@ fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> { vec!(e_str)) } -fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> { +fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> { let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name)); cx.expr_path(cx.path_global(sp, idents)) } -fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> { +fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> { let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name)); cx.expr_path(cx.path_global(sp, idents)) } -fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> Gc<ast::Expr> { +fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> P<ast::Expr> { let name = match bop { PLUS => "PLUS", MINUS => "MINUS", @@ -534,7 +532,7 @@ fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> Gc<ast::Expr> { mk_token_path(cx, sp, name) } -fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> { +fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> { match *tok { BINOP(binop) => { @@ -640,7 +638,7 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> { } -fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<Gc<ast::Stmt>> { +fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<P<ast::Stmt>> { match *tt { ast::TTTok(sp, ref tok) => { let e_sp = cx.expr_ident(sp, id_ext("_sp")); @@ -680,7 +678,7 @@ fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<Gc<ast::Stmt>> { } fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree]) - -> Vec<Gc<ast::Stmt>> { + -> Vec<P<ast::Stmt>> { let mut ss = Vec::new(); for tt in tts.iter() { ss.push_all_move(mk_tt(cx, sp, tt)); @@ -689,7 +687,7 @@ fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree]) } fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree]) - -> (Gc<ast::Expr>, Gc<ast::Expr>) { + -> (P<ast::Expr>, P<ast::Expr>) { // NB: It appears that the main parser loses its mind if we consider // $foo as a TTNonterminal during the main parse, so we have to re-parse // under quote_depth > 0. This is silly and should go away; the _guess_ is @@ -757,8 +755,8 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree]) fn expand_wrapper(cx: &ExtCtxt, sp: Span, - cx_expr: Gc<ast::Expr>, - expr: Gc<ast::Expr>) -> Gc<ast::Expr> { + cx_expr: P<ast::Expr>, + expr: P<ast::Expr>) -> P<ast::Expr> { let uses = [ &["syntax", "ext", "quote", "rt"], ].iter().map(|path| { @@ -776,8 +774,8 @@ fn expand_wrapper(cx: &ExtCtxt, fn expand_parse_call(cx: &ExtCtxt, sp: Span, parse_method: &str, - arg_exprs: Vec<Gc<ast::Expr>>, - tts: &[ast::TokenTree]) -> Gc<ast::Expr> { + arg_exprs: Vec<P<ast::Expr>> , + tts: &[ast::TokenTree]) -> P<ast::Expr> { let (cx_expr, tts_expr) = expand_tts(cx, sp, tts); let cfg_call = || cx.expr_method_call( diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 509d5bd4421..3006bcaf6f8 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -87,9 +87,9 @@ use parse::attr::ParserAttr; use parse::parser::{LifetimeAndTypesWithoutColons, Parser}; use parse::token::{Token, EOF, Nonterminal}; use parse::token; +use ptr::P; use std::rc::Rc; -use std::gc::GC; use std::collections::HashMap; /* to avoid costly uniqueness checks, we require that `MatchSeq` always has a @@ -451,7 +451,7 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal { "meta" => token::NtMeta(p.parse_meta_item()), "tt" => { p.quote_depth += 1u; //but in theory, non-quoted tts might be useful - let res = token::NtTT(box(GC) p.parse_token_tree()); + let res = token::NtTT(P(p.parse_token_tree())); p.quote_depth -= 1u; res } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index d8f0eb32ad7..6c7bbb2384c 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -8,8 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{Ident, Matcher_, Matcher, MatchTok, MatchNonterminal, MatchSeq}; -use ast::{TTDelim}; +use ast::{Ident, Matcher_, Matcher, MatchTok, MatchNonterminal, MatchSeq, TTDelim}; use ast; use codemap::{Span, Spanned, DUMMY_SP}; use ext::base::{ExtCtxt, MacResult, MacroDef}; @@ -24,11 +23,12 @@ use parse::token::{special_idents, gensym_ident}; use parse::token::{FAT_ARROW, SEMI, NtMatchers, NtTT, EOF}; use parse::token; use print; +use ptr::P; + use util::small_vector::SmallVector; use std::cell::RefCell; use std::rc::Rc; -use std::gc::Gc; struct ParserAnyMacro<'a> { parser: RefCell<Parser<'a>>, @@ -58,17 +58,17 @@ impl<'a> ParserAnyMacro<'a> { } impl<'a> MacResult for ParserAnyMacro<'a> { - fn make_expr(&self) -> Option<Gc<ast::Expr>> { + fn make_expr(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Expr>> { let ret = self.parser.borrow_mut().parse_expr(); self.ensure_complete_parse(true); Some(ret) } - fn make_pat(&self) -> Option<Gc<ast::Pat>> { + fn make_pat(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Pat>> { let ret = self.parser.borrow_mut().parse_pat(); self.ensure_complete_parse(false); Some(ret) } - fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> { + fn make_items(self: Box<ParserAnyMacro<'a>>) -> Option<SmallVector<P<ast::Item>>> { let mut ret = SmallVector::zero(); loop { let mut parser = self.parser.borrow_mut(); @@ -84,7 +84,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> { Some(ret) } - fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> { + fn make_methods(self: Box<ParserAnyMacro<'a>>) -> Option<SmallVector<P<ast::Method>>> { let mut ret = SmallVector::zero(); loop { let mut parser = self.parser.borrow_mut(); @@ -97,7 +97,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> { Some(ret) } - fn make_stmt(&self) -> Option<Gc<ast::Stmt>> { + fn make_stmt(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Stmt>> { let attrs = self.parser.borrow_mut().parse_outer_attributes(); let ret = self.parser.borrow_mut().parse_stmt(attrs); self.ensure_complete_parse(true); @@ -127,11 +127,11 @@ impl TTMacroExpander for MacroRulesMacroExpander { } struct MacroRulesDefiner { - def: RefCell<Option<MacroDef>> + def: Option<MacroDef> } impl MacResult for MacroRulesDefiner { - fn make_def(&self) -> Option<MacroDef> { - Some(self.def.borrow_mut().take().expect("MacroRulesDefiner expanded twice")) + fn make_def(&mut self) -> Option<MacroDef> { + Some(self.def.take().expect("empty MacroRulesDefiner")) } } @@ -170,8 +170,8 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt, Success(named_matches) => { let rhs = match *rhses[i] { // okay, what's your transcriber? - MatchedNonterminal(NtTT(tt)) => { - match *tt { + MatchedNonterminal(NtTT(ref tt)) => { + match **tt { // cut off delimiters; don't parse 'em TTDelim(ref tts) => { (*tts).slice(1u,(*tts).len()-1u) @@ -269,9 +269,9 @@ pub fn add_new_extension<'cx>(cx: &'cx mut ExtCtxt, }; box MacroRulesDefiner { - def: RefCell::new(Some(MacroDef { + def: Some(MacroDef { name: token::get_ident(name).to_string(), ext: NormalTT(exp, Some(sp)) - })) + }) } as Box<MacResult+'cx> } diff --git a/src/libsyntax/owned_slice.rs b/src/libsyntax/owned_slice.rs index d368477cd33..7d8a9e08ba0 100644 --- a/src/libsyntax/owned_slice.rs +++ b/src/libsyntax/owned_slice.rs @@ -11,7 +11,7 @@ use std::fmt; use std::default::Default; use std::hash; -use std::{mem, raw, ptr, slice}; +use std::{mem, raw, ptr, slice, vec}; use serialize::{Encodable, Decodable, Encoder, Decoder}; /// A non-growable owned slice. This would preferably become `~[T]` @@ -105,6 +105,10 @@ impl<T> OwnedSlice<T> { self.as_slice().iter() } + pub fn move_iter(self) -> vec::MoveItems<T> { + self.into_vec().move_iter() + } + pub fn map<U>(&self, f: |&T| -> U) -> OwnedSlice<U> { self.iter().map(f).collect() } diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index eca02d06ca9..74b93e75e64 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -15,8 +15,7 @@ use parse::common::*; //resolve bug? use parse::token; use parse::parser::Parser; use parse::token::INTERPOLATED; - -use std::gc::{Gc, GC}; +use ptr::P; /// A parser that can parse attributes. pub trait ParserAttr { @@ -24,9 +23,9 @@ pub trait ParserAttr { fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute; fn parse_inner_attrs_and_next(&mut self) -> (Vec<ast::Attribute>, Vec<ast::Attribute>); - fn parse_meta_item(&mut self) -> Gc<ast::MetaItem>; - fn parse_meta_seq(&mut self) -> Vec<Gc<ast::MetaItem>>; - fn parse_optional_meta(&mut self) -> Vec<Gc<ast::MetaItem>>; + fn parse_meta_item(&mut self) -> P<ast::MetaItem>; + fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>>; + fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>>; } impl<'a> ParserAttr for Parser<'a> { @@ -160,13 +159,20 @@ impl<'a> ParserAttr for Parser<'a> { /// matches meta_item = IDENT /// | IDENT = lit /// | IDENT meta_seq - fn parse_meta_item(&mut self) -> Gc<ast::MetaItem> { - match self.token { - token::INTERPOLATED(token::NtMeta(e)) => { + fn parse_meta_item(&mut self) -> P<ast::MetaItem> { + let nt_meta = match self.token { + token::INTERPOLATED(token::NtMeta(ref e)) => { + Some(e.clone()) + } + _ => None + }; + + match nt_meta { + Some(meta) => { self.bump(); - return e + return meta; } - _ => {} + None => {} } let lo = self.span.lo; @@ -187,29 +193,29 @@ impl<'a> ParserAttr for Parser<'a> { } } let hi = self.span.hi; - box(GC) spanned(lo, hi, ast::MetaNameValue(name, lit)) + P(spanned(lo, hi, ast::MetaNameValue(name, lit))) } token::LPAREN => { let inner_items = self.parse_meta_seq(); let hi = self.span.hi; - box(GC) spanned(lo, hi, ast::MetaList(name, inner_items)) + P(spanned(lo, hi, ast::MetaList(name, inner_items))) } _ => { let hi = self.last_span.hi; - box(GC) spanned(lo, hi, ast::MetaWord(name)) + P(spanned(lo, hi, ast::MetaWord(name))) } } } /// matches meta_seq = ( COMMASEP(meta_item) ) - fn parse_meta_seq(&mut self) -> Vec<Gc<ast::MetaItem>> { + fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>> { self.parse_seq(&token::LPAREN, &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_meta_item()).node } - fn parse_optional_meta(&mut self) -> Vec<Gc<ast::MetaItem>> { + fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>> { match self.token { token::LPAREN => self.parse_meta_seq(), _ => Vec::new() diff --git a/src/libsyntax/parse/classify.rs b/src/libsyntax/parse/classify.rs index 516f22cdf4d..cdd221aca7c 100644 --- a/src/libsyntax/parse/classify.rs +++ b/src/libsyntax/parse/classify.rs @@ -13,7 +13,6 @@ // Predicates on exprs and stmts that the pretty-printer and parser use use ast; -use std::gc::Gc; /// Does this expression require a semicolon to be treated /// as a statement? The negation of this: 'can this expression @@ -22,7 +21,7 @@ use std::gc::Gc; /// if true {...} else {...} /// |x| 5 /// isn't parsed as (if true {...} else {...} | x) | 5 -pub fn expr_requires_semi_to_be_stmt(e: Gc<ast::Expr>) -> bool { +pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool { match e.node { ast::ExprIf(..) | ast::ExprMatch(..) @@ -34,25 +33,25 @@ pub fn expr_requires_semi_to_be_stmt(e: Gc<ast::Expr>) -> bool { } } -pub fn expr_is_simple_block(e: Gc<ast::Expr>) -> bool { +pub fn expr_is_simple_block(e: &ast::Expr) -> bool { match e.node { - ast::ExprBlock(block) => block.rules == ast::DefaultBlock, - _ => false + ast::ExprBlock(ref block) => block.rules == ast::DefaultBlock, + _ => false } } /// this statement requires a semicolon after it. /// note that in one case (stmt_semi), we've already /// seen the semicolon, and thus don't need another. -pub fn stmt_ends_with_semi(stmt: &ast::Stmt) -> bool { - return match stmt.node { - ast::StmtDecl(d, _) => { +pub fn stmt_ends_with_semi(stmt: &ast::Stmt_) -> bool { + match *stmt { + ast::StmtDecl(ref d, _) => { match d.node { ast::DeclLocal(_) => true, ast::DeclItem(_) => false } } - ast::StmtExpr(e, _) => { expr_requires_semi_to_be_stmt(e) } + ast::StmtExpr(ref e, _) => { expr_requires_semi_to_be_stmt(&**e) } ast::StmtSemi(..) => { false } ast::StmtMac(..) => { false } } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 585b98925cc..c4ee1b60da4 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -15,9 +15,9 @@ use codemap::{Span, CodeMap, FileMap}; use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto}; use parse::attr::ParserAttr; use parse::parser::Parser; +use ptr::P; use std::cell::RefCell; -use std::gc::Gc; use std::io::File; use std::rc::Rc; use std::str; @@ -106,7 +106,7 @@ pub fn parse_expr_from_source_str(name: String, source: String, cfg: ast::CrateConfig, sess: &ParseSess) - -> Gc<ast::Expr> { + -> P<ast::Expr> { let mut p = new_parser_from_source_str(sess, cfg, name, source); maybe_aborted(p.parse_expr(), p) } @@ -115,7 +115,7 @@ pub fn parse_item_from_source_str(name: String, source: String, cfg: ast::CrateConfig, sess: &ParseSess) - -> Option<Gc<ast::Item>> { + -> Option<P<ast::Item>> { let mut p = new_parser_from_source_str(sess, cfg, name, source); maybe_aborted(p.parse_item_with_outer_attributes(),p) } @@ -124,7 +124,7 @@ pub fn parse_meta_from_source_str(name: String, source: String, cfg: ast::CrateConfig, sess: &ParseSess) - -> Gc<ast::MetaItem> { + -> P<ast::MetaItem> { let mut p = new_parser_from_source_str(sess, cfg, name, source); maybe_aborted(p.parse_meta_item(),p) } @@ -134,7 +134,7 @@ pub fn parse_stmt_from_source_str(name: String, cfg: ast::CrateConfig, attrs: Vec<ast::Attribute> , sess: &ParseSess) - -> Gc<ast::Stmt> { + -> P<ast::Stmt> { let mut p = new_parser_from_source_str( sess, cfg, @@ -722,7 +722,7 @@ mod test { #[test] fn path_exprs_1() { assert!(string_to_expr("a".to_string()) == - box(GC) ast::Expr{ + P(ast::Expr{ id: ast::DUMMY_NODE_ID, node: ast::ExprPath(ast::Path { span: sp(0, 1), @@ -736,12 +736,12 @@ mod test { ), }), span: sp(0, 1) - }) + })) } #[test] fn path_exprs_2 () { assert!(string_to_expr("::a::b".to_string()) == - box(GC) ast::Expr { + P(ast::Expr { id: ast::DUMMY_NODE_ID, node: ast::ExprPath(ast::Path { span: sp(0, 6), @@ -760,7 +760,7 @@ mod test { ) }), span: sp(0, 6) - }) + })) } #[should_fail] @@ -953,9 +953,9 @@ mod test { #[test] fn ret_expr() { assert!(string_to_expr("return d".to_string()) == - box(GC) ast::Expr{ + P(ast::Expr{ id: ast::DUMMY_NODE_ID, - node:ast::ExprRet(Some(box(GC) ast::Expr{ + node:ast::ExprRet(Some(P(ast::Expr{ id: ast::DUMMY_NODE_ID, node:ast::ExprPath(ast::Path{ span: sp(7, 8), @@ -969,15 +969,15 @@ mod test { ), }), span:sp(7,8) - })), + }))), span:sp(0,8) - }) + })) } #[test] fn parse_stmt_1 () { assert!(string_to_stmt("b;".to_string()) == - box(GC) Spanned{ - node: ast::StmtExpr(box(GC) ast::Expr { + P(Spanned{ + node: ast::StmtExpr(P(ast::Expr { id: ast::DUMMY_NODE_ID, node: ast::ExprPath(ast::Path { span:sp(0,1), @@ -990,9 +990,9 @@ mod test { } ), }), - span: sp(0,1)}, + span: sp(0,1)}), ast::DUMMY_NODE_ID), - span: sp(0,1)}) + span: sp(0,1)})) } @@ -1004,14 +1004,14 @@ mod test { let sess = new_parse_sess(); let mut parser = string_to_parser(&sess, "b".to_string()); assert!(parser.parse_pat() - == box(GC) ast::Pat{ + == P(ast::Pat{ id: ast::DUMMY_NODE_ID, node: ast::PatIdent(ast::BindByValue(ast::MutImmutable), Spanned{ span:sp(0, 1), node: str_to_ident("b") }, None), - span: sp(0,1)}); + span: sp(0,1)})); parser_done(parser); } @@ -1020,7 +1020,7 @@ mod test { // this test depends on the intern order of "fn" and "int" assert!(string_to_item("fn a (b : int) { b; }".to_string()) == Some( - box(GC) ast::Item{ident:str_to_ident("a"), + P(ast::Item{ident:str_to_ident("a"), attrs:Vec::new(), id: ast::DUMMY_NODE_ID, node: ast::ItemFn(ast::P(ast::FnDecl { @@ -1040,7 +1040,7 @@ mod test { }, None, ast::DUMMY_NODE_ID), span:sp(10,13) }), - pat: box(GC) ast::Pat { + pat: P(ast::Pat { id: ast::DUMMY_NODE_ID, node: ast::PatIdent( ast::BindByValue(ast::MutImmutable), @@ -1050,7 +1050,7 @@ mod test { None ), span: sp(6,7) - }, + }), id: ast::DUMMY_NODE_ID }), output: ast::P(ast::Ty{id: ast::DUMMY_NODE_ID, @@ -1071,8 +1071,8 @@ mod test { }, ast::P(ast::Block { view_items: Vec::new(), - stmts: vec!(box(GC) Spanned{ - node: ast::StmtSemi(box(GC) ast::Expr{ + stmts: vec!(P(Spanned{ + node: ast::StmtSemi(P(ast::Expr{ id: ast::DUMMY_NODE_ID, node: ast::ExprPath( ast::Path{ @@ -1090,28 +1090,28 @@ mod test { } ), }), - span: sp(17,18)}, + span: sp(17,18)}), ast::DUMMY_NODE_ID), - span: sp(17,19)}), + span: sp(17,19)})), expr: None, id: ast::DUMMY_NODE_ID, rules: ast::DefaultBlock, // no idea span: sp(15,21), })), vis: ast::Inherited, - span: sp(0,21)})); + span: sp(0,21)}))); } #[test] fn parse_exprs () { // just make sure that they parse.... string_to_expr("3 + 4".to_string()); - string_to_expr("a::z.froob(b,box(GC)(987+3))".to_string()); + string_to_expr("a::z.froob(b,&(987+3))".to_string()); } #[test] fn attrs_fix_bug () { string_to_item("pub fn mk_file_writer(path: &Path, flags: &[FileFlag]) - -> Result<Gc<Writer>, String> { + -> Result<Box<Writer>, String> { #[cfg(windows)] fn wb() -> c_int { (O_WRONLY | libc::consts::os::extra::O_BINARY) as c_int diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 9ed9e626c3d..d47231bc3e2 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -21,8 +21,7 @@ use ast::{Expr, ExprLit, LitNil}; use codemap::{Span, respan}; use parse::parser; use parse::token; - -use std::gc::{Gc, GC}; +use ptr::P; /// The specific types of unsupported syntax #[deriving(PartialEq, Eq, Hash)] @@ -44,7 +43,7 @@ pub trait ParserObsoleteMethods { fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax); /// Reports an obsolete syntax non-fatal error, and returns /// a placeholder expression - fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> Gc<Expr>; + fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>; fn report(&mut self, sp: Span, kind: ObsoleteSyntax, @@ -105,9 +104,9 @@ impl<'a> ParserObsoleteMethods for parser::Parser<'a> { /// Reports an obsolete syntax non-fatal error, and returns /// a placeholder expression - fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> Gc<Expr> { + fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> { self.obsolete(sp, kind); - self.mk_expr(sp.lo, sp.hi, ExprLit(box(GC) respan(sp, LitNil))) + self.mk_expr(sp.lo, sp.hi, ExprLit(P(respan(sp, LitNil)))) } fn report(&mut self, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 328bdf88335..f41362cad41 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -39,9 +39,8 @@ use ast::{LitBool, LitChar, LitByte, LitBinary}; use ast::{LitNil, LitStr, LitInt, Local, LocalLet}; use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, Matcher, MatchNonterminal}; use ast::{MatchSeq, MatchTok, Method, MutTy, BiMul, Mutability}; -use ast::{MethodImplItem}; -use ast::{NamedField, UnNeg, NoReturn, UnNot, P, Pat, PatEnum}; -use ast::{PatIdent, PatLit, PatRange, PatRegion, PatStruct}; +use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, UnNot}; +use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct}; use ast::{PatTup, PatBox, PatWild, PatWildMulti, PatWildSingle}; use ast::{BiRem, RequiredMethod}; use ast::{RetStyle, Return, BiShl, BiShr, Stmt, StmtDecl}; @@ -79,12 +78,13 @@ use parse::token::{is_ident, is_ident_or_path, is_plain_ident}; use parse::token::{keywords, special_idents, token_to_binop}; use parse::token; use parse::{new_sub_parser_from_file, ParseSess}; +use ptr::P; use owned_slice::OwnedSlice; use std::collections::HashSet; use std::mem::replace; +use std::mem; use std::rc::Rc; -use std::gc::{Gc, GC}; use std::iter; #[allow(non_camel_case_types)] @@ -127,8 +127,8 @@ enum ItemOrViewItem { /// Indicates a failure to parse any kind of item. The attributes are /// returned. IoviNone(Vec<Attribute>), - IoviItem(Gc<Item>), - IoviForeignItem(Gc<ForeignItem>), + IoviItem(P<Item>), + IoviForeignItem(P<ForeignItem>), IoviViewItem(ViewItem) } @@ -143,8 +143,8 @@ macro_rules! maybe_whole_expr ( ($p:expr) => ( { let found = match $p.token { - INTERPOLATED(token::NtExpr(e)) => { - Some(e) + INTERPOLATED(token::NtExpr(ref e)) => { + Some((*e).clone()) } INTERPOLATED(token::NtPath(_)) => { // FIXME: The following avoids an issue with lexical borrowck scopes, @@ -156,7 +156,13 @@ macro_rules! maybe_whole_expr ( let span = $p.span; Some($p.mk_expr(span.lo, span.hi, ExprPath(pt))) } - INTERPOLATED(token::NtBlock(b)) => { + INTERPOLATED(token::NtBlock(_)) => { + // FIXME: The following avoids an issue with lexical borrowck scopes, + // but the clone is unfortunate. + let b = match $p.token { + INTERPOLATED(token::NtBlock(ref b)) => (*b).clone(), + _ => unreachable!() + }; let span = $p.span; Some($p.mk_expr(span.lo, span.hi, ExprBlock(b))) } @@ -286,8 +292,8 @@ fn maybe_append(lhs: Vec<Attribute> , rhs: Option<Vec<Attribute> >) struct ParsedItemsAndViewItems { attrs_remaining: Vec<Attribute>, view_items: Vec<ViewItem>, - items: Vec<Gc<Item>>, - foreign_items: Vec<Gc<ForeignItem>> + items: Vec<P<Item>> , + foreign_items: Vec<P<ForeignItem>> } /* ident is handled by common.rs */ @@ -484,8 +490,7 @@ impl<'a> Parser<'a> { /// Commit to parsing a complete expression `e` expected to be /// followed by some token from the set edible + inedible. Recover /// from anticipated input errors, discarding erroneous characters. - pub fn commit_expr(&mut self, e: Gc<Expr>, edible: &[token::Token], - inedible: &[token::Token]) { + pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token], inedible: &[token::Token]) { debug!("commit_expr {:?}", e); match e.node { ExprPath(..) => { @@ -500,17 +505,14 @@ impl<'a> Parser<'a> { self.expect_one_of(edible, inedible) } - pub fn commit_expr_expecting(&mut self, e: Gc<Expr>, edible: token::Token) { + pub fn commit_expr_expecting(&mut self, e: &Expr, edible: token::Token) { self.commit_expr(e, &[edible], &[]) } /// Commit to parsing a complete statement `s`, which expects to be /// followed by some token from the set edible + inedible. Check /// for recoverable input errors, discarding erroneous characters. - pub fn commit_stmt(&mut self, s: Gc<Stmt>, edible: &[token::Token], - inedible: &[token::Token]) { - debug!("commit_stmt {:?}", s); - let _s = s; // unused, but future checks might want to inspect `s`. + pub fn commit_stmt(&mut self, edible: &[token::Token], inedible: &[token::Token]) { if self.last_token .as_ref() .map_or(false, |t| is_ident_or_path(&**t)) { @@ -522,8 +524,8 @@ impl<'a> Parser<'a> { self.expect_one_of(edible, inedible) } - pub fn commit_stmt_expecting(&mut self, s: Gc<Stmt>, edible: token::Token) { - self.commit_stmt(s, &[edible], &[]) + pub fn commit_stmt_expecting(&mut self, edible: token::Token) { + self.commit_stmt(&[edible], &[]) } pub fn parse_ident(&mut self) -> ast::Ident { @@ -1043,12 +1045,12 @@ impl<'a> Parser<'a> { self.expect_keyword(keywords::Fn); let (decl, lifetimes) = self.parse_ty_fn_decl(true); - return TyBareFn(box(GC) BareFnTy { + TyBareFn(P(BareFnTy { abi: abi, fn_style: fn_style, lifetimes: lifetimes, decl: decl - }); + })) } /// Parses a procedure type (`proc`). The initial `proc` keyword must @@ -1084,13 +1086,13 @@ impl<'a> Parser<'a> { cf: ret_style, variadic: variadic }); - TyProc(box(GC) ClosureTy { + TyProc(P(ClosureTy { fn_style: NormalFn, onceness: Once, bounds: bounds, decl: decl, lifetimes: lifetime_defs, - }) + })) } /// Parses an optional unboxed closure kind (`&:`, `&mut:`, or `:`). @@ -1176,19 +1178,19 @@ impl<'a> Parser<'a> { match optional_unboxed_closure_kind { Some(unboxed_closure_kind) => { - TyUnboxedFn(box(GC) UnboxedFnTy { + TyUnboxedFn(P(UnboxedFnTy { kind: unboxed_closure_kind, decl: decl, - }) + })) } None => { - TyClosure(box(GC) ClosureTy { + TyClosure(P(ClosureTy { fn_style: fn_style, onceness: onceness, bounds: bounds, decl: decl, lifetimes: lifetime_defs, - }) + })) } } } @@ -1291,8 +1293,9 @@ impl<'a> Parser<'a> { debug!("parse_trait_methods(): parsing provided method"); let (inner_attrs, body) = p.parse_inner_attrs_and_block(); - let attrs = attrs.append(inner_attrs.as_slice()); - ProvidedMethod(box(GC) ast::Method { + let mut attrs = attrs; + attrs.extend(inner_attrs.move_iter()); + ProvidedMethod(P(ast::Method { attrs: attrs, id: ast::DUMMY_NODE_ID, span: mk_sp(lo, hi), @@ -1304,7 +1307,7 @@ impl<'a> Parser<'a> { d, body, vis) - }) + })) } _ => { @@ -1400,7 +1403,7 @@ impl<'a> Parser<'a> { if ts.len() == 1 && !one_tuple { self.expect(&token::RPAREN); - TyParen(*ts.get(0)) + TyParen(ts.move_iter().nth(0).unwrap()) } else { let t = TyTup(ts); self.expect(&token::RPAREN); @@ -1588,7 +1591,7 @@ impl<'a> Parser<'a> { } } - pub fn maybe_parse_fixed_vstore(&mut self) -> Option<Gc<ast::Expr>> { + pub fn maybe_parse_fixed_vstore(&mut self) -> Option<P<ast::Expr>> { if self.token == token::COMMA && self.look_ahead(1, |t| *t == token::DOTDOT) { self.bump(); @@ -1640,12 +1643,12 @@ impl<'a> Parser<'a> { } /// matches '-' lit | lit - pub fn parse_literal_maybe_minus(&mut self) -> Gc<Expr> { + pub fn parse_literal_maybe_minus(&mut self) -> P<Expr> { let minus_lo = self.span.lo; let minus_present = self.eat(&token::BINOP(token::MINUS)); let lo = self.span.lo; - let literal = box(GC) self.parse_lit(); + let literal = P(self.parse_lit()); let hi = self.span.hi; let expr = self.mk_expr(lo, hi, ExprLit(literal)); @@ -1894,85 +1897,84 @@ impl<'a> Parser<'a> { let e = self.parse_expr(); ast::Field { ident: spanned(lo, hi, i), - expr: e, span: mk_sp(lo, e.span.hi), + expr: e, } } - pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> Gc<Expr> { - box(GC) Expr { + pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> P<Expr> { + P(Expr { id: ast::DUMMY_NODE_ID, node: node, span: mk_sp(lo, hi), - } + }) } - pub fn mk_unary(&mut self, unop: ast::UnOp, expr: Gc<Expr>) -> ast::Expr_ { + pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::Expr_ { ExprUnary(unop, expr) } - pub fn mk_binary(&mut self, binop: ast::BinOp, - lhs: Gc<Expr>, rhs: Gc<Expr>) -> ast::Expr_ { + pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ { ExprBinary(binop, lhs, rhs) } - pub fn mk_call(&mut self, f: Gc<Expr>, args: Vec<Gc<Expr>>) -> ast::Expr_ { + pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::Expr_ { ExprCall(f, args) } fn mk_method_call(&mut self, ident: ast::SpannedIdent, tps: Vec<P<Ty>>, - args: Vec<Gc<Expr>>) + args: Vec<P<Expr>>) -> ast::Expr_ { ExprMethodCall(ident, tps, args) } - pub fn mk_index(&mut self, expr: Gc<Expr>, idx: Gc<Expr>) -> ast::Expr_ { + pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::Expr_ { ExprIndex(expr, idx) } - pub fn mk_field(&mut self, expr: Gc<Expr>, ident: ast::SpannedIdent, + pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent, tys: Vec<P<Ty>>) -> ast::Expr_ { ExprField(expr, ident, tys) } - pub fn mk_tup_field(&mut self, expr: Gc<Expr>, idx: codemap::Spanned<uint>, + pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<uint>, tys: Vec<P<Ty>>) -> ast::Expr_ { ExprTupField(expr, idx, tys) } pub fn mk_assign_op(&mut self, binop: ast::BinOp, - lhs: Gc<Expr>, rhs: Gc<Expr>) -> ast::Expr_ { + lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ { ExprAssignOp(binop, lhs, rhs) } - pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> Gc<Expr> { - box(GC) Expr { + pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> P<Expr> { + P(Expr { id: ast::DUMMY_NODE_ID, node: ExprMac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}), span: mk_sp(lo, hi), - } + }) } - pub fn mk_lit_u32(&mut self, i: u32) -> Gc<Expr> { + pub fn mk_lit_u32(&mut self, i: u32) -> P<Expr> { let span = &self.span; - let lv_lit = box(GC) codemap::Spanned { + let lv_lit = P(codemap::Spanned { node: LitInt(i as u64, ast::UnsignedIntLit(TyU32)), span: *span - }; + }); - box(GC) Expr { + P(Expr { id: ast::DUMMY_NODE_ID, node: ExprLit(lv_lit), span: *span, - } + }) } /// At the bottom (top?) of the precedence hierarchy, /// parse things like parenthesized exprs, /// macros, return, etc. - pub fn parse_bottom_expr(&mut self) -> Gc<Expr> { + pub fn parse_bottom_expr(&mut self) -> P<Expr> { maybe_whole_expr!(self); let lo = self.span.lo; @@ -1989,28 +1991,27 @@ impl<'a> Parser<'a> { if self.token == token::RPAREN { hi = self.span.hi; self.bump(); - let lit = box(GC) spanned(lo, hi, LitNil); + let lit = P(spanned(lo, hi, LitNil)); return self.mk_expr(lo, hi, ExprLit(lit)); } let mut es = vec!(self.parse_expr()); - self.commit_expr(*es.last().unwrap(), &[], &[token::COMMA, token::RPAREN]); + self.commit_expr(&**es.last().unwrap(), &[], &[token::COMMA, token::RPAREN]); while self.token == token::COMMA { self.bump(); if self.token != token::RPAREN { es.push(self.parse_expr()); - self.commit_expr(*es.last().unwrap(), &[], &[token::COMMA, token::RPAREN]); - } - else { + self.commit_expr(&**es.last().unwrap(), &[], + &[token::COMMA, token::RPAREN]); + } else { trailing_comma = true; } } hi = self.span.hi; - self.commit_expr_expecting(*es.last().unwrap(), token::RPAREN); + self.commit_expr_expecting(&**es.last().unwrap(), token::RPAREN); return if es.len() == 1 && !trailing_comma { - self.mk_expr(lo, hi, ExprParen(*es.get(0))) - } - else { + self.mk_expr(lo, hi, ExprParen(es.move_iter().nth(0).unwrap())) + } else { self.mk_expr(lo, hi, ExprTup(es)) } }, @@ -2079,14 +2080,14 @@ impl<'a> Parser<'a> { let decl = self.parse_proc_decl(); let body = self.parse_expr(); let fakeblock = P(ast::Block { + id: ast::DUMMY_NODE_ID, view_items: Vec::new(), stmts: Vec::new(), - expr: Some(body), - id: ast::DUMMY_NODE_ID, rules: DefaultBlock, span: body.span, + expr: Some(body), }); - return self.mk_expr(lo, body.span.hi, ExprProc(decl, fakeblock)); + return self.mk_expr(lo, fakeblock.span.hi, ExprProc(decl, fakeblock)); } if self.eat_keyword(keywords::If) { return self.parse_if_expr(); @@ -2200,7 +2201,7 @@ impl<'a> Parser<'a> { } fields.push(self.parse_field()); - self.commit_expr(fields.last().unwrap().expr, + self.commit_expr(&*fields.last().unwrap().expr, &[token::COMMA], &[token::RBRACE]); } @@ -2227,7 +2228,7 @@ impl<'a> Parser<'a> { // other literal expression let lit = self.parse_lit(); hi = lit.span.hi; - ex = ExprLit(box(GC) lit); + ex = ExprLit(P(lit)); } } } @@ -2237,19 +2238,19 @@ impl<'a> Parser<'a> { /// Parse a block or unsafe block pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode) - -> Gc<Expr> { + -> P<Expr> { self.expect(&token::LBRACE); let blk = self.parse_block_tail(lo, blk_mode); return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk)); } /// parse a.b or a(13) or a[4] or just a - pub fn parse_dot_or_call_expr(&mut self) -> Gc<Expr> { + pub fn parse_dot_or_call_expr(&mut self) -> P<Expr> { let b = self.parse_bottom_expr(); self.parse_dot_or_call_expr_with(b) } - pub fn parse_dot_or_call_expr_with(&mut self, e0: Gc<Expr>) -> Gc<Expr> { + pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> { let mut e = e0; let lo = e.span.lo; let mut hi; @@ -2330,7 +2331,7 @@ impl<'a> Parser<'a> { } continue; } - if self.expr_is_complete(e) { break; } + if self.expr_is_complete(&*e) { break; } match self.token { // expr(...) token::LPAREN => { @@ -2351,7 +2352,7 @@ impl<'a> Parser<'a> { self.bump(); let ix = self.parse_expr(); hi = self.span.hi; - self.commit_expr_expecting(ix, token::RBRACKET); + self.commit_expr_expecting(&*ix, token::RBRACKET); let index = self.mk_index(e, ix); e = self.mk_expr(lo, hi, index) } @@ -2556,7 +2557,7 @@ impl<'a> Parser<'a> { } /// Parse a prefix-operator expr - pub fn parse_prefix_expr(&mut self) -> Gc<Expr> { + pub fn parse_prefix_expr(&mut self) -> P<Expr> { let lo = self.span.lo; let hi; @@ -2638,28 +2639,23 @@ impl<'a> Parser<'a> { } /// Parse an expression of binops - pub fn parse_binops(&mut self) -> Gc<Expr> { + pub fn parse_binops(&mut self) -> P<Expr> { let prefix_expr = self.parse_prefix_expr(); self.parse_more_binops(prefix_expr, 0) } /// Parse an expression of binops of at least min_prec precedence - pub fn parse_more_binops(&mut self, lhs: Gc<Expr>, - min_prec: uint) -> Gc<Expr> { - if self.expr_is_complete(lhs) { return lhs; } + pub fn parse_more_binops(&mut self, lhs: P<Expr>, min_prec: uint) -> P<Expr> { + if self.expr_is_complete(&*lhs) { return lhs; } // Prevent dynamic borrow errors later on by limiting the // scope of the borrows. - { - let token: &token::Token = &self.token; - let restriction: &restriction = &self.restriction; - match (token, restriction) { - (&token::BINOP(token::OR), &RESTRICT_NO_BAR_OP) => return lhs, - (&token::BINOP(token::OR), - &RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) => return lhs, - (&token::OROR, &RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) => return lhs, - _ => { } - } + match (&self.token, &self.restriction) { + (&token::BINOP(token::OR), &RESTRICT_NO_BAR_OP) => return lhs, + (&token::BINOP(token::OR), + &RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) => return lhs, + (&token::OROR, &RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) => return lhs, + _ => { } } let cur_opt = token_to_binop(&self.token); @@ -2670,8 +2666,10 @@ impl<'a> Parser<'a> { self.bump(); let expr = self.parse_prefix_expr(); let rhs = self.parse_more_binops(expr, cur_prec); + let lhs_span = lhs.span; + let rhs_span = rhs.span; let binary = self.mk_binary(cur_op, lhs, rhs); - let bin = self.mk_expr(lhs.span.lo, rhs.span.hi, binary); + let bin = self.mk_expr(lhs_span.lo, rhs_span.hi, binary); self.parse_more_binops(bin, min_prec) } else { lhs @@ -2694,7 +2692,7 @@ impl<'a> Parser<'a> { /// Parse an assignment expression.... /// actually, this seems to be the main entry point for /// parsing an arbitrary expression. - pub fn parse_assign_expr(&mut self) -> Gc<Expr> { + pub fn parse_assign_expr(&mut self) -> P<Expr> { let lo = self.span.lo; let lhs = self.parse_binops(); match self.token { @@ -2718,8 +2716,9 @@ impl<'a> Parser<'a> { token::SHL => BiShl, token::SHR => BiShr }; + let rhs_span = rhs.span; let assign_op = self.mk_assign_op(aop, lhs, rhs); - self.mk_expr(lo, rhs.span.hi, assign_op) + self.mk_expr(lo, rhs_span.hi, assign_op) } _ => { lhs @@ -2728,40 +2727,40 @@ impl<'a> Parser<'a> { } /// Parse an 'if' expression ('if' token already eaten) - pub fn parse_if_expr(&mut self) -> Gc<Expr> { + pub fn parse_if_expr(&mut self) -> P<Expr> { let lo = self.last_span.lo; let cond = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL); let thn = self.parse_block(); - let mut els: Option<Gc<Expr>> = None; + let mut els: Option<P<Expr>> = None; let mut hi = thn.span.hi; if self.eat_keyword(keywords::Else) { let elexpr = self.parse_else_expr(); - els = Some(elexpr); hi = elexpr.span.hi; + els = Some(elexpr); } self.mk_expr(lo, hi, ExprIf(cond, thn, els)) } // `|args| expr` pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause) - -> Gc<Expr> { + -> P<Expr> { let lo = self.span.lo; let (decl, optional_unboxed_closure_kind) = self.parse_fn_block_decl(); let body = self.parse_expr(); let fakeblock = P(ast::Block { + id: ast::DUMMY_NODE_ID, view_items: Vec::new(), stmts: Vec::new(), + span: body.span, expr: Some(body), - id: ast::DUMMY_NODE_ID, rules: DefaultBlock, - span: body.span, }); match optional_unboxed_closure_kind { Some(unboxed_closure_kind) => { self.mk_expr(lo, - body.span.hi, + fakeblock.span.hi, ExprUnboxedFn(capture_clause, unboxed_closure_kind, decl, @@ -2769,13 +2768,13 @@ impl<'a> Parser<'a> { } None => { self.mk_expr(lo, - body.span.hi, + fakeblock.span.hi, ExprFnBlock(capture_clause, decl, fakeblock)) } } } - pub fn parse_else_expr(&mut self) -> Gc<Expr> { + pub fn parse_else_expr(&mut self) -> P<Expr> { if self.eat_keyword(keywords::If) { return self.parse_if_expr(); } else { @@ -2785,7 +2784,7 @@ impl<'a> Parser<'a> { } /// Parse a 'for' .. 'in' expression ('for' token already eaten) - pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> Gc<Expr> { + pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> { // Parse: `for <src_pat> in <src_expr> <src_loop_block>` let lo = self.last_span.lo; @@ -2798,7 +2797,7 @@ impl<'a> Parser<'a> { self.mk_expr(lo, hi, ExprForLoop(pat, expr, loop_block, opt_ident)) } - pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> Gc<Expr> { + pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> { let lo = self.last_span.lo; let cond = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL); let body = self.parse_block(); @@ -2806,17 +2805,17 @@ impl<'a> Parser<'a> { return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident)); } - pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> Gc<Expr> { + pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> { let lo = self.last_span.lo; let body = self.parse_block(); let hi = body.span.hi; self.mk_expr(lo, hi, ExprLoop(body, opt_ident)) } - fn parse_match_expr(&mut self) -> Gc<Expr> { + fn parse_match_expr(&mut self) -> P<Expr> { let lo = self.last_span.lo; let discriminant = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL); - self.commit_expr_expecting(discriminant, token::LBRACE); + self.commit_expr_expecting(&*discriminant, token::LBRACE); let mut arms: Vec<Arm> = Vec::new(); while self.token != token::RBRACE { arms.push(self.parse_arm()); @@ -2837,11 +2836,11 @@ impl<'a> Parser<'a> { let expr = self.parse_expr_res(RESTRICT_STMT_EXPR); let require_comma = - !classify::expr_is_simple_block(expr) + !classify::expr_is_simple_block(&*expr) && self.token != token::RBRACE; if require_comma { - self.commit_expr(expr, &[token::COMMA], &[token::RBRACE]); + self.commit_expr(&*expr, &[token::COMMA], &[token::RBRACE]); } else { self.eat(&token::COMMA); } @@ -2855,12 +2854,12 @@ impl<'a> Parser<'a> { } /// Parse an expression - pub fn parse_expr(&mut self) -> Gc<Expr> { + pub fn parse_expr(&mut self) -> P<Expr> { return self.parse_expr_res(UNRESTRICTED); } /// Parse an expression, subject to the given restriction - pub fn parse_expr_res(&mut self, r: restriction) -> Gc<Expr> { + pub fn parse_expr_res(&mut self, r: restriction) -> P<Expr> { let old = self.restriction; self.restriction = r; let e = self.parse_assign_expr(); @@ -2869,7 +2868,7 @@ impl<'a> Parser<'a> { } /// Parse the RHS of a local variable declaration (e.g. '= 14;') - fn parse_initializer(&mut self) -> Option<Gc<Expr>> { + fn parse_initializer(&mut self) -> Option<P<Expr>> { if self.token == token::EQ { self.bump(); Some(self.parse_expr()) @@ -2879,7 +2878,7 @@ impl<'a> Parser<'a> { } /// Parse patterns, separated by '|' s - fn parse_pats(&mut self) -> Vec<Gc<Pat>> { + fn parse_pats(&mut self) -> Vec<P<Pat>> { let mut pats = Vec::new(); loop { pats.push(self.parse_pat()); @@ -2890,7 +2889,7 @@ impl<'a> Parser<'a> { fn parse_pat_vec_elements( &mut self, - ) -> (Vec<Gc<Pat>> , Option<Gc<Pat>>, Vec<Gc<Pat>> ) { + ) -> (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>) { let mut before = Vec::new(); let mut slice = None; let mut after = Vec::new(); @@ -2910,11 +2909,11 @@ impl<'a> Parser<'a> { if self.token == token::COMMA || self.token == token::RBRACKET { - slice = Some(box(GC) ast::Pat { + slice = Some(P(ast::Pat { id: ast::DUMMY_NODE_ID, node: PatWild(PatWildMulti), span: self.span, - }); + })); before_slice = false; } else { let _ = self.parse_pat(); @@ -2989,11 +2988,11 @@ impl<'a> Parser<'a> { self.parse_pat() } else { let fieldpath = codemap::Spanned{span:self.last_span, node: fieldname}; - box(GC) ast::Pat { + P(ast::Pat { id: ast::DUMMY_NODE_ID, node: PatIdent(bind_type, fieldpath, None), span: self.last_span - } + }) }; fields.push(ast::FieldPat { ident: fieldname, pat: subpat }); } @@ -3001,7 +3000,7 @@ impl<'a> Parser<'a> { } /// Parse a pattern. - pub fn parse_pat(&mut self) -> Gc<Pat> { + pub fn parse_pat(&mut self) -> P<Pat> { maybe_whole!(self, NtPat); let lo = self.span.lo; @@ -3013,11 +3012,11 @@ impl<'a> Parser<'a> { self.bump(); pat = PatWild(PatWildSingle); hi = self.last_span.hi; - return box(GC) ast::Pat { + return P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi) - } + }) } token::TILDE => { // parse ~pat @@ -3027,11 +3026,11 @@ impl<'a> Parser<'a> { let last_span = self.last_span; hi = last_span.hi; self.obsolete(last_span, ObsoleteOwnedPattern); - return box(GC) ast::Pat { + return P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi) - } + }) } token::BINOP(token::AND) | token::ANDAND => { // parse &pat @@ -3040,11 +3039,11 @@ impl<'a> Parser<'a> { let sub = self.parse_pat(); pat = PatRegion(sub); hi = self.last_span.hi; - return box(GC) ast::Pat { + return P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi) - } + }) } token::LPAREN => { // parse (pat,pat,pat,...) as tuple @@ -3052,9 +3051,9 @@ impl<'a> Parser<'a> { if self.token == token::RPAREN { hi = self.span.hi; self.bump(); - let lit = box(GC) codemap::Spanned { + let lit = P(codemap::Spanned { node: LitNil, - span: mk_sp(lo, hi)}; + span: mk_sp(lo, hi)}); let expr = self.mk_expr(lo, hi, ExprLit(lit)); pat = PatLit(expr); } else { @@ -3071,11 +3070,11 @@ impl<'a> Parser<'a> { pat = PatTup(fields); } hi = self.last_span.hi; - return box(GC) ast::Pat { + return P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi) - } + }) } token::LBRACKET => { // parse [pat,pat,...] as vector pattern @@ -3086,11 +3085,11 @@ impl<'a> Parser<'a> { self.expect(&token::RBRACKET); pat = ast::PatVec(before, slice, after); hi = self.last_span.hi; - return box(GC) ast::Pat { + return P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi) - } + }) } _ => {} } @@ -3135,11 +3134,11 @@ impl<'a> Parser<'a> { let sub = self.parse_pat(); pat = PatBox(sub); hi = self.last_span.hi; - return box(GC) ast::Pat { + return P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi) - } + }) } else { let can_be_enum_or_struct = self.look_ahead(1, |t| { match *t { @@ -3196,7 +3195,7 @@ impl<'a> Parser<'a> { pat = PatStruct(enum_path, fields, etc); } _ => { - let mut args: Vec<Gc<Pat>> = Vec::new(); + let mut args: Vec<P<Pat>> = Vec::new(); match self.token { token::LPAREN => { let is_dotdot = self.look_ahead(1, |t| { @@ -3251,11 +3250,11 @@ impl<'a> Parser<'a> { } } hi = self.last_span.hi; - box(GC) ast::Pat { + P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: mk_sp(lo, hi), - } + }) } /// Parse ident or ident @ pat @@ -3295,7 +3294,7 @@ impl<'a> Parser<'a> { } /// Parse a local variable declaration - fn parse_local(&mut self) -> Gc<Local> { + fn parse_local(&mut self) -> P<Local> { let lo = self.span.lo; let pat = self.parse_pat(); @@ -3308,21 +3307,21 @@ impl<'a> Parser<'a> { ty = self.parse_ty(true); } let init = self.parse_initializer(); - box(GC) ast::Local { + P(ast::Local { ty: ty, pat: pat, init: init, id: ast::DUMMY_NODE_ID, span: mk_sp(lo, self.last_span.hi), source: LocalLet, - } + }) } /// Parse a "let" stmt - fn parse_let(&mut self) -> Gc<Decl> { + fn parse_let(&mut self) -> P<Decl> { let lo = self.span.lo; let local = self.parse_local(); - box(GC) spanned(lo, self.last_span.hi, DeclLocal(local)) + P(spanned(lo, self.last_span.hi, DeclLocal(local))) } /// Parse a structure field @@ -3345,7 +3344,7 @@ impl<'a> Parser<'a> { /// Parse a statement. may include decl. /// Precondition: any attributes are parsed already - pub fn parse_stmt(&mut self, item_attrs: Vec<Attribute>) -> Gc<Stmt> { + pub fn parse_stmt(&mut self, item_attrs: Vec<Attribute>) -> P<Stmt> { maybe_whole!(self, NtStmt); fn check_expected_item(p: &mut Parser, found_attrs: bool) { @@ -3361,7 +3360,7 @@ impl<'a> Parser<'a> { check_expected_item(self, !item_attrs.is_empty()); self.expect_keyword(keywords::Let); let decl = self.parse_let(); - return box(GC) spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)); + P(spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID))) } else if is_ident(&self.token) && !token::is_any_keyword(&self.token) && self.look_ahead(1, |t| *t == token::NOT) { @@ -3409,17 +3408,17 @@ impl<'a> Parser<'a> { let hi = self.span.hi; if id.name == token::special_idents::invalid.name { - return box(GC) spanned(lo, hi, StmtMac( - spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)); + P(spanned(lo, hi, StmtMac( + spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false))) } else { // if it has a special ident, it's definitely an item - return box(GC) spanned(lo, hi, StmtDecl( - box(GC) spanned(lo, hi, DeclItem( + P(spanned(lo, hi, StmtDecl( + P(spanned(lo, hi, DeclItem( self.mk_item( lo, hi, id /*id is good here*/, ItemMac(spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT))), - Inherited, Vec::new(/*no attrs*/)))), - ast::DUMMY_NODE_ID)); + Inherited, Vec::new(/*no attrs*/))))), + ast::DUMMY_NODE_ID))) } } else { @@ -3427,8 +3426,8 @@ impl<'a> Parser<'a> { match self.parse_item_or_view_item(item_attrs, false) { IoviItem(i) => { let hi = i.span.hi; - let decl = box(GC) spanned(lo, hi, DeclItem(i)); - return box(GC) spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID)); + let decl = P(spanned(lo, hi, DeclItem(i))); + P(spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID))) } IoviViewItem(vi) => { self.span_fatal(vi.span, @@ -3437,21 +3436,21 @@ impl<'a> Parser<'a> { IoviForeignItem(_) => { self.fatal("foreign items are not allowed here"); } - IoviNone(_) => { /* fallthrough */ } - } + IoviNone(_) => { + check_expected_item(self, found_attrs); - check_expected_item(self, found_attrs); - - // Remainder are line-expr stmts. - let e = self.parse_expr_res(RESTRICT_STMT_EXPR); - return box(GC) spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)); + // Remainder are line-expr stmts. + let e = self.parse_expr_res(RESTRICT_STMT_EXPR); + P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID))) + } + } } } /// Is this expression a successfully-parsed statement? - fn expr_is_complete(&mut self, e: Gc<Expr>) -> bool { - return self.restriction == RESTRICT_STMT_EXPR && - !classify::expr_requires_semi_to_be_stmt(e); + fn expr_is_complete(&mut self, e: &Expr) -> bool { + self.restriction == RESTRICT_STMT_EXPR && + !classify::expr_requires_semi_to_be_stmt(e) } /// Parse a block. No inner attrs are allowed. @@ -3500,10 +3499,10 @@ impl<'a> Parser<'a> { } = self.parse_items_and_view_items(first_item_attrs, false, false); - for item in items.iter() { - let decl = box(GC) spanned(item.span.lo, item.span.hi, DeclItem(*item)); - stmts.push(box(GC) spanned(item.span.lo, item.span.hi, - StmtDecl(decl, ast::DUMMY_NODE_ID))); + for item in items.move_iter() { + let span = item.span; + let decl = P(spanned(span.lo, span.hi, DeclItem(item))); + stmts.push(P(spanned(span.lo, span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))); } let mut attributes_box = attrs_remaining; @@ -3527,66 +3526,75 @@ impl<'a> Parser<'a> { _ => { let stmt = self.parse_stmt(attributes_box); attributes_box = Vec::new(); - match stmt.node { + stmt.and_then(|Spanned {node, span}| match node { StmtExpr(e, stmt_id) => { // expression without semicolon - if classify::stmt_ends_with_semi(&*stmt) { + if classify::expr_requires_semi_to_be_stmt(&*e) { // Just check for errors and recover; do not eat semicolon yet. - self.commit_stmt(stmt, &[], &[token::SEMI, token::RBRACE]); + self.commit_stmt(&[], &[token::SEMI, token::RBRACE]); } match self.token { token::SEMI => { self.bump(); let span_with_semi = Span { - lo: stmt.span.lo, + lo: span.lo, hi: self.last_span.hi, - expn_info: stmt.span.expn_info, + expn_info: span.expn_info, }; - stmts.push(box(GC) codemap::Spanned { + stmts.push(P(Spanned { node: StmtSemi(e, stmt_id), span: span_with_semi, - }); + })); } token::RBRACE => { expr = Some(e); } _ => { - stmts.push(stmt); + stmts.push(P(Spanned { + node: StmtExpr(e, stmt_id), + span: span + })); } } } - StmtMac(ref m, _) => { + StmtMac(m, semi) => { // statement macro; might be an expr match self.token { token::SEMI => { + stmts.push(P(Spanned { + node: StmtMac(m, true), + span: span, + })); self.bump(); - stmts.push(box(GC) codemap::Spanned { - node: StmtMac((*m).clone(), true), - span: stmt.span, - }); } token::RBRACE => { // if a block ends in `m!(arg)` without // a `;`, it must be an expr expr = Some( - self.mk_mac_expr(stmt.span.lo, - stmt.span.hi, - m.node.clone())); + self.mk_mac_expr(span.lo, + span.hi, + m.node)); } _ => { - stmts.push(stmt); + stmts.push(P(Spanned { + node: StmtMac(m, semi), + span: span + })); } } } _ => { // all other kinds of statements: - stmts.push(stmt.clone()); - - if classify::stmt_ends_with_semi(&*stmt) { - self.commit_stmt_expecting(stmt, token::SEMI); + if classify::stmt_ends_with_semi(&node) { + self.commit_stmt_expecting(token::SEMI); } + + stmts.push(P(Spanned { + node: node, + span: span + })); } - } + }) } } } @@ -4187,15 +4195,15 @@ impl<'a> Parser<'a> { fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident, node: Item_, vis: Visibility, - attrs: Vec<Attribute>) -> Gc<Item> { - box(GC) Item { + attrs: Vec<Attribute>) -> P<Item> { + P(Item { ident: ident, attrs: attrs, id: ast::DUMMY_NODE_ID, node: node, vis: vis, span: mk_sp(lo, hi) - } + }) } /// Parse an item-position function declaration. @@ -4210,7 +4218,7 @@ impl<'a> Parser<'a> { /// Parse a method in a trait impl, starting with `attrs` attributes. pub fn parse_method(&mut self, already_parsed_attrs: Option<Vec<Attribute>>) - -> Gc<Method> { + -> P<Method> { let next_attrs = self.parse_outer_attributes(); let attrs = match already_parsed_attrs { Some(mut a) => { a.push_all_move(next_attrs); a } @@ -4264,6 +4272,7 @@ impl<'a> Parser<'a> { }); self.parse_where_clause(&mut generics); let (inner_attrs, body) = self.parse_inner_attrs_and_block(); + let body_span = body.span; let new_attrs = attrs.append(inner_attrs.as_slice()); (ast::MethDecl(ident, generics, @@ -4273,15 +4282,15 @@ impl<'a> Parser<'a> { decl, body, visa), - body.span.hi, new_attrs) + body_span.hi, new_attrs) } }; - box(GC) ast::Method { + P(ast::Method { attrs: new_attrs, id: ast::DUMMY_NODE_ID, span: mk_sp(lo, hi), node: method_, - } + }) } /// Parse trait Foo { ... } @@ -4444,12 +4453,12 @@ impl<'a> Parser<'a> { let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug. let new_id = ast::DUMMY_NODE_ID; (class_name, - ItemStruct(box(GC) ast::StructDef { + ItemStruct(P(ast::StructDef { fields: fields, ctor_id: if is_tuple_like { Some(new_id) } else { None }, super_struct: super_struct, is_virtual: is_virtual, - }, generics), + }), generics), None) } @@ -4524,7 +4533,7 @@ impl<'a> Parser<'a> { items: starting_items, .. } = self.parse_items_and_view_items(first_item_attrs, true, true); - let mut items: Vec<Gc<Item>> = starting_items; + let mut items: Vec<P<Item>> = starting_items; let attrs_remaining_len = attrs_remaining.len(); // don't think this other loop is even necessary.... @@ -4574,7 +4583,7 @@ impl<'a> Parser<'a> { let ty = self.parse_ty(true); self.expect(&token::EQ); let e = self.parse_expr(); - self.commit_expr_expecting(e, token::SEMI); + self.commit_expr_expecting(&*e, token::SEMI); (id, ItemStatic(ty, m, e), None) } @@ -4726,7 +4735,7 @@ impl<'a> Parser<'a> { /// Parse a function declaration from a foreign module fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, - attrs: Vec<Attribute>) -> Gc<ForeignItem> { + attrs: Vec<Attribute>) -> P<ForeignItem> { let lo = self.span.lo; self.expect_keyword(keywords::Fn); @@ -4735,17 +4744,19 @@ impl<'a> Parser<'a> { self.parse_where_clause(&mut generics); let hi = self.span.hi; self.expect(&token::SEMI); - box(GC) ast::ForeignItem { ident: ident, - attrs: attrs, - node: ForeignItemFn(decl, generics), - id: ast::DUMMY_NODE_ID, - span: mk_sp(lo, hi), - vis: vis } + P(ast::ForeignItem { + ident: ident, + attrs: attrs, + node: ForeignItemFn(decl, generics), + id: ast::DUMMY_NODE_ID, + span: mk_sp(lo, hi), + vis: vis + }) } /// Parse a static item from a foreign module fn parse_item_foreign_static(&mut self, vis: ast::Visibility, - attrs: Vec<Attribute> ) -> Gc<ForeignItem> { + attrs: Vec<Attribute>) -> P<ForeignItem> { let lo = self.span.lo; self.expect_keyword(keywords::Static); @@ -4756,14 +4767,14 @@ impl<'a> Parser<'a> { let ty = self.parse_ty(true); let hi = self.span.hi; self.expect(&token::SEMI); - box(GC) ast::ForeignItem { + P(ForeignItem { ident: ident, attrs: attrs, node: ForeignItemStatic(ty, mutbl), id: ast::DUMMY_NODE_ID, span: mk_sp(lo, hi), - vis: vis, - } + vis: vis + }) } /// Parse safe/unsafe and fn @@ -4903,19 +4914,19 @@ impl<'a> Parser<'a> { /// Parse a structure-like enum variant definition /// this should probably be renamed or refactored... - fn parse_struct_def(&mut self) -> Gc<StructDef> { + fn parse_struct_def(&mut self) -> P<StructDef> { let mut fields: Vec<StructField> = Vec::new(); while self.token != token::RBRACE { fields.push(self.parse_struct_decl_field()); } self.bump(); - return box(GC) ast::StructDef { + P(StructDef { fields: fields, ctor_id: None, super_struct: None, is_virtual: false, - }; + }) } /// Parse the part of an "enum" decl following the '{' @@ -5034,16 +5045,21 @@ impl<'a> Parser<'a> { attrs: Vec<Attribute> , macros_allowed: bool) -> ItemOrViewItem { - match self.token { - INTERPOLATED(token::NtItem(item)) => { + let nt_item = match self.token { + INTERPOLATED(token::NtItem(ref item)) => { + Some((**item).clone()) + } + _ => None + }; + match nt_item { + Some(mut item) => { self.bump(); - let new_attrs = attrs.append(item.attrs.as_slice()); - return IoviItem(box(GC) Item { - attrs: new_attrs, - ..(*item).clone() - }); + let mut attrs = attrs; + mem::swap(&mut item.attrs, &mut attrs); + item.attrs.extend(attrs.move_iter()); + return IoviItem(P(item)); } - _ => {} + None => {} } let lo = self.span.lo; @@ -5328,12 +5344,12 @@ impl<'a> Parser<'a> { return IoviNone(attrs); } - pub fn parse_item_with_outer_attributes(&mut self) -> Option<Gc<Item>> { + pub fn parse_item_with_outer_attributes(&mut self) -> Option<P<Item>> { let attrs = self.parse_outer_attributes(); self.parse_item(attrs) } - pub fn parse_item(&mut self, attrs: Vec<Attribute> ) -> Option<Gc<Item>> { + pub fn parse_item(&mut self, attrs: Vec<Attribute>) -> Option<P<Item>> { match self.parse_item_or_view_item(attrs, true) { IoviNone(_) => None, IoviViewItem(_) => @@ -5355,7 +5371,7 @@ impl<'a> Parser<'a> { /// | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE /// | MOD? non_global_path MOD_SEP STAR /// | MOD? non_global_path - fn parse_view_path(&mut self) -> Gc<ViewPath> { + fn parse_view_path(&mut self) -> P<ViewPath> { let lo = self.span.lo; if self.token == token::LBRACE { @@ -5369,8 +5385,8 @@ impl<'a> Parser<'a> { global: false, segments: Vec::new() }; - return box(GC) spanned(lo, self.span.hi, - ViewPathList(path, idents, ast::DUMMY_NODE_ID)); + return P(spanned(lo, self.span.hi, + ViewPathList(path, idents, ast::DUMMY_NODE_ID))); } let first_ident = self.parse_ident(); @@ -5399,9 +5415,9 @@ impl<'a> Parser<'a> { } }).collect() }; - return box(GC) spanned(lo, self.span.hi, - ViewPathSimple(first_ident, path, - ast::DUMMY_NODE_ID)); + return P(spanned(lo, self.span.hi, + ViewPathSimple(first_ident, path, + ast::DUMMY_NODE_ID))); } token::MOD_SEP => { @@ -5434,8 +5450,8 @@ impl<'a> Parser<'a> { } }).collect() }; - return box(GC) spanned(lo, self.span.hi, - ViewPathList(path, idents, ast::DUMMY_NODE_ID)); + return P(spanned(lo, self.span.hi, + ViewPathList(path, idents, ast::DUMMY_NODE_ID))); } // foo::bar::* @@ -5452,8 +5468,8 @@ impl<'a> Parser<'a> { } }).collect() }; - return box(GC) spanned(lo, self.span.hi, - ViewPathGlob(path, ast::DUMMY_NODE_ID)); + return P(spanned(lo, self.span.hi, + ViewPathGlob(path, ast::DUMMY_NODE_ID))); } _ => break @@ -5477,9 +5493,8 @@ impl<'a> Parser<'a> { if self.eat_keyword(keywords::As) { rename_to = self.parse_ident() } - return box(GC) spanned(lo, - self.last_span.hi, - ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID)); + P(spanned(lo, self.last_span.hi, + ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID))) } /// Parses a sequence of items. Stops when it finds program diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index cce14be1ba5..f113e0e6cff 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -9,15 +9,15 @@ // except according to those terms. use ast; -use ast::{P, Ident, Name, Mrk}; +use ast::{Ident, Name, Mrk}; use ext::mtwt; use parse::token; +use ptr::P; use util::interner::{RcStr, StrInterner}; use util::interner; use serialize::{Decodable, Decoder, Encodable, Encoder}; use std::fmt; -use std::gc::Gc; use std::mem; use std::path::BytesContainer; use std::rc::Rc; @@ -115,19 +115,19 @@ pub enum Token { #[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash)] /// For interpolation during macro expansion. pub enum Nonterminal { - NtItem(Gc<ast::Item>), + NtItem( P<ast::Item>), NtBlock(P<ast::Block>), - NtStmt(Gc<ast::Stmt>), - NtPat( Gc<ast::Pat>), - NtExpr(Gc<ast::Expr>), - NtTy( P<ast::Ty>), + NtStmt( P<ast::Stmt>), + NtPat( P<ast::Pat>), + NtExpr( P<ast::Expr>), + NtTy( P<ast::Ty>), /// See IDENT, above, for meaning of bool in NtIdent: NtIdent(Box<Ident>, bool), /// Stuff inside brackets for attributes - NtMeta(Gc<ast::MetaItem>), + NtMeta( P<ast::MetaItem>), NtPath(Box<ast::Path>), - NtTT( Gc<ast::TokenTree>), // needs Gc'd to break a circularity - NtMatchers(Vec<ast::Matcher> ) + NtTT( P<ast::TokenTree>), // needs P'ed to break a circularity + NtMatchers(Vec<ast::Matcher>) } impl fmt::Show for Nonterminal { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a4dff45ad35..4d46da8d835 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -10,7 +10,7 @@ use abi; use ast::{FnMutUnboxedClosureKind, FnOnceUnboxedClosureKind}; -use ast::{FnUnboxedClosureKind, MethodImplItem, P}; +use ast::{FnUnboxedClosureKind, MethodImplItem}; use ast::{RegionTyParamBound, TraitTyParamBound, UnboxedClosureKind}; use ast::{UnboxedFnTyParamBound, RequiredMethod, ProvidedMethod}; use ast; @@ -26,8 +26,8 @@ use parse; use print::pp::{break_offset, word, space, zerobreak, hardbreak}; use print::pp::{Breaks, Consistent, Inconsistent, eof}; use print::pp; +use ptr::P; -use std::gc::Gc; use std::io::{IoResult, MemWriter}; use std::io; use std::mem; @@ -246,7 +246,7 @@ pub fn ident_to_string(id: &ast::Ident) -> String { } pub fn fun_to_string(decl: &ast::FnDecl, fn_style: ast::FnStyle, name: ast::Ident, - opt_explicit_self: Option<ast::ExplicitSelf_>, + opt_explicit_self: Option<&ast::ExplicitSelf_>, generics: &ast::Generics) -> String { $to_string(|s| { try!(s.print_fn(decl, Some(fn_style), abi::Rust, @@ -278,7 +278,7 @@ pub fn lit_to_string(l: &ast::Lit) -> String { $to_string(|s| s.print_literal(l)) } -pub fn explicit_self_to_string(explicit_self: ast::ExplicitSelf_) -> String { +pub fn explicit_self_to_string(explicit_self: &ast::ExplicitSelf_) -> String { $to_string(|s| s.print_explicit_self(explicit_self, ast::MutImmutable).map(|_| {})) } @@ -502,7 +502,7 @@ impl<'a> State<'a> { } pub fn commasep_exprs(&mut self, b: Breaks, - exprs: &[Gc<ast::Expr>]) -> IoResult<()> { + exprs: &[P<ast::Expr>]) -> IoResult<()> { self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span) } @@ -574,7 +574,7 @@ impl<'a> State<'a> { ast::TyTup(ref elts) => { try!(self.popen()); try!(self.commasep(Inconsistent, elts.as_slice(), - |s, ty| s.print_type_ref(ty))); + |s, ty| s.print_type(&**ty))); if elts.len() == 1 { try!(word(&mut self.s, ",")); } @@ -585,7 +585,7 @@ impl<'a> State<'a> { try!(self.print_type(&**typ)); try!(self.pclose()); } - ast::TyBareFn(f) => { + ast::TyBareFn(ref f) => { let generics = ast::Generics { lifetimes: f.lifetimes.clone(), ty_params: OwnedSlice::empty(), @@ -605,7 +605,7 @@ impl<'a> State<'a> { None, None)); } - ast::TyClosure(f) => { + ast::TyClosure(ref f) => { let generics = ast::Generics { lifetimes: f.lifetimes.clone(), ty_params: OwnedSlice::empty(), @@ -645,7 +645,7 @@ impl<'a> State<'a> { None, None)); } - ast::TyUnboxedFn(f) => { + ast::TyUnboxedFn(ref f) => { try!(self.print_ty_fn(None, None, ast::NormalFn, @@ -679,10 +679,6 @@ impl<'a> State<'a> { self.end() } - pub fn print_type_ref(&mut self, ty: &P<ast::Ty>) -> IoResult<()> { - self.print_type(&**ty) - } - pub fn print_foreign_item(&mut self, item: &ast::ForeignItem) -> IoResult<()> { try!(self.hardbreak_if_not_bol()); @@ -794,10 +790,8 @@ impl<'a> State<'a> { if struct_def.is_virtual { try!(self.word_space("virtual")); } - try!(self.head(visibility_qualified(item.vis, - "struct").as_slice())); - try!(self.print_struct(&**struct_def, generics, item.ident, - item.span)); + try!(self.head(visibility_qualified(item.vis,"struct").as_slice())); + try!(self.print_struct(&**struct_def, generics, item.ident, item.span)); } ast::ItemImpl(ref generics, @@ -828,8 +822,8 @@ impl<'a> State<'a> { try!(self.print_inner_attributes(item.attrs.as_slice())); for impl_item in impl_items.iter() { match *impl_item { - ast::MethodImplItem(meth) => { - try!(self.print_method(&*meth)); + ast::MethodImplItem(ref meth) => { + try!(self.print_method(&**meth)); } } } @@ -1068,7 +1062,7 @@ impl<'a> State<'a> { Some(m.ident), &OwnedSlice::empty(), Some(&m.generics), - Some(m.explicit_self.node), + Some(&m.explicit_self.node), None)); word(&mut self.s, ";") } @@ -1097,18 +1091,18 @@ impl<'a> State<'a> { abi, ref explicit_self, fn_style, - decl, - body, + ref decl, + ref body, vis) => { - try!(self.print_fn(&*decl, + try!(self.print_fn(&**decl, Some(fn_style), abi, ident, generics, - Some(explicit_self.node), + Some(&explicit_self.node), vis)); try!(word(&mut self.s, " ")); - self.print_block_with_attrs(&*body, meth.attrs.as_slice()) + self.print_block_with_attrs(&**body, meth.attrs.as_slice()) }, ast::MethMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), ..}) => { @@ -1199,7 +1193,7 @@ impl<'a> State<'a> { } } } - if parse::classify::stmt_ends_with_semi(st) { + if parse::classify::stmt_ends_with_semi(&st.node) { try!(word(&mut self.s, ";")); } self.maybe_print_trailing_comment(st.span, None) @@ -1257,19 +1251,19 @@ impl<'a> State<'a> { self.ann.post(self, NodeBlock(blk)) } - fn print_else(&mut self, els: Option<Gc<ast::Expr>>) -> IoResult<()> { + fn print_else(&mut self, els: Option<&ast::Expr>) -> IoResult<()> { match els { Some(_else) => { match _else.node { // "another else-if" - ast::ExprIf(ref i, ref t, e) => { + ast::ExprIf(ref i, ref then, ref e) => { try!(self.cbox(indent_unit - 1u)); try!(self.ibox(0u)); try!(word(&mut self.s, " else if ")); try!(self.print_expr(&**i)); try!(space(&mut self.s)); - try!(self.print_block(&**t)); - self.print_else(e) + try!(self.print_block(&**then)); + self.print_else(e.as_ref().map(|e| &**e)) } // "final else" ast::ExprBlock(ref b) => { @@ -1289,7 +1283,7 @@ impl<'a> State<'a> { } pub fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, - elseopt: Option<Gc<ast::Expr>>, chk: bool) -> IoResult<()> { + elseopt: Option<&ast::Expr>, chk: bool) -> IoResult<()> { try!(self.head("if")); if chk { try!(self.word_nbsp("check")); } try!(self.print_expr(test)); @@ -1312,7 +1306,7 @@ impl<'a> State<'a> { } - fn print_call_post(&mut self, args: &[Gc<ast::Expr>]) -> IoResult<()> { + fn print_call_post(&mut self, args: &[P<ast::Expr>]) -> IoResult<()> { try!(self.popen()); try!(self.commasep_exprs(Inconsistent, args)); self.pclose() @@ -1361,7 +1355,7 @@ impl<'a> State<'a> { try!(self.end()); } - ast::ExprStruct(ref path, ref fields, wth) => { + ast::ExprStruct(ref path, ref fields, ref wth) => { try!(self.print_path(path, true)); try!(word(&mut self.s, "{")); try!(self.commasep_cmnt( @@ -1375,7 +1369,7 @@ impl<'a> State<'a> { s.end() }, |f| f.span)); - match wth { + match *wth { Some(ref expr) => { try!(self.ibox(indent_unit)); if !fields.is_empty() { @@ -1410,7 +1404,7 @@ impl<'a> State<'a> { if tys.len() > 0u { try!(word(&mut self.s, "::<")); try!(self.commasep(Inconsistent, tys.as_slice(), - |s, ty| s.print_type_ref(ty))); + |s, ty| s.print_type(&**ty))); try!(word(&mut self.s, ">")); } try!(self.print_call_post(base_args)); @@ -1437,8 +1431,8 @@ impl<'a> State<'a> { try!(self.word_space("as")); try!(self.print_type(&**ty)); } - ast::ExprIf(ref test, ref blk, elseopt) => { - try!(self.print_if(&**test, &**blk, elseopt, false)); + ast::ExprIf(ref test, ref blk, ref elseopt) => { + try!(self.print_if(&**test, &**blk, elseopt.as_ref().map(|e| &**e), false)); } ast::ExprWhile(ref test, ref blk, opt_ident) => { for ident in opt_ident.iter() { @@ -1500,13 +1494,13 @@ impl<'a> State<'a> { try!(self.print_block_unclosed(&**body)); } else { // we extract the block, so as not to create another set of boxes - match body.expr.unwrap().node { - ast::ExprBlock(blk) => { - try!(self.print_block_unclosed(&*blk)); + match body.expr.as_ref().unwrap().node { + ast::ExprBlock(ref blk) => { + try!(self.print_block_unclosed(&**blk)); } _ => { // this is a bare expression - try!(self.print_expr(&*body.expr.unwrap())); + try!(self.print_expr(&**body.expr.as_ref().unwrap())); try!(self.end()); // need to close a box } } @@ -1532,13 +1526,13 @@ impl<'a> State<'a> { try!(self.print_block_unclosed(&**body)); } else { // we extract the block, so as not to create another set of boxes - match body.expr.unwrap().node { + match body.expr.as_ref().unwrap().node { ast::ExprBlock(ref blk) => { try!(self.print_block_unclosed(&**blk)); } _ => { // this is a bare expression - try!(self.print_expr(&*body.expr.unwrap())); + try!(self.print_expr(body.expr.as_ref().map(|e| &**e).unwrap())); try!(self.end()); // need to close a box } } @@ -1560,13 +1554,13 @@ impl<'a> State<'a> { assert!(body.stmts.is_empty()); assert!(body.expr.is_some()); // we extract the block, so as not to create another set of boxes - match body.expr.unwrap().node { + match body.expr.as_ref().unwrap().node { ast::ExprBlock(ref blk) => { try!(self.print_block_unclosed(&**blk)); } _ => { // this is a bare expression - try!(self.print_expr(&*body.expr.unwrap())); + try!(self.print_expr(body.expr.as_ref().map(|e| &**e).unwrap())); try!(self.end()); // need to close a box } } @@ -1603,7 +1597,7 @@ impl<'a> State<'a> { try!(word(&mut self.s, "::<")); try!(self.commasep( Inconsistent, tys.as_slice(), - |s, ty| s.print_type_ref(ty))); + |s, ty| s.print_type(&**ty))); try!(word(&mut self.s, ">")); } } @@ -1615,7 +1609,7 @@ impl<'a> State<'a> { try!(word(&mut self.s, "::<")); try!(self.commasep( Inconsistent, tys.as_slice(), - |s, ty| s.print_type_ref(ty))); + |s, ty| s.print_type(&**ty))); try!(word(&mut self.s, ">")); } } @@ -1809,7 +1803,7 @@ impl<'a> State<'a> { try!(self.commasep( Inconsistent, segment.types.as_slice(), - |s, ty| s.print_type_ref(ty))); + |s, ty| s.print_type(&**ty))); } try!(word(&mut self.s, ">")) @@ -1841,7 +1835,7 @@ impl<'a> State<'a> { match pat.node { ast::PatWild(ast::PatWildSingle) => try!(word(&mut self.s, "_")), ast::PatWild(ast::PatWildMulti) => try!(word(&mut self.s, "..")), - ast::PatIdent(binding_mode, ref path1, sub) => { + ast::PatIdent(binding_mode, ref path1, ref sub) => { match binding_mode { ast::BindByRef(mutbl) => { try!(self.word_nbsp("ref")); @@ -1853,7 +1847,7 @@ impl<'a> State<'a> { } } try!(self.print_ident(path1.node)); - match sub { + match *sub { Some(ref p) => { try!(word(&mut self.s, "@")); try!(self.print_pat(&**p)); @@ -1921,7 +1915,7 @@ impl<'a> State<'a> { try!(word(&mut self.s, "..")); try!(self.print_expr(&**end)); } - ast::PatVec(ref before, slice, ref after) => { + ast::PatVec(ref before, ref slice, ref after) => { try!(word(&mut self.s, "[")); try!(self.commasep(Inconsistent, before.as_slice(), @@ -1994,10 +1988,10 @@ impl<'a> State<'a> { // Returns whether it printed anything fn print_explicit_self(&mut self, - explicit_self: ast::ExplicitSelf_, + explicit_self: &ast::ExplicitSelf_, mutbl: ast::Mutability) -> IoResult<bool> { try!(self.print_mutability(mutbl)); - match explicit_self { + match *explicit_self { ast::SelfStatic => { return Ok(false); } ast::SelfValue(_) => { try!(word(&mut self.s, "self")); @@ -2023,7 +2017,7 @@ impl<'a> State<'a> { abi: abi::Abi, name: ast::Ident, generics: &ast::Generics, - opt_explicit_self: Option<ast::ExplicitSelf_>, + opt_explicit_self: Option<&ast::ExplicitSelf_>, vis: ast::Visibility) -> IoResult<()> { try!(self.head("")); try!(self.print_fn_header_info(opt_explicit_self, fn_style, abi, vis)); @@ -2035,7 +2029,7 @@ impl<'a> State<'a> { } pub fn print_fn_args(&mut self, decl: &ast::FnDecl, - opt_explicit_self: Option<ast::ExplicitSelf_>) + opt_explicit_self: Option<&ast::ExplicitSelf_>) -> IoResult<()> { // It is unfortunate to duplicate the commasep logic, but we want the // self type and the args all in the same box. @@ -2043,7 +2037,7 @@ impl<'a> State<'a> { let mut first = true; for &explicit_self in opt_explicit_self.iter() { let m = match explicit_self { - ast::SelfStatic => ast::MutImmutable, + &ast::SelfStatic => ast::MutImmutable, _ => match decl.inputs.get(0).pat.node { ast::PatIdent(ast::BindByValue(m), _, _) => m, _ => ast::MutImmutable @@ -2068,7 +2062,7 @@ impl<'a> State<'a> { } pub fn print_fn_args_and_ret(&mut self, decl: &ast::FnDecl, - opt_explicit_self: Option<ast::ExplicitSelf_>) + opt_explicit_self: Option<&ast::ExplicitSelf_>) -> IoResult<()> { try!(self.popen()); try!(self.print_fn_args(decl, opt_explicit_self)); @@ -2413,7 +2407,7 @@ impl<'a> State<'a> { id: Option<ast::Ident>, bounds: &OwnedSlice<ast::TyParamBound>, generics: Option<&ast::Generics>, - opt_explicit_self: Option<ast::ExplicitSelf_>, + opt_explicit_self: Option<&ast::ExplicitSelf_>, opt_unboxed_closure_kind: Option<ast::UnboxedClosureKind>) -> IoResult<()> { @@ -2754,7 +2748,7 @@ impl<'a> State<'a> { } pub fn print_fn_header_info(&mut self, - _opt_explicit_self: Option<ast::ExplicitSelf_>, + _opt_explicit_self: Option<&ast::ExplicitSelf_>, opt_fn_style: Option<ast::FnStyle>, abi: abi::Abi, vis: ast::Visibility) -> IoResult<()> { diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs index 7b96cf3c60d..bc5b9bc6c18 100644 --- a/src/libsyntax/util/parser_testing.rs +++ b/src/libsyntax/util/parser_testing.rs @@ -9,6 +9,7 @@ // except according to those terms. use ast; +use ast::P; use parse::{new_parse_sess}; use parse::{ParseSess,string_to_filemap,filemap_to_tts}; use parse::{new_parser_from_source_str}; @@ -48,21 +49,21 @@ pub fn string_to_crate (source_str : String) -> ast::Crate { } /// Parse a string, return an expr -pub fn string_to_expr (source_str : String) -> Gc<ast::Expr> { +pub fn string_to_expr (source_str : String) -> P<ast::Expr> { with_error_checking_parse(source_str, |p| { p.parse_expr() }) } /// Parse a string, return an item -pub fn string_to_item (source_str : String) -> Option<Gc<ast::Item>> { +pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> { with_error_checking_parse(source_str, |p| { p.parse_item(Vec::new()) }) } /// Parse a string, return a stmt -pub fn string_to_stmt(source_str : String) -> Gc<ast::Stmt> { +pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> { with_error_checking_parse(source_str, |p| { p.parse_stmt(Vec::new()) }) @@ -70,7 +71,7 @@ pub fn string_to_stmt(source_str : String) -> Gc<ast::Stmt> { /// Parse a string, return a pat. Uses "irrefutable"... which doesn't /// (currently) affect parsing. -pub fn string_to_pat(source_str: String) -> Gc<ast::Pat> { +pub fn string_to_pat(source_str: String) -> P<ast::Pat> { string_to_parser(&new_parse_sess(), source_str).parse_pat() } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 2a989e6d63a..30a38e28729 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -27,10 +27,9 @@ use abi::Abi; use ast::*; use ast; use codemap::Span; +use ptr::P; use owned_slice::OwnedSlice; -use std::gc::Gc; - pub enum FnKind<'a> { /// fn foo() or extern "Abi" fn foo() FkItemFn(Ident, &'a Generics, FnStyle, Abi), @@ -121,16 +120,8 @@ pub fn walk_inlined_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v InlinedI match *item { IIItem(ref i) => visitor.visit_item(&**i), IIForeign(ref i) => visitor.visit_foreign_item(&**i), - IITraitItem(_, ref iti) => { - match *iti { - ProvidedInlinedTraitItem(ref m) => { - walk_method_helper(visitor, &**m) - } - RequiredInlinedTraitItem(ref m) => { - walk_method_helper(visitor, &**m) - } - } - } + IITraitItem(_, ref ti) => visitor.visit_trait_item(ti), + IIImplItem(_, MethodImplItem(ref m)) => walk_method_helper(visitor, &**m) } } @@ -644,14 +635,14 @@ pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) { } pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V, - optional_expression: &'v Option<Gc<Expr>>) { + optional_expression: &'v Option<P<Expr>>) { match *optional_expression { None => {} Some(ref expression) => visitor.visit_expr(&**expression), } } -pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [Gc<Expr>]) { +pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) { for expression in expressions.iter() { visitor.visit_expr(&**expression) } |
