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;
76 fn lifetime_def(&self,
79 bounds: Vec<ast::Lifetime>)
83 fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt>;
84 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
85 ex: Gc<ast::Expr>) -> Gc<ast::Stmt>;
86 fn stmt_let_typed(&self,
93 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt>;
96 fn block(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
97 expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
98 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block>;
99 fn block_all(&self, span: Span,
100 view_items: Vec<ast::ViewItem> ,
101 stmts: Vec<Gc<ast::Stmt>> ,
102 expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
105 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr>;
106 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr>;
107 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr>;
109 fn expr_self(&self, span: Span) -> Gc<ast::Expr>;
110 fn expr_binary(&self, sp: Span, op: ast::BinOp,
111 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr>;
112 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
113 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
115 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
116 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
117 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
118 fn expr_field_access(&self, span: Span, expr: Gc<ast::Expr>,
119 ident: ast::Ident) -> Gc<ast::Expr>;
120 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
121 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
122 fn expr_call_ident(&self, span: Span, id: ast::Ident,
123 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
124 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
125 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
126 fn expr_method_call(&self, span: Span,
127 expr: Gc<ast::Expr>, ident: ast::Ident,
128 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
129 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr>;
130 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>,
131 ty: P<ast::Ty>) -> Gc<ast::Expr>;
133 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field;
134 fn expr_struct(&self, span: Span, path: ast::Path,
135 fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
136 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
137 fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
139 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr>;
141 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr>;
142 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr>;
143 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr>;
144 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr>;
146 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
147 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr>;
148 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
149 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr>;
151 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
152 fn expr_none(&self, sp: Span) -> Gc<ast::Expr>;
154 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
156 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr>;
157 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr>;
159 fn expr_ok(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
160 fn expr_err(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
161 fn expr_try(&self, span: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr>;
163 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat>;
164 fn pat_wild(&self, span: Span) -> Gc<ast::Pat>;
165 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat>;
166 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat>;
168 fn pat_ident_binding_mode(&self,
171 bm: ast::BindingMode) -> Gc<ast::Pat>;
172 fn pat_enum(&self, span: Span, path: ast::Path,
173 subpats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
174 fn pat_struct(&self, span: Span,
175 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat>;
176 fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
178 fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
179 fn pat_none(&self, span: Span) -> Gc<ast::Pat>;
181 fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
182 fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
184 fn arm(&self, span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm;
185 fn arm_unreachable(&self, span: Span) -> ast::Arm;
187 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, arms: Vec<ast::Arm> ) -> Gc<ast::Expr>;
188 fn expr_if(&self, span: Span,
189 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
190 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr>;
191 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr>;
193 fn lambda_fn_decl(&self, span: Span,
194 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr>;
196 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr>;
197 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr>;
198 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr>;
200 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: Gc<ast::Expr>) -> Gc<ast::Expr>;
201 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
202 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr>;
204 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
205 blk: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
206 fn lambda_stmts_0(&self, span: Span,
207 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
208 fn lambda_stmts_1(&self, span: Span,
209 stmts: Vec<Gc<ast::Stmt>>, ident: ast::Ident) -> Gc<ast::Expr>;
212 fn item(&self, span: Span,
213 name: Ident, attrs: Vec<ast::Attribute>,
214 node: ast::Item_) -> Gc<ast::Item>;
216 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
218 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
220 fn item_fn_poly(&self,
223 inputs: Vec<ast::Arg> ,
226 body: P<ast::Block>) -> Gc<ast::Item>;
230 inputs: Vec<ast::Arg> ,
232 body: P<ast::Block>) -> Gc<ast::Item>;
234 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
235 fn item_enum_poly(&self,
238 enum_definition: ast::EnumDef,
239 generics: Generics) -> Gc<ast::Item>;
240 fn item_enum(&self, span: Span, name: Ident,
241 enum_def: ast::EnumDef) -> Gc<ast::Item>;
243 fn item_struct_poly(&self,
246 struct_def: ast::StructDef,
247 generics: Generics) -> Gc<ast::Item>;
248 fn item_struct(&self, span: Span, name: Ident,
249 struct_def: ast::StructDef) -> Gc<ast::Item>;
251 fn item_mod(&self, span: Span, inner_span: Span,
252 name: Ident, attrs: Vec<ast::Attribute>,
253 vi: Vec<ast::ViewItem>,
254 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item>;
256 fn item_static(&self,
260 mutbl: ast::Mutability,
264 fn item_ty_poly(&self,
268 generics: Generics) -> Gc<ast::Item>;
269 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item>;
271 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute;
273 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem>;
276 name: InternedString,
277 mis: Vec<Gc<ast::MetaItem>>)
278 -> Gc<ast::MetaItem>;
279 fn meta_name_value(&self,
281 name: InternedString,
283 -> Gc<ast::MetaItem>;
285 fn view_use(&self, sp: Span,
286 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem;
287 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
288 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
289 ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
290 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
291 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
292 fn view_use_glob(&self, sp: Span,
293 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
296 impl<'a> AstBuilder for ExtCtxt<'a> {
297 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
298 self.path_all(span, false, strs, Vec::new(), Vec::new())
300 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
301 self.path(span, vec!(id))
303 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
304 self.path_all(span, true, strs, Vec::new(), Vec::new())
309 mut idents: Vec<ast::Ident> ,
310 lifetimes: Vec<ast::Lifetime>,
311 types: Vec<P<ast::Ty>> )
313 let last_identifier = idents.pop().unwrap();
314 let mut segments: Vec<ast::PathSegment> = idents.move_iter()
318 lifetimes: Vec::new(),
319 types: OwnedSlice::empty(),
322 segments.push(ast::PathSegment {
323 identifier: last_identifier,
324 lifetimes: lifetimes,
325 types: OwnedSlice::from_vec(types),
334 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
341 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
343 id: ast::DUMMY_NODE_ID,
349 fn ty_path(&self, path: ast::Path, bounds: Option<OwnedSlice<ast::TyParamBound>>)
352 ast::TyPath(path, bounds, ast::DUMMY_NODE_ID))
355 // Might need to take bounds as an argument in the future, if you ever want
356 // to generate a bounded existential trait type.
357 fn ty_ident(&self, span: Span, ident: ast::Ident)
359 self.ty_path(self.path_ident(span, ident), None)
365 lifetime: Option<ast::Lifetime>,
366 mutbl: ast::Mutability)
369 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
372 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty> {
373 self.ty(span, ast::TyUniq(ty))
376 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
378 self.path_all(DUMMY_SP,
381 self.ident_of("std"),
382 self.ident_of("option"),
383 self.ident_of("Option")
389 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
392 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
397 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
398 self.ty(span, ast::TyInfer)
401 fn ty_nil(&self) -> P<ast::Ty> {
403 id: ast::DUMMY_NODE_ID,
412 bounds: OwnedSlice<ast::TyParamBound>,
413 unbound: Option<ast::TyParamBound>,
414 default: Option<P<ast::Ty>>) -> ast::TyParam {
417 id: ast::DUMMY_NODE_ID,
425 // these are strange, and probably shouldn't be used outside of
426 // pipes. Specifically, the global version possible generates
428 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
429 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
432 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
433 ty_params.iter().map(|p| self.ty_path(
434 self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect()
437 fn strip_bounds(&self, generics: &Generics) -> Generics {
438 let new_params = generics.ty_params.map(|ty_param| {
439 ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
442 ty_params: new_params,
443 .. (*generics).clone()
447 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
450 ref_id: ast::DUMMY_NODE_ID
454 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
455 ast::TraitTyParamBound(self.trait_ref(path))
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: Gc<ast::Expr>) -> Gc<ast::Stmt> {
474 box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
477 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
478 ex: Gc<ast::Expr>) -> Gc<ast::Stmt> {
480 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
482 self.pat_ident(sp, ident)
484 let local = box(GC) ast::Local {
485 ty: self.ty_infer(sp),
488 id: ast::DUMMY_NODE_ID,
490 source: ast::LocalLet,
492 let decl = respan(sp, ast::DeclLocal(local));
493 box(GC) respan(sp, ast::StmtDecl(box(GC) 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 = box(GC) ast::Local {
512 id: ast::DUMMY_NODE_ID,
514 source: ast::LocalLet,
516 let decl = respan(sp, ast::DeclLocal(local));
517 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
522 stmts: Vec<Gc<ast::Stmt>>,
523 expr: Option<Gc<Expr>>)
525 self.block_all(span, Vec::new(), stmts, expr)
528 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt> {
529 let decl = respan(sp, ast::DeclItem(item));
530 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
533 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block> {
534 self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
538 view_items: Vec<ast::ViewItem> ,
539 stmts: Vec<Gc<ast::Stmt>>,
540 expr: Option<Gc<ast::Expr>>) -> P<ast::Block> {
542 view_items: view_items,
545 id: ast::DUMMY_NODE_ID,
546 rules: ast::DefaultBlock,
551 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr> {
553 id: ast::DUMMY_NODE_ID,
559 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr> {
560 self.expr(path.span, ast::ExprPath(path))
563 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr> {
564 self.expr_path(self.path_ident(span, id))
566 fn expr_self(&self, span: Span) -> Gc<ast::Expr> {
567 self.expr_ident(span, special_idents::self_)
570 fn expr_binary(&self, sp: Span, op: ast::BinOp,
571 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr> {
572 self.expr(sp, ast::ExprBinary(op, lhs, rhs))
575 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
576 self.expr_unary(sp, ast::UnDeref, e)
578 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
579 self.expr(sp, ast::ExprUnary(op, e))
582 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
583 self.expr_unary(sp, ast::UnBox, e)
586 fn expr_field_access(&self, sp: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
587 let field_name = token::get_ident(ident);
588 let field_span = Span {
589 lo: sp.lo - Pos::from_uint(field_name.get().len()),
591 expn_info: sp.expn_info,
594 let id = Spanned { node: ident, span: field_span };
595 self.expr(sp, ast::ExprField(expr, id, Vec::new()))
597 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
598 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
600 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
601 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
604 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
605 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
606 self.expr(span, ast::ExprCall(expr, args))
608 fn expr_call_ident(&self, span: Span, id: ast::Ident,
609 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
610 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
612 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
613 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
614 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
615 self.expr_call(sp, pathexpr, args)
617 fn expr_method_call(&self, span: Span,
620 mut args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
621 let id = Spanned { node: ident, span: span };
623 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
625 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr> {
626 self.expr(b.span, ast::ExprBlock(b))
628 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field {
629 ast::Field { ident: respan(span, name), expr: e, span: span }
631 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
632 self.expr(span, ast::ExprStruct(path, fields, None))
634 fn expr_struct_ident(&self, span: Span,
635 id: ast::Ident, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
636 self.expr_struct(span, self.path_ident(span, id), fields)
639 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr> {
640 self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit)))
642 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> {
643 self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyU)))
645 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> {
646 self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyI, ast::Sign::new(i))))
648 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> {
649 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
651 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> {
652 self.expr_lit(sp, ast::LitBool(value))
655 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
656 self.expr(sp, ast::ExprVec(exprs))
658 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr> {
659 self.expr_call_global(sp,
660 vec!(self.ident_of("std"),
661 self.ident_of("vec"),
662 self.ident_of("Vec"),
663 self.ident_of("new")),
666 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
667 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
669 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
670 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
673 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, ty: P<ast::Ty>) -> Gc<ast::Expr> {
674 self.expr(sp, ast::ExprCast(expr, ty))
678 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
680 self.ident_of("std"),
681 self.ident_of("option"),
682 self.ident_of("Some"));
683 self.expr_call_global(sp, some, vec!(expr))
686 fn expr_none(&self, sp: Span) -> Gc<ast::Expr> {
687 let none = self.path_global(sp, vec!(
688 self.ident_of("std"),
689 self.ident_of("option"),
690 self.ident_of("None")));
694 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
695 self.expr(sp, ast::ExprTup(exprs))
698 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr> {
699 let loc = self.codemap().lookup_char_pos(span.lo);
700 let expr_file = self.expr_str(span,
701 token::intern_and_get_ident(loc.file
704 let expr_line = self.expr_uint(span, loc.line);
705 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
706 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
707 self.expr_call_global(
710 self.ident_of("std"),
712 self.ident_of("begin_unwind")),
714 self.expr_str(span, msg),
718 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr> {
721 "internal error: entered unreachable code"))
724 fn expr_ok(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
726 self.ident_of("std"),
727 self.ident_of("result"),
728 self.ident_of("Ok"));
729 self.expr_call_global(sp, ok, vec!(expr))
732 fn expr_err(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
734 self.ident_of("std"),
735 self.ident_of("result"),
736 self.ident_of("Err"));
737 self.expr_call_global(sp, err, vec!(expr))
740 fn expr_try(&self, sp: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr> {
741 let ok = self.ident_of("Ok");
742 let ok_path = self.path_ident(sp, ok);
743 let err = self.ident_of("Err");
744 let err_path = self.path_ident(sp, err);
746 let binding_variable = self.ident_of("__try_var");
747 let binding_pat = self.pat_ident(sp, binding_variable);
748 let binding_expr = self.expr_ident(sp, binding_variable);
750 // Ok(__try_var) pattern
751 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat));
753 // Err(__try_var) (pattern and expression resp.)
754 let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
755 let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr));
756 // return Err(__try_var)
757 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
759 // Ok(__try_var) => __try_var
760 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
761 // Err(__try_var) => return Err(__try_var)
762 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
764 // match head { Ok() => ..., Err() => ... }
765 self.expr_match(sp, head, vec!(ok_arm, err_arm))
769 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat> {
770 box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }
772 fn pat_wild(&self, span: Span) -> Gc<ast::Pat> {
773 self.pat(span, ast::PatWild(ast::PatWildSingle))
775 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat> {
776 self.pat(span, ast::PatLit(expr))
778 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat> {
779 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
782 fn pat_ident_binding_mode(&self,
785 bm: ast::BindingMode) -> Gc<ast::Pat> {
786 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
789 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<Gc<ast::Pat>> ) -> Gc<ast::Pat> {
790 let pat = ast::PatEnum(path, Some(subpats));
793 fn pat_struct(&self, span: Span,
794 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat> {
795 let pat = ast::PatStruct(path, field_pats, false);
798 fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat> {
799 let pat = ast::PatTup(pats);
803 fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
805 self.ident_of("std"),
806 self.ident_of("option"),
807 self.ident_of("Some"));
808 let path = self.path_global(span, some);
809 self.pat_enum(span, path, vec!(pat))
812 fn pat_none(&self, span: Span) -> Gc<ast::Pat> {
814 self.ident_of("std"),
815 self.ident_of("option"),
816 self.ident_of("None"));
817 let path = self.path_global(span, some);
818 self.pat_enum(span, path, vec!())
821 fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
823 self.ident_of("std"),
824 self.ident_of("result"),
825 self.ident_of("Ok"));
826 let path = self.path_global(span, some);
827 self.pat_enum(span, path, vec!(pat))
830 fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
832 self.ident_of("std"),
833 self.ident_of("result"),
834 self.ident_of("Err"));
835 let path = self.path_global(span, some);
836 self.pat_enum(span, path, vec!(pat))
839 fn arm(&self, _span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm {
848 fn arm_unreachable(&self, span: Span) -> ast::Arm {
849 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
852 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>,
853 arms: Vec<ast::Arm>) -> Gc<Expr> {
854 self.expr(span, ast::ExprMatch(arg, arms))
857 fn expr_if(&self, span: Span,
858 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
859 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr> {
860 let els = els.map(|x| self.expr_block(self.block_expr(x)));
861 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
864 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr> {
865 self.expr(span, ast::ExprLoop(block, None))
868 fn lambda_fn_decl(&self, span: Span,
869 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr> {
870 self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
872 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr> {
873 let fn_decl = self.fn_decl(
874 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
875 self.ty_infer(span));
877 self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
879 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr> {
880 self.lambda(span, Vec::new(), blk)
883 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr> {
884 self.lambda(span, vec!(ident), blk)
887 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
888 self.lambda(span, ids, self.block_expr(expr))
890 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
891 self.lambda0(span, self.block_expr(expr))
893 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
894 self.lambda1(span, self.block_expr(expr), ident)
897 fn lambda_stmts(&self,
899 ids: Vec<ast::Ident>,
900 stmts: Vec<Gc<ast::Stmt>>)
902 self.lambda(span, ids, self.block(span, stmts, None))
904 fn lambda_stmts_0(&self, span: Span,
905 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr> {
906 self.lambda0(span, self.block(span, stmts, None))
908 fn lambda_stmts_1(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
909 ident: ast::Ident) -> Gc<ast::Expr> {
910 self.lambda1(span, self.block(span, stmts, None), ident)
913 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
914 let arg_pat = self.pat_ident(span, ident);
918 id: ast::DUMMY_NODE_ID
923 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl> {
932 fn item(&self, span: Span,
933 name: Ident, attrs: Vec<ast::Attribute>,
934 node: ast::Item_) -> Gc<ast::Item> {
935 // FIXME: Would be nice if our generated code didn't violate
936 // Rust coding conventions
937 box(GC) ast::Item { ident: name,
939 id: ast::DUMMY_NODE_ID,
945 fn item_fn_poly(&self,
948 inputs: Vec<ast::Arg> ,
951 body: P<ast::Block>) -> Gc<ast::Item> {
955 ast::ItemFn(self.fn_decl(inputs, output),
965 inputs: Vec<ast::Arg> ,
974 ast_util::empty_generics(),
978 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
979 let args = tys.move_iter().map(|ty| {
980 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
987 kind: ast::TupleVariantKind(args),
988 id: ast::DUMMY_NODE_ID,
994 fn item_enum_poly(&self, span: Span, name: Ident,
995 enum_definition: ast::EnumDef,
996 generics: Generics) -> Gc<ast::Item> {
997 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1000 fn item_enum(&self, span: Span, name: Ident,
1001 enum_definition: ast::EnumDef) -> Gc<ast::Item> {
1002 self.item_enum_poly(span, name, enum_definition,
1003 ast_util::empty_generics())
1006 fn item_struct(&self, span: Span, name: Ident,
1007 struct_def: ast::StructDef) -> Gc<ast::Item> {
1008 self.item_struct_poly(
1012 ast_util::empty_generics()
1016 fn item_struct_poly(&self, span: Span, name: Ident,
1017 struct_def: ast::StructDef, generics: Generics) -> Gc<ast::Item> {
1018 self.item(span, name, Vec::new(), ast::ItemStruct(box(GC) struct_def, generics))
1021 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1022 attrs: Vec<ast::Attribute> ,
1023 vi: Vec<ast::ViewItem> ,
1024 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item> {
1029 ast::ItemMod(ast::Mod {
1037 fn item_static(&self,
1041 mutbl: ast::Mutability,
1042 expr: Gc<ast::Expr>)
1044 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1047 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1048 generics: Generics) -> Gc<ast::Item> {
1049 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1052 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item> {
1053 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1056 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute {
1057 respan(sp, ast::Attribute_ {
1058 id: attr::mk_attr_id(),
1059 style: ast::AttrOuter,
1061 is_sugared_doc: false,
1065 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem> {
1066 box(GC) respan(sp, ast::MetaWord(w))
1070 name: InternedString,
1071 mis: Vec<Gc<ast::MetaItem>> )
1072 -> Gc<ast::MetaItem> {
1073 box(GC) respan(sp, ast::MetaList(name, mis))
1075 fn meta_name_value(&self,
1077 name: InternedString,
1079 -> Gc<ast::MetaItem> {
1080 box(GC) respan(sp, ast::MetaNameValue(name, respan(sp, value)))
1083 fn view_use(&self, sp: Span,
1084 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem {
1086 node: ast::ViewItemUse(vp),
1093 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1094 let last = path.segments.last().unwrap().identifier;
1095 self.view_use_simple_(sp, vis, last, path)
1098 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1099 ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1100 self.view_use(sp, vis,
1102 ast::ViewPathSimple(ident,
1104 ast::DUMMY_NODE_ID)))
1107 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1108 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1109 let imports = imports.iter().map(|id| {
1110 respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1113 self.view_use(sp, vis,
1115 ast::ViewPathList(self.path(sp, path),
1117 ast::DUMMY_NODE_ID)))
1120 fn view_use_glob(&self, sp: Span,
1121 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1122 self.view_use(sp, vis,
1124 ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))
1128 struct Duplicator<'a>;
1130 impl<'a> Folder for Duplicator<'a> {
1131 fn new_id(&mut self, _: NodeId) -> NodeId {
1136 pub trait Duplicate {
1138 // Duplication functions
1140 // These functions just duplicate AST nodes.
1143 fn duplicate(&self, cx: &ExtCtxt) -> Self;
1146 impl Duplicate for Gc<ast::Expr> {
1147 fn duplicate(&self, _: &ExtCtxt) -> Gc<ast::Expr> {
1148 let mut folder = Duplicator;
1149 folder.fold_expr(*self)