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