build.rs 18.7 KB
Newer Older
1 2
use crate::base::ExtCtxt;

3 4
use rustc_ast::attr;
use rustc_ast::ptr::P;
C
Cameron Steffen 已提交
5
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
6
use rustc_span::source_map::Spanned;
7
use rustc_span::symbol::{kw, sym, Ident, Symbol};
T
Taiki Endo 已提交
8

9
use rustc_span::Span;
10

11
impl<'a> ExtCtxt<'a> {
12
    pub fn path(&self, span: Span, strs: Vec<Ident>) -> ast::Path {
13
        self.path_all(span, false, strs, vec![])
14
    }
15
    pub fn path_ident(&self, span: Span, id: Ident) -> ast::Path {
16
        self.path(span, vec![id])
17
    }
18
    pub fn path_global(&self, span: Span, strs: Vec<Ident>) -> ast::Path {
19
        self.path_all(span, true, strs, vec![])
20
    }
M
Mark Rousskov 已提交
21 22 23 24
    pub fn path_all(
        &self,
        span: Span,
        global: bool,
25
        mut idents: Vec<Ident>,
M
Mark Rousskov 已提交
26 27
        args: Vec<ast::GenericArg>,
    ) -> ast::Path {
V
Vadim Petrochenkov 已提交
28 29 30 31 32 33
        assert!(!idents.is_empty());
        let add_root = global && !idents[0].is_path_segment_keyword();
        let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
        if add_root {
            segments.push(ast::PathSegment::path_root(span));
        }
34
        let last_ident = idents.pop().unwrap();
M
Mark Rousskov 已提交
35 36 37
        segments.extend(
            idents.into_iter().map(|ident| ast::PathSegment::from_ident(ident.with_span_pos(span))),
        );
38
        let args = if !args.is_empty() {
39 40
            let args = args.into_iter().map(ast::AngleBracketedArg::Arg).collect();
            ast::AngleBracketedArgs { args, span }.into()
J
Jeffrey Seyfried 已提交
41
        } else {
42
            None
J
Jeffrey Seyfried 已提交
43
        };
N
Nick Cameron 已提交
44 45 46 47 48
        segments.push(ast::PathSegment {
            ident: last_ident.with_span_pos(span),
            id: ast::DUMMY_NODE_ID,
            args,
        });
A
Aaron Hill 已提交
49
        ast::Path { span, segments, tokens: None }
50 51
    }

52
    pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
M
Mark Rousskov 已提交
53
        ast::MutTy { ty, mutbl }
54 55
    }

V
varkor 已提交
56
    pub fn ty(&self, span: Span, kind: ast::TyKind) -> P<ast::Ty> {
A
Aaron Hill 已提交
57
        P(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind, tokens: None })
58 59
    }

60
    pub fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
61
        self.ty(path.span, ast::TyKind::Path(None, path))
62 63
    }

64 65
    // Might need to take bounds as an argument in the future, if you ever want
    // to generate a bounded existential trait type.
66
    pub fn ty_ident(&self, span: Span, ident: Ident) -> P<ast::Ty> {
67
        self.ty_path(self.path_ident(span, ident))
68 69
    }

V
varkor 已提交
70
    pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
V
varkor 已提交
71 72
        ast::AnonConst {
            id: ast::DUMMY_NODE_ID,
A
Aaron Hill 已提交
73 74 75 76 77 78 79
            value: P(ast::Expr {
                id: ast::DUMMY_NODE_ID,
                kind,
                span,
                attrs: AttrVec::new(),
                tokens: None,
            }),
V
varkor 已提交
80 81 82
        }
    }

83
    pub fn const_ident(&self, span: Span, ident: Ident) -> ast::AnonConst {
V
varkor 已提交
84 85 86
        self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
    }

