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::{P, Ident, Generics, NodeId, Expr};
16 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
17 use ext::base::ExtCtxt;
19 use owned_slice::OwnedSlice;
20 use parse::token::special_idents;
21 use parse::token::InternedString;
24 use std::gc::{Gc, GC};
26 // Transitional reexports so qquote can find the paths it is looking for
32 pub trait AstBuilder {
34 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
35 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
36 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
37 fn path_all(&self, sp: Span,
39 idents: Vec<ast::Ident> ,
40 lifetimes: Vec<ast::Lifetime>,
41 types: Vec<P<ast::Ty>> )
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, Option<OwnedSlice<ast::TyParamBound>>) -> P<ast::Ty>;
49 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
51 fn ty_rptr(&self, span: Span,
53 lifetime: Option<ast::Lifetime>,
54 mutbl: ast::Mutability) -> P<ast::Ty>;
55 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty>;
57 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
58 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
59 fn ty_nil(&self) -> P<ast::Ty>;
61 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
62 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
63 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField;
64 fn strip_bounds(&self, bounds: &Generics) -> Generics;
69 bounds: OwnedSlice<ast::TyParamBound>,
70 unbound: Option<ast::TyParamBound>,
71 default: Option<P<ast::Ty>>) -> ast::TyParam;
73 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
74 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
75 fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
78 fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt>;
79 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
80 ex: Gc<ast::Expr>) -> Gc<ast::Stmt>;
81 fn stmt_let_typed(&self,
88 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt>;
91 fn block(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
92 expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
93 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block>;
94 fn block_all(&self, span: Span,
95 view_items: Vec<ast::ViewItem> ,
96 stmts: Vec<Gc<ast::Stmt>> ,
97 expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
100 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr>;
101 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr>;
102 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr>;
104 fn expr_self(&self, span: Span) -> Gc<ast::Expr>;
105 fn expr_binary(&self, sp: Span, op: ast::BinOp,
106 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr>;
107 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
108 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
110 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
111 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
112 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
113 fn expr_field_access(&self, span: Span, expr: Gc<ast::Expr>,
114 ident: ast::Ident) -> Gc<ast::Expr>;
115 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
116 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
117 fn expr_call_ident(&self, span: Span, id: ast::Ident,
118 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
119 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
120 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
121 fn expr_method_call(&self, span: Span,
122 expr: Gc<ast::Expr>, ident: ast::Ident,
123 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
124 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr>;
125 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>,
126 ty: P<ast::Ty>) -> Gc<ast::Expr>;
128 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field;
129 fn expr_struct(&self, span: Span, path: ast::Path,
130 fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
131 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
132 fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
134 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr>;
136 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr>;
137 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr>;
138 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr>;
139 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr>;
141 fn expr_vstore(&self, sp: Span, expr: Gc<ast::Expr>, vst: ast::ExprVstore) -> Gc<ast::Expr>;
142 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
143 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr>;
144 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
145 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr>;
146 fn expr_str_uniq(&self, sp: Span, s: InternedString) -> Gc<ast::Expr>;
148 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
149 fn expr_none(&self, sp: Span) -> Gc<ast::Expr>;
151 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
153 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr>;
154 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr>;
156 fn expr_ok(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
157 fn expr_err(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
158 fn expr_try(&self, span: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr>;
160 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat>;
161 fn pat_wild(&self, span: Span) -> Gc<ast::Pat>;
162 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat>;
163 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat>;
165 fn pat_ident_binding_mode(&self,
168 bm: ast::BindingMode) -> Gc<ast::Pat>;
169 fn pat_enum(&self, span: Span, path: ast::Path,
170 subpats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
171 fn pat_struct(&self, span: Span,
172 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat>;
174 fn arm(&self, span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm;
175 fn arm_unreachable(&self, span: Span) -> ast::Arm;
177 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, arms: Vec<ast::Arm> ) -> Gc<ast::Expr>;
178 fn expr_if(&self, span: Span,
179 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
180 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr>;
182 fn lambda_fn_decl(&self, span: Span,
183 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr>;
185 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr>;
186 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr>;
187 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr>;
189 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: Gc<ast::Expr>) -> Gc<ast::Expr>;
190 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
191 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr>;
193 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
194 blk: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
195 fn lambda_stmts_0(&self, span: Span,
196 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
197 fn lambda_stmts_1(&self, span: Span,
198 stmts: Vec<Gc<ast::Stmt>>, ident: ast::Ident) -> Gc<ast::Expr>;
201 fn item(&self, span: Span,
202 name: Ident, attrs: Vec<ast::Attribute>,
203 node: ast::Item_) -> Gc<ast::Item>;
205 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
207 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
209 fn item_fn_poly(&self,
212 inputs: Vec<ast::Arg> ,
215 body: P<ast::Block>) -> Gc<ast::Item>;
219 inputs: Vec<ast::Arg> ,
221 body: P<ast::Block>) -> Gc<ast::Item>;
223 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
224 fn item_enum_poly(&self,
227 enum_definition: ast::EnumDef,
228 generics: Generics) -> Gc<ast::Item>;
229 fn item_enum(&self, span: Span, name: Ident,
230 enum_def: ast::EnumDef) -> Gc<ast::Item>;
232 fn item_struct_poly(&self,
235 struct_def: ast::StructDef,
236 generics: Generics) -> Gc<ast::Item>;
237 fn item_struct(&self, span: Span, name: Ident,
238 struct_def: ast::StructDef) -> Gc<ast::Item>;
240 fn item_mod(&self, span: Span, inner_span: Span,
241 name: Ident, attrs: Vec<ast::Attribute>,
242 vi: Vec<ast::ViewItem>,
243 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item>;
245 fn item_static(&self,
249 mutbl: ast::Mutability,
253 fn item_ty_poly(&self,
257 generics: Generics) -> Gc<ast::Item>;
258 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item>;
260 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute;
262 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem>;
265 name: InternedString,
266 mis: Vec<Gc<ast::MetaItem>>)
267 -> Gc<ast::MetaItem>;
268 fn meta_name_value(&self,
270 name: InternedString,
272 -> Gc<ast::MetaItem>;
274 fn view_use(&self, sp: Span,
275 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem;
276 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
277 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
278 ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
279 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
280 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
281 fn view_use_glob(&self, sp: Span,
282 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
285 impl<'a> AstBuilder for ExtCtxt<'a> {
286 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
287 self.path_all(span, false, strs, Vec::new(), Vec::new())
289 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
290 self.path(span, vec!(id))
292 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
293 self.path_all(span, true, strs, Vec::new(), Vec::new())
298 mut idents: Vec<ast::Ident> ,
299 lifetimes: Vec<ast::Lifetime>,
300 types: Vec<P<ast::Ty>> )
302 let last_identifier = idents.pop().unwrap();
303 let mut segments: Vec<ast::PathSegment> = idents.move_iter()
307 lifetimes: Vec::new(),
308 types: OwnedSlice::empty(),
311 segments.push(ast::PathSegment {
312 identifier: last_identifier,
313 lifetimes: lifetimes,
314 types: OwnedSlice::from_vec(types),
323 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
330 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
332 id: ast::DUMMY_NODE_ID,
338 fn ty_path(&self, path: ast::Path, bounds: Option<OwnedSlice<ast::TyParamBound>>)
341 ast::TyPath(path, bounds, ast::DUMMY_NODE_ID))
344 // Might need to take bounds as an argument in the future, if you ever want
345 // to generate a bounded existential trait type.
346 fn ty_ident(&self, span: Span, ident: ast::Ident)
348 self.ty_path(self.path_ident(span, ident), None)
354 lifetime: Option<ast::Lifetime>,
355 mutbl: ast::Mutability)
358 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
361 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty> {
362 self.ty(span, ast::TyUniq(ty))
365 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
367 self.path_all(DUMMY_SP,
370 self.ident_of("std"),
371 self.ident_of("option"),
372 self.ident_of("Option")
378 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
381 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
386 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
387 self.ty(span, ast::TyInfer)
390 fn ty_nil(&self) -> P<ast::Ty> {
392 id: ast::DUMMY_NODE_ID,
401 bounds: OwnedSlice<ast::TyParamBound>,
402 unbound: Option<ast::TyParamBound>,
403 default: Option<P<ast::Ty>>) -> ast::TyParam {
406 id: ast::DUMMY_NODE_ID,
414 // these are strange, and probably shouldn't be used outside of
415 // pipes. Specifically, the global version possible generates
417 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
418 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
421 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
422 ty_params.iter().map(|p| self.ty_path(
423 self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect()
426 fn strip_bounds(&self, generics: &Generics) -> Generics {
427 let new_params = generics.ty_params.map(|ty_param| {
428 ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
431 ty_params: new_params,
432 .. (*generics).clone()
436 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
439 ref_id: ast::DUMMY_NODE_ID
443 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
444 ast::TraitTyParamBound(self.trait_ref(path))
447 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
448 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
451 fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt> {
452 box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
455 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
456 ex: Gc<ast::Expr>) -> Gc<ast::Stmt> {
458 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
460 self.pat_ident(sp, ident)
462 let local = box(GC) ast::Local {
463 ty: self.ty_infer(sp),
466 id: ast::DUMMY_NODE_ID,
468 source: ast::LocalLet,
470 let decl = respan(sp, ast::DeclLocal(local));
471 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
474 fn stmt_let_typed(&self,
482 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
484 self.pat_ident(sp, ident)
486 let local = box(GC) ast::Local {
490 id: ast::DUMMY_NODE_ID,
492 source: ast::LocalLet,
494 let decl = respan(sp, ast::DeclLocal(local));
495 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
500 stmts: Vec<Gc<ast::Stmt>>,
501 expr: Option<Gc<Expr>>)
503 self.block_all(span, Vec::new(), stmts, expr)
506 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt> {
507 let decl = respan(sp, ast::DeclItem(item));
508 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
511 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block> {
512 self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
516 view_items: Vec<ast::ViewItem> ,
517 stmts: Vec<Gc<ast::Stmt>>,
518 expr: Option<Gc<ast::Expr>>) -> P<ast::Block> {
520 view_items: view_items,
523 id: ast::DUMMY_NODE_ID,
524 rules: ast::DefaultBlock,
529 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr> {
531 id: ast::DUMMY_NODE_ID,
537 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr> {
538 self.expr(path.span, ast::ExprPath(path))
541 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr> {
542 self.expr_path(self.path_ident(span, id))
544 fn expr_self(&self, span: Span) -> Gc<ast::Expr> {
545 self.expr_ident(span, special_idents::self_)
548 fn expr_binary(&self, sp: Span, op: ast::BinOp,
549 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr> {
550 self.expr(sp, ast::ExprBinary(op, lhs, rhs))
553 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
554 self.expr_unary(sp, ast::UnDeref, e)
556 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
557 self.expr(sp, ast::ExprUnary(op, e))
560 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
561 self.expr_unary(sp, ast::UnBox, e)
564 fn expr_field_access(&self, sp: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
565 let field_name = token::get_ident(ident);
566 let field_span = Span {
567 lo: sp.lo - Pos::from_uint(field_name.get().len()),
569 expn_info: sp.expn_info,
572 let id = Spanned { node: ident, span: field_span };
573 self.expr(sp, ast::ExprField(expr, id, Vec::new()))
575 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
576 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
578 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
579 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
582 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
583 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
584 self.expr(span, ast::ExprCall(expr, args))
586 fn expr_call_ident(&self, span: Span, id: ast::Ident,
587 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
588 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
590 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
591 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
592 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
593 self.expr_call(sp, pathexpr, args)
595 fn expr_method_call(&self, span: Span,
598 mut args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
599 let id = Spanned { node: ident, span: span };
601 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
603 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr> {
604 self.expr(b.span, ast::ExprBlock(b))
606 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field {
607 ast::Field { ident: respan(span, name), expr: e, span: span }
609 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
610 self.expr(span, ast::ExprStruct(path, fields, None))
612 fn expr_struct_ident(&self, span: Span,
613 id: ast::Ident, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
614 self.expr_struct(span, self.path_ident(span, id), fields)
617 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr> {
618 self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit)))
620 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> {
621 self.expr_lit(span, ast::LitUint(i as u64, ast::TyU))
623 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> {
624 self.expr_lit(sp, ast::LitInt(i as i64, ast::TyI))
626 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> {
627 self.expr_lit(sp, ast::LitUint(u as u64, ast::TyU8))
629 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> {
630 self.expr_lit(sp, ast::LitBool(value))
633 fn expr_vstore(&self, sp: Span, expr: Gc<ast::Expr>, vst: ast::ExprVstore) -> Gc<ast::Expr> {
634 self.expr(sp, ast::ExprVstore(expr, vst))
636 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
637 self.expr(sp, ast::ExprVec(exprs))
639 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr> {
640 self.expr_call_global(sp,
641 vec!(self.ident_of("std"),
642 self.ident_of("vec"),
643 self.ident_of("Vec"),
644 self.ident_of("new")),
647 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
648 self.expr_vstore(sp, self.expr_vec(sp, exprs), ast::ExprVstoreSlice)
650 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
651 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
653 fn expr_str_uniq(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
654 self.expr_vstore(sp, self.expr_str(sp, s), ast::ExprVstoreUniq)
658 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, ty: P<ast::Ty>) -> Gc<ast::Expr> {
659 self.expr(sp, ast::ExprCast(expr, ty))
663 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
665 self.ident_of("std"),
666 self.ident_of("option"),
667 self.ident_of("Some"));
668 self.expr_call_global(sp, some, vec!(expr))
671 fn expr_none(&self, sp: Span) -> Gc<ast::Expr> {
672 let none = self.path_global(sp, vec!(
673 self.ident_of("std"),
674 self.ident_of("option"),
675 self.ident_of("None")));
679 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
680 self.expr(sp, ast::ExprTup(exprs))
683 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr> {
684 let loc = self.codemap().lookup_char_pos(span.lo);
685 self.expr_call_global(
688 self.ident_of("std"),
690 self.ident_of("begin_unwind")),
692 self.expr_str(span, msg),
694 token::intern_and_get_ident(loc.file
697 self.expr_uint(span, loc.line)))
700 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr> {
703 "internal error: entered unreachable code"))
706 fn expr_ok(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
708 self.ident_of("std"),
709 self.ident_of("result"),
710 self.ident_of("Ok"));
711 self.expr_call_global(sp, ok, vec!(expr))
714 fn expr_err(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
716 self.ident_of("std"),
717 self.ident_of("result"),
718 self.ident_of("Err"));
719 self.expr_call_global(sp, err, vec!(expr))
722 fn expr_try(&self, sp: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr> {
723 let ok = self.ident_of("Ok");
724 let ok_path = self.path_ident(sp, ok);
725 let err = self.ident_of("Err");
726 let err_path = self.path_ident(sp, err);
728 let binding_variable = self.ident_of("__try_var");
729 let binding_pat = self.pat_ident(sp, binding_variable);
730 let binding_expr = self.expr_ident(sp, binding_variable);
732 // Ok(__try_var) pattern
733 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat));
735 // Err(__try_var) (pattern and expression resp.)
736 let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
737 let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr));
738 // return Err(__try_var)
739 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
741 // Ok(__try_var) => __try_var
742 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
743 // Err(__try_var) => return Err(__try_var)
744 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
746 // match head { Ok() => ..., Err() => ... }
747 self.expr_match(sp, head, vec!(ok_arm, err_arm))
751 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat> {
752 box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }
754 fn pat_wild(&self, span: Span) -> Gc<ast::Pat> {
755 self.pat(span, ast::PatWild)
757 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat> {
758 self.pat(span, ast::PatLit(expr))
760 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat> {
761 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
764 fn pat_ident_binding_mode(&self,
767 bm: ast::BindingMode) -> Gc<ast::Pat> {
768 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
771 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<Gc<ast::Pat>> ) -> Gc<ast::Pat> {
772 let pat = ast::PatEnum(path, Some(subpats));
775 fn pat_struct(&self, span: Span,
776 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat> {
777 let pat = ast::PatStruct(path, field_pats, false);
781 fn arm(&self, _span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm {
790 fn arm_unreachable(&self, span: Span) -> ast::Arm {
791 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
794 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>,
795 arms: Vec<ast::Arm>) -> Gc<Expr> {
796 self.expr(span, ast::ExprMatch(arg, arms))
799 fn expr_if(&self, span: Span,
800 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
801 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr> {
802 let els = els.map(|x| self.expr_block(self.block_expr(x)));
803 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
806 fn lambda_fn_decl(&self, span: Span,
807 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr> {
808 self.expr(span, ast::ExprFnBlock(fn_decl, blk))
810 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr> {
811 let fn_decl = self.fn_decl(
812 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
813 self.ty_infer(span));
815 self.expr(span, ast::ExprFnBlock(fn_decl, blk))
817 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr> {
818 self.lambda(span, Vec::new(), blk)
821 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr> {
822 self.lambda(span, vec!(ident), blk)
825 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
826 self.lambda(span, ids, self.block_expr(expr))
828 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
829 self.lambda0(span, self.block_expr(expr))
831 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
832 self.lambda1(span, self.block_expr(expr), ident)
835 fn lambda_stmts(&self,
837 ids: Vec<ast::Ident>,
838 stmts: Vec<Gc<ast::Stmt>>)
840 self.lambda(span, ids, self.block(span, stmts, None))
842 fn lambda_stmts_0(&self, span: Span,
843 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr> {
844 self.lambda0(span, self.block(span, stmts, None))
846 fn lambda_stmts_1(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
847 ident: ast::Ident) -> Gc<ast::Expr> {
848 self.lambda1(span, self.block(span, stmts, None), ident)
851 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
852 let arg_pat = self.pat_ident(span, ident);
856 id: ast::DUMMY_NODE_ID
861 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl> {
870 fn item(&self, span: Span,
871 name: Ident, attrs: Vec<ast::Attribute>,
872 node: ast::Item_) -> Gc<ast::Item> {
873 // FIXME: Would be nice if our generated code didn't violate
874 // Rust coding conventions
875 box(GC) ast::Item { ident: name,
877 id: ast::DUMMY_NODE_ID,
883 fn item_fn_poly(&self,
886 inputs: Vec<ast::Arg> ,
889 body: P<ast::Block>) -> Gc<ast::Item> {
893 ast::ItemFn(self.fn_decl(inputs, output),
903 inputs: Vec<ast::Arg> ,
912 ast_util::empty_generics(),
916 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
917 let args = tys.move_iter().map(|ty| {
918 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
925 kind: ast::TupleVariantKind(args),
926 id: ast::DUMMY_NODE_ID,
932 fn item_enum_poly(&self, span: Span, name: Ident,
933 enum_definition: ast::EnumDef,
934 generics: Generics) -> Gc<ast::Item> {
935 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
938 fn item_enum(&self, span: Span, name: Ident,
939 enum_definition: ast::EnumDef) -> Gc<ast::Item> {
940 self.item_enum_poly(span, name, enum_definition,
941 ast_util::empty_generics())
944 fn item_struct(&self, span: Span, name: Ident,
945 struct_def: ast::StructDef) -> Gc<ast::Item> {
946 self.item_struct_poly(
950 ast_util::empty_generics()
954 fn item_struct_poly(&self, span: Span, name: Ident,
955 struct_def: ast::StructDef, generics: Generics) -> Gc<ast::Item> {
956 self.item(span, name, Vec::new(), ast::ItemStruct(box(GC) struct_def, generics))
959 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
960 attrs: Vec<ast::Attribute> ,
961 vi: Vec<ast::ViewItem> ,
962 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item> {
967 ast::ItemMod(ast::Mod {
975 fn item_static(&self,
979 mutbl: ast::Mutability,
982 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
985 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
986 generics: Generics) -> Gc<ast::Item> {
987 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
990 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item> {
991 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
994 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute {
995 respan(sp, ast::Attribute_ {
996 id: attr::mk_attr_id(),
997 style: ast::AttrOuter,
999 is_sugared_doc: false,
1003 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem> {
1004 box(GC) respan(sp, ast::MetaWord(w))
1008 name: InternedString,
1009 mis: Vec<Gc<ast::MetaItem>> )
1010 -> Gc<ast::MetaItem> {
1011 box(GC) respan(sp, ast::MetaList(name, mis))
1013 fn meta_name_value(&self,
1015 name: InternedString,
1017 -> Gc<ast::MetaItem> {
1018 box(GC) respan(sp, ast::MetaNameValue(name, respan(sp, value)))
1021 fn view_use(&self, sp: Span,
1022 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem {
1024 node: ast::ViewItemUse(vp),
1031 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1032 let last = path.segments.last().unwrap().identifier;
1033 self.view_use_simple_(sp, vis, last, path)
1036 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1037 ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1038 self.view_use(sp, vis,
1040 ast::ViewPathSimple(ident,
1042 ast::DUMMY_NODE_ID)))
1045 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1046 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1047 let imports = imports.iter().map(|id| {
1048 respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1051 self.view_use(sp, vis,
1053 ast::ViewPathList(self.path(sp, path),
1055 ast::DUMMY_NODE_ID)))
1058 fn view_use_glob(&self, sp: Span,
1059 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1060 self.view_use(sp, vis,
1062 ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))
1066 struct Duplicator<'a>;
1068 impl<'a> Folder for Duplicator<'a> {
1069 fn new_id(&mut self, _: NodeId) -> NodeId {
1074 pub trait Duplicate {
1076 // Duplication functions
1078 // These functions just duplicate AST nodes.
1081 fn duplicate(&self, cx: &ExtCtxt) -> Self;
1084 impl Duplicate for Gc<ast::Expr> {
1085 fn duplicate(&self, _: &ExtCtxt) -> Gc<ast::Expr> {
1086 let mut folder = Duplicator;
1087 folder.fold_expr(*self)