]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/build.rs
Rollup merge of #28680 - xaviershay:doc-readme, r=steveklabnik
[rust.git] / src / libsyntax / ext / build.rs
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.
4 //
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.
10
11 use abi;
12 use ast::{Ident, Generics, Expr};
13 use ast;
14 use ast_util;
15 use attr;
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;
21 use parse::token;
22 use ptr::P;
23
24 // Transitional reexports so qquote can find the paths it is looking for
25 mod syntax {
26     pub use ext;
27     pub use parse;
28 }
29
30 pub trait AstBuilder {
31     // paths
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,
36                 global: bool,
37                 idents: Vec<ast::Ident> ,
38                 lifetimes: Vec<ast::Lifetime>,
39                 types: Vec<P<ast::Ty>>,
40                 bindings: Vec<P<ast::TypeBinding>> )
41         -> ast::Path;
42
43     fn qpath(&self, self_type: P<ast::Ty>,
44              trait_path: ast::Path,
45              ident: ast::Ident)
46              -> (ast::QSelf, ast::Path);
47     fn qpath_all(&self, self_type: P<ast::Ty>,
48                 trait_path: ast::Path,
49                 ident: ast::Ident,
50                 lifetimes: Vec<ast::Lifetime>,
51                 types: Vec<P<ast::Ty>>,
52                 bindings: Vec<P<ast::TypeBinding>>)
53                 -> (ast::QSelf, ast::Path);
54
55     // types
56     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
57
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>;
62
63     fn ty_rptr(&self, span: Span,
64                ty: P<ast::Ty>,
65                lifetime: Option<ast::Lifetime>,
66                mutbl: ast::Mutability) -> P<ast::Ty>;
67     fn ty_ptr(&self, span: Span,
68               ty: P<ast::Ty>,
69               mutbl: ast::Mutability) -> P<ast::Ty>;
70
71     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
72     fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
73
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
77     fn typaram(&self,
78                span: Span,
79                id: ast::Ident,
80                bounds: OwnedSlice<ast::TyParamBound>,
81                default: Option<P<ast::Ty>>) -> ast::TyParam;
82
83     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
84     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
85     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
86     fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
87     fn lifetime_def(&self,
88                     span: Span,
89                     name: ast::Name,
90                     bounds: Vec<ast::Lifetime>)
91                     -> ast::LifetimeDef;
92
93     // statements
94     fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt>;
95     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> P<ast::Stmt>;
96     fn stmt_let_typed(&self,
97                       sp: Span,
98                       mutbl: bool,
99                       ident: ast::Ident,
100                       typ: P<ast::Ty>,
101                       ex: P<ast::Expr>)
102                       -> P<ast::Stmt>;
103     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt>;
104
105     // blocks
106     fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
107              expr: Option<P<ast::Expr>>) -> P<ast::Block>;
108     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
109     fn block_all(&self, span: Span,
110                  stmts: Vec<P<ast::Stmt>>,
111                  expr: Option<P<ast::Expr>>) -> P<ast::Block>;
112
113     // expressions
114     fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>;
115     fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
116     fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
117     fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
118
119     fn expr_self(&self, span: Span) -> P<ast::Expr>;
120     fn expr_binary(&self, sp: Span, op: ast::BinOp_,
121                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
122     fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
123     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
124
125     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
126     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
127     fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
128     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
129                              idx: usize) -> P<ast::Expr>;
130     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
131     fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
132     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
133                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
134     fn expr_method_call(&self, span: Span,
135                         expr: P<ast::Expr>, ident: ast::Ident,
136                         args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
137     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
138     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
139
140     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
141     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
142     fn expr_struct_ident(&self, span: Span, id: ast::Ident,
143                          fields: Vec<ast::Field>) -> P<ast::Expr>;
144
145     fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>;
146
147     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
148     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
149     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
150     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
151     fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
152
153     fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
154     fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
155     fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
156     fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
157
158     fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
159     fn expr_none(&self, sp: Span) -> P<ast::Expr>;
160
161     fn expr_break(&self, sp: Span) -> P<ast::Expr>;
162
163     fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
164
165     fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
166     fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
167
168     fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
169     fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
170     fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
171
172     fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
173     fn pat_wild(&self, span: Span) -> P<ast::Pat>;
174     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
175     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
176
177     fn pat_ident_binding_mode(&self,
178                               span: Span,
179                               ident: ast::Ident,
180                               bm: ast::BindingMode) -> P<ast::Pat>;
181     fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
182     fn pat_struct(&self, span: Span,
183                   path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>> ) -> P<ast::Pat>;
184     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
185
186     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
187     fn pat_none(&self, span: Span) -> P<ast::Pat>;
188
189     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
190     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
191
192     fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
193     fn arm_unreachable(&self, span: Span) -> ast::Arm;
194
195     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
196     fn expr_if(&self, span: Span,
197                cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
198     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
199
200     fn lambda_fn_decl(&self, span: Span,
201                       fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
202
203     fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
204     fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
205     fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
206
207     fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
208     fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
209     fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
210
211     fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
212                     blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
213     fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
214     fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
215                       ident: ast::Ident) -> P<ast::Expr>;
216
217     // items
218     fn item(&self, span: Span,
219             name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>;
220
221     fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
222     // FIXME unused self
223     fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
224
225     fn item_fn_poly(&self,
226                     span: Span,
227                     name: Ident,
228                     inputs: Vec<ast::Arg> ,
229                     output: P<ast::Ty>,
230                     generics: Generics,
231                     body: P<ast::Block>) -> P<ast::Item>;
232     fn item_fn(&self,
233                span: Span,
234                name: Ident,
235                inputs: Vec<ast::Arg> ,
236                output: P<ast::Ty>,
237                body: P<ast::Block>) -> P<ast::Item>;
238
239     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
240     fn item_enum_poly(&self,
241                       span: Span,
242                       name: Ident,
243                       enum_definition: ast::EnumDef,
244                       generics: Generics) -> P<ast::Item>;
245     fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
246
247     fn item_struct_poly(&self,
248                         span: Span,
249                         name: Ident,
250                         struct_def: ast::StructDef,
251                         generics: Generics) -> P<ast::Item>;
252     fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> P<ast::Item>;
253
254     fn item_mod(&self, span: Span, inner_span: Span,
255                 name: Ident, attrs: Vec<ast::Attribute>,
256                 items: Vec<P<ast::Item>>) -> P<ast::Item>;
257
258     fn item_static(&self,
259                    span: Span,
260                    name: Ident,
261                    ty: P<ast::Ty>,
262                    mutbl: ast::Mutability,
263                    expr: P<ast::Expr>)
264                    -> P<ast::Item>;
265
266     fn item_const(&self,
267                    span: Span,
268                    name: Ident,
269                    ty: P<ast::Ty>,
270                    expr: P<ast::Expr>)
271                    -> P<ast::Item>;
272
273     fn item_ty_poly(&self,
274                     span: Span,
275                     name: Ident,
276                     ty: P<ast::Ty>,
277                     generics: Generics) -> P<ast::Item>;
278     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
279
280     fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
281
282     fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
283     fn meta_list(&self,
284                  sp: Span,
285                  name: InternedString,
286                  mis: Vec<P<ast::MetaItem>> )
287                  -> P<ast::MetaItem>;
288     fn meta_name_value(&self,
289                        sp: Span,
290                        name: InternedString,
291                        value: ast::Lit_)
292                        -> P<ast::MetaItem>;
293
294     fn item_use(&self, sp: Span,
295                 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
296     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
297     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
298                         ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
299     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
300                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
301     fn item_use_glob(&self, sp: Span,
302                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
303 }
304
305 impl<'a> AstBuilder for ExtCtxt<'a> {
306     fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
307         self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
308     }
309     fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
310         self.path(span, vec!(id))
311     }
312     fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
313         self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
314     }
315     fn path_all(&self,
316                 sp: Span,
317                 global: bool,
318                 mut idents: Vec<ast::Ident> ,
319                 lifetimes: Vec<ast::Lifetime>,
320                 types: Vec<P<ast::Ty>>,
321                 bindings: Vec<P<ast::TypeBinding>> )
322                 -> ast::Path {
323         let last_identifier = idents.pop().unwrap();
324         let mut segments: Vec<ast::PathSegment> = idents.into_iter()
325                                                       .map(|ident| {
326             ast::PathSegment {
327                 identifier: ident,
328                 parameters: ast::PathParameters::none(),
329             }
330         }).collect();
331         segments.push(ast::PathSegment {
332             identifier: last_identifier,
333             parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
334                 lifetimes: lifetimes,
335                 types: OwnedSlice::from_vec(types),
336                 bindings: OwnedSlice::from_vec(bindings),
337             })
338         });
339         ast::Path {
340             span: sp,
341             global: global,
342             segments: segments,
343         }
344     }
345
346     /// Constructs a qualified path.
347     ///
348     /// Constructs a path like `<self_type as trait_path>::ident`.
349     fn qpath(&self,
350              self_type: P<ast::Ty>,
351              trait_path: ast::Path,
352              ident: ast::Ident)
353              -> (ast::QSelf, ast::Path) {
354         self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
355     }
356
357     /// Constructs a qualified path.
358     ///
359     /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
360     fn qpath_all(&self,
361                  self_type: P<ast::Ty>,
362                  trait_path: ast::Path,
363                  ident: ast::Ident,
364                  lifetimes: Vec<ast::Lifetime>,
365                  types: Vec<P<ast::Ty>>,
366                  bindings: Vec<P<ast::TypeBinding>>)
367                  -> (ast::QSelf, ast::Path) {
368         let mut path = trait_path;
369         path.segments.push(ast::PathSegment {
370             identifier: ident,
371             parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
372                 lifetimes: lifetimes,
373                 types: OwnedSlice::from_vec(types),
374                 bindings: OwnedSlice::from_vec(bindings),
375             })
376         });
377
378         (ast::QSelf {
379             ty: self_type,
380             position: path.segments.len() - 1
381         }, path)
382     }
383
384     fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
385         ast::MutTy {
386             ty: ty,
387             mutbl: mutbl
388         }
389     }
390
391     fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
392         P(ast::Ty {
393             id: ast::DUMMY_NODE_ID,
394             span: span,
395             node: ty
396         })
397     }
398
399     fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
400         self.ty(path.span, ast::TyPath(None, path))
401     }
402
403     fn ty_sum(&self, path: ast::Path, bounds: OwnedSlice<ast::TyParamBound>) -> P<ast::Ty> {
404         self.ty(path.span,
405                 ast::TyObjectSum(self.ty_path(path),
406                                  bounds))
407     }
408
409     // Might need to take bounds as an argument in the future, if you ever want
410     // to generate a bounded existential trait type.
411     fn ty_ident(&self, span: Span, ident: ast::Ident)
412         -> P<ast::Ty> {
413         self.ty_path(self.path_ident(span, ident))
414     }
415
416     fn ty_rptr(&self,
417                span: Span,
418                ty: P<ast::Ty>,
419                lifetime: Option<ast::Lifetime>,
420                mutbl: ast::Mutability)
421         -> P<ast::Ty> {
422         self.ty(span,
423                 ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
424     }
425
426     fn ty_ptr(&self,
427               span: Span,
428               ty: P<ast::Ty>,
429               mutbl: ast::Mutability)
430         -> P<ast::Ty> {
431         self.ty(span,
432                 ast::TyPtr(self.ty_mt(ty, mutbl)))
433     }
434
435     fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
436         self.ty_path(
437             self.path_all(DUMMY_SP,
438                           true,
439                           self.std_path(&["option", "Option"]),
440                           Vec::new(),
441                           vec!( ty ),
442                           Vec::new()))
443     }
444
445     fn ty_infer(&self, span: Span) -> P<ast::Ty> {
446         self.ty(span, ast::TyInfer)
447     }
448
449     fn typaram(&self,
450                span: Span,
451                id: ast::Ident,
452                bounds: OwnedSlice<ast::TyParamBound>,
453                default: Option<P<ast::Ty>>) -> ast::TyParam {
454         ast::TyParam {
455             ident: id,
456             id: ast::DUMMY_NODE_ID,
457             bounds: bounds,
458             default: default,
459             span: span
460         }
461     }
462
463     // these are strange, and probably shouldn't be used outside of
464     // pipes. Specifically, the global version possible generates
465     // incorrect code.
466     fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
467         ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
468     }
469
470     fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
471         ty_params
472             .iter()
473             .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
474             .collect()
475     }
476
477     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
478         ast::TraitRef {
479             path: path,
480             ref_id: ast::DUMMY_NODE_ID,
481         }
482     }
483
484     fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
485         ast::PolyTraitRef {
486             bound_lifetimes: Vec::new(),
487             trait_ref: self.trait_ref(path),
488             span: span,
489         }
490     }
491
492     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
493         ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
494     }
495
496     fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
497         ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
498     }
499
500     fn lifetime_def(&self,
501                     span: Span,
502                     name: ast::Name,
503                     bounds: Vec<ast::Lifetime>)
504                     -> ast::LifetimeDef {
505         ast::LifetimeDef {
506             lifetime: self.lifetime(span, name),
507             bounds: bounds
508         }
509     }
510
511     fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> {
512         P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)))
513     }
514
515     fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
516                 ex: P<ast::Expr>) -> P<ast::Stmt> {
517         let pat = if mutbl {
518             self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
519         } else {
520             self.pat_ident(sp, ident)
521         };
522         let local = P(ast::Local {
523             pat: pat,
524             ty: None,
525             init: Some(ex),
526             id: ast::DUMMY_NODE_ID,
527             span: sp,
528         });
529         let decl = respan(sp, ast::DeclLocal(local));
530         P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
531     }
532
533     fn stmt_let_typed(&self,
534                       sp: Span,
535                       mutbl: bool,
536                       ident: ast::Ident,
537                       typ: P<ast::Ty>,
538                       ex: P<ast::Expr>)
539                       -> P<ast::Stmt> {
540         let pat = if mutbl {
541             self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
542         } else {
543             self.pat_ident(sp, ident)
544         };
545         let local = P(ast::Local {
546             pat: pat,
547             ty: Some(typ),
548             init: Some(ex),
549             id: ast::DUMMY_NODE_ID,
550             span: sp,
551         });
552         let decl = respan(sp, ast::DeclLocal(local));
553         P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
554     }
555
556     fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
557              expr: Option<P<Expr>>) -> P<ast::Block> {
558         self.block_all(span, stmts, expr)
559     }
560
561     fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
562         let decl = respan(sp, ast::DeclItem(item));
563         P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
564     }
565
566     fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
567         self.block_all(expr.span, Vec::new(), Some(expr))
568     }
569     fn block_all(&self,
570                  span: Span,
571                  stmts: Vec<P<ast::Stmt>>,
572                  expr: Option<P<ast::Expr>>) -> P<ast::Block> {
573             P(ast::Block {
574                stmts: stmts,
575                expr: expr,
576                id: ast::DUMMY_NODE_ID,
577                rules: ast::DefaultBlock,
578                span: span,
579             })
580     }
581
582     fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> {
583         P(ast::Expr {
584             id: ast::DUMMY_NODE_ID,
585             node: node,
586             span: span,
587         })
588     }
589
590     fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
591         self.expr(path.span, ast::ExprPath(None, path))
592     }
593
594     /// Constructs a QPath expression.
595     fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
596         self.expr(span, ast::ExprPath(Some(qself), path))
597     }
598
599     fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
600         self.expr_path(self.path_ident(span, id))
601     }
602     fn expr_self(&self, span: Span) -> P<ast::Expr> {
603         self.expr_ident(span, special_idents::self_)
604     }
605
606     fn expr_binary(&self, sp: Span, op: ast::BinOp_,
607                    lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
608         self.expr(sp, ast::ExprBinary(Spanned { node: op, span: sp }, lhs, rhs))
609     }
610
611     fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
612         self.expr_unary(sp, ast::UnDeref, e)
613     }
614     fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
615         self.expr(sp, ast::ExprUnary(op, e))
616     }
617
618     fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
619         let field_span = Span {
620             lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
621             hi: sp.hi,
622             expn_id: sp.expn_id,
623         };
624
625         let id = Spanned { node: ident, span: field_span };
626         self.expr(sp, ast::ExprField(expr, id))
627     }
628     fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
629         let field_span = Span {
630             lo: sp.lo - Pos::from_usize(idx.to_string().len()),
631             hi: sp.hi,
632             expn_id: sp.expn_id,
633         };
634
635         let id = Spanned { node: idx, span: field_span };
636         self.expr(sp, ast::ExprTupField(expr, id))
637     }
638     fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
639         self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
640     }
641     fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
642         self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
643     }
644
645     fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
646         self.expr(span, ast::ExprCall(expr, args))
647     }
648     fn expr_call_ident(&self, span: Span, id: ast::Ident,
649                        args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
650         self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
651     }
652     fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
653                       args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
654         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
655         self.expr_call(sp, pathexpr, args)
656     }
657     fn expr_method_call(&self, span: Span,
658                         expr: P<ast::Expr>,
659                         ident: ast::Ident,
660                         mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
661         let id = Spanned { node: ident, span: span };
662         args.insert(0, expr);
663         self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
664     }
665     fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
666         self.expr(b.span, ast::ExprBlock(b))
667     }
668     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
669         ast::Field { ident: respan(span, name), expr: e, span: span }
670     }
671     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
672         self.expr(span, ast::ExprStruct(path, fields, None))
673     }
674     fn expr_struct_ident(&self, span: Span,
675                          id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
676         self.expr_struct(span, self.path_ident(span, id), fields)
677     }
678
679     fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> {
680         self.expr(sp, ast::ExprLit(P(respan(sp, lit))))
681     }
682     fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
683         self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs)))
684     }
685     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
686         self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs,
687                                                                   ast::Sign::new(i))))
688     }
689     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
690         self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))
691     }
692     fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
693         self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
694     }
695     fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
696         self.expr_lit(sp, ast::LitBool(value))
697     }
698
699     fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
700         self.expr(sp, ast::ExprVec(exprs))
701     }
702     fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
703         self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
704                               Vec::new())
705     }
706     fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
707         self.expr_addr_of(sp, self.expr_vec(sp, exprs))
708     }
709     fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
710         self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
711     }
712
713     fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
714         self.expr(sp, ast::ExprCast(expr, ty))
715     }
716
717
718     fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
719         let some = self.std_path(&["option", "Option", "Some"]);
720         self.expr_call_global(sp, some, vec!(expr))
721     }
722
723     fn expr_none(&self, sp: Span) -> P<ast::Expr> {
724         let none = self.std_path(&["option", "Option", "None"]);
725         let none = self.path_global(sp, none);
726         self.expr_path(none)
727     }
728
729
730     fn expr_break(&self, sp: Span) -> P<ast::Expr> {
731         self.expr(sp, ast::ExprBreak(None))
732     }
733
734
735     fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
736         self.expr(sp, ast::ExprTup(exprs))
737     }
738
739     fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
740         let loc = self.codemap().lookup_char_pos(span.lo);
741         let expr_file = self.expr_str(span,
742                                       token::intern_and_get_ident(&loc.file.name));
743         let expr_line = self.expr_u32(span, loc.line as u32);
744         let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
745         let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
746         self.expr_call_global(
747             span,
748             self.std_path(&["rt", "begin_unwind"]),
749             vec!(
750                 self.expr_str(span, msg),
751                 expr_file_line_ptr))
752     }
753
754     fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
755         self.expr_fail(span,
756                        InternedString::new(
757                            "internal error: entered unreachable code"))
758     }
759
760     fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
761         let ok = self.std_path(&["result", "Result", "Ok"]);
762         self.expr_call_global(sp, ok, vec!(expr))
763     }
764
765     fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
766         let err = self.std_path(&["result", "Result", "Err"]);
767         self.expr_call_global(sp, err, vec!(expr))
768     }
769
770     fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
771         let ok = self.std_path(&["result", "Result", "Ok"]);
772         let ok_path = self.path_global(sp, ok);
773         let err = self.std_path(&["result", "Result", "Err"]);
774         let err_path = self.path_global(sp, err);
775
776         let binding_variable = self.ident_of("__try_var");
777         let binding_pat = self.pat_ident(sp, binding_variable);
778         let binding_expr = self.expr_ident(sp, binding_variable);
779
780         // Ok(__try_var) pattern
781         let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
782
783         // Err(__try_var)  (pattern and expression resp.)
784         let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat));
785         let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
786                                             vec!(binding_expr.clone()));
787         // return Err(__try_var)
788         let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
789
790         // Ok(__try_var) => __try_var
791         let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
792         // Err(__try_var) => return Err(__try_var)
793         let err_arm = self.arm(sp, vec!(err_pat), err_expr);
794
795         // match head { Ok() => ..., Err() => ... }
796         self.expr_match(sp, head, vec!(ok_arm, err_arm))
797     }
798
799
800     fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
801         P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
802     }
803     fn pat_wild(&self, span: Span) -> P<ast::Pat> {
804         self.pat(span, ast::PatWild(ast::PatWildSingle))
805     }
806     fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
807         self.pat(span, ast::PatLit(expr))
808     }
809     fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
810         self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
811     }
812
813     fn pat_ident_binding_mode(&self,
814                               span: Span,
815                               ident: ast::Ident,
816                               bm: ast::BindingMode) -> P<ast::Pat> {
817         let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
818         self.pat(span, pat)
819     }
820     fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
821         let pat = ast::PatEnum(path, Some(subpats));
822         self.pat(span, pat)
823     }
824     fn pat_struct(&self, span: Span,
825                   path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
826         let pat = ast::PatStruct(path, field_pats, false);
827         self.pat(span, pat)
828     }
829     fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
830         self.pat(span, ast::PatTup(pats))
831     }
832
833     fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
834         let some = self.std_path(&["option", "Option", "Some"]);
835         let path = self.path_global(span, some);
836         self.pat_enum(span, path, vec!(pat))
837     }
838
839     fn pat_none(&self, span: Span) -> P<ast::Pat> {
840         let some = self.std_path(&["option", "Option", "None"]);
841         let path = self.path_global(span, some);
842         self.pat_enum(span, path, vec!())
843     }
844
845     fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
846         let some = self.std_path(&["result", "Result", "Ok"]);
847         let path = self.path_global(span, some);
848         self.pat_enum(span, path, vec!(pat))
849     }
850
851     fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
852         let some = self.std_path(&["result", "Result", "Err"]);
853         let path = self.path_global(span, some);
854         self.pat_enum(span, path, vec!(pat))
855     }
856
857     fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
858         ast::Arm {
859             attrs: vec!(),
860             pats: pats,
861             guard: None,
862             body: expr
863         }
864     }
865
866     fn arm_unreachable(&self, span: Span) -> ast::Arm {
867         self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
868     }
869
870     fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
871         self.expr(span, ast::ExprMatch(arg, arms, ast::MatchSource::Normal))
872     }
873
874     fn expr_if(&self, span: Span, cond: P<ast::Expr>,
875                then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
876         let els = els.map(|x| self.expr_block(self.block_expr(x)));
877         self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
878     }
879
880     fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
881         self.expr(span, ast::ExprLoop(block, None))
882     }
883
884     fn lambda_fn_decl(&self, span: Span,
885                       fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
886         self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
887     }
888     fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
889         let fn_decl = self.fn_decl(
890             ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
891             self.ty_infer(span));
892
893         self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
894     }
895     fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
896         self.lambda(span, Vec::new(), blk)
897     }
898
899     fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
900         self.lambda(span, vec!(ident), blk)
901     }
902
903     fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
904                    expr: P<ast::Expr>) -> P<ast::Expr> {
905         self.lambda(span, ids, self.block_expr(expr))
906     }
907     fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
908         self.lambda0(span, self.block_expr(expr))
909     }
910     fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
911         self.lambda1(span, self.block_expr(expr), ident)
912     }
913
914     fn lambda_stmts(&self,
915                     span: Span,
916                     ids: Vec<ast::Ident>,
917                     stmts: Vec<P<ast::Stmt>>)
918                     -> P<ast::Expr> {
919         self.lambda(span, ids, self.block(span, stmts, None))
920     }
921     fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> {
922         self.lambda0(span, self.block(span, stmts, None))
923     }
924     fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
925                       ident: ast::Ident) -> P<ast::Expr> {
926         self.lambda1(span, self.block(span, stmts, None), ident)
927     }
928
929     fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
930         let arg_pat = self.pat_ident(span, ident);
931         ast::Arg {
932             ty: ty,
933             pat: arg_pat,
934             id: ast::DUMMY_NODE_ID
935         }
936     }
937
938     // FIXME unused self
939     fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
940         P(ast::FnDecl {
941             inputs: inputs,
942             output: ast::Return(output),
943             variadic: false
944         })
945     }
946
947     fn item(&self, span: Span, name: Ident,
948             attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> {
949         // FIXME: Would be nice if our generated code didn't violate
950         // Rust coding conventions
951         P(ast::Item {
952             ident: name,
953             attrs: attrs,
954             id: ast::DUMMY_NODE_ID,
955             node: node,
956             vis: ast::Inherited,
957             span: span
958         })
959     }
960
961     fn item_fn_poly(&self,
962                     span: Span,
963                     name: Ident,
964                     inputs: Vec<ast::Arg> ,
965                     output: P<ast::Ty>,
966                     generics: Generics,
967                     body: P<ast::Block>) -> P<ast::Item> {
968         self.item(span,
969                   name,
970                   Vec::new(),
971                   ast::ItemFn(self.fn_decl(inputs, output),
972                               ast::Unsafety::Normal,
973                               ast::Constness::NotConst,
974                               abi::Rust,
975                               generics,
976                               body))
977     }
978
979     fn item_fn(&self,
980                span: Span,
981                name: Ident,
982                inputs: Vec<ast::Arg> ,
983                output: P<ast::Ty>,
984                body: P<ast::Block>
985               ) -> P<ast::Item> {
986         self.item_fn_poly(
987             span,
988             name,
989             inputs,
990             output,
991             ast_util::empty_generics(),
992             body)
993     }
994
995     fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
996         let args = tys.into_iter().map(|ty| {
997             ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
998         }).collect();
999
1000         respan(span,
1001                ast::Variant_ {
1002                    name: name,
1003                    attrs: Vec::new(),
1004                    kind: ast::TupleVariantKind(args),
1005                    id: ast::DUMMY_NODE_ID,
1006                    disr_expr: None,
1007                })
1008     }
1009
1010     fn item_enum_poly(&self, span: Span, name: Ident,
1011                       enum_definition: ast::EnumDef,
1012                       generics: Generics) -> P<ast::Item> {
1013         self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
1014     }
1015
1016     fn item_enum(&self, span: Span, name: Ident,
1017                  enum_definition: ast::EnumDef) -> P<ast::Item> {
1018         self.item_enum_poly(span, name, enum_definition,
1019                             ast_util::empty_generics())
1020     }
1021
1022     fn item_struct(&self, span: Span, name: Ident,
1023                    struct_def: ast::StructDef) -> P<ast::Item> {
1024         self.item_struct_poly(
1025             span,
1026             name,
1027             struct_def,
1028             ast_util::empty_generics()
1029         )
1030     }
1031
1032     fn item_struct_poly(&self, span: Span, name: Ident,
1033         struct_def: ast::StructDef, generics: Generics) -> P<ast::Item> {
1034         self.item(span, name, Vec::new(), ast::ItemStruct(P(struct_def), generics))
1035     }
1036
1037     fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
1038                 attrs: Vec<ast::Attribute>,
1039                 items: Vec<P<ast::Item>>) -> P<ast::Item> {
1040         self.item(
1041             span,
1042             name,
1043             attrs,
1044             ast::ItemMod(ast::Mod {
1045                 inner: inner_span,
1046                 items: items,
1047             })
1048         )
1049     }
1050
1051     fn item_static(&self,
1052                    span: Span,
1053                    name: Ident,
1054                    ty: P<ast::Ty>,
1055                    mutbl: ast::Mutability,
1056                    expr: P<ast::Expr>)
1057                    -> P<ast::Item> {
1058         self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
1059     }
1060
1061     fn item_const(&self,
1062                   span: Span,
1063                   name: Ident,
1064                   ty: P<ast::Ty>,
1065                   expr: P<ast::Expr>)
1066                   -> P<ast::Item> {
1067         self.item(span, name, Vec::new(), ast::ItemConst(ty, expr))
1068     }
1069
1070     fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
1071                     generics: Generics) -> P<ast::Item> {
1072         self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
1073     }
1074
1075     fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
1076         self.item_ty_poly(span, name, ty, ast_util::empty_generics())
1077     }
1078
1079     fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
1080         respan(sp, ast::Attribute_ {
1081             id: attr::mk_attr_id(),
1082             style: ast::AttrOuter,
1083             value: mi,
1084             is_sugared_doc: false,
1085         })
1086     }
1087
1088     fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
1089         P(respan(sp, ast::MetaWord(w)))
1090     }
1091     fn meta_list(&self,
1092                  sp: Span,
1093                  name: InternedString,
1094                  mis: Vec<P<ast::MetaItem>> )
1095                  -> P<ast::MetaItem> {
1096         P(respan(sp, ast::MetaList(name, mis)))
1097     }
1098     fn meta_name_value(&self,
1099                        sp: Span,
1100                        name: InternedString,
1101                        value: ast::Lit_)
1102                        -> P<ast::MetaItem> {
1103         P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
1104     }
1105
1106     fn item_use(&self, sp: Span,
1107                 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
1108         P(ast::Item {
1109             id: ast::DUMMY_NODE_ID,
1110             ident: special_idents::invalid,
1111             attrs: vec![],
1112             node: ast::ItemUse(vp),
1113             vis: vis,
1114             span: sp
1115         })
1116     }
1117
1118     fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
1119         let last = path.segments.last().unwrap().identifier;
1120         self.item_use_simple_(sp, vis, last, path)
1121     }
1122
1123     fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
1124                         ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
1125         self.item_use(sp, vis,
1126                       P(respan(sp,
1127                                ast::ViewPathSimple(ident,
1128                                                    path))))
1129     }
1130
1131     fn item_use_list(&self, sp: Span, vis: ast::Visibility,
1132                      path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
1133         let imports = imports.iter().map(|id| {
1134             respan(sp, ast::PathListIdent { name: *id, rename: None, id: ast::DUMMY_NODE_ID })
1135         }).collect();
1136
1137         self.item_use(sp, vis,
1138                       P(respan(sp,
1139                                ast::ViewPathList(self.path(sp, path),
1140                                                  imports))))
1141     }
1142
1143     fn item_use_glob(&self, sp: Span,
1144                      vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
1145         self.item_use(sp, vis,
1146                       P(respan(sp,
1147                                ast::ViewPathGlob(self.path(sp, path)))))
1148     }
1149 }