]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/build.rs
Auto merge of #102318 - Amanieu:default_alloc_error_handler, r=oli-obk
[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_rptr(
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::Rptr(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_call(
276         &self,
277         span: Span,
278         expr: P<ast::Expr>,
279         args: Vec<P<ast::Expr>>,
280     ) -> P<ast::Expr> {
281         self.expr(span, ast::ExprKind::Call(expr, args))
282     }
283     pub fn expr_call_ident(&self, span: Span, id: Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
284         self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
285     }
286     pub fn expr_call_global(
287         &self,
288         sp: Span,
289         fn_path: Vec<Ident>,
290         args: Vec<P<ast::Expr>>,
291     ) -> P<ast::Expr> {
292         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
293         self.expr_call(sp, pathexpr, args)
294     }
295     pub fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
296         self.expr(b.span, ast::ExprKind::Block(b, None))
297     }
298     pub fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::ExprField {
299         ast::ExprField {
300             ident: ident.with_span_pos(span),
301             expr: e,
302             span,
303             is_shorthand: false,
304             attrs: AttrVec::new(),
305             id: ast::DUMMY_NODE_ID,
306             is_placeholder: false,
307         }
308     }
309     pub fn expr_struct(
310         &self,
311         span: Span,
312         path: ast::Path,
313         fields: Vec<ast::ExprField>,
314     ) -> P<ast::Expr> {
315         self.expr(
316             span,
317             ast::ExprKind::Struct(P(ast::StructExpr {
318                 qself: None,
319                 path,
320                 fields,
321                 rest: ast::StructRest::None,
322             })),
323         )
324     }
325     pub fn expr_struct_ident(
326         &self,
327         span: Span,
328         id: Ident,
329         fields: Vec<ast::ExprField>,
330     ) -> P<ast::Expr> {
331         self.expr_struct(span, self.path_ident(span, id), fields)
332     }
333
334     pub fn expr_usize(&self, span: Span, n: usize) -> P<ast::Expr> {
335         let suffix = Some(ast::UintTy::Usize.name());
336         let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
337         self.expr(span, ast::ExprKind::Lit(lit))
338     }
339
340     pub fn expr_u32(&self, span: Span, n: u32) -> P<ast::Expr> {
341         let suffix = Some(ast::UintTy::U32.name());
342         let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
343         self.expr(span, ast::ExprKind::Lit(lit))
344     }
345
346     pub fn expr_bool(&self, span: Span, value: bool) -> P<ast::Expr> {
347         let lit = token::Lit::new(token::Bool, if value { kw::True } else { kw::False }, None);
348         self.expr(span, ast::ExprKind::Lit(lit))
349     }
350
351     pub fn expr_str(&self, span: Span, s: Symbol) -> P<ast::Expr> {
352         let lit = token::Lit::new(token::Str, literal::escape_string_symbol(s), None);
353         self.expr(span, ast::ExprKind::Lit(lit))
354     }
355
356     pub fn expr_char(&self, span: Span, ch: char) -> P<ast::Expr> {
357         let lit = token::Lit::new(token::Char, literal::escape_char_symbol(ch), None);
358         self.expr(span, ast::ExprKind::Lit(lit))
359     }
360
361     pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> P<ast::Expr> {
362         let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
363         self.expr(span, ast::ExprKind::Lit(lit))
364     }
365
366     /// `[expr1, expr2, ...]`
367     pub fn expr_array(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
368         self.expr(sp, ast::ExprKind::Array(exprs))
369     }
370
371     /// `&[expr1, expr2, ...]`
372     pub fn expr_array_ref(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
373         self.expr_addr_of(sp, self.expr_array(sp, exprs))
374     }
375
376     pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
377         self.expr(sp, ast::ExprKind::Cast(expr, ty))
378     }
379
380     pub fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
381         let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
382         self.expr_call_global(sp, some, vec![expr])
383     }
384
385     pub fn expr_none(&self, sp: Span) -> P<ast::Expr> {
386         let none = self.std_path(&[sym::option, sym::Option, sym::None]);
387         self.expr_path(self.path_global(sp, none))
388     }
389     pub fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
390         self.expr(sp, ast::ExprKind::Tup(exprs))
391     }
392
393     pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
394         self.expr_call_global(
395             span,
396             [sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
397             vec![self.expr_str(span, msg)],
398         )
399     }
400
401     pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
402         self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
403     }
404
405     pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
406         let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
407         self.expr_call_global(sp, ok, vec![expr])
408     }
409
410     pub fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
411         let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
412         let ok_path = self.path_global(sp, ok);
413         let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
414         let err_path = self.path_global(sp, err);
415
416         let binding_variable = Ident::new(sym::__try_var, sp);
417         let binding_pat = self.pat_ident(sp, binding_variable);
418         let binding_expr = self.expr_ident(sp, binding_variable);
419
420         // `Ok(__try_var)` pattern
421         let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
422
423         // `Err(__try_var)` (pattern and expression respectively)
424         let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
425         let err_inner_expr =
426             self.expr_call(sp, self.expr_path(err_path), vec![binding_expr.clone()]);
427         // `return Err(__try_var)`
428         let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
429
430         // `Ok(__try_var) => __try_var`
431         let ok_arm = self.arm(sp, ok_pat, binding_expr);
432         // `Err(__try_var) => return Err(__try_var)`
433         let err_arm = self.arm(sp, err_pat, err_expr);
434
435         // `match head { Ok() => ..., Err() => ... }`
436         self.expr_match(sp, head, vec![ok_arm, err_arm])
437     }
438
439     pub fn pat(&self, span: Span, kind: PatKind) -> P<ast::Pat> {
440         P(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None })
441     }
442     pub fn pat_wild(&self, span: Span) -> P<ast::Pat> {
443         self.pat(span, PatKind::Wild)
444     }
445     pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
446         self.pat(span, PatKind::Lit(expr))
447     }
448     pub fn pat_ident(&self, span: Span, ident: Ident) -> P<ast::Pat> {
449         self.pat_ident_binding_mode(span, ident, ast::BindingAnnotation::NONE)
450     }
451
452     pub fn pat_ident_binding_mode(
453         &self,
454         span: Span,
455         ident: Ident,
456         ann: ast::BindingAnnotation,
457     ) -> P<ast::Pat> {
458         let pat = PatKind::Ident(ann, ident.with_span_pos(span), None);
459         self.pat(span, pat)
460     }
461     pub fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
462         self.pat(span, PatKind::Path(None, path))
463     }
464     pub fn pat_tuple_struct(
465         &self,
466         span: Span,
467         path: ast::Path,
468         subpats: Vec<P<ast::Pat>>,
469     ) -> P<ast::Pat> {
470         self.pat(span, PatKind::TupleStruct(None, path, subpats))
471     }
472     pub fn pat_struct(
473         &self,
474         span: Span,
475         path: ast::Path,
476         field_pats: Vec<ast::PatField>,
477     ) -> P<ast::Pat> {
478         self.pat(span, PatKind::Struct(None, path, field_pats, false))
479     }
480     pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
481         self.pat(span, PatKind::Tuple(pats))
482     }
483
484     pub fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
485         let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
486         let path = self.path_global(span, some);
487         self.pat_tuple_struct(span, path, vec![pat])
488     }
489
490     pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm {
491         ast::Arm {
492             attrs: AttrVec::new(),
493             pat,
494             guard: None,
495             body: expr,
496             span,
497             id: ast::DUMMY_NODE_ID,
498             is_placeholder: false,
499         }
500     }
501
502     pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
503         self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
504     }
505
506     pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
507         self.expr(span, ast::ExprKind::Match(arg, arms))
508     }
509
510     pub fn expr_if(
511         &self,
512         span: Span,
513         cond: P<ast::Expr>,
514         then: P<ast::Expr>,
515         els: Option<P<ast::Expr>>,
516     ) -> P<ast::Expr> {
517         let els = els.map(|x| self.expr_block(self.block_expr(x)));
518         self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
519     }
520
521     pub fn lambda(&self, span: Span, ids: Vec<Ident>, body: P<ast::Expr>) -> P<ast::Expr> {
522         let fn_decl = self.fn_decl(
523             ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
524             ast::FnRetTy::Default(span),
525         );
526
527         // FIXME -- We are using `span` as the span of the `|...|`
528         // part of the lambda, but it probably (maybe?) corresponds to
529         // the entire lambda body. Probably we should extend the API
530         // here, but that's not entirely clear.
531         self.expr(
532             span,
533             ast::ExprKind::Closure(Box::new(ast::Closure {
534                 binder: ast::ClosureBinder::NotPresent,
535                 capture_clause: ast::CaptureBy::Ref,
536                 asyncness: ast::Async::No,
537                 movability: ast::Movability::Movable,
538                 fn_decl,
539                 body,
540                 fn_decl_span: span,
541                 // FIXME(SarthakSingh31): This points to the start of the declaration block and
542                 // not the span of the argument block.
543                 fn_arg_span: span,
544             })),
545         )
546     }
547
548     pub fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
549         self.lambda(span, Vec::new(), body)
550     }
551
552     pub fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: Ident) -> P<ast::Expr> {
553         self.lambda(span, vec![ident], body)
554     }
555
556     pub fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>, ident: Ident) -> P<ast::Expr> {
557         self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
558     }
559
560     pub fn param(&self, span: Span, ident: Ident, ty: P<ast::Ty>) -> ast::Param {
561         let arg_pat = self.pat_ident(span, ident);
562         ast::Param {
563             attrs: AttrVec::default(),
564             id: ast::DUMMY_NODE_ID,
565             pat: arg_pat,
566             span,
567             ty,
568             is_placeholder: false,
569         }
570     }
571
572     // `self` is unused but keep it as method for the convenience use.
573     pub fn fn_decl(&self, inputs: Vec<ast::Param>, output: ast::FnRetTy) -> P<ast::FnDecl> {
574         P(ast::FnDecl { inputs, output })
575     }
576
577     pub fn item(
578         &self,
579         span: Span,
580         name: Ident,
581         attrs: ast::AttrVec,
582         kind: ast::ItemKind,
583     ) -> P<ast::Item> {
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     // Builds `#[name]`.
622     pub fn attr_word(&self, name: Symbol, span: Span) -> ast::Attribute {
623         let g = &self.sess.parse_sess.attr_id_generator;
624         attr::mk_attr_word(g, ast::AttrStyle::Outer, name, span)
625     }
626
627     // Builds `#[name = val]`.
628     //
629     // Note: `span` is used for both the identifer and the value.
630     pub fn attr_name_value_str(&self, name: Symbol, val: Symbol, span: Span) -> ast::Attribute {
631         let g = &self.sess.parse_sess.attr_id_generator;
632         attr::mk_attr_name_value_str(g, ast::AttrStyle::Outer, name, val, span)
633     }
634
635     // Builds `#[outer(inner)]`.
636     pub fn attr_nested_word(&self, outer: Symbol, inner: Symbol, span: Span) -> ast::Attribute {
637         let g = &self.sess.parse_sess.attr_id_generator;
638         attr::mk_attr_nested_word(g, ast::AttrStyle::Outer, outer, inner, span)
639     }
640 }