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