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_ptr(&self, span: Span,
57 mutbl: ast::Mutability) -> P<ast::Ty>;
58 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty>;
60 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
61 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
62 fn ty_nil(&self) -> P<ast::Ty>;
64 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
65 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
66 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField;
67 fn strip_bounds(&self, bounds: &Generics) -> Generics;
72 bounds: OwnedSlice<ast::TyParamBound>,
73 unbound: Option<ast::TyParamBound>,
74 default: Option<P<ast::Ty>>) -> ast::TyParam;
76 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
77 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
78 fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
79 fn lifetime_def(&self,
82 bounds: Vec<ast::Lifetime>)
86 fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt>;
87 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
88 ex: Gc<ast::Expr>) -> Gc<ast::Stmt>;
89 fn stmt_let_typed(&self,
96 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt>;
99 fn block(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
100 expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
101 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block>;
102 fn block_all(&self, span: Span,
103 view_items: Vec<ast::ViewItem> ,
104 stmts: Vec<Gc<ast::Stmt>> ,
105 expr: Option<Gc<ast::Expr>>) -> P<ast::Block>;
108 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr>;
109 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr>;
110 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr>;
112 fn expr_self(&self, span: Span) -> Gc<ast::Expr>;
113 fn expr_binary(&self, sp: Span, op: ast::BinOp,
114 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr>;
115 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
116 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
118 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
119 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
120 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr>;
121 fn expr_field_access(&self, span: Span, expr: Gc<ast::Expr>,
122 ident: ast::Ident) -> Gc<ast::Expr>;
123 fn expr_tup_field_access(&self, sp: Span, expr: Gc<ast::Expr>,
124 idx: uint) -> Gc<ast::Expr>;
125 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
126 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
127 fn expr_call_ident(&self, span: Span, id: ast::Ident,
128 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
129 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
130 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
131 fn expr_method_call(&self, span: Span,
132 expr: Gc<ast::Expr>, ident: ast::Ident,
133 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
134 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr>;
135 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>,
136 ty: P<ast::Ty>) -> Gc<ast::Expr>;
138 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field;
139 fn expr_struct(&self, span: Span, path: ast::Path,
140 fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
141 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
142 fields: Vec<ast::Field> ) -> Gc<ast::Expr>;
144 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr>;
146 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr>;
147 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr>;
148 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr>;
149 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr>;
151 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
152 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr>;
153 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr>;
154 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr>;
156 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
157 fn expr_none(&self, sp: Span) -> Gc<ast::Expr>;
159 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr>;
161 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr>;
162 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr>;
164 fn expr_ok(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
165 fn expr_err(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
166 fn expr_try(&self, span: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr>;
168 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat>;
169 fn pat_wild(&self, span: Span) -> Gc<ast::Pat>;
170 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat>;
171 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat>;
173 fn pat_ident_binding_mode(&self,
176 bm: ast::BindingMode) -> Gc<ast::Pat>;
177 fn pat_enum(&self, span: Span, path: ast::Path,
178 subpats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
179 fn pat_struct(&self, span: Span,
180 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat>;
181 fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat>;
183 fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
184 fn pat_none(&self, span: Span) -> Gc<ast::Pat>;
186 fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
187 fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat>;
189 fn arm(&self, span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm;
190 fn arm_unreachable(&self, span: Span) -> ast::Arm;
192 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>, arms: Vec<ast::Arm> ) -> Gc<ast::Expr>;
193 fn expr_if(&self, span: Span,
194 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
195 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr>;
196 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr>;
198 fn lambda_fn_decl(&self, span: Span,
199 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr>;
201 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr>;
202 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr>;
203 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr>;
205 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: Gc<ast::Expr>) -> Gc<ast::Expr>;
206 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr>;
207 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr>;
209 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
210 blk: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
211 fn lambda_stmts_0(&self, span: Span,
212 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr>;
213 fn lambda_stmts_1(&self, span: Span,
214 stmts: Vec<Gc<ast::Stmt>>, ident: ast::Ident) -> Gc<ast::Expr>;
217 fn item(&self, span: Span,
218 name: Ident, attrs: Vec<ast::Attribute>,
219 node: ast::Item_) -> Gc<ast::Item>;
221 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
223 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
225 fn item_fn_poly(&self,
228 inputs: Vec<ast::Arg> ,
231 body: P<ast::Block>) -> Gc<ast::Item>;
235 inputs: Vec<ast::Arg> ,
237 body: P<ast::Block>) -> Gc<ast::Item>;
239 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
240 fn item_enum_poly(&self,
243 enum_definition: ast::EnumDef,
244 generics: Generics) -> Gc<ast::Item>;
245 fn item_enum(&self, span: Span, name: Ident,
246 enum_def: ast::EnumDef) -> Gc<ast::Item>;
248 fn item_struct_poly(&self,
251 struct_def: ast::StructDef,
252 generics: Generics) -> Gc<ast::Item>;
253 fn item_struct(&self, span: Span, name: Ident,
254 struct_def: ast::StructDef) -> Gc<ast::Item>;
256 fn item_mod(&self, span: Span, inner_span: Span,
257 name: Ident, attrs: Vec<ast::Attribute>,
258 vi: Vec<ast::ViewItem>,
259 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item>;
261 fn item_static(&self,
265 mutbl: ast::Mutability,
269 fn item_ty_poly(&self,
273 generics: Generics) -> Gc<ast::Item>;
274 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item>;
276 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute;
278 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem>;
281 name: InternedString,
282 mis: Vec<Gc<ast::MetaItem>>)
283 -> Gc<ast::MetaItem>;
284 fn meta_name_value(&self,
286 name: InternedString,
288 -> Gc<ast::MetaItem>;
290 fn view_use(&self, sp: Span,
291 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem;
292 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem;
293 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
294 ident: ast::Ident, path: ast::Path) -> ast::ViewItem;
295 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
296 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem;
297 fn view_use_glob(&self, sp: Span,
298 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem;
301 impl<'a> AstBuilder for ExtCtxt<'a> {
302 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
303 self.path_all(span, false, strs, Vec::new(), Vec::new())
305 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
306 self.path(span, vec!(id))
308 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
309 self.path_all(span, true, strs, Vec::new(), Vec::new())
314 mut idents: Vec<ast::Ident> ,
315 lifetimes: Vec<ast::Lifetime>,
316 types: Vec<P<ast::Ty>> )
318 let last_identifier = idents.pop().unwrap();
319 let mut segments: Vec<ast::PathSegment> = idents.move_iter()
323 lifetimes: Vec::new(),
324 types: OwnedSlice::empty(),
327 segments.push(ast::PathSegment {
328 identifier: last_identifier,
329 lifetimes: lifetimes,
330 types: OwnedSlice::from_vec(types),
339 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
346 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
348 id: ast::DUMMY_NODE_ID,
354 fn ty_path(&self, path: ast::Path, bounds: Option<OwnedSlice<ast::TyParamBound>>)
357 ast::TyPath(path, bounds, ast::DUMMY_NODE_ID))
360 // Might need to take bounds as an argument in the future, if you ever want
361 // to generate a bounded existential trait type.
362 fn ty_ident(&self, span: Span, ident: ast::Ident)
364 self.ty_path(self.path_ident(span, ident), None)
370 lifetime: Option<ast::Lifetime>,
371 mutbl: ast::Mutability)
374 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
380 mutbl: ast::Mutability)
383 ast::TyPtr(self.ty_mt(ty, mutbl)))
385 fn ty_uniq(&self, span: Span, ty: P<ast::Ty>) -> P<ast::Ty> {
386 self.ty(span, ast::TyUniq(ty))
389 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
391 self.path_all(DUMMY_SP,
394 self.ident_of("std"),
395 self.ident_of("option"),
396 self.ident_of("Option")
402 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
405 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
410 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
411 self.ty(span, ast::TyInfer)
414 fn ty_nil(&self) -> P<ast::Ty> {
416 id: ast::DUMMY_NODE_ID,
425 bounds: OwnedSlice<ast::TyParamBound>,
426 unbound: Option<ast::TyParamBound>,
427 default: Option<P<ast::Ty>>) -> ast::TyParam {
430 id: ast::DUMMY_NODE_ID,
438 // these are strange, and probably shouldn't be used outside of
439 // pipes. Specifically, the global version possible generates
441 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
442 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
445 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
446 ty_params.iter().map(|p| self.ty_path(
447 self.path_global(DUMMY_SP, vec!(p.ident)), None)).collect()
450 fn strip_bounds(&self, generics: &Generics) -> Generics {
451 let new_params = generics.ty_params.map(|ty_param| {
452 ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
455 ty_params: new_params,
456 .. (*generics).clone()
460 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
463 ref_id: ast::DUMMY_NODE_ID
467 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
468 ast::TraitTyParamBound(self.trait_ref(path))
471 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
472 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
475 fn lifetime_def(&self,
478 bounds: Vec<ast::Lifetime>)
479 -> ast::LifetimeDef {
481 lifetime: self.lifetime(span, name),
486 fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt> {
487 box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
490 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
491 ex: Gc<ast::Expr>) -> Gc<ast::Stmt> {
493 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
495 self.pat_ident(sp, ident)
497 let local = box(GC) ast::Local {
498 ty: self.ty_infer(sp),
501 id: ast::DUMMY_NODE_ID,
503 source: ast::LocalLet,
505 let decl = respan(sp, ast::DeclLocal(local));
506 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
509 fn stmt_let_typed(&self,
517 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
519 self.pat_ident(sp, ident)
521 let local = box(GC) ast::Local {
525 id: ast::DUMMY_NODE_ID,
527 source: ast::LocalLet,
529 let decl = respan(sp, ast::DeclLocal(local));
530 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
535 stmts: Vec<Gc<ast::Stmt>>,
536 expr: Option<Gc<Expr>>)
538 self.block_all(span, Vec::new(), stmts, expr)
541 fn stmt_item(&self, sp: Span, item: Gc<ast::Item>) -> Gc<ast::Stmt> {
542 let decl = respan(sp, ast::DeclItem(item));
543 box(GC) respan(sp, ast::StmtDecl(box(GC) decl, ast::DUMMY_NODE_ID))
546 fn block_expr(&self, expr: Gc<ast::Expr>) -> P<ast::Block> {
547 self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr))
551 view_items: Vec<ast::ViewItem> ,
552 stmts: Vec<Gc<ast::Stmt>>,
553 expr: Option<Gc<ast::Expr>>) -> P<ast::Block> {
555 view_items: view_items,
558 id: ast::DUMMY_NODE_ID,
559 rules: ast::DefaultBlock,
564 fn expr(&self, span: Span, node: ast::Expr_) -> Gc<ast::Expr> {
566 id: ast::DUMMY_NODE_ID,
572 fn expr_path(&self, path: ast::Path) -> Gc<ast::Expr> {
573 self.expr(path.span, ast::ExprPath(path))
576 fn expr_ident(&self, span: Span, id: ast::Ident) -> Gc<ast::Expr> {
577 self.expr_path(self.path_ident(span, id))
579 fn expr_self(&self, span: Span) -> Gc<ast::Expr> {
580 self.expr_ident(span, special_idents::self_)
583 fn expr_binary(&self, sp: Span, op: ast::BinOp,
584 lhs: Gc<ast::Expr>, rhs: Gc<ast::Expr>) -> Gc<ast::Expr> {
585 self.expr(sp, ast::ExprBinary(op, lhs, rhs))
588 fn expr_deref(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
589 self.expr_unary(sp, ast::UnDeref, e)
591 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
592 self.expr(sp, ast::ExprUnary(op, e))
595 fn expr_managed(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
596 self.expr_unary(sp, ast::UnBox, e)
599 fn expr_field_access(&self, sp: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
600 let field_name = token::get_ident(ident);
601 let field_span = Span {
602 lo: sp.lo - Pos::from_uint(field_name.get().len()),
604 expn_info: sp.expn_info,
607 let id = Spanned { node: ident, span: field_span };
608 self.expr(sp, ast::ExprField(expr, id, Vec::new()))
610 fn expr_tup_field_access(&self, sp: Span, expr: Gc<ast::Expr>, idx: uint) -> Gc<ast::Expr> {
611 let field_span = Span {
612 lo: sp.lo - Pos::from_uint(idx.to_string().len()),
614 expn_info: sp.expn_info,
617 let id = Spanned { node: idx, span: field_span };
618 self.expr(sp, ast::ExprTupField(expr, id, Vec::new()))
620 fn expr_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
621 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
623 fn expr_mut_addr_of(&self, sp: Span, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
624 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
627 fn expr_call(&self, span: Span, expr: Gc<ast::Expr>,
628 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
629 self.expr(span, ast::ExprCall(expr, args))
631 fn expr_call_ident(&self, span: Span, id: ast::Ident,
632 args: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
633 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
635 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
636 args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
637 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
638 self.expr_call(sp, pathexpr, args)
640 fn expr_method_call(&self, span: Span,
643 mut args: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
644 let id = Spanned { node: ident, span: span };
646 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
648 fn expr_block(&self, b: P<ast::Block>) -> Gc<ast::Expr> {
649 self.expr(b.span, ast::ExprBlock(b))
651 fn field_imm(&self, span: Span, name: Ident, e: Gc<ast::Expr>) -> ast::Field {
652 ast::Field { ident: respan(span, name), expr: e, span: span }
654 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
655 self.expr(span, ast::ExprStruct(path, fields, None))
657 fn expr_struct_ident(&self, span: Span,
658 id: ast::Ident, fields: Vec<ast::Field> ) -> Gc<ast::Expr> {
659 self.expr_struct(span, self.path_ident(span, id), fields)
662 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> Gc<ast::Expr> {
663 self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit)))
665 fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> {
666 self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyU)))
668 fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> {
669 self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyI, ast::Sign::new(i))))
671 fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> {
672 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
674 fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> {
675 self.expr_lit(sp, ast::LitBool(value))
678 fn expr_vec(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
679 self.expr(sp, ast::ExprVec(exprs))
681 fn expr_vec_ng(&self, sp: Span) -> Gc<ast::Expr> {
682 self.expr_call_global(sp,
683 vec!(self.ident_of("std"),
684 self.ident_of("vec"),
685 self.ident_of("Vec"),
686 self.ident_of("new")),
689 fn expr_vec_slice(&self, sp: Span, exprs: Vec<Gc<ast::Expr>> ) -> Gc<ast::Expr> {
690 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
692 fn expr_str(&self, sp: Span, s: InternedString) -> Gc<ast::Expr> {
693 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
696 fn expr_cast(&self, sp: Span, expr: Gc<ast::Expr>, ty: P<ast::Ty>) -> Gc<ast::Expr> {
697 self.expr(sp, ast::ExprCast(expr, ty))
701 fn expr_some(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
703 self.ident_of("std"),
704 self.ident_of("option"),
705 self.ident_of("Some"));
706 self.expr_call_global(sp, some, vec!(expr))
709 fn expr_none(&self, sp: Span) -> Gc<ast::Expr> {
710 let none = self.path_global(sp, vec!(
711 self.ident_of("std"),
712 self.ident_of("option"),
713 self.ident_of("None")));
717 fn expr_tuple(&self, sp: Span, exprs: Vec<Gc<ast::Expr>>) -> Gc<ast::Expr> {
718 self.expr(sp, ast::ExprTup(exprs))
721 fn expr_fail(&self, span: Span, msg: InternedString) -> Gc<ast::Expr> {
722 let loc = self.codemap().lookup_char_pos(span.lo);
723 let expr_file = self.expr_str(span,
724 token::intern_and_get_ident(loc.file
727 let expr_line = self.expr_uint(span, loc.line);
728 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
729 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
730 self.expr_call_global(
733 self.ident_of("std"),
735 self.ident_of("begin_unwind")),
737 self.expr_str(span, msg),
741 fn expr_unreachable(&self, span: Span) -> Gc<ast::Expr> {
744 "internal error: entered unreachable code"))
747 fn expr_ok(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
749 self.ident_of("std"),
750 self.ident_of("result"),
751 self.ident_of("Ok"));
752 self.expr_call_global(sp, ok, vec!(expr))
755 fn expr_err(&self, sp: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
757 self.ident_of("std"),
758 self.ident_of("result"),
759 self.ident_of("Err"));
760 self.expr_call_global(sp, err, vec!(expr))
763 fn expr_try(&self, sp: Span, head: Gc<ast::Expr>) -> Gc<ast::Expr> {
764 let ok = self.ident_of("Ok");
765 let ok_path = self.path_ident(sp, ok);
766 let err = self.ident_of("Err");
767 let err_path = self.path_ident(sp, err);
769 let binding_variable = self.ident_of("__try_var");
770 let binding_pat = self.pat_ident(sp, binding_variable);
771 let binding_expr = self.expr_ident(sp, binding_variable);
773 // Ok(__try_var) pattern
774 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat));
776 // Err(__try_var) (pattern and expression resp.)
777 let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
778 let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr));
779 // return Err(__try_var)
780 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
782 // Ok(__try_var) => __try_var
783 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
784 // Err(__try_var) => return Err(__try_var)
785 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
787 // match head { Ok() => ..., Err() => ... }
788 self.expr_match(sp, head, vec!(ok_arm, err_arm))
792 fn pat(&self, span: Span, pat: ast::Pat_) -> Gc<ast::Pat> {
793 box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span }
795 fn pat_wild(&self, span: Span) -> Gc<ast::Pat> {
796 self.pat(span, ast::PatWild(ast::PatWildSingle))
798 fn pat_lit(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Pat> {
799 self.pat(span, ast::PatLit(expr))
801 fn pat_ident(&self, span: Span, ident: ast::Ident) -> Gc<ast::Pat> {
802 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
805 fn pat_ident_binding_mode(&self,
808 bm: ast::BindingMode) -> Gc<ast::Pat> {
809 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
812 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<Gc<ast::Pat>> ) -> Gc<ast::Pat> {
813 let pat = ast::PatEnum(path, Some(subpats));
816 fn pat_struct(&self, span: Span,
817 path: ast::Path, field_pats: Vec<ast::FieldPat> ) -> Gc<ast::Pat> {
818 let pat = ast::PatStruct(path, field_pats, false);
821 fn pat_tuple(&self, span: Span, pats: Vec<Gc<ast::Pat>>) -> Gc<ast::Pat> {
822 let pat = ast::PatTup(pats);
826 fn pat_some(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
828 self.ident_of("std"),
829 self.ident_of("option"),
830 self.ident_of("Some"));
831 let path = self.path_global(span, some);
832 self.pat_enum(span, path, vec!(pat))
835 fn pat_none(&self, span: Span) -> Gc<ast::Pat> {
837 self.ident_of("std"),
838 self.ident_of("option"),
839 self.ident_of("None"));
840 let path = self.path_global(span, some);
841 self.pat_enum(span, path, vec!())
844 fn pat_ok(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
846 self.ident_of("std"),
847 self.ident_of("result"),
848 self.ident_of("Ok"));
849 let path = self.path_global(span, some);
850 self.pat_enum(span, path, vec!(pat))
853 fn pat_err(&self, span: Span, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
855 self.ident_of("std"),
856 self.ident_of("result"),
857 self.ident_of("Err"));
858 let path = self.path_global(span, some);
859 self.pat_enum(span, path, vec!(pat))
862 fn arm(&self, _span: Span, pats: Vec<Gc<ast::Pat>> , expr: Gc<ast::Expr>) -> ast::Arm {
871 fn arm_unreachable(&self, span: Span) -> ast::Arm {
872 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
875 fn expr_match(&self, span: Span, arg: Gc<ast::Expr>,
876 arms: Vec<ast::Arm>) -> Gc<Expr> {
877 self.expr(span, ast::ExprMatch(arg, arms))
880 fn expr_if(&self, span: Span,
881 cond: Gc<ast::Expr>, then: Gc<ast::Expr>,
882 els: Option<Gc<ast::Expr>>) -> Gc<ast::Expr> {
883 let els = els.map(|x| self.expr_block(self.block_expr(x)));
884 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
887 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> Gc<ast::Expr> {
888 self.expr(span, ast::ExprLoop(block, None))
891 fn lambda_fn_decl(&self, span: Span,
892 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> Gc<ast::Expr> {
893 self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
895 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> Gc<ast::Expr> {
896 let fn_decl = self.fn_decl(
897 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
898 self.ty_infer(span));
900 self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk))
902 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> Gc<ast::Expr> {
903 self.lambda(span, Vec::new(), blk)
906 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> Gc<ast::Expr> {
907 self.lambda(span, vec!(ident), blk)
910 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
911 self.lambda(span, ids, self.block_expr(expr))
913 fn lambda_expr_0(&self, span: Span, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
914 self.lambda0(span, self.block_expr(expr))
916 fn lambda_expr_1(&self, span: Span, expr: Gc<ast::Expr>, ident: ast::Ident) -> Gc<ast::Expr> {
917 self.lambda1(span, self.block_expr(expr), ident)
920 fn lambda_stmts(&self,
922 ids: Vec<ast::Ident>,
923 stmts: Vec<Gc<ast::Stmt>>)
925 self.lambda(span, ids, self.block(span, stmts, None))
927 fn lambda_stmts_0(&self, span: Span,
928 stmts: Vec<Gc<ast::Stmt>>) -> Gc<ast::Expr> {
929 self.lambda0(span, self.block(span, stmts, None))
931 fn lambda_stmts_1(&self, span: Span, stmts: Vec<Gc<ast::Stmt>>,
932 ident: ast::Ident) -> Gc<ast::Expr> {
933 self.lambda1(span, self.block(span, stmts, None), ident)
936 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
937 let arg_pat = self.pat_ident(span, ident);
941 id: ast::DUMMY_NODE_ID
946 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl> {
955 fn item(&self, span: Span,
956 name: Ident, attrs: Vec<ast::Attribute>,
957 node: ast::Item_) -> Gc<ast::Item> {
958 // FIXME: Would be nice if our generated code didn't violate
959 // Rust coding conventions
960 box(GC) ast::Item { ident: name,
962 id: ast::DUMMY_NODE_ID,
968 fn item_fn_poly(&self,
971 inputs: Vec<ast::Arg> ,
974 body: P<ast::Block>) -> Gc<ast::Item> {
978 ast::ItemFn(self.fn_decl(inputs, output),
988 inputs: Vec<ast::Arg> ,
997 ast_util::empty_generics(),
1001 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1002 let args = tys.move_iter().map(|ty| {
1003 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
1010 kind: ast::TupleVariantKind(args),
1011 id: ast::DUMMY_NODE_ID,
1017 fn item_enum_poly(&self, span: Span, name: Ident,
1018 enum_definition: ast::EnumDef,
1019 generics: Generics) -> Gc<ast::Item> {
1020 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1023 fn item_enum(&self, span: Span, name: Ident,
1024 enum_definition: ast::EnumDef) -> Gc<ast::Item> {
1025 self.item_enum_poly(span, name, enum_definition,
1026 ast_util::empty_generics())
1029 fn item_struct(&self, span: Span, name: Ident,
1030 struct_def: ast::StructDef) -> Gc<ast::Item> {
1031 self.item_struct_poly(
1035 ast_util::empty_generics()
1039 fn item_struct_poly(&self, span: Span, name: Ident,
1040 struct_def: ast::StructDef, generics: Generics) -> Gc<ast::Item> {
1041 self.item(span, name, Vec::new(), ast::ItemStruct(box(GC) struct_def, generics))
1044 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1045 attrs: Vec<ast::Attribute> ,
1046 vi: Vec<ast::ViewItem> ,
1047 items: Vec<Gc<ast::Item>>) -> Gc<ast::Item> {
1052 ast::ItemMod(ast::Mod {
1060 fn item_static(&self,
1064 mutbl: ast::Mutability,
1065 expr: Gc<ast::Expr>)
1067 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1070 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1071 generics: Generics) -> Gc<ast::Item> {
1072 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1075 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> Gc<ast::Item> {
1076 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1079 fn attribute(&self, sp: Span, mi: Gc<ast::MetaItem>) -> ast::Attribute {
1080 respan(sp, ast::Attribute_ {
1081 id: attr::mk_attr_id(),
1082 style: ast::AttrOuter,
1084 is_sugared_doc: false,
1088 fn meta_word(&self, sp: Span, w: InternedString) -> Gc<ast::MetaItem> {
1089 box(GC) respan(sp, ast::MetaWord(w))
1093 name: InternedString,
1094 mis: Vec<Gc<ast::MetaItem>> )
1095 -> Gc<ast::MetaItem> {
1096 box(GC) respan(sp, ast::MetaList(name, mis))
1098 fn meta_name_value(&self,
1100 name: InternedString,
1102 -> Gc<ast::MetaItem> {
1103 box(GC) respan(sp, ast::MetaNameValue(name, respan(sp, value)))
1106 fn view_use(&self, sp: Span,
1107 vis: ast::Visibility, vp: Gc<ast::ViewPath>) -> ast::ViewItem {
1109 node: ast::ViewItemUse(vp),
1116 fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem {
1117 let last = path.segments.last().unwrap().identifier;
1118 self.view_use_simple_(sp, vis, last, path)
1121 fn view_use_simple_(&self, sp: Span, vis: ast::Visibility,
1122 ident: ast::Ident, path: ast::Path) -> ast::ViewItem {
1123 self.view_use(sp, vis,
1125 ast::ViewPathSimple(ident,
1127 ast::DUMMY_NODE_ID)))
1130 fn view_use_list(&self, sp: Span, vis: ast::Visibility,
1131 path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem {
1132 let imports = imports.iter().map(|id| {
1133 respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1136 self.view_use(sp, vis,
1138 ast::ViewPathList(self.path(sp, path),
1140 ast::DUMMY_NODE_ID)))
1143 fn view_use_glob(&self, sp: Span,
1144 vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem {
1145 self.view_use(sp, vis,
1147 ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))
1151 struct Duplicator<'a>;
1153 impl<'a> Folder for Duplicator<'a> {
1154 fn new_id(&mut self, _: NodeId) -> NodeId {
1159 pub trait Duplicate {
1161 // Duplication functions
1163 // These functions just duplicate AST nodes.
1166 fn duplicate(&self, cx: &ExtCtxt) -> Self;
1169 impl Duplicate for Gc<ast::Expr> {
1170 fn duplicate(&self, _: &ExtCtxt) -> Gc<ast::Expr> {
1171 let mut folder = Duplicator;
1172 folder.fold_expr(*self)