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