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