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>> )
44 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
46 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty>;
47 fn ty_path(&self, ast::Path, Option<OwnedSlice<ast::TyParamBound>>) -> P<ast::Ty>;
48 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
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>;
57 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty>;
59 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
60 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
61 fn ty_nil(&self) -> 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 unbound: Option<ast::TyParamBound>,
72 default: Option<P<ast::Ty>>) -> ast::TyParam;
74 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
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_managed(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
116 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
117 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
118 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
119 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
120 idx: uint) -> P<ast::Expr>;
121 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
122 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
123 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
124 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
125 fn expr_method_call(&self, span: Span,
126 expr: P<ast::Expr>, ident: ast::Ident,
127 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
128 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
129 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
131 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
132 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
133 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
134 fields: Vec<ast::Field>) -> P<ast::Expr>;
136 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>;
138 fn expr_uint(&self, span: Span, i: uint) -> P<ast::Expr>;
139 fn expr_int(&self, sp: Span, i: int) -> P<ast::Expr>;
140 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
141 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
143 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
144 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
145 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
146 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
148 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
149 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
151 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
153 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
154 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
156 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
157 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
158 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
160 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
161 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
162 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
163 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
165 fn pat_ident_binding_mode(&self,
168 bm: ast::BindingMode) -> P<ast::Pat>;
169 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
170 fn pat_struct(&self, span: Span,
171 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> P<ast::Pat>;
172 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
174 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
175 fn pat_none(&self, span: Span) -> P<ast::Pat>;
177 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
178 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
180 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
181 fn arm_unreachable(&self, span: Span) -> ast::Arm;
183 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
184 fn expr_if(&self, span: Span,
185 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
186 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
188 fn lambda_fn_decl(&self, span: Span,
189 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
191 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
192 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
193 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
195 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
196 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
197 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
199 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
200 blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
201 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
202 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
203 ident: ast::Ident) -> P<ast::Expr>;
206 fn item(&self, span: Span,
207 name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>;
209 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
211 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
213 fn item_fn_poly(&self,
216 inputs: Vec<ast::Arg> ,
219 body: P<ast::Block>) -> P<ast::Item>;
223 inputs: Vec<ast::Arg> ,
225 body: P<ast::Block>) -> P<ast::Item>;
227 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
228 fn item_enum_poly(&self,
231 enum_definition: ast::EnumDef,
232 generics: Generics) -> P<ast::Item>;
233 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
235 fn item_struct_poly(&self,
238 struct_def: ast::StructDef,
239 generics: Generics) -> P<ast::Item>;
240 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> P<ast::Item>;
242 fn item_mod(&self, span: Span, inner_span: Span,
243 name: Ident, attrs: Vec<ast::Attribute>,
244 vi: Vec<ast::ViewItem> , items: Vec<P<ast::Item>> ) -> P<ast::Item>;
246 fn item_static(&self,
250 mutbl: ast::Mutability,
254 fn item_ty_poly(&self,
258 generics: Generics) -> P<ast::Item>;
259 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
261 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
263 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
266 name: InternedString,
267 mis: Vec<P<ast::MetaItem>> )
269 fn meta_name_value(&self,
271 name: InternedString,
275 fn view_use(&self, sp: Span,
276 vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem;
277 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
278 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
279 ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
280 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
281 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
282 fn view_use_glob(&self, sp: Span,
283 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
286 impl<'a> AstBuilder for ExtCtxt<'a> {
287 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
288 self.path_all(span, false, strs, Vec::new(), Vec::new())
290 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
291 self.path(span, vec!(id))
293 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
294 self.path_all(span, true, strs, Vec::new(), Vec::new())
299 mut idents: Vec<ast::Ident> ,
300 lifetimes: Vec<ast::Lifetime>,
301 types: Vec<P<ast::Ty>> )
303 let last_identifier = idents.pop().unwrap();
304 let mut segments: Vec<ast::PathSegment> = idents.into_iter()
308 lifetimes: Vec::new(),
309 types: OwnedSlice::empty(),
312 segments.push(ast::PathSegment {
313 identifier: last_identifier,
314 lifetimes: lifetimes,
315 types: OwnedSlice::from_vec(types),
324 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
331 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
333 id: ast::DUMMY_NODE_ID,
339 fn ty_path(&self, path: ast::Path, bounds: Option<OwnedSlice<ast::TyParamBound>>)
342 ast::TyPath(path, bounds, ast::DUMMY_NODE_ID))
345 // Might need to take bounds as an argument in the future, if you ever want
346 // to generate a bounded existential trait type.
347 fn ty_ident(&self, span: Span, ident: ast::Ident)
349 self.ty_path(self.path_ident(span, ident), None)
355 lifetime: Option<ast::Lifetime>,
356 mutbl: ast::Mutability)
359 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
365 mutbl: ast::Mutability)
368 ast::TyPtr(self.ty_mt(ty, mutbl)))
370 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty> {
371 self.ty(span, ast::TyUniq(ty))
374 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
376 self.path_all(DUMMY_SP,
379 self.ident_of("std"),
380 self.ident_of("option"),
381 self.ident_of("Option")
387 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
390 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
395 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
396 self.ty(span, ast::TyInfer)
399 fn ty_nil(&self) -> P<ast::Ty> {
401 id: ast::DUMMY_NODE_ID,
410 bounds: OwnedSlice<ast::TyParamBound>,
411 unbound: Option<ast::TyParamBound>,
412 default: Option<P<ast::Ty>>) -> ast::TyParam {
415 id: ast::DUMMY_NODE_ID,
423 // these are strange, and probably shouldn't be used outside of
424 // pipes. Specifically, the global version possible generates
426 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
427 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
430 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
431 ty_params.iter().map(|p| self.ty_path(
432 self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect()
435 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
438 ref_id: ast::DUMMY_NODE_ID
442 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
443 ast::TraitTyParamBound(self.trait_ref(path))
446 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
447 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
450 fn lifetime_def(&self,
453 bounds: Vec<ast::Lifetime>)
454 -> ast::LifetimeDef {
456 lifetime: self.lifetime(span, name),
461 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> {
462 P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)))
465 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
466 ex: P<ast::Expr>) -> P<ast::Stmt> {
468 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
470 self.pat_ident(sp, ident)
472 let local = P(ast::Local {
473 ty: self.ty_infer(sp),
476 id: ast::DUMMY_NODE_ID,
478 source: ast::LocalLet,
480 let decl = respan(sp, ast::DeclLocal(local));
481 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
484 fn stmt_let_typed(&self,
492 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
494 self.pat_ident(sp, ident)
496 let local = P(ast::Local {
500 id: ast::DUMMY_NODE_ID,
502 source: ast::LocalLet,
504 let decl = respan(sp, ast::DeclLocal(local));
505 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
508 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
509 expr: Option<P<Expr>>) -> P<ast::Block> {
510 self.block_all(span, Vec::new(), stmts, expr)
513 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
514 let decl = respan(sp, ast::DeclItem(item));
515 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
518 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
519 self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
523 view_items: Vec<ast::ViewItem>,
524 stmts: Vec<P<ast::Stmt>>,
525 expr: Option<P<ast::Expr>>) -> P<ast::Block> {
527 view_items: view_items,
530 id: ast::DUMMY_NODE_ID,
531 rules: ast::DefaultBlock,
536 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> {
538 id: ast::DUMMY_NODE_ID,
544 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
545 self.expr(path.span, ast::ExprPath(path))
548 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
549 self.expr_path(self.path_ident(span, id))
551 fn expr_self(&self, span: Span) -> P<ast::Expr> {
552 self.expr_ident(span, special_idents::self_)
555 fn expr_binary(&self, sp: Span, op: ast::BinOp,
556 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
557 self.expr(sp, ast::ExprBinary(op, lhs, rhs))
560 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
561 self.expr_unary(sp, ast::UnDeref, e)
563 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
564 self.expr(sp, ast::ExprUnary(op, e))
567 fn expr_managed(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
568 self.expr_unary(sp, ast::UnBox, e)
571 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
572 let field_name = token::get_ident(ident);
573 let field_span = Span {
574 lo: sp.lo - Pos::from_uint(field_name.get().len()),
579 let id = Spanned { node: ident, span: field_span };
580 self.expr(sp, ast::ExprField(expr, id, Vec::new()))
582 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: uint) -> P<ast::Expr> {
583 let field_span = Span {
584 lo: sp.lo - Pos::from_uint(idx.to_string().len()),
589 let id = Spanned { node: idx, span: field_span };
590 self.expr(sp, ast::ExprTupField(expr, id, Vec::new()))
592 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
593 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
595 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
596 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
599 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
600 self.expr(span, ast::ExprCall(expr, args))
602 fn expr_call_ident(&self, span: Span, id: ast::Ident,
603 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
604 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
606 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
607 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
608 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
609 self.expr_call(sp, pathexpr, args)
611 fn expr_method_call(&self, span: Span,
614 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
615 let id = Spanned { node: ident, span: span };
617 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
619 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
620 self.expr(b.span, ast::ExprBlock(b))
622 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
623 ast::Field { ident: respan(span, name), expr: e, span: span }
625 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
626 self.expr(span, ast::ExprStruct(path, fields, None))
628 fn expr_struct_ident(&self, span: Span,
629 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
630 self.expr_struct(span, self.path_ident(span, id), fields)
633 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> {
634 self.expr(sp, ast::ExprLit(P(respan(sp, lit))))
636 fn expr_uint(&self, span: Span, i: uint) -> P<ast::Expr> {
637 self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyU)))
639 fn expr_int(&self, sp: Span, i: int) -> P<ast::Expr> {
640 self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyI, ast::Sign::new(i))))
642 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
643 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
645 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
646 self.expr_lit(sp, ast::LitBool(value))
649 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
650 self.expr(sp, ast::ExprVec(exprs))
652 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
653 self.expr_call_global(sp,
654 vec!(self.ident_of("std"),
655 self.ident_of("vec"),
656 self.ident_of("Vec"),
657 self.ident_of("new")),
660 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
661 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
663 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
664 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
667 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
668 self.expr(sp, ast::ExprCast(expr, ty))
672 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
674 self.ident_of("std"),
675 self.ident_of("option"),
676 self.ident_of("Some"));
677 self.expr_call_global(sp, some, vec!(expr))
680 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
681 let none = self.path_global(sp, vec!(
682 self.ident_of("std"),
683 self.ident_of("option"),
684 self.ident_of("None")));
688 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
689 self.expr(sp, ast::ExprTup(exprs))
692 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
693 let loc = self.codemap().lookup_char_pos(span.lo);
694 let expr_file = self.expr_str(span,
695 token::intern_and_get_ident(loc.file
698 let expr_line = self.expr_uint(span, loc.line);
699 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
700 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
701 self.expr_call_global(
704 self.ident_of("std"),
706 self.ident_of("begin_unwind")),
708 self.expr_str(span, msg),
712 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
715 "internal error: entered unreachable code"))
718 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
720 self.ident_of("std"),
721 self.ident_of("result"),
722 self.ident_of("Ok"));
723 self.expr_call_global(sp, ok, vec!(expr))
726 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
728 self.ident_of("std"),
729 self.ident_of("result"),
730 self.ident_of("Err"));
731 self.expr_call_global(sp, err, vec!(expr))
734 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
735 let ok = self.ident_of("Ok");
736 let ok_path = self.path_ident(sp, ok);
737 let err = self.ident_of("Err");
738 let err_path = self.path_ident(sp, err);
740 let binding_variable = self.ident_of("__try_var");
741 let binding_pat = self.pat_ident(sp, binding_variable);
742 let binding_expr = self.expr_ident(sp, binding_variable);
744 // Ok(__try_var) pattern
745 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
747 // Err(__try_var) (pattern and expression resp.)
748 let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
749 let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr.clone()));
750 // return Err(__try_var)
751 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
753 // Ok(__try_var) => __try_var
754 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
755 // Err(__try_var) => return Err(__try_var)
756 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
758 // match head { Ok() => ..., Err() => ... }
759 self.expr_match(sp, head, vec!(ok_arm, err_arm))
763 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
764 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
766 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
767 self.pat(span, ast::PatWild(ast::PatWildSingle))
769 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
770 self.pat(span, ast::PatLit(expr))
772 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
773 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
776 fn pat_ident_binding_mode(&self,
779 bm: ast::BindingMode) -> P<ast::Pat> {
780 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
783 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
784 let pat = ast::PatEnum(path, Some(subpats));
787 fn pat_struct(&self, span: Span,
788 path: ast::Path, field_pats: Vec<ast::FieldPat>) -> P<ast::Pat> {
789 let pat = ast::PatStruct(path, field_pats, false);
792 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
793 let pat = ast::PatTup(pats);
797 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
799 self.ident_of("std"),
800 self.ident_of("option"),
801 self.ident_of("Some"));
802 let path = self.path_global(span, some);
803 self.pat_enum(span, path, vec!(pat))
806 fn pat_none(&self, span: Span) -> P<ast::Pat> {
808 self.ident_of("std"),
809 self.ident_of("option"),
810 self.ident_of("None"));
811 let path = self.path_global(span, some);
812 self.pat_enum(span, path, vec!())
815 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
817 self.ident_of("std"),
818 self.ident_of("result"),
819 self.ident_of("Ok"));
820 let path = self.path_global(span, some);
821 self.pat_enum(span, path, vec!(pat))
824 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
826 self.ident_of("std"),
827 self.ident_of("result"),
828 self.ident_of("Err"));
829 let path = self.path_global(span, some);
830 self.pat_enum(span, path, vec!(pat))
833 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
842 fn arm_unreachable(&self, span: Span) -> ast::Arm {
843 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
846 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
847 self.expr(span, ast::ExprMatch(arg, arms))
850 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
851 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
852 let els = els.map(|x| self.expr_block(self.block_expr(x)));
853 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
856 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
857 self.expr(span, ast::ExprLoop(block, None))
860 fn lambda_fn_decl(&self, span: Span,
861 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
862 self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
864 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
865 let fn_decl = self.fn_decl(
866 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
867 self.ty_infer(span));
869 self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
871 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
872 self.lambda(span, Vec::new(), blk)
875 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
876 self.lambda(span, vec!(ident), blk)
879 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
880 expr: P<ast::Expr>) -> P<ast::Expr> {
881 self.lambda(span, ids, self.block_expr(expr))
883 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
884 self.lambda0(span, self.block_expr(expr))
886 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
887 self.lambda1(span, self.block_expr(expr), ident)
890 fn lambda_stmts(&self,
892 ids: Vec<ast::Ident>,
893 stmts: Vec<P<ast::Stmt>>)
895 self.lambda(span, ids, self.block(span, stmts, None))
897 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> {
898 self.lambda0(span, self.block(span, stmts, None))
900 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
901 ident: ast::Ident) -> P<ast::Expr> {
902 self.lambda1(span, self.block(span, stmts, None), ident)
905 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
906 let arg_pat = self.pat_ident(span, ident);
910 id: ast::DUMMY_NODE_ID
915 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl> {
924 fn item(&self, span: Span, name: Ident,
925 attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> {
926 // FIXME: Would be nice if our generated code didn't violate
927 // Rust coding conventions
931 id: ast::DUMMY_NODE_ID,
938 fn item_fn_poly(&self,
941 inputs: Vec<ast::Arg> ,
944 body: P<ast::Block>) -> P<ast::Item> {
948 ast::ItemFn(self.fn_decl(inputs, output),
958 inputs: Vec<ast::Arg> ,
967 ast_util::empty_generics(),
971 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
972 let args = tys.into_iter().map(|ty| {
973 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
980 kind: ast::TupleVariantKind(args),
981 id: ast::DUMMY_NODE_ID,
987 fn item_enum_poly(&self, span: Span, name: Ident,
988 enum_definition: ast::EnumDef,
989 generics: Generics) -> P<ast::Item> {
990 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
993 fn item_enum(&self, span: Span, name: Ident,
994 enum_definition: ast::EnumDef) -> P<ast::Item> {
995 self.item_enum_poly(span, name, enum_definition,
996 ast_util::empty_generics())
999 fn item_struct(&self, span: Span, name: Ident,
1000 struct_def: ast::StructDef) -> P<ast::Item> {
1001 self.item_struct_poly(
1005 ast_util::empty_generics()
1009 fn item_struct_poly(&self, span: Span, name: Ident,
1010 struct_def: ast::StructDef, generics: Generics) -> P<ast::Item> {
1011 self.item(span, name, Vec::new(), ast::ItemStruct(P(struct_def), generics))
1014 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1015 attrs: Vec<ast::Attribute> ,
1016 vi: Vec<ast::ViewItem> ,
1017 items: Vec<P<ast::Item>> ) -> P<ast::Item> {
1022 ast::ItemMod(ast::Mod {
1030 fn item_static(&self,
1034 mutbl: ast::Mutability,
1037 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1040 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1041 generics: Generics) -> P<ast::Item> {
1042 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1045 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1046 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1049 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1050 respan(sp, ast::Attribute_ {
1051 id: attr::mk_attr_id(),
1052 style: ast::AttrOuter,
1054 is_sugared_doc: false,
1058 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1059 P(respan(sp, ast::MetaWord(w)))
1063 name: InternedString,
1064 mis: Vec<P<ast::MetaItem>> )
1065 -> P<ast::MetaItem> {
1066 P(respan(sp, ast::MetaList(name, mis)))
1068 fn meta_name_value(&self,
1070 name: InternedString,
1072 -> P<ast::MetaItem> {
1073 P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
1076 fn view_use(&self, sp: Span,
1077 vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem {
1079 node: ast::ViewItemUse(vp),
1086 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1087 let last = path.segments.last().unwrap().identifier;
1088 self.view_use_simple_(sp, vis, last, path)
1091 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1092 ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1093 self.view_use(sp, vis,
1095 ast::ViewPathSimple(ident,
1097 ast::DUMMY_NODE_ID))))
1100 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1101 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1102 let imports = imports.iter().map(|id| {
1103 respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1106 self.view_use(sp, vis,
1108 ast::ViewPathList(self.path(sp, path),
1110 ast::DUMMY_NODE_ID))))
1113 fn view_use_glob(&self, sp: Span,
1114 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1115 self.view_use(sp, vis,
1117 ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID))))