1use rustc_ast::token::Delimiter;
2use rustc_ast::tokenstream::TokenStream;
3use rustc_ast::util::literal;
4use rustc_ast::{
5 self as ast, AnonConst, AttrItem, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind, PatKind,
6 UnOp, attr, token, tokenstream,
7};
8use rustc_span::source_map::Spanned;
9use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
10use thin_vec::{ThinVec, thin_vec};
11
12use crate::base::ExtCtxt;
13
14impl<'a> ExtCtxt<'a> {
15 pub fn path(&self, span: Span, strs: Vec<Ident>) -> ast::Path {
16 self.path_all(span, false, strs, vec![])
17 }
18 pub fn path_ident(&self, span: Span, id: Ident) -> ast::Path {
19 self.path(span, vec![id])
20 }
21 pub fn path_global(&self, span: Span, strs: Vec<Ident>) -> ast::Path {
22 self.path_all(span, true, strs, vec![])
23 }
24 pub fn path_all(
25 &self,
26 span: Span,
27 global: bool,
28 mut idents: Vec<Ident>,
29 args: Vec<ast::GenericArg>,
30 ) -> ast::Path {
31 assert!(!idents.is_empty());
32 let add_root = global && !idents[0].is_path_segment_keyword();
33 let mut segments = ThinVec::with_capacity(idents.len() + add_root as usize);
34 if add_root {
35 segments.push(ast::PathSegment::path_root(span));
36 }
37 let last_ident = idents.pop().unwrap();
38 segments.extend(
39 idents.into_iter().map(|ident| ast::PathSegment::from_ident(ident.with_span_pos(span))),
40 );
41 let args = if !args.is_empty() {
42 let args = args.into_iter().map(ast::AngleBracketedArg::Arg).collect();
43 Some(ast::AngleBracketedArgs { args, span }.into())
44 } else {
45 None
46 };
47 segments.push(ast::PathSegment {
48 ident: last_ident.with_span_pos(span),
49 id: ast::DUMMY_NODE_ID,
50 args,
51 });
52 ast::Path { span, segments, tokens: None }
53 }
54
55 pub fn macro_call(
56 &self,
57 span: Span,
58 path: ast::Path,
59 delim: Delimiter,
60 tokens: TokenStream,
61 ) -> Box<ast::MacCall> {
62 Box::new(ast::MacCall {
63 path,
64 args: Box::new(ast::DelimArgs {
65 dspan: tokenstream::DelimSpan { open: span, close: span },
66 delim,
67 tokens,
68 }),
69 })
70 }
71
72 pub fn ty_mt(&self, ty: Box<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
73 ast::MutTy { ty, mutbl }
74 }
75
76 pub fn ty(&self, span: Span, kind: ast::TyKind) -> Box<ast::Ty> {
77 Box::new(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind, tokens: None })
78 }
79
80 pub fn ty_infer(&self, span: Span) -> Box<ast::Ty> {
81 self.ty(span, ast::TyKind::Infer)
82 }
83
84 pub fn ty_path(&self, path: ast::Path) -> Box<ast::Ty> {
85 self.ty(path.span, ast::TyKind::Path(None, path))
86 }
87
88 pub fn ty_ident(&self, span: Span, ident: Ident) -> Box<ast::Ty> {
91 self.ty_path(self.path_ident(span, ident))
92 }
93
94 pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
95 ast::AnonConst {
96 id: ast::DUMMY_NODE_ID,
97 value: Box::new(ast::Expr {
98 id: ast::DUMMY_NODE_ID,
99 kind,
100 span,
101 attrs: AttrVec::new(),
102 tokens: None,
103 }),
104 }
105 }
106
107 pub fn anon_const_block(&self, b: Box<ast::Block>) -> Box<ast::AnonConst> {
108 Box::new(self.anon_const(b.span, ast::ExprKind::Block(b, None)))
109 }
110
111 pub fn const_ident(&self, span: Span, ident: Ident) -> ast::AnonConst {
112 self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
113 }
114
115 pub fn ty_ref(
116 &self,
117 span: Span,
118 ty: Box<ast::Ty>,
119 lifetime: Option<ast::Lifetime>,
120 mutbl: ast::Mutability,
121 ) -> Box<ast::Ty> {
122 self.ty(span, ast::TyKind::Ref(lifetime, self.ty_mt(ty, mutbl)))
123 }
124
125 pub fn ty_ptr(&self, span: Span, ty: Box<ast::Ty>, mutbl: ast::Mutability) -> Box<ast::Ty> {
126 self.ty(span, ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
127 }
128
129 pub fn typaram(
130 &self,
131 span: Span,
132 ident: Ident,
133 bounds: ast::GenericBounds,
134 default: Option<Box<ast::Ty>>,
135 ) -> ast::GenericParam {
136 ast::GenericParam {
137 ident: ident.with_span_pos(span),
138 id: ast::DUMMY_NODE_ID,
139 attrs: AttrVec::new(),
140 bounds,
141 kind: ast::GenericParamKind::Type { default },
142 is_placeholder: false,
143 colon_span: None,
144 }
145 }
146
147 pub fn lifetime_param(
148 &self,
149 span: Span,
150 ident: Ident,
151 bounds: ast::GenericBounds,
152 ) -> ast::GenericParam {
153 ast::GenericParam {
154 id: ast::DUMMY_NODE_ID,
155 ident: ident.with_span_pos(span),
156 attrs: AttrVec::new(),
157 bounds,
158 is_placeholder: false,
159 kind: ast::GenericParamKind::Lifetime,
160 colon_span: None,
161 }
162 }
163
164 pub fn const_param(
165 &self,
166 span: Span,
167 ident: Ident,
168 bounds: ast::GenericBounds,
169 ty: Box<ast::Ty>,
170 default: Option<AnonConst>,
171 ) -> ast::GenericParam {
172 ast::GenericParam {
173 id: ast::DUMMY_NODE_ID,
174 ident: ident.with_span_pos(span),
175 attrs: AttrVec::new(),
176 bounds,
177 is_placeholder: false,
178 kind: ast::GenericParamKind::Const { ty, span: DUMMY_SP, default },
179 colon_span: None,
180 }
181 }
182
183 pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
184 ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
185 }
186
187 pub fn poly_trait_ref(&self, span: Span, path: ast::Path, is_const: bool) -> ast::PolyTraitRef {
188 ast::PolyTraitRef {
189 bound_generic_params: ThinVec::new(),
190 modifiers: ast::TraitBoundModifiers {
191 polarity: ast::BoundPolarity::Positive,
192 constness: if is_const {
193 ast::BoundConstness::Maybe(DUMMY_SP)
194 } else {
195 ast::BoundConstness::Never
196 },
197 asyncness: ast::BoundAsyncness::Normal,
198 },
199 trait_ref: self.trait_ref(path),
200 span,
201 parens: ast::Parens::No,
202 }
203 }
204
205 pub fn trait_bound(&self, path: ast::Path, is_const: bool) -> ast::GenericBound {
206 ast::GenericBound::Trait(self.poly_trait_ref(path.span, path, is_const))
207 }
208
209 pub fn lifetime(&self, span: Span, ident: Ident) -> ast::Lifetime {
210 ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
211 }
212
213 pub fn lifetime_static(&self, span: Span) -> ast::Lifetime {
214 self.lifetime(span, Ident::new(kw::StaticLifetime, span))
215 }
216
217 pub fn stmt_expr(&self, expr: Box<ast::Expr>) -> ast::Stmt {
218 ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Expr(expr) }
219 }
220
221 pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: Ident, ex: Box<ast::Expr>) -> ast::Stmt {
222 self.stmt_let_ty(sp, mutbl, ident, None, ex)
223 }
224
225 pub fn stmt_let_ty(
226 &self,
227 sp: Span,
228 mutbl: bool,
229 ident: Ident,
230 ty: Option<Box<ast::Ty>>,
231 ex: Box<ast::Expr>,
232 ) -> ast::Stmt {
233 let pat = if mutbl {
234 self.pat_ident_binding_mode(sp, ident, ast::BindingMode::MUT)
235 } else {
236 self.pat_ident(sp, ident)
237 };
238 let local = Box::new(ast::Local {
239 super_: None,
240 pat: Box::new(pat),
241 ty,
242 id: ast::DUMMY_NODE_ID,
243 kind: LocalKind::Init(ex),
244 span: sp,
245 colon_sp: None,
246 attrs: AttrVec::new(),
247 tokens: None,
248 });
249 self.stmt_local(local, sp)
250 }
251
252 pub fn stmt_let_type_only(&self, span: Span, ty: Box<ast::Ty>) -> ast::Stmt {
254 let local = Box::new(ast::Local {
255 super_: None,
256 pat: Box::new(self.pat_wild(span)),
257 ty: Some(ty),
258 id: ast::DUMMY_NODE_ID,
259 kind: LocalKind::Decl,
260 span,
261 colon_sp: None,
262 attrs: AttrVec::new(),
263 tokens: None,
264 });
265 self.stmt_local(local, span)
266 }
267
268 pub fn stmt_semi(&self, expr: Box<ast::Expr>) -> ast::Stmt {
269 ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Semi(expr) }
270 }
271
272 pub fn stmt_local(&self, local: Box<ast::Local>, span: Span) -> ast::Stmt {
273 ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Let(local), span }
274 }
275
276 pub fn stmt_item(&self, sp: Span, item: Box<ast::Item>) -> ast::Stmt {
277 ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Item(item), span: sp }
278 }
279
280 pub fn block_expr(&self, expr: Box<ast::Expr>) -> Box<ast::Block> {
281 self.block(
282 expr.span,
283 thin_vec![ast::Stmt {
284 id: ast::DUMMY_NODE_ID,
285 span: expr.span,
286 kind: ast::StmtKind::Expr(expr),
287 }],
288 )
289 }
290 pub fn block(&self, span: Span, stmts: ThinVec<ast::Stmt>) -> Box<ast::Block> {
291 Box::new(ast::Block {
292 stmts,
293 id: ast::DUMMY_NODE_ID,
294 rules: BlockCheckMode::Default,
295 span,
296 tokens: None,
297 })
298 }
299
300 pub fn expr(&self, span: Span, kind: ast::ExprKind) -> Box<ast::Expr> {
301 Box::new(ast::Expr {
302 id: ast::DUMMY_NODE_ID,
303 kind,
304 span,
305 attrs: AttrVec::new(),
306 tokens: None,
307 })
308 }
309
310 pub fn expr_path(&self, path: ast::Path) -> Box<ast::Expr> {
311 self.expr(path.span, ast::ExprKind::Path(None, path))
312 }
313
314 pub fn expr_ident(&self, span: Span, id: Ident) -> Box<ast::Expr> {
315 self.expr_path(self.path_ident(span, id))
316 }
317 pub fn expr_self(&self, span: Span) -> Box<ast::Expr> {
318 self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
319 }
320
321 pub fn expr_macro_call(&self, span: Span, call: Box<ast::MacCall>) -> Box<ast::Expr> {
322 self.expr(span, ast::ExprKind::MacCall(call))
323 }
324
325 pub fn expr_binary(
326 &self,
327 sp: Span,
328 op: ast::BinOpKind,
329 lhs: Box<ast::Expr>,
330 rhs: Box<ast::Expr>,
331 ) -> Box<ast::Expr> {
332 self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
333 }
334
335 pub fn expr_deref(&self, sp: Span, e: Box<ast::Expr>) -> Box<ast::Expr> {
336 self.expr(sp, ast::ExprKind::Unary(UnOp::Deref, e))
337 }
338
339 pub fn expr_addr_of(&self, sp: Span, e: Box<ast::Expr>) -> Box<ast::Expr> {
340 self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
341 }
342
343 pub fn expr_paren(&self, sp: Span, e: Box<ast::Expr>) -> Box<ast::Expr> {
344 self.expr(sp, ast::ExprKind::Paren(e))
345 }
346
347 pub fn expr_method_call(
348 &self,
349 span: Span,
350 expr: Box<ast::Expr>,
351 ident: Ident,
352 args: ThinVec<Box<ast::Expr>>,
353 ) -> Box<ast::Expr> {
354 let seg = ast::PathSegment::from_ident(ident);
355 self.expr(
356 span,
357 ast::ExprKind::MethodCall(Box::new(ast::MethodCall {
358 seg,
359 receiver: expr,
360 args,
361 span,
362 })),
363 )
364 }
365
366 pub fn expr_call(
367 &self,
368 span: Span,
369 expr: Box<ast::Expr>,
370 args: ThinVec<Box<ast::Expr>>,
371 ) -> Box<ast::Expr> {
372 self.expr(span, ast::ExprKind::Call(expr, args))
373 }
374 pub fn expr_loop(&self, sp: Span, block: Box<ast::Block>) -> Box<ast::Expr> {
375 self.expr(sp, ast::ExprKind::Loop(block, None, sp))
376 }
377 pub fn expr_asm(&self, sp: Span, expr: Box<ast::InlineAsm>) -> Box<ast::Expr> {
378 self.expr(sp, ast::ExprKind::InlineAsm(expr))
379 }
380 pub fn expr_call_ident(
381 &self,
382 span: Span,
383 id: Ident,
384 args: ThinVec<Box<ast::Expr>>,
385 ) -> Box<ast::Expr> {
386 self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
387 }
388 pub fn expr_call_global(
389 &self,
390 sp: Span,
391 fn_path: Vec<Ident>,
392 args: ThinVec<Box<ast::Expr>>,
393 ) -> Box<ast::Expr> {
394 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
395 self.expr_call(sp, pathexpr, args)
396 }
397 pub fn expr_block(&self, b: Box<ast::Block>) -> Box<ast::Expr> {
398 self.expr(b.span, ast::ExprKind::Block(b, None))
399 }
400 pub fn field_imm(&self, span: Span, ident: Ident, e: Box<ast::Expr>) -> ast::ExprField {
401 ast::ExprField {
402 ident: ident.with_span_pos(span),
403 expr: e,
404 span,
405 is_shorthand: false,
406 attrs: AttrVec::new(),
407 id: ast::DUMMY_NODE_ID,
408 is_placeholder: false,
409 }
410 }
411 pub fn expr_struct(
412 &self,
413 span: Span,
414 path: ast::Path,
415 fields: ThinVec<ast::ExprField>,
416 ) -> Box<ast::Expr> {
417 self.expr(
418 span,
419 ast::ExprKind::Struct(Box::new(ast::StructExpr {
420 qself: None,
421 path,
422 fields,
423 rest: ast::StructRest::None,
424 })),
425 )
426 }
427 pub fn expr_struct_ident(
428 &self,
429 span: Span,
430 id: Ident,
431 fields: ThinVec<ast::ExprField>,
432 ) -> Box<ast::Expr> {
433 self.expr_struct(span, self.path_ident(span, id), fields)
434 }
435
436 pub fn expr_usize(&self, span: Span, n: usize) -> Box<ast::Expr> {
437 let suffix = Some(ast::UintTy::Usize.name());
438 let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
439 self.expr(span, ast::ExprKind::Lit(lit))
440 }
441
442 pub fn expr_u32(&self, span: Span, n: u32) -> Box<ast::Expr> {
443 let suffix = Some(ast::UintTy::U32.name());
444 let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
445 self.expr(span, ast::ExprKind::Lit(lit))
446 }
447
448 pub fn expr_bool(&self, span: Span, value: bool) -> Box<ast::Expr> {
449 let lit = token::Lit::new(token::Bool, if value { kw::True } else { kw::False }, None);
450 self.expr(span, ast::ExprKind::Lit(lit))
451 }
452
453 pub fn expr_str(&self, span: Span, s: Symbol) -> Box<ast::Expr> {
454 let lit = token::Lit::new(token::Str, literal::escape_string_symbol(s), None);
455 self.expr(span, ast::ExprKind::Lit(lit))
456 }
457
458 pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> Box<ast::Expr> {
459 let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
460 self.expr(span, ast::ExprKind::Lit(lit))
461 }
462
463 pub fn expr_array(&self, sp: Span, exprs: ThinVec<Box<ast::Expr>>) -> Box<ast::Expr> {
465 self.expr(sp, ast::ExprKind::Array(exprs))
466 }
467
468 pub fn expr_array_ref(&self, sp: Span, exprs: ThinVec<Box<ast::Expr>>) -> Box<ast::Expr> {
470 self.expr_addr_of(sp, self.expr_array(sp, exprs))
471 }
472
473 pub fn expr_some(&self, sp: Span, expr: Box<ast::Expr>) -> Box<ast::Expr> {
474 let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
475 self.expr_call_global(sp, some, thin_vec![expr])
476 }
477
478 pub fn expr_none(&self, sp: Span) -> Box<ast::Expr> {
479 let none = self.std_path(&[sym::option, sym::Option, sym::None]);
480 self.expr_path(self.path_global(sp, none))
481 }
482 pub fn expr_tuple(&self, sp: Span, exprs: ThinVec<Box<ast::Expr>>) -> Box<ast::Expr> {
483 self.expr(sp, ast::ExprKind::Tup(exprs))
484 }
485
486 pub fn expr_unreachable(&self, span: Span) -> Box<ast::Expr> {
487 self.expr_macro_call(
488 span,
489 self.macro_call(
490 span,
491 self.path_global(
492 span,
493 [sym::std, sym::unreachable].map(|s| Ident::new(s, span)).to_vec(),
494 ),
495 Delimiter::Parenthesis,
496 TokenStream::default(),
497 ),
498 )
499 }
500
501 pub fn expr_ok(&self, sp: Span, expr: Box<ast::Expr>) -> Box<ast::Expr> {
502 let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
503 self.expr_call_global(sp, ok, thin_vec![expr])
504 }
505
506 pub fn expr_try(&self, sp: Span, head: Box<ast::Expr>) -> Box<ast::Expr> {
507 let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
508 let ok_path = self.path_global(sp, ok);
509 let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
510 let err_path = self.path_global(sp, err);
511
512 let binding_variable = Ident::new(sym::__try_var, sp);
513 let binding_pat = self.pat_ident(sp, binding_variable);
514 let binding_expr = self.expr_ident(sp, binding_variable);
515
516 let ok_pat = self.pat_tuple_struct(sp, ok_path, thin_vec![binding_pat.clone()]);
518
519 let err_pat = self.pat_tuple_struct(sp, err_path.clone(), thin_vec![binding_pat]);
521 let err_inner_expr =
522 self.expr_call(sp, self.expr_path(err_path), thin_vec![binding_expr.clone()]);
523 let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
525
526 let ok_arm = self.arm(sp, ok_pat, binding_expr);
528 let err_arm = self.arm(sp, err_pat, err_expr);
530
531 self.expr_match(sp, head, thin_vec![ok_arm, err_arm])
533 }
534
535 pub fn pat(&self, span: Span, kind: PatKind) -> ast::Pat {
536 ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None }
537 }
538 pub fn pat_wild(&self, span: Span) -> ast::Pat {
539 self.pat(span, PatKind::Wild)
540 }
541 pub fn pat_lit(&self, span: Span, expr: Box<ast::Expr>) -> ast::Pat {
542 self.pat(span, PatKind::Expr(expr))
543 }
544 pub fn pat_ident(&self, span: Span, ident: Ident) -> ast::Pat {
545 self.pat_ident_binding_mode(span, ident, ast::BindingMode::NONE)
546 }
547
548 pub fn pat_ident_binding_mode(
549 &self,
550 span: Span,
551 ident: Ident,
552 ann: ast::BindingMode,
553 ) -> ast::Pat {
554 let pat = PatKind::Ident(ann, ident.with_span_pos(span), None);
555 self.pat(span, pat)
556 }
557 pub fn pat_path(&self, span: Span, path: ast::Path) -> ast::Pat {
558 self.pat(span, PatKind::Path(None, path))
559 }
560 pub fn pat_tuple_struct(
561 &self,
562 span: Span,
563 path: ast::Path,
564 subpats: ThinVec<ast::Pat>,
565 ) -> ast::Pat {
566 self.pat(span, PatKind::TupleStruct(None, path, subpats))
567 }
568 pub fn pat_struct(
569 &self,
570 span: Span,
571 path: ast::Path,
572 field_pats: ThinVec<ast::PatField>,
573 ) -> ast::Pat {
574 self.pat(span, PatKind::Struct(None, path, field_pats, ast::PatFieldsRest::None))
575 }
576 pub fn pat_tuple(&self, span: Span, pats: ThinVec<ast::Pat>) -> ast::Pat {
577 self.pat(span, PatKind::Tuple(pats))
578 }
579
580 pub fn pat_some(&self, span: Span, pat: ast::Pat) -> ast::Pat {
581 let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
582 let path = self.path_global(span, some);
583 self.pat_tuple_struct(span, path, thin_vec![pat])
584 }
585
586 pub fn arm(&self, span: Span, pat: ast::Pat, expr: Box<ast::Expr>) -> ast::Arm {
587 ast::Arm {
588 attrs: AttrVec::new(),
589 pat: Box::new(pat),
590 guard: None,
591 body: Some(expr),
592 span,
593 id: ast::DUMMY_NODE_ID,
594 is_placeholder: false,
595 }
596 }
597
598 pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
599 self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
600 }
601
602 pub fn expr_match(
603 &self,
604 span: Span,
605 arg: Box<ast::Expr>,
606 arms: ThinVec<ast::Arm>,
607 ) -> Box<Expr> {
608 self.expr(span, ast::ExprKind::Match(arg, arms, MatchKind::Prefix))
609 }
610
611 pub fn expr_if(
612 &self,
613 span: Span,
614 cond: Box<ast::Expr>,
615 then: Box<ast::Expr>,
616 els: Option<Box<ast::Expr>>,
617 ) -> Box<ast::Expr> {
618 let els = els.map(|x| self.expr_block(self.block_expr(x)));
619 self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
620 }
621
622 pub fn lambda(&self, span: Span, ids: Vec<Ident>, body: Box<ast::Expr>) -> Box<ast::Expr> {
623 let fn_decl = self.fn_decl(
624 ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
625 ast::FnRetTy::Default(span),
626 );
627
628 self.expr(
633 span,
634 ast::ExprKind::Closure(Box::new(ast::Closure {
635 binder: ast::ClosureBinder::NotPresent,
636 capture_clause: ast::CaptureBy::Ref,
637 constness: ast::Const::No,
638 coroutine_kind: None,
639 movability: ast::Movability::Movable,
640 fn_decl,
641 body,
642 fn_decl_span: span,
643 fn_arg_span: span,
646 })),
647 )
648 }
649
650 pub fn lambda0(&self, span: Span, body: Box<ast::Expr>) -> Box<ast::Expr> {
651 self.lambda(span, Vec::new(), body)
652 }
653
654 pub fn lambda1(&self, span: Span, body: Box<ast::Expr>, ident: Ident) -> Box<ast::Expr> {
655 self.lambda(span, vec![ident], body)
656 }
657
658 pub fn lambda_stmts_1(
659 &self,
660 span: Span,
661 stmts: ThinVec<ast::Stmt>,
662 ident: Ident,
663 ) -> Box<ast::Expr> {
664 self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
665 }
666
667 pub fn param(&self, span: Span, ident: Ident, ty: Box<ast::Ty>) -> ast::Param {
668 let pat = Box::new(self.pat_ident(span, ident));
669 ast::Param {
670 attrs: AttrVec::default(),
671 id: ast::DUMMY_NODE_ID,
672 pat,
673 span,
674 ty,
675 is_placeholder: false,
676 }
677 }
678
679 pub fn fn_decl(&self, inputs: ThinVec<ast::Param>, output: ast::FnRetTy) -> Box<ast::FnDecl> {
681 Box::new(ast::FnDecl { inputs, output })
682 }
683
684 pub fn item(&self, span: Span, attrs: ast::AttrVec, kind: ast::ItemKind) -> Box<ast::Item> {
685 Box::new(ast::Item {
686 attrs,
687 id: ast::DUMMY_NODE_ID,
688 kind,
689 vis: ast::Visibility {
690 span: span.shrink_to_lo(),
691 kind: ast::VisibilityKind::Inherited,
692 tokens: None,
693 },
694 span,
695 tokens: None,
696 })
697 }
698
699 pub fn item_static(
700 &self,
701 span: Span,
702 ident: Ident,
703 ty: Box<ast::Ty>,
704 mutability: ast::Mutability,
705 expr: Box<ast::Expr>,
706 ) -> Box<ast::Item> {
707 self.item(
708 span,
709 AttrVec::new(),
710 ast::ItemKind::Static(
711 ast::StaticItem {
712 ident,
713 ty,
714 safety: ast::Safety::Default,
715 mutability,
716 expr: Some(expr),
717 define_opaque: None,
718 }
719 .into(),
720 ),
721 )
722 }
723
724 pub fn item_const(
725 &self,
726 span: Span,
727 ident: Ident,
728 ty: Box<ast::Ty>,
729 rhs: ast::ConstItemRhs,
730 ) -> Box<ast::Item> {
731 let defaultness = ast::Defaultness::Final;
732 self.item(
733 span,
734 AttrVec::new(),
735 ast::ItemKind::Const(
736 ast::ConstItem {
737 defaultness,
738 ident,
739 generics: ast::Generics::default(),
741 ty,
742 rhs: Some(rhs),
743 define_opaque: None,
744 }
745 .into(),
746 ),
747 )
748 }
749
750 pub fn attr_word(&self, name: Symbol, span: Span) -> ast::Attribute {
752 let g = &self.sess.psess.attr_id_generator;
753 attr::mk_attr_word(g, ast::AttrStyle::Outer, ast::Safety::Default, name, span)
754 }
755
756 pub fn attr_name_value_str(&self, name: Symbol, val: Symbol, span: Span) -> ast::Attribute {
760 let g = &self.sess.psess.attr_id_generator;
761 attr::mk_attr_name_value_str(
762 g,
763 ast::AttrStyle::Outer,
764 ast::Safety::Default,
765 name,
766 val,
767 span,
768 )
769 }
770
771 pub fn attr_nested_word(&self, outer: Symbol, inner: Symbol, span: Span) -> ast::Attribute {
773 let g = &self.sess.psess.attr_id_generator;
774 attr::mk_attr_nested_word(
775 g,
776 ast::AttrStyle::Outer,
777 ast::Safety::Default,
778 outer,
779 inner,
780 span,
781 )
782 }
783
784 pub fn attr_nested(&self, inner: AttrItem, span: Span) -> ast::Attribute {
786 let g = &self.sess.psess.attr_id_generator;
787 attr::mk_attr_from_item(g, inner, None, ast::AttrStyle::Outer, span)
788 }
789}