]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/build.rs
auto merge of #16897 : japaric/rust/mut-slice-collection, r=alexcrichton
[rust.git] / src / libsyntax / ext / build.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use abi;
12 use ast::{P, Ident, Generics, NodeId, Expr};
13 use ast;
14 use ast_util;
15 use attr;
16 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
17 use ext::base::ExtCtxt;
18 use fold::Folder;
19 use owned_slice::OwnedSlice;
20 use parse::token::special_idents;
21 use parse::token::InternedString;
22 use parse::token;
23
24 use std::gc::{Gc, GC};
25
26 // Transitional reexports so qquote can find the paths it is looking for
27 mod syntax {
28     pub use ext;
29     pub use parse;
30 }
31
32 pub trait AstBuilder {
33     // paths
34     fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
35     fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
36     fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
37     fn path_all(&self, sp: Span,
38                 global: bool,
39                 idents: Vec<ast::Ident> ,
40                 lifetimes: Vec<ast::Lifetime>,
41                 types: Vec<P<ast::Ty>> )
42         -> ast::Path;
43
44     // types
45     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
46
47     fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty>;
48     fn ty_path(&self, ast::Path, Option<OwnedSlice<ast::TyParamBound>>) -> P<ast::Ty>;
49     fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
50
51     fn ty_rptr(&self, span: Span,
52                ty: P<ast::Ty>,
53                lifetime: Option<ast::Lifetime>,
54                mutbl: ast::Mutability) -> P<ast::Ty>;
55     fn ty_ptr(&self, span: Span,
56               ty: P<ast::Ty>,
57               mutbl: ast::Mutability) -> P<ast::Ty>;
58     fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty>;
59
60     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
61     fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
62     fn ty_nil(&self) -> P<ast::Ty>;
63
64     fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
65     fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
66     fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField;
67     fn strip_bounds(&self, bounds: &Generics) -> Generics;
68
69     fn typaram(&self,
70                span: Span,
71                id: ast::Ident,
72                bounds: OwnedSlice<ast::TyParamBound>,
73                unbound: Option<ast::TyParamBound>,
74                default: Option<P<ast::Ty>>) -> ast::TyParam;
75
76     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
77     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
78     fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
79     fn lifetime_def(&self,
80                     span: Span,
81                     name: ast::Name,
82                     bounds: Vec<ast::Lifetime>)
83                     -> ast::LifetimeDef;
84
85     // statements
86     fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt>;
87     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
88                 ex: Gc<ast::Expr>) -> Gc<ast::Stmt>;
89     fn stmt_let_typed(&self,
90                       sp: Span,
91                       mutbl: bool,
92                       ident: ast::Ident,
93                       typ: P<ast::Ty>,
94                       ex: Gc<ast::Expr>)
95                       -> Gc<ast::Stmt>;
96     fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt>;
97
98     // blocks
99     fn block(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
100              expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
101     fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block>;
102     fn block_all(&self, span: Span,
103                  view_items: Vec<ast::ViewItem> ,
104                  stmts: Vec<Gc<ast::Stmt>> ,
105                  expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
106
107     // expressions
108     fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr>;
109     fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr>;
110     fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr>;
111
112     fn expr_self(&self, span: Span) -> Gc<ast::Expr>;
113     fn expr_binary(&self, sp: Span, op: ast::BinOp,
114                    lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr>;
115     fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
116     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
117
118     fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
119     fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
120     fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
121     fn expr_field_access(&self, span: Span, expr: Gc<ast::Expr>,
122                          ident: ast::Ident) -> Gc<ast::Expr>;
123     fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
124                  args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
125     fn expr_call_ident(&self, span: Span, id: ast::Ident,
126                        args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
127     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
128                         args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
129     fn expr_method_call(&self, span: Span,
130                         expr: Gc<ast::Expr>, ident: ast::Ident,
131                         args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
132     fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr>;
133     fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>,
134                  ty: P<ast::Ty>) -> Gc<ast::Expr>;
135
136     fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field;
137     fn expr_struct(&self, span: Span, path: ast::Path,
138                    fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
139     fn expr_struct_ident(&self, span: Span, id: ast::Ident,
140                          fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
141
142     fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr>;
143
144     fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr>;
145     fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr>;
146     fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr>;
147     fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr>;
148
149     fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
150     fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr>;
151     fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
152     fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr>;
153
154     fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
155     fn expr_none(&self, sp: Span) -> Gc<ast::Expr>;
156
157     fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
158
159     fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr>;
160     fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr>;
161
162     fn expr_ok(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
163     fn expr_err(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
164     fn expr_try(&self, span: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr>;
165
166     fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat>;
167     fn pat_wild(&self, span: Span) -> Gc<ast::Pat>;
168     fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat>;
169     fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat>;
170
171     fn pat_ident_binding_mode(&self,
172                               span: Span,
173                               ident: ast::Ident,
174                               bm: ast::BindingMode) -> Gc<ast::Pat>;
175     fn pat_enum(&self, span: Span, path: ast::Path,
176                 subpats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
177     fn pat_struct(&self, span: Span,
178                   path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat>;
179     fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
180
181     fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
182     fn pat_none(&self, span: Span) -> Gc<ast::Pat>;
183
184     fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
185     fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
186
187     fn arm(&self, span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm;
188     fn arm_unreachable(&self, span: Span) -> ast::Arm;
189
190     fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, arms: Vec<ast::Arm> ) -> Gc<ast::Expr>;
191     fn expr_if(&self, span: Span,
192                cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
193                els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr>;
194     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr>;
195
196     fn lambda_fn_decl(&self, span: Span,
197                       fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr>;
198
199     fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr>;
200     fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr>;
201     fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr>;
202
203     fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: Gc<ast::Expr>) -> Gc<ast::Expr>;
204     fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
205     fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr>;
206
207     fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
208                     blk: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
209     fn lambda_stmts_0(&self, span: Span,
210                       stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
211     fn lambda_stmts_1(&self, span: Span,
212                       stmts: Vec<Gc<ast::Stmt>>, ident: ast::Ident) -> Gc<ast::Expr>;
213
214     // items
215     fn item(&self, span: Span,
216             name: Ident, attrs: Vec<ast::Attribute>,
217             node: ast::Item_) -> Gc<ast::Item>;
218
219     fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
220     // FIXME unused self
221     fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
222
223     fn item_fn_poly(&self,
224                     span: Span,
225                     name: Ident,
226                     inputs: Vec<ast::Arg> ,
227                     output: P<ast::Ty>,
228                     generics: Generics,
229                     body: P<ast::Block>) -> Gc<ast::Item>;
230     fn item_fn(&self,
231                span: Span,
232                name: Ident,
233                inputs: Vec<ast::Arg> ,
234                output: P<ast::Ty>,
235                body: P<ast::Block>) -> Gc<ast::Item>;
236
237     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
238     fn item_enum_poly(&self,
239                       span: Span,
240                       name: Ident,
241                       enum_definition: ast::EnumDef,
242                       generics: Generics) -> Gc<ast::Item>;
243     fn item_enum(&self, span: Span, name: Ident,
244                  enum_def: ast::EnumDef) -> Gc<ast::Item>;
245
246     fn item_struct_poly(&self,
247                         span: Span,
248                         name: Ident,
249                         struct_def: ast::StructDef,
250                         generics: Generics) -> Gc<ast::Item>;
251     fn item_struct(&self, span: Span, name: Ident,
252                    struct_def: ast::StructDef) -> Gc<ast::Item>;
253
254     fn item_mod(&self, span: Span, inner_span: Span,
255                 name: Ident, attrs: Vec<ast::Attribute>,
256                 vi: Vec<ast::ViewItem>,
257                 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item>;
258
259     fn item_static(&self,
260                    span: Span,
261                    name: Ident,
262                    ty: P<ast::Ty>,
263                    mutbl: ast::Mutability,
264                    expr: Gc<ast::Expr>)
265                    -> Gc<ast::Item>;
266
267     fn item_ty_poly(&self,
268                     span: Span,
269                     name: Ident,
270                     ty: P<ast::Ty>,
271                     generics: Generics) -> Gc<ast::Item>;
272     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item>;
273
274     fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute;
275
276     fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem>;
277     fn meta_list(&self,
278                  sp: Span,
279                  name: InternedString,
280                  mis: Vec<Gc<ast::MetaItem>>)
281                  -> Gc<ast::MetaItem>;
282     fn meta_name_value(&self,
283                        sp: Span,
284                        name: InternedString,
285                        value: ast::Lit_)
286                        -> Gc<ast::MetaItem>;
287
288     fn view_use(&self, sp: Span,
289                 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem;
290     fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
291     fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
292                         ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
293     fn view_use_list(&self, sp: Span, vis: ast::Visibility,
294                      path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
295     fn view_use_glob(&self, sp: Span,
296                      vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
297 }
298
299 impl<'a> AstBuilder for ExtCtxt<'a> {
300     fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
301         self.path_all(span, false, strs, Vec::new(), Vec::new())
302     }
303     fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
304         self.path(span, vec!(id))
305     }
306     fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
307         self.path_all(span, true, strs, Vec::new(), Vec::new())
308     }
309     fn path_all(&self,
310                 sp: Span,
311                 global: bool,
312                 mut idents: Vec<ast::Ident> ,
313                 lifetimes: Vec<ast::Lifetime>,
314                 types: Vec<P<ast::Ty>> )
315                 -> ast::Path {
316         let last_identifier = idents.pop().unwrap();
317         let mut segments: Vec<ast::PathSegment> = idents.move_iter()
318                                                       .map(|ident| {
319             ast::PathSegment {
320                 identifier: ident,
321                 lifetimes: Vec::new(),
322                 types: OwnedSlice::empty(),
323             }
324         }).collect();
325         segments.push(ast::PathSegment {
326             identifier: last_identifier,
327             lifetimes: lifetimes,
328             types: OwnedSlice::from_vec(types),
329         });
330         ast::Path {
331             span: sp,
332             global: global,
333             segments: segments,
334         }
335     }
336
337     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
338         ast::MutTy {
339             ty: ty,
340             mutbl: mutbl
341         }
342     }
343
344     fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
345         P(ast::Ty {
346             id: ast::DUMMY_NODE_ID,
347             span: span,
348             node: ty
349         })
350     }
351
352     fn ty_path(&self, path: ast::Path, bounds: Option<OwnedSlice<ast::TyParamBound>>)
353               -> P<ast::Ty> {
354         self.ty(path.span,
355                 ast::TyPath(path, bounds, ast::DUMMY_NODE_ID))
356     }
357
358     // Might need to take bounds as an argument in the future, if you ever want
359     // to generate a bounded existential trait type.
360     fn ty_ident(&self, span: Span, ident: ast::Ident)
361         -> P<ast::Ty> {
362         self.ty_path(self.path_ident(span, ident), None)
363     }
364
365     fn ty_rptr(&self,
366                span: Span,
367                ty: P<ast::Ty>,
368                lifetime: Option<ast::Lifetime>,
369                mutbl: ast::Mutability)
370         -> P<ast::Ty> {
371         self.ty(span,
372                 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
373     }
374
375     fn ty_ptr(&self,
376               span: Span,
377               ty: P<ast::Ty>,
378               mutbl: ast::Mutability)
379         -> P<ast::Ty> {
380         self.ty(span,
381                 ast::TyPtr(self.ty_mt(ty, mutbl)))
382     }
383     fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty> {
384         self.ty(span, ast::TyUniq(ty))
385     }
386
387     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
388         self.ty_path(
389             self.path_all(DUMMY_SP,
390                           true,
391                           vec!(
392                               self.ident_of("std"),
393                               self.ident_of("option"),
394                               self.ident_of("Option")
395                           ),
396                           Vec::new(),
397                           vec!( ty )), None)
398     }
399
400     fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
401         ast::TypeField {
402             ident: name,
403             mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
404             span: span,
405         }
406     }
407
408     fn ty_infer(&self, span: Span) -> P<ast::Ty> {
409         self.ty(span, ast::TyInfer)
410     }
411
412     fn ty_nil(&self) -> P<ast::Ty> {
413         P(ast::Ty {
414             id: ast::DUMMY_NODE_ID,
415             node: ast::TyNil,
416             span: DUMMY_SP,
417         })
418     }
419
420     fn typaram(&self,
421                span: Span,
422                id: ast::Ident,
423                bounds: OwnedSlice<ast::TyParamBound>,
424                unbound: Option<ast::TyParamBound>,
425                default: Option<P<ast::Ty>>) -> ast::TyParam {
426         ast::TyParam {
427             ident: id,
428             id: ast::DUMMY_NODE_ID,
429             bounds: bounds,
430             unbound: unbound,
431             default: default,
432             span: span
433         }
434     }
435
436     // these are strange, and probably shouldn't be used outside of
437     // pipes. Specifically, the global version possible generates
438     // incorrect code.
439     fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
440         ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
441     }
442
443     fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
444         ty_params.iter().map(|p| self.ty_path(
445                 self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect()
446     }
447
448     fn strip_bounds(&self, generics: &Generics) -> Generics {
449         let new_params = generics.ty_params.map(|ty_param| {
450             ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
451         });
452         Generics {
453             ty_params: new_params,
454             .. (*generics).clone()
455         }
456     }
457
458     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
459         ast::TraitRef {
460             path: path,
461             ref_id: ast::DUMMY_NODE_ID
462         }
463     }
464
465     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
466         ast::TraitTyParamBound(self.trait_ref(path))
467     }
468
469     fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
470         ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
471     }
472
473     fn lifetime_def(&self,
474                     span: Span,
475                     name: ast::Name,
476                     bounds: Vec<ast::Lifetime>)
477                     -> ast::LifetimeDef {
478         ast::LifetimeDef {
479             lifetime: self.lifetime(span, name),
480             bounds: bounds
481         }
482     }
483
484     fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt> {
485         box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
486     }
487
488     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
489                 ex: Gc<ast::Expr>) -> Gc<ast::Stmt> {
490         let pat = if mutbl {
491             self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
492         } else {
493             self.pat_ident(sp, ident)
494         };
495         let local = box(GC) ast::Local {
496             ty: self.ty_infer(sp),
497             pat: pat,
498             init: Some(ex),
499             id: ast::DUMMY_NODE_ID,
500             span: sp,
501             source: ast::LocalLet,
502         };
503         let decl = respan(sp, ast::DeclLocal(local));
504         box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
505     }
506
507     fn stmt_let_typed(&self,
508                       sp: Span,
509                       mutbl: bool,
510                       ident: ast::Ident,
511                       typ: P<ast::Ty>,
512                       ex: Gc<ast::Expr>)
513                       -> Gc<ast::Stmt> {
514         let pat = if mutbl {
515             self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
516         } else {
517             self.pat_ident(sp, ident)
518         };
519         let local = box(GC) ast::Local {
520             ty: typ,
521             pat: pat,
522             init: Some(ex),
523             id: ast::DUMMY_NODE_ID,
524             span: sp,
525             source: ast::LocalLet,
526         };
527         let decl = respan(sp, ast::DeclLocal(local));
528         box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
529     }
530
531     fn block(&self,
532              span: Span,
533              stmts: Vec<Gc<ast::Stmt>>,
534              expr: Option<Gc<Expr>>)
535              -> P<ast::Block> {
536         self.block_all(span, Vec::new(), stmts, expr)
537     }
538
539     fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt> {
540         let decl = respan(sp, ast::DeclItem(item));
541         box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
542     }
543
544     fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block> {
545         self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
546     }
547     fn block_all(&self,
548                  span: Span,
549                  view_items: Vec<ast::ViewItem> ,
550                  stmts: Vec<Gc<ast::Stmt>>,
551                  expr: Option<Gc<ast::Expr>>) -> P<ast::Block> {
552             P(ast::Block {
553                view_items: view_items,
554                stmts: stmts,
555                expr: expr,
556                id: ast::DUMMY_NODE_ID,
557                rules: ast::DefaultBlock,
558                span: span,
559             })
560     }
561
562     fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr> {
563         box(GC) ast::Expr {
564             id: ast::DUMMY_NODE_ID,
565             node: node,
566             span: span,
567         }
568     }
569
570     fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr> {
571         self.expr(path.span, ast::ExprPath(path))
572     }
573
574     fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr> {
575         self.expr_path(self.path_ident(span, id))
576     }
577     fn expr_self(&self, span: Span) -> Gc<ast::Expr> {
578         self.expr_ident(span, special_idents::self_)
579     }
580
581     fn expr_binary(&self, sp: Span, op: ast::BinOp,
582                    lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr> {
583         self.expr(sp, ast::ExprBinary(op, lhs, rhs))
584     }
585
586     fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
587         self.expr_unary(sp, ast::UnDeref, e)
588     }
589     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
590         self.expr(sp, ast::ExprUnary(op, e))
591     }
592
593     fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
594         self.expr_unary(sp, ast::UnBox, e)
595     }
596
597     fn expr_field_access(&self, sp: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
598         let field_name = token::get_ident(ident);
599         let field_span = Span {
600             lo: sp.lo - Pos::from_uint(field_name.get().len()),
601             hi: sp.hi,
602             expn_info: sp.expn_info,
603         };
604
605         let id = Spanned { node: ident, span: field_span };
606         self.expr(sp, ast::ExprField(expr, id, Vec::new()))
607     }
608     fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
609         self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
610     }
611     fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
612         self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
613     }
614
615     fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
616                  args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
617         self.expr(span, ast::ExprCall(expr, args))
618     }
619     fn expr_call_ident(&self, span: Span, id: ast::Ident,
620                        args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
621         self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
622     }
623     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
624                       args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
625         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
626         self.expr_call(sp, pathexpr, args)
627     }
628     fn expr_method_call(&self, span: Span,
629                         expr: Gc<ast::Expr>,
630                         ident: ast::Ident,
631                         mut args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
632         let id = Spanned { node: ident, span: span };
633         args.unshift(expr);
634         self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
635     }
636     fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr> {
637         self.expr(b.span, ast::ExprBlock(b))
638     }
639     fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field {
640         ast::Field { ident: respan(span, name), expr: e, span: span }
641     }
642     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
643         self.expr(span, ast::ExprStruct(path, fields, None))
644     }
645     fn expr_struct_ident(&self, span: Span,
646                          id: ast::Ident, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
647         self.expr_struct(span, self.path_ident(span, id), fields)
648     }
649
650     fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr> {
651         self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit)))
652     }
653     fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> {
654         self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyU)))
655     }
656     fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> {
657         self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyI, ast::Sign::new(i))))
658     }
659     fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> {
660         self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
661     }
662     fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> {
663         self.expr_lit(sp, ast::LitBool(value))
664     }
665
666     fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
667         self.expr(sp, ast::ExprVec(exprs))
668     }
669     fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr> {
670         self.expr_call_global(sp,
671                               vec!(self.ident_of("std"),
672                                    self.ident_of("vec"),
673                                    self.ident_of("Vec"),
674                                    self.ident_of("new")),
675                               Vec::new())
676     }
677     fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
678         self.expr_addr_of(sp, self.expr_vec(sp, exprs))
679     }
680     fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
681         self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
682     }
683
684     fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, ty: P<ast::Ty>) -> Gc<ast::Expr> {
685         self.expr(sp, ast::ExprCast(expr, ty))
686     }
687
688
689     fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
690         let some = vec!(
691             self.ident_of("std"),
692             self.ident_of("option"),
693             self.ident_of("Some"));
694         self.expr_call_global(sp, some, vec!(expr))
695     }
696
697     fn expr_none(&self, sp: Span) -> Gc<ast::Expr> {
698         let none = self.path_global(sp, vec!(
699             self.ident_of("std"),
700             self.ident_of("option"),
701             self.ident_of("None")));
702         self.expr_path(none)
703     }
704
705     fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
706         self.expr(sp, ast::ExprTup(exprs))
707     }
708
709     fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr> {
710         let loc = self.codemap().lookup_char_pos(span.lo);
711         let expr_file = self.expr_str(span,
712                                       token::intern_and_get_ident(loc.file
713                                                                   .name
714                                                                   .as_slice()));
715         let expr_line = self.expr_uint(span, loc.line);
716         let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
717         let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
718         self.expr_call_global(
719             span,
720             vec!(
721                 self.ident_of("std"),
722                 self.ident_of("rt"),
723                 self.ident_of("begin_unwind")),
724             vec!(
725                 self.expr_str(span, msg),
726                 expr_file_line_ptr))
727     }
728
729     fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr> {
730         self.expr_fail(span,
731                        InternedString::new(
732                            "internal error: entered unreachable code"))
733     }
734
735     fn expr_ok(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
736         let ok = vec!(
737             self.ident_of("std"),
738             self.ident_of("result"),
739             self.ident_of("Ok"));
740         self.expr_call_global(sp, ok, vec!(expr))
741     }
742
743     fn expr_err(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
744         let err = vec!(
745             self.ident_of("std"),
746             self.ident_of("result"),
747             self.ident_of("Err"));
748         self.expr_call_global(sp, err, vec!(expr))
749     }
750
751     fn expr_try(&self, sp: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr> {
752         let ok = self.ident_of("Ok");
753         let ok_path = self.path_ident(sp, ok);
754         let err = self.ident_of("Err");
755         let err_path = self.path_ident(sp, err);
756
757         let binding_variable = self.ident_of("__try_var");
758         let binding_pat = self.pat_ident(sp, binding_variable);
759         let binding_expr = self.expr_ident(sp, binding_variable);
760
761         // Ok(__try_var) pattern
762         let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat));
763
764         // Err(__try_var)  (pattern and expression resp.)
765         let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
766         let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr));
767         // return Err(__try_var)
768         let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
769
770         // Ok(__try_var) => __try_var
771         let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
772         // Err(__try_var) => return Err(__try_var)
773         let err_arm = self.arm(sp, vec!(err_pat), err_expr);
774
775         // match head { Ok() => ..., Err() => ... }
776         self.expr_match(sp, head, vec!(ok_arm, err_arm))
777     }
778
779
780     fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat> {
781         box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }
782     }
783     fn pat_wild(&self, span: Span) -> Gc<ast::Pat> {
784         self.pat(span, ast::PatWild(ast::PatWildSingle))
785     }
786     fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat> {
787         self.pat(span, ast::PatLit(expr))
788     }
789     fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat> {
790         self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
791     }
792
793     fn pat_ident_binding_mode(&self,
794                               span: Span,
795                               ident: ast::Ident,
796                               bm: ast::BindingMode) -> Gc<ast::Pat> {
797         let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
798         self.pat(span, pat)
799     }
800     fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<Gc<ast::Pat>> ) -> Gc<ast::Pat> {
801         let pat = ast::PatEnum(path, Some(subpats));
802         self.pat(span, pat)
803     }
804     fn pat_struct(&self, span: Span,
805                   path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat> {
806         let pat = ast::PatStruct(path, field_pats, false);
807         self.pat(span, pat)
808     }
809     fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat> {
810         let pat = ast::PatTup(pats);
811         self.pat(span, pat)
812     }
813
814     fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
815         let some = vec!(
816             self.ident_of("std"),
817             self.ident_of("option"),
818             self.ident_of("Some"));
819         let path = self.path_global(span, some);
820         self.pat_enum(span, path, vec!(pat))
821     }
822
823     fn pat_none(&self, span: Span) -> Gc<ast::Pat> {
824         let some = vec!(
825             self.ident_of("std"),
826             self.ident_of("option"),
827             self.ident_of("None"));
828         let path = self.path_global(span, some);
829         self.pat_enum(span, path, vec!())
830     }
831
832     fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
833         let some = vec!(
834             self.ident_of("std"),
835             self.ident_of("result"),
836             self.ident_of("Ok"));
837         let path = self.path_global(span, some);
838         self.pat_enum(span, path, vec!(pat))
839     }
840
841     fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
842         let some = vec!(
843             self.ident_of("std"),
844             self.ident_of("result"),
845             self.ident_of("Err"));
846         let path = self.path_global(span, some);
847         self.pat_enum(span, path, vec!(pat))
848     }
849
850     fn arm(&self, _span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm {
851         ast::Arm {
852             attrs: vec!(),
853             pats: pats,
854             guard: None,
855             body: expr
856         }
857     }
858
859     fn arm_unreachable(&self, span: Span) -> ast::Arm {
860         self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
861     }
862
863     fn expr_match(&self, span: Span, arg: Gc<ast::Expr>,
864                   arms: Vec<ast::Arm>) -> Gc<Expr> {
865         self.expr(span, ast::ExprMatch(arg, arms))
866     }
867
868     fn expr_if(&self, span: Span,
869                cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
870                els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr> {
871         let els = els.map(|x| self.expr_block(self.block_expr(x)));
872         self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
873     }
874
875     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr> {
876         self.expr(span, ast::ExprLoop(block, None))
877     }
878
879     fn lambda_fn_decl(&self, span: Span,
880                       fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr> {
881         self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
882     }
883     fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr> {
884         let fn_decl = self.fn_decl(
885             ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
886             self.ty_infer(span));
887
888         self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
889     }
890     fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr> {
891         self.lambda(span, Vec::new(), blk)
892     }
893
894     fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr> {
895         self.lambda(span, vec!(ident), blk)
896     }
897
898     fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
899         self.lambda(span, ids, self.block_expr(expr))
900     }
901     fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
902         self.lambda0(span, self.block_expr(expr))
903     }
904     fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
905         self.lambda1(span, self.block_expr(expr), ident)
906     }
907
908     fn lambda_stmts(&self,
909                     span: Span,
910                     ids: Vec<ast::Ident>,
911                     stmts: Vec<Gc<ast::Stmt>>)
912                     -> Gc<ast::Expr> {
913         self.lambda(span, ids, self.block(span, stmts, None))
914     }
915     fn lambda_stmts_0(&self, span: Span,
916                       stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr> {
917         self.lambda0(span, self.block(span, stmts, None))
918     }
919     fn lambda_stmts_1(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
920                       ident: ast::Ident) -> Gc<ast::Expr> {
921         self.lambda1(span, self.block(span, stmts, None), ident)
922     }
923
924     fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
925         let arg_pat = self.pat_ident(span, ident);
926         ast::Arg {
927             ty: ty,
928             pat: arg_pat,
929             id: ast::DUMMY_NODE_ID
930         }
931     }
932
933     // FIXME unused self
934     fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl> {
935         P(ast::FnDecl {
936             inputs: inputs,
937             output: output,
938             cf: ast::Return,
939             variadic: false
940         })
941     }
942
943     fn item(&self, span: Span,
944             name: Ident, attrs: Vec<ast::Attribute>,
945             node: ast::Item_) -> Gc<ast::Item> {
946         // FIXME: Would be nice if our generated code didn't violate
947         // Rust coding conventions
948         box(GC) ast::Item { ident: name,
949                     attrs: attrs,
950                     id: ast::DUMMY_NODE_ID,
951                     node: node,
952                     vis: ast::Inherited,
953                     span: span }
954     }
955
956     fn item_fn_poly(&self,
957                     span: Span,
958                     name: Ident,
959                     inputs: Vec<ast::Arg> ,
960                     output: P<ast::Ty>,
961                     generics: Generics,
962                     body: P<ast::Block>) -> Gc<ast::Item> {
963         self.item(span,
964                   name,
965                   Vec::new(),
966                   ast::ItemFn(self.fn_decl(inputs, output),
967                               ast::NormalFn,
968                               abi::Rust,
969                               generics,
970                               body))
971     }
972
973     fn item_fn(&self,
974                span: Span,
975                name: Ident,
976                inputs: Vec<ast::Arg> ,
977                output: P<ast::Ty>,
978                body: P<ast::Block>
979               ) -> Gc<ast::Item> {
980         self.item_fn_poly(
981             span,
982             name,
983             inputs,
984             output,
985             ast_util::empty_generics(),
986             body)
987     }
988
989     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
990         let args = tys.move_iter().map(|ty| {
991             ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
992         }).collect();
993
994         respan(span,
995                ast::Variant_ {
996                    name: name,
997                    attrs: Vec::new(),
998                    kind: ast::TupleVariantKind(args),
999                    id: ast::DUMMY_NODE_ID,
1000                    disr_expr: None,
1001                    vis: ast::Public
1002                })
1003     }
1004
1005     fn item_enum_poly(&self, span: Span, name: Ident,
1006                       enum_definition: ast::EnumDef,
1007                       generics: Generics) -> Gc<ast::Item> {
1008         self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1009     }
1010
1011     fn item_enum(&self, span: Span, name: Ident,
1012                  enum_definition: ast::EnumDef) -> Gc<ast::Item> {
1013         self.item_enum_poly(span, name, enum_definition,
1014                             ast_util::empty_generics())
1015     }
1016
1017     fn item_struct(&self, span: Span, name: Ident,
1018                    struct_def: ast::StructDef) -> Gc<ast::Item> {
1019         self.item_struct_poly(
1020             span,
1021             name,
1022             struct_def,
1023             ast_util::empty_generics()
1024         )
1025     }
1026
1027     fn item_struct_poly(&self, span: Span, name: Ident,
1028         struct_def: ast::StructDef, generics: Generics) -> Gc<ast::Item> {
1029         self.item(span, name, Vec::new(), ast::ItemStruct(box(GC) struct_def, generics))
1030     }
1031
1032     fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1033                 attrs: Vec<ast::Attribute> ,
1034                 vi: Vec<ast::ViewItem> ,
1035                 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item> {
1036         self.item(
1037             span,
1038             name,
1039             attrs,
1040             ast::ItemMod(ast::Mod {
1041                 inner: inner_span,
1042                 view_items: vi,
1043                 items: items,
1044             })
1045         )
1046     }
1047
1048     fn item_static(&self,
1049                    span: Span,
1050                    name: Ident,
1051                    ty: P<ast::Ty>,
1052                    mutbl: ast::Mutability,
1053                    expr: Gc<ast::Expr>)
1054                    -> Gc<ast::Item> {
1055         self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1056     }
1057
1058     fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1059                     generics: Generics) -> Gc<ast::Item> {
1060         self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1061     }
1062
1063     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item> {
1064         self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1065     }
1066
1067     fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute {
1068         respan(sp, ast::Attribute_ {
1069             id: attr::mk_attr_id(),
1070             style: ast::AttrOuter,
1071             value: mi,
1072             is_sugared_doc: false,
1073         })
1074     }
1075
1076     fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem> {
1077         box(GC) respan(sp, ast::MetaWord(w))
1078     }
1079     fn meta_list(&self,
1080                  sp: Span,
1081                  name: InternedString,
1082                  mis: Vec<Gc<ast::MetaItem>> )
1083                  -> Gc<ast::MetaItem> {
1084         box(GC) respan(sp, ast::MetaList(name, mis))
1085     }
1086     fn meta_name_value(&self,
1087                        sp: Span,
1088                        name: InternedString,
1089                        value: ast::Lit_)
1090                        -> Gc<ast::MetaItem> {
1091         box(GC) respan(sp, ast::MetaNameValue(name, respan(sp, value)))
1092     }
1093
1094     fn view_use(&self, sp: Span,
1095                 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem {
1096         ast::ViewItem {
1097             node: ast::ViewItemUse(vp),
1098             attrs: Vec::new(),
1099             vis: vis,
1100             span: sp
1101         }
1102     }
1103
1104     fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1105         let last = path.segments.last().unwrap().identifier;
1106         self.view_use_simple_(sp, vis, last, path)
1107     }
1108
1109     fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1110                         ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1111         self.view_use(sp, vis,
1112                       box(GC) respan(sp,
1113                            ast::ViewPathSimple(ident,
1114                                                path,
1115                                                ast::DUMMY_NODE_ID)))
1116     }
1117
1118     fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1119                      path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1120         let imports = imports.iter().map(|id| {
1121             respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1122         }).collect();
1123
1124         self.view_use(sp, vis,
1125                       box(GC) respan(sp,
1126                            ast::ViewPathList(self.path(sp, path),
1127                                              imports,
1128                                              ast::DUMMY_NODE_ID)))
1129     }
1130
1131     fn view_use_glob(&self, sp: Span,
1132                      vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1133         self.view_use(sp, vis,
1134                       box(GC) respan(sp,
1135                            ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))
1136     }
1137 }
1138
1139 struct Duplicator<'a>;
1140
1141 impl<'a> Folder for Duplicator<'a> {
1142     fn new_id(&mut self, _: NodeId) -> NodeId {
1143         ast::DUMMY_NODE_ID
1144     }
1145 }
1146
1147 pub trait Duplicate {
1148     //
1149     // Duplication functions
1150     //
1151     // These functions just duplicate AST nodes.
1152     //
1153
1154     fn duplicate(&self, cx: &ExtCtxt) -> Self;
1155 }
1156
1157 impl Duplicate for Gc<ast::Expr> {
1158     fn duplicate(&self, _: &ExtCtxt) -> Gc<ast::Expr> {
1159         let mut folder = Duplicator;
1160         folder.fold_expr(*self)
1161     }
1162 }