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