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