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>;
173 fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
175 fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
176 fn pat_none(&self, span: Span) -> Gc<ast::Pat>;
178 fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
179 fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
181 fn arm(&self, span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm;
182 fn arm_unreachable(&self, span: Span) -> ast::Arm;
184 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, arms: Vec<ast::Arm> ) -> Gc<ast::Expr>;
185 fn expr_if(&self, span: Span,
186 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
187 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr>;
188 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr>;
190 fn lambda_fn_decl(&self, span: Span,
191 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr>;
193 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr>;
194 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr>;
195 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr>;
197 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: Gc<ast::Expr>) -> Gc<ast::Expr>;
198 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
199 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr>;
201 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
202 blk: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
203 fn lambda_stmts_0(&self, span: Span,
204 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
205 fn lambda_stmts_1(&self, span: Span,
206 stmts: Vec<Gc<ast::Stmt>>, ident: ast::Ident) -> Gc<ast::Expr>;
209 fn item(&self, span: Span,
210 name: Ident, attrs: Vec<ast::Attribute>,
211 node: ast::Item_) -> Gc<ast::Item>;
213 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
215 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
217 fn item_fn_poly(&self,
220 inputs: Vec<ast::Arg> ,
223 body: P<ast::Block>) -> Gc<ast::Item>;
227 inputs: Vec<ast::Arg> ,
229 body: P<ast::Block>) -> Gc<ast::Item>;
231 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
232 fn item_enum_poly(&self,
235 enum_definition: ast::EnumDef,
236 generics: Generics) -> Gc<ast::Item>;
237 fn item_enum(&self, span: Span, name: Ident,
238 enum_def: ast::EnumDef) -> Gc<ast::Item>;
240 fn item_struct_poly(&self,
243 struct_def: ast::StructDef,
244 generics: Generics) -> Gc<ast::Item>;
245 fn item_struct(&self, span: Span, name: Ident,
246 struct_def: ast::StructDef) -> Gc<ast::Item>;
248 fn item_mod(&self, span: Span, inner_span: Span,
249 name: Ident, attrs: Vec<ast::Attribute>,
250 vi: Vec<ast::ViewItem>,
251 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item>;
253 fn item_static(&self,
257 mutbl: ast::Mutability,
261 fn item_ty_poly(&self,
265 generics: Generics) -> Gc<ast::Item>;
266 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item>;
268 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute;
270 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem>;
273 name: InternedString,
274 mis: Vec<Gc<ast::MetaItem>>)
275 -> Gc<ast::MetaItem>;
276 fn meta_name_value(&self,
278 name: InternedString,
280 -> Gc<ast::MetaItem>;
282 fn view_use(&self, sp: Span,
283 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem;
284 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
285 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
286 ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
287 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
288 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
289 fn view_use_glob(&self, sp: Span,
290 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
293 impl<'a> AstBuilder for ExtCtxt<'a> {
294 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
295 self.path_all(span, false, strs, Vec::new(), Vec::new())
297 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
298 self.path(span, vec!(id))
300 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
301 self.path_all(span, true, strs, Vec::new(), Vec::new())
306 mut idents: Vec<ast::Ident> ,
307 lifetimes: Vec<ast::Lifetime>,
308 types: Vec<P<ast::Ty>> )
310 let last_identifier = idents.pop().unwrap();
311 let mut segments: Vec<ast::PathSegment> = idents.move_iter()
315 lifetimes: Vec::new(),
316 types: OwnedSlice::empty(),
319 segments.push(ast::PathSegment {
320 identifier: last_identifier,
321 lifetimes: lifetimes,
322 types: OwnedSlice::from_vec(types),
331 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
338 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
340 id: ast::DUMMY_NODE_ID,
346 fn ty_path(&self, path: ast::Path, bounds: Option<OwnedSlice<ast::TyParamBound>>)
349 ast::TyPath(path, bounds, ast::DUMMY_NODE_ID))
352 // Might need to take bounds as an argument in the future, if you ever want
353 // to generate a bounded existential trait type.
354 fn ty_ident(&self, span: Span, ident: ast::Ident)
356 self.ty_path(self.path_ident(span, ident), None)
362 lifetime: Option<ast::Lifetime>,
363 mutbl: ast::Mutability)
366 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
369 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty> {
370 self.ty(span, ast::TyUniq(ty))
373 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
375 self.path_all(DUMMY_SP,
378 self.ident_of("std"),
379 self.ident_of("option"),
380 self.ident_of("Option")
386 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
389 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
394 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
395 self.ty(span, ast::TyInfer)
398 fn ty_nil(&self) -> P<ast::Ty> {
400 id: ast::DUMMY_NODE_ID,
409 bounds: OwnedSlice<ast::TyParamBound>,
410 unbound: Option<ast::TyParamBound>,
411 default: Option<P<ast::Ty>>) -> ast::TyParam {
414 id: ast::DUMMY_NODE_ID,
422 // these are strange, and probably shouldn't be used outside of
423 // pipes. Specifically, the global version possible generates
425 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
426 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
429 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
430 ty_params.iter().map(|p| self.ty_path(
431 self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect()
434 fn strip_bounds(&self, generics: &Generics) -> Generics {
435 let new_params = generics.ty_params.map(|ty_param| {
436 ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
439 ty_params: new_params,
440 .. (*generics).clone()
444 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
447 ref_id: ast::DUMMY_NODE_ID
451 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
452 ast::TraitTyParamBound(self.trait_ref(path))
455 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
456 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
459 fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt> {
460 box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
463 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
464 ex: Gc<ast::Expr>) -> Gc<ast::Stmt> {
466 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
468 self.pat_ident(sp, ident)
470 let local = box(GC) ast::Local {
471 ty: self.ty_infer(sp),
474 id: ast::DUMMY_NODE_ID,
476 source: ast::LocalLet,
478 let decl = respan(sp, ast::DeclLocal(local));
479 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
482 fn stmt_let_typed(&self,
490 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
492 self.pat_ident(sp, ident)
494 let local = box(GC) ast::Local {
498 id: ast::DUMMY_NODE_ID,
500 source: ast::LocalLet,
502 let decl = respan(sp, ast::DeclLocal(local));
503 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
508 stmts: Vec<Gc<ast::Stmt>>,
509 expr: Option<Gc<Expr>>)
511 self.block_all(span, Vec::new(), stmts, expr)
514 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt> {
515 let decl = respan(sp, ast::DeclItem(item));
516 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
519 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block> {
520 self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
524 view_items: Vec<ast::ViewItem> ,
525 stmts: Vec<Gc<ast::Stmt>>,
526 expr: Option<Gc<ast::Expr>>) -> P<ast::Block> {
528 view_items: view_items,
531 id: ast::DUMMY_NODE_ID,
532 rules: ast::DefaultBlock,
537 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr> {
539 id: ast::DUMMY_NODE_ID,
545 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr> {
546 self.expr(path.span, ast::ExprPath(path))
549 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr> {
550 self.expr_path(self.path_ident(span, id))
552 fn expr_self(&self, span: Span) -> Gc<ast::Expr> {
553 self.expr_ident(span, special_idents::self_)
556 fn expr_binary(&self, sp: Span, op: ast::BinOp,
557 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr> {
558 self.expr(sp, ast::ExprBinary(op, lhs, rhs))
561 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
562 self.expr_unary(sp, ast::UnDeref, e)
564 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
565 self.expr(sp, ast::ExprUnary(op, e))
568 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
569 self.expr_unary(sp, ast::UnBox, e)
572 fn expr_field_access(&self, sp: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
573 let field_name = token::get_ident(ident);
574 let field_span = Span {
575 lo: sp.lo - Pos::from_uint(field_name.get().len()),
577 expn_info: sp.expn_info,
580 let id = Spanned { node: ident, span: field_span };
581 self.expr(sp, ast::ExprField(expr, id, Vec::new()))
583 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
584 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
586 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
587 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
590 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
591 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
592 self.expr(span, ast::ExprCall(expr, args))
594 fn expr_call_ident(&self, span: Span, id: ast::Ident,
595 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
596 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
598 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
599 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
600 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
601 self.expr_call(sp, pathexpr, args)
603 fn expr_method_call(&self, span: Span,
606 mut args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
607 let id = Spanned { node: ident, span: span };
609 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
611 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr> {
612 self.expr(b.span, ast::ExprBlock(b))
614 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field {
615 ast::Field { ident: respan(span, name), expr: e, span: span }
617 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
618 self.expr(span, ast::ExprStruct(path, fields, None))
620 fn expr_struct_ident(&self, span: Span,
621 id: ast::Ident, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
622 self.expr_struct(span, self.path_ident(span, id), fields)
625 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr> {
626 self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit)))
628 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> {
629 self.expr_lit(span, ast::LitUint(i as u64, ast::TyU))
631 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> {
632 self.expr_lit(sp, ast::LitInt(i as i64, ast::TyI))
634 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> {
635 self.expr_lit(sp, ast::LitUint(u as u64, ast::TyU8))
637 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> {
638 self.expr_lit(sp, ast::LitBool(value))
641 fn expr_vstore(&self, sp: Span, expr: Gc<ast::Expr>, vst: ast::ExprVstore) -> Gc<ast::Expr> {
642 self.expr(sp, ast::ExprVstore(expr, vst))
644 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
645 self.expr(sp, ast::ExprVec(exprs))
647 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr> {
648 self.expr_call_global(sp,
649 vec!(self.ident_of("std"),
650 self.ident_of("vec"),
651 self.ident_of("Vec"),
652 self.ident_of("new")),
655 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
656 self.expr_vstore(sp, self.expr_vec(sp, exprs), ast::ExprVstoreSlice)
658 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
659 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
661 fn expr_str_uniq(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
662 self.expr_vstore(sp, self.expr_str(sp, s), ast::ExprVstoreUniq)
666 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, ty: P<ast::Ty>) -> Gc<ast::Expr> {
667 self.expr(sp, ast::ExprCast(expr, ty))
671 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
673 self.ident_of("std"),
674 self.ident_of("option"),
675 self.ident_of("Some"));
676 self.expr_call_global(sp, some, vec!(expr))
679 fn expr_none(&self, sp: Span) -> Gc<ast::Expr> {
680 let none = self.path_global(sp, vec!(
681 self.ident_of("std"),
682 self.ident_of("option"),
683 self.ident_of("None")));
687 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
688 self.expr(sp, ast::ExprTup(exprs))
691 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr> {
692 let loc = self.codemap().lookup_char_pos(span.lo);
693 self.expr_call_global(
696 self.ident_of("std"),
698 self.ident_of("begin_unwind")),
700 self.expr_str(span, msg),
702 token::intern_and_get_ident(loc.file
705 self.expr_uint(span, loc.line)))
708 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr> {
711 "internal error: entered unreachable code"))
714 fn expr_ok(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
716 self.ident_of("std"),
717 self.ident_of("result"),
718 self.ident_of("Ok"));
719 self.expr_call_global(sp, ok, vec!(expr))
722 fn expr_err(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
724 self.ident_of("std"),
725 self.ident_of("result"),
726 self.ident_of("Err"));
727 self.expr_call_global(sp, err, vec!(expr))
730 fn expr_try(&self, sp: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr> {
731 let ok = self.ident_of("Ok");
732 let ok_path = self.path_ident(sp, ok);
733 let err = self.ident_of("Err");
734 let err_path = self.path_ident(sp, err);
736 let binding_variable = self.ident_of("__try_var");
737 let binding_pat = self.pat_ident(sp, binding_variable);
738 let binding_expr = self.expr_ident(sp, binding_variable);
740 // Ok(__try_var) pattern
741 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat));
743 // Err(__try_var) (pattern and expression resp.)
744 let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
745 let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr));
746 // return Err(__try_var)
747 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
749 // Ok(__try_var) => __try_var
750 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
751 // Err(__try_var) => return Err(__try_var)
752 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
754 // match head { Ok() => ..., Err() => ... }
755 self.expr_match(sp, head, vec!(ok_arm, err_arm))
759 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat> {
760 box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }
762 fn pat_wild(&self, span: Span) -> Gc<ast::Pat> {
763 self.pat(span, ast::PatWild)
765 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat> {
766 self.pat(span, ast::PatLit(expr))
768 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat> {
769 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
772 fn pat_ident_binding_mode(&self,
775 bm: ast::BindingMode) -> Gc<ast::Pat> {
776 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
779 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<Gc<ast::Pat>> ) -> Gc<ast::Pat> {
780 let pat = ast::PatEnum(path, Some(subpats));
783 fn pat_struct(&self, span: Span,
784 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat> {
785 let pat = ast::PatStruct(path, field_pats, false);
788 fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat> {
789 let pat = ast::PatTup(pats);
793 fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
795 self.ident_of("std"),
796 self.ident_of("option"),
797 self.ident_of("Some"));
798 let path = self.path_global(span, some);
799 self.pat_enum(span, path, vec!(pat))
802 fn pat_none(&self, span: Span) -> Gc<ast::Pat> {
804 self.ident_of("std"),
805 self.ident_of("option"),
806 self.ident_of("None"));
807 let path = self.path_global(span, some);
808 self.pat_enum(span, path, vec!())
811 fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
813 self.ident_of("std"),
814 self.ident_of("result"),
815 self.ident_of("Ok"));
816 let path = self.path_global(span, some);
817 self.pat_enum(span, path, vec!(pat))
820 fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
822 self.ident_of("std"),
823 self.ident_of("result"),
824 self.ident_of("Err"));
825 let path = self.path_global(span, some);
826 self.pat_enum(span, path, vec!(pat))
829 fn arm(&self, _span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm {
838 fn arm_unreachable(&self, span: Span) -> ast::Arm {
839 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
842 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>,
843 arms: Vec<ast::Arm>) -> Gc<Expr> {
844 self.expr(span, ast::ExprMatch(arg, arms))
847 fn expr_if(&self, span: Span,
848 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
849 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr> {
850 let els = els.map(|x| self.expr_block(self.block_expr(x)));
851 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
854 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr> {
855 self.expr(span, ast::ExprLoop(block, None))
858 fn lambda_fn_decl(&self, span: Span,
859 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr> {
860 self.expr(span, ast::ExprFnBlock(fn_decl, blk))
862 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr> {
863 let fn_decl = self.fn_decl(
864 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
865 self.ty_infer(span));
867 self.expr(span, ast::ExprFnBlock(fn_decl, blk))
869 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr> {
870 self.lambda(span, Vec::new(), blk)
873 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr> {
874 self.lambda(span, vec!(ident), blk)
877 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
878 self.lambda(span, ids, self.block_expr(expr))
880 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
881 self.lambda0(span, self.block_expr(expr))
883 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
884 self.lambda1(span, self.block_expr(expr), ident)
887 fn lambda_stmts(&self,
889 ids: Vec<ast::Ident>,
890 stmts: Vec<Gc<ast::Stmt>>)
892 self.lambda(span, ids, self.block(span, stmts, None))
894 fn lambda_stmts_0(&self, span: Span,
895 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr> {
896 self.lambda0(span, self.block(span, stmts, None))
898 fn lambda_stmts_1(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
899 ident: ast::Ident) -> Gc<ast::Expr> {
900 self.lambda1(span, self.block(span, stmts, None), ident)
903 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
904 let arg_pat = self.pat_ident(span, ident);
908 id: ast::DUMMY_NODE_ID
913 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl> {
922 fn item(&self, span: Span,
923 name: Ident, attrs: Vec<ast::Attribute>,
924 node: ast::Item_) -> Gc<ast::Item> {
925 // FIXME: Would be nice if our generated code didn't violate
926 // Rust coding conventions
927 box(GC) ast::Item { ident: name,
929 id: ast::DUMMY_NODE_ID,
935 fn item_fn_poly(&self,
938 inputs: Vec<ast::Arg> ,
941 body: P<ast::Block>) -> Gc<ast::Item> {
945 ast::ItemFn(self.fn_decl(inputs, output),
955 inputs: Vec<ast::Arg> ,
964 ast_util::empty_generics(),
968 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
969 let args = tys.move_iter().map(|ty| {
970 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
977 kind: ast::TupleVariantKind(args),
978 id: ast::DUMMY_NODE_ID,
984 fn item_enum_poly(&self, span: Span, name: Ident,
985 enum_definition: ast::EnumDef,
986 generics: Generics) -> Gc<ast::Item> {
987 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
990 fn item_enum(&self, span: Span, name: Ident,
991 enum_definition: ast::EnumDef) -> Gc<ast::Item> {
992 self.item_enum_poly(span, name, enum_definition,
993 ast_util::empty_generics())
996 fn item_struct(&self, span: Span, name: Ident,
997 struct_def: ast::StructDef) -> Gc<ast::Item> {
998 self.item_struct_poly(
1002 ast_util::empty_generics()
1006 fn item_struct_poly(&self, span: Span, name: Ident,
1007 struct_def: ast::StructDef, generics: Generics) -> Gc<ast::Item> {
1008 self.item(span, name, Vec::new(), ast::ItemStruct(box(GC) struct_def, generics))
1011 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1012 attrs: Vec<ast::Attribute> ,
1013 vi: Vec<ast::ViewItem> ,
1014 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item> {
1019 ast::ItemMod(ast::Mod {
1027 fn item_static(&self,
1031 mutbl: ast::Mutability,
1032 expr: Gc<ast::Expr>)
1034 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1037 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1038 generics: Generics) -> Gc<ast::Item> {
1039 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1042 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item> {
1043 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1046 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute {
1047 respan(sp, ast::Attribute_ {
1048 id: attr::mk_attr_id(),
1049 style: ast::AttrOuter,
1051 is_sugared_doc: false,
1055 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem> {
1056 box(GC) respan(sp, ast::MetaWord(w))
1060 name: InternedString,
1061 mis: Vec<Gc<ast::MetaItem>> )
1062 -> Gc<ast::MetaItem> {
1063 box(GC) respan(sp, ast::MetaList(name, mis))
1065 fn meta_name_value(&self,
1067 name: InternedString,
1069 -> Gc<ast::MetaItem> {
1070 box(GC) respan(sp, ast::MetaNameValue(name, respan(sp, value)))
1073 fn view_use(&self, sp: Span,
1074 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem {
1076 node: ast::ViewItemUse(vp),
1083 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1084 let last = path.segments.last().unwrap().identifier;
1085 self.view_use_simple_(sp, vis, last, path)
1088 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1089 ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1090 self.view_use(sp, vis,
1092 ast::ViewPathSimple(ident,
1094 ast::DUMMY_NODE_ID)))
1097 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1098 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1099 let imports = imports.iter().map(|id| {
1100 respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1103 self.view_use(sp, vis,
1105 ast::ViewPathList(self.path(sp, path),
1107 ast::DUMMY_NODE_ID)))
1110 fn view_use_glob(&self, sp: Span,
1111 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1112 self.view_use(sp, vis,
1114 ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))
1118 struct Duplicator<'a>;
1120 impl<'a> Folder for Duplicator<'a> {
1121 fn new_id(&mut self, _: NodeId) -> NodeId {
1126 pub trait Duplicate {
1128 // Duplication functions
1130 // These functions just duplicate AST nodes.
1133 fn duplicate(&self, cx: &ExtCtxt) -> Self;
1136 impl Duplicate for Gc<ast::Expr> {
1137 fn duplicate(&self, _: &ExtCtxt) -> Gc<ast::Expr> {
1138 let mut folder = Duplicator;
1139 folder.fold_expr(*self)