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