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