]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/build.rs
Auto merge of #51678 - Zoxc:combine-lints, r=estebank
[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                 args: Vec<ast::GenericArg>,
35                 bindings: Vec<ast::TypeBinding>)
36         -> ast::Path;
37
38     fn qpath(&self, self_type: P<ast::Ty>,
39              trait_path: ast::Path,
40              ident: ast::Ident)
41              -> (ast::QSelf, ast::Path);
42     fn qpath_all(&self, self_type: P<ast::Ty>,
43                 trait_path: ast::Path,
44                 ident: ast::Ident,
45                 args: Vec<ast::GenericArg>,
46                 bindings: Vec<ast::TypeBinding>)
47                 -> (ast::QSelf, ast::Path);
48
49     // types
50     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
51
52     fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
53     fn ty_path(&self, path: ast::Path) -> P<ast::Ty>;
54     fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
55
56     fn ty_rptr(&self, span: Span,
57                ty: P<ast::Ty>,
58                lifetime: Option<ast::Lifetime>,
59                mutbl: ast::Mutability) -> P<ast::Ty>;
60     fn ty_ptr(&self, span: Span,
61               ty: P<ast::Ty>,
62               mutbl: ast::Mutability) -> P<ast::Ty>;
63
64     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
65     fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
66
67     fn typaram(&self,
68                span: Span,
69                id: ast::Ident,
70                attrs: Vec<ast::Attribute>,
71                bounds: ast::GenericBounds,
72                default: Option<P<ast::Ty>>) -> ast::GenericParam;
73
74     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
75     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
76     fn trait_bound(&self, path: ast::Path) -> ast::GenericBound;
77     fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime;
78     fn lifetime_def(&self,
79                     span: Span,
80                     ident: ast::Ident,
81                     attrs: Vec<ast::Attribute>,
82                     bounds: ast::GenericBounds)
83                     -> ast::GenericParam;
84
85     // statements
86     fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
87     fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
88     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
89     fn stmt_let_typed(&self,
90                       sp: Span,
91                       mutbl: bool,
92                       ident: ast::Ident,
93                       typ: P<ast::Ty>,
94                       ex: P<ast::Expr>)
95                       -> ast::Stmt;
96     fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
97     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
98
99     // blocks
100     fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
101     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
102
103     // expressions
104     fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
105     fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
106     fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
107     fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
108
109     fn expr_self(&self, span: Span) -> P<ast::Expr>;
110     fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
111                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
112     fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
113     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
114
115     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
116     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
117     fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
118     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
119                              idx: usize) -> P<ast::Expr>;
120     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
121     fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
122     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
123                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
124     fn expr_method_call(&self, span: Span,
125                         expr: P<ast::Expr>, ident: ast::Ident,
126                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
127     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
128     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
129
130     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
131     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
132     fn expr_struct_ident(&self, span: Span, id: ast::Ident,
133                          fields: Vec<ast::Field>) -> P<ast::Expr>;
134
135     fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
136
137     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
138     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
139     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
140     fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr>;
141     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
142     fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
143
144     fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
145     fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
146     fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
147     fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr>;
148
149     fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
150     fn expr_none(&self, sp: Span) -> P<ast::Expr>;
151
152     fn expr_break(&self, sp: Span) -> P<ast::Expr>;
153
154     fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
155
156     fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr>;
157     fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
158
159     fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
160     fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
161     fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
162
163     fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
164     fn pat_wild(&self, span: Span) -> P<ast::Pat>;
165     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
166     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
167
168     fn pat_ident_binding_mode(&self,
169                               span: Span,
170                               ident: ast::Ident,
171                               bm: ast::BindingMode) -> P<ast::Pat>;
172     fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat>;
173     fn pat_tuple_struct(&self, span: Span, path: ast::Path,
174                         subpats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
175     fn pat_struct(&self, span: Span, path: ast::Path,
176                   field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat>;
177     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
178
179     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
180     fn pat_none(&self, span: Span) -> P<ast::Pat>;
181
182     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
183     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
184
185     fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
186     fn arm_unreachable(&self, span: Span) -> ast::Arm;
187
188     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
189     fn expr_if(&self, span: Span,
190                cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
191     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
192
193     fn lambda_fn_decl(&self,
194                       span: Span,
195                       fn_decl: P<ast::FnDecl>,
196                       body: P<ast::Expr>,
197                       fn_decl_span: Span)
198                       -> P<ast::Expr>;
199
200     fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr>;
201     fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr>;
202     fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
203
204     fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
205                     blk: Vec<ast::Stmt>) -> P<ast::Expr>;
206     fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
207     fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
208                       ident: ast::Ident) -> P<ast::Expr>;
209
210     // items
211     fn item(&self, span: Span,
212             name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
213
214     fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
215     // FIXME unused self
216     fn fn_decl(&self, inputs: Vec<ast::Arg> , output: ast::FunctionRetTy) -> P<ast::FnDecl>;
217
218     fn item_fn_poly(&self,
219                     span: Span,
220                     name: Ident,
221                     inputs: Vec<ast::Arg> ,
222                     output: P<ast::Ty>,
223                     generics: Generics,
224                     body: P<ast::Block>) -> P<ast::Item>;
225     fn item_fn(&self,
226                span: Span,
227                name: Ident,
228                inputs: Vec<ast::Arg> ,
229                output: P<ast::Ty>,
230                body: P<ast::Block>) -> P<ast::Item>;
231
232     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
233     fn item_enum_poly(&self,
234                       span: Span,
235                       name: Ident,
236                       enum_definition: ast::EnumDef,
237                       generics: Generics) -> P<ast::Item>;
238     fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
239
240     fn item_struct_poly(&self,
241                         span: Span,
242                         name: Ident,
243                         struct_def: ast::VariantData,
244                         generics: Generics) -> P<ast::Item>;
245     fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
246
247     fn item_mod(&self, span: Span, inner_span: Span,
248                 name: Ident, attrs: Vec<ast::Attribute>,
249                 items: Vec<P<ast::Item>>) -> P<ast::Item>;
250
251     fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item>;
252
253     fn item_static(&self,
254                    span: Span,
255                    name: Ident,
256                    ty: P<ast::Ty>,
257                    mutbl: ast::Mutability,
258                    expr: P<ast::Expr>)
259                    -> P<ast::Item>;
260
261     fn item_const(&self,
262                    span: Span,
263                    name: Ident,
264                    ty: P<ast::Ty>,
265                    expr: P<ast::Expr>)
266                    -> P<ast::Item>;
267
268     fn item_ty_poly(&self,
269                     span: Span,
270                     name: Ident,
271                     ty: P<ast::Ty>,
272                     generics: Generics) -> P<ast::Item>;
273     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
274
275     fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute;
276
277     fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem;
278
279     fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem;
280
281     fn meta_list(&self,
282                  sp: Span,
283                  name: ast::Name,
284                  mis: Vec<ast::NestedMetaItem> )
285                  -> ast::MetaItem;
286     fn meta_name_value(&self,
287                        sp: Span,
288                        name: ast::Name,
289                        value: ast::LitKind)
290                        -> ast::MetaItem;
291
292     fn item_use(&self, sp: Span,
293                 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item>;
294     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
295     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
296                         ident: Option<ast::Ident>, path: ast::Path) -> P<ast::Item>;
297     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
298                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
299     fn item_use_glob(&self, sp: Span,
300                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
301 }
302
303 impl<'a> AstBuilder for ExtCtxt<'a> {
304     fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
305         self.path_all(span, false, strs, vec![], vec![])
306     }
307     fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
308         self.path(span, vec![id])
309     }
310     fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
311         self.path_all(span, true, strs, vec![], vec![])
312     }
313     fn path_all(&self,
314                 span: Span,
315                 global: bool,
316                 mut idents: Vec<ast::Ident> ,
317                 args: Vec<ast::GenericArg>,
318                 bindings: Vec<ast::TypeBinding> )
319                 -> ast::Path {
320         let last_ident = idents.pop().unwrap();
321         let mut segments: Vec<ast::PathSegment> = vec![];
322
323         segments.extend(idents.into_iter().map(|ident| {
324             ast::PathSegment::from_ident(ident.with_span_pos(span))
325         }));
326         let args = if !args.is_empty() || !bindings.is_empty() {
327             ast::AngleBracketedArgs { args, bindings, span }.into()
328         } else {
329             None
330         };
331         segments.push(ast::PathSegment { ident: last_ident.with_span_pos(span), args });
332         let mut path = ast::Path { span, segments };
333         if global {
334             if let Some(seg) = path.make_root() {
335                 path.segments.insert(0, seg);
336             }
337         }
338         path
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::Ident)
348              -> (ast::QSelf, ast::Path) {
349         self.qpath_all(self_type, trait_path, ident, 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::Ident,
359                  args: Vec<ast::GenericArg>,
360                  bindings: Vec<ast::TypeBinding>)
361                  -> (ast::QSelf, ast::Path) {
362         let mut path = trait_path;
363         let args = if !args.is_empty() || !bindings.is_empty() {
364             ast::AngleBracketedArgs { args, bindings, span: ident.span }.into()
365         } else {
366             None
367         };
368         path.segments.push(ast::PathSegment { ident, args });
369
370         (ast::QSelf {
371             ty: self_type,
372             path_span: path.span,
373             position: path.segments.len() - 1
374         }, path)
375     }
376
377     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
378         ast::MutTy {
379             ty,
380             mutbl,
381         }
382     }
383
384     fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
385         P(ast::Ty {
386             id: ast::DUMMY_NODE_ID,
387             span,
388             node: ty
389         })
390     }
391
392     fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
393         self.ty(path.span, ast::TyKind::Path(None, path))
394     }
395
396     // Might need to take bounds as an argument in the future, if you ever want
397     // to generate a bounded existential trait type.
398     fn ty_ident(&self, span: Span, ident: ast::Ident)
399         -> P<ast::Ty> {
400         self.ty_path(self.path_ident(span, ident))
401     }
402
403     fn ty_rptr(&self,
404                span: Span,
405                ty: P<ast::Ty>,
406                lifetime: Option<ast::Lifetime>,
407                mutbl: ast::Mutability)
408         -> P<ast::Ty> {
409         self.ty(span,
410                 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
411     }
412
413     fn ty_ptr(&self,
414               span: Span,
415               ty: P<ast::Ty>,
416               mutbl: ast::Mutability)
417         -> P<ast::Ty> {
418         self.ty(span,
419                 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
420     }
421
422     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
423         self.ty_path(
424             self.path_all(DUMMY_SP,
425                           true,
426                           self.std_path(&["option", "Option"]),
427                           vec![ast::GenericArg::Type(ty)],
428                           Vec::new()))
429     }
430
431     fn ty_infer(&self, span: Span) -> P<ast::Ty> {
432         self.ty(span, ast::TyKind::Infer)
433     }
434
435     fn typaram(&self,
436                span: Span,
437                ident: ast::Ident,
438                attrs: Vec<ast::Attribute>,
439                bounds: ast::GenericBounds,
440                default: Option<P<ast::Ty>>) -> ast::GenericParam {
441         ast::GenericParam {
442             ident: ident.with_span_pos(span),
443             id: ast::DUMMY_NODE_ID,
444             attrs: attrs.into(),
445             bounds,
446             kind: ast::GenericParamKind::Type {
447                 default,
448             }
449         }
450     }
451
452     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
453         ast::TraitRef {
454             path,
455             ref_id: ast::DUMMY_NODE_ID,
456         }
457     }
458
459     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
460         ast::PolyTraitRef {
461             bound_generic_params: Vec::new(),
462             trait_ref: self.trait_ref(path),
463             span,
464         }
465     }
466
467     fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
468         ast::GenericBound::Trait(self.poly_trait_ref(path.span, path),
469                                  ast::TraitBoundModifier::None)
470     }
471
472     fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
473         ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
474     }
475
476     fn lifetime_def(&self,
477                     span: Span,
478                     ident: ast::Ident,
479                     attrs: Vec<ast::Attribute>,
480                     bounds: ast::GenericBounds)
481                     -> ast::GenericParam {
482         let lifetime = self.lifetime(span, ident);
483         ast::GenericParam {
484             ident: lifetime.ident,
485             id: lifetime.id,
486             attrs: attrs.into(),
487             bounds,
488             kind: ast::GenericParamKind::Lifetime,
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         self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
640     }
641     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
642         let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
643         self.expr(sp, ast::ExprKind::Field(expr, ident))
644     }
645     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
646         self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
647     }
648     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
649         self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
650     }
651
652     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
653         self.expr(span, ast::ExprKind::Call(expr, args))
654     }
655     fn expr_call_ident(&self, span: Span, id: ast::Ident,
656                        args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
657         self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
658     }
659     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
660                       args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
661         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
662         self.expr_call(sp, pathexpr, args)
663     }
664     fn expr_method_call(&self, span: Span,
665                         expr: P<ast::Expr>,
666                         ident: ast::Ident,
667                         mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
668         args.insert(0, expr);
669         let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
670         self.expr(span, ast::ExprKind::MethodCall(segment, args))
671     }
672     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
673         self.expr(b.span, ast::ExprKind::Block(b, None))
674     }
675     fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
676         ast::Field {
677             ident: ident.with_span_pos(span),
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_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
715         self.expr_lit(sp, ast::LitKind::Int(u as u128,
716                                             ast::LitIntType::Unsigned(ast::UintTy::U16)))
717     }
718     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
719         self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
720     }
721     fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
722         self.expr_lit(sp, ast::LitKind::Bool(value))
723     }
724
725     fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
726         self.expr(sp, ast::ExprKind::Array(exprs))
727     }
728     fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
729         self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
730                               Vec::new())
731     }
732     fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
733         self.expr_addr_of(sp, self.expr_vec(sp, exprs))
734     }
735     fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
736         self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
737     }
738
739     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
740         self.expr(sp, ast::ExprKind::Cast(expr, ty))
741     }
742
743
744     fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
745         let some = self.std_path(&["option", "Option", "Some"]);
746         self.expr_call_global(sp, some, vec![expr])
747     }
748
749     fn expr_none(&self, sp: Span) -> P<ast::Expr> {
750         let none = self.std_path(&["option", "Option", "None"]);
751         let none = self.path_global(sp, none);
752         self.expr_path(none)
753     }
754
755
756     fn expr_break(&self, sp: Span) -> P<ast::Expr> {
757         self.expr(sp, ast::ExprKind::Break(None, None))
758     }
759
760
761     fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
762         self.expr(sp, ast::ExprKind::Tup(exprs))
763     }
764
765     fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
766         let loc = self.codemap().lookup_char_pos(span.lo());
767         let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
768         let expr_line = self.expr_u32(span, loc.line as u32);
769         let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
770         let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
771         let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
772         self.expr_call_global(
773             span,
774             self.std_path(&["rt", "begin_panic"]),
775             vec![
776                 self.expr_str(span, msg),
777                 expr_loc_ptr])
778     }
779
780     fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
781         self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
782     }
783
784     fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
785         let ok = self.std_path(&["result", "Result", "Ok"]);
786         self.expr_call_global(sp, ok, vec![expr])
787     }
788
789     fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
790         let err = self.std_path(&["result", "Result", "Err"]);
791         self.expr_call_global(sp, err, vec![expr])
792     }
793
794     fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
795         let ok = self.std_path(&["result", "Result", "Ok"]);
796         let ok_path = self.path_global(sp, ok);
797         let err = self.std_path(&["result", "Result", "Err"]);
798         let err_path = self.path_global(sp, err);
799
800         let binding_variable = self.ident_of("__try_var");
801         let binding_pat = self.pat_ident(sp, binding_variable);
802         let binding_expr = self.expr_ident(sp, binding_variable);
803
804         // Ok(__try_var) pattern
805         let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
806
807         // Err(__try_var)  (pattern and expression resp.)
808         let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
809         let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
810                                             vec![binding_expr.clone()]);
811         // return Err(__try_var)
812         let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
813
814         // Ok(__try_var) => __try_var
815         let ok_arm = self.arm(sp, vec![ok_pat], binding_expr);
816         // Err(__try_var) => return Err(__try_var)
817         let err_arm = self.arm(sp, vec![err_pat], err_expr);
818
819         // match head { Ok() => ..., Err() => ... }
820         self.expr_match(sp, head, vec![ok_arm, err_arm])
821     }
822
823
824     fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
825         P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
826     }
827     fn pat_wild(&self, span: Span) -> P<ast::Pat> {
828         self.pat(span, PatKind::Wild)
829     }
830     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
831         self.pat(span, PatKind::Lit(expr))
832     }
833     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
834         let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
835         self.pat_ident_binding_mode(span, ident, binding_mode)
836     }
837
838     fn pat_ident_binding_mode(&self,
839                               span: Span,
840                               ident: ast::Ident,
841                               bm: ast::BindingMode) -> P<ast::Pat> {
842         let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
843         self.pat(span, pat)
844     }
845     fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
846         self.pat(span, PatKind::Path(None, path))
847     }
848     fn pat_tuple_struct(&self, span: Span, path: ast::Path,
849                         subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
850         self.pat(span, PatKind::TupleStruct(path, subpats, None))
851     }
852     fn pat_struct(&self, span: Span, path: ast::Path,
853                   field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
854         self.pat(span, PatKind::Struct(path, field_pats, false))
855     }
856     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
857         self.pat(span, PatKind::Tuple(pats, None))
858     }
859
860     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
861         let some = self.std_path(&["option", "Option", "Some"]);
862         let path = self.path_global(span, some);
863         self.pat_tuple_struct(span, path, vec![pat])
864     }
865
866     fn pat_none(&self, span: Span) -> P<ast::Pat> {
867         let some = self.std_path(&["option", "Option", "None"]);
868         let path = self.path_global(span, some);
869         self.pat_path(span, path)
870     }
871
872     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
873         let some = self.std_path(&["result", "Result", "Ok"]);
874         let path = self.path_global(span, some);
875         self.pat_tuple_struct(span, path, vec![pat])
876     }
877
878     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
879         let some = self.std_path(&["result", "Result", "Err"]);
880         let path = self.path_global(span, some);
881         self.pat_tuple_struct(span, path, vec![pat])
882     }
883
884     fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
885         ast::Arm {
886             attrs: vec![],
887             pats,
888             guard: None,
889             body: expr,
890         }
891     }
892
893     fn arm_unreachable(&self, span: Span) -> ast::Arm {
894         self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span))
895     }
896
897     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
898         self.expr(span, ast::ExprKind::Match(arg, arms))
899     }
900
901     fn expr_if(&self, span: Span, cond: P<ast::Expr>,
902                then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
903         let els = els.map(|x| self.expr_block(self.block_expr(x)));
904         self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
905     }
906
907     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
908         self.expr(span, ast::ExprKind::Loop(block, None))
909     }
910
911     fn lambda_fn_decl(&self,
912                       span: Span,
913                       fn_decl: P<ast::FnDecl>,
914                       body: P<ast::Expr>,
915                       fn_decl_span: Span) // span of the `|...|` part
916                       -> P<ast::Expr> {
917         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
918                                                ast::IsAsync::NotAsync,
919                                                ast::Movability::Movable,
920                                                fn_decl,
921                                                body,
922                                                fn_decl_span))
923     }
924
925     fn lambda(&self,
926               span: Span,
927               ids: Vec<ast::Ident>,
928               body: P<ast::Expr>)
929               -> P<ast::Expr> {
930         let fn_decl = self.fn_decl(
931             ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
932             ast::FunctionRetTy::Default(span));
933
934         // FIXME -- We are using `span` as the span of the `|...|`
935         // part of the lambda, but it probably (maybe?) corresponds to
936         // the entire lambda body. Probably we should extend the API
937         // here, but that's not entirely clear.
938         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
939                                                ast::IsAsync::NotAsync,
940                                                ast::Movability::Movable,
941                                                fn_decl,
942                                                body,
943                                                span))
944     }
945
946     fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
947         self.lambda(span, Vec::new(), body)
948     }
949
950     fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
951         self.lambda(span, vec![ident], body)
952     }
953
954     fn lambda_stmts(&self,
955                     span: Span,
956                     ids: Vec<ast::Ident>,
957                     stmts: Vec<ast::Stmt>)
958                     -> P<ast::Expr> {
959         self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
960     }
961     fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
962         self.lambda0(span, self.expr_block(self.block(span, stmts)))
963     }
964     fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
965                       ident: ast::Ident) -> P<ast::Expr> {
966         self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
967     }
968
969     fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
970         let arg_pat = self.pat_ident(span, ident);
971         ast::Arg {
972             ty,
973             pat: arg_pat,
974             id: ast::DUMMY_NODE_ID
975         }
976     }
977
978     // FIXME unused self
979     fn fn_decl(&self, inputs: Vec<ast::Arg>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
980         P(ast::FnDecl {
981             inputs,
982             output,
983             variadic: false
984         })
985     }
986
987     fn item(&self, span: Span, name: Ident,
988             attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
989         // FIXME: Would be nice if our generated code didn't violate
990         // Rust coding conventions
991         P(ast::Item {
992             ident: name,
993             attrs,
994             id: ast::DUMMY_NODE_ID,
995             node,
996             vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
997             span,
998             tokens: None,
999         })
1000     }
1001
1002     fn item_fn_poly(&self,
1003                     span: Span,
1004                     name: Ident,
1005                     inputs: Vec<ast::Arg> ,
1006                     output: P<ast::Ty>,
1007                     generics: Generics,
1008                     body: P<ast::Block>) -> P<ast::Item> {
1009         self.item(span,
1010                   name,
1011                   Vec::new(),
1012                   ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
1013                               ast::FnHeader {
1014                                   unsafety: ast::Unsafety::Normal,
1015                                   asyncness: ast::IsAsync::NotAsync,
1016                                   constness: dummy_spanned(ast::Constness::NotConst),
1017                                   abi: Abi::Rust,
1018                               },
1019                               generics,
1020                               body))
1021     }
1022
1023     fn item_fn(&self,
1024                span: Span,
1025                name: Ident,
1026                inputs: Vec<ast::Arg> ,
1027                output: P<ast::Ty>,
1028                body: P<ast::Block>
1029               ) -> P<ast::Item> {
1030         self.item_fn_poly(
1031             span,
1032             name,
1033             inputs,
1034             output,
1035             Generics::default(),
1036             body)
1037     }
1038
1039     fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1040         let fields: Vec<_> = tys.into_iter().map(|ty| {
1041             ast::StructField {
1042                 span: ty.span,
1043                 ty,
1044                 ident: None,
1045                 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1046                 attrs: Vec::new(),
1047                 id: ast::DUMMY_NODE_ID,
1048             }
1049         }).collect();
1050
1051         let vdata = if fields.is_empty() {
1052             ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1053         } else {
1054             ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1055         };
1056
1057         respan(span,
1058                ast::Variant_ {
1059                    ident,
1060                    attrs: Vec::new(),
1061                    data: vdata,
1062                    disr_expr: None,
1063                })
1064     }
1065
1066     fn item_enum_poly(&self, span: Span, name: Ident,
1067                       enum_definition: ast::EnumDef,
1068                       generics: Generics) -> P<ast::Item> {
1069         self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1070     }
1071
1072     fn item_enum(&self, span: Span, name: Ident,
1073                  enum_definition: ast::EnumDef) -> P<ast::Item> {
1074         self.item_enum_poly(span, name, enum_definition,
1075                             Generics::default())
1076     }
1077
1078     fn item_struct(&self, span: Span, name: Ident,
1079                    struct_def: ast::VariantData) -> P<ast::Item> {
1080         self.item_struct_poly(
1081             span,
1082             name,
1083             struct_def,
1084             Generics::default()
1085         )
1086     }
1087
1088     fn item_struct_poly(&self, span: Span, name: Ident,
1089         struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1090         self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1091     }
1092
1093     fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1094                 attrs: Vec<ast::Attribute>,
1095                 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1096         self.item(
1097             span,
1098             name,
1099             attrs,
1100             ast::ItemKind::Mod(ast::Mod {
1101                 inner: inner_span,
1102                 items,
1103             })
1104         )
1105     }
1106
1107     fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item> {
1108         self.item(span, name, Vec::new(), ast::ItemKind::ExternCrate(None))
1109     }
1110
1111     fn item_static(&self,
1112                    span: Span,
1113                    name: Ident,
1114                    ty: P<ast::Ty>,
1115                    mutbl: ast::Mutability,
1116                    expr: P<ast::Expr>)
1117                    -> P<ast::Item> {
1118         self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1119     }
1120
1121     fn item_const(&self,
1122                   span: Span,
1123                   name: Ident,
1124                   ty: P<ast::Ty>,
1125                   expr: P<ast::Expr>)
1126                   -> P<ast::Item> {
1127         self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1128     }
1129
1130     fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1131                     generics: Generics) -> P<ast::Item> {
1132         self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1133     }
1134
1135     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1136         self.item_ty_poly(span, name, ty, Generics::default())
1137     }
1138
1139     fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute {
1140         attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
1141     }
1142
1143     fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
1144         attr::mk_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1145     }
1146
1147     fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
1148         attr::mk_nested_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1149     }
1150
1151     fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
1152                  -> ast::MetaItem {
1153         attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
1154     }
1155
1156     fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
1157                        -> ast::MetaItem {
1158         attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp),
1159                                  respan(sp, value))
1160     }
1161
1162     fn item_use(&self, sp: Span,
1163                 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item> {
1164         P(ast::Item {
1165             id: ast::DUMMY_NODE_ID,
1166             ident: keywords::Invalid.ident(),
1167             attrs: vec![],
1168             node: ast::ItemKind::Use(vp),
1169             vis,
1170             span: sp,
1171             tokens: None,
1172         })
1173     }
1174
1175     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1176         self.item_use_simple_(sp, vis, None, path)
1177     }
1178
1179     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1180                         rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
1181         self.item_use(sp, vis, P(ast::UseTree {
1182             span: sp,
1183             prefix: path,
1184             kind: ast::UseTreeKind::Simple(rename, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1185         }))
1186     }
1187
1188     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1189                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1190         let imports = imports.iter().map(|id| {
1191             (ast::UseTree {
1192                 span: sp,
1193                 prefix: self.path(sp, vec![*id]),
1194                 kind: ast::UseTreeKind::Simple(None, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1195             }, ast::DUMMY_NODE_ID)
1196         }).collect();
1197
1198         self.item_use(sp, vis, P(ast::UseTree {
1199             span: sp,
1200             prefix: self.path(sp, path),
1201             kind: ast::UseTreeKind::Nested(imports),
1202         }))
1203     }
1204
1205     fn item_use_glob(&self, sp: Span,
1206                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1207         self.item_use(sp, vis, P(ast::UseTree {
1208             span: sp,
1209             prefix: self.path(sp, path),
1210             kind: ast::UseTreeKind::Glob,
1211         }))
1212     }
1213 }