M
Mark Rousskov 已提交
87 88 89 90 91 92 93 94
    pub fn ty_rptr(
        &self,
        span: Span,
        ty: P<ast::Ty>,
        lifetime: Option<ast::Lifetime>,
        mutbl: ast::Mutability,
    ) -> P<ast::Ty> {
        self.ty(span, ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
95
    }
96

M
Mark Rousskov 已提交
97 98
    pub fn ty_ptr(&self, span: Span, ty: P<ast::Ty>, mutbl: ast::Mutability) -> P<ast::Ty> {
        self.ty(span, ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
99
    }
100

M
Mark Rousskov 已提交
101 102 103
    pub fn typaram(
        &self,
        span: Span,
104
        ident: Ident,
M
Mark Rousskov 已提交
105 106 107 108
        attrs: Vec<ast::Attribute>,
        bounds: ast::GenericBounds,
        default: Option<P<ast::Ty>>,
    ) -> ast::GenericParam {
V
varkor 已提交
109
        ast::GenericParam {
110
            ident: ident.with_span_pos(span),
111
            id: ast::DUMMY_NODE_ID,
112
            attrs: attrs.into(),
V
varkor 已提交
113
            bounds,
M
Mark Rousskov 已提交
114 115
            kind: ast::GenericParamKind::Type { default },
            is_placeholder: false,
116
        }
117 118
    }

119
    pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
120
        ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
N
Niko Matsakis 已提交
121 122
    }

123
    pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
N
Niko Matsakis 已提交
124
        ast::PolyTraitRef {
125
            bound_generic_params: Vec::new(),
126
            trait_ref: self.trait_ref(path),
127
            span,
128 129
        }
    }
130

131
    pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
M
Mark Rousskov 已提交
132 133 134 135
        ast::GenericBound::Trait(
            self.poly_trait_ref(path.span, path),
            ast::TraitBoundModifier::None,
        )
136 137
    }

138
    pub fn lifetime(&self, span: Span, ident: Ident) -> ast::Lifetime {
139
        ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
140 141
    }

142
    pub fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
143
        ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Expr(expr) }
144 145
    }

146
    pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: Ident, ex: P<ast::Expr>) -> ast::Stmt {
147
        let pat = if mutbl {
148
            let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mut);
149
            self.pat_ident_binding_mode(sp, ident, binding_mode)
150 151 152
        } else {
            self.pat_ident(sp, ident)
        };
153
        let local = P(ast::Local {
154
            pat,
S
Seo Sanghyeon 已提交
155
            ty: None,
156
            id: ast::DUMMY_NODE_ID,
C
Cameron Steffen 已提交
157
            kind: LocalKind::Init(ex),
158
            span: sp,
M
Mazdak Farrokhzad 已提交
159
            attrs: AttrVec::new(),
A
Aaron Hill 已提交
160
            tokens: None,
161 162
        });
        ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp }
163 164
    }

165
    // Generates `let _: Type;`, which is usually used for type assertions.
166
    pub fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
V
Vadim Petrochenkov 已提交
167 168 169 170
        let local = P(ast::Local {
            pat: self.pat_wild(span),
            ty: Some(ty),
            id: ast::DUMMY_NODE_ID,
C
Cameron Steffen 已提交
171
            kind: LocalKind::Decl,
172
            span,
M
Mazdak Farrokhzad 已提交
173
            attrs: AttrVec::new(),
174
            tokens: None,
V
Vadim Petrochenkov 已提交
175
        });
176
        ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span }
V
Vadim Petrochenkov 已提交
177 178
    }

179
    pub fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
180
        ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Item(item), span: sp }
181 182
    }

183
    pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
M
Mark Rousskov 已提交
184 185 186 187 188 189 190 191
        self.block(
            expr.span,
            vec![ast::Stmt {
                id: ast::DUMMY_NODE_ID,
                span: expr.span,
                kind: ast::StmtKind::Expr(expr),
            }],
        )
192
    }
193
    pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
194 195 196 197 198 199
        P(ast::Block {
            stmts,
            id: ast::DUMMY_NODE_ID,
            rules: BlockCheckMode::Default,
            span,
            tokens: None,
200
            could_be_bare_literal: false,
201
        })
202 203
    }

V
varkor 已提交
204
    pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
A
Aaron Hill 已提交
205
        P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new(), tokens: None })
206 207
    }

208
    pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
209
        self.expr(path.span, ast::ExprKind::Path(None, path))
210 211
    }

212
    pub fn expr_ident(&self, span: Span, id: Ident) -> P<ast::Expr> {
213 214
        self.expr_path(self.path_ident(span, id))
    }
215
    pub fn expr_self(&self, span: Span) -> P<ast::Expr> {
216
        self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
217 218
    }

