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.
15 use codemap::{Span, respan, dummy_sp};
16 use ext::base::ExtCtxt;
17 use ext::quote::rt::*;
27 // Transitional reexports so qquote can find the paths it is looking for
33 pub trait AstBuilder {
35 fn path(&self, span: Span, strs: ~[ast::Ident]) -> ast::Path;
36 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
37 fn path_global(&self, span: Span, strs: ~[ast::Ident]) -> ast::Path;
38 fn path_all(&self, sp: Span,
40 idents: ~[ast::Ident],
41 rp: Option<ast::Lifetime>,
46 fn ty_mt(&self, ty: ast::Ty, mutbl: ast::Mutability) -> ast::mt;
48 fn ty(&self, span: Span, ty: ast::ty_) -> ast::Ty;
49 fn ty_path(&self, ast::Path, Option<OptVec<ast::TyParamBound>>) -> ast::Ty;
50 fn ty_ident(&self, span: Span, idents: ast::Ident) -> ast::Ty;
52 fn ty_rptr(&self, span: Span,
54 lifetime: Option<ast::Lifetime>,
55 mutbl: ast::Mutability) -> ast::Ty;
56 fn ty_uniq(&self, span: Span, ty: ast::Ty) -> ast::Ty;
57 fn ty_box(&self, span: Span, ty: ast::Ty, mutbl: ast::Mutability) -> ast::Ty;
59 fn ty_option(&self, ty: ast::Ty) -> ast::Ty;
60 fn ty_infer(&self, sp: Span) -> ast::Ty;
61 fn ty_nil(&self) -> ast::Ty;
63 fn ty_vars(&self, ty_params: &OptVec<ast::TyParam>) -> ~[ast::Ty];
64 fn ty_vars_global(&self, ty_params: &OptVec<ast::TyParam>) -> ~[ast::Ty];
65 fn ty_field_imm(&self, span: Span, name: Ident, ty: ast::Ty) -> ast::TypeField;
66 fn strip_bounds(&self, bounds: &Generics) -> Generics;
68 fn typaram(&self, id: ast::Ident, bounds: OptVec<ast::TyParamBound>) -> ast::TyParam;
70 fn trait_ref(&self, path: ast::Path) -> ast::trait_ref;
71 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
72 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime;
75 fn stmt_expr(&self, expr: @ast::Expr) -> @ast::Stmt;
76 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: @ast::Expr) -> @ast::Stmt;
77 fn stmt_let_typed(&self,
86 fn block(&self, span: Span, stmts: ~[@ast::Stmt], expr: Option<@ast::Expr>) -> ast::Block;
87 fn block_expr(&self, expr: @ast::Expr) -> ast::Block;
88 fn block_all(&self, span: Span,
89 view_items: ~[ast::view_item],
91 expr: Option<@ast::Expr>) -> ast::Block;
94 fn expr(&self, span: Span, node: ast::Expr_) -> @ast::Expr;
95 fn expr_path(&self, path: ast::Path) -> @ast::Expr;
96 fn expr_ident(&self, span: Span, id: ast::Ident) -> @ast::Expr;
98 fn expr_self(&self, span: Span) -> @ast::Expr;
99 fn expr_binary(&self, sp: Span, op: ast::BinOp,
100 lhs: @ast::Expr, rhs: @ast::Expr) -> @ast::Expr;
101 fn expr_deref(&self, sp: Span, e: @ast::Expr) -> @ast::Expr;
102 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: @ast::Expr) -> @ast::Expr;
104 fn expr_managed(&self, sp: Span, e: @ast::Expr) -> @ast::Expr;
105 fn expr_addr_of(&self, sp: Span, e: @ast::Expr) -> @ast::Expr;
106 fn expr_mut_addr_of(&self, sp: Span, e: @ast::Expr) -> @ast::Expr;
107 fn expr_field_access(&self, span: Span, expr: @ast::Expr, ident: ast::Ident) -> @ast::Expr;
108 fn expr_call(&self, span: Span, expr: @ast::Expr, args: ~[@ast::Expr]) -> @ast::Expr;
109 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: ~[@ast::Expr]) -> @ast::Expr;
110 fn expr_call_global(&self, sp: Span, fn_path: ~[ast::Ident],
111 args: ~[@ast::Expr]) -> @ast::Expr;
112 fn expr_method_call(&self, span: Span,
113 expr: @ast::Expr, ident: ast::Ident,
114 args: ~[@ast::Expr]) -> @ast::Expr;
115 fn expr_block(&self, b: ast::Block) -> @ast::Expr;
116 fn expr_cast(&self, sp: Span, expr: @ast::Expr, ty: ast::Ty) -> @ast::Expr;
118 fn field_imm(&self, span: Span, name: Ident, e: @ast::Expr) -> ast::Field;
119 fn expr_struct(&self, span: Span, path: ast::Path, fields: ~[ast::Field]) -> @ast::Expr;
120 fn expr_struct_ident(&self, span: Span, id: ast::Ident, fields: ~[ast::Field]) -> @ast::Expr;
122 fn expr_lit(&self, sp: Span, lit: ast::lit_) -> @ast::Expr;
124 fn expr_uint(&self, span: Span, i: uint) -> @ast::Expr;
125 fn expr_int(&self, sp: Span, i: int) -> @ast::Expr;
126 fn expr_u8(&self, sp: Span, u: u8) -> @ast::Expr;
127 fn expr_bool(&self, sp: Span, value: bool) -> @ast::Expr;
129 fn expr_vstore(&self, sp: Span, expr: @ast::Expr, vst: ast::ExprVstore) -> @ast::Expr;
130 fn expr_vec(&self, sp: Span, exprs: ~[@ast::Expr]) -> @ast::Expr;
131 fn expr_vec_uniq(&self, sp: Span, exprs: ~[@ast::Expr]) -> @ast::Expr;
132 fn expr_vec_slice(&self, sp: Span, exprs: ~[@ast::Expr]) -> @ast::Expr;
133 fn expr_str(&self, sp: Span, s: @str) -> @ast::Expr;
134 fn expr_str_uniq(&self, sp: Span, s: @str) -> @ast::Expr;
136 fn expr_some(&self, sp: Span, expr: @ast::Expr) -> @ast::Expr;
137 fn expr_none(&self, sp: Span) -> @ast::Expr;
139 fn expr_fail(&self, span: Span, msg: @str) -> @ast::Expr;
140 fn expr_unreachable(&self, span: Span) -> @ast::Expr;
142 fn pat(&self, span: Span, pat: ast::Pat_) -> @ast::Pat;
143 fn pat_wild(&self, span: Span) -> @ast::Pat;
144 fn pat_lit(&self, span: Span, expr: @ast::Expr) -> @ast::Pat;
145 fn pat_ident(&self, span: Span, ident: ast::Ident) -> @ast::Pat;
147 fn pat_ident_binding_mode(&self,
150 bm: ast::BindingMode) -> @ast::Pat;
151 fn pat_enum(&self, span: Span, path: ast::Path, subpats: ~[@ast::Pat]) -> @ast::Pat;
152 fn pat_struct(&self, span: Span,
153 path: ast::Path, field_pats: ~[ast::FieldPat]) -> @ast::Pat;
155 fn arm(&self, span: Span, pats: ~[@ast::Pat], expr: @ast::Expr) -> ast::Arm;
156 fn arm_unreachable(&self, span: Span) -> ast::Arm;
158 fn expr_match(&self, span: Span, arg: @ast::Expr, arms: ~[ast::Arm]) -> @ast::Expr;
159 fn expr_if(&self, span: Span,
160 cond: @ast::Expr, then: @ast::Expr, els: Option<@ast::Expr>) -> @ast::Expr;
162 fn lambda_fn_decl(&self, span: Span, fn_decl: ast::fn_decl, blk: ast::Block) -> @ast::Expr;
164 fn lambda(&self, span: Span, ids: ~[ast::Ident], blk: ast::Block) -> @ast::Expr;
165 fn lambda0(&self, span: Span, blk: ast::Block) -> @ast::Expr;
166 fn lambda1(&self, span: Span, blk: ast::Block, ident: ast::Ident) -> @ast::Expr;
168 fn lambda_expr(&self, span: Span, ids: ~[ast::Ident], blk: @ast::Expr) -> @ast::Expr;
169 fn lambda_expr_0(&self, span: Span, expr: @ast::Expr) -> @ast::Expr;
170 fn lambda_expr_1(&self, span: Span, expr: @ast::Expr, ident: ast::Ident) -> @ast::Expr;
172 fn lambda_stmts(&self, span: Span, ids: ~[ast::Ident], blk: ~[@ast::Stmt]) -> @ast::Expr;
173 fn lambda_stmts_0(&self, span: Span, stmts: ~[@ast::Stmt]) -> @ast::Expr;
174 fn lambda_stmts_1(&self, span: Span, stmts: ~[@ast::Stmt], ident: ast::Ident) -> @ast::Expr;
177 fn item(&self, span: Span,
178 name: Ident, attrs: ~[ast::Attribute], node: ast::item_) -> @ast::item;
180 fn arg(&self, span: Span, name: Ident, ty: ast::Ty) -> ast::arg;
182 fn fn_decl(&self, inputs: ~[ast::arg], output: ast::Ty) -> ast::fn_decl;
184 fn item_fn_poly(&self,
190 body: ast::Block) -> @ast::item;
196 body: ast::Block) -> @ast::item;
198 fn variant(&self, span: Span, name: Ident, tys: ~[ast::Ty]) -> ast::variant;
199 fn item_enum_poly(&self,
202 enum_definition: ast::enum_def,
203 generics: Generics) -> @ast::item;
204 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::enum_def) -> @ast::item;
206 fn item_struct_poly(&self,
209 struct_def: ast::struct_def,
210 generics: Generics) -> @ast::item;
211 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::struct_def) -> @ast::item;
213 fn item_mod(&self, span: Span,
214 name: Ident, attrs: ~[ast::Attribute],
215 vi: ~[ast::view_item], items: ~[@ast::item]) -> @ast::item;
217 fn item_ty_poly(&self,
221 generics: Generics) -> @ast::item;
222 fn item_ty(&self, span: Span, name: Ident, ty: ast::Ty) -> @ast::item;
224 fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute;
226 fn meta_word(&self, sp: Span, w: @str) -> @ast::MetaItem;
227 fn meta_list(&self, sp: Span, name: @str, mis: ~[@ast::MetaItem]) -> @ast::MetaItem;
228 fn meta_name_value(&self, sp: Span, name: @str, value: ast::lit_) -> @ast::MetaItem;
230 fn view_use(&self, sp: Span,
231 vis: ast::visibility, vp: ~[@ast::view_path]) -> ast::view_item;
232 fn view_use_list(&self, sp: Span, vis: ast::visibility,
233 path: ~[ast::Ident], imports: &[ast::Ident]) -> ast::view_item;
234 fn view_use_glob(&self, sp: Span,
235 vis: ast::visibility, path: ~[ast::Ident]) -> ast::view_item;
238 impl AstBuilder for @ExtCtxt {
239 fn path(&self, span: Span, strs: ~[ast::Ident]) -> ast::Path {
240 self.path_all(span, false, strs, None, ~[])
242 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
243 self.path(span, ~[id])
245 fn path_global(&self, span: Span, strs: ~[ast::Ident]) -> ast::Path {
246 self.path_all(span, true, strs, None, ~[])
251 mut idents: ~[ast::Ident],
252 rp: Option<ast::Lifetime>,
255 let last_identifier = idents.pop();
256 let mut segments: ~[ast::PathSegment] = idents.move_iter()
261 types: opt_vec::Empty,
264 segments.push(ast::PathSegment {
265 identifier: last_identifier,
267 types: opt_vec::from(types),
276 fn ty_mt(&self, ty: ast::Ty, mutbl: ast::Mutability) -> ast::mt {
283 fn ty(&self, span: Span, ty: ast::ty_) -> ast::Ty {
285 id: ast::DUMMY_NODE_ID,
291 fn ty_path(&self, path: ast::Path, bounds: Option<OptVec<ast::TyParamBound>>)
294 ast::ty_path(path, bounds, ast::DUMMY_NODE_ID))
297 // Might need to take bounds as an argument in the future, if you ever want
298 // to generate a bounded existential trait type.
299 fn ty_ident(&self, span: Span, ident: ast::Ident)
301 self.ty_path(self.path_ident(span, ident), None)
307 lifetime: Option<ast::Lifetime>,
308 mutbl: ast::Mutability)
311 ast::ty_rptr(lifetime, self.ty_mt(ty, mutbl)))
313 fn ty_uniq(&self, span: Span, ty: ast::Ty) -> ast::Ty {
314 self.ty(span, ast::ty_uniq(self.ty_mt(ty, ast::MutImmutable)))
316 fn ty_box(&self, span: Span,
317 ty: ast::Ty, mutbl: ast::Mutability) -> ast::Ty {
318 self.ty(span, ast::ty_box(self.ty_mt(ty, mutbl)))
321 fn ty_option(&self, ty: ast::Ty) -> ast::Ty {
323 self.path_all(dummy_sp(),
326 self.ident_of("std"),
327 self.ident_of("option"),
328 self.ident_of("Option")
334 fn ty_field_imm(&self, span: Span, name: Ident, ty: ast::Ty) -> ast::TypeField {
337 mt: ast::mt { ty: ~ty, mutbl: ast::MutImmutable },
342 fn ty_infer(&self, span: Span) -> ast::Ty {
343 self.ty(span, ast::ty_infer)
346 fn ty_nil(&self) -> ast::Ty {
348 id: ast::DUMMY_NODE_ID,
354 fn typaram(&self, id: ast::Ident, bounds: OptVec<ast::TyParamBound>) -> ast::TyParam {
355 ast::TyParam { ident: id, id: ast::DUMMY_NODE_ID, bounds: bounds }
358 // these are strange, and probably shouldn't be used outside of
359 // pipes. Specifically, the global version possible generates
361 fn ty_vars(&self, ty_params: &OptVec<ast::TyParam>) -> ~[ast::Ty] {
363 ty_params.map(|p| self.ty_ident(dummy_sp(), p.ident)))
366 fn ty_vars_global(&self, ty_params: &OptVec<ast::TyParam>) -> ~[ast::Ty] {
368 ty_params.map(|p| self.ty_path(
369 self.path_global(dummy_sp(), ~[p.ident]), None)))
372 fn strip_bounds(&self, generics: &Generics) -> Generics {
373 let new_params = do generics.ty_params.map |ty_param| {
374 ast::TyParam { bounds: opt_vec::Empty, ..*ty_param }
377 ty_params: new_params,
378 .. (*generics).clone()
382 fn trait_ref(&self, path: ast::Path) -> ast::trait_ref {
385 ref_id: ast::DUMMY_NODE_ID
389 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
390 ast::TraitTyParamBound(self.trait_ref(path))
393 fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
394 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, ident: ident }
397 fn stmt_expr(&self, expr: @ast::Expr) -> @ast::Stmt {
398 @respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
401 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: @ast::Expr) -> @ast::Stmt {
403 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
405 self.pat_ident(sp, ident)
407 let local = @ast::Local {
408 ty: self.ty_infer(sp),
411 id: ast::DUMMY_NODE_ID,
414 let decl = respan(sp, ast::DeclLocal(local));
415 @respan(sp, ast::StmtDecl(@decl, ast::DUMMY_NODE_ID))
418 fn stmt_let_typed(&self,
426 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
428 self.pat_ident(sp, ident)
430 let local = @ast::Local {
434 id: ast::DUMMY_NODE_ID,
437 let decl = respan(sp, ast::DeclLocal(local));
438 @respan(sp, ast::StmtDecl(@decl, ast::DUMMY_NODE_ID))
441 fn block(&self, span: Span, stmts: ~[@ast::Stmt], expr: Option<@Expr>) -> ast::Block {
442 self.block_all(span, ~[], stmts, expr)
445 fn block_expr(&self, expr: @ast::Expr) -> ast::Block {
446 self.block_all(expr.span, ~[], ~[], Some(expr))
450 view_items: ~[ast::view_item],
451 stmts: ~[@ast::Stmt],
452 expr: Option<@ast::Expr>) -> ast::Block {
454 view_items: view_items,
457 id: ast::DUMMY_NODE_ID,
458 rules: ast::DefaultBlock,
463 fn expr(&self, span: Span, node: ast::Expr_) -> @ast::Expr {
465 id: ast::DUMMY_NODE_ID,
471 fn expr_path(&self, path: ast::Path) -> @ast::Expr {
472 self.expr(path.span, ast::ExprPath(path))
475 fn expr_ident(&self, span: Span, id: ast::Ident) -> @ast::Expr {
476 self.expr_path(self.path_ident(span, id))
478 fn expr_self(&self, span: Span) -> @ast::Expr {
479 self.expr(span, ast::ExprSelf)
482 fn expr_binary(&self, sp: Span, op: ast::BinOp,
483 lhs: @ast::Expr, rhs: @ast::Expr) -> @ast::Expr {
484 self.expr(sp, ast::ExprBinary(ast::DUMMY_NODE_ID, op, lhs, rhs))
487 fn expr_deref(&self, sp: Span, e: @ast::Expr) -> @ast::Expr {
488 self.expr_unary(sp, ast::UnDeref, e)
490 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: @ast::Expr)
492 self.expr(sp, ast::ExprUnary(ast::DUMMY_NODE_ID, op, e))
495 fn expr_managed(&self, sp: Span, e: @ast::Expr) -> @ast::Expr {
496 self.expr_unary(sp, ast::UnBox(ast::MutImmutable), e)
499 fn expr_field_access(&self, sp: Span, expr: @ast::Expr, ident: ast::Ident) -> @ast::Expr {
500 self.expr(sp, ast::ExprField(expr, ident, ~[]))
502 fn expr_addr_of(&self, sp: Span, e: @ast::Expr) -> @ast::Expr {
503 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
505 fn expr_mut_addr_of(&self, sp: Span, e: @ast::Expr) -> @ast::Expr {
506 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
509 fn expr_call(&self, span: Span, expr: @ast::Expr, args: ~[@ast::Expr]) -> @ast::Expr {
510 self.expr(span, ast::ExprCall(expr, args, ast::NoSugar))
512 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: ~[@ast::Expr]) -> @ast::Expr {
514 ast::ExprCall(self.expr_ident(span, id), args, ast::NoSugar))
516 fn expr_call_global(&self, sp: Span, fn_path: ~[ast::Ident],
517 args: ~[@ast::Expr]) -> @ast::Expr {
518 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
519 self.expr_call(sp, pathexpr, args)
521 fn expr_method_call(&self, span: Span,
524 args: ~[@ast::Expr]) -> @ast::Expr {
526 ast::ExprMethodCall(ast::DUMMY_NODE_ID, expr, ident, ~[], args, ast::NoSugar))
528 fn expr_block(&self, b: ast::Block) -> @ast::Expr {
529 self.expr(b.span, ast::ExprBlock(b))
531 fn field_imm(&self, span: Span, name: Ident, e: @ast::Expr) -> ast::Field {
532 ast::Field { ident: name, expr: e, span: span }
534 fn expr_struct(&self, span: Span, path: ast::Path, fields: ~[ast::Field]) -> @ast::Expr {
535 self.expr(span, ast::ExprStruct(path, fields, None))
537 fn expr_struct_ident(&self, span: Span,
538 id: ast::Ident, fields: ~[ast::Field]) -> @ast::Expr {
539 self.expr_struct(span, self.path_ident(span, id), fields)
542 fn expr_lit(&self, sp: Span, lit: ast::lit_) -> @ast::Expr {
543 self.expr(sp, ast::ExprLit(@respan(sp, lit)))
545 fn expr_uint(&self, span: Span, i: uint) -> @ast::Expr {
546 self.expr_lit(span, ast::lit_uint(i as u64, ast::ty_u))
548 fn expr_int(&self, sp: Span, i: int) -> @ast::Expr {
549 self.expr_lit(sp, ast::lit_int(i as i64, ast::ty_i))
551 fn expr_u8(&self, sp: Span, u: u8) -> @ast::Expr {
552 self.expr_lit(sp, ast::lit_uint(u as u64, ast::ty_u8))
554 fn expr_bool(&self, sp: Span, value: bool) -> @ast::Expr {
555 self.expr_lit(sp, ast::lit_bool(value))
558 fn expr_vstore(&self, sp: Span, expr: @ast::Expr, vst: ast::ExprVstore) -> @ast::Expr {
559 self.expr(sp, ast::ExprVstore(expr, vst))
561 fn expr_vec(&self, sp: Span, exprs: ~[@ast::Expr]) -> @ast::Expr {
562 self.expr(sp, ast::ExprVec(exprs, ast::MutImmutable))
564 fn expr_vec_uniq(&self, sp: Span, exprs: ~[@ast::Expr]) -> @ast::Expr {
565 self.expr_vstore(sp, self.expr_vec(sp, exprs), ast::ExprVstoreUniq)
567 fn expr_vec_slice(&self, sp: Span, exprs: ~[@ast::Expr]) -> @ast::Expr {
568 self.expr_vstore(sp, self.expr_vec(sp, exprs), ast::ExprVstoreSlice)
570 fn expr_str(&self, sp: Span, s: @str) -> @ast::Expr {
571 self.expr_lit(sp, ast::lit_str(s, ast::CookedStr))
573 fn expr_str_uniq(&self, sp: Span, s: @str) -> @ast::Expr {
574 self.expr_vstore(sp, self.expr_str(sp, s), ast::ExprVstoreUniq)
578 fn expr_cast(&self, sp: Span, expr: @ast::Expr, ty: ast::Ty) -> @ast::Expr {
579 self.expr(sp, ast::ExprCast(expr, ty))
583 fn expr_some(&self, sp: Span, expr: @ast::Expr) -> @ast::Expr {
585 self.ident_of("std"),
586 self.ident_of("option"),
587 self.ident_of("Some"),
589 self.expr_call_global(sp, some, ~[expr])
592 fn expr_none(&self, sp: Span) -> @ast::Expr {
593 let none = self.path_global(sp, ~[
594 self.ident_of("std"),
595 self.ident_of("option"),
596 self.ident_of("None"),
601 fn expr_fail(&self, span: Span, msg: @str) -> @ast::Expr {
602 let loc = self.codemap().lookup_char_pos(span.lo);
603 self.expr_call_global(
606 self.ident_of("std"),
607 self.ident_of("sys"),
608 self.ident_of("FailWithCause"),
609 self.ident_of("fail_with"),
612 self.expr_str(span, msg),
613 self.expr_str(span, loc.file.name),
614 self.expr_uint(span, loc.line),
618 fn expr_unreachable(&self, span: Span) -> @ast::Expr {
619 self.expr_fail(span, @"internal error: entered unreachable code")
623 fn pat(&self, span: Span, pat: ast::Pat_) -> @ast::Pat {
624 @ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }
626 fn pat_wild(&self, span: Span) -> @ast::Pat {
627 self.pat(span, ast::PatWild)
629 fn pat_lit(&self, span: Span, expr: @ast::Expr) -> @ast::Pat {
630 self.pat(span, ast::PatLit(expr))
632 fn pat_ident(&self, span: Span, ident: ast::Ident) -> @ast::Pat {
633 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
636 fn pat_ident_binding_mode(&self,
639 bm: ast::BindingMode) -> @ast::Pat {
640 let path = self.path_ident(span, ident);
641 let pat = ast::PatIdent(bm, path, None);
644 fn pat_enum(&self, span: Span, path: ast::Path, subpats: ~[@ast::Pat]) -> @ast::Pat {
645 let pat = ast::PatEnum(path, Some(subpats));
648 fn pat_struct(&self, span: Span,
649 path: ast::Path, field_pats: ~[ast::FieldPat]) -> @ast::Pat {
650 let pat = ast::PatStruct(path, field_pats, false);
654 fn arm(&self, _span: Span, pats: ~[@ast::Pat], expr: @ast::Expr) -> ast::Arm {
658 body: self.block_expr(expr)
662 fn arm_unreachable(&self, span: Span) -> ast::Arm {
663 self.arm(span, ~[self.pat_wild(span)], self.expr_unreachable(span))
666 fn expr_match(&self, span: Span, arg: @ast::Expr, arms: ~[ast::Arm]) -> @Expr {
667 self.expr(span, ast::ExprMatch(arg, arms))
670 fn expr_if(&self, span: Span,
671 cond: @ast::Expr, then: @ast::Expr, els: Option<@ast::Expr>) -> @ast::Expr {
672 let els = els.map(|x| self.expr_block(self.block_expr(x)));
673 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
676 fn lambda_fn_decl(&self, span: Span, fn_decl: ast::fn_decl, blk: ast::Block) -> @ast::Expr {
677 self.expr(span, ast::ExprFnBlock(fn_decl, blk))
679 fn lambda(&self, span: Span, ids: ~[ast::Ident], blk: ast::Block) -> @ast::Expr {
680 let fn_decl = self.fn_decl(
681 ids.map(|id| self.arg(span, *id, self.ty_infer(span))),
682 self.ty_infer(span));
684 self.expr(span, ast::ExprFnBlock(fn_decl, blk))
686 fn lambda0(&self, _span: Span, blk: ast::Block) -> @ast::Expr {
687 let blk_e = self.expr(blk.span, ast::ExprBlock(blk.clone()));
688 quote_expr!(*self, || $blk_e )
691 fn lambda1(&self, _span: Span, blk: ast::Block, ident: ast::Ident) -> @ast::Expr {
692 let blk_e = self.expr(blk.span, ast::ExprBlock(blk.clone()));
693 quote_expr!(*self, |$ident| $blk_e )
696 fn lambda_expr(&self, span: Span, ids: ~[ast::Ident], expr: @ast::Expr) -> @ast::Expr {
697 self.lambda(span, ids, self.block_expr(expr))
699 fn lambda_expr_0(&self, span: Span, expr: @ast::Expr) -> @ast::Expr {
700 self.lambda0(span, self.block_expr(expr))
702 fn lambda_expr_1(&self, span: Span, expr: @ast::Expr, ident: ast::Ident) -> @ast::Expr {
703 self.lambda1(span, self.block_expr(expr), ident)
706 fn lambda_stmts(&self, span: Span, ids: ~[ast::Ident], stmts: ~[@ast::Stmt]) -> @ast::Expr {
707 self.lambda(span, ids, self.block(span, stmts, None))
709 fn lambda_stmts_0(&self, span: Span, stmts: ~[@ast::Stmt]) -> @ast::Expr {
710 self.lambda0(span, self.block(span, stmts, None))
712 fn lambda_stmts_1(&self, span: Span, stmts: ~[@ast::Stmt], ident: ast::Ident) -> @ast::Expr {
713 self.lambda1(span, self.block(span, stmts, None), ident)
716 fn arg(&self, span: Span, ident: ast::Ident, ty: ast::Ty) -> ast::arg {
717 let arg_pat = self.pat_ident(span, ident);
721 id: ast::DUMMY_NODE_ID
726 fn fn_decl(&self, inputs: ~[ast::arg], output: ast::Ty) -> ast::fn_decl {
734 fn item(&self, span: Span,
735 name: Ident, attrs: ~[ast::Attribute], node: ast::item_) -> @ast::item {
736 // XXX: Would be nice if our generated code didn't violate
737 // Rust coding conventions
738 @ast::item { ident: name,
740 id: ast::DUMMY_NODE_ID,
746 fn item_fn_poly(&self,
752 body: ast::Block) -> @ast::item {
756 ast::item_fn(self.fn_decl(inputs, output),
775 ast_util::empty_generics(),
779 fn variant(&self, span: Span, name: Ident, tys: ~[ast::Ty]) -> ast::variant {
780 let args = tys.move_iter().map(|ty| {
781 ast::variant_arg { ty: ty, id: ast::DUMMY_NODE_ID }
788 kind: ast::tuple_variant_kind(args),
789 id: ast::DUMMY_NODE_ID,
795 fn item_enum_poly(&self, span: Span, name: Ident,
796 enum_definition: ast::enum_def,
797 generics: Generics) -> @ast::item {
798 self.item(span, name, ~[], ast::item_enum(enum_definition, generics))
801 fn item_enum(&self, span: Span, name: Ident,
802 enum_definition: ast::enum_def) -> @ast::item {
803 self.item_enum_poly(span, name, enum_definition,
804 ast_util::empty_generics())
811 struct_def: ast::struct_def
813 self.item_struct_poly(
817 ast_util::empty_generics()
825 struct_def: ast::struct_def,
828 self.item(span, name, ~[], ast::item_struct(@struct_def, generics))
831 fn item_mod(&self, span: Span, name: Ident,
832 attrs: ~[ast::Attribute],
833 vi: ~[ast::view_item],
834 items: ~[@ast::item]) -> @ast::item {
839 ast::item_mod(ast::_mod {
846 fn item_ty_poly(&self, span: Span, name: Ident, ty: ast::Ty,
847 generics: Generics) -> @ast::item {
848 self.item(span, name, ~[], ast::item_ty(ty, generics))
851 fn item_ty(&self, span: Span, name: Ident, ty: ast::Ty) -> @ast::item {
852 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
855 fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute {
856 respan(sp, ast::Attribute_ {
857 style: ast::AttrOuter,
859 is_sugared_doc: false,
863 fn meta_word(&self, sp: Span, w: @str) -> @ast::MetaItem {
864 @respan(sp, ast::MetaWord(w))
866 fn meta_list(&self, sp: Span, name: @str, mis: ~[@ast::MetaItem]) -> @ast::MetaItem {
867 @respan(sp, ast::MetaList(name, mis))
869 fn meta_name_value(&self, sp: Span, name: @str, value: ast::lit_) -> @ast::MetaItem {
870 @respan(sp, ast::MetaNameValue(name, respan(sp, value)))
873 fn view_use(&self, sp: Span,
874 vis: ast::visibility, vp: ~[@ast::view_path]) -> ast::view_item {
876 node: ast::view_item_use(vp),
883 fn view_use_list(&self, sp: Span, vis: ast::visibility,
884 path: ~[ast::Ident], imports: &[ast::Ident]) -> ast::view_item {
885 let imports = do imports.map |id| {
886 respan(sp, ast::path_list_ident_ { name: *id, id: ast::DUMMY_NODE_ID })
889 self.view_use(sp, vis,
891 ast::view_path_list(self.path(sp, path),
893 ast::DUMMY_NODE_ID))])
896 fn view_use_glob(&self, sp: Span,
897 vis: ast::visibility, path: ~[ast::Ident]) -> ast::view_item {
898 self.view_use(sp, vis,
900 ast::view_path_glob(self.path(sp, path), ast::DUMMY_NODE_ID))])
908 impl fold::ast_fold for Duplicator {
909 fn new_id(&self, _: NodeId) -> NodeId {
914 pub trait Duplicate {
916 // Duplication functions
918 // These functions just duplicate AST nodes.
921 fn duplicate(&self, cx: @ExtCtxt) -> Self;
924 impl Duplicate for @ast::Expr {
925 fn duplicate(&self, cx: @ExtCtxt) -> @ast::Expr {
926 let folder = @Duplicator {
928 } as @fold::ast_fold;
929 folder.fold_expr(*self)