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::{Symbol, keywords};
9 use rustc_target::spec::abi::Abi;
10 use syntax_pos::{Pos, Span, DUMMY_SP};
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 bindings: Vec<ast::TypeBinding>)
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 bindings: Vec<ast::TypeBinding>)
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_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
53 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
58 attrs: Vec<ast::Attribute>,
59 bounds: ast::GenericBounds,
60 default: Option<P<ast::Ty>>) -> ast::GenericParam;
62 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
63 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
64 fn trait_bound(&self, path: ast::Path) -> ast::GenericBound;
65 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime;
66 fn lifetime_def(&self,
69 attrs: Vec<ast::Attribute>,
70 bounds: ast::GenericBounds)
74 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
75 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
76 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
77 fn stmt_let_typed(&self,
84 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
85 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
88 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
89 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
92 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
93 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
94 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
95 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
97 fn expr_self(&self, span: Span) -> P<ast::Expr>;
98 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
99 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
100 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
101 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
103 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
104 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
105 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
106 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
107 idx: usize) -> P<ast::Expr>;
108 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
109 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
110 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
111 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
112 fn expr_method_call(&self, span: Span,
113 expr: P<ast::Expr>, ident: ast::Ident,
114 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
115 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
116 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
118 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
119 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
120 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
121 fields: Vec<ast::Field>) -> P<ast::Expr>;
123 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
125 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
126 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
127 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
128 fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr>;
129 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
130 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
132 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
133 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
134 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
135 fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr>;
137 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
138 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
140 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
142 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
144 fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr>;
145 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
147 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
148 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
149 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
151 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
152 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
153 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
154 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
156 fn pat_ident_binding_mode(&self,
159 bm: ast::BindingMode) -> P<ast::Pat>;
160 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat>;
161 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
162 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
163 fn pat_struct(&self, span: Span, path: ast::Path,
164 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat>;
165 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
167 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
168 fn pat_none(&self, span: Span) -> P<ast::Pat>;
170 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
171 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
173 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
174 fn arm_unreachable(&self, span: Span) -> ast::Arm;
176 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
177 fn expr_if(&self, span: Span,
178 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
179 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
181 fn lambda_fn_decl(&self,
183 fn_decl: P<ast::FnDecl>,
188 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr>;
189 fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr>;
190 fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
192 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
193 blk: Vec<ast::Stmt>) -> P<ast::Expr>;
194 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
195 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
196 ident: ast::Ident) -> P<ast::Expr>;
199 fn item(&self, span: Span,
200 name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
202 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
204 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: ast::FunctionRetTy) -> P<ast::FnDecl>;
206 fn item_fn_poly(&self,
209 inputs: Vec<ast::Arg> ,
212 body: P<ast::Block>) -> P<ast::Item>;
216 inputs: Vec<ast::Arg> ,
218 body: P<ast::Block>) -> P<ast::Item>;
220 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
221 fn item_enum_poly(&self,
224 enum_definition: ast::EnumDef,
225 generics: Generics) -> P<ast::Item>;
226 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
228 fn item_struct_poly(&self,
231 struct_def: ast::VariantData,
232 generics: Generics) -> P<ast::Item>;
233 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
235 fn item_mod(&self, span: Span, inner_span: Span,
236 name: Ident, attrs: Vec<ast::Attribute>,
237 items: Vec<P<ast::Item>>) -> P<ast::Item>;
239 fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item>;
241 fn item_static(&self,
245 mutbl: ast::Mutability,
256 fn item_ty_poly(&self,
260 generics: Generics) -> P<ast::Item>;
261 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
263 fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute;
265 fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem;
267 fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem;
272 mis: Vec<ast::NestedMetaItem> )
274 fn meta_name_value(&self,
280 fn item_use(&self, sp: Span,
281 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item>;
282 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
283 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
284 ident: Option<ast::Ident>, path: ast::Path) -> P<ast::Item>;
285 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
286 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
287 fn item_use_glob(&self, sp: Span,
288 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
291 impl<'a> AstBuilder for ExtCtxt<'a> {
292 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
293 self.path_all(span, false, strs, vec![], vec![])
295 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
296 self.path(span, vec![id])
298 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
299 self.path_all(span, true, strs, vec![], vec![])
304 mut idents: Vec<ast::Ident> ,
305 args: Vec<ast::GenericArg>,
306 bindings: Vec<ast::TypeBinding> )
308 assert!(!idents.is_empty());
309 let add_root = global && !idents[0].is_path_segment_keyword();
310 let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
312 segments.push(ast::PathSegment::path_root(span));
314 let last_ident = idents.pop().unwrap();
315 segments.extend(idents.into_iter().map(|ident| {
316 ast::PathSegment::from_ident(ident.with_span_pos(span))
318 let args = if !args.is_empty() || !bindings.is_empty() {
319 ast::AngleBracketedArgs { args, bindings, span }.into()
323 segments.push(ast::PathSegment {
324 ident: last_ident.with_span_pos(span),
325 id: ast::DUMMY_NODE_ID,
328 ast::Path { span, segments }
331 /// Constructs a qualified path.
333 /// Constructs a path like `<self_type as trait_path>::ident`.
335 self_type: P<ast::Ty>,
336 trait_path: ast::Path,
338 -> (ast::QSelf, ast::Path) {
339 self.qpath_all(self_type, trait_path, ident, vec![], vec![])
342 /// Constructs a qualified path.
344 /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A = Bar>`.
346 self_type: P<ast::Ty>,
347 trait_path: ast::Path,
349 args: Vec<ast::GenericArg>,
350 bindings: Vec<ast::TypeBinding>)
351 -> (ast::QSelf, ast::Path) {
352 let mut path = trait_path;
353 let args = if !args.is_empty() || !bindings.is_empty() {
354 ast::AngleBracketedArgs { args, bindings, span: ident.span }.into()
358 path.segments.push(ast::PathSegment { ident, id: ast::DUMMY_NODE_ID, args });
362 path_span: path.span,
363 position: path.segments.len() - 1
367 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
374 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
376 id: ast::DUMMY_NODE_ID,
382 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
383 self.ty(path.span, ast::TyKind::Path(None, path))
386 // Might need to take bounds as an argument in the future, if you ever want
387 // to generate a bounded existential trait type.
388 fn ty_ident(&self, span: Span, ident: ast::Ident)
390 self.ty_path(self.path_ident(span, ident))
393 fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst {
395 id: ast::DUMMY_NODE_ID,
397 id: ast::DUMMY_NODE_ID,
400 attrs: ThinVec::new(),
405 fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
406 self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
412 lifetime: Option<ast::Lifetime>,
413 mutbl: ast::Mutability)
416 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
422 mutbl: ast::Mutability)
425 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
428 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
430 self.path_all(DUMMY_SP,
432 self.std_path(&["option", "Option"]),
433 vec![ast::GenericArg::Type(ty)],
437 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
438 self.ty(span, ast::TyKind::Infer)
444 attrs: Vec<ast::Attribute>,
445 bounds: ast::GenericBounds,
446 default: Option<P<ast::Ty>>) -> ast::GenericParam {
448 ident: ident.with_span_pos(span),
449 id: ast::DUMMY_NODE_ID,
452 kind: ast::GenericParamKind::Type {
458 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
461 ref_id: ast::DUMMY_NODE_ID,
465 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
467 bound_generic_params: Vec::new(),
468 trait_ref: self.trait_ref(path),
473 fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
474 ast::GenericBound::Trait(self.poly_trait_ref(path.span, path),
475 ast::TraitBoundModifier::None)
478 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
479 ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
482 fn lifetime_def(&self,
485 attrs: Vec<ast::Attribute>,
486 bounds: ast::GenericBounds)
487 -> ast::GenericParam {
488 let lifetime = self.lifetime(span, ident);
490 ident: lifetime.ident,
494 kind: ast::GenericParamKind::Lifetime,
498 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
500 id: ast::DUMMY_NODE_ID,
502 node: ast::StmtKind::Expr(expr),
506 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
508 id: ast::DUMMY_NODE_ID,
510 node: ast::StmtKind::Semi(expr),
514 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
515 ex: P<ast::Expr>) -> ast::Stmt {
517 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
518 self.pat_ident_binding_mode(sp, ident, binding_mode)
520 self.pat_ident(sp, ident)
522 let local = P(ast::Local {
526 id: ast::DUMMY_NODE_ID,
528 attrs: ThinVec::new(),
531 id: ast::DUMMY_NODE_ID,
532 node: ast::StmtKind::Local(local),
537 fn stmt_let_typed(&self,
545 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
546 self.pat_ident_binding_mode(sp, ident, binding_mode)
548 self.pat_ident(sp, ident)
550 let local = P(ast::Local {
554 id: ast::DUMMY_NODE_ID,
556 attrs: ThinVec::new(),
559 id: ast::DUMMY_NODE_ID,
560 node: ast::StmtKind::Local(local),
565 // Generate `let _: Type;`, usually used for type assertions.
566 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
567 let local = P(ast::Local {
568 pat: self.pat_wild(span),
571 id: ast::DUMMY_NODE_ID,
573 attrs: ThinVec::new(),
576 id: ast::DUMMY_NODE_ID,
577 node: ast::StmtKind::Local(local),
582 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
584 id: ast::DUMMY_NODE_ID,
585 node: ast::StmtKind::Item(item),
590 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
591 self.block(expr.span, vec![ast::Stmt {
592 id: ast::DUMMY_NODE_ID,
594 node: ast::StmtKind::Expr(expr),
597 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
600 id: ast::DUMMY_NODE_ID,
601 rules: BlockCheckMode::Default,
606 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
608 id: ast::DUMMY_NODE_ID,
611 attrs: ThinVec::new(),
615 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
616 self.expr(path.span, ast::ExprKind::Path(None, path))
619 /// Constructs a QPath expression.
620 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
621 self.expr(span, ast::ExprKind::Path(Some(qself), path))
624 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
625 self.expr_path(self.path_ident(span, id))
627 fn expr_self(&self, span: Span) -> P<ast::Expr> {
628 self.expr_ident(span, keywords::SelfLower.ident())
631 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
632 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
633 self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
636 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
637 self.expr_unary(sp, UnOp::Deref, e)
639 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
640 self.expr(sp, ast::ExprKind::Unary(op, e))
643 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
644 self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
646 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
647 let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
648 self.expr(sp, ast::ExprKind::Field(expr, ident))
650 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
651 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
653 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
654 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
657 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
658 self.expr(span, ast::ExprKind::Call(expr, args))
660 fn expr_call_ident(&self, span: Span, id: ast::Ident,
661 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
662 self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
664 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
665 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
666 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
667 self.expr_call(sp, pathexpr, args)
669 fn expr_method_call(&self, span: Span,
672 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
673 args.insert(0, expr);
674 let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
675 self.expr(span, ast::ExprKind::MethodCall(segment, args))
677 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
678 self.expr(b.span, ast::ExprKind::Block(b, None))
680 fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
682 ident: ident.with_span_pos(span),
686 attrs: ThinVec::new(),
689 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
690 self.expr(span, ast::ExprKind::Struct(path, fields, None))
692 fn expr_struct_ident(&self, span: Span,
693 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
694 self.expr_struct(span, self.path_ident(span, id), fields)
697 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
698 self.expr(sp, ast::ExprKind::Lit(respan(sp, lit)))
700 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
701 self.expr_lit(span, ast::LitKind::Int(i as u128,
702 ast::LitIntType::Unsigned(ast::UintTy::Usize)))
704 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
706 let i = (-i) as u128;
707 let lit_ty = ast::LitIntType::Signed(ast::IntTy::Isize);
708 let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
709 self.expr_unary(sp, ast::UnOp::Neg, lit)
711 self.expr_lit(sp, ast::LitKind::Int(i as u128,
712 ast::LitIntType::Signed(ast::IntTy::Isize)))
715 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
716 self.expr_lit(sp, ast::LitKind::Int(u as u128,
717 ast::LitIntType::Unsigned(ast::UintTy::U32)))
719 fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
720 self.expr_lit(sp, ast::LitKind::Int(u as u128,
721 ast::LitIntType::Unsigned(ast::UintTy::U16)))
723 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
724 self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
726 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
727 self.expr_lit(sp, ast::LitKind::Bool(value))
730 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
731 self.expr(sp, ast::ExprKind::Array(exprs))
733 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
734 self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
737 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
738 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
740 fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
741 self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
744 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
745 self.expr(sp, ast::ExprKind::Cast(expr, ty))
749 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
750 let some = self.std_path(&["option", "Option", "Some"]);
751 self.expr_call_global(sp, some, vec![expr])
754 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
755 let none = self.std_path(&["option", "Option", "None"]);
756 let none = self.path_global(sp, none);
761 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
762 self.expr(sp, ast::ExprKind::Break(None, None))
766 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
767 self.expr(sp, ast::ExprKind::Tup(exprs))
770 fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
771 let loc = self.source_map().lookup_char_pos(span.lo());
772 let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
773 let expr_line = self.expr_u32(span, loc.line as u32);
774 let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
775 let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
776 let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
777 self.expr_call_global(
779 self.std_path(&["rt", "begin_panic"]),
781 self.expr_str(span, msg),
785 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
786 self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
789 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
790 let ok = self.std_path(&["result", "Result", "Ok"]);
791 self.expr_call_global(sp, ok, vec![expr])
794 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
795 let err = self.std_path(&["result", "Result", "Err"]);
796 self.expr_call_global(sp, err, vec![expr])
799 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
800 let ok = self.std_path(&["result", "Result", "Ok"]);
801 let ok_path = self.path_global(sp, ok);
802 let err = self.std_path(&["result", "Result", "Err"]);
803 let err_path = self.path_global(sp, err);
805 let binding_variable = self.ident_of("__try_var");
806 let binding_pat = self.pat_ident(sp, binding_variable);
807 let binding_expr = self.expr_ident(sp, binding_variable);
809 // Ok(__try_var) pattern
810 let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
812 // Err(__try_var) (pattern and expression resp.)
813 let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
814 let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
815 vec![binding_expr.clone()]);
816 // return Err(__try_var)
817 let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
819 // Ok(__try_var) => __try_var
820 let ok_arm = self.arm(sp, vec![ok_pat], binding_expr);
821 // Err(__try_var) => return Err(__try_var)
822 let err_arm = self.arm(sp, vec![err_pat], err_expr);
824 // match head { Ok() => ..., Err() => ... }
825 self.expr_match(sp, head, vec![ok_arm, err_arm])
829 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
830 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
832 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
833 self.pat(span, PatKind::Wild)
835 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
836 self.pat(span, PatKind::Lit(expr))
838 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
839 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
840 self.pat_ident_binding_mode(span, ident, binding_mode)
843 fn pat_ident_binding_mode(&self,
846 bm: ast::BindingMode) -> P<ast::Pat> {
847 let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
850 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
851 self.pat(span, PatKind::Path(None, path))
853 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
854 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
855 self.pat(span, PatKind::TupleStruct(path, subpats, None))
857 fn pat_struct(&self, span: Span, path: ast::Path,
858 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
859 self.pat(span, PatKind::Struct(path, field_pats, false))
861 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
862 self.pat(span, PatKind::Tuple(pats, None))
865 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
866 let some = self.std_path(&["option", "Option", "Some"]);
867 let path = self.path_global(span, some);
868 self.pat_tuple_struct(span, path, vec![pat])
871 fn pat_none(&self, span: Span) -> P<ast::Pat> {
872 let some = self.std_path(&["option", "Option", "None"]);
873 let path = self.path_global(span, some);
874 self.pat_path(span, path)
877 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
878 let some = self.std_path(&["result", "Result", "Ok"]);
879 let path = self.path_global(span, some);
880 self.pat_tuple_struct(span, path, vec![pat])
883 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
884 let some = self.std_path(&["result", "Result", "Err"]);
885 let path = self.path_global(span, some);
886 self.pat_tuple_struct(span, path, vec![pat])
889 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
898 fn arm_unreachable(&self, span: Span) -> ast::Arm {
899 self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span))
902 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
903 self.expr(span, ast::ExprKind::Match(arg, arms))
906 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
907 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
908 let els = els.map(|x| self.expr_block(self.block_expr(x)));
909 self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
912 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
913 self.expr(span, ast::ExprKind::Loop(block, None))
916 fn lambda_fn_decl(&self,
918 fn_decl: P<ast::FnDecl>,
920 fn_decl_span: Span) // span of the `|...|` part
922 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
923 ast::IsAsync::NotAsync,
924 ast::Movability::Movable,
932 ids: Vec<ast::Ident>,
935 let fn_decl = self.fn_decl(
936 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
937 ast::FunctionRetTy::Default(span));
939 // FIXME -- We are using `span` as the span of the `|...|`
940 // part of the lambda, but it probably (maybe?) corresponds to
941 // the entire lambda body. Probably we should extend the API
942 // here, but that's not entirely clear.
943 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
944 ast::IsAsync::NotAsync,
945 ast::Movability::Movable,
951 fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
952 self.lambda(span, Vec::new(), body)
955 fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
956 self.lambda(span, vec![ident], body)
959 fn lambda_stmts(&self,
961 ids: Vec<ast::Ident>,
962 stmts: Vec<ast::Stmt>)
964 self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
966 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
967 self.lambda0(span, self.expr_block(self.block(span, stmts)))
969 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
970 ident: ast::Ident) -> P<ast::Expr> {
971 self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
974 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
975 let arg_pat = self.pat_ident(span, ident);
979 id: ast::DUMMY_NODE_ID
984 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
992 fn item(&self, span: Span, name: Ident,
993 attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
994 // FIXME: Would be nice if our generated code didn't violate
995 // Rust coding conventions
999 id: ast::DUMMY_NODE_ID,
1001 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1007 fn item_fn_poly(&self,
1010 inputs: Vec<ast::Arg> ,
1013 body: P<ast::Block>) -> P<ast::Item> {
1017 ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
1019 unsafety: ast::Unsafety::Normal,
1020 asyncness: dummy_spanned(ast::IsAsync::NotAsync),
1021 constness: dummy_spanned(ast::Constness::NotConst),
1031 inputs: Vec<ast::Arg> ,
1040 Generics::default(),
1044 fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1045 let fields: Vec<_> = tys.into_iter().map(|ty| {
1050 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1052 id: ast::DUMMY_NODE_ID,
1056 let vdata = if fields.is_empty() {
1057 ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1059 ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1065 id: ast::DUMMY_NODE_ID,
1072 fn item_enum_poly(&self, span: Span, name: Ident,
1073 enum_definition: ast::EnumDef,
1074 generics: Generics) -> P<ast::Item> {
1075 self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1078 fn item_enum(&self, span: Span, name: Ident,
1079 enum_definition: ast::EnumDef) -> P<ast::Item> {
1080 self.item_enum_poly(span, name, enum_definition,
1081 Generics::default())
1084 fn item_struct(&self, span: Span, name: Ident,
1085 struct_def: ast::VariantData) -> P<ast::Item> {
1086 self.item_struct_poly(
1094 fn item_struct_poly(&self, span: Span, name: Ident,
1095 struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1096 self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1099 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1100 attrs: Vec<ast::Attribute>,
1101 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1106 ast::ItemKind::Mod(ast::Mod {
1114 fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item> {
1115 self.item(span, name, Vec::new(), ast::ItemKind::ExternCrate(None))
1118 fn item_static(&self,
1122 mutbl: ast::Mutability,
1125 self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1128 fn item_const(&self,
1134 self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1137 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1138 generics: Generics) -> P<ast::Item> {
1139 self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1142 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1143 self.item_ty_poly(span, name, ty, Generics::default())
1146 fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute {
1147 attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
1150 fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
1151 attr::mk_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1154 fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
1155 attr::mk_nested_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1158 fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
1160 attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
1163 fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
1165 attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp),
1169 fn item_use(&self, sp: Span,
1170 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item> {
1172 id: ast::DUMMY_NODE_ID,
1173 ident: keywords::Invalid.ident(),
1175 node: ast::ItemKind::Use(vp),
1182 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1183 self.item_use_simple_(sp, vis, None, path)
1186 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1187 rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
1188 self.item_use(sp, vis, P(ast::UseTree {
1191 kind: ast::UseTreeKind::Simple(rename, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1195 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1196 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1197 let imports = imports.iter().map(|id| {
1200 prefix: self.path(sp, vec![*id]),
1201 kind: ast::UseTreeKind::Simple(None, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1202 }, ast::DUMMY_NODE_ID)
1205 self.item_use(sp, vis, P(ast::UseTree {
1207 prefix: self.path(sp, path),
1208 kind: ast::UseTreeKind::Nested(imports),
1212 fn item_use_glob(&self, sp: Span,
1213 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1214 self.item_use(sp, vis, P(ast::UseTree {
1216 prefix: self.path(sp, path),
1217 kind: ast::UseTreeKind::Glob,