M
Mark Rousskov 已提交
219 220 221 222 223 224 225
    pub fn expr_binary(
        &self,
        sp: Span,
        op: ast::BinOpKind,
        lhs: P<ast::Expr>,
        rhs: P<ast::Expr>,
    ) -> P<ast::Expr> {
226
        self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
227 228
    }

229
    pub fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
M
Mark Rousskov 已提交
230
        self.expr(sp, ast::ExprKind::Unary(UnOp::Deref, e))
231 232
    }

233
    pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
234
        self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
235
    }
236

237
    pub fn expr_call(
M
Mark Rousskov 已提交
238 239 240 241
        &self,
        span: Span,
        expr: P<ast::Expr>,
        args: Vec<P<ast::Expr>>,
242
    ) -> P<ast::Expr> {
243
        self.expr(span, ast::ExprKind::Call(expr, args))
244
    }
245
    pub fn expr_call_ident(&self, span: Span, id: Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
246
        self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
247
    }
M
Mark Rousskov 已提交
248 249 250
    pub fn expr_call_global(
        &self,
        sp: Span,
251
        fn_path: Vec<Ident>,
M
Mark Rousskov 已提交
252 253
        args: Vec<P<ast::Expr>>,
    ) -> P<ast::Expr> {
254 255 256
        let pathexpr = self.expr_path(self.path_global(sp, fn_path));
        self.expr_call(sp, pathexpr, args)
    }
257
    pub fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
258
        self.expr(b.span, ast::ExprKind::Block(b, None))
259
    }
260 261
    pub fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::ExprField {
        ast::ExprField {
V
Vadim Petrochenkov 已提交
262
            ident: ident.with_span_pos(span),
263
            expr: e,
264
            span,
265
            is_shorthand: false,
M
Mazdak Farrokhzad 已提交
266
            attrs: AttrVec::new(),
C
Caio 已提交
267
            id: ast::DUMMY_NODE_ID,
C
Caio 已提交
268
            is_placeholder: false,
269
        }
270
    }
271
    pub fn expr_struct(
M
Mark Rousskov 已提交
272 273 274
        &self,
        span: Span,
        path: ast::Path,
275
        fields: Vec<ast::ExprField>,
276
    ) -> P<ast::Expr> {
277 278
        self.expr(
            span,
279 280 281 282 283 284
            ast::ExprKind::Struct(P(ast::StructExpr {
                qself: None,
                path,
                fields,
                rest: ast::StructRest::None,
            })),
285
        )
286
    }
M
Mark Rousskov 已提交
287 288 289
    pub fn expr_struct_ident(
        &self,
        span: Span,
290
        id: Ident,
291
        fields: Vec<ast::ExprField>,
M
Mark Rousskov 已提交
292
    ) -> P<ast::Expr> {
293 294 295
        self.expr_struct(span, self.path_ident(span, id), fields)
    }

296
    pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
297 298
        let lit = ast::Lit::from_lit_kind(lit_kind, span);
        self.expr(span, ast::ExprKind::Lit(lit))
299
    }
300
    pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
M
Mark Rousskov 已提交
301 302 303 304
        self.expr_lit(
            span,
            ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
        )
305
    }
306
    pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
M
Mark Rousskov 已提交
307
        self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
N
Nick Cameron 已提交
308
    }
309
    pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
310
        self.expr_lit(sp, ast::LitKind::Bool(value))
311 312
    }

313
    pub fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
314
        self.expr(sp, ast::ExprKind::Array(exprs))
315
    }
316
    pub fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
N
Nick Cameron 已提交
317
        self.expr_addr_of(sp, self.expr_vec(sp, exprs))
318
    }
319
    pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
320
        self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
321 322
    }

323
    pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
324
        self.expr(sp, ast::ExprKind::Cast(expr, ty))
325 326
    }

327
    pub fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
328
        let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
329
        self.expr_call_global(sp, some, vec![expr])
330 331
    }

332
    pub fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
333
        self.expr(sp, ast::ExprKind::Tup(exprs))
334 335
    }

336
    pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
337 338
        self.expr_call_global(
            span,
339
            [sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
340
            vec![self.expr_str(span, msg)],
M
Mark Rousskov 已提交
341
        )
342 343
    }

344
    pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
345
        self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
346 347
    }

348
    pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
