]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/format.rs
Auto merge of #29498 - wthrowe:replace-pattern, r=alexcrichton
[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 self::ArgumentType::*;
12 use self::Position::*;
13
14 use fmt_macros as parse;
15
16 use syntax::ast;
17 use syntax::codemap::{Span, respan};
18 use syntax::ext::base::*;
19 use syntax::ext::base;
20 use syntax::ext::build::AstBuilder;
21 use syntax::fold::Folder;
22 use syntax::parse::token::special_idents;
23 use syntax::parse::token;
24 use syntax::ptr::P;
25
26 use std::collections::HashMap;
27
28 #[derive(PartialEq)]
29 enum ArgumentType {
30     Known(String),
31     Unsigned
32 }
33
34 enum Position {
35     Exact(usize),
36     Named(String),
37 }
38
39 struct Context<'a, 'b:'a> {
40     ecx: &'a mut ExtCtxt<'b>,
41     /// The macro's call site. References to unstable formatting internals must
42     /// use this span to pass the stability checker.
43     macsp: Span,
44     /// The span of the format string literal.
45     fmtsp: Span,
46
47     /// Parsed argument expressions and the types that we've found so far for
48     /// them.
49     args: Vec<P<ast::Expr>>,
50     arg_types: Vec<Option<ArgumentType>>,
51     /// Parsed named expressions and the types that we've found for them so far.
52     /// Note that we keep a side-array of the ordering of the named arguments
53     /// found to be sure that we can translate them in the same order that they
54     /// were declared in.
55     names: HashMap<String, P<ast::Expr>>,
56     name_types: HashMap<String, ArgumentType>,
57     name_ordering: Vec<String>,
58
59     /// The latest consecutive literal strings, or empty if there weren't any.
60     literal: String,
61
62     /// Collection of the compiled `rt::Argument` structures
63     pieces: Vec<P<ast::Expr>>,
64     /// Collection of string literals
65     str_pieces: Vec<P<ast::Expr>>,
66     /// Stays `true` if all formatting parameters are default (as in "{}{}").
67     all_pieces_simple: bool,
68
69     name_positions: HashMap<String, usize>,
70
71     /// Updated as arguments are consumed or methods are entered
72     nest_level: usize,
73     next_arg: usize,
74 }
75
76 /// Parses the arguments from the given list of tokens, returning None
77 /// if there's a parse error so we can continue parsing other format!
78 /// expressions.
79 ///
80 /// If parsing succeeds, the return value is:
81 /// ```ignore
82 /// Some((fmtstr, unnamed arguments, ordering of named arguments,
83 ///       named arguments))
84 /// ```
85 fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
86               -> Option<(P<ast::Expr>, Vec<P<ast::Expr>>, Vec<String>,
87                          HashMap<String, P<ast::Expr>>)> {
88     let mut args = Vec::new();
89     let mut names = HashMap::<String, P<ast::Expr>>::new();
90     let mut order = Vec::new();
91
92     let mut p = ecx.new_parser_from_tts(tts);
93
94     if p.token == token::Eof {
95         ecx.span_err(sp, "requires at least a format string argument");
96         return None;
97     }
98     let fmtstr = panictry!(p.parse_expr());
99     let mut named = false;
100     while p.token != token::Eof {
101         if !p.eat(&token::Comma) {
102             ecx.span_err(sp, "expected token: `,`");
103             return None;
104         }
105         if p.token == token::Eof { break } // accept trailing commas
106         if named || (p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq)) {
107             named = true;
108             let ident = match p.token {
109                 token::Ident(i, _) => {
110                     p.bump();
111                     i
112                 }
113                 _ if named => {
114                     ecx.span_err(p.span,
115                                  "expected ident, positional arguments \
116                                  cannot follow named arguments");
117                     return None;
118                 }
119                 _ => {
120                     ecx.span_err(p.span,
121                                  &format!("expected ident for named argument, found `{}`",
122                                          p.this_token_to_string()));
123                     return None;
124                 }
125             };
126             let name: &str = &ident.name.as_str();
127
128             panictry!(p.expect(&token::Eq));
129             let e = panictry!(p.parse_expr());
130             match names.get(name) {
131                 None => {}
132                 Some(prev) => {
133                     ecx.struct_span_err(e.span,
134                                         &format!("duplicate argument named `{}`",
135                                                  name))
136                        .span_note(prev.span, "previously here")
137                        .emit();
138                     continue
139                 }
140             }
141             order.push(name.to_string());
142             names.insert(name.to_string(), e);
143         } else {
144             args.push(panictry!(p.parse_expr()));
145         }
146     }
147     Some((fmtstr, args, order, names))
148 }
149
150 impl<'a, 'b> Context<'a, 'b> {
151     /// Verifies one piece of a parse string. All errors are not emitted as
152     /// fatal so we can continue giving errors about this and possibly other
153     /// format strings.
154     fn verify_piece(&mut self, p: &parse::Piece) {
155         match *p {
156             parse::String(..) => {}
157             parse::NextArgument(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_string()),
175                 };
176
177                 let ty = Known(arg.format.ty.to_string());
178                 self.verify_arg_type(pos, ty);
179             }
180         }
181     }
182
183     fn verify_count(&mut self, c: parse::Count) {
184         match c {
185             parse::CountImplied | parse::CountIs(..) => {}
186             parse::CountIsParam(i) => {
187                 self.verify_arg_type(Exact(i), Unsigned);
188             }
189             parse::CountIsName(s) => {
190                 self.verify_arg_type(Named(s.to_string()), Unsigned);
191             }
192             parse::CountIsNextParam => {
193                 if self.check_positional_ok() {
194                     let next_arg = self.next_arg;
195                     self.verify_arg_type(Exact(next_arg), Unsigned);
196                     self.next_arg += 1;
197                 }
198             }
199         }
200     }
201
202     fn check_positional_ok(&mut self) -> bool {
203         if self.nest_level != 0 {
204             self.ecx.span_err(self.fmtsp, "cannot use implicit positional \
205                                            arguments nested inside methods");
206             false
207         } else {
208             true
209         }
210     }
211
212     fn describe_num_args(&self) -> String {
213         match self.args.len() {
214             0 => "no arguments given".to_string(),
215             1 => "there is 1 argument".to_string(),
216             x => format!("there are {} arguments", x),
217         }
218     }
219
220     fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
221         match arg {
222             Exact(arg) => {
223                 if self.args.len() <= arg {
224                     let msg = format!("invalid reference to argument `{}` ({})",
225                                       arg, self.describe_num_args());
226
227                     self.ecx.span_err(self.fmtsp, &msg[..]);
228                     return;
229                 }
230                 {
231                     let arg_type = match self.arg_types[arg] {
232                         None => None,
233                         Some(ref x) => Some(x)
234                     };
235                     self.verify_same(self.args[arg].span, &ty, arg_type);
236                 }
237                 if self.arg_types[arg].is_none() {
238                     self.arg_types[arg] = Some(ty);
239                 }
240             }
241
242             Named(name) => {
243                 let span = match self.names.get(&name) {
244                     Some(e) => e.span,
245                     None => {
246                         let msg = format!("there is no argument named `{}`", name);
247                         self.ecx.span_err(self.fmtsp, &msg[..]);
248                         return;
249                     }
250                 };
251                 self.verify_same(span, &ty, self.name_types.get(&name));
252                 if !self.name_types.contains_key(&name) {
253                     self.name_types.insert(name.clone(), ty);
254                 }
255                 // Assign this named argument a slot in the arguments array if
256                 // it hasn't already been assigned a slot.
257                 if !self.name_positions.contains_key(&name) {
258                     let slot = self.name_positions.len();
259                     self.name_positions.insert(name, slot);
260                 }
261             }
262         }
263     }
264
265     /// When we're keeping track of the types that are declared for certain
266     /// arguments, we assume that `None` means we haven't seen this argument
267     /// yet, `Some(None)` means that we've seen the argument, but no format was
268     /// specified, and `Some(Some(x))` means that the argument was declared to
269     /// have type `x`.
270     ///
271     /// Obviously `Some(Some(x)) != Some(Some(y))`, but we consider it true
272     /// that: `Some(None) == Some(Some(x))`
273     fn verify_same(&self,
274                    sp: Span,
275                    ty: &ArgumentType,
276                    before: Option<&ArgumentType>) {
277         let cur = match before {
278             None => return,
279             Some(t) => t,
280         };
281         if *ty == *cur {
282             return
283         }
284         match (cur, ty) {
285             (&Known(ref cur), &Known(ref ty)) => {
286                 self.ecx.span_err(sp,
287                                   &format!("argument redeclared with type `{}` when \
288                                            it was previously `{}`",
289                                           *ty,
290                                           *cur));
291             }
292             (&Known(ref cur), _) => {
293                 self.ecx.span_err(sp,
294                                   &format!("argument used to format with `{}` was \
295                                            attempted to not be used for formatting",
296                                            *cur));
297             }
298             (_, &Known(ref ty)) => {
299                 self.ecx.span_err(sp,
300                                   &format!("argument previously used as a format \
301                                            argument attempted to be used as `{}`",
302                                            *ty));
303             }
304             (_, _) => {
305                 self.ecx.span_err(sp, "argument declared with multiple formats");
306             }
307         }
308     }
309
310     fn rtpath(ecx: &ExtCtxt, s: &str) -> Vec<ast::Ident> {
311         ecx.std_path(&["fmt", "rt", "v1", s])
312     }
313
314     fn trans_count(&self, c: parse::Count) -> P<ast::Expr> {
315         let sp = self.macsp;
316         let count = |c, arg| {
317             let mut path = Context::rtpath(self.ecx, "Count");
318             path.push(self.ecx.ident_of(c));
319             match arg {
320                 Some(arg) => self.ecx.expr_call_global(sp, path, vec![arg]),
321                 None => self.ecx.expr_path(self.ecx.path_global(sp, path)),
322             }
323         };
324         match c {
325             parse::CountIs(i) => count("Is", Some(self.ecx.expr_usize(sp, i))),
326             parse::CountIsParam(i) => {
327                 count("Param", Some(self.ecx.expr_usize(sp, i)))
328             }
329             parse::CountImplied => count("Implied", None),
330             parse::CountIsNextParam => count("NextParam", None),
331             parse::CountIsName(n) => {
332                 let i = match self.name_positions.get(n) {
333                     Some(&i) => i,
334                     None => 0, // error already emitted elsewhere
335                 };
336                 let i = i + self.args.len();
337                 count("Param", Some(self.ecx.expr_usize(sp, i)))
338             }
339         }
340     }
341
342     /// Translate the accumulated string literals to a literal expression
343     fn trans_literal_string(&mut self) -> P<ast::Expr> {
344         let sp = self.fmtsp;
345         let s = token::intern_and_get_ident(&self.literal);
346         self.literal.clear();
347         self.ecx.expr_str(sp, s)
348     }
349
350     /// Translate a `parse::Piece` to a static `rt::Argument` or append
351     /// to the `literal` string.
352     fn trans_piece(&mut self, piece: &parse::Piece) -> Option<P<ast::Expr>> {
353         let sp = self.macsp;
354         match *piece {
355             parse::String(s) => {
356                 self.literal.push_str(s);
357                 None
358             }
359             parse::NextArgument(ref arg) => {
360                 // Translate the position
361                 let pos = {
362                     let pos = |c, arg| {
363                         let mut path = Context::rtpath(self.ecx, "Position");
364                         path.push(self.ecx.ident_of(c));
365                         match arg {
366                             Some(i) => {
367                                 let arg = self.ecx.expr_usize(sp, i);
368                                 self.ecx.expr_call_global(sp, path, vec![arg])
369                             }
370                             None => {
371                                 self.ecx.expr_path(self.ecx.path_global(sp, path))
372                             }
373                         }
374                     };
375                     match arg.position {
376                         // These two have a direct mapping
377                         parse::ArgumentNext => pos("Next", None),
378                         parse::ArgumentIs(i) => pos("At", Some(i)),
379
380                         // Named arguments are converted to positional arguments
381                         // at the end of the list of arguments
382                         parse::ArgumentNamed(n) => {
383                             let i = match self.name_positions.get(n) {
384                                 Some(&i) => i,
385                                 None => 0, // error already emitted elsewhere
386                             };
387                             let i = i + self.args.len();
388                             pos("At", Some(i))
389                         }
390                     }
391                 };
392
393                 let simple_arg = parse::Argument {
394                     position: parse::ArgumentNext,
395                     format: parse::FormatSpec {
396                         fill: arg.format.fill,
397                         align: parse::AlignUnknown,
398                         flags: 0,
399                         precision: parse::CountImplied,
400                         width: parse::CountImplied,
401                         ty: arg.format.ty
402                     }
403                 };
404
405                 let fill = match arg.format.fill { Some(c) => c, None => ' ' };
406
407                 if *arg != simple_arg || fill != ' ' {
408                     self.all_pieces_simple = false;
409                 }
410
411                 // Translate the format
412                 let fill = self.ecx.expr_lit(sp, ast::LitChar(fill));
413                 let align = |name| {
414                     let mut p = Context::rtpath(self.ecx, "Alignment");
415                     p.push(self.ecx.ident_of(name));
416                     self.ecx.path_global(sp, p)
417                 };
418                 let align = match arg.format.align {
419                     parse::AlignLeft => align("Left"),
420                     parse::AlignRight => align("Right"),
421                     parse::AlignCenter => align("Center"),
422                     parse::AlignUnknown => align("Unknown"),
423                 };
424                 let align = self.ecx.expr_path(align);
425                 let flags = self.ecx.expr_u32(sp, arg.format.flags);
426                 let prec = self.trans_count(arg.format.precision);
427                 let width = self.trans_count(arg.format.width);
428                 let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "FormatSpec"));
429                 let fmt = self.ecx.expr_struct(sp, path, vec!(
430                     self.ecx.field_imm(sp, self.ecx.ident_of("fill"), fill),
431                     self.ecx.field_imm(sp, self.ecx.ident_of("align"), align),
432                     self.ecx.field_imm(sp, self.ecx.ident_of("flags"), flags),
433                     self.ecx.field_imm(sp, self.ecx.ident_of("precision"), prec),
434                     self.ecx.field_imm(sp, self.ecx.ident_of("width"), width)));
435
436                 let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "Argument"));
437                 Some(self.ecx.expr_struct(sp, path, vec!(
438                     self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos),
439                     self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt))))
440             }
441         }
442     }
443
444     fn static_array(ecx: &mut ExtCtxt,
445                     name: &str,
446                     piece_ty: P<ast::Ty>,
447                     pieces: Vec<P<ast::Expr>>)
448                     -> P<ast::Expr> {
449         let sp = piece_ty.span;
450         let ty = ecx.ty_rptr(sp,
451             ecx.ty(sp, ast::TyVec(piece_ty)),
452             Some(ecx.lifetime(sp, special_idents::static_lifetime.name)),
453             ast::MutImmutable);
454         let slice = ecx.expr_vec_slice(sp, pieces);
455         // static instead of const to speed up codegen by not requiring this to be inlined
456         let st = ast::ItemStatic(ty, ast::MutImmutable, slice);
457
458         let name = ecx.ident_of(name);
459         let item = ecx.item(sp, name, vec![], st);
460         let decl = respan(sp, ast::DeclItem(item));
461
462         // Wrap the declaration in a block so that it forms a single expression.
463         ecx.expr_block(ecx.block(sp,
464             vec![P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))],
465             Some(ecx.expr_ident(sp, name))))
466     }
467
468     /// Actually builds the expression which the iformat! block will be expanded
469     /// to
470     fn into_expr(mut self) -> P<ast::Expr> {
471         let mut locals = Vec::new();
472         let mut names = vec![None; self.name_positions.len()];
473         let mut pats = Vec::new();
474         let mut heads = Vec::new();
475
476         // First, build up the static array which will become our precompiled
477         // format "string"
478         let static_lifetime = self.ecx.lifetime(self.fmtsp, special_idents::static_lifetime.name);
479         let piece_ty = self.ecx.ty_rptr(
480                 self.fmtsp,
481                 self.ecx.ty_ident(self.fmtsp, self.ecx.ident_of("str")),
482                 Some(static_lifetime),
483                 ast::MutImmutable);
484         let pieces = Context::static_array(self.ecx,
485                                            "__STATIC_FMTSTR",
486                                            piece_ty,
487                                            self.str_pieces);
488
489
490         // Right now there is a bug such that for the expression:
491         //      foo(bar(&1))
492         // the lifetime of `1` doesn't outlast the call to `bar`, so it's not
493         // valid for the call to `foo`. To work around this all arguments to the
494         // format! string are shoved into locals. Furthermore, we shove the address
495         // of each variable because we don't want to move out of the arguments
496         // passed to this function.
497         for (i, e) in self.args.into_iter().enumerate() {
498             let arg_ty = match self.arg_types[i].as_ref() {
499                 Some(ty) => ty,
500                 None => continue // error already generated
501             };
502
503             let name = self.ecx.ident_of(&format!("__arg{}", i));
504             pats.push(self.ecx.pat_ident(e.span, name));
505             locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty,
506                                             self.ecx.expr_ident(e.span, name)));
507             heads.push(self.ecx.expr_addr_of(e.span, e));
508         }
509         for name in &self.name_ordering {
510             let e = match self.names.remove(name) {
511                 Some(e) => e,
512                 None => continue
513             };
514             let arg_ty = match self.name_types.get(name) {
515                 Some(ty) => ty,
516                 None => continue
517             };
518
519             let lname = self.ecx.ident_of(&format!("__arg{}",
520                                                   *name));
521             pats.push(self.ecx.pat_ident(e.span, lname));
522             names[*self.name_positions.get(name).unwrap()] =
523                 Some(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty,
524                                          self.ecx.expr_ident(e.span, lname)));
525             heads.push(self.ecx.expr_addr_of(e.span, e));
526         }
527
528         // Now create a vector containing all the arguments
529         let args = locals.into_iter().chain(names.into_iter().map(|a| a.unwrap()));
530
531         let args_array = self.ecx.expr_vec(self.fmtsp, args.collect());
532
533         // Constructs an AST equivalent to:
534         //
535         //      match (&arg0, &arg1) {
536         //          (tmp0, tmp1) => args_array
537         //      }
538         //
539         // It was:
540         //
541         //      let tmp0 = &arg0;
542         //      let tmp1 = &arg1;
543         //      args_array
544         //
545         // Because of #11585 the new temporary lifetime rule, the enclosing
546         // statements for these temporaries become the let's themselves.
547         // If one or more of them are RefCell's, RefCell borrow() will also
548         // end there; they don't last long enough for args_array to use them.
549         // The match expression solves the scope problem.
550         //
551         // Note, it may also very well be transformed to:
552         //
553         //      match arg0 {
554         //          ref tmp0 => {
555         //              match arg1 => {
556         //                  ref tmp1 => args_array } } }
557         //
558         // But the nested match expression is proved to perform not as well
559         // as series of let's; the first approach does.
560         let pat = self.ecx.pat_tuple(self.fmtsp, pats);
561         let arm = self.ecx.arm(self.fmtsp, vec!(pat), args_array);
562         let head = self.ecx.expr(self.fmtsp, ast::ExprTup(heads));
563         let result = self.ecx.expr_match(self.fmtsp, head, vec!(arm));
564
565         let args_slice = self.ecx.expr_addr_of(self.fmtsp, result);
566
567         // Now create the fmt::Arguments struct with all our locals we created.
568         let (fn_name, fn_args) = if self.all_pieces_simple {
569             ("new_v1", vec![pieces, args_slice])
570         } else {
571             // Build up the static array which will store our precompiled
572             // nonstandard placeholders, if there are any.
573             let piece_ty = self.ecx.ty_path(self.ecx.path_global(
574                     self.macsp,
575                     Context::rtpath(self.ecx, "Argument")));
576             let fmt = Context::static_array(self.ecx,
577                                             "__STATIC_FMTARGS",
578                                             piece_ty,
579                                             self.pieces);
580
581             ("new_v1_formatted", vec![pieces, args_slice, fmt])
582         };
583
584         let path = self.ecx.std_path(&["fmt", "Arguments", fn_name]);
585         self.ecx.expr_call_global(self.macsp, path, fn_args)
586     }
587
588     fn format_arg(ecx: &ExtCtxt, macsp: Span, sp: Span,
589                   ty: &ArgumentType, arg: P<ast::Expr>)
590                   -> P<ast::Expr> {
591         let trait_ = match *ty {
592             Known(ref tyname) => {
593                 match &tyname[..] {
594                     ""  => "Display",
595                     "?" => "Debug",
596                     "e" => "LowerExp",
597                     "E" => "UpperExp",
598                     "o" => "Octal",
599                     "p" => "Pointer",
600                     "b" => "Binary",
601                     "x" => "LowerHex",
602                     "X" => "UpperHex",
603                     _ => {
604                         ecx.span_err(sp,
605                                      &format!("unknown format trait `{}`",
606                                              *tyname));
607                         "Dummy"
608                     }
609                 }
610             }
611             Unsigned => {
612                 let path = ecx.std_path(&["fmt", "ArgumentV1", "from_usize"]);
613                 return ecx.expr_call_global(macsp, path, vec![arg])
614             }
615         };
616
617         let path = ecx.std_path(&["fmt", trait_, "fmt"]);
618         let format_fn = ecx.path_global(sp, path);
619         let path = ecx.std_path(&["fmt", "ArgumentV1", "new"]);
620         ecx.expr_call_global(macsp, path, vec![arg, ecx.expr_path(format_fn)])
621     }
622 }
623
624 pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, sp: Span,
625                                tts: &[ast::TokenTree])
626                                -> Box<base::MacResult+'cx> {
627
628     match parse_args(ecx, sp, tts) {
629         Some((efmt, args, order, names)) => {
630             MacEager::expr(expand_preparsed_format_args(ecx, sp, efmt,
631                                                       args, order, names))
632         }
633         None => DummyResult::expr(sp)
634     }
635 }
636
637 /// Take the various parts of `format_args!(efmt, args..., name=names...)`
638 /// and construct the appropriate formatting expression.
639 pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
640                                     efmt: P<ast::Expr>,
641                                     args: Vec<P<ast::Expr>>,
642                                     name_ordering: Vec<String>,
643                                     names: HashMap<String, P<ast::Expr>>)
644                                     -> P<ast::Expr> {
645     let arg_types: Vec<_> = (0..args.len()).map(|_| None).collect();
646     let macsp = ecx.call_site();
647     // Expand the format literal so that efmt.span will have a backtrace. This
648     // is essential for locating a bug when the format literal is generated in
649     // a macro. (e.g. println!("{}"), which uses concat!($fmt, "\n")).
650     let efmt = ecx.expander().fold_expr(efmt);
651     let mut cx = Context {
652         ecx: ecx,
653         args: args,
654         arg_types: arg_types,
655         names: names,
656         name_positions: HashMap::new(),
657         name_types: HashMap::new(),
658         name_ordering: name_ordering,
659         nest_level: 0,
660         next_arg: 0,
661         literal: String::new(),
662         pieces: Vec::new(),
663         str_pieces: Vec::new(),
664         all_pieces_simple: true,
665         macsp: macsp,
666         fmtsp: efmt.span,
667     };
668     let fmt = match expr_to_string(cx.ecx,
669                                    efmt,
670                                    "format argument must be a string literal.") {
671         Some((fmt, _)) => fmt,
672         None => return DummyResult::raw_expr(sp)
673     };
674
675     let mut parser = parse::Parser::new(&fmt);
676
677     loop {
678         match parser.next() {
679             Some(piece) => {
680                 if !parser.errors.is_empty() { break }
681                 cx.verify_piece(&piece);
682                 match cx.trans_piece(&piece) {
683                     Some(piece) => {
684                         let s = cx.trans_literal_string();
685                         cx.str_pieces.push(s);
686                         cx.pieces.push(piece);
687                     }
688                     None => {}
689                 }
690             }
691             None => break
692         }
693     }
694     if !parser.errors.is_empty() {
695         cx.ecx.span_err(cx.fmtsp, &format!("invalid format string: {}",
696                                           parser.errors.remove(0)));
697         return DummyResult::raw_expr(sp);
698     }
699     if !cx.literal.is_empty() {
700         let s = cx.trans_literal_string();
701         cx.str_pieces.push(s);
702     }
703
704     // Make sure that all arguments were used and all arguments have types.
705     for (i, ty) in cx.arg_types.iter().enumerate() {
706         if ty.is_none() {
707             cx.ecx.span_err(cx.args[i].span, "argument never used");
708         }
709     }
710     for (name, e) in &cx.names {
711         if !cx.name_types.contains_key(name) {
712             cx.ecx.span_err(e.span, "named argument never used");
713         }
714     }
715
716     cx.into_expr()
717 }