1 use crate::ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
3 use crate::source_map::{dummy_spanned, respan, Spanned};
4 use crate::ext::base::ExtCtxt;
6 use crate::symbol::{kw, sym, Symbol};
9 use rustc_target::spec::abi::Abi;
10 use syntax_pos::{Pos, Span};
12 pub trait AstBuilder {
14 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
15 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
16 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
17 fn path_all(&self, sp: Span,
19 idents: Vec<ast::Ident>,
20 args: Vec<ast::GenericArg>,
21 constraints: Vec<ast::AssocTyConstraint>)
24 fn qpath(&self, self_type: P<ast::Ty>,
25 trait_path: ast::Path,
27 -> (ast::QSelf, ast::Path);
28 fn qpath_all(&self, self_type: P<ast::Ty>,
29 trait_path: ast::Path,
31 args: Vec<ast::GenericArg>,
32 constraints: Vec<ast::AssocTyConstraint>)
33 -> (ast::QSelf, ast::Path);
36 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
38 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
39 fn ty_path(&self, path: ast::Path) -> P<ast::Ty>;
40 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
41 fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst;
42 fn const_ident(&self, span: Span, idents: ast::Ident) -> ast::AnonConst;
44 fn ty_rptr(&self, span: Span,
46 lifetime: Option<ast::Lifetime>,
47 mutbl: ast::Mutability) -> P<ast::Ty>;
48 fn ty_ptr(&self, span: Span,
50 mutbl: ast::Mutability) -> P<ast::Ty>;
52 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
57 attrs: Vec<ast::Attribute>,
58 bounds: ast::GenericBounds,
59 default: Option<P<ast::Ty>>) -> ast::GenericParam;
61 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
62 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
63 fn trait_bound(&self, path: ast::Path) -> ast::GenericBound;
64 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime;
65 fn lifetime_def(&self,
68 attrs: Vec<ast::Attribute>,
69 bounds: ast::GenericBounds)
73 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
74 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
75 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
76 fn stmt_let_typed(&self,
83 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
84 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
87 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
88 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
91 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
92 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
93 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
94 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
96 fn expr_self(&self, span: Span) -> P<ast::Expr>;
97 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
98 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
99 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
100 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
102 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
103 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
104 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
105 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
106 idx: usize) -> P<ast::Expr>;
107 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
108 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
109 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
110 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
111 fn expr_method_call(&self, span: Span,
112 expr: P<ast::Expr>, ident: ast::Ident,
113 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
114 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
115 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
117 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
118 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
119 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
120 fields: Vec<ast::Field>) -> P<ast::Expr>;
122 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
124 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
125 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
126 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
127 fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr>;
128 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
129 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
131 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
132 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
133 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
134 fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr>;
136 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
137 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
139 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
141 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
143 fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr>;
144 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
146 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
147 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
148 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
150 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
151 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
152 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
153 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
155 fn pat_ident_binding_mode(&self,
158 bm: ast::BindingMode) -> P<ast::Pat>;
159 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat>;
160 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
161 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
162 fn pat_struct(&self, span: Span, path: ast::Path,
163 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat>;
164 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
166 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
167 fn pat_none(&self, span: Span) -> P<ast::Pat>;
169 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
170 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
172 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
173 fn arm_unreachable(&self, span: Span) -> ast::Arm;
175 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
176 fn expr_if(&self, span: Span,
177 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
178 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
180 fn lambda_fn_decl(&self,
182 fn_decl: P<ast::FnDecl>,
187 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr>;
188 fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr>;
189 fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
191 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
192 blk: Vec<ast::Stmt>) -> P<ast::Expr>;
193 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
194 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
195 ident: ast::Ident) -> P<ast::Expr>;
198 fn item(&self, span: Span,
199 name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
201 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
202 // FIXME: unused `self`
203 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: ast::FunctionRetTy) -> P<ast::FnDecl>;
205 fn item_fn_poly(&self,
208 inputs: Vec<ast::Arg> ,
211 body: P<ast::Block>) -> P<ast::Item>;
215 inputs: Vec<ast::Arg> ,
217 body: P<ast::Block>) -> P<ast::Item>;
219 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
220 fn item_enum_poly(&self,
223 enum_definition: ast::EnumDef,
224 generics: Generics) -> P<ast::Item>;
225 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
227 fn item_struct_poly(&self,
230 struct_def: ast::VariantData,
231 generics: Generics) -> P<ast::Item>;
232 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
234 fn item_mod(&self, span: Span, inner_span: Span,
235 name: Ident, attrs: Vec<ast::Attribute>,
236 items: Vec<P<ast::Item>>) -> P<ast::Item>;
238 fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item>;
240 fn item_static(&self,
244 mutbl: ast::Mutability,
255 fn item_ty_poly(&self,
259 generics: Generics) -> P<ast::Item>;
260 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
262 fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute;
264 fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem;
266 fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem;
271 mis: Vec<ast::NestedMetaItem> )
273 fn meta_name_value(&self,
279 fn item_use(&self, sp: Span,
280 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item>;
281 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
282 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
283 ident: Option<ast::Ident>, path: ast::Path) -> P<ast::Item>;
284 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
285 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
286 fn item_use_glob(&self, sp: Span,
287 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
290 impl<'a> AstBuilder for ExtCtxt<'a> {
291 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
292 self.path_all(span, false, strs, vec![], vec![])
294 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
295 self.path(span, vec![id])
297 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
298 self.path_all(span, true, strs, vec![], vec![])
303 mut idents: Vec<ast::Ident> ,
304 args: Vec<ast::GenericArg>,
305 constraints: Vec<ast::AssocTyConstraint> )
307 assert!(!idents.is_empty());
308 let add_root = global && !idents[0].is_path_segment_keyword();
309 let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
311 segments.push(ast::PathSegment::path_root(span));
313 let last_ident = idents.pop().unwrap();
314 segments.extend(idents.into_iter().map(|ident| {
315 ast::PathSegment::from_ident(ident.with_span_pos(span))
317 let args = if !args.is_empty() || !constraints.is_empty() {
318 ast::AngleBracketedArgs { args, constraints, span }.into()
322 segments.push(ast::PathSegment {
323 ident: last_ident.with_span_pos(span),
324 id: ast::DUMMY_NODE_ID,
327 ast::Path { span, segments }
330 /// Constructs a qualified path.
332 /// Constructs a path like `<self_type as trait_path>::ident`.
334 self_type: P<ast::Ty>,
335 trait_path: ast::Path,
337 -> (ast::QSelf, ast::Path) {
338 self.qpath_all(self_type, trait_path, ident, vec![], vec![])
341 /// Constructs a qualified path.
343 /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A = Bar>`.
345 self_type: P<ast::Ty>,
346 trait_path: ast::Path,
348 args: Vec<ast::GenericArg>,
349 constraints: Vec<ast::AssocTyConstraint>)
350 -> (ast::QSelf, ast::Path) {
351 let mut path = trait_path;
352 let args = if !args.is_empty() || !constraints.is_empty() {
353 ast::AngleBracketedArgs { args, constraints, span: ident.span }.into()
357 path.segments.push(ast::PathSegment { ident, id: ast::DUMMY_NODE_ID, args });
361 path_span: path.span,
362 position: path.segments.len() - 1
366 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
373 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
375 id: ast::DUMMY_NODE_ID,
381 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
382 self.ty(path.span, ast::TyKind::Path(None, path))
385 // Might need to take bounds as an argument in the future, if you ever want
386 // to generate a bounded existential trait type.
387 fn ty_ident(&self, span: Span, ident: ast::Ident)
389 self.ty_path(self.path_ident(span, ident))
392 fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst {
394 id: ast::DUMMY_NODE_ID,
396 id: ast::DUMMY_NODE_ID,
399 attrs: ThinVec::new(),
404 fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
405 self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
411 lifetime: Option<ast::Lifetime>,
412 mutbl: ast::Mutability)
415 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
421 mutbl: ast::Mutability)
424 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
427 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
428 self.ty(span, ast::TyKind::Infer)
434 attrs: Vec<ast::Attribute>,
435 bounds: ast::GenericBounds,
436 default: Option<P<ast::Ty>>) -> ast::GenericParam {
438 ident: ident.with_span_pos(span),
439 id: ast::DUMMY_NODE_ID,
442 kind: ast::GenericParamKind::Type {
448 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
451 ref_id: ast::DUMMY_NODE_ID,
455 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
457 bound_generic_params: Vec::new(),
458 trait_ref: self.trait_ref(path),
463 fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
464 ast::GenericBound::Trait(self.poly_trait_ref(path.span, path),
465 ast::TraitBoundModifier::None)
468 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
469 ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
472 fn lifetime_def(&self,
475 attrs: Vec<ast::Attribute>,
476 bounds: ast::GenericBounds)
477 -> ast::GenericParam {
478 let lifetime = self.lifetime(span, ident);
480 ident: lifetime.ident,
484 kind: ast::GenericParamKind::Lifetime,
488 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
490 id: ast::DUMMY_NODE_ID,
492 node: ast::StmtKind::Expr(expr),
496 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
498 id: ast::DUMMY_NODE_ID,
500 node: ast::StmtKind::Semi(expr),
504 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
505 ex: P<ast::Expr>) -> ast::Stmt {
507 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
508 self.pat_ident_binding_mode(sp, ident, binding_mode)
510 self.pat_ident(sp, ident)
512 let local = P(ast::Local {
516 id: ast::DUMMY_NODE_ID,
518 attrs: ThinVec::new(),
521 id: ast::DUMMY_NODE_ID,
522 node: ast::StmtKind::Local(local),
527 fn stmt_let_typed(&self,
535 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
536 self.pat_ident_binding_mode(sp, ident, binding_mode)
538 self.pat_ident(sp, ident)
540 let local = P(ast::Local {
544 id: ast::DUMMY_NODE_ID,
546 attrs: ThinVec::new(),
549 id: ast::DUMMY_NODE_ID,
550 node: ast::StmtKind::Local(local),
555 // Generates `let _: Type;`, which is usually used for type assertions.
556 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
557 let local = P(ast::Local {
558 pat: self.pat_wild(span),
561 id: ast::DUMMY_NODE_ID,
563 attrs: ThinVec::new(),
566 id: ast::DUMMY_NODE_ID,
567 node: ast::StmtKind::Local(local),
572 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
574 id: ast::DUMMY_NODE_ID,
575 node: ast::StmtKind::Item(item),
580 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
581 self.block(expr.span, vec![ast::Stmt {
582 id: ast::DUMMY_NODE_ID,
584 node: ast::StmtKind::Expr(expr),
587 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
590 id: ast::DUMMY_NODE_ID,
591 rules: BlockCheckMode::Default,
596 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
598 id: ast::DUMMY_NODE_ID,
601 attrs: ThinVec::new(),
605 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
606 self.expr(path.span, ast::ExprKind::Path(None, path))
609 /// Constructs a `QPath` expression.
610 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
611 self.expr(span, ast::ExprKind::Path(Some(qself), path))
614 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
615 self.expr_path(self.path_ident(span, id))
617 fn expr_self(&self, span: Span) -> P<ast::Expr> {
618 self.expr_ident(span, Ident::with_empty_ctxt(kw::SelfLower))
621 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
622 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
623 self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
626 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
627 self.expr_unary(sp, UnOp::Deref, e)
629 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
630 self.expr(sp, ast::ExprKind::Unary(op, e))
633 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
634 self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
636 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
637 let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
638 self.expr(sp, ast::ExprKind::Field(expr, ident))
640 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
641 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
643 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
644 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
647 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
648 self.expr(span, ast::ExprKind::Call(expr, args))
650 fn expr_call_ident(&self, span: Span, id: ast::Ident,
651 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
652 self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
654 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
655 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
656 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
657 self.expr_call(sp, pathexpr, args)
659 fn expr_method_call(&self, span: Span,
662 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
663 args.insert(0, expr);
664 let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
665 self.expr(span, ast::ExprKind::MethodCall(segment, args))
667 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
668 self.expr(b.span, ast::ExprKind::Block(b, None))
670 fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
672 ident: ident.with_span_pos(span),
676 attrs: ThinVec::new(),
679 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
680 self.expr(span, ast::ExprKind::Struct(path, fields, None))
682 fn expr_struct_ident(&self, span: Span,
683 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
684 self.expr_struct(span, self.path_ident(span, id), fields)
687 fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
688 let lit = ast::Lit::from_lit_kind(lit_kind, span);
689 self.expr(span, ast::ExprKind::Lit(lit))
691 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
692 self.expr_lit(span, ast::LitKind::Int(i as u128,
693 ast::LitIntType::Unsigned(ast::UintTy::Usize)))
695 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
697 let i = (-i) as u128;
698 let lit_ty = ast::LitIntType::Signed(ast::IntTy::Isize);
699 let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
700 self.expr_unary(sp, ast::UnOp::Neg, lit)
702 self.expr_lit(sp, ast::LitKind::Int(i as u128,
703 ast::LitIntType::Signed(ast::IntTy::Isize)))
706 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
707 self.expr_lit(sp, ast::LitKind::Int(u as u128,
708 ast::LitIntType::Unsigned(ast::UintTy::U32)))
710 fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
711 self.expr_lit(sp, ast::LitKind::Int(u as u128,
712 ast::LitIntType::Unsigned(ast::UintTy::U16)))
714 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
715 self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
717 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
718 self.expr_lit(sp, ast::LitKind::Bool(value))
721 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
722 self.expr(sp, ast::ExprKind::Array(exprs))
724 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
725 self.expr_call_global(sp, self.std_path(&[sym::vec, sym::Vec, sym::new]),
728 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
729 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
731 fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
732 self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
735 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
736 self.expr(sp, ast::ExprKind::Cast(expr, ty))
739 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
740 let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
741 self.expr_call_global(sp, some, vec![expr])
744 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
745 let none = self.std_path(&[sym::option, sym::Option, sym::None]);
746 let none = self.path_global(sp, none);
750 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
751 self.expr(sp, ast::ExprKind::Break(None, None))
754 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
755 self.expr(sp, ast::ExprKind::Tup(exprs))
758 fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
759 let loc = self.source_map().lookup_char_pos(span.lo());
760 let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
761 let expr_line = self.expr_u32(span, loc.line as u32);
762 let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
763 let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
764 let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
765 self.expr_call_global(
767 self.std_path(&[sym::rt, sym::begin_panic]),
769 self.expr_str(span, msg),
773 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
774 self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
777 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
778 let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
779 self.expr_call_global(sp, ok, vec![expr])
782 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
783 let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
784 self.expr_call_global(sp, err, vec![expr])
787 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
788 let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
789 let ok_path = self.path_global(sp, ok);
790 let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
791 let err_path = self.path_global(sp, err);
793 let binding_variable = self.ident_of("__try_var");
794 let binding_pat = self.pat_ident(sp, binding_variable);
795 let binding_expr = self.expr_ident(sp, binding_variable);
797 // `Ok(__try_var)` pattern
798 let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
800 // `Err(__try_var)` (pattern and expression respectively)
801 let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
802 let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
803 vec![binding_expr.clone()]);
804 // `return Err(__try_var)`
805 let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
807 // `Ok(__try_var) => __try_var`
808 let ok_arm = self.arm(sp, vec![ok_pat], binding_expr);
809 // `Err(__try_var) => return Err(__try_var)`
810 let err_arm = self.arm(sp, vec![err_pat], err_expr);
812 // `match head { Ok() => ..., Err() => ... }`
813 self.expr_match(sp, head, vec![ok_arm, err_arm])
817 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
818 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span })
820 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
821 self.pat(span, PatKind::Wild)
823 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
824 self.pat(span, PatKind::Lit(expr))
826 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
827 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
828 self.pat_ident_binding_mode(span, ident, binding_mode)
831 fn pat_ident_binding_mode(&self,
834 bm: ast::BindingMode) -> P<ast::Pat> {
835 let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
838 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
839 self.pat(span, PatKind::Path(None, path))
841 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
842 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
843 self.pat(span, PatKind::TupleStruct(path, subpats))
845 fn pat_struct(&self, span: Span, path: ast::Path,
846 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
847 self.pat(span, PatKind::Struct(path, field_pats, false))
849 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
850 self.pat(span, PatKind::Tuple(pats))
853 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
854 let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
855 let path = self.path_global(span, some);
856 self.pat_tuple_struct(span, path, vec![pat])
859 fn pat_none(&self, span: Span) -> P<ast::Pat> {
860 let some = self.std_path(&[sym::option, sym::Option, sym::None]);
861 let path = self.path_global(span, some);
862 self.pat_path(span, path)
865 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
866 let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
867 let path = self.path_global(span, some);
868 self.pat_tuple_struct(span, path, vec![pat])
871 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
872 let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
873 let path = self.path_global(span, some);
874 self.pat_tuple_struct(span, path, vec![pat])
877 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
887 fn arm_unreachable(&self, span: Span) -> ast::Arm {
888 self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span))
891 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
892 self.expr(span, ast::ExprKind::Match(arg, arms))
895 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
896 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
897 let els = els.map(|x| self.expr_block(self.block_expr(x)));
898 self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
901 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
902 self.expr(span, ast::ExprKind::Loop(block, None))
905 fn lambda_fn_decl(&self,
907 fn_decl: P<ast::FnDecl>,
909 fn_decl_span: Span) // span of the `|...|` part
911 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
912 ast::IsAsync::NotAsync,
913 ast::Movability::Movable,
921 ids: Vec<ast::Ident>,
924 let fn_decl = self.fn_decl(
925 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
926 ast::FunctionRetTy::Default(span));
928 // FIXME -- We are using `span` as the span of the `|...|`
929 // part of the lambda, but it probably (maybe?) corresponds to
930 // the entire lambda body. Probably we should extend the API
931 // here, but that's not entirely clear.
932 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
933 ast::IsAsync::NotAsync,
934 ast::Movability::Movable,
940 fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
941 self.lambda(span, Vec::new(), body)
944 fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
945 self.lambda(span, vec![ident], body)
948 fn lambda_stmts(&self,
950 ids: Vec<ast::Ident>,
951 stmts: Vec<ast::Stmt>)
953 self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
955 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
956 self.lambda0(span, self.expr_block(self.block(span, stmts)))
958 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
959 ident: ast::Ident) -> P<ast::Expr> {
960 self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
963 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
964 let arg_pat = self.pat_ident(span, ident);
966 attrs: ThinVec::default(),
967 id: ast::DUMMY_NODE_ID,
974 // FIXME: unused `self`
975 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
983 fn item(&self, span: Span, name: Ident,
984 attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
985 // FIXME: Would be nice if our generated code didn't violate
986 // Rust coding conventions
990 id: ast::DUMMY_NODE_ID,
992 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
998 fn item_fn_poly(&self,
1001 inputs: Vec<ast::Arg> ,
1004 body: P<ast::Block>) -> P<ast::Item> {
1008 ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
1010 unsafety: ast::Unsafety::Normal,
1011 asyncness: dummy_spanned(ast::IsAsync::NotAsync),
1012 constness: dummy_spanned(ast::Constness::NotConst),
1022 inputs: Vec<ast::Arg> ,
1031 Generics::default(),
1035 fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1036 let fields: Vec<_> = tys.into_iter().map(|ty| {
1041 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1043 id: ast::DUMMY_NODE_ID,
1047 let vdata = if fields.is_empty() {
1048 ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1050 ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1056 id: ast::DUMMY_NODE_ID,
1063 fn item_enum_poly(&self, span: Span, name: Ident,
1064 enum_definition: ast::EnumDef,
1065 generics: Generics) -> P<ast::Item> {
1066 self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1069 fn item_enum(&self, span: Span, name: Ident,
1070 enum_definition: ast::EnumDef) -> P<ast::Item> {
1071 self.item_enum_poly(span, name, enum_definition,
1072 Generics::default())
1075 fn item_struct(&self, span: Span, name: Ident,
1076 struct_def: ast::VariantData) -> P<ast::Item> {
1077 self.item_struct_poly(
1085 fn item_struct_poly(&self, span: Span, name: Ident,
1086 struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1087 self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1090 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1091 attrs: Vec<ast::Attribute>,
1092 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1097 ast::ItemKind::Mod(ast::Mod {
1105 fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item> {
1106 self.item(span, name, Vec::new(), ast::ItemKind::ExternCrate(None))
1109 fn item_static(&self,
1113 mutbl: ast::Mutability,
1116 self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1119 fn item_const(&self,
1125 self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1128 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1129 generics: Generics) -> P<ast::Item> {
1130 self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1133 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1134 self.item_ty_poly(span, name, ty, Generics::default())
1137 fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute {
1138 attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
1141 fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
1142 attr::mk_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1145 fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
1146 attr::mk_nested_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1149 fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
1151 attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
1154 fn meta_name_value(&self, span: Span, name: ast::Name, lit_kind: ast::LitKind)
1156 attr::mk_name_value_item(span, Ident::with_empty_ctxt(name).with_span_pos(span),
1160 fn item_use(&self, sp: Span,
1161 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item> {
1163 id: ast::DUMMY_NODE_ID,
1164 ident: Ident::invalid(),
1166 node: ast::ItemKind::Use(vp),
1173 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1174 self.item_use_simple_(sp, vis, None, path)
1177 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1178 rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
1179 self.item_use(sp, vis, P(ast::UseTree {
1182 kind: ast::UseTreeKind::Simple(rename, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1186 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1187 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1188 let imports = imports.iter().map(|id| {
1191 prefix: self.path(sp, vec![*id]),
1192 kind: ast::UseTreeKind::Simple(None, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1193 }, ast::DUMMY_NODE_ID)
1196 self.item_use(sp, vis, P(ast::UseTree {
1198 prefix: self.path(sp, path),
1199 kind: ast::UseTreeKind::Nested(imports),
1203 fn item_use_glob(&self, sp: Span,
1204 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1205 self.item_use(sp, vis, P(ast::UseTree {
1207 prefix: self.path(sp, path),
1208 kind: ast::UseTreeKind::Glob,