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