rustc_expand/
build.rs

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