]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/build.rs
Add `ecx.stmt_semi()` and fix issues with the pretty-printer
[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::Abi;
12 use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
13 use attr;
14 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
15 use ext::base::ExtCtxt;
16 use parse::token::{self, keywords, InternedString};
17 use ptr::P;
18
19 // Transitional reexports so qquote can find the paths it is looking for
20 mod syntax {
21     pub use ext;
22     pub use parse;
23 }
24
25 pub trait AstBuilder {
26     // paths
27     fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
28     fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
29     fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
30     fn path_all(&self, sp: Span,
31                 global: bool,
32                 idents: Vec<ast::Ident> ,
33                 lifetimes: Vec<ast::Lifetime>,
34                 types: Vec<P<ast::Ty>>,
35                 bindings: Vec<ast::TypeBinding> )
36         -> ast::Path;
37
38     fn qpath(&self, self_type: P<ast::Ty>,
39              trait_path: ast::Path,
40              ident: ast::Ident)
41              -> (ast::QSelf, ast::Path);
42     fn qpath_all(&self, self_type: P<ast::Ty>,
43                 trait_path: ast::Path,
44                 ident: ast::Ident,
45                 lifetimes: Vec<ast::Lifetime>,
46                 types: Vec<P<ast::Ty>>,
47                 bindings: Vec<ast::TypeBinding>)
48                 -> (ast::QSelf, ast::Path);
49
50     // types
51     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
52
53     fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
54     fn ty_path(&self, ast::Path) -> P<ast::Ty>;
55     fn ty_sum(&self, ast::Path, ast::TyParamBounds) -> P<ast::Ty>;
56     fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
57
58     fn ty_rptr(&self, span: Span,
59                ty: P<ast::Ty>,
60                lifetime: Option<ast::Lifetime>,
61                mutbl: ast::Mutability) -> P<ast::Ty>;
62     fn ty_ptr(&self, span: Span,
63               ty: P<ast::Ty>,
64               mutbl: ast::Mutability) -> P<ast::Ty>;
65
66     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
67     fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
68
69     fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
70     fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
71
72     fn typaram(&self,
73                span: Span,
74                id: ast::Ident,
75                bounds: ast::TyParamBounds,
76                default: Option<P<ast::Ty>>) -> ast::TyParam;
77
78     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
79     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
80     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
81     fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
82     fn lifetime_def(&self,
83                     span: Span,
84                     name: ast::Name,
85                     bounds: Vec<ast::Lifetime>)
86                     -> ast::LifetimeDef;
87
88     // statements
89     fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
90     fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
91     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
92     fn stmt_let_typed(&self,
93                       sp: Span,
94                       mutbl: bool,
95                       ident: ast::Ident,
96                       typ: P<ast::Ty>,
97                       ex: P<ast::Expr>)
98                       -> P<ast::Stmt>;
99     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
100
101     // blocks
102     fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
103     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
104
105     // expressions
106     fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
107     fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
108     fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
109     fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
110
111     fn expr_self(&self, span: Span) -> P<ast::Expr>;
112     fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
113                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
114     fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
115     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
116
117     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
118     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
119     fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
120     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
121                              idx: usize) -> P<ast::Expr>;
122     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
123     fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
124     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
125                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
126     fn expr_method_call(&self, span: Span,
127                         expr: P<ast::Expr>, ident: ast::Ident,
128                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
129     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
130     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
131
132     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
133     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
134     fn expr_struct_ident(&self, span: Span, id: ast::Ident,
135                          fields: Vec<ast::Field>) -> P<ast::Expr>;
136
137     fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
138
139     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
140     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
141     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
142     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
143     fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
144
145     fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
146     fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
147     fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
148     fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
149
150     fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
151     fn expr_none(&self, sp: Span) -> P<ast::Expr>;
152
153     fn expr_break(&self, sp: Span) -> P<ast::Expr>;
154
155     fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
156
157     fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
158     fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
159
160     fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
161     fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
162     fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
163
164     fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
165     fn pat_wild(&self, span: Span) -> P<ast::Pat>;
166     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
167     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
168
169     fn pat_ident_binding_mode(&self,
170                               span: Span,
171                               ident: ast::Ident,
172                               bm: ast::BindingMode) -> P<ast::Pat>;
173     fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
174     fn pat_struct(&self, span: Span,
175                   path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>> ) -> P<ast::Pat>;
176     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
177
178     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
179     fn pat_none(&self, span: Span) -> P<ast::Pat>;
180
181     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
182     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
183
184     fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
185     fn arm_unreachable(&self, span: Span) -> ast::Arm;
186
187     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
188     fn expr_if(&self, span: Span,
189                cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
190     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
191
192     fn lambda_fn_decl(&self,
193                       span: Span,
194                       fn_decl: P<ast::FnDecl>,
195                       blk: P<ast::Block>,
196                       fn_decl_span: Span)
197                       -> P<ast::Expr>;
198
199     fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr>;
200     fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
201     fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
202
203     fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
204     fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
205     fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
206
207     fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
208                     blk: Vec<ast::Stmt>) -> P<ast::Expr>;
209     fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
210     fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
211                       ident: ast::Ident) -> P<ast::Expr>;
212
213     // items
214     fn item(&self, span: Span,
215             name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
216
217     fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
218     // FIXME unused self
219     fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
220
221     fn item_fn_poly(&self,
222                     span: Span,
223                     name: Ident,
224                     inputs: Vec<ast::Arg> ,
225                     output: P<ast::Ty>,
226                     generics: Generics,
227                     body: P<ast::Block>) -> P<ast::Item>;
228     fn item_fn(&self,
229                span: Span,
230                name: Ident,
231                inputs: Vec<ast::Arg> ,
232                output: P<ast::Ty>,
233                body: P<ast::Block>) -> P<ast::Item>;
234
235     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
236     fn item_enum_poly(&self,
237                       span: Span,
238                       name: Ident,
239                       enum_definition: ast::EnumDef,
240                       generics: Generics) -> P<ast::Item>;
241     fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
242
243     fn item_struct_poly(&self,
244                         span: Span,
245                         name: Ident,
246                         struct_def: ast::VariantData,
247                         generics: Generics) -> P<ast::Item>;
248     fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
249
250     fn item_mod(&self, span: Span, inner_span: Span,
251                 name: Ident, attrs: Vec<ast::Attribute>,
252                 items: Vec<P<ast::Item>>) -> P<ast::Item>;
253
254     fn item_static(&self,
255                    span: Span,
256                    name: Ident,
257                    ty: P<ast::Ty>,
258                    mutbl: ast::Mutability,
259                    expr: P<ast::Expr>)
260                    -> P<ast::Item>;
261
262     fn item_const(&self,
263                    span: Span,
264                    name: Ident,
265                    ty: P<ast::Ty>,
266                    expr: P<ast::Expr>)
267                    -> P<ast::Item>;
268
269     fn item_ty_poly(&self,
270                     span: Span,
271                     name: Ident,
272                     ty: P<ast::Ty>,
273                     generics: Generics) -> P<ast::Item>;
274     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
275
276     fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
277
278     fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
279     fn meta_list(&self,
280                  sp: Span,
281                  name: InternedString,
282                  mis: Vec<P<ast::MetaItem>> )
283                  -> P<ast::MetaItem>;
284     fn meta_name_value(&self,
285                        sp: Span,
286                        name: InternedString,
287                        value: ast::LitKind)
288                        -> P<ast::MetaItem>;
289
290     fn item_use(&self, sp: Span,
291                 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
292     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
293     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
294                         ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
295     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
296                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
297     fn item_use_glob(&self, sp: Span,
298                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
299 }
300
301 impl<'a> AstBuilder for ExtCtxt<'a> {
302     fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
303         self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
304     }
305     fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
306         self.path(span, vec!(id))
307     }
308     fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
309         self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
310     }
311     fn path_all(&self,
312                 sp: Span,
313                 global: bool,
314                 mut idents: Vec<ast::Ident> ,
315                 lifetimes: Vec<ast::Lifetime>,
316                 types: Vec<P<ast::Ty>>,
317                 bindings: Vec<ast::TypeBinding> )
318                 -> ast::Path {
319         let last_identifier = idents.pop().unwrap();
320         let mut segments: Vec<ast::PathSegment> = idents.into_iter()
321                                                       .map(|ident| {
322             ast::PathSegment {
323                 identifier: ident,
324                 parameters: ast::PathParameters::none(),
325             }
326         }).collect();
327         segments.push(ast::PathSegment {
328             identifier: last_identifier,
329             parameters: ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
330                 lifetimes: lifetimes,
331                 types: P::from_vec(types),
332                 bindings: P::from_vec(bindings),
333             })
334         });
335         ast::Path {
336             span: sp,
337             global: global,
338             segments: segments,
339         }
340     }
341
342     /// Constructs a qualified path.
343     ///
344     /// Constructs a path like `<self_type as trait_path>::ident`.
345     fn qpath(&self,
346              self_type: P<ast::Ty>,
347              trait_path: ast::Path,
348              ident: ast::Ident)
349              -> (ast::QSelf, ast::Path) {
350         self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
351     }
352
353     /// Constructs a qualified path.
354     ///
355     /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
356     fn qpath_all(&self,
357                  self_type: P<ast::Ty>,
358                  trait_path: ast::Path,
359                  ident: ast::Ident,
360                  lifetimes: Vec<ast::Lifetime>,
361                  types: Vec<P<ast::Ty>>,
362                  bindings: Vec<ast::TypeBinding>)
363                  -> (ast::QSelf, ast::Path) {
364         let mut path = trait_path;
365         path.segments.push(ast::PathSegment {
366             identifier: ident,
367             parameters: ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
368                 lifetimes: lifetimes,
369                 types: P::from_vec(types),
370                 bindings: P::from_vec(bindings),
371             })
372         });
373
374         (ast::QSelf {
375             ty: self_type,
376             position: path.segments.len() - 1
377         }, path)
378     }
379
380     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
381         ast::MutTy {
382             ty: ty,
383             mutbl: mutbl
384         }
385     }
386
387     fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
388         P(ast::Ty {
389             id: ast::DUMMY_NODE_ID,
390             span: span,
391             node: ty
392         })
393     }
394
395     fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
396         self.ty(path.span, ast::TyKind::Path(None, path))
397     }
398
399     fn ty_sum(&self, path: ast::Path, bounds: ast::TyParamBounds) -> P<ast::Ty> {
400         self.ty(path.span,
401                 ast::TyKind::ObjectSum(self.ty_path(path),
402                                  bounds))
403     }
404
405     // Might need to take bounds as an argument in the future, if you ever want
406     // to generate a bounded existential trait type.
407     fn ty_ident(&self, span: Span, ident: ast::Ident)
408         -> P<ast::Ty> {
409         self.ty_path(self.path_ident(span, ident))
410     }
411
412     fn ty_rptr(&self,
413                span: Span,
414                ty: P<ast::Ty>,
415                lifetime: Option<ast::Lifetime>,
416                mutbl: ast::Mutability)
417         -> P<ast::Ty> {
418         self.ty(span,
419                 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
420     }
421
422     fn ty_ptr(&self,
423               span: Span,
424               ty: P<ast::Ty>,
425               mutbl: ast::Mutability)
426         -> P<ast::Ty> {
427         self.ty(span,
428                 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
429     }
430
431     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
432         self.ty_path(
433             self.path_all(DUMMY_SP,
434                           true,
435                           self.std_path(&["option", "Option"]),
436                           Vec::new(),
437                           vec!( ty ),
438                           Vec::new()))
439     }
440
441     fn ty_infer(&self, span: Span) -> P<ast::Ty> {
442         self.ty(span, ast::TyKind::Infer)
443     }
444
445     fn typaram(&self,
446                span: Span,
447                id: ast::Ident,
448                bounds: ast::TyParamBounds,
449                default: Option<P<ast::Ty>>) -> ast::TyParam {
450         ast::TyParam {
451             ident: id,
452             id: ast::DUMMY_NODE_ID,
453             bounds: bounds,
454             default: default,
455             span: span
456         }
457     }
458
459     // these are strange, and probably shouldn't be used outside of
460     // pipes. Specifically, the global version possible generates
461     // incorrect code.
462     fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
463         ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
464     }
465
466     fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
467         ty_params
468             .iter()
469             .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
470             .collect()
471     }
472
473     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
474         ast::TraitRef {
475             path: path,
476             ref_id: ast::DUMMY_NODE_ID,
477         }
478     }
479
480     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
481         ast::PolyTraitRef {
482             bound_lifetimes: Vec::new(),
483             trait_ref: self.trait_ref(path),
484             span: span,
485         }
486     }
487
488     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
489         ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
490     }
491
492     fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
493         ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
494     }
495
496     fn lifetime_def(&self,
497                     span: Span,
498                     name: ast::Name,
499                     bounds: Vec<ast::Lifetime>)
500                     -> ast::LifetimeDef {
501         ast::LifetimeDef {
502             lifetime: self.lifetime(span, name),
503             bounds: bounds
504         }
505     }
506
507     fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
508         respan(expr.span, ast::StmtKind::Expr(expr, ast::DUMMY_NODE_ID))
509     }
510
511     fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
512         respan(expr.span, ast::StmtKind::Semi(expr, ast::DUMMY_NODE_ID))
513     }
514
515     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
516                 ex: P<ast::Expr>) -> ast::Stmt {
517         let pat = if mutbl {
518             let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
519             self.pat_ident_binding_mode(sp, ident, binding_mode)
520         } else {
521             self.pat_ident(sp, ident)
522         };
523         let local = P(ast::Local {
524             pat: pat,
525             ty: None,
526             init: Some(ex),
527             id: ast::DUMMY_NODE_ID,
528             span: sp,
529             attrs: None,
530         });
531         let decl = respan(sp, ast::DeclKind::Local(local));
532         respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))
533     }
534
535     fn stmt_let_typed(&self,
536                       sp: Span,
537                       mutbl: bool,
538                       ident: ast::Ident,
539                       typ: P<ast::Ty>,
540                       ex: P<ast::Expr>)
541                       -> P<ast::Stmt> {
542         let pat = if mutbl {
543             let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
544             self.pat_ident_binding_mode(sp, ident, binding_mode)
545         } else {
546             self.pat_ident(sp, ident)
547         };
548         let local = P(ast::Local {
549             pat: pat,
550             ty: Some(typ),
551             init: Some(ex),
552             id: ast::DUMMY_NODE_ID,
553             span: sp,
554             attrs: None,
555         });
556         let decl = respan(sp, ast::DeclKind::Local(local));
557         P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)))
558     }
559
560     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
561         let decl = respan(sp, ast::DeclKind::Item(item));
562         respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))
563     }
564
565     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
566         self.block(expr.span, vec![Spanned {
567             span: expr.span,
568             node: ast::StmtKind::Expr(expr, ast::DUMMY_NODE_ID),
569         }])
570     }
571     fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
572         P(ast::Block {
573            stmts: stmts,
574            id: ast::DUMMY_NODE_ID,
575            rules: BlockCheckMode::Default,
576            span: span,
577         })
578     }
579
580     fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
581         P(ast::Expr {
582             id: ast::DUMMY_NODE_ID,
583             node: node,
584             span: span,
585             attrs: None,
586         })
587     }
588
589     fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
590         self.expr(path.span, ast::ExprKind::Path(None, path))
591     }
592
593     /// Constructs a QPath expression.
594     fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
595         self.expr(span, ast::ExprKind::Path(Some(qself), path))
596     }
597
598     fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
599         self.expr_path(self.path_ident(span, id))
600     }
601     fn expr_self(&self, span: Span) -> P<ast::Expr> {
602         self.expr_ident(span, keywords::SelfValue.ident())
603     }
604
605     fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
606                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
607         self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
608     }
609
610     fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
611         self.expr_unary(sp, UnOp::Deref, e)
612     }
613     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
614         self.expr(sp, ast::ExprKind::Unary(op, e))
615     }
616
617     fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
618         let field_span = Span {
619             lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
620             hi: sp.hi,
621             expn_id: sp.expn_id,
622         };
623
624         let id = Spanned { node: ident, span: field_span };
625         self.expr(sp, ast::ExprKind::Field(expr, id))
626     }
627     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
628         let field_span = Span {
629             lo: sp.lo - Pos::from_usize(idx.to_string().len()),
630             hi: sp.hi,
631             expn_id: sp.expn_id,
632         };
633
634         let id = Spanned { node: idx, span: field_span };
635         self.expr(sp, ast::ExprKind::TupField(expr, id))
636     }
637     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
638         self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
639     }
640     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
641         self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
642     }
643
644     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
645         self.expr(span, ast::ExprKind::Call(expr, args))
646     }
647     fn expr_call_ident(&self, span: Span, id: ast::Ident,
648                        args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
649         self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
650     }
651     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
652                       args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
653         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
654         self.expr_call(sp, pathexpr, args)
655     }
656     fn expr_method_call(&self, span: Span,
657                         expr: P<ast::Expr>,
658                         ident: ast::Ident,
659                         mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
660         let id = Spanned { node: ident, span: span };
661         args.insert(0, expr);
662         self.expr(span, ast::ExprKind::MethodCall(id, Vec::new(), args))
663     }
664     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
665         self.expr(b.span, ast::ExprKind::Block(b))
666     }
667     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
668         ast::Field { ident: respan(span, name), expr: e, span: span }
669     }
670     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
671         self.expr(span, ast::ExprKind::Struct(path, fields, None))
672     }
673     fn expr_struct_ident(&self, span: Span,
674                          id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
675         self.expr_struct(span, self.path_ident(span, id), fields)
676     }
677
678     fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
679         self.expr(sp, ast::ExprKind::Lit(P(respan(sp, lit))))
680     }
681     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
682         self.expr_lit(span, ast::LitKind::Int(i as u64, ast::LitIntType::Unsigned(ast::UintTy::Us)))
683     }
684     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
685         if i < 0 {
686             let i = (-i) as u64;
687             let lit_ty = ast::LitIntType::Signed(ast::IntTy::Is);
688             let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
689             self.expr_unary(sp, ast::UnOp::Neg, lit)
690         } else {
691             self.expr_lit(sp, ast::LitKind::Int(i as u64, ast::LitIntType::Signed(ast::IntTy::Is)))
692         }
693     }
694     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
695         self.expr_lit(sp, ast::LitKind::Int(u as u64, ast::LitIntType::Unsigned(ast::UintTy::U32)))
696     }
697     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
698         self.expr_lit(sp, ast::LitKind::Int(u as u64, ast::LitIntType::Unsigned(ast::UintTy::U8)))
699     }
700     fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
701         self.expr_lit(sp, ast::LitKind::Bool(value))
702     }
703
704     fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
705         self.expr(sp, ast::ExprKind::Vec(exprs))
706     }
707     fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
708         self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
709                               Vec::new())
710     }
711     fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
712         self.expr_addr_of(sp, self.expr_vec(sp, exprs))
713     }
714     fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
715         self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
716     }
717
718     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
719         self.expr(sp, ast::ExprKind::Cast(expr, ty))
720     }
721
722
723     fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
724         let some = self.std_path(&["option", "Option", "Some"]);
725         self.expr_call_global(sp, some, vec!(expr))
726     }
727
728     fn expr_none(&self, sp: Span) -> P<ast::Expr> {
729         let none = self.std_path(&["option", "Option", "None"]);
730         let none = self.path_global(sp, none);
731         self.expr_path(none)
732     }
733
734
735     fn expr_break(&self, sp: Span) -> P<ast::Expr> {
736         self.expr(sp, ast::ExprKind::Break(None))
737     }
738
739
740     fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
741         self.expr(sp, ast::ExprKind::Tup(exprs))
742     }
743
744     fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
745         let loc = self.codemap().lookup_char_pos(span.lo);
746         let expr_file = self.expr_str(span,
747                                       token::intern_and_get_ident(&loc.file.name));
748         let expr_line = self.expr_u32(span, loc.line as u32);
749         let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
750         let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
751         self.expr_call_global(
752             span,
753             self.std_path(&["rt", "begin_panic"]),
754             vec!(
755                 self.expr_str(span, msg),
756                 expr_file_line_ptr))
757     }
758
759     fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
760         self.expr_fail(span,
761                        InternedString::new(
762                            "internal error: entered unreachable code"))
763     }
764
765     fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
766         let ok = self.std_path(&["result", "Result", "Ok"]);
767         self.expr_call_global(sp, ok, vec!(expr))
768     }
769
770     fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
771         let err = self.std_path(&["result", "Result", "Err"]);
772         self.expr_call_global(sp, err, vec!(expr))
773     }
774
775     fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
776         let ok = self.std_path(&["result", "Result", "Ok"]);
777         let ok_path = self.path_global(sp, ok);
778         let err = self.std_path(&["result", "Result", "Err"]);
779         let err_path = self.path_global(sp, err);
780
781         let binding_variable = self.ident_of("__try_var");
782         let binding_pat = self.pat_ident(sp, binding_variable);
783         let binding_expr = self.expr_ident(sp, binding_variable);
784
785         // Ok(__try_var) pattern
786         let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
787
788         // Err(__try_var)  (pattern and expression resp.)
789         let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat));
790         let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
791                                             vec!(binding_expr.clone()));
792         // return Err(__try_var)
793         let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
794
795         // Ok(__try_var) => __try_var
796         let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
797         // Err(__try_var) => return Err(__try_var)
798         let err_arm = self.arm(sp, vec!(err_pat), err_expr);
799
800         // match head { Ok() => ..., Err() => ... }
801         self.expr_match(sp, head, vec!(ok_arm, err_arm))
802     }
803
804
805     fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
806         P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
807     }
808     fn pat_wild(&self, span: Span) -> P<ast::Pat> {
809         self.pat(span, PatKind::Wild)
810     }
811     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
812         self.pat(span, PatKind::Lit(expr))
813     }
814     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
815         let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
816         self.pat_ident_binding_mode(span, ident, binding_mode)
817     }
818
819     fn pat_ident_binding_mode(&self,
820                               span: Span,
821                               ident: ast::Ident,
822                               bm: ast::BindingMode) -> P<ast::Pat> {
823         let pat = PatKind::Ident(bm, Spanned{span: span, node: ident}, None);
824         self.pat(span, pat)
825     }
826     fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
827         let pat = if subpats.is_empty() {
828             PatKind::Path(path)
829         } else {
830             PatKind::TupleStruct(path, subpats, None)
831         };
832         self.pat(span, pat)
833     }
834     fn pat_struct(&self, span: Span,
835                   path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
836         let pat = PatKind::Struct(path, field_pats, false);
837         self.pat(span, pat)
838     }
839     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
840         self.pat(span, PatKind::Tuple(pats, None))
841     }
842
843     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
844         let some = self.std_path(&["option", "Option", "Some"]);
845         let path = self.path_global(span, some);
846         self.pat_enum(span, path, vec!(pat))
847     }
848
849     fn pat_none(&self, span: Span) -> P<ast::Pat> {
850         let some = self.std_path(&["option", "Option", "None"]);
851         let path = self.path_global(span, some);
852         self.pat_enum(span, path, vec!())
853     }
854
855     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
856         let some = self.std_path(&["result", "Result", "Ok"]);
857         let path = self.path_global(span, some);
858         self.pat_enum(span, path, vec!(pat))
859     }
860
861     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
862         let some = self.std_path(&["result", "Result", "Err"]);
863         let path = self.path_global(span, some);
864         self.pat_enum(span, path, vec!(pat))
865     }
866
867     fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
868         ast::Arm {
869             attrs: vec!(),
870             pats: pats,
871             guard: None,
872             body: expr
873         }
874     }
875
876     fn arm_unreachable(&self, span: Span) -> ast::Arm {
877         self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
878     }
879
880     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
881         self.expr(span, ast::ExprKind::Match(arg, arms))
882     }
883
884     fn expr_if(&self, span: Span, cond: P<ast::Expr>,
885                then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
886         let els = els.map(|x| self.expr_block(self.block_expr(x)));
887         self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
888     }
889
890     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
891         self.expr(span, ast::ExprKind::Loop(block, None))
892     }
893
894     fn lambda_fn_decl(&self,
895                       span: Span,
896                       fn_decl: P<ast::FnDecl>,
897                       blk: P<ast::Block>,
898                       fn_decl_span: Span) // span of the `|...|` part
899                       -> P<ast::Expr> {
900         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
901                                                fn_decl,
902                                                blk,
903                                                fn_decl_span))
904     }
905
906     fn lambda(&self,
907               span: Span,
908               ids: Vec<ast::Ident>,
909               blk: P<ast::Block>)
910               -> P<ast::Expr> {
911         let fn_decl = self.fn_decl(
912             ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
913             self.ty_infer(span));
914
915         // FIXME -- We are using `span` as the span of the `|...|`
916         // part of the lambda, but it probably (maybe?) corresponds to
917         // the entire lambda body. Probably we should extend the API
918         // here, but that's not entirely clear.
919         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk, span))
920     }
921
922     fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
923         self.lambda(span, Vec::new(), blk)
924     }
925
926     fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
927         self.lambda(span, vec!(ident), blk)
928     }
929
930     fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
931                    expr: P<ast::Expr>) -> P<ast::Expr> {
932         self.lambda(span, ids, self.block_expr(expr))
933     }
934     fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
935         self.lambda0(span, self.block_expr(expr))
936     }
937     fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
938         self.lambda1(span, self.block_expr(expr), ident)
939     }
940
941     fn lambda_stmts(&self,
942                     span: Span,
943                     ids: Vec<ast::Ident>,
944                     stmts: Vec<ast::Stmt>)
945                     -> P<ast::Expr> {
946         self.lambda(span, ids, self.block(span, stmts))
947     }
948     fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
949         self.lambda0(span, self.block(span, stmts))
950     }
951     fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
952                       ident: ast::Ident) -> P<ast::Expr> {
953         self.lambda1(span, self.block(span, stmts), ident)
954     }
955
956     fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
957         let arg_pat = self.pat_ident(span, ident);
958         ast::Arg {
959             ty: ty,
960             pat: arg_pat,
961             id: ast::DUMMY_NODE_ID
962         }
963     }
964
965     // FIXME unused self
966     fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
967         P(ast::FnDecl {
968             inputs: inputs,
969             output: ast::FunctionRetTy::Ty(output),
970             variadic: false
971         })
972     }
973
974     fn item(&self, span: Span, name: Ident,
975             attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
976         // FIXME: Would be nice if our generated code didn't violate
977         // Rust coding conventions
978         P(ast::Item {
979             ident: name,
980             attrs: attrs,
981             id: ast::DUMMY_NODE_ID,
982             node: node,
983             vis: ast::Visibility::Inherited,
984             span: span
985         })
986     }
987
988     fn item_fn_poly(&self,
989                     span: Span,
990                     name: Ident,
991                     inputs: Vec<ast::Arg> ,
992                     output: P<ast::Ty>,
993                     generics: Generics,
994                     body: P<ast::Block>) -> P<ast::Item> {
995         self.item(span,
996                   name,
997                   Vec::new(),
998                   ast::ItemKind::Fn(self.fn_decl(inputs, output),
999                               ast::Unsafety::Normal,
1000                               ast::Constness::NotConst,
1001                               Abi::Rust,
1002                               generics,
1003                               body))
1004     }
1005
1006     fn item_fn(&self,
1007                span: Span,
1008                name: Ident,
1009                inputs: Vec<ast::Arg> ,
1010                output: P<ast::Ty>,
1011                body: P<ast::Block>
1012               ) -> P<ast::Item> {
1013         self.item_fn_poly(
1014             span,
1015             name,
1016             inputs,
1017             output,
1018             Generics::default(),
1019             body)
1020     }
1021
1022     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1023         let fields: Vec<_> = tys.into_iter().map(|ty| {
1024             ast::StructField {
1025                 span: ty.span,
1026                 ty: ty,
1027                 ident: None,
1028                 vis: ast::Visibility::Inherited,
1029                 attrs: Vec::new(),
1030                 id: ast::DUMMY_NODE_ID,
1031             }
1032         }).collect();
1033
1034         let vdata = if fields.is_empty() {
1035             ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1036         } else {
1037             ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1038         };
1039
1040         respan(span,
1041                ast::Variant_ {
1042                    name: name,
1043                    attrs: Vec::new(),
1044                    data: vdata,
1045                    disr_expr: None,
1046                })
1047     }
1048
1049     fn item_enum_poly(&self, span: Span, name: Ident,
1050                       enum_definition: ast::EnumDef,
1051                       generics: Generics) -> P<ast::Item> {
1052         self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1053     }
1054
1055     fn item_enum(&self, span: Span, name: Ident,
1056                  enum_definition: ast::EnumDef) -> P<ast::Item> {
1057         self.item_enum_poly(span, name, enum_definition,
1058                             Generics::default())
1059     }
1060
1061     fn item_struct(&self, span: Span, name: Ident,
1062                    struct_def: ast::VariantData) -> P<ast::Item> {
1063         self.item_struct_poly(
1064             span,
1065             name,
1066             struct_def,
1067             Generics::default()
1068         )
1069     }
1070
1071     fn item_struct_poly(&self, span: Span, name: Ident,
1072         struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1073         self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1074     }
1075
1076     fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1077                 attrs: Vec<ast::Attribute>,
1078                 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1079         self.item(
1080             span,
1081             name,
1082             attrs,
1083             ast::ItemKind::Mod(ast::Mod {
1084                 inner: inner_span,
1085                 items: items,
1086             })
1087         )
1088     }
1089
1090     fn item_static(&self,
1091                    span: Span,
1092                    name: Ident,
1093                    ty: P<ast::Ty>,
1094                    mutbl: ast::Mutability,
1095                    expr: P<ast::Expr>)
1096                    -> P<ast::Item> {
1097         self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1098     }
1099
1100     fn item_const(&self,
1101                   span: Span,
1102                   name: Ident,
1103                   ty: P<ast::Ty>,
1104                   expr: P<ast::Expr>)
1105                   -> P<ast::Item> {
1106         self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1107     }
1108
1109     fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1110                     generics: Generics) -> P<ast::Item> {
1111         self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1112     }
1113
1114     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1115         self.item_ty_poly(span, name, ty, Generics::default())
1116     }
1117
1118     fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1119         respan(sp, ast::Attribute_ {
1120             id: attr::mk_attr_id(),
1121             style: ast::AttrStyle::Outer,
1122             value: mi,
1123             is_sugared_doc: false,
1124         })
1125     }
1126
1127     fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1128         P(respan(sp, ast::MetaItemKind::Word(w)))
1129     }
1130     fn meta_list(&self,
1131                  sp: Span,
1132                  name: InternedString,
1133                  mis: Vec<P<ast::MetaItem>> )
1134                  -> P<ast::MetaItem> {
1135         P(respan(sp, ast::MetaItemKind::List(name, mis)))
1136     }
1137     fn meta_name_value(&self,
1138                        sp: Span,
1139                        name: InternedString,
1140                        value: ast::LitKind)
1141                        -> P<ast::MetaItem> {
1142         P(respan(sp, ast::MetaItemKind::NameValue(name, respan(sp, value))))
1143     }
1144
1145     fn item_use(&self, sp: Span,
1146                 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
1147         P(ast::Item {
1148             id: ast::DUMMY_NODE_ID,
1149             ident: keywords::Invalid.ident(),
1150             attrs: vec![],
1151             node: ast::ItemKind::Use(vp),
1152             vis: vis,
1153             span: sp
1154         })
1155     }
1156
1157     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1158         let last = path.segments.last().unwrap().identifier;
1159         self.item_use_simple_(sp, vis, last, path)
1160     }
1161
1162     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1163                         ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
1164         self.item_use(sp, vis,
1165                       P(respan(sp,
1166                                ast::ViewPathSimple(ident,
1167                                                    path))))
1168     }
1169
1170     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1171                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1172         let imports = imports.iter().map(|id| {
1173             let item = ast::PathListItemKind::Ident {
1174                 name: *id,
1175                 rename: None,
1176                 id: ast::DUMMY_NODE_ID,
1177             };
1178             respan(sp, item)
1179         }).collect();
1180
1181         self.item_use(sp, vis,
1182                       P(respan(sp,
1183                                ast::ViewPathList(self.path(sp, path),
1184                                                  imports))))
1185     }
1186
1187     fn item_use_glob(&self, sp: Span,
1188                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1189         self.item_use(sp, vis,
1190                       P(respan(sp,
1191                                ast::ViewPathGlob(self.path(sp, path)))))
1192     }
1193 }