349
        let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
350
        self.expr_call_global(sp, ok, vec![expr])
S
Sean McArthur 已提交
351 352
    }

353
    pub fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
354
        let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
355
        let ok_path = self.path_global(sp, ok);
356
        let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
357
        let err_path = self.path_global(sp, err);
S
Sean McArthur 已提交
358

359
        let binding_variable = Ident::new(sym::__try_var, sp);
S
Sean McArthur 已提交
360 361 362
        let binding_pat = self.pat_ident(sp, binding_variable);
        let binding_expr = self.expr_ident(sp, binding_variable);

363
        // `Ok(__try_var)` pattern
364
        let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
S
Sean McArthur 已提交
365

366
        // `Err(__try_var)` (pattern and expression respectively)
367
        let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
M
Mark Rousskov 已提交
368 369
        let err_inner_expr =
            self.expr_call(sp, self.expr_path(err_path), vec![binding_expr.clone()]);
370
        // `return Err(__try_var)`
371
        let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
S
Sean McArthur 已提交
372

373
        // `Ok(__try_var) => __try_var`
374
        let ok_arm = self.arm(sp, ok_pat, binding_expr);
375
        // `Err(__try_var) => return Err(__try_var)`
376
        let err_arm = self.arm(sp, err_pat, err_expr);
S
Sean McArthur 已提交
377

378
        // `match head { Ok() => ..., Err() => ... }`
379
        self.expr_match(sp, head, vec![ok_arm, err_arm])
S
Sean McArthur 已提交
380 381
    }

V
varkor 已提交
382
    pub fn pat(&self, span: Span, kind: PatKind) -> P<ast::Pat> {
383
        P(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None })
384
    }
385
    pub fn pat_wild(&self, span: Span) -> P<ast::Pat> {
386
        self.pat(span, PatKind::Wild)
387
    }
388
    pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
389
        self.pat(span, PatKind::Lit(expr))
390
    }
391
    pub fn pat_ident(&self, span: Span, ident: Ident) -> P<ast::Pat> {
392
        let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Not);
393
        self.pat_ident_binding_mode(span, ident, binding_mode)
394 395
    }

M
Mark Rousskov 已提交
396 397 398
    pub fn pat_ident_binding_mode(
        &self,
        span: Span,
399
        ident: Ident,
M
Mark Rousskov 已提交
400 401
        bm: ast::BindingMode,
    ) -> P<ast::Pat> {
V
Vadim Petrochenkov 已提交
402
        let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
403 404
        self.pat(span, pat)
    }
405
    pub fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
406
        self.pat(span, PatKind::Path(None, path))
407
    }
M
Mark Rousskov 已提交
408 409 410 411 412 413
    pub fn pat_tuple_struct(
        &self,
        span: Span,
        path: ast::Path,
        subpats: Vec<P<ast::Pat>>,
    ) -> P<ast::Pat> {
414
        self.pat(span, PatKind::TupleStruct(None, path, subpats))
415
    }
M
Mark Rousskov 已提交
416 417 418 419
    pub fn pat_struct(
        &self,
        span: Span,
        path: ast::Path,
420
        field_pats: Vec<ast::PatField>,
M
Mark Rousskov 已提交
421
    ) -> P<ast::Pat> {
422
        self.pat(span, PatKind::Struct(None, path, field_pats, false))
423
    }
424
    pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
425
        self.pat(span, PatKind::Tuple(pats))
426 427
    }

428
    pub fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
429
        let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
430
        let path = self.path_global(span, some);
431
        self.pat_tuple_struct(span, path, vec![pat])
432 433
    }

434
    pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm {
435
        ast::Arm {
436
            attrs: AttrVec::new(),
437
            pat,
438
            guard: None,
M
Matt Ickstadt 已提交
439
            body: expr,
440
            span,
C
Caio 已提交
441
            id: ast::DUMMY_NODE_ID,
C
Caio 已提交
442
            is_placeholder: false,
443 444 445
        }
    }

446
    pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
447
        self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
448 449
    }

450
    pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
451
        self.expr(span, ast::ExprKind::Match(arg, arms))
452
    }
453

