]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/build.rs
label-break-value: Parsing and AST/HIR changes
[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 rustc_target::spec::abi::Abi;
12 use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
13 use attr;
14 use syntax_pos::{Pos, Span, DUMMY_SP};
15 use codemap::{dummy_spanned, respan, Spanned};
16 use ext::base::ExtCtxt;
17 use ptr::P;
18 use symbol::{Symbol, keywords};
19
20 // Transitional re-exports 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, path: ast::Path) -> P<ast::Ty>;
56     fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
57
58     fn ty_rptr(&self, span: Span,
59                ty: P<ast::Ty>,
60                lifetime: Option<ast::Lifetime>,
61                mutbl: ast::Mutability) -> P<ast::Ty>;
62     fn ty_ptr(&self, span: Span,
63               ty: P<ast::Ty>,
64               mutbl: ast::Mutability) -> P<ast::Ty>;
65
66     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
67     fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
68
69     fn typaram(&self,
70                span: Span,
71                id: ast::Ident,
72                attrs: Vec<ast::Attribute>,
73                bounds: ast::TyParamBounds,
74                default: Option<P<ast::Ty>>) -> ast::TyParam;
75
76     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
77     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
78     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
79     fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime;
80     fn lifetime_def(&self,
81                     span: Span,
82                     ident: ast::Ident,
83                     attrs: Vec<ast::Attribute>,
84                     bounds: Vec<ast::Lifetime>)
85                     -> ast::LifetimeDef;
86
87     // statements
88     fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
89     fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
90     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
91     fn stmt_let_typed(&self,
92                       sp: Span,
93                       mutbl: bool,
94                       ident: ast::Ident,
95                       typ: P<ast::Ty>,
96                       ex: P<ast::Expr>)
97                       -> ast::Stmt;
98     fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
99     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
100
101     // blocks
102     fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
103     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
104
105     // expressions
106     fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
107     fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
108     fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
109     fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
110
111     fn expr_self(&self, span: Span) -> P<ast::Expr>;
112     fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
113                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
114     fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
115     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
116
117     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
118     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
119     fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
120     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
121                              idx: usize) -> P<ast::Expr>;
122     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
123     fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
124     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
125                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
126     fn expr_method_call(&self, span: Span,
127                         expr: P<ast::Expr>, ident: ast::Ident,
128                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
129     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
130     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
131
132     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
133     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
134     fn expr_struct_ident(&self, span: Span, id: ast::Ident,
135                          fields: Vec<ast::Field>) -> P<ast::Expr>;
136
137     fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
138
139     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
140     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
141     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
142     fn expr_u16(&self, sp: Span, u: u16) -> 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: Symbol) -> 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: Symbol) -> 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                       body: P<ast::Expr>,
199                       fn_decl_span: Span)
200                       -> P<ast::Expr>;
201
202     fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr>;
203     fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr>;
204     fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
205
206     fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
207                     blk: Vec<ast::Stmt>) -> P<ast::Expr>;
208     fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
209     fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
210                       ident: ast::Ident) -> P<ast::Expr>;
211
212     // items
213     fn item(&self, span: Span,
214             name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
215
216     fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
217     // FIXME unused self
218     fn fn_decl(&self, inputs: Vec<ast::Arg> , output: ast::FunctionRetTy) -> P<ast::FnDecl>;
219
220     fn item_fn_poly(&self,
221                     span: Span,
222                     name: Ident,
223                     inputs: Vec<ast::Arg> ,
224                     output: P<ast::Ty>,
225                     generics: Generics,
226                     body: P<ast::Block>) -> P<ast::Item>;
227     fn item_fn(&self,
228                span: Span,
229                name: Ident,
230                inputs: Vec<ast::Arg> ,
231                output: P<ast::Ty>,
232                body: P<ast::Block>) -> P<ast::Item>;
233
234     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
235     fn item_enum_poly(&self,
236                       span: Span,
237                       name: Ident,
238                       enum_definition: ast::EnumDef,
239                       generics: Generics) -> P<ast::Item>;
240     fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
241
242     fn item_struct_poly(&self,
243                         span: Span,
244                         name: Ident,
245                         struct_def: ast::VariantData,
246                         generics: Generics) -> P<ast::Item>;
247     fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
248
249     fn item_mod(&self, span: Span, inner_span: Span,
250                 name: Ident, attrs: Vec<ast::Attribute>,
251                 items: Vec<P<ast::Item>>) -> P<ast::Item>;
252
253     fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item>;
254
255     fn item_static(&self,
256                    span: Span,
257                    name: Ident,
258                    ty: P<ast::Ty>,
259                    mutbl: ast::Mutability,
260                    expr: P<ast::Expr>)
261                    -> P<ast::Item>;
262
263     fn item_const(&self,
264                    span: Span,
265                    name: Ident,
266                    ty: P<ast::Ty>,
267                    expr: P<ast::Expr>)
268                    -> P<ast::Item>;
269
270     fn item_ty_poly(&self,
271                     span: Span,
272                     name: Ident,
273                     ty: P<ast::Ty>,
274                     generics: Generics) -> P<ast::Item>;
275     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
276
277     fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute;
278
279     fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem;
280
281     fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem;
282
283     fn meta_list(&self,
284                  sp: Span,
285                  name: ast::Name,
286                  mis: Vec<ast::NestedMetaItem> )
287                  -> ast::MetaItem;
288     fn meta_name_value(&self,
289                        sp: Span,
290                        name: ast::Name,
291                        value: ast::LitKind)
292                        -> ast::MetaItem;
293
294     fn item_use(&self, sp: Span,
295                 vis: ast::Visibility, vp: P<ast::UseTree>) -> 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: Option<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                 span: 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_ident = idents.pop().unwrap();
324         let mut segments: Vec<ast::PathSegment> = Vec::new();
325
326         segments.extend(idents.into_iter().map(|ident| {
327             ast::PathSegment::from_ident(ident.with_span_pos(span))
328         }));
329         let parameters = if !lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty() {
330             ast::AngleBracketedParameterData { lifetimes, types, bindings, span }.into()
331         } else {
332             None
333         };
334         segments.push(ast::PathSegment { ident: last_ident.with_span_pos(span), parameters });
335         let mut path = ast::Path { span, segments };
336         if global {
337             if let Some(seg) = path.make_root() {
338                 path.segments.insert(0, seg);
339             }
340         }
341         path
342     }
343
344     /// Constructs a qualified path.
345     ///
346     /// Constructs a path like `<self_type as trait_path>::ident`.
347     fn qpath(&self,
348              self_type: P<ast::Ty>,
349              trait_path: ast::Path,
350              ident: ast::Ident)
351              -> (ast::QSelf, ast::Path) {
352         self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
353     }
354
355     /// Constructs a qualified path.
356     ///
357     /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
358     fn qpath_all(&self,
359                  self_type: P<ast::Ty>,
360                  trait_path: ast::Path,
361                  ident: ast::Ident,
362                  lifetimes: Vec<ast::Lifetime>,
363                  types: Vec<P<ast::Ty>>,
364                  bindings: Vec<ast::TypeBinding>)
365                  -> (ast::QSelf, ast::Path) {
366         let mut path = trait_path;
367         let parameters = if !lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty() {
368             ast::AngleBracketedParameterData { lifetimes, types, bindings, span: ident.span }.into()
369         } else {
370             None
371         };
372         path.segments.push(ast::PathSegment { ident, parameters });
373
374         (ast::QSelf {
375             ty: self_type,
376             position: path.segments.len() - 1
377         }, path)
378     }
379
380     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
381         ast::MutTy {
382             ty,
383             mutbl,
384         }
385     }
386
387     fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
388         P(ast::Ty {
389             id: ast::DUMMY_NODE_ID,
390             span,
391             node: ty
392         })
393     }
394
395     fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
396         self.ty(path.span, ast::TyKind::Path(None, path))
397     }
398
399     // Might need to take bounds as an argument in the future, if you ever want
400     // to generate a bounded existential trait type.
401     fn ty_ident(&self, span: Span, ident: ast::Ident)
402         -> P<ast::Ty> {
403         self.ty_path(self.path_ident(span, ident))
404     }
405
406     fn ty_rptr(&self,
407                span: Span,
408                ty: P<ast::Ty>,
409                lifetime: Option<ast::Lifetime>,
410                mutbl: ast::Mutability)
411         -> P<ast::Ty> {
412         self.ty(span,
413                 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
414     }
415
416     fn ty_ptr(&self,
417               span: Span,
418               ty: P<ast::Ty>,
419               mutbl: ast::Mutability)
420         -> P<ast::Ty> {
421         self.ty(span,
422                 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
423     }
424
425     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
426         self.ty_path(
427             self.path_all(DUMMY_SP,
428                           true,
429                           self.std_path(&["option", "Option"]),
430                           Vec::new(),
431                           vec![ ty ],
432                           Vec::new()))
433     }
434
435     fn ty_infer(&self, span: Span) -> P<ast::Ty> {
436         self.ty(span, ast::TyKind::Infer)
437     }
438
439     fn typaram(&self,
440                span: Span,
441                ident: ast::Ident,
442                attrs: Vec<ast::Attribute>,
443                bounds: ast::TyParamBounds,
444                default: Option<P<ast::Ty>>) -> ast::TyParam {
445         ast::TyParam {
446             ident: ident.with_span_pos(span),
447             id: ast::DUMMY_NODE_ID,
448             attrs: attrs.into(),
449             bounds,
450             default,
451         }
452     }
453
454     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
455         ast::TraitRef {
456             path,
457             ref_id: ast::DUMMY_NODE_ID,
458         }
459     }
460
461     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
462         ast::PolyTraitRef {
463             bound_generic_params: Vec::new(),
464             trait_ref: self.trait_ref(path),
465             span,
466         }
467     }
468
469     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
470         ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
471     }
472
473     fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
474         ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
475     }
476
477     fn lifetime_def(&self,
478                     span: Span,
479                     ident: ast::Ident,
480                     attrs: Vec<ast::Attribute>,
481                     bounds: Vec<ast::Lifetime>)
482                     -> ast::LifetimeDef {
483         ast::LifetimeDef {
484             attrs: attrs.into(),
485             lifetime: self.lifetime(span, ident),
486             bounds,
487         }
488     }
489
490     fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
491         ast::Stmt {
492             id: ast::DUMMY_NODE_ID,
493             span: expr.span,
494             node: ast::StmtKind::Expr(expr),
495         }
496     }
497
498     fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
499         ast::Stmt {
500             id: ast::DUMMY_NODE_ID,
501             span: expr.span,
502             node: ast::StmtKind::Semi(expr),
503         }
504     }
505
506     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
507                 ex: P<ast::Expr>) -> ast::Stmt {
508         let pat = if mutbl {
509             let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
510             self.pat_ident_binding_mode(sp, ident, binding_mode)
511         } else {
512             self.pat_ident(sp, ident)
513         };
514         let local = P(ast::Local {
515             pat,
516             ty: None,
517             init: Some(ex),
518             id: ast::DUMMY_NODE_ID,
519             span: sp,
520             attrs: ast::ThinVec::new(),
521         });
522         ast::Stmt {
523             id: ast::DUMMY_NODE_ID,
524             node: ast::StmtKind::Local(local),
525             span: sp,
526         }
527     }
528
529     fn stmt_let_typed(&self,
530                       sp: Span,
531                       mutbl: bool,
532                       ident: ast::Ident,
533                       typ: P<ast::Ty>,
534                       ex: P<ast::Expr>)
535                       -> ast::Stmt {
536         let pat = if mutbl {
537             let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
538             self.pat_ident_binding_mode(sp, ident, binding_mode)
539         } else {
540             self.pat_ident(sp, ident)
541         };
542         let local = P(ast::Local {
543             pat,
544             ty: Some(typ),
545             init: Some(ex),
546             id: ast::DUMMY_NODE_ID,
547             span: sp,
548             attrs: ast::ThinVec::new(),
549         });
550         ast::Stmt {
551             id: ast::DUMMY_NODE_ID,
552             node: ast::StmtKind::Local(local),
553             span: sp,
554         }
555     }
556
557     // Generate `let _: Type;`, usually used for type assertions.
558     fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
559         let local = P(ast::Local {
560             pat: self.pat_wild(span),
561             ty: Some(ty),
562             init: None,
563             id: ast::DUMMY_NODE_ID,
564             span,
565             attrs: ast::ThinVec::new(),
566         });
567         ast::Stmt {
568             id: ast::DUMMY_NODE_ID,
569             node: ast::StmtKind::Local(local),
570             span,
571         }
572     }
573
574     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
575         ast::Stmt {
576             id: ast::DUMMY_NODE_ID,
577             node: ast::StmtKind::Item(item),
578             span: sp,
579         }
580     }
581
582     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
583         self.block(expr.span, vec![ast::Stmt {
584             id: ast::DUMMY_NODE_ID,
585             span: expr.span,
586             node: ast::StmtKind::Expr(expr),
587         }])
588     }
589     fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
590         P(ast::Block {
591            stmts,
592            id: ast::DUMMY_NODE_ID,
593            rules: BlockCheckMode::Default,
594            span,
595            recovered: false,
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,
603             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         self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
638     }
639     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
640         let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
641         self.expr(sp, ast::ExprKind::Field(expr, ident))
642     }
643     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
644         self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
645     }
646     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
647         self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
648     }
649
650     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
651         self.expr(span, ast::ExprKind::Call(expr, args))
652     }
653     fn expr_call_ident(&self, span: Span, id: ast::Ident,
654                        args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
655         self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
656     }
657     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
658                       args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
659         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
660         self.expr_call(sp, pathexpr, args)
661     }
662     fn expr_method_call(&self, span: Span,
663                         expr: P<ast::Expr>,
664                         ident: ast::Ident,
665                         mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
666         args.insert(0, expr);
667         let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
668         self.expr(span, ast::ExprKind::MethodCall(segment, args))
669     }
670     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
671         self.expr(b.span, ast::ExprKind::Block(b, None))
672     }
673     fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
674         ast::Field {
675             ident: ident.with_span_pos(span),
676             expr: e,
677             span,
678             is_shorthand: false,
679             attrs: ast::ThinVec::new(),
680         }
681     }
682     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
683         self.expr(span, ast::ExprKind::Struct(path, fields, None))
684     }
685     fn expr_struct_ident(&self, span: Span,
686                          id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
687         self.expr_struct(span, self.path_ident(span, id), fields)
688     }
689
690     fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
691         self.expr(sp, ast::ExprKind::Lit(P(respan(sp, lit))))
692     }
693     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
694         self.expr_lit(span, ast::LitKind::Int(i as u128,
695                                               ast::LitIntType::Unsigned(ast::UintTy::Usize)))
696     }
697     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
698         if i < 0 {
699             let i = (-i) as u128;
700             let lit_ty = ast::LitIntType::Signed(ast::IntTy::Isize);
701             let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
702             self.expr_unary(sp, ast::UnOp::Neg, lit)
703         } else {
704             self.expr_lit(sp, ast::LitKind::Int(i as u128,
705                                                 ast::LitIntType::Signed(ast::IntTy::Isize)))
706         }
707     }
708     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
709         self.expr_lit(sp, ast::LitKind::Int(u as u128,
710                                             ast::LitIntType::Unsigned(ast::UintTy::U32)))
711     }
712     fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
713         self.expr_lit(sp, ast::LitKind::Int(u as u128,
714                                             ast::LitIntType::Unsigned(ast::UintTy::U16)))
715     }
716     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
717         self.expr_lit(sp, ast::LitKind::Int(u as u128, 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::Array(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: Symbol) -> 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, 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: Symbol) -> P<ast::Expr> {
764         let loc = self.codemap().lookup_char_pos(span.lo());
765         let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
766         let expr_line = self.expr_u32(span, loc.line as u32);
767         let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
768         let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
769         let expr_loc_ptr = self.expr_addr_of(span, expr_loc_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_loc_ptr])
776     }
777
778     fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
779         self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
780     }
781
782     fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
783         let ok = self.std_path(&["result", "Result", "Ok"]);
784         self.expr_call_global(sp, ok, vec![expr])
785     }
786
787     fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
788         let err = self.std_path(&["result", "Result", "Err"]);
789         self.expr_call_global(sp, err, vec![expr])
790     }
791
792     fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
793         let ok = self.std_path(&["result", "Result", "Ok"]);
794         let ok_path = self.path_global(sp, ok);
795         let err = self.std_path(&["result", "Result", "Err"]);
796         let err_path = self.path_global(sp, err);
797
798         let binding_variable = self.ident_of("__try_var");
799         let binding_pat = self.pat_ident(sp, binding_variable);
800         let binding_expr = self.expr_ident(sp, binding_variable);
801
802         // Ok(__try_var) pattern
803         let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
804
805         // Err(__try_var)  (pattern and expression resp.)
806         let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
807         let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
808                                             vec![binding_expr.clone()]);
809         // return Err(__try_var)
810         let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
811
812         // Ok(__try_var) => __try_var
813         let ok_arm = self.arm(sp, vec![ok_pat], binding_expr);
814         // Err(__try_var) => return Err(__try_var)
815         let err_arm = self.arm(sp, vec![err_pat], err_expr);
816
817         // match head { Ok() => ..., Err() => ... }
818         self.expr_match(sp, head, vec![ok_arm, err_arm])
819     }
820
821
822     fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
823         P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
824     }
825     fn pat_wild(&self, span: Span) -> P<ast::Pat> {
826         self.pat(span, PatKind::Wild)
827     }
828     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
829         self.pat(span, PatKind::Lit(expr))
830     }
831     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
832         let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
833         self.pat_ident_binding_mode(span, ident, binding_mode)
834     }
835
836     fn pat_ident_binding_mode(&self,
837                               span: Span,
838                               ident: ast::Ident,
839                               bm: ast::BindingMode) -> P<ast::Pat> {
840         let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
841         self.pat(span, pat)
842     }
843     fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
844         self.pat(span, PatKind::Path(None, path))
845     }
846     fn pat_tuple_struct(&self, span: Span, path: ast::Path,
847                         subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
848         self.pat(span, PatKind::TupleStruct(path, subpats, None))
849     }
850     fn pat_struct(&self, span: Span, path: ast::Path,
851                   field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
852         self.pat(span, PatKind::Struct(path, field_pats, false))
853     }
854     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
855         self.pat(span, PatKind::Tuple(pats, None))
856     }
857
858     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
859         let some = self.std_path(&["option", "Option", "Some"]);
860         let path = self.path_global(span, some);
861         self.pat_tuple_struct(span, path, vec![pat])
862     }
863
864     fn pat_none(&self, span: Span) -> P<ast::Pat> {
865         let some = self.std_path(&["option", "Option", "None"]);
866         let path = self.path_global(span, some);
867         self.pat_path(span, path)
868     }
869
870     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
871         let some = self.std_path(&["result", "Result", "Ok"]);
872         let path = self.path_global(span, some);
873         self.pat_tuple_struct(span, path, vec![pat])
874     }
875
876     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
877         let some = self.std_path(&["result", "Result", "Err"]);
878         let path = self.path_global(span, some);
879         self.pat_tuple_struct(span, path, vec![pat])
880     }
881
882     fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
883         ast::Arm {
884             attrs: vec![],
885             pats,
886             guard: None,
887             body: expr,
888         }
889     }
890
891     fn arm_unreachable(&self, span: Span) -> ast::Arm {
892         self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span))
893     }
894
895     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
896         self.expr(span, ast::ExprKind::Match(arg, arms))
897     }
898
899     fn expr_if(&self, span: Span, cond: P<ast::Expr>,
900                then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
901         let els = els.map(|x| self.expr_block(self.block_expr(x)));
902         self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
903     }
904
905     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
906         self.expr(span, ast::ExprKind::Loop(block, None))
907     }
908
909     fn lambda_fn_decl(&self,
910                       span: Span,
911                       fn_decl: P<ast::FnDecl>,
912                       body: P<ast::Expr>,
913                       fn_decl_span: Span) // span of the `|...|` part
914                       -> P<ast::Expr> {
915         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
916                                                ast::Movability::Movable,
917                                                fn_decl,
918                                                body,
919                                                fn_decl_span))
920     }
921
922     fn lambda(&self,
923               span: Span,
924               ids: Vec<ast::Ident>,
925               body: P<ast::Expr>)
926               -> P<ast::Expr> {
927         let fn_decl = self.fn_decl(
928             ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
929             ast::FunctionRetTy::Default(span));
930
931         // FIXME -- We are using `span` as the span of the `|...|`
932         // part of the lambda, but it probably (maybe?) corresponds to
933         // the entire lambda body. Probably we should extend the API
934         // here, but that's not entirely clear.
935         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
936                                                ast::Movability::Movable,
937                                                fn_decl,
938                                                body,
939                                                span))
940     }
941
942     fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
943         self.lambda(span, Vec::new(), body)
944     }
945
946     fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
947         self.lambda(span, vec![ident], body)
948     }
949
950     fn lambda_stmts(&self,
951                     span: Span,
952                     ids: Vec<ast::Ident>,
953                     stmts: Vec<ast::Stmt>)
954                     -> P<ast::Expr> {
955         self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
956     }
957     fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
958         self.lambda0(span, self.expr_block(self.block(span, stmts)))
959     }
960     fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
961                       ident: ast::Ident) -> P<ast::Expr> {
962         self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
963     }
964
965     fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
966         let arg_pat = self.pat_ident(span, ident);
967         ast::Arg {
968             ty,
969             pat: arg_pat,
970             id: ast::DUMMY_NODE_ID
971         }
972     }
973
974     // FIXME unused self
975     fn fn_decl(&self, inputs: Vec<ast::Arg>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
976         P(ast::FnDecl {
977             inputs,
978             output,
979             variadic: false
980         })
981     }
982
983     fn item(&self, span: Span, name: Ident,
984             attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
985         // FIXME: Would be nice if our generated code didn't violate
986         // Rust coding conventions
987         P(ast::Item {
988             ident: name,
989             attrs,
990             id: ast::DUMMY_NODE_ID,
991             node,
992             vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
993             span,
994             tokens: None,
995         })
996     }
997
998     fn item_fn_poly(&self,
999                     span: Span,
1000                     name: Ident,
1001                     inputs: Vec<ast::Arg> ,
1002                     output: P<ast::Ty>,
1003                     generics: Generics,
1004                     body: P<ast::Block>) -> P<ast::Item> {
1005         self.item(span,
1006                   name,
1007                   Vec::new(),
1008                   ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
1009                               ast::Unsafety::Normal,
1010                               dummy_spanned(ast::Constness::NotConst),
1011                               Abi::Rust,
1012                               generics,
1013                               body))
1014     }
1015
1016     fn item_fn(&self,
1017                span: Span,
1018                name: Ident,
1019                inputs: Vec<ast::Arg> ,
1020                output: P<ast::Ty>,
1021                body: P<ast::Block>
1022               ) -> P<ast::Item> {
1023         self.item_fn_poly(
1024             span,
1025             name,
1026             inputs,
1027             output,
1028             Generics::default(),
1029             body)
1030     }
1031
1032     fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1033         let fields: Vec<_> = tys.into_iter().map(|ty| {
1034             ast::StructField {
1035                 span: ty.span,
1036                 ty,
1037                 ident: None,
1038                 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1039                 attrs: Vec::new(),
1040                 id: ast::DUMMY_NODE_ID,
1041             }
1042         }).collect();
1043
1044         let vdata = if fields.is_empty() {
1045             ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1046         } else {
1047             ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1048         };
1049
1050         respan(span,
1051                ast::Variant_ {
1052                    ident,
1053                    attrs: Vec::new(),
1054                    data: vdata,
1055                    disr_expr: None,
1056                })
1057     }
1058
1059     fn item_enum_poly(&self, span: Span, name: Ident,
1060                       enum_definition: ast::EnumDef,
1061                       generics: Generics) -> P<ast::Item> {
1062         self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1063     }
1064
1065     fn item_enum(&self, span: Span, name: Ident,
1066                  enum_definition: ast::EnumDef) -> P<ast::Item> {
1067         self.item_enum_poly(span, name, enum_definition,
1068                             Generics::default())
1069     }
1070
1071     fn item_struct(&self, span: Span, name: Ident,
1072                    struct_def: ast::VariantData) -> P<ast::Item> {
1073         self.item_struct_poly(
1074             span,
1075             name,
1076             struct_def,
1077             Generics::default()
1078         )
1079     }
1080
1081     fn item_struct_poly(&self, span: Span, name: Ident,
1082         struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1083         self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1084     }
1085
1086     fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1087                 attrs: Vec<ast::Attribute>,
1088                 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1089         self.item(
1090             span,
1091             name,
1092             attrs,
1093             ast::ItemKind::Mod(ast::Mod {
1094                 inner: inner_span,
1095                 items,
1096             })
1097         )
1098     }
1099
1100     fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item> {
1101         self.item(span, name, Vec::new(), ast::ItemKind::ExternCrate(None))
1102     }
1103
1104     fn item_static(&self,
1105                    span: Span,
1106                    name: Ident,
1107                    ty: P<ast::Ty>,
1108                    mutbl: ast::Mutability,
1109                    expr: P<ast::Expr>)
1110                    -> P<ast::Item> {
1111         self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1112     }
1113
1114     fn item_const(&self,
1115                   span: Span,
1116                   name: Ident,
1117                   ty: P<ast::Ty>,
1118                   expr: P<ast::Expr>)
1119                   -> P<ast::Item> {
1120         self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1121     }
1122
1123     fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1124                     generics: Generics) -> P<ast::Item> {
1125         self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1126     }
1127
1128     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1129         self.item_ty_poly(span, name, ty, Generics::default())
1130     }
1131
1132     fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute {
1133         attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
1134     }
1135
1136     fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
1137         attr::mk_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1138     }
1139
1140     fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
1141         attr::mk_nested_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1142     }
1143
1144     fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
1145                  -> ast::MetaItem {
1146         attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
1147     }
1148
1149     fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
1150                        -> ast::MetaItem {
1151         attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp),
1152                                  respan(sp, value))
1153     }
1154
1155     fn item_use(&self, sp: Span,
1156                 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item> {
1157         P(ast::Item {
1158             id: ast::DUMMY_NODE_ID,
1159             ident: keywords::Invalid.ident(),
1160             attrs: vec![],
1161             node: ast::ItemKind::Use(vp),
1162             vis,
1163             span: sp,
1164             tokens: None,
1165         })
1166     }
1167
1168     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1169         self.item_use_simple_(sp, vis, None, path)
1170     }
1171
1172     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1173                         rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
1174         self.item_use(sp, vis, P(ast::UseTree {
1175             span: sp,
1176             prefix: path,
1177             kind: ast::UseTreeKind::Simple(rename),
1178         }))
1179     }
1180
1181     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1182                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1183         let imports = imports.iter().map(|id| {
1184             (ast::UseTree {
1185                 span: sp,
1186                 prefix: self.path(sp, vec![*id]),
1187                 kind: ast::UseTreeKind::Simple(None),
1188             }, ast::DUMMY_NODE_ID)
1189         }).collect();
1190
1191         self.item_use(sp, vis, P(ast::UseTree {
1192             span: sp,
1193             prefix: self.path(sp, path),
1194             kind: ast::UseTreeKind::Nested(imports),
1195         }))
1196     }
1197
1198     fn item_use_glob(&self, sp: Span,
1199                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1200         self.item_use(sp, vis, P(ast::UseTree {
1201             span: sp,
1202             prefix: self.path(sp, path),
1203             kind: ast::UseTreeKind::Glob,
1204         }))
1205     }
1206 }