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