M
Mark Rousskov 已提交
454 455 456 457 458 459 460
    pub fn expr_if(
        &self,
        span: Span,
        cond: P<ast::Expr>,
        then: P<ast::Expr>,
        els: Option<P<ast::Expr>>,
    ) -> P<ast::Expr> {
461
        let els = els.map(|x| self.expr_block(self.block_expr(x)));
462
        self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
463 464
    }

465
    pub fn lambda(&self, span: Span, ids: Vec<Ident>, body: P<ast::Expr>) -> P<ast::Expr> {
466
        let fn_decl = self.fn_decl(
M
Mark Rousskov 已提交
467
            ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
468
            ast::FnRetTy::Default(span),
M
Mark Rousskov 已提交
469
        );
470

471 472 473 474
        // FIXME -- We are using `span` as the span of the `|...|`
        // part of the lambda, but it probably (maybe?) corresponds to
        // the entire lambda body. Probably we should extend the API
        // here, but that's not entirely clear.
M
Mark Rousskov 已提交
475 476 477 478
        self.expr(
            span,
            ast::ExprKind::Closure(
                ast::CaptureBy::Ref,
479
                ast::Async::No,
M
Mark Rousskov 已提交
480 481 482 483 484 485
                ast::Movability::Movable,
                fn_decl,
                body,
                span,
            ),
        )
S
Steven Fackler 已提交
486
    }
487

488
    pub fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
489
        self.lambda(span, Vec::new(), body)
S
Steven Fackler 已提交
490
    }
491

492
    pub fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: Ident) -> P<ast::Expr> {
493
        self.lambda(span, vec![ident], body)
494 495
    }

496
    pub fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>, ident: Ident) -> P<ast::Expr> {
497
        self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
498 499
    }

500
    pub fn param(&self, span: Span, ident: Ident, ty: P<ast::Ty>) -> ast::Param {
501
        let arg_pat = self.pat_ident(span, ident);
502
        ast::Param {
M
Mazdak Farrokhzad 已提交
503
            attrs: AttrVec::default(),
504
            id: ast::DUMMY_NODE_ID,
505
            pat: arg_pat,
C
Caio 已提交
506
            span,
507
            ty,
C
Caio 已提交
508
            is_placeholder: false,
509
        }
510
    }
511

512
    // FIXME: unused `self`
513
    pub fn fn_decl(&self, inputs: Vec<ast::Param>, output: ast::FnRetTy) -> P<ast::FnDecl> {
M
Mark Rousskov 已提交
514
        P(ast::FnDecl { inputs, output })
515 516
    }

M
Mark Rousskov 已提交
517 518 519 520 521 522 523
    pub fn item(
        &self,
        span: Span,
        name: Ident,
        attrs: Vec<ast::Attribute>,
        kind: ast::ItemKind,
    ) -> P<ast::Item> {
524
        // FIXME: Would be nice if our generated code didn't violate
525
        // Rust coding conventions
526 527
        P(ast::Item {
            ident: name,
528
            attrs,
529
            id: ast::DUMMY_NODE_ID,
V
varkor 已提交
530
            kind,
531 532 533 534 535
            vis: ast::Visibility {
                span: span.shrink_to_lo(),
                kind: ast::VisibilityKind::Inherited,
                tokens: None,
            },
536
            span,
537
            tokens: None,
538
        })
539 540
    }

M
Mark Rousskov 已提交
541 542 543 544 545 546 547 548
    pub fn item_static(
        &self,
        span: Span,
        name: Ident,
        ty: P<ast::Ty>,
        mutbl: ast::Mutability,
        expr: P<ast::Expr>,
    ) -> P<ast::Item> {
549
        self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr)))
550 551
    }

M
Mark Rousskov 已提交
552 553 554 555 556 557 558
    pub fn item_const(
        &self,
        span: Span,
        name: Ident,
        ty: P<ast::Ty>,
        expr: P<ast::Expr>,
    ) -> P<ast::Item> {
559 560
        let def = ast::Defaultness::Final;
        self.item(span, name, Vec::new(), ast::ItemKind::Const(def, ty, Some(expr)))
561 562
    }

563
    pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute {
564
        attr::mk_attr_outer(mi)
565 566
    }

567
    pub fn meta_word(&self, sp: Span, w: Symbol) -> ast::MetaItem {
568
        attr::mk_word_item(Ident::new(w, sp))
569
    }
570
}