]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/format.rs
libsyntax: Mechanically change `~[T]` to `Vec<T>`
[rust.git] / src / libsyntax / ext / format.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 ast;
12 use ast::P;
13 use codemap::{Span, respan};
14 use ext::base::*;
15 use ext::base;
16 use ext::build::AstBuilder;
17 use opt_vec;
18 use parse::token::InternedString;
19 use parse::token;
20 use rsparse = parse;
21
22 use std::fmt::parse;
23 use collections::{HashMap, HashSet};
24 use std::vec;
25
26 #[deriving(Eq)]
27 enum ArgumentType {
28     Known(~str),
29     Unsigned,
30     String,
31 }
32
33 enum Position {
34     Exact(uint),
35     Named(~str),
36 }
37
38 struct Context<'a> {
39     ecx: &'a mut ExtCtxt<'a>,
40     fmtsp: Span,
41
42     // Parsed argument expressions and the types that we've found so far for
43     // them.
44     args: Vec<@ast::Expr>,
45     arg_types: Vec<Option<ArgumentType>>,
46     // Parsed named expressions and the types that we've found for them so far.
47     // Note that we keep a side-array of the ordering of the named arguments
48     // found to be sure that we can translate them in the same order that they
49     // were declared in.
50     names: HashMap<~str, @ast::Expr>,
51     name_types: HashMap<~str, ArgumentType>,
52     name_ordering: ~[~str],
53
54     // Collection of the compiled `rt::Piece` structures
55     pieces: Vec<@ast::Expr> ,
56     name_positions: HashMap<~str, uint>,
57     method_statics: Vec<@ast::Item> ,
58
59     // Updated as arguments are consumed or methods are entered
60     nest_level: uint,
61     next_arg: uint,
62 }
63
64 /// Parses the arguments from the given list of tokens, returning None
65 /// if there's a parse error so we can continue parsing other format!
66 /// expressions.
67 ///
68 /// If parsing succeeds, the second return value is:
69 ///
70 ///     Some((fmtstr, unnamed arguments, ordering of named arguments,
71 ///           named arguments))
72 fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
73     -> (@ast::Expr, Option<(@ast::Expr, Vec<@ast::Expr>, ~[~str],
74                             HashMap<~str, @ast::Expr>)>) {
75     let mut args = Vec::new();
76     let mut names = HashMap::<~str, @ast::Expr>::new();
77     let mut order = ~[];
78
79     let mut p = rsparse::new_parser_from_tts(ecx.parse_sess(),
80                                              ecx.cfg(),
81                                              tts.to_owned());
82     // Parse the leading function expression (maybe a block, maybe a path)
83     let extra = p.parse_expr();
84     if !p.eat(&token::COMMA) {
85         ecx.span_err(sp, "expected token: `,`");
86         return (extra, None);
87     }
88
89     if p.token == token::EOF {
90         ecx.span_err(sp, "requires at least a format string argument");
91         return (extra, None);
92     }
93     let fmtstr = p.parse_expr();
94     let mut named = false;
95     while p.token != token::EOF {
96         if !p.eat(&token::COMMA) {
97             ecx.span_err(sp, "expected token: `,`");
98             return (extra, None);
99         }
100         if p.token == token::EOF { break } // accept trailing commas
101         if named || (token::is_ident(&p.token) &&
102                      p.look_ahead(1, |t| *t == token::EQ)) {
103             named = true;
104             let ident = match p.token {
105                 token::IDENT(i, _) => {
106                     p.bump();
107                     i
108                 }
109                 _ if named => {
110                     ecx.span_err(p.span,
111                                  "expected ident, positional arguments \
112                                  cannot follow named arguments");
113                     return (extra, None);
114                 }
115                 _ => {
116                     ecx.span_err(p.span,
117                                  format!("expected ident for named argument, but found `{}`",
118                                          p.this_token_to_str()));
119                     return (extra, None);
120                 }
121             };
122             let interned_name = token::get_ident(ident);
123             let name = interned_name.get();
124             p.expect(&token::EQ);
125             let e = p.parse_expr();
126             match names.find_equiv(&name) {
127                 None => {}
128                 Some(prev) => {
129                     ecx.span_err(e.span, format!("duplicate argument named `{}`", name));
130                     ecx.parse_sess.span_diagnostic.span_note(prev.span, "previously here");
131                     continue
132                 }
133             }
134             order.push(name.to_str());
135             names.insert(name.to_str(), e);
136         } else {
137             args.push(p.parse_expr());
138         }
139     }
140     return (extra, Some((fmtstr, args, order, names)));
141 }
142
143 impl<'a> Context<'a> {
144     /// Verifies one piece of a parse string. All errors are not emitted as
145     /// fatal so we can continue giving errors about this and possibly other
146     /// format strings.
147     fn verify_piece(&mut self, p: &parse::Piece) {
148         match *p {
149             parse::String(..) => {}
150             parse::CurrentArgument => {
151                 if self.nest_level == 0 {
152                     self.ecx.span_err(self.fmtsp,
153                                       "`#` reference used with nothing to \
154                                        reference back to");
155                 }
156             }
157             parse::Argument(ref arg) => {
158                 // width/precision first, if they have implicit positional
159                 // parameters it makes more sense to consume them first.
160                 self.verify_count(arg.format.width);
161                 self.verify_count(arg.format.precision);
162
163                 // argument second, if it's an implicit positional parameter
164                 // it's written second, so it should come after width/precision.
165                 let pos = match arg.position {
166                     parse::ArgumentNext => {
167                         let i = self.next_arg;
168                         if self.check_positional_ok() {
169                             self.next_arg += 1;
170                         }
171                         Exact(i)
172                     }
173                     parse::ArgumentIs(i) => Exact(i),
174                     parse::ArgumentNamed(s) => Named(s.to_str()),
175                 };
176
177                 // and finally the method being applied
178                 match arg.method {
179                     None => {
180                         let ty = Known(arg.format.ty.to_str());
181                         self.verify_arg_type(pos, ty);
182                     }
183                     Some(ref method) => { self.verify_method(pos, *method); }
184                 }
185             }
186         }
187     }
188
189     fn verify_pieces(&mut self, pieces: &[parse::Piece]) {
190         for piece in pieces.iter() {
191             self.verify_piece(piece);
192         }
193     }
194
195     fn verify_count(&mut self, c: parse::Count) {
196         match c {
197             parse::CountImplied | parse::CountIs(..) => {}
198             parse::CountIsParam(i) => {
199                 self.verify_arg_type(Exact(i), Unsigned);
200             }
201             parse::CountIsName(s) => {
202                 self.verify_arg_type(Named(s.to_str()), Unsigned);
203             }
204             parse::CountIsNextParam => {
205                 if self.check_positional_ok() {
206                     self.verify_arg_type(Exact(self.next_arg), Unsigned);
207                     self.next_arg += 1;
208                 }
209             }
210         }
211     }
212
213     fn check_positional_ok(&mut self) -> bool {
214         if self.nest_level != 0 {
215             self.ecx.span_err(self.fmtsp, "cannot use implicit positional \
216                                            arguments nested inside methods");
217             false
218         } else {
219             true
220         }
221     }
222
223     fn verify_method(&mut self, pos: Position, m: &parse::Method) {
224         self.nest_level += 1;
225         match *m {
226             parse::Plural(_, ref arms, ref default) => {
227                 let mut seen_cases = HashSet::new();
228                 self.verify_arg_type(pos, Unsigned);
229                 for arm in arms.iter() {
230                     if !seen_cases.insert(arm.selector) {
231                         match arm.selector {
232                             parse::Keyword(name) => {
233                                 self.ecx.span_err(self.fmtsp,
234                                                   format!("duplicate selector \
235                                                            `{:?}`", name));
236                             }
237                             parse::Literal(idx) => {
238                                 self.ecx.span_err(self.fmtsp,
239                                                   format!("duplicate selector \
240                                                            `={}`", idx));
241                             }
242                         }
243                     }
244                     self.verify_pieces(arm.result);
245                 }
246                 self.verify_pieces(*default);
247             }
248             parse::Select(ref arms, ref default) => {
249                 self.verify_arg_type(pos, String);
250                 let mut seen_cases = HashSet::new();
251                 for arm in arms.iter() {
252                     if !seen_cases.insert(arm.selector) {
253                         self.ecx.span_err(self.fmtsp,
254                                           format!("duplicate selector `{}`",
255                                                arm.selector));
256                     } else if arm.selector == "" {
257                         self.ecx.span_err(self.fmtsp,
258                                           "empty selector in `select`");
259                     }
260                     self.verify_pieces(arm.result);
261                 }
262                 self.verify_pieces(*default);
263             }
264         }
265         self.nest_level -= 1;
266     }
267
268     fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
269         match arg {
270             Exact(arg) => {
271                 if arg < 0 || self.args.len() <= arg {
272                     let msg = format!("invalid reference to argument `{}` (there \
273                                     are {} arguments)", arg, self.args.len());
274                     self.ecx.span_err(self.fmtsp, msg);
275                     return;
276                 }
277                 {
278                     let arg_type = match self.arg_types[arg] {
279                         None => None,
280                         Some(ref x) => Some(x)
281                     };
282                     self.verify_same(self.args[arg].span, &ty, arg_type);
283                 }
284                 if self.arg_types[arg].is_none() {
285                     self.arg_types[arg] = Some(ty);
286                 }
287             }
288
289             Named(name) => {
290                 let span = match self.names.find(&name) {
291                     Some(e) => e.span,
292                     None => {
293                         let msg = format!("there is no argument named `{}`", name);
294                         self.ecx.span_err(self.fmtsp, msg);
295                         return;
296                     }
297                 };
298                 self.verify_same(span, &ty, self.name_types.find(&name));
299                 if !self.name_types.contains_key(&name) {
300                     self.name_types.insert(name.clone(), ty);
301                 }
302                 // Assign this named argument a slot in the arguments array if
303                 // it hasn't already been assigned a slot.
304                 if !self.name_positions.contains_key(&name) {
305                     let slot = self.name_positions.len();
306                     self.name_positions.insert(name, slot);
307                 }
308             }
309         }
310     }
311
312     /// When we're keeping track of the types that are declared for certain
313     /// arguments, we assume that `None` means we haven't seen this argument
314     /// yet, `Some(None)` means that we've seen the argument, but no format was
315     /// specified, and `Some(Some(x))` means that the argument was declared to
316     /// have type `x`.
317     ///
318     /// Obviously `Some(Some(x)) != Some(Some(y))`, but we consider it true
319     /// that: `Some(None) == Some(Some(x))`
320     fn verify_same(&self,
321                    sp: Span,
322                    ty: &ArgumentType,
323                    before: Option<&ArgumentType>) {
324         let cur = match before {
325             None => return,
326             Some(t) => t,
327         };
328         if *ty == *cur {
329             return
330         }
331         match (cur, ty) {
332             (&Known(ref cur), &Known(ref ty)) => {
333                 self.ecx.span_err(sp,
334                                   format!("argument redeclared with type `{}` when \
335                                            it was previously `{}`",
336                                           *ty,
337                                           *cur));
338             }
339             (&Known(ref cur), _) => {
340                 self.ecx.span_err(sp,
341                                   format!("argument used to format with `{}` was \
342                                            attempted to not be used for formatting",
343                                            *cur));
344             }
345             (_, &Known(ref ty)) => {
346                 self.ecx.span_err(sp,
347                                   format!("argument previously used as a format \
348                                            argument attempted to be used as `{}`",
349                                            *ty));
350             }
351             (_, _) => {
352                 self.ecx.span_err(sp, "argument declared with multiple formats");
353             }
354         }
355     }
356
357     /// These attributes are applied to all statics that this syntax extension
358     /// will generate.
359     fn static_attrs(&self) -> Vec<ast::Attribute> {
360         // Flag statics as `address_insignificant` so LLVM can merge duplicate
361         // globals as much as possible (which we're generating a whole lot of).
362         let unnamed = self.ecx
363                           .meta_word(self.fmtsp,
364                                      InternedString::new(
365                                          "address_insignificant"));
366         let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
367
368         // Do not warn format string as dead code
369         let dead_code = self.ecx.meta_word(self.fmtsp,
370                                            InternedString::new("dead_code"));
371         let allow_dead_code = self.ecx.meta_list(self.fmtsp,
372                                                  InternedString::new("allow"),
373                                                  vec!(dead_code));
374         let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code);
375         return vec!(unnamed, allow_dead_code);
376     }
377
378     fn parsepath(&self, s: &str) -> Vec<ast::Ident> {
379         vec!(self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
380           self.ecx.ident_of("parse"), self.ecx.ident_of(s))
381     }
382
383     fn rtpath(&self, s: &str) -> Vec<ast::Ident> {
384         vec!(self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
385           self.ecx.ident_of("rt"), self.ecx.ident_of(s))
386     }
387
388     fn ctpath(&self, s: &str) -> Vec<ast::Ident> {
389         vec!(self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
390           self.ecx.ident_of("parse"), self.ecx.ident_of(s))
391     }
392
393     fn none(&self) -> @ast::Expr {
394         let none = self.ecx.path_global(self.fmtsp, vec!(
395                 self.ecx.ident_of("std"),
396                 self.ecx.ident_of("option"),
397                 self.ecx.ident_of("None")));
398         self.ecx.expr_path(none)
399     }
400
401     fn some(&self, e: @ast::Expr) -> @ast::Expr {
402         let p = self.ecx.path_global(self.fmtsp, vec!(
403                 self.ecx.ident_of("std"),
404                 self.ecx.ident_of("option"),
405                 self.ecx.ident_of("Some")));
406         let p = self.ecx.expr_path(p);
407         self.ecx.expr_call(self.fmtsp, p, vec!(e))
408     }
409
410     fn trans_count(&self, c: parse::Count) -> @ast::Expr {
411         let sp = self.fmtsp;
412         match c {
413             parse::CountIs(i) => {
414                 self.ecx.expr_call_global(sp, self.rtpath("CountIs"),
415                                           vec!(self.ecx.expr_uint(sp, i)))
416             }
417             parse::CountIsParam(i) => {
418                 self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"),
419                                           vec!(self.ecx.expr_uint(sp, i)))
420             }
421             parse::CountImplied => {
422                 let path = self.ecx.path_global(sp, self.rtpath("CountImplied"));
423                 self.ecx.expr_path(path)
424             }
425             parse::CountIsNextParam => {
426                 let path = self.ecx.path_global(sp, self.rtpath("CountIsNextParam"));
427                 self.ecx.expr_path(path)
428             }
429             parse::CountIsName(n) => {
430                 let i = match self.name_positions.find_equiv(&n) {
431                     Some(&i) => i,
432                     None => 0, // error already emitted elsewhere
433                 };
434                 let i = i + self.args.len();
435                 self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"),
436                                           vec!(self.ecx.expr_uint(sp, i)))
437             }
438         }
439     }
440
441     fn trans_method(&mut self, method: &parse::Method) -> @ast::Expr {
442         let sp = self.fmtsp;
443         let method = match *method {
444             parse::Select(ref arms, ref default) => {
445                 let arms = arms.iter().map(|arm| {
446                         let p = self.ecx.path_global(sp, self.rtpath("SelectArm"));
447                         let result = arm.result.iter().map(|p| {
448                             self.trans_piece(p)
449                         }).collect();
450                         let s = token::intern_and_get_ident(arm.selector);
451                         let selector = self.ecx.expr_str(sp, s);
452                         self.ecx.expr_struct(sp, p, vec!(
453                                 self.ecx.field_imm(sp,
454                                                    self.ecx.ident_of("selector"),
455                                                    selector),
456                                 self.ecx.field_imm(sp, self.ecx.ident_of("result"),
457                                                    self.ecx.expr_vec_slice(sp, result))))
458                     }).collect();
459                 let default = default.iter().map(|p| {
460                         self.trans_piece(p)
461                     }).collect();
462                 self.ecx.expr_call_global(sp, self.rtpath("Select"), vec!(
463                         self.ecx.expr_vec_slice(sp, arms),
464                         self.ecx.expr_vec_slice(sp, default)))
465             }
466             parse::Plural(offset, ref arms, ref default) => {
467                 let offset = match offset {
468                     Some(i) => { self.some(self.ecx.expr_uint(sp, i)) }
469                     None => { self.none() }
470                 };
471                 let arms = arms.iter().map(|arm| {
472                         let p = self.ecx.path_global(sp, self.rtpath("PluralArm"));
473                         let result = arm.result.iter().map(|p| {
474                                 self.trans_piece(p)
475                             }).collect();
476                         let (lr, selarg) = match arm.selector {
477                             parse::Keyword(t) => {
478                                 let p = self.ctpath(format!("{:?}", t));
479                                 let p = self.ecx.path_global(sp, p);
480                                 (self.rtpath("Keyword"), self.ecx.expr_path(p))
481                             }
482                             parse::Literal(i) => {
483                                 (self.rtpath("Literal"), self.ecx.expr_uint(sp, i))
484                             }
485                         };
486                         let selector = self.ecx.expr_call_global(sp,
487                                                                  lr, vec!(selarg));
488                         self.ecx.expr_struct(sp, p, vec!(
489                                 self.ecx.field_imm(sp,
490                                                    self.ecx.ident_of("selector"),
491                                                    selector),
492                                 self.ecx.field_imm(sp, self.ecx.ident_of("result"),
493                                                    self.ecx.expr_vec_slice(sp, result))))
494                     }).collect();
495                 let default = default.iter().map(|p| {
496                         self.trans_piece(p)
497                     }).collect();
498                 self.ecx.expr_call_global(sp, self.rtpath("Plural"), vec!(
499                         offset,
500                         self.ecx.expr_vec_slice(sp, arms),
501                         self.ecx.expr_vec_slice(sp, default)))
502             }
503         };
504         let life = self.ecx.lifetime(sp, self.ecx.ident_of("static").name);
505         let ty = self.ecx.ty_path(self.ecx.path_all(
506                 sp,
507                 true,
508                 self.rtpath("Method"),
509                 opt_vec::with(life),
510                 Vec::new()
511                     ), None);
512         let st = ast::ItemStatic(ty, ast::MutImmutable, method);
513         let static_name = self.ecx.ident_of(format!("__STATIC_METHOD_{}",
514                                                     self.method_statics.len()));
515         let item = self.ecx.item(sp, static_name, self.static_attrs(), st);
516         self.method_statics.push(item);
517         self.ecx.expr_ident(sp, static_name)
518     }
519
520     /// Translate a `parse::Piece` to a static `rt::Piece`
521     fn trans_piece(&mut self, piece: &parse::Piece) -> @ast::Expr {
522         let sp = self.fmtsp;
523         match *piece {
524             parse::String(s) => {
525                 let s = token::intern_and_get_ident(s);
526                 self.ecx.expr_call_global(sp,
527                                           self.rtpath("String"),
528                                           vec!(
529                     self.ecx.expr_str(sp, s)
530                 ))
531             }
532             parse::CurrentArgument => {
533                 let nil = self.ecx.expr_lit(sp, ast::LitNil);
534                 self.ecx.expr_call_global(sp, self.rtpath("CurrentArgument"), vec!(nil))
535             }
536             parse::Argument(ref arg) => {
537                 // Translate the position
538                 let pos = match arg.position {
539                     // These two have a direct mapping
540                     parse::ArgumentNext => {
541                         let path = self.ecx.path_global(sp,
542                                                         self.rtpath("ArgumentNext"));
543                         self.ecx.expr_path(path)
544                     }
545                     parse::ArgumentIs(i) => {
546                         self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"),
547                                                   vec!(self.ecx.expr_uint(sp, i)))
548                     }
549                     // Named arguments are converted to positional arguments at
550                     // the end of the list of arguments
551                     parse::ArgumentNamed(n) => {
552                         let i = match self.name_positions.find_equiv(&n) {
553                             Some(&i) => i,
554                             None => 0, // error already emitted elsewhere
555                         };
556                         let i = i + self.args.len();
557                         self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"),
558                                                   vec!(self.ecx.expr_uint(sp, i)))
559                     }
560                 };
561
562                 // Translate the format
563                 let fill = match arg.format.fill { Some(c) => c, None => ' ' };
564                 let fill = self.ecx.expr_lit(sp, ast::LitChar(fill as u32));
565                 let align = match arg.format.align {
566                     parse::AlignLeft => {
567                         self.ecx.path_global(sp, self.parsepath("AlignLeft"))
568                     }
569                     parse::AlignRight => {
570                         self.ecx.path_global(sp, self.parsepath("AlignRight"))
571                     }
572                     parse::AlignUnknown => {
573                         self.ecx.path_global(sp, self.parsepath("AlignUnknown"))
574                     }
575                 };
576                 let align = self.ecx.expr_path(align);
577                 let flags = self.ecx.expr_uint(sp, arg.format.flags);
578                 let prec = self.trans_count(arg.format.precision);
579                 let width = self.trans_count(arg.format.width);
580                 let path = self.ecx.path_global(sp, self.rtpath("FormatSpec"));
581                 let fmt = self.ecx.expr_struct(sp, path, vec!(
582                     self.ecx.field_imm(sp, self.ecx.ident_of("fill"), fill),
583                     self.ecx.field_imm(sp, self.ecx.ident_of("align"), align),
584                     self.ecx.field_imm(sp, self.ecx.ident_of("flags"), flags),
585                     self.ecx.field_imm(sp, self.ecx.ident_of("precision"), prec),
586                     self.ecx.field_imm(sp, self.ecx.ident_of("width"), width)));
587
588                 // Translate the method (if any)
589                 let method = match arg.method {
590                     None => { self.none() }
591                     Some(ref m) => {
592                         let m = self.trans_method(*m);
593                         self.some(self.ecx.expr_addr_of(sp, m))
594                     }
595                 };
596                 let path = self.ecx.path_global(sp, self.rtpath("Argument"));
597                 let s = self.ecx.expr_struct(sp, path, vec!(
598                     self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos),
599                     self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt),
600                     self.ecx.field_imm(sp, self.ecx.ident_of("method"), method)));
601                 self.ecx.expr_call_global(sp, self.rtpath("Argument"), vec!(s))
602             }
603         }
604     }
605
606     /// Actually builds the expression which the iformat! block will be expanded
607     /// to
608     fn to_expr(&self, extra: @ast::Expr) -> @ast::Expr {
609         let mut lets = Vec::new();
610         let mut locals = Vec::new();
611         let mut names = vec::from_fn(self.name_positions.len(), |_| None);
612         let mut pats = Vec::new();
613         let mut heads = Vec::new();
614
615         // First, declare all of our methods that are statics
616         for &method in self.method_statics.iter() {
617             let decl = respan(self.fmtsp, ast::DeclItem(method));
618             lets.push(@respan(self.fmtsp,
619                               ast::StmtDecl(@decl, ast::DUMMY_NODE_ID)));
620         }
621
622         // Next, build up the static array which will become our precompiled
623         // format "string"
624         let fmt = self.ecx.expr_vec(self.fmtsp, self.pieces.clone());
625         let piece_ty = self.ecx.ty_path(self.ecx.path_all(
626                 self.fmtsp,
627                 true, vec!(
628                     self.ecx.ident_of("std"),
629                     self.ecx.ident_of("fmt"),
630                     self.ecx.ident_of("rt"),
631                     self.ecx.ident_of("Piece")),
632                 opt_vec::with(
633                     self.ecx.lifetime(self.fmtsp, self.ecx.ident_of("static").name)),
634                 Vec::new()
635             ), None);
636         let ty = ast::TyFixedLengthVec(
637             piece_ty,
638             self.ecx.expr_uint(self.fmtsp, self.pieces.len())
639         );
640         let ty = self.ecx.ty(self.fmtsp, ty);
641         let st = ast::ItemStatic(ty, ast::MutImmutable, fmt);
642         let static_name = self.ecx.ident_of("__STATIC_FMTSTR");
643         let item = self.ecx.item(self.fmtsp, static_name,
644                                  self.static_attrs(), st);
645         let decl = respan(self.fmtsp, ast::DeclItem(item));
646         lets.push(@respan(self.fmtsp, ast::StmtDecl(@decl, ast::DUMMY_NODE_ID)));
647
648         // Right now there is a bug such that for the expression:
649         //      foo(bar(&1))
650         // the lifetime of `1` doesn't outlast the call to `bar`, so it's not
651         // vald for the call to `foo`. To work around this all arguments to the
652         // format! string are shoved into locals. Furthermore, we shove the address
653         // of each variable because we don't want to move out of the arguments
654         // passed to this function.
655         for (i, &e) in self.args.iter().enumerate() {
656             if self.arg_types[i].is_none() { continue } // error already generated
657
658             let name = self.ecx.ident_of(format!("__arg{}", i));
659             pats.push(self.ecx.pat_ident(e.span, name));
660             heads.push(self.ecx.expr_addr_of(e.span, e));
661             locals.push(self.format_arg(e.span, Exact(i),
662                                         self.ecx.expr_ident(e.span, name)));
663         }
664         for name in self.name_ordering.iter() {
665             let e = match self.names.find(name) {
666                 Some(&e) if self.name_types.contains_key(name) => e,
667                 Some(..) | None => continue
668             };
669
670             let lname = self.ecx.ident_of(format!("__arg{}", *name));
671             pats.push(self.ecx.pat_ident(e.span, lname));
672             heads.push(self.ecx.expr_addr_of(e.span, e));
673             names[*self.name_positions.get(name)] =
674                 Some(self.format_arg(e.span,
675                                      Named((*name).clone()),
676                                      self.ecx.expr_ident(e.span, lname)));
677         }
678
679         // Now create a vector containing all the arguments
680         let slicename = self.ecx.ident_of("__args_vec");
681         {
682             let args = names.move_iter().map(|a| a.unwrap());
683             let mut args = locals.move_iter().chain(args);
684             let args = self.ecx.expr_vec_slice(self.fmtsp, args.collect());
685             lets.push(self.ecx.stmt_let(self.fmtsp, false, slicename, args));
686         }
687
688         // Now create the fmt::Arguments struct with all our locals we created.
689         let fmt = self.ecx.expr_ident(self.fmtsp, static_name);
690         let args_slice = self.ecx.expr_ident(self.fmtsp, slicename);
691         let result = self.ecx.expr_call_global(self.fmtsp, vec!(
692                 self.ecx.ident_of("std"),
693                 self.ecx.ident_of("fmt"),
694                 self.ecx.ident_of("Arguments"),
695                 self.ecx.ident_of("new")), vec!(fmt, args_slice));
696
697         // We did all the work of making sure that the arguments
698         // structure is safe, so we can safely have an unsafe block.
699         let result = self.ecx.expr_block(P(ast::Block {
700            view_items: Vec::new(),
701            stmts: Vec::new(),
702            expr: Some(result),
703            id: ast::DUMMY_NODE_ID,
704            rules: ast::UnsafeBlock(ast::CompilerGenerated),
705            span: self.fmtsp,
706         }));
707         let resname = self.ecx.ident_of("__args");
708         lets.push(self.ecx.stmt_let(self.fmtsp, false, resname, result));
709         let res = self.ecx.expr_ident(self.fmtsp, resname);
710         let result = self.ecx.expr_call(extra.span, extra, vec!(
711                             self.ecx.expr_addr_of(extra.span, res)));
712         let body = self.ecx.expr_block(self.ecx.block(self.fmtsp, lets,
713                                                       Some(result)));
714
715         // Constructs an AST equivalent to:
716         //
717         //      match (&arg0, &arg1) {
718         //          (tmp0, tmp1) => body
719         //      }
720         //
721         // It was:
722         //
723         //      let tmp0 = &arg0;
724         //      let tmp1 = &arg1;
725         //      body
726         //
727         // Because of #11585 the new temporary lifetime rule, the enclosing
728         // statements for these temporaries become the let's themselves.
729         // If one or more of them are RefCell's, RefCell borrow() will also
730         // end there; they don't last long enough for body to use them. The
731         // match expression solves the scope problem.
732         //
733         // Note, it may also very well be transformed to:
734         //
735         //      match arg0 {
736         //          ref tmp0 => {
737         //              match arg1 => {
738         //                  ref tmp1 => body } } }
739         //
740         // But the nested match expression is proved to perform not as well
741         // as series of let's; the first approach does.
742         let pat = self.ecx.pat(self.fmtsp, ast::PatTup(pats));
743         let arm = self.ecx.arm(self.fmtsp, vec!(pat), body);
744         let head = self.ecx.expr(self.fmtsp, ast::ExprTup(heads));
745         self.ecx.expr_match(self.fmtsp, head, vec!(arm))
746     }
747
748     fn format_arg(&self, sp: Span, argno: Position, arg: @ast::Expr)
749                   -> @ast::Expr {
750         let ty = match argno {
751             Exact(ref i) => self.arg_types[*i].get_ref(),
752             Named(ref s) => self.name_types.get(s)
753         };
754
755         let fmt_fn = match *ty {
756             Known(ref tyname) => {
757                 match tyname.as_slice() {
758                     ""  => "secret_show",
759                     "?" => "secret_poly",
760                     "b" => "secret_bool",
761                     "c" => "secret_char",
762                     "d" | "i" => "secret_signed",
763                     "e" => "secret_lower_exp",
764                     "E" => "secret_upper_exp",
765                     "f" => "secret_float",
766                     "o" => "secret_octal",
767                     "p" => "secret_pointer",
768                     "s" => "secret_string",
769                     "t" => "secret_binary",
770                     "u" => "secret_unsigned",
771                     "x" => "secret_lower_hex",
772                     "X" => "secret_upper_hex",
773                     _ => {
774                         self.ecx.span_err(sp, format!("unknown format trait `{}`",
775                                                       *tyname));
776                         "dummy"
777                     }
778                 }
779             }
780             String => {
781                 return self.ecx.expr_call_global(sp, vec!(
782                         self.ecx.ident_of("std"),
783                         self.ecx.ident_of("fmt"),
784                         self.ecx.ident_of("argumentstr")), vec!(arg))
785             }
786             Unsigned => {
787                 return self.ecx.expr_call_global(sp, vec!(
788                         self.ecx.ident_of("std"),
789                         self.ecx.ident_of("fmt"),
790                         self.ecx.ident_of("argumentuint")), vec!(arg))
791             }
792         };
793
794         let format_fn = self.ecx.path_global(sp, vec!(
795                 self.ecx.ident_of("std"),
796                 self.ecx.ident_of("fmt"),
797                 self.ecx.ident_of(fmt_fn)));
798         self.ecx.expr_call_global(sp, vec!(
799                 self.ecx.ident_of("std"),
800                 self.ecx.ident_of("fmt"),
801                 self.ecx.ident_of("argument")), vec!(self.ecx.expr_path(format_fn), arg))
802     }
803 }
804
805 pub fn expand_args(ecx: &mut ExtCtxt, sp: Span,
806                    tts: &[ast::TokenTree]) -> base::MacResult {
807
808     match parse_args(ecx, sp, tts) {
809         (extra, Some((efmt, args, order, names))) => {
810             MRExpr(expand_preparsed_format_args(ecx, sp, extra, efmt, args,
811                                                 order, names))
812         }
813         (_, None) => MRExpr(ecx.expr_uint(sp, 2))
814     }
815 }
816
817 /// Take the various parts of `format_args!(extra, efmt, args...,
818 /// name=names...)` and construct the appropriate formatting
819 /// expression.
820 pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
821                                     extra: @ast::Expr,
822                                     efmt: @ast::Expr, args: Vec<@ast::Expr>,
823                                     name_ordering: Vec<~str>,
824                                     names: HashMap<~str, @ast::Expr>) -> @ast::Expr {
825     let arg_types = vec::from_fn(args.len(), |_| None);
826     let mut cx = Context {
827         ecx: ecx,
828         args: args,
829         arg_types: arg_types,
830         names: names,
831         name_positions: HashMap::new(),
832         name_types: HashMap::new(),
833         name_ordering: name_ordering,
834         nest_level: 0,
835         next_arg: 0,
836         pieces: Vec::new(),
837         method_statics: Vec::new(),
838         fmtsp: sp,
839     };
840     cx.fmtsp = efmt.span;
841     // Be sure to recursively expand macros just in case the format string uses
842     // a macro to build the format expression.
843     let expr = cx.ecx.expand_expr(efmt);
844     let fmt = match expr_to_str(cx.ecx,
845                                 expr,
846                                 "format argument must be a string literal.") {
847         Some((fmt, _)) => fmt,
848         None => return MacResult::raw_dummy_expr(sp)
849     };
850
851     let mut parser = parse::Parser::new(fmt.get());
852     loop {
853         match parser.next() {
854             Some(piece) => {
855                 if parser.errors.len() > 0 { break }
856                 cx.verify_piece(&piece);
857                 let piece = cx.trans_piece(&piece);
858                 cx.pieces.push(piece);
859             }
860             None => break
861         }
862     }
863     match parser.errors.shift() {
864         Some(error) => {
865             cx.ecx.span_err(efmt.span, "invalid format string: " + error);
866             return MacResult::raw_dummy_expr(sp);
867         }
868         None => {}
869     }
870
871     // Make sure that all arguments were used and all arguments have types.
872     for (i, ty) in cx.arg_types.iter().enumerate() {
873         if ty.is_none() {
874             cx.ecx.span_err(cx.args[i].span, "argument never used");
875         }
876     }
877     for (name, e) in cx.names.iter() {
878         if !cx.name_types.contains_key(name) {
879             cx.ecx.span_err(e.span, "named argument never used");
880         }
881     }
882
883     cx.to_expr(extra)
884 }