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};
16 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
17 use ext::base::ExtCtxt;
18 use owned_slice::OwnedSlice;
19 use parse::token::special_idents;
20 use parse::token::InternedString;
24 // Transitional reexports so qquote can find the paths it is looking for
30 pub trait AstBuilder {
32 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
33 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
34 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
35 fn path_all(&self, sp: Span,
37 idents: Vec<ast::Ident> ,
38 lifetimes: Vec<ast::Lifetime>,
39 types: Vec<P<ast::Ty>>,
40 bindings: Vec<P<ast::TypeBinding>> )
43 fn qpath(&self, self_type: P<ast::Ty>,
44 trait_path: ast::Path,
46 -> (ast::QSelf, ast::Path);
47 fn qpath_all(&self, self_type: P<ast::Ty>,
48 trait_path: ast::Path,
50 lifetimes: Vec<ast::Lifetime>,
51 types: Vec<P<ast::Ty>>,
52 bindings: Vec<P<ast::TypeBinding>>)
53 -> (ast::QSelf, ast::Path);
56 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
58 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty>;
59 fn ty_path(&self, ast::Path) -> P<ast::Ty>;
60 fn ty_sum(&self, ast::Path, OwnedSlice<ast::TyParamBound>) -> P<ast::Ty>;
61 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
63 fn ty_rptr(&self, span: Span,
65 lifetime: Option<ast::Lifetime>,
66 mutbl: ast::Mutability) -> P<ast::Ty>;
67 fn ty_ptr(&self, span: Span,
69 mutbl: ast::Mutability) -> P<ast::Ty>;
71 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
72 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
74 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
75 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
76 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField;
81 bounds: OwnedSlice<ast::TyParamBound>,
82 default: Option<P<ast::Ty>>) -> ast::TyParam;
84 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
85 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
86 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
87 fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
88 fn lifetime_def(&self,
91 bounds: Vec<ast::Lifetime>)
95 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt>;
96 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> P<ast::Stmt>;
97 fn stmt_let_typed(&self,
104 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt>;
107 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
108 expr: Option<P<ast::Expr>>) -> P<ast::Block>;
109 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
110 fn block_all(&self, span: Span,
111 stmts: Vec<P<ast::Stmt>>,
112 expr: Option<P<ast::Expr>>) -> P<ast::Block>;
115 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>;
116 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
117 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
118 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
120 fn expr_self(&self, span: Span) -> P<ast::Expr>;
121 fn expr_binary(&self, sp: Span, op: ast::BinOp_,
122 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
123 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
124 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
126 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
127 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
128 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
129 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
130 idx: usize) -> P<ast::Expr>;
131 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
132 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
133 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
134 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
135 fn expr_method_call(&self, span: Span,
136 expr: P<ast::Expr>, ident: ast::Ident,
137 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
138 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
139 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
141 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
142 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
143 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
144 fields: Vec<ast::Field>) -> P<ast::Expr>;
146 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>;
148 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
149 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
150 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
151 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
152 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
154 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
155 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
156 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
157 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
159 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
160 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
162 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
164 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
166 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
167 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
169 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
170 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
171 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
173 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
174 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
175 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
176 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
178 fn pat_ident_binding_mode(&self,
181 bm: ast::BindingMode) -> P<ast::Pat>;
182 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
183 fn pat_struct(&self, span: Span,
184 path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>> ) -> P<ast::Pat>;
185 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
187 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
188 fn pat_none(&self, span: Span) -> P<ast::Pat>;
190 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
191 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
193 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
194 fn arm_unreachable(&self, span: Span) -> ast::Arm;
196 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
197 fn expr_if(&self, span: Span,
198 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
199 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
201 fn lambda_fn_decl(&self, span: Span,
202 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
204 fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
205 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
206 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
208 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
209 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
210 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
212 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
213 blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
214 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
215 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
216 ident: ast::Ident) -> P<ast::Expr>;
219 fn item(&self, span: Span,
220 name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>;
222 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
224 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
226 fn item_fn_poly(&self,
229 inputs: Vec<ast::Arg> ,
232 body: P<ast::Block>) -> P<ast::Item>;
236 inputs: Vec<ast::Arg> ,
238 body: P<ast::Block>) -> P<ast::Item>;
240 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
241 fn item_enum_poly(&self,
244 enum_definition: ast::EnumDef,
245 generics: Generics) -> P<ast::Item>;
246 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
248 fn item_struct_poly(&self,
251 struct_def: ast::StructDef,
252 generics: Generics) -> P<ast::Item>;
253 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> P<ast::Item>;
255 fn item_mod(&self, span: Span, inner_span: Span,
256 name: Ident, attrs: Vec<ast::Attribute>,
257 items: Vec<P<ast::Item>>) -> P<ast::Item>;
259 fn item_static(&self,
263 mutbl: ast::Mutability,
274 fn item_ty_poly(&self,
278 generics: Generics) -> P<ast::Item>;
279 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
281 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
283 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
286 name: InternedString,
287 mis: Vec<P<ast::MetaItem>> )
289 fn meta_name_value(&self,
291 name: InternedString,
295 fn item_use(&self, sp: Span,
296 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
297 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
298 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
299 ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
300 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
301 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
302 fn item_use_glob(&self, sp: Span,
303 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
306 impl<'a> AstBuilder for ExtCtxt<'a> {
307 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
308 self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
310 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
311 self.path(span, vec!(id))
313 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
314 self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
319 mut idents: Vec<ast::Ident> ,
320 lifetimes: Vec<ast::Lifetime>,
321 types: Vec<P<ast::Ty>>,
322 bindings: Vec<P<ast::TypeBinding>> )
324 let last_identifier = idents.pop().unwrap();
325 let mut segments: Vec<ast::PathSegment> = idents.into_iter()
329 parameters: ast::PathParameters::none(),
332 segments.push(ast::PathSegment {
333 identifier: last_identifier,
334 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
335 lifetimes: lifetimes,
336 types: OwnedSlice::from_vec(types),
337 bindings: OwnedSlice::from_vec(bindings),
347 /// Constructs a qualified path.
349 /// Constructs a path like `<self_type as trait_path>::ident`.
351 self_type: P<ast::Ty>,
352 trait_path: ast::Path,
354 -> (ast::QSelf, ast::Path) {
355 self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
358 /// Constructs a qualified path.
360 /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
362 self_type: P<ast::Ty>,
363 trait_path: ast::Path,
365 lifetimes: Vec<ast::Lifetime>,
366 types: Vec<P<ast::Ty>>,
367 bindings: Vec<P<ast::TypeBinding>>)
368 -> (ast::QSelf, ast::Path) {
369 let mut path = trait_path;
370 path.segments.push(ast::PathSegment {
372 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
373 lifetimes: lifetimes,
374 types: OwnedSlice::from_vec(types),
375 bindings: OwnedSlice::from_vec(bindings),
381 position: path.segments.len() - 1
385 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
392 fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
394 id: ast::DUMMY_NODE_ID,
400 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
401 self.ty(path.span, ast::TyPath(None, path))
404 fn ty_sum(&self, path: ast::Path, bounds: OwnedSlice<ast::TyParamBound>) -> P<ast::Ty> {
406 ast::TyObjectSum(self.ty_path(path),
410 // Might need to take bounds as an argument in the future, if you ever want
411 // to generate a bounded existential trait type.
412 fn ty_ident(&self, span: Span, ident: ast::Ident)
414 self.ty_path(self.path_ident(span, ident))
420 lifetime: Option<ast::Lifetime>,
421 mutbl: ast::Mutability)
424 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
430 mutbl: ast::Mutability)
433 ast::TyPtr(self.ty_mt(ty, mutbl)))
436 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
438 self.path_all(DUMMY_SP,
440 self.std_path(&["option", "Option"]),
446 fn ty_field_imm(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::TypeField {
449 mt: ast::MutTy { ty: ty, mutbl: ast::MutImmutable },
454 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
455 self.ty(span, ast::TyInfer)
461 bounds: OwnedSlice<ast::TyParamBound>,
462 default: Option<P<ast::Ty>>) -> ast::TyParam {
465 id: ast::DUMMY_NODE_ID,
472 // these are strange, and probably shouldn't be used outside of
473 // pipes. Specifically, the global version possible generates
475 fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
476 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
479 fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
482 .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
486 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
489 ref_id: ast::DUMMY_NODE_ID,
493 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
495 bound_lifetimes: Vec::new(),
496 trait_ref: self.trait_ref(path),
501 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
502 ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
505 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
506 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
509 fn lifetime_def(&self,
512 bounds: Vec<ast::Lifetime>)
513 -> ast::LifetimeDef {
515 lifetime: self.lifetime(span, name),
520 fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> {
521 P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)))
524 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
525 ex: P<ast::Expr>) -> P<ast::Stmt> {
527 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
529 self.pat_ident(sp, ident)
531 let local = P(ast::Local {
535 id: ast::DUMMY_NODE_ID,
538 let decl = respan(sp, ast::DeclLocal(local));
539 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
542 fn stmt_let_typed(&self,
550 self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
552 self.pat_ident(sp, ident)
554 let local = P(ast::Local {
558 id: ast::DUMMY_NODE_ID,
561 let decl = respan(sp, ast::DeclLocal(local));
562 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
565 fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
566 expr: Option<P<Expr>>) -> P<ast::Block> {
567 self.block_all(span, stmts, expr)
570 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
571 let decl = respan(sp, ast::DeclItem(item));
572 P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
575 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
576 self.block_all(expr.span, Vec::new(), Some(expr))
580 stmts: Vec<P<ast::Stmt>>,
581 expr: Option<P<ast::Expr>>) -> P<ast::Block> {
585 id: ast::DUMMY_NODE_ID,
586 rules: ast::DefaultBlock,
591 fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> {
593 id: ast::DUMMY_NODE_ID,
599 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
600 self.expr(path.span, ast::ExprPath(None, path))
603 /// Constructs a QPath expression.
604 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
605 self.expr(span, ast::ExprPath(Some(qself), path))
608 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
609 self.expr_path(self.path_ident(span, id))
611 fn expr_self(&self, span: Span) -> P<ast::Expr> {
612 self.expr_ident(span, special_idents::self_)
615 fn expr_binary(&self, sp: Span, op: ast::BinOp_,
616 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
617 self.expr(sp, ast::ExprBinary(Spanned { node: op, span: sp }, lhs, rhs))
620 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
621 self.expr_unary(sp, ast::UnDeref, e)
623 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
624 self.expr(sp, ast::ExprUnary(op, e))
627 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
628 let field_span = Span {
629 lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
634 let id = Spanned { node: ident, span: field_span };
635 self.expr(sp, ast::ExprField(expr, id))
637 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
638 let field_span = Span {
639 lo: sp.lo - Pos::from_usize(idx.to_string().len()),
644 let id = Spanned { node: idx, span: field_span };
645 self.expr(sp, ast::ExprTupField(expr, id))
647 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
648 self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
650 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
651 self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
654 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
655 self.expr(span, ast::ExprCall(expr, args))
657 fn expr_call_ident(&self, span: Span, id: ast::Ident,
658 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
659 self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
661 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
662 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
663 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
664 self.expr_call(sp, pathexpr, args)
666 fn expr_method_call(&self, span: Span,
669 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
670 let id = Spanned { node: ident, span: span };
671 args.insert(0, expr);
672 self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
674 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
675 self.expr(b.span, ast::ExprBlock(b))
677 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
678 ast::Field { ident: respan(span, name), expr: e, span: span }
680 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
681 self.expr(span, ast::ExprStruct(path, fields, None))
683 fn expr_struct_ident(&self, span: Span,
684 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
685 self.expr_struct(span, self.path_ident(span, id), fields)
688 fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> {
689 self.expr(sp, ast::ExprLit(P(respan(sp, lit))))
691 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
692 self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs)))
694 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
695 self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs,
698 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
699 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))
701 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
702 self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
704 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
705 self.expr_lit(sp, ast::LitBool(value))
708 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
709 self.expr(sp, ast::ExprVec(exprs))
711 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
712 self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
715 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
716 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
718 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
719 self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
722 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
723 self.expr(sp, ast::ExprCast(expr, ty))
727 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
728 let some = self.std_path(&["option", "Option", "Some"]);
729 self.expr_call_global(sp, some, vec!(expr))
732 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
733 let none = self.std_path(&["option", "Option", "None"]);
734 let none = self.path_global(sp, none);
739 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
740 self.expr(sp, ast::ExprBreak(None))
744 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
745 self.expr(sp, ast::ExprTup(exprs))
748 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
749 let loc = self.codemap().lookup_char_pos(span.lo);
750 let expr_file = self.expr_str(span,
751 token::intern_and_get_ident(&loc.file.name));
752 let expr_line = self.expr_u32(span, loc.line as u32);
753 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
754 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
755 self.expr_call_global(
757 self.std_path(&["rt", "begin_unwind"]),
759 self.expr_str(span, msg),
763 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
766 "internal error: entered unreachable code"))
769 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
770 let ok = self.std_path(&["result", "Result", "Ok"]);
771 self.expr_call_global(sp, ok, vec!(expr))
774 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
775 let err = self.std_path(&["result", "Result", "Err"]);
776 self.expr_call_global(sp, err, vec!(expr))
779 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
780 let ok = self.std_path(&["result", "Result", "Ok"]);
781 let ok_path = self.path_global(sp, ok);
782 let err = self.std_path(&["result", "Result", "Err"]);
783 let err_path = self.path_global(sp, err);
785 let binding_variable = self.ident_of("__try_var");
786 let binding_pat = self.pat_ident(sp, binding_variable);
787 let binding_expr = self.expr_ident(sp, binding_variable);
789 // Ok(__try_var) pattern
790 let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
792 // Err(__try_var) (pattern and expression resp.)
793 let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat));
794 let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
795 vec!(binding_expr.clone()));
796 // return Err(__try_var)
797 let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
799 // Ok(__try_var) => __try_var
800 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
801 // Err(__try_var) => return Err(__try_var)
802 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
804 // match head { Ok() => ..., Err() => ... }
805 self.expr_match(sp, head, vec!(ok_arm, err_arm))
809 fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
810 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
812 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
813 self.pat(span, ast::PatWild(ast::PatWildSingle))
815 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
816 self.pat(span, ast::PatLit(expr))
818 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
819 self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
822 fn pat_ident_binding_mode(&self,
825 bm: ast::BindingMode) -> P<ast::Pat> {
826 let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
829 fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
830 let pat = ast::PatEnum(path, Some(subpats));
833 fn pat_struct(&self, span: Span,
834 path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
835 let pat = ast::PatStruct(path, field_pats, false);
838 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
839 self.pat(span, ast::PatTup(pats))
842 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
843 let some = self.std_path(&["option", "Option", "Some"]);
844 let path = self.path_global(span, some);
845 self.pat_enum(span, path, vec!(pat))
848 fn pat_none(&self, span: Span) -> P<ast::Pat> {
849 let some = self.std_path(&["option", "Option", "None"]);
850 let path = self.path_global(span, some);
851 self.pat_enum(span, path, vec!())
854 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
855 let some = self.std_path(&["result", "Result", "Ok"]);
856 let path = self.path_global(span, some);
857 self.pat_enum(span, path, vec!(pat))
860 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
861 let some = self.std_path(&["result", "Result", "Err"]);
862 let path = self.path_global(span, some);
863 self.pat_enum(span, path, vec!(pat))
866 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
875 fn arm_unreachable(&self, span: Span) -> ast::Arm {
876 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
879 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
880 self.expr(span, ast::ExprMatch(arg, arms, ast::MatchSource::Normal))
883 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
884 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
885 let els = els.map(|x| self.expr_block(self.block_expr(x)));
886 self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
889 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
890 self.expr(span, ast::ExprLoop(block, None))
893 fn lambda_fn_decl(&self, span: Span,
894 fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
895 self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
897 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
898 let fn_decl = self.fn_decl(
899 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
900 self.ty_infer(span));
902 self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
904 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
905 self.lambda(span, Vec::new(), blk)
908 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
909 self.lambda(span, vec!(ident), blk)
912 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
913 expr: P<ast::Expr>) -> P<ast::Expr> {
914 self.lambda(span, ids, self.block_expr(expr))
916 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
917 self.lambda0(span, self.block_expr(expr))
919 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
920 self.lambda1(span, self.block_expr(expr), ident)
923 fn lambda_stmts(&self,
925 ids: Vec<ast::Ident>,
926 stmts: Vec<P<ast::Stmt>>)
928 self.lambda(span, ids, self.block(span, stmts, None))
930 fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> {
931 self.lambda0(span, self.block(span, stmts, None))
933 fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
934 ident: ast::Ident) -> P<ast::Expr> {
935 self.lambda1(span, self.block(span, stmts, None), ident)
938 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
939 let arg_pat = self.pat_ident(span, ident);
943 id: ast::DUMMY_NODE_ID
948 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
951 output: ast::Return(output),
956 fn item(&self, span: Span, name: Ident,
957 attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> {
958 // FIXME: Would be nice if our generated code didn't violate
959 // Rust coding conventions
963 id: ast::DUMMY_NODE_ID,
970 fn item_fn_poly(&self,
973 inputs: Vec<ast::Arg> ,
976 body: P<ast::Block>) -> P<ast::Item> {
980 ast::ItemFn(self.fn_decl(inputs, output),
981 ast::Unsafety::Normal,
982 ast::Constness::NotConst,
991 inputs: Vec<ast::Arg> ,
1000 ast_util::empty_generics(),
1004 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1005 let args = tys.into_iter().map(|ty| {
1006 ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
1013 kind: ast::TupleVariantKind(args),
1014 id: ast::DUMMY_NODE_ID,
1019 fn item_enum_poly(&self, span: Span, name: Ident,
1020 enum_definition: ast::EnumDef,
1021 generics: Generics) -> P<ast::Item> {
1022 self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1025 fn item_enum(&self, span: Span, name: Ident,
1026 enum_definition: ast::EnumDef) -> P<ast::Item> {
1027 self.item_enum_poly(span, name, enum_definition,
1028 ast_util::empty_generics())
1031 fn item_struct(&self, span: Span, name: Ident,
1032 struct_def: ast::StructDef) -> P<ast::Item> {
1033 self.item_struct_poly(
1037 ast_util::empty_generics()
1041 fn item_struct_poly(&self, span: Span, name: Ident,
1042 struct_def: ast::StructDef, generics: Generics) -> P<ast::Item> {
1043 self.item(span, name, Vec::new(), ast::ItemStruct(P(struct_def), generics))
1046 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1047 attrs: Vec<ast::Attribute>,
1048 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1053 ast::ItemMod(ast::Mod {
1060 fn item_static(&self,
1064 mutbl: ast::Mutability,
1067 self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1070 fn item_const(&self,
1076 self.item(span, name, Vec::new(), ast::ItemConst(ty, expr))
1079 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1080 generics: Generics) -> P<ast::Item> {
1081 self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1084 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1085 self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1088 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1089 respan(sp, ast::Attribute_ {
1090 id: attr::mk_attr_id(),
1091 style: ast::AttrOuter,
1093 is_sugared_doc: false,
1097 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1098 P(respan(sp, ast::MetaWord(w)))
1102 name: InternedString,
1103 mis: Vec<P<ast::MetaItem>> )
1104 -> P<ast::MetaItem> {
1105 P(respan(sp, ast::MetaList(name, mis)))
1107 fn meta_name_value(&self,
1109 name: InternedString,
1111 -> P<ast::MetaItem> {
1112 P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
1115 fn item_use(&self, sp: Span,
1116 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
1118 id: ast::DUMMY_NODE_ID,
1119 ident: special_idents::invalid,
1121 node: ast::ItemUse(vp),
1127 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1128 let last = path.segments.last().unwrap().identifier;
1129 self.item_use_simple_(sp, vis, last, path)
1132 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1133 ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
1134 self.item_use(sp, vis,
1136 ast::ViewPathSimple(ident,
1140 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1141 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1142 let imports = imports.iter().map(|id| {
1143 respan(sp, ast::PathListIdent { name: *id, rename: None, id: ast::DUMMY_NODE_ID })
1146 self.item_use(sp, vis,
1148 ast::ViewPathList(self.path(sp, path),
1152 fn item_use_glob(&self, sp: Span,
1153 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1154 self.item_use(sp, vis,
1156 ast::ViewPathGlob(self.path(sp, path)))))