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