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