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 // Transitional re-exports so qquote can find the paths it is looking for
18 pub trait AstBuilder {
20 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
21 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
22 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
23 fn path_all(&self, sp: Span,
25 idents: Vec<ast::Ident>,
26 args: Vec<ast::GenericArg>,
27 bindings: Vec<ast::TypeBinding>)
30 fn qpath(&self, self_type: P<ast::Ty>,
31 trait_path: ast::Path,
33 -> (ast::QSelf, ast::Path);
34 fn qpath_all(&self, self_type: P<ast::Ty>,
35 trait_path: ast::Path,
37 args: Vec<ast::GenericArg>,
38 bindings: Vec<ast::TypeBinding>)
39 -> (ast::QSelf, ast::Path);
42 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
44 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
45 fn ty_path(&self, path: ast::Path) -> P<ast::Ty>;
46 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
47 fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst;
48 fn const_ident(&self, span: Span, idents: ast::Ident) -> ast::AnonConst;
50 fn ty_rptr(&self, span: Span,
52 lifetime: Option<ast::Lifetime>,
53 mutbl: ast::Mutability) -> P<ast::Ty>;
54 fn ty_ptr(&self, span: Span,
56 mutbl: ast::Mutability) -> P<ast::Ty>;
58 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
59 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
64 attrs: Vec<ast::Attribute>,
65 bounds: ast::GenericBounds,
66 default: Option<P<ast::Ty>>) -> ast::GenericParam;
68 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
69 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
70 fn trait_bound(&self, path: ast::Path) -> ast::GenericBound;
71 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime;
72 fn lifetime_def(&self,
75 attrs: Vec<ast::Attribute>,
76 bounds: ast::GenericBounds)
80 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
81 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
82 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
83 fn stmt_let_typed(&self,
90 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
91 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
94 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
95 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
98 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
99 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
100 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
101 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
103 fn expr_self(&self, span: Span) -> P<ast::Expr>;
104 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
105 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
106 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
107 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
109 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
110 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
111 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
112 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
113 idx: usize) -> P<ast::Expr>;
114 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
115 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
116 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
117 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
118 fn expr_method_call(&self, span: Span,
119 expr: P<ast::Expr>, ident: ast::Ident,
120 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
121 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
122 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
124 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
125 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
126 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
127 fields: Vec<ast::Field>) -> P<ast::Expr>;
129 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
131 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
132 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
133 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
134 fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr>;
135 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
136 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
138 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
139 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
140 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
141 fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr>;
143 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
144 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
146 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
148 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
150 fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr>;
151 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
153 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
154 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
155 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
157 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
158 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
159 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
160 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
162 fn pat_ident_binding_mode(&self,
165 bm: ast::BindingMode) -> P<ast::Pat>;
166 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat>;
167 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
168 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
169 fn pat_struct(&self, span: Span, path: ast::Path,
170 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat>;
171 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
173 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
174 fn pat_none(&self, span: Span) -> P<ast::Pat>;
176 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
177 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
179 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
180 fn arm_unreachable(&self, span: Span) -> ast::Arm;
182 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
183 fn expr_if(&self, span: Span,
184 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
185 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
187 fn lambda_fn_decl(&self,
189 fn_decl: P<ast::FnDecl>,
194 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr>;
195 fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr>;
196 fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
198 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
199 blk: Vec<ast::Stmt>) -> P<ast::Expr>;
200 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
201 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
202 ident: ast::Ident) -> P<ast::Expr>;
205 fn item(&self, span: Span,
206 name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
208 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
210 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: ast::FunctionRetTy) -> P<ast::FnDecl>;
212 fn item_fn_poly(&self,
215 inputs: Vec<ast::Arg> ,
218 body: P<ast::Block>) -> P<ast::Item>;
222 inputs: Vec<ast::Arg> ,
224 body: P<ast::Block>) -> P<ast::Item>;
226 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
227 fn item_enum_poly(&self,
230 enum_definition: ast::EnumDef,
231 generics: Generics) -> P<ast::Item>;
232 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
234 fn item_struct_poly(&self,
237 struct_def: ast::VariantData,
238 generics: Generics) -> P<ast::Item>;
239 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
241 fn item_mod(&self, span: Span, inner_span: Span,
242 name: Ident, attrs: Vec<ast::Attribute>,
243 items: Vec<P<ast::Item>>) -> P<ast::Item>;
245 fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item>;
247 fn item_static(&self,
251 mutbl: ast::Mutability,
262 fn item_ty_poly(&self,
266 generics: Generics) -> P<ast::Item>;
267 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
269 fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute;
271 fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem;
273 fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem;
278 mis: Vec<ast::NestedMetaItem> )
280 fn meta_name_value(&self,
286 fn item_use(&self, sp: Span,
287 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item>;
288 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
289 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
290 ident: Option<ast::Ident>, path: ast::Path) -> P<ast::Item>;
291 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
292 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
293 fn item_use_glob(&self, sp: Span,
294 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
297 impl<'a> AstBuilder for ExtCtxt<'a> {
298 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
299 self.path_all(span, false, strs, vec![], vec![])
301 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
302 self.path(span, vec![id])
304 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
305 self.path_all(span, true, strs, vec![], vec![])
310 mut idents: Vec<ast::Ident> ,
311 args: Vec<ast::GenericArg>,
312 bindings: Vec<ast::TypeBinding> )
314 assert!(!idents.is_empty());
315 let add_root = global && !idents[0].is_path_segment_keyword();
316 let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
318 segments.push(ast::PathSegment::path_root(span));
320 let last_ident = idents.pop().unwrap();
321 segments.extend(idents.into_iter().map(|ident| {
322 ast::PathSegment::from_ident(ident.with_span_pos(span))
324 let args = if !args.is_empty() || !bindings.is_empty() {
325 ast::AngleBracketedArgs { args, bindings, span }.into()
329 segments.push(ast::PathSegment {
330 ident: last_ident.with_span_pos(span),
331 id: ast::DUMMY_NODE_ID,
334 ast::Path { span, segments }
337 /// Constructs a qualified path.
339 /// Constructs a path like `<self_type as trait_path>::ident`.
341 self_type: P<ast::Ty>,
342 trait_path: ast::Path,
344 -> (ast::QSelf, ast::Path) {
345 self.qpath_all(self_type, trait_path, ident, vec![], vec![])
348 /// Constructs a qualified path.
350 /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
352 self_type: P<ast::Ty>,
353 trait_path: ast::Path,
355 args: Vec<ast::GenericArg>,
356 bindings: Vec<ast::TypeBinding>)
357 -> (ast::QSelf, ast::Path) {
358 let mut path = trait_path;
359 let args = if !args.is_empty() || !bindings.is_empty() {
360 ast::AngleBracketedArgs { args, bindings, span: ident.span }.into()
364 path.segments.push(ast::PathSegment { ident, id: ast::DUMMY_NODE_ID, args });
368 path_span: path.span,
369 position: path.segments.len() - 1
373 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
380 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
382 id: ast::DUMMY_NODE_ID,
388 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
389 self.ty(path.span, ast::TyKind::Path(None, path))
392 // Might need to take bounds as an argument in the future, if you ever want
393 // to generate a bounded existential trait type.
394 fn ty_ident(&self, span: Span, ident: ast::Ident)
396 self.ty_path(self.path_ident(span, ident))
399 fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst {
401 id: ast::DUMMY_NODE_ID,
403 id: ast::DUMMY_NODE_ID,
406 attrs: ThinVec::new(),
411 fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
412 self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
418 lifetime: Option<ast::Lifetime>,
419 mutbl: ast::Mutability)
422 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
428 mutbl: ast::Mutability)
431 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
434 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
436 self.path_all(DUMMY_SP,
438 self.std_path(&["option", "Option"]),
439 vec![ast::GenericArg::Type(ty)],
443 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
444 self.ty(span, ast::TyKind::Infer)
450 attrs: Vec<ast::Attribute>,
451 bounds: ast::GenericBounds,
452 default: Option<P<ast::Ty>>) -> ast::GenericParam {
454 ident: ident.with_span_pos(span),
455 id: ast::DUMMY_NODE_ID,
458 kind: ast::GenericParamKind::Type {
464 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
467 ref_id: ast::DUMMY_NODE_ID,
471 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
473 bound_generic_params: Vec::new(),
474 trait_ref: self.trait_ref(path),
479 fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
480 ast::GenericBound::Trait(self.poly_trait_ref(path.span, path),
481 ast::TraitBoundModifier::None)
484 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
485 ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
488 fn lifetime_def(&self,
491 attrs: Vec<ast::Attribute>,
492 bounds: ast::GenericBounds)
493 -> ast::GenericParam {
494 let lifetime = self.lifetime(span, ident);
496 ident: lifetime.ident,
500 kind: ast::GenericParamKind::Lifetime,
504 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
506 id: ast::DUMMY_NODE_ID,
508 node: ast::StmtKind::Expr(expr),
512 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
514 id: ast::DUMMY_NODE_ID,
516 node: ast::StmtKind::Semi(expr),
520 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
521 ex: P<ast::Expr>) -> ast::Stmt {
523 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
524 self.pat_ident_binding_mode(sp, ident, binding_mode)
526 self.pat_ident(sp, ident)
528 let local = P(ast::Local {
532 id: ast::DUMMY_NODE_ID,
534 attrs: ThinVec::new(),
537 id: ast::DUMMY_NODE_ID,
538 node: ast::StmtKind::Local(local),
543 fn stmt_let_typed(&self,
551 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
552 self.pat_ident_binding_mode(sp, ident, binding_mode)
554 self.pat_ident(sp, ident)
556 let local = P(ast::Local {
560 id: ast::DUMMY_NODE_ID,
562 attrs: ThinVec::new(),
565 id: ast::DUMMY_NODE_ID,
566 node: ast::StmtKind::Local(local),
571 // Generate `let _: Type;`, usually used for type assertions.
572 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
573 let local = P(ast::Local {
574 pat: self.pat_wild(span),
577 id: ast::DUMMY_NODE_ID,
579 attrs: ThinVec::new(),
582 id: ast::DUMMY_NODE_ID,
583 node: ast::StmtKind::Local(local),
588 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
590 id: ast::DUMMY_NODE_ID,
591 node: ast::StmtKind::Item(item),
596 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
597 self.block(expr.span, vec![ast::Stmt {
598 id: ast::DUMMY_NODE_ID,
600 node: ast::StmtKind::Expr(expr),
603 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
606 id: ast::DUMMY_NODE_ID,
607 rules: BlockCheckMode::Default,
612 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
614 id: ast::DUMMY_NODE_ID,
617 attrs: ThinVec::new(),
621 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
622 self.expr(path.span, ast::ExprKind::Path(None, path))
625 /// Constructs a QPath expression.
626 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
627 self.expr(span, ast::ExprKind::Path(Some(qself), path))
630 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
631 self.expr_path(self.path_ident(span, id))
633 fn expr_self(&self, span: Span) -> P<ast::Expr> {
634 self.expr_ident(span, keywords::SelfLower.ident())
637 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
638 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
639 self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
642 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
643 self.expr_unary(sp, UnOp::Deref, e)
645 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
646 self.expr(sp, ast::ExprKind::Unary(op, e))
649 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
650 self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
652 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
653 let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
654 self.expr(sp, ast::ExprKind::Field(expr, ident))
656 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
657 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
659 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
660 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
663 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
664 self.expr(span, ast::ExprKind::Call(expr, args))
666 fn expr_call_ident(&self, span: Span, id: ast::Ident,
667 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
668 self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
670 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
671 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
672 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
673 self.expr_call(sp, pathexpr, args)
675 fn expr_method_call(&self, span: Span,
678 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
679 args.insert(0, expr);
680 let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
681 self.expr(span, ast::ExprKind::MethodCall(segment, args))
683 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
684 self.expr(b.span, ast::ExprKind::Block(b, None))
686 fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
688 ident: ident.with_span_pos(span),
692 attrs: ThinVec::new(),
695 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
696 self.expr(span, ast::ExprKind::Struct(path, fields, None))
698 fn expr_struct_ident(&self, span: Span,
699 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
700 self.expr_struct(span, self.path_ident(span, id), fields)
703 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
704 self.expr(sp, ast::ExprKind::Lit(respan(sp, lit)))
706 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
707 self.expr_lit(span, ast::LitKind::Int(i as u128,
708 ast::LitIntType::Unsigned(ast::UintTy::Usize)))
710 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
712 let i = (-i) as u128;
713 let lit_ty = ast::LitIntType::Signed(ast::IntTy::Isize);
714 let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
715 self.expr_unary(sp, ast::UnOp::Neg, lit)
717 self.expr_lit(sp, ast::LitKind::Int(i as u128,
718 ast::LitIntType::Signed(ast::IntTy::Isize)))
721 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
722 self.expr_lit(sp, ast::LitKind::Int(u as u128,
723 ast::LitIntType::Unsigned(ast::UintTy::U32)))
725 fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
726 self.expr_lit(sp, ast::LitKind::Int(u as u128,
727 ast::LitIntType::Unsigned(ast::UintTy::U16)))
729 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
730 self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
732 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
733 self.expr_lit(sp, ast::LitKind::Bool(value))
736 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
737 self.expr(sp, ast::ExprKind::Array(exprs))
739 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
740 self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
743 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
744 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
746 fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
747 self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
750 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
751 self.expr(sp, ast::ExprKind::Cast(expr, ty))
755 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
756 let some = self.std_path(&["option", "Option", "Some"]);
757 self.expr_call_global(sp, some, vec![expr])
760 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
761 let none = self.std_path(&["option", "Option", "None"]);
762 let none = self.path_global(sp, none);
767 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
768 self.expr(sp, ast::ExprKind::Break(None, None))
772 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
773 self.expr(sp, ast::ExprKind::Tup(exprs))
776 fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
777 let loc = self.source_map().lookup_char_pos(span.lo());
778 let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
779 let expr_line = self.expr_u32(span, loc.line as u32);
780 let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
781 let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
782 let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
783 self.expr_call_global(
785 self.std_path(&["rt", "begin_panic"]),
787 self.expr_str(span, msg),
791 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
792 self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
795 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
796 let ok = self.std_path(&["result", "Result", "Ok"]);
797 self.expr_call_global(sp, ok, vec![expr])
800 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
801 let err = self.std_path(&["result", "Result", "Err"]);
802 self.expr_call_global(sp, err, vec![expr])
805 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
806 let ok = self.std_path(&["result", "Result", "Ok"]);
807 let ok_path = self.path_global(sp, ok);
808 let err = self.std_path(&["result", "Result", "Err"]);
809 let err_path = self.path_global(sp, err);
811 let binding_variable = self.ident_of("__try_var");
812 let binding_pat = self.pat_ident(sp, binding_variable);
813 let binding_expr = self.expr_ident(sp, binding_variable);
815 // Ok(__try_var) pattern
816 let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
818 // Err(__try_var) (pattern and expression resp.)
819 let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
820 let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
821 vec![binding_expr.clone()]);
822 // return Err(__try_var)
823 let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
825 // Ok(__try_var) => __try_var
826 let ok_arm = self.arm(sp, vec![ok_pat], binding_expr);
827 // Err(__try_var) => return Err(__try_var)
828 let err_arm = self.arm(sp, vec![err_pat], err_expr);
830 // match head { Ok() => ..., Err() => ... }
831 self.expr_match(sp, head, vec![ok_arm, err_arm])
835 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
836 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
838 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
839 self.pat(span, PatKind::Wild)
841 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
842 self.pat(span, PatKind::Lit(expr))
844 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
845 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
846 self.pat_ident_binding_mode(span, ident, binding_mode)
849 fn pat_ident_binding_mode(&self,
852 bm: ast::BindingMode) -> P<ast::Pat> {
853 let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
856 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
857 self.pat(span, PatKind::Path(None, path))
859 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
860 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
861 self.pat(span, PatKind::TupleStruct(path, subpats, None))
863 fn pat_struct(&self, span: Span, path: ast::Path,
864 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
865 self.pat(span, PatKind::Struct(path, field_pats, false))
867 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
868 self.pat(span, PatKind::Tuple(pats, None))
871 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
872 let some = self.std_path(&["option", "Option", "Some"]);
873 let path = self.path_global(span, some);
874 self.pat_tuple_struct(span, path, vec![pat])
877 fn pat_none(&self, span: Span) -> P<ast::Pat> {
878 let some = self.std_path(&["option", "Option", "None"]);
879 let path = self.path_global(span, some);
880 self.pat_path(span, path)
883 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
884 let some = self.std_path(&["result", "Result", "Ok"]);
885 let path = self.path_global(span, some);
886 self.pat_tuple_struct(span, path, vec![pat])
889 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
890 let some = self.std_path(&["result", "Result", "Err"]);
891 let path = self.path_global(span, some);
892 self.pat_tuple_struct(span, path, vec![pat])
895 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
904 fn arm_unreachable(&self, span: Span) -> ast::Arm {
905 self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span))
908 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
909 self.expr(span, ast::ExprKind::Match(arg, arms))
912 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
913 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
914 let els = els.map(|x| self.expr_block(self.block_expr(x)));
915 self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
918 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
919 self.expr(span, ast::ExprKind::Loop(block, None))
922 fn lambda_fn_decl(&self,
924 fn_decl: P<ast::FnDecl>,
926 fn_decl_span: Span) // span of the `|...|` part
928 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
929 ast::IsAsync::NotAsync,
930 ast::Movability::Movable,
938 ids: Vec<ast::Ident>,
941 let fn_decl = self.fn_decl(
942 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
943 ast::FunctionRetTy::Default(span));
945 // FIXME -- We are using `span` as the span of the `|...|`
946 // part of the lambda, but it probably (maybe?) corresponds to
947 // the entire lambda body. Probably we should extend the API
948 // here, but that's not entirely clear.
949 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
950 ast::IsAsync::NotAsync,
951 ast::Movability::Movable,
957 fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
958 self.lambda(span, Vec::new(), body)
961 fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
962 self.lambda(span, vec![ident], body)
965 fn lambda_stmts(&self,
967 ids: Vec<ast::Ident>,
968 stmts: Vec<ast::Stmt>)
970 self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
972 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
973 self.lambda0(span, self.expr_block(self.block(span, stmts)))
975 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
976 ident: ast::Ident) -> P<ast::Expr> {
977 self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
980 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
981 let arg_pat = self.pat_ident(span, ident);
985 id: ast::DUMMY_NODE_ID
990 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
998 fn item(&self, span: Span, name: Ident,
999 attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
1000 // FIXME: Would be nice if our generated code didn't violate
1001 // Rust coding conventions
1005 id: ast::DUMMY_NODE_ID,
1007 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1013 fn item_fn_poly(&self,
1016 inputs: Vec<ast::Arg> ,
1019 body: P<ast::Block>) -> P<ast::Item> {
1023 ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
1025 unsafety: ast::Unsafety::Normal,
1026 asyncness: ast::IsAsync::NotAsync,
1027 constness: dummy_spanned(ast::Constness::NotConst),
1037 inputs: Vec<ast::Arg> ,
1046 Generics::default(),
1050 fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1051 let fields: Vec<_> = tys.into_iter().map(|ty| {
1056 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
1058 id: ast::DUMMY_NODE_ID,
1062 let vdata = if fields.is_empty() {
1063 ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1065 ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1077 fn item_enum_poly(&self, span: Span, name: Ident,
1078 enum_definition: ast::EnumDef,
1079 generics: Generics) -> P<ast::Item> {
1080 self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1083 fn item_enum(&self, span: Span, name: Ident,
1084 enum_definition: ast::EnumDef) -> P<ast::Item> {
1085 self.item_enum_poly(span, name, enum_definition,
1086 Generics::default())
1089 fn item_struct(&self, span: Span, name: Ident,
1090 struct_def: ast::VariantData) -> P<ast::Item> {
1091 self.item_struct_poly(
1099 fn item_struct_poly(&self, span: Span, name: Ident,
1100 struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1101 self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1104 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1105 attrs: Vec<ast::Attribute>,
1106 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1111 ast::ItemKind::Mod(ast::Mod {
1119 fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item> {
1120 self.item(span, name, Vec::new(), ast::ItemKind::ExternCrate(None))
1123 fn item_static(&self,
1127 mutbl: ast::Mutability,
1130 self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1133 fn item_const(&self,
1139 self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1142 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1143 generics: Generics) -> P<ast::Item> {
1144 self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1147 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1148 self.item_ty_poly(span, name, ty, Generics::default())
1151 fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute {
1152 attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
1155 fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
1156 attr::mk_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1159 fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
1160 attr::mk_nested_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp))
1163 fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
1165 attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
1168 fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
1170 attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp),
1174 fn item_use(&self, sp: Span,
1175 vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item> {
1177 id: ast::DUMMY_NODE_ID,
1178 ident: keywords::Invalid.ident(),
1180 node: ast::ItemKind::Use(vp),
1187 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1188 self.item_use_simple_(sp, vis, None, path)
1191 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1192 rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
1193 self.item_use(sp, vis, P(ast::UseTree {
1196 kind: ast::UseTreeKind::Simple(rename, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1200 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1201 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1202 let imports = imports.iter().map(|id| {
1205 prefix: self.path(sp, vec![*id]),
1206 kind: ast::UseTreeKind::Simple(None, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
1207 }, ast::DUMMY_NODE_ID)
1210 self.item_use(sp, vis, P(ast::UseTree {
1212 prefix: self.path(sp, path),
1213 kind: ast::UseTreeKind::Nested(imports),
1217 fn item_use_glob(&self, sp: Span,
1218 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1219 self.item_use(sp, vis, P(ast::UseTree {
1221 prefix: self.path(sp, path),
1222 kind: ast::UseTreeKind::Glob,