rustc_expand/
build.rs

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