]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_expand/build.rs
Remove unchecked inline attribute, remove unused functions, make chache mod private...
[rust.git] / src / libsyntax_expand / build.rs
1 use crate::base::ExtCtxt;
2
3 use syntax::ast::{self, Ident, Expr, BlockCheckMode, UnOp, PatKind};
4 use syntax::attr;
5 use syntax::source_map::{respan, Spanned};
6 use syntax::ptr::P;
7 use syntax::symbol::{kw, sym, Symbol};
8 use syntax::ThinVec;
9
10 use syntax_pos::{Pos, Span};
11
12 impl<'a> ExtCtxt<'a> {
13     pub fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
14         self.path_all(span, false, strs, vec![])
15     }
16     pub fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
17         self.path(span, vec![id])
18     }
19     pub fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
20         self.path_all(span, true, strs, vec![])
21     }
22     pub fn path_all(&self,
23                 span: Span,
24                 global: bool,
25                 mut idents: Vec<ast::Ident> ,
26                 args: Vec<ast::GenericArg>)
27                 -> ast::Path {
28         assert!(!idents.is_empty());
29         let add_root = global && !idents[0].is_path_segment_keyword();
30         let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
31         if add_root {
32             segments.push(ast::PathSegment::path_root(span));
33         }
34         let last_ident = idents.pop().unwrap();
35         segments.extend(idents.into_iter().map(|ident| {
36             ast::PathSegment::from_ident(ident.with_span_pos(span))
37         }));
38         let args = if !args.is_empty() {
39             ast::AngleBracketedArgs { args, constraints: Vec::new(), span }.into()
40         } else {
41             None
42         };
43         segments.push(ast::PathSegment {
44             ident: last_ident.with_span_pos(span),
45             id: ast::DUMMY_NODE_ID,
46             args,
47         });
48         ast::Path { span, segments }
49     }
50
51     pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
52         ast::MutTy {
53             ty,
54             mutbl,
55         }
56     }
57
58     pub fn ty(&self, span: Span, kind: ast::TyKind) -> P<ast::Ty> {
59         P(ast::Ty {
60             id: ast::DUMMY_NODE_ID,
61             span,
62             kind,
63         })
64     }
65
66     pub fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
67         self.ty(path.span, ast::TyKind::Path(None, path))
68     }
69
70     // Might need to take bounds as an argument in the future, if you ever want
71     // to generate a bounded existential trait type.
72     pub fn ty_ident(&self, span: Span, ident: ast::Ident)
73         -> P<ast::Ty> {
74         self.ty_path(self.path_ident(span, ident))
75     }
76
77     pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
78         ast::AnonConst {
79             id: ast::DUMMY_NODE_ID,
80             value: P(ast::Expr {
81                 id: ast::DUMMY_NODE_ID,
82                 kind,
83                 span,
84                 attrs: ThinVec::new(),
85             })
86         }
87     }
88
89     pub fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
90         self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
91     }
92
93     pub fn ty_rptr(&self,
94                span: Span,
95                ty: P<ast::Ty>,
96                lifetime: Option<ast::Lifetime>,
97                mutbl: ast::Mutability)
98         -> P<ast::Ty> {
99         self.ty(span,
100                 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
101     }
102
103     pub fn ty_ptr(&self,
104               span: Span,
105               ty: P<ast::Ty>,
106               mutbl: ast::Mutability)
107         -> P<ast::Ty> {
108         self.ty(span,
109                 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
110     }
111
112     pub fn typaram(&self,
113                span: Span,
114                ident: ast::Ident,
115                attrs: Vec<ast::Attribute>,
116                bounds: ast::GenericBounds,
117                default: Option<P<ast::Ty>>) -> ast::GenericParam {
118         ast::GenericParam {
119             ident: ident.with_span_pos(span),
120             id: ast::DUMMY_NODE_ID,
121             attrs: attrs.into(),
122             bounds,
123             kind: ast::GenericParamKind::Type {
124                 default,
125             },
126             is_placeholder: false
127         }
128     }
129
130     pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
131         ast::TraitRef {
132             path,
133             ref_id: ast::DUMMY_NODE_ID,
134         }
135     }
136
137     pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
138         ast::PolyTraitRef {
139             bound_generic_params: Vec::new(),
140             trait_ref: self.trait_ref(path),
141             span,
142         }
143     }
144
145     pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
146         ast::GenericBound::Trait(self.poly_trait_ref(path.span, path),
147                                  ast::TraitBoundModifier::None)
148     }
149
150     pub fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
151         ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
152     }
153
154     pub fn lifetime_def(&self,
155                     span: Span,
156                     ident: ast::Ident,
157                     attrs: Vec<ast::Attribute>,
158                     bounds: ast::GenericBounds)
159                     -> ast::GenericParam {
160         let lifetime = self.lifetime(span, ident);
161         ast::GenericParam {
162             ident: lifetime.ident,
163             id: lifetime.id,
164             attrs: attrs.into(),
165             bounds,
166             kind: ast::GenericParamKind::Lifetime,
167             is_placeholder: false
168         }
169     }
170
171     pub fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
172         ast::Stmt {
173             id: ast::DUMMY_NODE_ID,
174             span: expr.span,
175             kind: ast::StmtKind::Expr(expr),
176         }
177     }
178
179     pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
180                 ex: P<ast::Expr>) -> ast::Stmt {
181         let pat = if mutbl {
182             let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
183             self.pat_ident_binding_mode(sp, ident, binding_mode)
184         } else {
185             self.pat_ident(sp, ident)
186         };
187         let local = P(ast::Local {
188             pat,
189             ty: None,
190             init: Some(ex),
191             id: ast::DUMMY_NODE_ID,
192             span: sp,
193             attrs: ThinVec::new(),
194         });
195         ast::Stmt {
196             id: ast::DUMMY_NODE_ID,
197             kind: ast::StmtKind::Local(local),
198             span: sp,
199         }
200     }
201
202     // Generates `let _: Type;`, which is usually used for type assertions.
203     pub fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
204         let local = P(ast::Local {
205             pat: self.pat_wild(span),
206             ty: Some(ty),
207             init: None,
208             id: ast::DUMMY_NODE_ID,
209             span,
210             attrs: ThinVec::new(),
211         });
212         ast::Stmt {
213             id: ast::DUMMY_NODE_ID,
214             kind: ast::StmtKind::Local(local),
215             span,
216         }
217     }
218
219     pub fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
220         ast::Stmt {
221             id: ast::DUMMY_NODE_ID,
222             kind: ast::StmtKind::Item(item),
223             span: sp,
224         }
225     }
226
227     pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
228         self.block(expr.span, vec![ast::Stmt {
229             id: ast::DUMMY_NODE_ID,
230             span: expr.span,
231             kind: ast::StmtKind::Expr(expr),
232         }])
233     }
234     pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
235         P(ast::Block {
236            stmts,
237            id: ast::DUMMY_NODE_ID,
238            rules: BlockCheckMode::Default,
239            span,
240         })
241     }
242
243     pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
244         P(ast::Expr {
245             id: ast::DUMMY_NODE_ID,
246             kind,
247             span,
248             attrs: ThinVec::new(),
249         })
250     }
251
252     pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
253         self.expr(path.span, ast::ExprKind::Path(None, path))
254     }
255
256     pub fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
257         self.expr_path(self.path_ident(span, id))
258     }
259     pub fn expr_self(&self, span: Span) -> P<ast::Expr> {
260         self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
261     }
262
263     pub fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
264                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
265         self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
266     }
267
268     pub fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
269         self.expr(sp, ast::ExprKind::Unary(UnOp::Deref, e))
270     }
271
272     pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
273         self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Immutable, e))
274     }
275
276     pub fn expr_call(
277         &self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>,
278     ) -> P<ast::Expr> {
279         self.expr(span, ast::ExprKind::Call(expr, args))
280     }
281     pub fn expr_call_ident(&self, span: Span, id: ast::Ident,
282                        args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
283         self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
284     }
285     pub fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
286                       args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
287         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
288         self.expr_call(sp, pathexpr, args)
289     }
290     pub fn expr_method_call(&self, span: Span,
291                         expr: P<ast::Expr>,
292                         ident: ast::Ident,
293                         mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
294         args.insert(0, expr);
295         let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
296         self.expr(span, ast::ExprKind::MethodCall(segment, args))
297     }
298     pub fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
299         self.expr(b.span, ast::ExprKind::Block(b, None))
300     }
301     pub fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
302         ast::Field {
303             ident: ident.with_span_pos(span),
304             expr: e,
305             span,
306             is_shorthand: false,
307             attrs: ThinVec::new(),
308             id: ast::DUMMY_NODE_ID,
309             is_placeholder: false,
310         }
311     }
312     pub fn expr_struct(
313         &self, span: Span, path: ast::Path, fields: Vec<ast::Field>
314     ) -> P<ast::Expr> {
315         self.expr(span, ast::ExprKind::Struct(path, fields, None))
316     }
317     pub fn expr_struct_ident(&self, span: Span,
318                          id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
319         self.expr_struct(span, self.path_ident(span, id), fields)
320     }
321
322     pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
323         let lit = ast::Lit::from_lit_kind(lit_kind, span);
324         self.expr(span, ast::ExprKind::Lit(lit))
325     }
326     pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
327         self.expr_lit(span, ast::LitKind::Int(i as u128,
328                                               ast::LitIntType::Unsigned(ast::UintTy::Usize)))
329     }
330     pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
331         self.expr_lit(sp, ast::LitKind::Int(u as u128,
332                                             ast::LitIntType::Unsigned(ast::UintTy::U32)))
333     }
334     pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
335         self.expr_lit(sp, ast::LitKind::Bool(value))
336     }
337
338     pub fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
339         self.expr(sp, ast::ExprKind::Array(exprs))
340     }
341     pub fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
342         self.expr_addr_of(sp, self.expr_vec(sp, exprs))
343     }
344     pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
345         self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
346     }
347
348     pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
349         self.expr(sp, ast::ExprKind::Cast(expr, ty))
350     }
351
352     pub fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
353         let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
354         self.expr_call_global(sp, some, vec![expr])
355     }
356
357     pub fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
358         self.expr(sp, ast::ExprKind::Tup(exprs))
359     }
360
361     pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
362         let loc = self.source_map().lookup_char_pos(span.lo());
363         let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
364         let expr_line = self.expr_u32(span, loc.line as u32);
365         let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
366         let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
367         let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
368         self.expr_call_global(
369             span,
370             [sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
371             vec![
372                 self.expr_str(span, msg),
373                 expr_loc_ptr])
374     }
375
376     pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
377         self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
378     }
379
380     pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
381         let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
382         self.expr_call_global(sp, ok, vec![expr])
383     }
384
385     pub fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
386         let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
387         let ok_path = self.path_global(sp, ok);
388         let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
389         let err_path = self.path_global(sp, err);
390
391         let binding_variable = self.ident_of("__try_var", sp);
392         let binding_pat = self.pat_ident(sp, binding_variable);
393         let binding_expr = self.expr_ident(sp, binding_variable);
394
395         // `Ok(__try_var)` pattern
396         let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
397
398         // `Err(__try_var)` (pattern and expression respectively)
399         let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
400         let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
401                                             vec![binding_expr.clone()]);
402         // `return Err(__try_var)`
403         let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
404
405         // `Ok(__try_var) => __try_var`
406         let ok_arm = self.arm(sp, ok_pat, binding_expr);
407         // `Err(__try_var) => return Err(__try_var)`
408         let err_arm = self.arm(sp, err_pat, err_expr);
409
410         // `match head { Ok() => ..., Err() => ... }`
411         self.expr_match(sp, head, vec![ok_arm, err_arm])
412     }
413
414
415     pub fn pat(&self, span: Span, kind: PatKind) -> P<ast::Pat> {
416         P(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span })
417     }
418     pub fn pat_wild(&self, span: Span) -> P<ast::Pat> {
419         self.pat(span, PatKind::Wild)
420     }
421     pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
422         self.pat(span, PatKind::Lit(expr))
423     }
424     pub fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
425         let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
426         self.pat_ident_binding_mode(span, ident, binding_mode)
427     }
428
429     pub fn pat_ident_binding_mode(&self,
430                               span: Span,
431                               ident: ast::Ident,
432                               bm: ast::BindingMode) -> P<ast::Pat> {
433         let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
434         self.pat(span, pat)
435     }
436     pub fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
437         self.pat(span, PatKind::Path(None, path))
438     }
439     pub fn pat_tuple_struct(&self, span: Span, path: ast::Path,
440                         subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
441         self.pat(span, PatKind::TupleStruct(path, subpats))
442     }
443     pub fn pat_struct(&self, span: Span, path: ast::Path,
444                       field_pats: Vec<ast::FieldPat>) -> P<ast::Pat> {
445         self.pat(span, PatKind::Struct(path, field_pats, false))
446     }
447     pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
448         self.pat(span, PatKind::Tuple(pats))
449     }
450
451     pub fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
452         let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
453         let path = self.path_global(span, some);
454         self.pat_tuple_struct(span, path, vec![pat])
455     }
456
457     pub fn pat_none(&self, span: Span) -> P<ast::Pat> {
458         let some = self.std_path(&[sym::option, sym::Option, sym::None]);
459         let path = self.path_global(span, some);
460         self.pat_path(span, path)
461     }
462
463     pub fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
464         let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
465         let path = self.path_global(span, some);
466         self.pat_tuple_struct(span, path, vec![pat])
467     }
468
469     pub fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
470         let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
471         let path = self.path_global(span, some);
472         self.pat_tuple_struct(span, path, vec![pat])
473     }
474
475     pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm {
476         ast::Arm {
477             attrs: vec![],
478             pat,
479             guard: None,
480             body: expr,
481             span,
482             id: ast::DUMMY_NODE_ID,
483             is_placeholder: false,
484         }
485     }
486
487     pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
488         self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
489     }
490
491     pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
492         self.expr(span, ast::ExprKind::Match(arg, arms))
493     }
494
495     pub fn expr_if(&self, span: Span, cond: P<ast::Expr>,
496                then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
497         let els = els.map(|x| self.expr_block(self.block_expr(x)));
498         self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
499     }
500
501     pub fn lambda_fn_decl(&self,
502                       span: Span,
503                       fn_decl: P<ast::FnDecl>,
504                       body: P<ast::Expr>,
505                       fn_decl_span: Span) // span of the `|...|` part
506                       -> P<ast::Expr> {
507         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
508                                                ast::IsAsync::NotAsync,
509                                                ast::Movability::Movable,
510                                                fn_decl,
511                                                body,
512                                                fn_decl_span))
513     }
514
515     pub fn lambda(&self,
516               span: Span,
517               ids: Vec<ast::Ident>,
518               body: P<ast::Expr>)
519               -> P<ast::Expr> {
520         let fn_decl = self.fn_decl(
521             ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
522             ast::FunctionRetTy::Default(span));
523
524         // FIXME -- We are using `span` as the span of the `|...|`
525         // part of the lambda, but it probably (maybe?) corresponds to
526         // the entire lambda body. Probably we should extend the API
527         // here, but that's not entirely clear.
528         self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
529                                                ast::IsAsync::NotAsync,
530                                                ast::Movability::Movable,
531                                                fn_decl,
532                                                body,
533                                                span))
534     }
535
536     pub fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
537         self.lambda(span, Vec::new(), body)
538     }
539
540     pub fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
541         self.lambda(span, vec![ident], body)
542     }
543
544     pub fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
545                       ident: ast::Ident) -> P<ast::Expr> {
546         self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
547     }
548
549     pub fn param(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Param {
550         let arg_pat = self.pat_ident(span, ident);
551         ast::Param {
552             attrs: ThinVec::default(),
553             id: ast::DUMMY_NODE_ID,
554             pat: arg_pat,
555             span,
556             ty,
557             is_placeholder: false,
558         }
559     }
560
561     // FIXME: unused `self`
562     pub fn fn_decl(&self, inputs: Vec<ast::Param>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
563         P(ast::FnDecl {
564             inputs,
565             output,
566         })
567     }
568
569     pub fn item(&self, span: Span, name: Ident,
570             attrs: Vec<ast::Attribute>, kind: ast::ItemKind) -> P<ast::Item> {
571         // FIXME: Would be nice if our generated code didn't violate
572         // Rust coding conventions
573         P(ast::Item {
574             ident: name,
575             attrs,
576             id: ast::DUMMY_NODE_ID,
577             kind,
578             vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
579             span,
580             tokens: None,
581         })
582     }
583
584     pub fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
585         let vis_span = span.shrink_to_lo();
586         let fields: Vec<_> = tys.into_iter().map(|ty| {
587             ast::StructField {
588                 span: ty.span,
589                 ty,
590                 ident: None,
591                 vis: respan(vis_span, ast::VisibilityKind::Inherited),
592                 attrs: Vec::new(),
593                 id: ast::DUMMY_NODE_ID,
594                 is_placeholder: false,
595             }
596         }).collect();
597
598         let vdata = if fields.is_empty() {
599             ast::VariantData::Unit(ast::DUMMY_NODE_ID)
600         } else {
601             ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
602         };
603
604         ast::Variant {
605             attrs: Vec::new(),
606             data: vdata,
607             disr_expr: None,
608             id: ast::DUMMY_NODE_ID,
609             ident,
610             vis: respan(vis_span, ast::VisibilityKind::Inherited),
611             span,
612             is_placeholder: false,
613         }
614     }
615
616     pub fn item_static(&self,
617                    span: Span,
618                    name: Ident,
619                    ty: P<ast::Ty>,
620                    mutbl: ast::Mutability,
621                    expr: P<ast::Expr>)
622                    -> P<ast::Item> {
623         self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
624     }
625
626     pub fn item_const(&self,
627                   span: Span,
628                   name: Ident,
629                   ty: P<ast::Ty>,
630                   expr: P<ast::Expr>)
631                   -> P<ast::Item> {
632         self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
633     }
634
635     pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute {
636         attr::mk_attr_outer(mi)
637     }
638
639     pub fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
640         attr::mk_word_item(Ident::new(w, sp))
641     }
642 }