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