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::{Ident, Generics, Expr};
15 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
16 use ext::base::ExtCtxt;
17 use owned_slice::OwnedSlice;
18 use parse::token::special_idents;
19 use parse::token::InternedString;
23 // Transitional reexports so qquote can find the paths it is looking for
29 pub trait AstBuilder {
31 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
32 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
33 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
34 fn path_all(&self, sp: Span,
36 idents: Vec<ast::Ident> ,
37 lifetimes: Vec<ast::Lifetime>,
38 types: Vec<P<ast::Ty>>,
39 bindings: Vec<P<ast::TypeBinding>> )
42 fn qpath(&self, self_type: P<ast::Ty>,
43 trait_path: ast::Path,
45 -> (ast::QSelf, ast::Path);
46 fn qpath_all(&self, self_type: P<ast::Ty>,
47 trait_path: ast::Path,
49 lifetimes: Vec<ast::Lifetime>,
50 types: Vec<P<ast::Ty>>,
51 bindings: Vec<P<ast::TypeBinding>>)
52 -> (ast::QSelf, ast::Path);
55 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
57 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty>;
58 fn ty_path(&self, ast::Path) -> P<ast::Ty>;
59 fn ty_sum(&self, ast::Path, OwnedSlice<ast::TyParamBound>) -> P<ast::Ty>;
60 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
62 fn ty_rptr(&self, span: Span,
64 lifetime: Option<ast::Lifetime>,
65 mutbl: ast::Mutability) -> P<ast::Ty>;
66 fn ty_ptr(&self, span: Span,
68 mutbl: ast::Mutability) -> P<ast::Ty>;
70 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
71 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
73 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
74 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
79 bounds: OwnedSlice<ast::TyParamBound>,
80 default: Option<P<ast::Ty>>) -> ast::TyParam;
82 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
83 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
84 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
85 fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
86 fn lifetime_def(&self,
89 bounds: Vec<ast::Lifetime>)
93 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt>;
94 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> P<ast::Stmt>;
95 fn stmt_let_typed(&self,
102 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt>;
105 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
106 expr: Option<P<ast::Expr>>) -> P<ast::Block>;
107 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
108 fn block_all(&self, span: Span,
109 stmts: Vec<P<ast::Stmt>>,
110 expr: Option<P<ast::Expr>>) -> P<ast::Block>;
113 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>;
114 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
115 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
116 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
118 fn expr_self(&self, span: Span) -> P<ast::Expr>;
119 fn expr_binary(&self, sp: Span, op: ast::BinOp_,
120 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
121 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
122 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
124 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
125 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
126 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
127 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
128 idx: usize) -> P<ast::Expr>;
129 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
130 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
131 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
132 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
133 fn expr_method_call(&self, span: Span,
134 expr: P<ast::Expr>, ident: ast::Ident,
135 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
136 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
137 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
139 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
140 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
141 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
142 fields: Vec<ast::Field>) -> P<ast::Expr>;
144 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>;
146 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
147 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
148 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
149 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
150 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
152 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
153 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
154 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
155 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
157 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
158 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
160 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
162 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
164 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
165 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
167 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
168 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
169 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
171 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
172 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
173 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
174 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
176 fn pat_ident_binding_mode(&self,
179 bm: ast::BindingMode) -> P<ast::Pat>;
180 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
181 fn pat_struct(&self, span: Span,
182 path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>> ) -> P<ast::Pat>;
183 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
185 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
186 fn pat_none(&self, span: Span) -> P<ast::Pat>;
188 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
189 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
191 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
192 fn arm_unreachable(&self, span: Span) -> ast::Arm;
194 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
195 fn expr_if(&self, span: Span,
196 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
197 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
199 fn lambda_fn_decl(&self, span: Span,
200 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
202 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
203 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
204 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
206 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
207 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
208 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
210 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
211 blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
212 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
213 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
214 ident: ast::Ident) -> P<ast::Expr>;
217 fn item(&self, span: Span,
218 name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>;
220 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
222 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
224 fn item_fn_poly(&self,
227 inputs: Vec<ast::Arg> ,
230 body: P<ast::Block>) -> P<ast::Item>;
234 inputs: Vec<ast::Arg> ,
236 body: P<ast::Block>) -> P<ast::Item>;
238 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
239 fn item_enum_poly(&self,
242 enum_definition: ast::EnumDef,
243 generics: Generics) -> P<ast::Item>;
244 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
246 fn item_struct_poly(&self,
249 struct_def: ast::VariantData,
250 generics: Generics) -> P<ast::Item>;
251 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
253 fn item_mod(&self, span: Span, inner_span: Span,
254 name: Ident, attrs: Vec<ast::Attribute>,
255 items: Vec<P<ast::Item>>) -> P<ast::Item>;
257 fn item_static(&self,
261 mutbl: ast::Mutability,
272 fn item_ty_poly(&self,
276 generics: Generics) -> P<ast::Item>;
277 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
279 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
281 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
284 name: InternedString,
285 mis: Vec<P<ast::MetaItem>> )
287 fn meta_name_value(&self,
289 name: InternedString,
293 fn item_use(&self, sp: Span,
294 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
295 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
296 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
297 ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
298 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
299 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
300 fn item_use_glob(&self, sp: Span,
301 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
304 impl<'a> AstBuilder for ExtCtxt<'a> {
305 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
306 self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
308 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
309 self.path(span, vec!(id))
311 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
312 self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
317 mut idents: Vec<ast::Ident> ,
318 lifetimes: Vec<ast::Lifetime>,
319 types: Vec<P<ast::Ty>>,
320 bindings: Vec<P<ast::TypeBinding>> )
322 let last_identifier = idents.pop().unwrap();
323 let mut segments: Vec<ast::PathSegment> = idents.into_iter()
327 parameters: ast::PathParameters::none(),
330 segments.push(ast::PathSegment {
331 identifier: last_identifier,
332 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
333 lifetimes: lifetimes,
334 types: OwnedSlice::from_vec(types),
335 bindings: OwnedSlice::from_vec(bindings),
345 /// Constructs a qualified path.
347 /// Constructs a path like `<self_type as trait_path>::ident`.
349 self_type: P<ast::Ty>,
350 trait_path: ast::Path,
352 -> (ast::QSelf, ast::Path) {
353 self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
356 /// Constructs a qualified path.
358 /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
360 self_type: P<ast::Ty>,
361 trait_path: ast::Path,
363 lifetimes: Vec<ast::Lifetime>,
364 types: Vec<P<ast::Ty>>,
365 bindings: Vec<P<ast::TypeBinding>>)
366 -> (ast::QSelf, ast::Path) {
367 let mut path = trait_path;
368 path.segments.push(ast::PathSegment {
370 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
371 lifetimes: lifetimes,
372 types: OwnedSlice::from_vec(types),
373 bindings: OwnedSlice::from_vec(bindings),
379 position: path.segments.len() - 1
383 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
390 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
392 id: ast::DUMMY_NODE_ID,
398 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
399 self.ty(path.span, ast::TyPath(None, path))
402 fn ty_sum(&self, path: ast::Path, bounds: OwnedSlice<ast::TyParamBound>) -> P<ast::Ty> {
404 ast::TyObjectSum(self.ty_path(path),
408 // Might need to take bounds as an argument in the future, if you ever want
409 // to generate a bounded existential trait type.
410 fn ty_ident(&self, span: Span, ident: ast::Ident)
412 self.ty_path(self.path_ident(span, ident))
418 lifetime: Option<ast::Lifetime>,
419 mutbl: ast::Mutability)
422 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
428 mutbl: ast::Mutability)
431 ast::TyPtr(self.ty_mt(ty, mutbl)))
434 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
436 self.path_all(DUMMY_SP,
438 self.std_path(&["option", "Option"]),
444 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
445 self.ty(span, ast::TyInfer)
451 bounds: OwnedSlice<ast::TyParamBound>,
452 default: Option<P<ast::Ty>>) -> ast::TyParam {
455 id: ast::DUMMY_NODE_ID,
462 // these are strange, and probably shouldn't be used outside of
463 // pipes. Specifically, the global version possible generates
465 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
466 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
469 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
472 .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
476 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
479 ref_id: ast::DUMMY_NODE_ID,
483 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
485 bound_lifetimes: Vec::new(),
486 trait_ref: self.trait_ref(path),
491 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
492 ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
495 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
496 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
499 fn lifetime_def(&self,
502 bounds: Vec<ast::Lifetime>)
503 -> ast::LifetimeDef {
505 lifetime: self.lifetime(span, name),
510 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> {
511 P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)))
514 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
515 ex: P<ast::Expr>) -> P<ast::Stmt> {
517 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
519 self.pat_ident(sp, ident)
521 let local = P(ast::Local {
525 id: ast::DUMMY_NODE_ID,
529 let decl = respan(sp, ast::DeclLocal(local));
530 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
533 fn stmt_let_typed(&self,
541 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
543 self.pat_ident(sp, ident)
545 let local = P(ast::Local {
549 id: ast::DUMMY_NODE_ID,
553 let decl = respan(sp, ast::DeclLocal(local));
554 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
557 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
558 expr: Option<P<Expr>>) -> P<ast::Block> {
559 self.block_all(span, stmts, expr)
562 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
563 let decl = respan(sp, ast::DeclItem(item));
564 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
567 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
568 self.block_all(expr.span, Vec::new(), Some(expr))
572 stmts: Vec<P<ast::Stmt>>,
573 expr: Option<P<ast::Expr>>) -> P<ast::Block> {
577 id: ast::DUMMY_NODE_ID,
578 rules: ast::DefaultBlock,
583 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> {
585 id: ast::DUMMY_NODE_ID,
592 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
593 self.expr(path.span, ast::ExprPath(None, path))
596 /// Constructs a QPath expression.
597 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
598 self.expr(span, ast::ExprPath(Some(qself), path))
601 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
602 self.expr_path(self.path_ident(span, id))
604 fn expr_self(&self, span: Span) -> P<ast::Expr> {
605 self.expr_ident(span, special_idents::self_)
608 fn expr_binary(&self, sp: Span, op: ast::BinOp_,
609 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
610 self.expr(sp, ast::ExprBinary(Spanned { node: op, span: sp }, lhs, rhs))
613 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
614 self.expr_unary(sp, ast::UnDeref, e)
616 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
617 self.expr(sp, ast::ExprUnary(op, e))
620 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
621 let field_span = Span {
622 lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
627 let id = Spanned { node: ident, span: field_span };
628 self.expr(sp, ast::ExprField(expr, id))
630 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
631 let field_span = Span {
632 lo: sp.lo - Pos::from_usize(idx.to_string().len()),
637 let id = Spanned { node: idx, span: field_span };
638 self.expr(sp, ast::ExprTupField(expr, id))
640 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
641 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
643 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
644 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
647 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
648 self.expr(span, ast::ExprCall(expr, args))
650 fn expr_call_ident(&self, span: Span, id: ast::Ident,
651 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
652 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
654 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
655 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
656 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
657 self.expr_call(sp, pathexpr, args)
659 fn expr_method_call(&self, span: Span,
662 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
663 let id = Spanned { node: ident, span: span };
664 args.insert(0, expr);
665 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
667 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
668 self.expr(b.span, ast::ExprBlock(b))
670 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
671 ast::Field { ident: respan(span, name), expr: e, span: span }
673 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
674 self.expr(span, ast::ExprStruct(path, fields, None))
676 fn expr_struct_ident(&self, span: Span,
677 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
678 self.expr_struct(span, self.path_ident(span, id), fields)
681 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> {
682 self.expr(sp, ast::ExprLit(P(respan(sp, lit))))
684 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
685 self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs)))
687 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
688 self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs,
691 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
692 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))
694 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
695 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
697 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
698 self.expr_lit(sp, ast::LitBool(value))
701 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
702 self.expr(sp, ast::ExprVec(exprs))
704 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
705 self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
708 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
709 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
711 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
712 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
715 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
716 self.expr(sp, ast::ExprCast(expr, ty))
720 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
721 let some = self.std_path(&["option", "Option", "Some"]);
722 self.expr_call_global(sp, some, vec!(expr))
725 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
726 let none = self.std_path(&["option", "Option", "None"]);
727 let none = self.path_global(sp, none);
732 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
733 self.expr(sp, ast::ExprBreak(None))
737 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
738 self.expr(sp, ast::ExprTup(exprs))
741 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
742 let loc = self.codemap().lookup_char_pos(span.lo);
743 let expr_file = self.expr_str(span,
744 token::intern_and_get_ident(&loc.file.name));
745 let expr_line = self.expr_u32(span, loc.line as u32);
746 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
747 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
748 self.expr_call_global(
750 self.std_path(&["rt", "begin_unwind"]),
752 self.expr_str(span, msg),
756 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
759 "internal error: entered unreachable code"))
762 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
763 let ok = self.std_path(&["result", "Result", "Ok"]);
764 self.expr_call_global(sp, ok, vec!(expr))
767 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
768 let err = self.std_path(&["result", "Result", "Err"]);
769 self.expr_call_global(sp, err, vec!(expr))
772 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
773 let ok = self.std_path(&["result", "Result", "Ok"]);
774 let ok_path = self.path_global(sp, ok);
775 let err = self.std_path(&["result", "Result", "Err"]);
776 let err_path = self.path_global(sp, err);
778 let binding_variable = self.ident_of("__try_var");
779 let binding_pat = self.pat_ident(sp, binding_variable);
780 let binding_expr = self.expr_ident(sp, binding_variable);
782 // Ok(__try_var) pattern
783 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
785 // Err(__try_var) (pattern and expression resp.)
786 let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat));
787 let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
788 vec!(binding_expr.clone()));
789 // return Err(__try_var)
790 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
792 // Ok(__try_var) => __try_var
793 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
794 // Err(__try_var) => return Err(__try_var)
795 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
797 // match head { Ok() => ..., Err() => ... }
798 self.expr_match(sp, head, vec!(ok_arm, err_arm))
802 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
803 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
805 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
806 self.pat(span, ast::PatWild)
808 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
809 self.pat(span, ast::PatLit(expr))
811 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
812 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
815 fn pat_ident_binding_mode(&self,
818 bm: ast::BindingMode) -> P<ast::Pat> {
819 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
822 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
823 let pat = ast::PatEnum(path, Some(subpats));
826 fn pat_struct(&self, span: Span,
827 path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
828 let pat = ast::PatStruct(path, field_pats, false);
831 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
832 self.pat(span, ast::PatTup(pats))
835 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
836 let some = self.std_path(&["option", "Option", "Some"]);
837 let path = self.path_global(span, some);
838 self.pat_enum(span, path, vec!(pat))
841 fn pat_none(&self, span: Span) -> P<ast::Pat> {
842 let some = self.std_path(&["option", "Option", "None"]);
843 let path = self.path_global(span, some);
844 self.pat_enum(span, path, vec!())
847 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
848 let some = self.std_path(&["result", "Result", "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: P<ast::Pat>) -> P<ast::Pat> {
854 let some = self.std_path(&["result", "Result", "Err"]);
855 let path = self.path_global(span, some);
856 self.pat_enum(span, path, vec!(pat))
859 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
868 fn arm_unreachable(&self, span: Span) -> ast::Arm {
869 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
872 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
873 self.expr(span, ast::ExprMatch(arg, arms))
876 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
877 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
878 let els = els.map(|x| self.expr_block(self.block_expr(x)));
879 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
882 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
883 self.expr(span, ast::ExprLoop(block, None))
886 fn lambda_fn_decl(&self, span: Span,
887 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
888 self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
890 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
891 let fn_decl = self.fn_decl(
892 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
893 self.ty_infer(span));
895 self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
897 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
898 self.lambda(span, Vec::new(), blk)
901 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
902 self.lambda(span, vec!(ident), blk)
905 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
906 expr: P<ast::Expr>) -> P<ast::Expr> {
907 self.lambda(span, ids, self.block_expr(expr))
909 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
910 self.lambda0(span, self.block_expr(expr))
912 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
913 self.lambda1(span, self.block_expr(expr), ident)
916 fn lambda_stmts(&self,
918 ids: Vec<ast::Ident>,
919 stmts: Vec<P<ast::Stmt>>)
921 self.lambda(span, ids, self.block(span, stmts, None))
923 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> {
924 self.lambda0(span, self.block(span, stmts, None))
926 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
927 ident: ast::Ident) -> P<ast::Expr> {
928 self.lambda1(span, self.block(span, stmts, None), ident)
931 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
932 let arg_pat = self.pat_ident(span, ident);
936 id: ast::DUMMY_NODE_ID
941 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
944 output: ast::Return(output),
949 fn item(&self, span: Span, name: Ident,
950 attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> {
951 // FIXME: Would be nice if our generated code didn't violate
952 // Rust coding conventions
956 id: ast::DUMMY_NODE_ID,
963 fn item_fn_poly(&self,
966 inputs: Vec<ast::Arg> ,
969 body: P<ast::Block>) -> P<ast::Item> {
973 ast::ItemFn(self.fn_decl(inputs, output),
974 ast::Unsafety::Normal,
975 ast::Constness::NotConst,
984 inputs: Vec<ast::Arg> ,
997 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
998 let fields: Vec<_> = tys.into_iter().map(|ty| {
999 Spanned { span: ty.span, node: ast::StructField_ {
1001 kind: ast::UnnamedField(ast::Inherited),
1003 id: ast::DUMMY_NODE_ID,
1007 let vdata = if fields.is_empty() {
1008 ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1010 ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1022 fn item_enum_poly(&self, span: Span, name: Ident,
1023 enum_definition: ast::EnumDef,
1024 generics: Generics) -> P<ast::Item> {
1025 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1028 fn item_enum(&self, span: Span, name: Ident,
1029 enum_definition: ast::EnumDef) -> P<ast::Item> {
1030 self.item_enum_poly(span, name, enum_definition,
1031 Generics::default())
1034 fn item_struct(&self, span: Span, name: Ident,
1035 struct_def: ast::VariantData) -> P<ast::Item> {
1036 self.item_struct_poly(
1044 fn item_struct_poly(&self, span: Span, name: Ident,
1045 struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1046 self.item(span, name, Vec::new(), ast::ItemStruct(struct_def, generics))
1049 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1050 attrs: Vec<ast::Attribute>,
1051 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1056 ast::ItemMod(ast::Mod {
1063 fn item_static(&self,
1067 mutbl: ast::Mutability,
1070 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1073 fn item_const(&self,
1079 self.item(span, name, Vec::new(), ast::ItemConst(ty, expr))
1082 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1083 generics: Generics) -> P<ast::Item> {
1084 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1087 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1088 self.item_ty_poly(span, name, ty, Generics::default())
1091 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1092 respan(sp, ast::Attribute_ {
1093 id: attr::mk_attr_id(),
1094 style: ast::AttrStyle::Outer,
1096 is_sugared_doc: false,
1100 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1101 P(respan(sp, ast::MetaWord(w)))
1105 name: InternedString,
1106 mis: Vec<P<ast::MetaItem>> )
1107 -> P<ast::MetaItem> {
1108 P(respan(sp, ast::MetaList(name, mis)))
1110 fn meta_name_value(&self,
1112 name: InternedString,
1114 -> P<ast::MetaItem> {
1115 P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
1118 fn item_use(&self, sp: Span,
1119 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
1121 id: ast::DUMMY_NODE_ID,
1122 ident: special_idents::invalid,
1124 node: ast::ItemUse(vp),
1130 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1131 let last = path.segments.last().unwrap().identifier;
1132 self.item_use_simple_(sp, vis, last, path)
1135 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1136 ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
1137 self.item_use(sp, vis,
1139 ast::ViewPathSimple(ident,
1143 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1144 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1145 let imports = imports.iter().map(|id| {
1146 respan(sp, ast::PathListIdent { name: *id, rename: None, id: ast::DUMMY_NODE_ID })
1149 self.item_use(sp, vis,
1151 ast::ViewPathList(self.path(sp, path),
1155 fn item_use_glob(&self, sp: Span,
1156 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1157 self.item_use(sp, vis,
1159 ast::ViewPathGlob(self.path(sp, path)))))