1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use ast::{Ident, Generics, Expr};
16 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
17 use ext::base::ExtCtxt;
18 use owned_slice::OwnedSlice;
19 use parse::token::special_idents;
20 use parse::token::InternedString;
25 // Transitional reexports so qquote can find the paths it is looking for
31 pub trait AstBuilder {
33 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
34 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
35 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
36 fn path_all(&self, sp: Span,
38 idents: Vec<ast::Ident> ,
39 lifetimes: Vec<ast::Lifetime>,
40 types: Vec<P<ast::Ty>>,
41 bindings: Vec<P<ast::TypeBinding>> )
45 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
47 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty>;
48 fn ty_path(&self, ast::Path) -> P<ast::Ty>;
49 fn ty_sum(&self, ast::Path, OwnedSlice<ast::TyParamBound>) -> P<ast::Ty>;
50 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
52 fn ty_rptr(&self, span: Span,
54 lifetime: Option<ast::Lifetime>,
55 mutbl: ast::Mutability) -> P<ast::Ty>;
56 fn ty_ptr(&self, span: Span,
58 mutbl: ast::Mutability) -> P<ast::Ty>;
60 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
61 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
63 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
64 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
65 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField;
70 bounds: OwnedSlice<ast::TyParamBound>,
71 default: Option<P<ast::Ty>>) -> ast::TyParam;
73 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
74 fn poly_trait_ref(&self, path: ast::Path) -> ast::PolyTraitRef;
75 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
76 fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
77 fn lifetime_def(&self,
80 bounds: Vec<ast::Lifetime>)
84 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt>;
85 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> P<ast::Stmt>;
86 fn stmt_let_typed(&self,
93 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt>;
96 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
97 expr: Option<P<ast::Expr>>) -> P<ast::Block>;
98 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
99 fn block_all(&self, span: Span,
100 view_items: Vec<ast::ViewItem>,
101 stmts: Vec<P<ast::Stmt>>,
102 expr: Option<P<ast::Expr>>) -> P<ast::Block>;
105 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>;
106 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
107 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
109 fn expr_self(&self, span: Span) -> P<ast::Expr>;
110 fn expr_binary(&self, sp: Span, op: ast::BinOp,
111 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
112 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
113 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
115 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
116 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
117 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
118 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
119 idx: usize) -> P<ast::Expr>;
120 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
121 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
122 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
123 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
124 fn expr_method_call(&self, span: Span,
125 expr: P<ast::Expr>, ident: ast::Ident,
126 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
127 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
128 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
130 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
131 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
132 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
133 fields: Vec<ast::Field>) -> P<ast::Expr>;
135 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>;
137 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
138 fn expr_int(&self, sp: Span, i: isize) -> P<ast::Expr>;
139 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
140 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
142 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
143 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
144 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
145 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
147 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
148 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
150 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
152 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
154 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
155 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
157 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
158 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
159 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
161 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
162 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
163 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
164 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
166 fn pat_ident_binding_mode(&self,
169 bm: ast::BindingMode) -> P<ast::Pat>;
170 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
171 fn pat_struct(&self, span: Span,
172 path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>> ) -> P<ast::Pat>;
173 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
175 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
176 fn pat_none(&self, span: Span) -> P<ast::Pat>;
178 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
179 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
181 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
182 fn arm_unreachable(&self, span: Span) -> ast::Arm;
184 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
185 fn expr_if(&self, span: Span,
186 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
187 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
189 fn lambda_fn_decl(&self, span: Span,
190 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
192 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
193 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
194 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
196 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
197 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
198 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
200 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
201 blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
202 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
203 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
204 ident: ast::Ident) -> P<ast::Expr>;
207 fn item(&self, span: Span,
208 name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>;
210 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
212 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
214 fn item_fn_poly(&self,
217 inputs: Vec<ast::Arg> ,
220 body: P<ast::Block>) -> P<ast::Item>;
224 inputs: Vec<ast::Arg> ,
226 body: P<ast::Block>) -> P<ast::Item>;
228 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
229 fn item_enum_poly(&self,
232 enum_definition: ast::EnumDef,
233 generics: Generics) -> P<ast::Item>;
234 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
236 fn item_struct_poly(&self,
239 struct_def: ast::StructDef,
240 generics: Generics) -> P<ast::Item>;
241 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> P<ast::Item>;
243 fn item_mod(&self, span: Span, inner_span: Span,
244 name: Ident, attrs: Vec<ast::Attribute>,
245 vi: Vec<ast::ViewItem> , items: Vec<P<ast::Item>> ) -> 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: P<ast::MetaItem>) -> ast::Attribute;
271 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
274 name: InternedString,
275 mis: Vec<P<ast::MetaItem>> )
277 fn meta_name_value(&self,
279 name: InternedString,
283 fn view_use(&self, sp: Span,
284 vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem;
285 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
286 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
287 ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
288 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
289 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
290 fn view_use_glob(&self, sp: Span,
291 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
294 impl<'a> AstBuilder for ExtCtxt<'a> {
295 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
296 self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
298 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
299 self.path(span, vec!(id))
301 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
302 self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
307 mut idents: Vec<ast::Ident> ,
308 lifetimes: Vec<ast::Lifetime>,
309 types: Vec<P<ast::Ty>>,
310 bindings: Vec<P<ast::TypeBinding>> )
312 let last_identifier = idents.pop().unwrap();
313 let mut segments: Vec<ast::PathSegment> = idents.into_iter()
317 parameters: ast::PathParameters::none(),
320 segments.push(ast::PathSegment {
321 identifier: last_identifier,
322 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
323 lifetimes: lifetimes,
324 types: OwnedSlice::from_vec(types),
325 bindings: OwnedSlice::from_vec(bindings),
335 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
342 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
344 id: ast::DUMMY_NODE_ID,
350 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
351 self.ty(path.span, ast::TyPath(path, ast::DUMMY_NODE_ID))
354 fn ty_sum(&self, path: ast::Path, bounds: OwnedSlice<ast::TyParamBound>) -> P<ast::Ty> {
356 ast::TyObjectSum(self.ty_path(path),
360 // Might need to take bounds as an argument in the future, if you ever want
361 // to generate a bounded existential trait type.
362 fn ty_ident(&self, span: Span, ident: ast::Ident)
364 self.ty_path(self.path_ident(span, ident))
370 lifetime: Option<ast::Lifetime>,
371 mutbl: ast::Mutability)
374 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
380 mutbl: ast::Mutability)
383 ast::TyPtr(self.ty_mt(ty, mutbl)))
386 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
388 self.path_all(DUMMY_SP,
391 self.ident_of("std"),
392 self.ident_of("option"),
393 self.ident_of("Option")
400 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
403 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
408 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
409 self.ty(span, ast::TyInfer)
415 bounds: OwnedSlice<ast::TyParamBound>,
416 default: Option<P<ast::Ty>>) -> ast::TyParam {
419 id: ast::DUMMY_NODE_ID,
426 // these are strange, and probably shouldn't be used outside of
427 // pipes. Specifically, the global version possible generates
429 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
430 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
433 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
436 .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
440 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
443 ref_id: ast::DUMMY_NODE_ID,
447 fn poly_trait_ref(&self, path: ast::Path) -> ast::PolyTraitRef {
449 bound_lifetimes: Vec::new(),
450 trait_ref: self.trait_ref(path)
454 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
455 ast::TraitTyParamBound(self.poly_trait_ref(path), ast::TraitBoundModifier::None)
458 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
459 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
462 fn lifetime_def(&self,
465 bounds: Vec<ast::Lifetime>)
466 -> ast::LifetimeDef {
468 lifetime: self.lifetime(span, name),
473 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> {
474 P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)))
477 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
478 ex: P<ast::Expr>) -> P<ast::Stmt> {
480 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
482 self.pat_ident(sp, ident)
484 let local = P(ast::Local {
488 id: ast::DUMMY_NODE_ID,
490 source: ast::LocalLet,
492 let decl = respan(sp, ast::DeclLocal(local));
493 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
496 fn stmt_let_typed(&self,
504 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
506 self.pat_ident(sp, ident)
508 let local = P(ast::Local {
512 id: ast::DUMMY_NODE_ID,
514 source: ast::LocalLet,
516 let decl = respan(sp, ast::DeclLocal(local));
517 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
520 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
521 expr: Option<P<Expr>>) -> P<ast::Block> {
522 self.block_all(span, Vec::new(), stmts, expr)
525 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
526 let decl = respan(sp, ast::DeclItem(item));
527 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
530 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
531 self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
535 view_items: Vec<ast::ViewItem>,
536 stmts: Vec<P<ast::Stmt>>,
537 expr: Option<P<ast::Expr>>) -> P<ast::Block> {
539 view_items: view_items,
542 id: ast::DUMMY_NODE_ID,
543 rules: ast::DefaultBlock,
548 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> {
550 id: ast::DUMMY_NODE_ID,
556 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
557 self.expr(path.span, ast::ExprPath(path))
560 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
561 self.expr_path(self.path_ident(span, id))
563 fn expr_self(&self, span: Span) -> P<ast::Expr> {
564 self.expr_ident(span, special_idents::self_)
567 fn expr_binary(&self, sp: Span, op: ast::BinOp,
568 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
569 self.expr(sp, ast::ExprBinary(op, lhs, rhs))
572 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
573 self.expr_unary(sp, ast::UnDeref, e)
575 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
576 self.expr(sp, ast::ExprUnary(op, e))
579 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
580 let field_name = token::get_ident(ident);
581 let field_span = Span {
582 lo: sp.lo - Pos::from_usize(field_name.get().len()),
587 let id = Spanned { node: ident, span: field_span };
588 self.expr(sp, ast::ExprField(expr, id))
590 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
591 let field_span = Span {
592 lo: sp.lo - Pos::from_usize(idx.to_string().len()),
597 let id = Spanned { node: idx, span: field_span };
598 self.expr(sp, ast::ExprTupField(expr, id))
600 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
601 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
603 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
604 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
607 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
608 self.expr(span, ast::ExprCall(expr, args))
610 fn expr_call_ident(&self, span: Span, id: ast::Ident,
611 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
612 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
614 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
615 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
616 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
617 self.expr_call(sp, pathexpr, args)
619 fn expr_method_call(&self, span: Span,
622 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
623 let id = Spanned { node: ident, span: span };
624 args.insert(0, expr);
625 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
627 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
628 self.expr(b.span, ast::ExprBlock(b))
630 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
631 ast::Field { ident: respan(span, name), expr: e, span: span }
633 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
634 self.expr(span, ast::ExprStruct(path, fields, None))
636 fn expr_struct_ident(&self, span: Span,
637 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
638 self.expr_struct(span, self.path_ident(span, id), fields)
641 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> {
642 self.expr(sp, ast::ExprLit(P(respan(sp, lit))))
644 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
645 self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs(false))))
647 fn expr_int(&self, sp: Span, i: isize) -> P<ast::Expr> {
648 self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs(false),
651 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
652 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
654 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
655 self.expr_lit(sp, ast::LitBool(value))
658 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
659 self.expr(sp, ast::ExprVec(exprs))
661 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
662 self.expr_call_global(sp,
663 vec!(self.ident_of("std"),
664 self.ident_of("vec"),
665 self.ident_of("Vec"),
666 self.ident_of("new")),
669 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
670 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
672 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
673 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
676 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
677 self.expr(sp, ast::ExprCast(expr, ty))
681 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
683 self.ident_of("std"),
684 self.ident_of("option"),
685 self.ident_of("Option"),
686 self.ident_of("Some"));
687 self.expr_call_global(sp, some, vec!(expr))
690 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
691 let none = self.path_global(sp, vec!(
692 self.ident_of("std"),
693 self.ident_of("option"),
694 self.ident_of("Option"),
695 self.ident_of("None")));
700 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
701 self.expr(sp, ast::ExprBreak(None))
705 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
706 self.expr(sp, ast::ExprTup(exprs))
709 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
710 let loc = self.codemap().lookup_char_pos(span.lo);
711 let expr_file = self.expr_str(span,
712 token::intern_and_get_ident(&loc.file.name[]));
713 let expr_line = self.expr_usize(span, loc.line);
714 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
715 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
716 self.expr_call_global(
719 self.ident_of("std"),
721 self.ident_of("begin_unwind")),
723 self.expr_str(span, msg),
727 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
730 "internal error: entered unreachable code"))
733 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
735 self.ident_of("std"),
736 self.ident_of("result"),
737 self.ident_of("Result"),
738 self.ident_of("Ok"));
739 self.expr_call_global(sp, ok, vec!(expr))
742 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
744 self.ident_of("std"),
745 self.ident_of("result"),
746 self.ident_of("Result"),
747 self.ident_of("Err"));
748 self.expr_call_global(sp, err, vec!(expr))
751 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
752 let ok = self.ident_of("Ok");
753 let ok_path = self.path_ident(sp, ok);
754 let err = self.ident_of("Err");
755 let err_path = self.path_ident(sp, err);
757 let binding_variable = self.ident_of("__try_var");
758 let binding_pat = self.pat_ident(sp, binding_variable);
759 let binding_expr = self.expr_ident(sp, binding_variable);
761 // Ok(__try_var) pattern
762 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
764 // Err(__try_var) (pattern and expression resp.)
765 let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
766 let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr.clone()));
767 // return Err(__try_var)
768 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
770 // Ok(__try_var) => __try_var
771 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
772 // Err(__try_var) => return Err(__try_var)
773 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
775 // match head { Ok() => ..., Err() => ... }
776 self.expr_match(sp, head, vec!(ok_arm, err_arm))
780 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
781 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
783 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
784 self.pat(span, ast::PatWild(ast::PatWildSingle))
786 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
787 self.pat(span, ast::PatLit(expr))
789 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
790 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
793 fn pat_ident_binding_mode(&self,
796 bm: ast::BindingMode) -> P<ast::Pat> {
797 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
800 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
801 let pat = ast::PatEnum(path, Some(subpats));
804 fn pat_struct(&self, span: Span,
805 path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
806 let pat = ast::PatStruct(path, field_pats, false);
809 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
810 self.pat(span, ast::PatTup(pats))
813 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
815 self.ident_of("std"),
816 self.ident_of("option"),
817 self.ident_of("Option"),
818 self.ident_of("Some"));
819 let path = self.path_global(span, some);
820 self.pat_enum(span, path, vec!(pat))
823 fn pat_none(&self, span: Span) -> P<ast::Pat> {
825 self.ident_of("std"),
826 self.ident_of("option"),
827 self.ident_of("Option"),
828 self.ident_of("None"));
829 let path = self.path_global(span, some);
830 self.pat_enum(span, path, vec!())
833 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
835 self.ident_of("std"),
836 self.ident_of("result"),
837 self.ident_of("Result"),
838 self.ident_of("Ok"));
839 let path = self.path_global(span, some);
840 self.pat_enum(span, path, vec!(pat))
843 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
845 self.ident_of("std"),
846 self.ident_of("result"),
847 self.ident_of("Result"),
848 self.ident_of("Err"));
849 let path = self.path_global(span, some);
850 self.pat_enum(span, path, vec!(pat))
853 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
862 fn arm_unreachable(&self, span: Span) -> ast::Arm {
863 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
866 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
867 self.expr(span, ast::ExprMatch(arg, arms, ast::MatchSource::Normal))
870 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
871 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
872 let els = els.map(|x| self.expr_block(self.block_expr(x)));
873 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
876 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
877 self.expr(span, ast::ExprLoop(block, None))
880 fn lambda_fn_decl(&self, span: Span,
881 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
882 self.expr(span, ast::ExprClosure(ast::CaptureByRef, None, fn_decl, blk))
884 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
885 let fn_decl = self.fn_decl(
886 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
887 self.ty_infer(span));
889 self.expr(span, ast::ExprClosure(ast::CaptureByRef, None, fn_decl, blk))
891 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
892 self.lambda(span, Vec::new(), blk)
895 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
896 self.lambda(span, vec!(ident), blk)
899 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
900 expr: P<ast::Expr>) -> P<ast::Expr> {
901 self.lambda(span, ids, self.block_expr(expr))
903 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
904 self.lambda0(span, self.block_expr(expr))
906 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
907 self.lambda1(span, self.block_expr(expr), ident)
910 fn lambda_stmts(&self,
912 ids: Vec<ast::Ident>,
913 stmts: Vec<P<ast::Stmt>>)
915 self.lambda(span, ids, self.block(span, stmts, None))
917 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> {
918 self.lambda0(span, self.block(span, stmts, None))
920 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
921 ident: ast::Ident) -> P<ast::Expr> {
922 self.lambda1(span, self.block(span, stmts, None), ident)
925 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
926 let arg_pat = self.pat_ident(span, ident);
930 id: ast::DUMMY_NODE_ID
935 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
938 output: ast::Return(output),
943 fn item(&self, span: Span, name: Ident,
944 attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> {
945 // FIXME: Would be nice if our generated code didn't violate
946 // Rust coding conventions
950 id: ast::DUMMY_NODE_ID,
957 fn item_fn_poly(&self,
960 inputs: Vec<ast::Arg> ,
963 body: P<ast::Block>) -> P<ast::Item> {
967 ast::ItemFn(self.fn_decl(inputs, output),
968 ast::Unsafety::Normal,
977 inputs: Vec<ast::Arg> ,
986 ast_util::empty_generics(),
990 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
991 let args = tys.into_iter().map(|ty| {
992 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
999 kind: ast::TupleVariantKind(args),
1000 id: ast::DUMMY_NODE_ID,
1006 fn item_enum_poly(&self, span: Span, name: Ident,
1007 enum_definition: ast::EnumDef,
1008 generics: Generics) -> P<ast::Item> {
1009 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1012 fn item_enum(&self, span: Span, name: Ident,
1013 enum_definition: ast::EnumDef) -> P<ast::Item> {
1014 self.item_enum_poly(span, name, enum_definition,
1015 ast_util::empty_generics())
1018 fn item_struct(&self, span: Span, name: Ident,
1019 struct_def: ast::StructDef) -> P<ast::Item> {
1020 self.item_struct_poly(
1024 ast_util::empty_generics()
1028 fn item_struct_poly(&self, span: Span, name: Ident,
1029 struct_def: ast::StructDef, generics: Generics) -> P<ast::Item> {
1030 self.item(span, name, Vec::new(), ast::ItemStruct(P(struct_def), generics))
1033 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1034 attrs: Vec<ast::Attribute> ,
1035 vi: Vec<ast::ViewItem> ,
1036 items: Vec<P<ast::Item>> ) -> P<ast::Item> {
1041 ast::ItemMod(ast::Mod {
1049 fn item_static(&self,
1053 mutbl: ast::Mutability,
1056 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1059 fn item_const(&self,
1065 self.item(span, name, Vec::new(), ast::ItemConst(ty, expr))
1068 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1069 generics: Generics) -> P<ast::Item> {
1070 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1073 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1074 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1077 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1078 respan(sp, ast::Attribute_ {
1079 id: attr::mk_attr_id(),
1080 style: ast::AttrOuter,
1082 is_sugared_doc: false,
1086 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1087 P(respan(sp, ast::MetaWord(w)))
1091 name: InternedString,
1092 mis: Vec<P<ast::MetaItem>> )
1093 -> P<ast::MetaItem> {
1094 P(respan(sp, ast::MetaList(name, mis)))
1096 fn meta_name_value(&self,
1098 name: InternedString,
1100 -> P<ast::MetaItem> {
1101 P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
1104 fn view_use(&self, sp: Span,
1105 vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem {
1107 node: ast::ViewItemUse(vp),
1114 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1115 let last = path.segments.last().unwrap().identifier;
1116 self.view_use_simple_(sp, vis, last, path)
1119 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1120 ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1121 self.view_use(sp, vis,
1123 ast::ViewPathSimple(ident,
1125 ast::DUMMY_NODE_ID))))
1128 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1129 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1130 let imports = imports.iter().map(|id| {
1131 respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1134 self.view_use(sp, vis,
1136 ast::ViewPathList(self.path(sp, path),
1138 ast::DUMMY_NODE_ID))))
1141 fn view_use_glob(&self, sp: Span,
1142 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1143 self.view_use(sp, vis,
1145 ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID))))