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