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