]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_expand/src/build.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.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, PatKind, UnOp};
6 use rustc_span::source_map::Spanned;
7 use rustc_span::symbol::{kw, sym, Ident, Symbol};
8
9 use rustc_span::Span;
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 = Vec::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_path(&self, path: ast::Path) -> P<ast::Ty> {
61 self.ty(path.span, ast::TyKind::Path(None, path))
62 }
63
64 // Might need to take bounds as an argument in the future, if you ever want
65 // to generate a bounded existential trait type.
66 pub fn ty_ident(&self, span: Span, ident: Ident) -> P<ast::Ty> {
67 self.ty_path(self.path_ident(span, ident))
68 }
69
70 pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
71 ast::AnonConst {
72 id: ast::DUMMY_NODE_ID,
73 value: P(ast::Expr {
74 id: ast::DUMMY_NODE_ID,
75 kind,
76 span,
77 attrs: AttrVec::new(),
78 tokens: None,
79 }),
80 }
81 }
82
83 pub fn const_ident(&self, span: Span, ident: Ident) -> ast::AnonConst {
84 self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
85 }
86
87 pub fn ty_rptr(
88 &self,
89 span: Span,
90 ty: P<ast::Ty>,
91 lifetime: Option<ast::Lifetime>,
92 mutbl: ast::Mutability,
93 ) -> P<ast::Ty> {
94 self.ty(span, ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
95 }
96
97 pub fn ty_ptr(&self, span: Span, ty: P<ast::Ty>, mutbl: ast::Mutability) -> P<ast::Ty> {
98 self.ty(span, ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
99 }
100
101 pub fn typaram(
102 &self,
103 span: Span,
104 ident: Ident,
105 attrs: Vec<ast::Attribute>,
106 bounds: ast::GenericBounds,
107 default: Option<P<ast::Ty>>,
108 ) -> ast::GenericParam {
109 ast::GenericParam {
110 ident: ident.with_span_pos(span),
111 id: ast::DUMMY_NODE_ID,
112 attrs: attrs.into(),
113 bounds,
114 kind: ast::GenericParamKind::Type { default },
115 is_placeholder: false,
116 }
117 }
118
119 pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
120 ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
121 }
122
123 pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
124 ast::PolyTraitRef {
125 bound_generic_params: Vec::new(),
126 trait_ref: self.trait_ref(path),
127 span,
128 }
129 }
130
131 pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
132 ast::GenericBound::Trait(
133 self.poly_trait_ref(path.span, path),
134 ast::TraitBoundModifier::None,
135 )
136 }
137
138 pub fn lifetime(&self, span: Span, ident: Ident) -> ast::Lifetime {
139 ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
140 }
141
142 pub fn lifetime_def(
143 &self,
144 span: Span,
145 ident: Ident,
146 attrs: Vec<ast::Attribute>,
147 bounds: ast::GenericBounds,
148 ) -> ast::GenericParam {
149 let lifetime = self.lifetime(span, ident);
150 ast::GenericParam {
151 ident: lifetime.ident,
152 id: lifetime.id,
153 attrs: attrs.into(),
154 bounds,
155 kind: ast::GenericParamKind::Lifetime,
156 is_placeholder: false,
157 }
158 }
159
160 pub fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
161 ast::Stmt {
162 id: ast::DUMMY_NODE_ID,
163 span: expr.span,
164 kind: ast::StmtKind::Expr(expr),
165 tokens: None,
166 }
167 }
168
169 pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: Ident, ex: P<ast::Expr>) -> ast::Stmt {
170 let pat = if mutbl {
171 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mut);
172 self.pat_ident_binding_mode(sp, ident, binding_mode)
173 } else {
174 self.pat_ident(sp, ident)
175 };
176 let local = P(ast::Local {
177 pat,
178 ty: None,
179 init: Some(ex),
180 id: ast::DUMMY_NODE_ID,
181 span: sp,
182 attrs: AttrVec::new(),
183 });
184 ast::Stmt {
185 id: ast::DUMMY_NODE_ID,
186 kind: ast::StmtKind::Local(local),
187 span: sp,
188 tokens: None,
189 }
190 }
191
192 // Generates `let _: Type;`, which is usually used for type assertions.
193 pub fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
194 let local = P(ast::Local {
195 pat: self.pat_wild(span),
196 ty: Some(ty),
197 init: None,
198 id: ast::DUMMY_NODE_ID,
199 span,
200 attrs: AttrVec::new(),
201 });
202 ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span, tokens: None }
203 }
204
205 pub fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
206 ast::Stmt {
207 id: ast::DUMMY_NODE_ID,
208 kind: ast::StmtKind::Item(item),
209 span: sp,
210 tokens: None,
211 }
212 }
213
214 pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
215 self.block(
216 expr.span,
217 vec![ast::Stmt {
218 id: ast::DUMMY_NODE_ID,
219 span: expr.span,
220 kind: ast::StmtKind::Expr(expr),
221 tokens: None,
222 }],
223 )
224 }
225 pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
226 P(ast::Block {
227 stmts,
228 id: ast::DUMMY_NODE_ID,
229 rules: BlockCheckMode::Default,
230 span,
231 tokens: None,
232 })
233 }
234
235 pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
236 P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new(), tokens: None })
237 }
238
239 pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
240 self.expr(path.span, ast::ExprKind::Path(None, path))
241 }
242
243 pub fn expr_ident(&self, span: Span, id: Ident) -> P<ast::Expr> {
244 self.expr_path(self.path_ident(span, id))
245 }
246 pub fn expr_self(&self, span: Span) -> P<ast::Expr> {
247 self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
248 }
249
250 pub fn expr_binary(
251 &self,
252 sp: Span,
253 op: ast::BinOpKind,
254 lhs: P<ast::Expr>,
255 rhs: P<ast::Expr>,
256 ) -> P<ast::Expr> {
257 self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
258 }
259
260 pub fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
261 self.expr(sp, ast::ExprKind::Unary(UnOp::Deref, e))
262 }
263
264 pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
265 self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
266 }
267
268 pub fn expr_call(
269 &self,
270 span: Span,
271 expr: P<ast::Expr>,
272 args: Vec<P<ast::Expr>>,
273 ) -> P<ast::Expr> {
274 self.expr(span, ast::ExprKind::Call(expr, args))
275 }
276 pub fn expr_call_ident(&self, span: Span, id: Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
277 self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
278 }
279 pub fn expr_call_global(
280 &self,
281 sp: Span,
282 fn_path: Vec<Ident>,
283 args: Vec<P<ast::Expr>>,
284 ) -> P<ast::Expr> {
285 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
286 self.expr_call(sp, pathexpr, args)
287 }
288 pub fn expr_method_call(
289 &self,
290 span: Span,
291 expr: P<ast::Expr>,
292 ident: Ident,
293 mut args: Vec<P<ast::Expr>>,
294 ) -> P<ast::Expr> {
295 args.insert(0, expr);
296 let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
297 self.expr(span, ast::ExprKind::MethodCall(segment, args, span))
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::Field {
303 ast::Field {
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::Field>,
318 ) -> P<ast::Expr> {
319 self.expr(span, ast::ExprKind::Struct(path, fields, None))
320 }
321 pub fn expr_struct_ident(
322 &self,
323 span: Span,
324 id: Ident,
325 fields: Vec<ast::Field>,
326 ) -> P<ast::Expr> {
327 self.expr_struct(span, self.path_ident(span, id), fields)
328 }
329
330 pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
331 let lit = ast::Lit::from_lit_kind(lit_kind, span);
332 self.expr(span, ast::ExprKind::Lit(lit))
333 }
334 pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
335 self.expr_lit(
336 span,
337 ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
338 )
339 }
340 pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
341 self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
342 }
343 pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
344 self.expr_lit(sp, ast::LitKind::Bool(value))
345 }
346
347 pub fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
348 self.expr(sp, ast::ExprKind::Array(exprs))
349 }
350 pub fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
351 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
352 }
353 pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
354 self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
355 }
356
357 pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
358 self.expr(sp, ast::ExprKind::Cast(expr, ty))
359 }
360
361 pub fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
362 let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
363 self.expr_call_global(sp, some, vec![expr])
364 }
365
366 pub fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
367 self.expr(sp, ast::ExprKind::Tup(exprs))
368 }
369
370 pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
371 self.expr_call_global(
372 span,
373 [sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
374 vec![self.expr_str(span, msg)],
375 )
376 }
377
378 pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
379 self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
380 }
381
382 pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
383 let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
384 self.expr_call_global(sp, ok, vec![expr])
385 }
386
387 pub fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
388 let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
389 let ok_path = self.path_global(sp, ok);
390 let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
391 let err_path = self.path_global(sp, err);
392
393 let binding_variable = Ident::new(sym::__try_var, sp);
394 let binding_pat = self.pat_ident(sp, binding_variable);
395 let binding_expr = self.expr_ident(sp, binding_variable);
396
397 // `Ok(__try_var)` pattern
398 let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
399
400 // `Err(__try_var)` (pattern and expression respectively)
401 let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
402 let err_inner_expr =
403 self.expr_call(sp, self.expr_path(err_path), vec![binding_expr.clone()]);
404 // `return Err(__try_var)`
405 let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
406
407 // `Ok(__try_var) => __try_var`
408 let ok_arm = self.arm(sp, ok_pat, binding_expr);
409 // `Err(__try_var) => return Err(__try_var)`
410 let err_arm = self.arm(sp, err_pat, err_expr);
411
412 // `match head { Ok() => ..., Err() => ... }`
413 self.expr_match(sp, head, vec![ok_arm, err_arm])
414 }
415
416 pub fn pat(&self, span: Span, kind: PatKind) -> P<ast::Pat> {
417 P(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None })
418 }
419 pub fn pat_wild(&self, span: Span) -> P<ast::Pat> {
420 self.pat(span, PatKind::Wild)
421 }
422 pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
423 self.pat(span, PatKind::Lit(expr))
424 }
425 pub fn pat_ident(&self, span: Span, ident: Ident) -> P<ast::Pat> {
426 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Not);
427 self.pat_ident_binding_mode(span, ident, binding_mode)
428 }
429
430 pub fn pat_ident_binding_mode(
431 &self,
432 span: Span,
433 ident: Ident,
434 bm: ast::BindingMode,
435 ) -> P<ast::Pat> {
436 let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
437 self.pat(span, pat)
438 }
439 pub fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
440 self.pat(span, PatKind::Path(None, path))
441 }
442 pub fn pat_tuple_struct(
443 &self,
444 span: Span,
445 path: ast::Path,
446 subpats: Vec<P<ast::Pat>>,
447 ) -> P<ast::Pat> {
448 self.pat(span, PatKind::TupleStruct(path, subpats))
449 }
450 pub fn pat_struct(
451 &self,
452 span: Span,
453 path: ast::Path,
454 field_pats: Vec<ast::FieldPat>,
455 ) -> P<ast::Pat> {
456 self.pat(span, PatKind::Struct(path, field_pats, false))
457 }
458 pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
459 self.pat(span, PatKind::Tuple(pats))
460 }
461
462 pub fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
463 let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
464 let path = self.path_global(span, some);
465 self.pat_tuple_struct(span, path, vec![pat])
466 }
467
468 pub fn pat_none(&self, span: Span) -> P<ast::Pat> {
469 let some = self.std_path(&[sym::option, sym::Option, sym::None]);
470 let path = self.path_global(span, some);
471 self.pat_path(span, path)
472 }
473
474 pub fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
475 let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
476 let path = self.path_global(span, some);
477 self.pat_tuple_struct(span, path, vec![pat])
478 }
479
480 pub fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
481 let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
482 let path = self.path_global(span, some);
483 self.pat_tuple_struct(span, path, vec![pat])
484 }
485
486 pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm {
487 ast::Arm {
488 attrs: vec![],
489 pat,
490 guard: None,
491 body: expr,
492 span,
493 id: ast::DUMMY_NODE_ID,
494 is_placeholder: false,
495 }
496 }
497
498 pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
499 self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
500 }
501
502 pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
503 self.expr(span, ast::ExprKind::Match(arg, arms))
504 }
505
506 pub fn expr_if(
507 &self,
508 span: Span,
509 cond: P<ast::Expr>,
510 then: P<ast::Expr>,
511 els: Option<P<ast::Expr>>,
512 ) -> P<ast::Expr> {
513 let els = els.map(|x| self.expr_block(self.block_expr(x)));
514 self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
515 }
516
517 pub fn lambda_fn_decl(
518 &self,
519 span: Span,
520 fn_decl: P<ast::FnDecl>,
521 body: P<ast::Expr>,
522 fn_decl_span: Span,
523 ) -> P<ast::Expr> {
524 self.expr(
525 span,
526 ast::ExprKind::Closure(
527 ast::CaptureBy::Ref,
528 ast::Async::No,
529 ast::Movability::Movable,
530 fn_decl,
531 body,
532 fn_decl_span,
533 ),
534 )
535 }
536
537 pub fn lambda(&self, span: Span, ids: Vec<Ident>, body: P<ast::Expr>) -> P<ast::Expr> {
538 let fn_decl = self.fn_decl(
539 ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
540 ast::FnRetTy::Default(span),
541 );
542
543 // FIXME -- We are using `span` as the span of the `|...|`
544 // part of the lambda, but it probably (maybe?) corresponds to
545 // the entire lambda body. Probably we should extend the API
546 // here, but that's not entirely clear.
547 self.expr(
548 span,
549 ast::ExprKind::Closure(
550 ast::CaptureBy::Ref,
551 ast::Async::No,
552 ast::Movability::Movable,
553 fn_decl,
554 body,
555 span,
556 ),
557 )
558 }
559
560 pub fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
561 self.lambda(span, Vec::new(), body)
562 }
563
564 pub fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: Ident) -> P<ast::Expr> {
565 self.lambda(span, vec![ident], body)
566 }
567
568 pub fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>, ident: Ident) -> P<ast::Expr> {
569 self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
570 }
571
572 pub fn param(&self, span: Span, ident: Ident, ty: P<ast::Ty>) -> ast::Param {
573 let arg_pat = self.pat_ident(span, ident);
574 ast::Param {
575 attrs: AttrVec::default(),
576 id: ast::DUMMY_NODE_ID,
577 pat: arg_pat,
578 span,
579 ty,
580 is_placeholder: false,
581 }
582 }
583
584 // FIXME: unused `self`
585 pub fn fn_decl(&self, inputs: Vec<ast::Param>, output: ast::FnRetTy) -> P<ast::FnDecl> {
586 P(ast::FnDecl { inputs, output })
587 }
588
589 pub fn item(
590 &self,
591 span: Span,
592 name: Ident,
593 attrs: Vec<ast::Attribute>,
594 kind: ast::ItemKind,
595 ) -> P<ast::Item> {
596 // FIXME: Would be nice if our generated code didn't violate
597 // Rust coding conventions
598 P(ast::Item {
599 ident: name,
600 attrs,
601 id: ast::DUMMY_NODE_ID,
602 kind,
603 vis: ast::Visibility {
604 span: span.shrink_to_lo(),
605 kind: ast::VisibilityKind::Inherited,
606 tokens: None,
607 },
608 span,
609 tokens: None,
610 })
611 }
612
613 pub fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>>) -> ast::Variant {
614 let vis_span = span.shrink_to_lo();
615 let fields: Vec<_> = tys
616 .into_iter()
617 .map(|ty| ast::StructField {
618 span: ty.span,
619 ty,
620 ident: None,
621 vis: ast::Visibility {
622 span: vis_span,
623 kind: ast::VisibilityKind::Inherited,
624 tokens: None,
625 },
626 attrs: Vec::new(),
627 id: ast::DUMMY_NODE_ID,
628 is_placeholder: false,
629 })
630 .collect();
631
632 let vdata = if fields.is_empty() {
633 ast::VariantData::Unit(ast::DUMMY_NODE_ID)
634 } else {
635 ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
636 };
637
638 ast::Variant {
639 attrs: Vec::new(),
640 data: vdata,
641 disr_expr: None,
642 id: ast::DUMMY_NODE_ID,
643 ident,
644 vis: ast::Visibility {
645 span: vis_span,
646 kind: ast::VisibilityKind::Inherited,
647 tokens: None,
648 },
649 span,
650 is_placeholder: false,
651 }
652 }
653
654 pub fn item_static(
655 &self,
656 span: Span,
657 name: Ident,
658 ty: P<ast::Ty>,
659 mutbl: ast::Mutability,
660 expr: P<ast::Expr>,
661 ) -> P<ast::Item> {
662 self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr)))
663 }
664
665 pub fn item_const(
666 &self,
667 span: Span,
668 name: Ident,
669 ty: P<ast::Ty>,
670 expr: P<ast::Expr>,
671 ) -> P<ast::Item> {
672 let def = ast::Defaultness::Final;
673 self.item(span, name, Vec::new(), ast::ItemKind::Const(def, ty, Some(expr)))
674 }
675
676 pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute {
677 attr::mk_attr_outer(mi)
678 }
679
680 pub fn meta_word(&self, sp: Span, w: Symbol) -> ast::MetaItem {
681 attr::mk_word_item(Ident::new(w, sp))
682 }
683 }