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