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