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