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