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::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
14 use syntax_pos::{Span, DUMMY_SP, Pos};
15 use codemap::{dummy_spanned, respan, Spanned};
16 use ext::base::ExtCtxt;
17 use parse::token::{self, keywords, InternedString};
20 // Transitional reexports so qquote can find the paths it is looking for
26 pub trait AstBuilder {
28 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
29 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
30 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
31 fn path_all(&self, sp: Span,
33 idents: Vec<ast::Ident> ,
34 lifetimes: Vec<ast::Lifetime>,
35 types: Vec<P<ast::Ty>>,
36 bindings: Vec<ast::TypeBinding> )
39 fn qpath(&self, self_type: P<ast::Ty>,
40 trait_path: ast::Path,
42 -> (ast::QSelf, ast::Path);
43 fn qpath_all(&self, self_type: P<ast::Ty>,
44 trait_path: ast::Path,
46 lifetimes: Vec<ast::Lifetime>,
47 types: Vec<P<ast::Ty>>,
48 bindings: Vec<ast::TypeBinding>)
49 -> (ast::QSelf, ast::Path);
52 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
54 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
55 fn ty_path(&self, ast::Path) -> P<ast::Ty>;
56 fn ty_sum(&self, ast::Path, ast::TyParamBounds) -> P<ast::Ty>;
57 fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
59 fn ty_rptr(&self, span: Span,
61 lifetime: Option<ast::Lifetime>,
62 mutbl: ast::Mutability) -> P<ast::Ty>;
63 fn ty_ptr(&self, span: Span,
65 mutbl: ast::Mutability) -> P<ast::Ty>;
67 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
68 fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
70 fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
71 fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
76 attrs: Vec<ast::Attribute>,
77 bounds: ast::TyParamBounds,
78 default: Option<P<ast::Ty>>) -> ast::TyParam;
80 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
81 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
82 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
83 fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
84 fn lifetime_def(&self,
87 attrs: Vec<ast::Attribute>,
88 bounds: Vec<ast::Lifetime>)
92 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
93 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
94 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
95 fn stmt_let_typed(&self,
102 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
103 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
106 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
107 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
110 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
111 fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
112 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
113 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
115 fn expr_self(&self, span: Span) -> P<ast::Expr>;
116 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
117 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
118 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
119 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
121 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
122 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
123 fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
124 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
125 idx: usize) -> P<ast::Expr>;
126 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
127 fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
128 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
129 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
130 fn expr_method_call(&self, span: Span,
131 expr: P<ast::Expr>, ident: ast::Ident,
132 args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
133 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
134 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
136 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
137 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
138 fn expr_struct_ident(&self, span: Span, id: ast::Ident,
139 fields: Vec<ast::Field>) -> P<ast::Expr>;
141 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
143 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
144 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
145 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
146 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
147 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
149 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
150 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
151 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
152 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
154 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
155 fn expr_none(&self, sp: Span) -> P<ast::Expr>;
157 fn expr_break(&self, sp: Span) -> P<ast::Expr>;
159 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
161 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
162 fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
164 fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
165 fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
166 fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
168 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
169 fn pat_wild(&self, span: Span) -> P<ast::Pat>;
170 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
171 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
173 fn pat_ident_binding_mode(&self,
176 bm: ast::BindingMode) -> P<ast::Pat>;
177 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat>;
178 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
179 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
180 fn pat_struct(&self, span: Span, path: ast::Path,
181 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat>;
182 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
184 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
185 fn pat_none(&self, span: Span) -> P<ast::Pat>;
187 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
188 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
190 fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
191 fn arm_unreachable(&self, span: Span) -> ast::Arm;
193 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
194 fn expr_if(&self, span: Span,
195 cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
196 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
198 fn lambda_fn_decl(&self,
200 fn_decl: P<ast::FnDecl>,
205 fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr>;
206 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
207 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
209 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
210 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
211 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
213 fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
214 blk: Vec<ast::Stmt>) -> P<ast::Expr>;
215 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
216 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
217 ident: ast::Ident) -> P<ast::Expr>;
220 fn item(&self, span: Span,
221 name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
223 fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
225 fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
227 fn item_fn_poly(&self,
230 inputs: Vec<ast::Arg> ,
233 body: P<ast::Block>) -> P<ast::Item>;
237 inputs: Vec<ast::Arg> ,
239 body: P<ast::Block>) -> P<ast::Item>;
241 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
242 fn item_enum_poly(&self,
245 enum_definition: ast::EnumDef,
246 generics: Generics) -> P<ast::Item>;
247 fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
249 fn item_struct_poly(&self,
252 struct_def: ast::VariantData,
253 generics: Generics) -> P<ast::Item>;
254 fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
256 fn item_mod(&self, span: Span, inner_span: Span,
257 name: Ident, attrs: Vec<ast::Attribute>,
258 items: Vec<P<ast::Item>>) -> P<ast::Item>;
260 fn item_static(&self,
264 mutbl: ast::Mutability,
275 fn item_ty_poly(&self,
279 generics: Generics) -> P<ast::Item>;
280 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
282 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
284 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
286 fn meta_list_item_word(&self, sp: Span, w: InternedString) -> ast::NestedMetaItem;
290 name: InternedString,
291 mis: Vec<ast::NestedMetaItem> )
293 fn meta_name_value(&self,
295 name: InternedString,
299 fn item_use(&self, sp: Span,
300 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
301 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
302 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
303 ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
304 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
305 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
306 fn item_use_glob(&self, sp: Span,
307 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
310 impl<'a> AstBuilder for ExtCtxt<'a> {
311 fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
312 self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
314 fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
315 self.path(span, vec!(id))
317 fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
318 self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
323 mut idents: Vec<ast::Ident> ,
324 lifetimes: Vec<ast::Lifetime>,
325 types: Vec<P<ast::Ty>>,
326 bindings: Vec<ast::TypeBinding> )
328 let last_identifier = idents.pop().unwrap();
329 let mut segments: Vec<ast::PathSegment> = idents.into_iter()
333 parameters: ast::PathParameters::none(),
336 segments.push(ast::PathSegment {
337 identifier: last_identifier,
338 parameters: ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
339 lifetimes: lifetimes,
340 types: P::from_vec(types),
341 bindings: P::from_vec(bindings),
351 /// Constructs a qualified path.
353 /// Constructs a path like `<self_type as trait_path>::ident`.
355 self_type: P<ast::Ty>,
356 trait_path: ast::Path,
358 -> (ast::QSelf, ast::Path) {
359 self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
362 /// Constructs a qualified path.
364 /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
366 self_type: P<ast::Ty>,
367 trait_path: ast::Path,
369 lifetimes: Vec<ast::Lifetime>,
370 types: Vec<P<ast::Ty>>,
371 bindings: Vec<ast::TypeBinding>)
372 -> (ast::QSelf, ast::Path) {
373 let mut path = trait_path;
374 path.segments.push(ast::PathSegment {
376 parameters: ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
377 lifetimes: lifetimes,
378 types: P::from_vec(types),
379 bindings: P::from_vec(bindings),
385 position: path.segments.len() - 1
389 fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
396 fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
398 id: ast::DUMMY_NODE_ID,
404 fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
405 self.ty(path.span, ast::TyKind::Path(None, path))
408 fn ty_sum(&self, path: ast::Path, bounds: ast::TyParamBounds) -> P<ast::Ty> {
410 ast::TyKind::ObjectSum(self.ty_path(path),
414 // Might need to take bounds as an argument in the future, if you ever want
415 // to generate a bounded existential trait type.
416 fn ty_ident(&self, span: Span, ident: ast::Ident)
418 self.ty_path(self.path_ident(span, ident))
424 lifetime: Option<ast::Lifetime>,
425 mutbl: ast::Mutability)
428 ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
434 mutbl: ast::Mutability)
437 ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
440 fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
442 self.path_all(DUMMY_SP,
444 self.std_path(&["option", "Option"]),
450 fn ty_infer(&self, span: Span) -> P<ast::Ty> {
451 self.ty(span, ast::TyKind::Infer)
457 attrs: Vec<ast::Attribute>,
458 bounds: ast::TyParamBounds,
459 default: Option<P<ast::Ty>>) -> ast::TyParam {
462 id: ast::DUMMY_NODE_ID,
470 // these are strange, and probably shouldn't be used outside of
471 // pipes. Specifically, the global version possible generates
473 fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
474 ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
477 fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
480 .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
484 fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
487 ref_id: ast::DUMMY_NODE_ID,
491 fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
493 bound_lifetimes: Vec::new(),
494 trait_ref: self.trait_ref(path),
499 fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
500 ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
503 fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
504 ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
507 fn lifetime_def(&self,
510 attrs: Vec<ast::Attribute>,
511 bounds: Vec<ast::Lifetime>)
512 -> ast::LifetimeDef {
515 lifetime: self.lifetime(span, name),
520 fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
522 id: ast::DUMMY_NODE_ID,
524 node: ast::StmtKind::Expr(expr),
528 fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
530 id: ast::DUMMY_NODE_ID,
532 node: ast::StmtKind::Semi(expr),
536 fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
537 ex: P<ast::Expr>) -> ast::Stmt {
539 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
540 self.pat_ident_binding_mode(sp, ident, binding_mode)
542 self.pat_ident(sp, ident)
544 let local = P(ast::Local {
548 id: ast::DUMMY_NODE_ID,
550 attrs: ast::ThinVec::new(),
553 id: ast::DUMMY_NODE_ID,
554 node: ast::StmtKind::Local(local),
559 fn stmt_let_typed(&self,
567 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
568 self.pat_ident_binding_mode(sp, ident, binding_mode)
570 self.pat_ident(sp, ident)
572 let local = P(ast::Local {
576 id: ast::DUMMY_NODE_ID,
578 attrs: ast::ThinVec::new(),
581 id: ast::DUMMY_NODE_ID,
582 node: ast::StmtKind::Local(local),
587 // Generate `let _: Type;`, usually used for type assertions.
588 fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
589 let local = P(ast::Local {
590 pat: self.pat_wild(span),
593 id: ast::DUMMY_NODE_ID,
595 attrs: ast::ThinVec::new(),
598 id: ast::DUMMY_NODE_ID,
599 node: ast::StmtKind::Local(local),
604 fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
606 id: ast::DUMMY_NODE_ID,
607 node: ast::StmtKind::Item(item),
612 fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
613 self.block(expr.span, vec![ast::Stmt {
614 id: ast::DUMMY_NODE_ID,
616 node: ast::StmtKind::Expr(expr),
619 fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
622 id: ast::DUMMY_NODE_ID,
623 rules: BlockCheckMode::Default,
628 fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
630 id: ast::DUMMY_NODE_ID,
633 attrs: ast::ThinVec::new(),
637 fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
638 self.expr(path.span, ast::ExprKind::Path(None, path))
641 /// Constructs a QPath expression.
642 fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
643 self.expr(span, ast::ExprKind::Path(Some(qself), path))
646 fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
647 self.expr_path(self.path_ident(span, id))
649 fn expr_self(&self, span: Span) -> P<ast::Expr> {
650 self.expr_ident(span, keywords::SelfValue.ident())
653 fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
654 lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
655 self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
658 fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
659 self.expr_unary(sp, UnOp::Deref, e)
661 fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
662 self.expr(sp, ast::ExprKind::Unary(op, e))
665 fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
666 let field_span = Span {
667 lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
672 let id = Spanned { node: ident, span: field_span };
673 self.expr(sp, ast::ExprKind::Field(expr, id))
675 fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
676 let field_span = Span {
677 lo: sp.lo - Pos::from_usize(idx.to_string().len()),
682 let id = Spanned { node: idx, span: field_span };
683 self.expr(sp, ast::ExprKind::TupField(expr, id))
685 fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
686 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
688 fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
689 self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
692 fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
693 self.expr(span, ast::ExprKind::Call(expr, args))
695 fn expr_call_ident(&self, span: Span, id: ast::Ident,
696 args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
697 self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
699 fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
700 args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
701 let pathexpr = self.expr_path(self.path_global(sp, fn_path));
702 self.expr_call(sp, pathexpr, args)
704 fn expr_method_call(&self, span: Span,
707 mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
708 let id = Spanned { node: ident, span: span };
709 args.insert(0, expr);
710 self.expr(span, ast::ExprKind::MethodCall(id, Vec::new(), args))
712 fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
713 self.expr(b.span, ast::ExprKind::Block(b))
715 fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
716 ast::Field { ident: respan(span, name), expr: e, span: span }
718 fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
719 self.expr(span, ast::ExprKind::Struct(path, fields, None))
721 fn expr_struct_ident(&self, span: Span,
722 id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
723 self.expr_struct(span, self.path_ident(span, id), fields)
726 fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
727 self.expr(sp, ast::ExprKind::Lit(P(respan(sp, lit))))
729 fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
730 self.expr_lit(span, ast::LitKind::Int(i as u64, ast::LitIntType::Unsigned(ast::UintTy::Us)))
732 fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
735 let lit_ty = ast::LitIntType::Signed(ast::IntTy::Is);
736 let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
737 self.expr_unary(sp, ast::UnOp::Neg, lit)
739 self.expr_lit(sp, ast::LitKind::Int(i as u64, ast::LitIntType::Signed(ast::IntTy::Is)))
742 fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
743 self.expr_lit(sp, ast::LitKind::Int(u as u64, ast::LitIntType::Unsigned(ast::UintTy::U32)))
745 fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
746 self.expr_lit(sp, ast::LitKind::Int(u as u64, ast::LitIntType::Unsigned(ast::UintTy::U8)))
748 fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
749 self.expr_lit(sp, ast::LitKind::Bool(value))
752 fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
753 self.expr(sp, ast::ExprKind::Vec(exprs))
755 fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
756 self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
759 fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
760 self.expr_addr_of(sp, self.expr_vec(sp, exprs))
762 fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
763 self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
766 fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
767 self.expr(sp, ast::ExprKind::Cast(expr, ty))
771 fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
772 let some = self.std_path(&["option", "Option", "Some"]);
773 self.expr_call_global(sp, some, vec!(expr))
776 fn expr_none(&self, sp: Span) -> P<ast::Expr> {
777 let none = self.std_path(&["option", "Option", "None"]);
778 let none = self.path_global(sp, none);
783 fn expr_break(&self, sp: Span) -> P<ast::Expr> {
784 self.expr(sp, ast::ExprKind::Break(None))
788 fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
789 self.expr(sp, ast::ExprKind::Tup(exprs))
792 fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
793 let loc = self.codemap().lookup_char_pos(span.lo);
794 let expr_file = self.expr_str(span,
795 token::intern_and_get_ident(&loc.file.name));
796 let expr_line = self.expr_u32(span, loc.line as u32);
797 let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
798 let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
799 self.expr_call_global(
801 self.std_path(&["rt", "begin_panic"]),
803 self.expr_str(span, msg),
807 fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
810 "internal error: entered unreachable code"))
813 fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
814 let ok = self.std_path(&["result", "Result", "Ok"]);
815 self.expr_call_global(sp, ok, vec!(expr))
818 fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
819 let err = self.std_path(&["result", "Result", "Err"]);
820 self.expr_call_global(sp, err, vec!(expr))
823 fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
824 let ok = self.std_path(&["result", "Result", "Ok"]);
825 let ok_path = self.path_global(sp, ok);
826 let err = self.std_path(&["result", "Result", "Err"]);
827 let err_path = self.path_global(sp, err);
829 let binding_variable = self.ident_of("__try_var");
830 let binding_pat = self.pat_ident(sp, binding_variable);
831 let binding_expr = self.expr_ident(sp, binding_variable);
833 // Ok(__try_var) pattern
834 let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
836 // Err(__try_var) (pattern and expression resp.)
837 let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
838 let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
839 vec!(binding_expr.clone()));
840 // return Err(__try_var)
841 let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
843 // Ok(__try_var) => __try_var
844 let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
845 // Err(__try_var) => return Err(__try_var)
846 let err_arm = self.arm(sp, vec!(err_pat), err_expr);
848 // match head { Ok() => ..., Err() => ... }
849 self.expr_match(sp, head, vec!(ok_arm, err_arm))
853 fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
854 P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
856 fn pat_wild(&self, span: Span) -> P<ast::Pat> {
857 self.pat(span, PatKind::Wild)
859 fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
860 self.pat(span, PatKind::Lit(expr))
862 fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
863 let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
864 self.pat_ident_binding_mode(span, ident, binding_mode)
867 fn pat_ident_binding_mode(&self,
870 bm: ast::BindingMode) -> P<ast::Pat> {
871 let pat = PatKind::Ident(bm, Spanned{span: span, node: ident}, None);
874 fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
875 self.pat(span, PatKind::Path(None, path))
877 fn pat_tuple_struct(&self, span: Span, path: ast::Path,
878 subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
879 self.pat(span, PatKind::TupleStruct(path, subpats, None))
881 fn pat_struct(&self, span: Span, path: ast::Path,
882 field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
883 self.pat(span, PatKind::Struct(path, field_pats, false))
885 fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
886 self.pat(span, PatKind::Tuple(pats, None))
889 fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
890 let some = self.std_path(&["option", "Option", "Some"]);
891 let path = self.path_global(span, some);
892 self.pat_tuple_struct(span, path, vec![pat])
895 fn pat_none(&self, span: Span) -> P<ast::Pat> {
896 let some = self.std_path(&["option", "Option", "None"]);
897 let path = self.path_global(span, some);
898 self.pat_path(span, path)
901 fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
902 let some = self.std_path(&["result", "Result", "Ok"]);
903 let path = self.path_global(span, some);
904 self.pat_tuple_struct(span, path, vec![pat])
907 fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
908 let some = self.std_path(&["result", "Result", "Err"]);
909 let path = self.path_global(span, some);
910 self.pat_tuple_struct(span, path, vec![pat])
913 fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
922 fn arm_unreachable(&self, span: Span) -> ast::Arm {
923 self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
926 fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
927 self.expr(span, ast::ExprKind::Match(arg, arms))
930 fn expr_if(&self, span: Span, cond: P<ast::Expr>,
931 then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
932 let els = els.map(|x| self.expr_block(self.block_expr(x)));
933 self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
936 fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
937 self.expr(span, ast::ExprKind::Loop(block, None))
940 fn lambda_fn_decl(&self,
942 fn_decl: P<ast::FnDecl>,
944 fn_decl_span: Span) // span of the `|...|` part
946 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
954 ids: Vec<ast::Ident>,
957 let fn_decl = self.fn_decl(
958 ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
959 self.ty_infer(span));
961 // FIXME -- We are using `span` as the span of the `|...|`
962 // part of the lambda, but it probably (maybe?) corresponds to
963 // the entire lambda body. Probably we should extend the API
964 // here, but that's not entirely clear.
965 self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk, span))
968 fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
969 self.lambda(span, Vec::new(), blk)
972 fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
973 self.lambda(span, vec!(ident), blk)
976 fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
977 expr: P<ast::Expr>) -> P<ast::Expr> {
978 self.lambda(span, ids, self.block_expr(expr))
980 fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
981 self.lambda0(span, self.block_expr(expr))
983 fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
984 self.lambda1(span, self.block_expr(expr), ident)
987 fn lambda_stmts(&self,
989 ids: Vec<ast::Ident>,
990 stmts: Vec<ast::Stmt>)
992 self.lambda(span, ids, self.block(span, stmts))
994 fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
995 self.lambda0(span, self.block(span, stmts))
997 fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
998 ident: ast::Ident) -> P<ast::Expr> {
999 self.lambda1(span, self.block(span, stmts), ident)
1002 fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
1003 let arg_pat = self.pat_ident(span, ident);
1007 id: ast::DUMMY_NODE_ID
1011 // FIXME unused self
1012 fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
1015 output: ast::FunctionRetTy::Ty(output),
1020 fn item(&self, span: Span, name: Ident,
1021 attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
1022 // FIXME: Would be nice if our generated code didn't violate
1023 // Rust coding conventions
1027 id: ast::DUMMY_NODE_ID,
1029 vis: ast::Visibility::Inherited,
1034 fn item_fn_poly(&self,
1037 inputs: Vec<ast::Arg> ,
1040 body: P<ast::Block>) -> P<ast::Item> {
1044 ast::ItemKind::Fn(self.fn_decl(inputs, output),
1045 ast::Unsafety::Normal,
1046 dummy_spanned(ast::Constness::NotConst),
1055 inputs: Vec<ast::Arg> ,
1064 Generics::default(),
1068 fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
1069 let fields: Vec<_> = tys.into_iter().map(|ty| {
1074 vis: ast::Visibility::Inherited,
1076 id: ast::DUMMY_NODE_ID,
1080 let vdata = if fields.is_empty() {
1081 ast::VariantData::Unit(ast::DUMMY_NODE_ID)
1083 ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
1095 fn item_enum_poly(&self, span: Span, name: Ident,
1096 enum_definition: ast::EnumDef,
1097 generics: Generics) -> P<ast::Item> {
1098 self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
1101 fn item_enum(&self, span: Span, name: Ident,
1102 enum_definition: ast::EnumDef) -> P<ast::Item> {
1103 self.item_enum_poly(span, name, enum_definition,
1104 Generics::default())
1107 fn item_struct(&self, span: Span, name: Ident,
1108 struct_def: ast::VariantData) -> P<ast::Item> {
1109 self.item_struct_poly(
1117 fn item_struct_poly(&self, span: Span, name: Ident,
1118 struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
1119 self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
1122 fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1123 attrs: Vec<ast::Attribute>,
1124 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1129 ast::ItemKind::Mod(ast::Mod {
1136 fn item_static(&self,
1140 mutbl: ast::Mutability,
1143 self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
1146 fn item_const(&self,
1152 self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
1155 fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1156 generics: Generics) -> P<ast::Item> {
1157 self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
1160 fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1161 self.item_ty_poly(span, name, ty, Generics::default())
1164 fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1165 attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
1168 fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1169 attr::mk_spanned_word_item(sp, w)
1172 fn meta_list_item_word(&self, sp: Span, w: InternedString) -> ast::NestedMetaItem {
1173 respan(sp, ast::NestedMetaItemKind::MetaItem(attr::mk_spanned_word_item(sp, w)))
1176 fn meta_list(&self, sp: Span, name: InternedString, mis: Vec<ast::NestedMetaItem>)
1177 -> P<ast::MetaItem> {
1178 attr::mk_spanned_list_item(sp, name, mis)
1181 fn meta_name_value(&self, sp: Span, name: InternedString, value: ast::LitKind)
1182 -> P<ast::MetaItem> {
1183 attr::mk_spanned_name_value_item(sp, name, respan(sp, value))
1186 fn item_use(&self, sp: Span,
1187 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
1189 id: ast::DUMMY_NODE_ID,
1190 ident: keywords::Invalid.ident(),
1192 node: ast::ItemKind::Use(vp),
1198 fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1199 let last = path.segments.last().unwrap().identifier;
1200 self.item_use_simple_(sp, vis, last, path)
1203 fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1204 ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
1205 self.item_use(sp, vis,
1207 ast::ViewPathSimple(ident,
1211 fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1212 path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1213 let imports = imports.iter().map(|id| {
1214 let item = ast::PathListItem_ {
1217 id: ast::DUMMY_NODE_ID,
1222 self.item_use(sp, vis,
1224 ast::ViewPathList(self.path(sp, path),
1228 fn item_use_glob(&self, sp: Span,
1229 vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1230 self.item_use(sp, vis,
1232 ast::ViewPathGlob(self.path(sp, path)))))