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