]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
1a4d82fc | 14 | use fmt_macros as parse; |
9cc50fc6 SL |
15 | |
16 | use syntax::ast; | |
9cc50fc6 SL |
17 | use syntax::ext::base::*; |
18 | use syntax::ext::base; | |
19 | use syntax::ext::build::AstBuilder; | |
476ff2be | 20 | use syntax::parse::token; |
9cc50fc6 | 21 | use syntax::ptr::P; |
ea8adc8c | 22 | use syntax::symbol::Symbol; |
3b2f2976 | 23 | use syntax_pos::{Span, DUMMY_SP}; |
3157f602 | 24 | use syntax::tokenstream; |
1a4d82fc | 25 | |
476ff2be | 26 | use std::collections::{HashMap, HashSet}; |
5bcae85e | 27 | use std::collections::hash_map::Entry; |
1a4d82fc JJ |
28 | |
29 | #[derive(PartialEq)] | |
30 | enum ArgumentType { | |
5bcae85e SL |
31 | Placeholder(String), |
32 | Count, | |
1a4d82fc JJ |
33 | } |
34 | ||
35 | enum Position { | |
85aaf69f | 36 | Exact(usize), |
1a4d82fc JJ |
37 | Named(String), |
38 | } | |
39 | ||
9e0c209e | 40 | struct Context<'a, 'b: 'a> { |
1a4d82fc | 41 | ecx: &'a mut ExtCtxt<'b>, |
9346a6ac AL |
42 | /// The macro's call site. References to unstable formatting internals must |
43 | /// use this span to pass the stability checker. | |
44 | macsp: Span, | |
45 | /// The span of the format string literal. | |
1a4d82fc JJ |
46 | fmtsp: Span, |
47 | ||
5bcae85e SL |
48 | /// List of parsed argument expressions. |
49 | /// Named expressions are resolved early, and are appended to the end of | |
50 | /// argument expressions. | |
51 | /// | |
52 | /// Example showing the various data structures in motion: | |
53 | /// | |
54 | /// * Original: `"{foo:o} {:o} {foo:x} {0:x} {1:o} {:x} {1:x} {0:o}"` | |
55 | /// * Implicit argument resolution: `"{foo:o} {0:o} {foo:x} {0:x} {1:o} {1:x} {1:x} {0:o}"` | |
56 | /// * Name resolution: `"{2:o} {0:o} {2:x} {0:x} {1:o} {1:x} {1:x} {0:o}"` | |
57 | /// * `arg_types` (in JSON): `[[0, 1, 0], [0, 1, 1], [0, 1]]` | |
58 | /// * `arg_unique_types` (in simplified JSON): `[["o", "x"], ["o", "x"], ["o", "x"]]` | |
59 | /// * `names` (in JSON): `{"foo": 2}` | |
1a4d82fc | 60 | args: Vec<P<ast::Expr>>, |
5bcae85e SL |
61 | /// Placeholder slot numbers indexed by argument. |
62 | arg_types: Vec<Vec<usize>>, | |
63 | /// Unique format specs seen for each argument. | |
64 | arg_unique_types: Vec<Vec<ArgumentType>>, | |
65 | /// Map from named arguments to their resolved indices. | |
66 | names: HashMap<String, usize>, | |
1a4d82fc JJ |
67 | |
68 | /// The latest consecutive literal strings, or empty if there weren't any. | |
69 | literal: String, | |
70 | ||
71 | /// Collection of the compiled `rt::Argument` structures | |
72 | pieces: Vec<P<ast::Expr>>, | |
73 | /// Collection of string literals | |
74 | str_pieces: Vec<P<ast::Expr>>, | |
75 | /// Stays `true` if all formatting parameters are default (as in "{}{}"). | |
76 | all_pieces_simple: bool, | |
77 | ||
5bcae85e SL |
78 | /// Mapping between positional argument references and indices into the |
79 | /// final generated static argument array. We record the starting indices | |
80 | /// corresponding to each positional argument, and number of references | |
81 | /// consumed so far for each argument, to facilitate correct `Position` | |
82 | /// mapping in `trans_piece`. In effect this can be seen as a "flattened" | |
83 | /// version of `arg_unique_types`. | |
84 | /// | |
85 | /// Again with the example described above in docstring for `args`: | |
86 | /// | |
87 | /// * `arg_index_map` (in JSON): `[[0, 1, 0], [2, 3, 3], [4, 5]]` | |
88 | arg_index_map: Vec<Vec<usize>>, | |
89 | ||
90 | /// Starting offset of count argument slots. | |
91 | count_args_index_offset: usize, | |
1a4d82fc | 92 | |
5bcae85e SL |
93 | /// Count argument slots and tracking data structures. |
94 | /// Count arguments are separately tracked for de-duplication in case | |
95 | /// multiple references are made to one argument. For example, in this | |
96 | /// format string: | |
97 | /// | |
98 | /// * Original: `"{:.*} {:.foo$} {1:.*} {:.0$}"` | |
99 | /// * Implicit argument resolution: `"{1:.0$} {2:.foo$} {1:.3$} {4:.0$}"` | |
100 | /// * Name resolution: `"{1:.0$} {2:.5$} {1:.3$} {4:.0$}"` | |
101 | /// * `count_positions` (in JSON): `{0: 0, 5: 1, 3: 2}` | |
102 | /// * `count_args`: `vec![Exact(0), Exact(5), Exact(3)]` | |
103 | count_args: Vec<Position>, | |
104 | /// Relative slot numbers for count arguments. | |
105 | count_positions: HashMap<usize, usize>, | |
106 | /// Number of count slots assigned. | |
107 | count_positions_count: usize, | |
108 | ||
109 | /// Current position of the implicit positional arg pointer, as if it | |
110 | /// still existed in this phase of processing. | |
111 | /// Used only for `all_pieces_simple` tracking in `trans_piece`. | |
112 | curarg: usize, | |
abe05a73 XL |
113 | /// Keep track of invalid references to positional arguments |
114 | invalid_refs: Vec<usize>, | |
1a4d82fc JJ |
115 | } |
116 | ||
117 | /// Parses the arguments from the given list of tokens, returning None | |
118 | /// if there's a parse error so we can continue parsing other format! | |
119 | /// expressions. | |
120 | /// | |
121 | /// If parsing succeeds, the return value is: | |
041b39d2 XL |
122 | /// |
123 | /// ```text | |
5bcae85e | 124 | /// Some((fmtstr, parsed arguments, index map for named arguments)) |
92a42be0 | 125 | /// ``` |
9e0c209e SL |
126 | fn parse_args(ecx: &mut ExtCtxt, |
127 | sp: Span, | |
128 | tts: &[tokenstream::TokenTree]) | |
5bcae85e SL |
129 | -> Option<(P<ast::Expr>, Vec<P<ast::Expr>>, HashMap<String, usize>)> { |
130 | let mut args = Vec::<P<ast::Expr>>::new(); | |
131 | let mut names = HashMap::<String, usize>::new(); | |
1a4d82fc JJ |
132 | |
133 | let mut p = ecx.new_parser_from_tts(tts); | |
134 | ||
135 | if p.token == token::Eof { | |
136 | ecx.span_err(sp, "requires at least a format string argument"); | |
137 | return None; | |
138 | } | |
92a42be0 | 139 | let fmtstr = panictry!(p.parse_expr()); |
1a4d82fc JJ |
140 | let mut named = false; |
141 | while p.token != token::Eof { | |
9cc50fc6 | 142 | if !p.eat(&token::Comma) { |
1a4d82fc JJ |
143 | ecx.span_err(sp, "expected token: `,`"); |
144 | return None; | |
145 | } | |
9e0c209e SL |
146 | if p.token == token::Eof { |
147 | break; | |
148 | } // accept trailing commas | |
1a4d82fc JJ |
149 | if named || (p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq)) { |
150 | named = true; | |
151 | let ident = match p.token { | |
0531ce1d | 152 | token::Ident(i, _) => { |
9cc50fc6 | 153 | p.bump(); |
1a4d82fc JJ |
154 | i |
155 | } | |
156 | _ if named => { | |
157 | ecx.span_err(p.span, | |
158 | "expected ident, positional arguments \ | |
159 | cannot follow named arguments"); | |
160 | return None; | |
161 | } | |
162 | _ => { | |
163 | ecx.span_err(p.span, | |
164 | &format!("expected ident for named argument, found `{}`", | |
9e0c209e | 165 | p.this_token_to_string())); |
1a4d82fc JJ |
166 | return None; |
167 | } | |
168 | }; | |
c1a9b12d | 169 | let name: &str = &ident.name.as_str(); |
85aaf69f | 170 | |
9346a6ac | 171 | panictry!(p.expect(&token::Eq)); |
92a42be0 | 172 | let e = panictry!(p.parse_expr()); |
3157f602 | 173 | if let Some(prev) = names.get(name) { |
9e0c209e | 174 | ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", name)) |
5bcae85e | 175 | .span_note(args[*prev].span, "previously here") |
3157f602 XL |
176 | .emit(); |
177 | continue; | |
1a4d82fc | 178 | } |
5bcae85e SL |
179 | |
180 | // Resolve names into slots early. | |
181 | // Since all the positional args are already seen at this point | |
182 | // if the input is valid, we can simply append to the positional | |
183 | // args. And remember the names. | |
184 | let slot = args.len(); | |
185 | names.insert(name.to_string(), slot); | |
186 | args.push(e); | |
1a4d82fc | 187 | } else { |
92a42be0 | 188 | args.push(panictry!(p.parse_expr())); |
1a4d82fc JJ |
189 | } |
190 | } | |
5bcae85e | 191 | Some((fmtstr, args, names)) |
1a4d82fc JJ |
192 | } |
193 | ||
194 | impl<'a, 'b> Context<'a, 'b> { | |
5bcae85e SL |
195 | fn resolve_name_inplace(&self, p: &mut parse::Piece) { |
196 | // NOTE: the `unwrap_or` branch is needed in case of invalid format | |
197 | // arguments, e.g. `format_args!("{foo}")`. | |
198 | let lookup = |s| *self.names.get(s).unwrap_or(&0); | |
199 | ||
200 | match *p { | |
201 | parse::String(_) => {} | |
202 | parse::NextArgument(ref mut arg) => { | |
203 | if let parse::ArgumentNamed(s) = arg.position { | |
204 | arg.position = parse::ArgumentIs(lookup(s)); | |
205 | } | |
206 | if let parse::CountIsName(s) = arg.format.width { | |
207 | arg.format.width = parse::CountIsParam(lookup(s)); | |
208 | } | |
209 | if let parse::CountIsName(s) = arg.format.precision { | |
210 | arg.format.precision = parse::CountIsParam(lookup(s)); | |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | /// Verifies one piece of a parse string, and remembers it if valid. | |
217 | /// All errors are not emitted as fatal so we can continue giving errors | |
218 | /// about this and possibly other format strings. | |
1a4d82fc JJ |
219 | fn verify_piece(&mut self, p: &parse::Piece) { |
220 | match *p { | |
221 | parse::String(..) => {} | |
222 | parse::NextArgument(ref arg) => { | |
223 | // width/precision first, if they have implicit positional | |
224 | // parameters it makes more sense to consume them first. | |
225 | self.verify_count(arg.format.width); | |
226 | self.verify_count(arg.format.precision); | |
227 | ||
228 | // argument second, if it's an implicit positional parameter | |
229 | // it's written second, so it should come after width/precision. | |
230 | let pos = match arg.position { | |
abe05a73 | 231 | parse::ArgumentIs(i) | parse::ArgumentImplicitlyIs(i) => Exact(i), |
1a4d82fc JJ |
232 | parse::ArgumentNamed(s) => Named(s.to_string()), |
233 | }; | |
234 | ||
5bcae85e | 235 | let ty = Placeholder(arg.format.ty.to_string()); |
1a4d82fc JJ |
236 | self.verify_arg_type(pos, ty); |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | fn verify_count(&mut self, c: parse::Count) { | |
242 | match c { | |
9e0c209e SL |
243 | parse::CountImplied | |
244 | parse::CountIs(..) => {} | |
1a4d82fc | 245 | parse::CountIsParam(i) => { |
5bcae85e | 246 | self.verify_arg_type(Exact(i), Count); |
1a4d82fc JJ |
247 | } |
248 | parse::CountIsName(s) => { | |
5bcae85e | 249 | self.verify_arg_type(Named(s.to_string()), Count); |
1a4d82fc JJ |
250 | } |
251 | } | |
252 | } | |
253 | ||
1a4d82fc JJ |
254 | fn describe_num_args(&self) -> String { |
255 | match self.args.len() { | |
abe05a73 | 256 | 0 => "no arguments were given".to_string(), |
1a4d82fc JJ |
257 | 1 => "there is 1 argument".to_string(), |
258 | x => format!("there are {} arguments", x), | |
259 | } | |
260 | } | |
261 | ||
abe05a73 XL |
262 | /// Handle invalid references to positional arguments. Output different |
263 | /// errors for the case where all arguments are positional and for when | |
264 | /// there are named arguments or numbered positional arguments in the | |
265 | /// format string. | |
266 | fn report_invalid_references(&self, numbered_position_args: bool) { | |
267 | let mut e; | |
268 | let mut refs: Vec<String> = self.invalid_refs | |
269 | .iter() | |
270 | .map(|r| r.to_string()) | |
271 | .collect(); | |
272 | ||
273 | if self.names.is_empty() && !numbered_position_args { | |
274 | e = self.ecx.mut_span_err(self.fmtsp, | |
275 | &format!("{} positional argument{} in format string, but {}", | |
276 | self.pieces.len(), | |
277 | if self.pieces.len() > 1 { "s" } else { "" }, | |
278 | self.describe_num_args())); | |
279 | } else { | |
280 | let arg_list = match refs.len() { | |
281 | 1 => format!("argument {}", refs.pop().unwrap()), | |
282 | _ => format!("arguments {head} and {tail}", | |
283 | tail=refs.pop().unwrap(), | |
284 | head=refs.join(", ")) | |
285 | }; | |
286 | ||
287 | e = self.ecx.mut_span_err(self.fmtsp, | |
288 | &format!("invalid reference to positional {} ({})", | |
289 | arg_list, | |
290 | self.describe_num_args())); | |
291 | e.note("positional arguments are zero-based"); | |
292 | }; | |
293 | ||
294 | e.emit(); | |
295 | } | |
296 | ||
5bcae85e SL |
297 | /// Actually verifies and tracks a given format placeholder |
298 | /// (a.k.a. argument). | |
1a4d82fc JJ |
299 | fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) { |
300 | match arg { | |
301 | Exact(arg) => { | |
302 | if self.args.len() <= arg { | |
abe05a73 | 303 | self.invalid_refs.push(arg); |
1a4d82fc JJ |
304 | return; |
305 | } | |
5bcae85e SL |
306 | match ty { |
307 | Placeholder(_) => { | |
308 | // record every (position, type) combination only once | |
309 | let ref mut seen_ty = self.arg_unique_types[arg]; | |
310 | let i = match seen_ty.iter().position(|x| *x == ty) { | |
311 | Some(i) => i, | |
312 | None => { | |
313 | let i = seen_ty.len(); | |
314 | seen_ty.push(ty); | |
315 | i | |
316 | } | |
317 | }; | |
318 | self.arg_types[arg].push(i); | |
319 | } | |
320 | Count => { | |
321 | match self.count_positions.entry(arg) { | |
322 | Entry::Vacant(e) => { | |
323 | let i = self.count_positions_count; | |
324 | e.insert(i); | |
325 | self.count_args.push(Exact(arg)); | |
326 | self.count_positions_count += 1; | |
327 | } | |
328 | Entry::Occupied(_) => {} | |
329 | } | |
330 | } | |
1a4d82fc JJ |
331 | } |
332 | } | |
333 | ||
334 | Named(name) => { | |
5bcae85e SL |
335 | let idx = match self.names.get(&name) { |
336 | Some(e) => *e, | |
1a4d82fc JJ |
337 | None => { |
338 | let msg = format!("there is no argument named `{}`", name); | |
85aaf69f | 339 | self.ecx.span_err(self.fmtsp, &msg[..]); |
1a4d82fc JJ |
340 | return; |
341 | } | |
342 | }; | |
5bcae85e SL |
343 | // Treat as positional arg. |
344 | self.verify_arg_type(Exact(idx), ty) | |
1a4d82fc JJ |
345 | } |
346 | } | |
347 | } | |
348 | ||
5bcae85e SL |
349 | /// Builds the mapping between format placeholders and argument objects. |
350 | fn build_index_map(&mut self) { | |
351 | // NOTE: Keep the ordering the same as `into_expr`'s expansion would do! | |
352 | let args_len = self.args.len(); | |
353 | self.arg_index_map.reserve(args_len); | |
354 | ||
355 | let mut sofar = 0usize; | |
356 | ||
357 | // Map the arguments | |
358 | for i in 0..args_len { | |
359 | let ref arg_types = self.arg_types[i]; | |
360 | let mut arg_offsets = Vec::with_capacity(arg_types.len()); | |
361 | for offset in arg_types { | |
362 | arg_offsets.push(sofar + *offset); | |
1a4d82fc | 363 | } |
5bcae85e SL |
364 | self.arg_index_map.push(arg_offsets); |
365 | sofar += self.arg_unique_types[i].len(); | |
1a4d82fc | 366 | } |
5bcae85e SL |
367 | |
368 | // Record starting index for counts, which appear just after arguments | |
369 | self.count_args_index_offset = sofar; | |
1a4d82fc JJ |
370 | } |
371 | ||
1a4d82fc | 372 | fn rtpath(ecx: &ExtCtxt, s: &str) -> Vec<ast::Ident> { |
e9174d1e | 373 | ecx.std_path(&["fmt", "rt", "v1", s]) |
1a4d82fc JJ |
374 | } |
375 | ||
376 | fn trans_count(&self, c: parse::Count) -> P<ast::Expr> { | |
9346a6ac | 377 | let sp = self.macsp; |
85aaf69f SL |
378 | let count = |c, arg| { |
379 | let mut path = Context::rtpath(self.ecx, "Count"); | |
380 | path.push(self.ecx.ident_of(c)); | |
381 | match arg { | |
382 | Some(arg) => self.ecx.expr_call_global(sp, path, vec![arg]), | |
383 | None => self.ecx.expr_path(self.ecx.path_global(sp, path)), | |
1a4d82fc | 384 | } |
85aaf69f SL |
385 | }; |
386 | match c { | |
387 | parse::CountIs(i) => count("Is", Some(self.ecx.expr_usize(sp, i))), | |
1a4d82fc | 388 | parse::CountIsParam(i) => { |
5bcae85e SL |
389 | // This needs mapping too, as `i` is referring to a macro |
390 | // argument. | |
391 | let i = match self.count_positions.get(&i) { | |
1a4d82fc JJ |
392 | Some(&i) => i, |
393 | None => 0, // error already emitted elsewhere | |
394 | }; | |
5bcae85e | 395 | let i = i + self.count_args_index_offset; |
85aaf69f | 396 | count("Param", Some(self.ecx.expr_usize(sp, i))) |
1a4d82fc | 397 | } |
5bcae85e SL |
398 | parse::CountImplied => count("Implied", None), |
399 | // should never be the case, names are already resolved | |
400 | parse::CountIsName(_) => panic!("should never happen"), | |
1a4d82fc JJ |
401 | } |
402 | } | |
403 | ||
404 | /// Translate the accumulated string literals to a literal expression | |
405 | fn trans_literal_string(&mut self) -> P<ast::Expr> { | |
406 | let sp = self.fmtsp; | |
476ff2be | 407 | let s = Symbol::intern(&self.literal); |
1a4d82fc JJ |
408 | self.literal.clear(); |
409 | self.ecx.expr_str(sp, s) | |
410 | } | |
411 | ||
412 | /// Translate a `parse::Piece` to a static `rt::Argument` or append | |
413 | /// to the `literal` string. | |
5bcae85e SL |
414 | fn trans_piece(&mut self, |
415 | piece: &parse::Piece, | |
416 | arg_index_consumed: &mut Vec<usize>) | |
417 | -> Option<P<ast::Expr>> { | |
9346a6ac | 418 | let sp = self.macsp; |
1a4d82fc JJ |
419 | match *piece { |
420 | parse::String(s) => { | |
421 | self.literal.push_str(s); | |
422 | None | |
423 | } | |
424 | parse::NextArgument(ref arg) => { | |
425 | // Translate the position | |
85aaf69f SL |
426 | let pos = { |
427 | let pos = |c, arg| { | |
428 | let mut path = Context::rtpath(self.ecx, "Position"); | |
429 | path.push(self.ecx.ident_of(c)); | |
430 | match arg { | |
431 | Some(i) => { | |
432 | let arg = self.ecx.expr_usize(sp, i); | |
433 | self.ecx.expr_call_global(sp, path, vec![arg]) | |
434 | } | |
9e0c209e | 435 | None => self.ecx.expr_path(self.ecx.path_global(sp, path)), |
85aaf69f SL |
436 | } |
437 | }; | |
438 | match arg.position { | |
abe05a73 XL |
439 | parse::ArgumentIs(i) |
440 | | parse::ArgumentImplicitlyIs(i) => { | |
5bcae85e SL |
441 | // Map to index in final generated argument array |
442 | // in case of multiple types specified | |
443 | let arg_idx = match arg_index_consumed.get_mut(i) { | |
85aaf69f | 444 | None => 0, // error already emitted elsewhere |
5bcae85e SL |
445 | Some(offset) => { |
446 | let ref idx_map = self.arg_index_map[i]; | |
447 | // unwrap_or branch: error already emitted elsewhere | |
448 | let arg_idx = *idx_map.get(*offset).unwrap_or(&0); | |
449 | *offset += 1; | |
450 | arg_idx | |
451 | } | |
85aaf69f | 452 | }; |
5bcae85e | 453 | pos("At", Some(arg_idx)) |
85aaf69f | 454 | } |
5bcae85e SL |
455 | |
456 | // should never be the case, because names are already | |
457 | // resolved. | |
458 | parse::ArgumentNamed(_) => panic!("should never happen"), | |
1a4d82fc JJ |
459 | } |
460 | }; | |
461 | ||
462 | let simple_arg = parse::Argument { | |
5bcae85e SL |
463 | position: { |
464 | // We don't have ArgumentNext any more, so we have to | |
465 | // track the current argument ourselves. | |
466 | let i = self.curarg; | |
467 | self.curarg += 1; | |
468 | parse::ArgumentIs(i) | |
469 | }, | |
1a4d82fc JJ |
470 | format: parse::FormatSpec { |
471 | fill: arg.format.fill, | |
472 | align: parse::AlignUnknown, | |
473 | flags: 0, | |
474 | precision: parse::CountImplied, | |
475 | width: parse::CountImplied, | |
9e0c209e SL |
476 | ty: arg.format.ty, |
477 | }, | |
1a4d82fc JJ |
478 | }; |
479 | ||
9e0c209e SL |
480 | let fill = match arg.format.fill { |
481 | Some(c) => c, | |
482 | None => ' ', | |
483 | }; | |
1a4d82fc JJ |
484 | |
485 | if *arg != simple_arg || fill != ' ' { | |
486 | self.all_pieces_simple = false; | |
487 | } | |
488 | ||
489 | // Translate the format | |
7453a54e | 490 | let fill = self.ecx.expr_lit(sp, ast::LitKind::Char(fill)); |
85aaf69f SL |
491 | let align = |name| { |
492 | let mut p = Context::rtpath(self.ecx, "Alignment"); | |
493 | p.push(self.ecx.ident_of(name)); | |
494 | self.ecx.path_global(sp, p) | |
495 | }; | |
1a4d82fc | 496 | let align = match arg.format.align { |
85aaf69f SL |
497 | parse::AlignLeft => align("Left"), |
498 | parse::AlignRight => align("Right"), | |
499 | parse::AlignCenter => align("Center"), | |
500 | parse::AlignUnknown => align("Unknown"), | |
1a4d82fc JJ |
501 | }; |
502 | let align = self.ecx.expr_path(align); | |
c34b1796 | 503 | let flags = self.ecx.expr_u32(sp, arg.format.flags); |
1a4d82fc JJ |
504 | let prec = self.trans_count(arg.format.precision); |
505 | let width = self.trans_count(arg.format.width); | |
506 | let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "FormatSpec")); | |
9e0c209e SL |
507 | let fmt = |
508 | self.ecx.expr_struct(sp, | |
509 | path, | |
510 | vec![self.ecx | |
511 | .field_imm(sp, self.ecx.ident_of("fill"), fill), | |
512 | self.ecx.field_imm(sp, | |
513 | self.ecx.ident_of("align"), | |
514 | align), | |
515 | self.ecx.field_imm(sp, | |
516 | self.ecx.ident_of("flags"), | |
517 | flags), | |
518 | self.ecx.field_imm(sp, | |
519 | self.ecx.ident_of("precision"), | |
520 | prec), | |
521 | self.ecx.field_imm(sp, | |
522 | self.ecx.ident_of("width"), | |
523 | width)]); | |
1a4d82fc JJ |
524 | |
525 | let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "Argument")); | |
9e0c209e SL |
526 | Some(self.ecx.expr_struct(sp, |
527 | path, | |
528 | vec![self.ecx.field_imm(sp, | |
529 | self.ecx.ident_of("position"), | |
530 | pos), | |
531 | self.ecx.field_imm(sp, | |
532 | self.ecx.ident_of("format"), | |
533 | fmt)])) | |
1a4d82fc JJ |
534 | } |
535 | } | |
536 | } | |
537 | ||
5bcae85e SL |
538 | /// Actually builds the expression which the format_args! block will be |
539 | /// expanded to | |
3b2f2976 | 540 | fn into_expr(self) -> P<ast::Expr> { |
1a4d82fc | 541 | let mut locals = Vec::new(); |
5bcae85e | 542 | let mut counts = Vec::new(); |
1a4d82fc JJ |
543 | let mut pats = Vec::new(); |
544 | let mut heads = Vec::new(); | |
545 | ||
83c7162d XL |
546 | let names_pos: Vec<_> = (0..self.args.len()).map(|i| { |
547 | self.ecx.ident_of(&format!("arg{}", i)).gensym() | |
548 | }).collect(); | |
549 | ||
1a4d82fc JJ |
550 | // First, build up the static array which will become our precompiled |
551 | // format "string" | |
ea8adc8c | 552 | let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces); |
1a4d82fc | 553 | |
5bcae85e SL |
554 | // Before consuming the expressions, we have to remember spans for |
555 | // count arguments as they are now generated separate from other | |
556 | // arguments, hence have no access to the `P<ast::Expr>`'s. | |
557 | let spans_pos: Vec<_> = self.args.iter().map(|e| e.span.clone()).collect(); | |
1a4d82fc JJ |
558 | |
559 | // Right now there is a bug such that for the expression: | |
560 | // foo(bar(&1)) | |
561 | // the lifetime of `1` doesn't outlast the call to `bar`, so it's not | |
562 | // valid for the call to `foo`. To work around this all arguments to the | |
563 | // format! string are shoved into locals. Furthermore, we shove the address | |
564 | // of each variable because we don't want to move out of the arguments | |
565 | // passed to this function. | |
566 | for (i, e) in self.args.into_iter().enumerate() { | |
83c7162d | 567 | let name = names_pos[i]; |
ea8adc8c XL |
568 | let span = |
569 | DUMMY_SP.with_ctxt(e.span.ctxt().apply_mark(self.ecx.current_expansion.mark)); | |
041b39d2 | 570 | pats.push(self.ecx.pat_ident(span, name)); |
5bcae85e | 571 | for ref arg_ty in self.arg_unique_types[i].iter() { |
cc61c64b | 572 | locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name)); |
5bcae85e | 573 | } |
1a4d82fc JJ |
574 | heads.push(self.ecx.expr_addr_of(e.span, e)); |
575 | } | |
5bcae85e | 576 | for pos in self.count_args { |
83c7162d XL |
577 | let index = match pos { |
578 | Exact(i) => i, | |
5bcae85e | 579 | _ => panic!("should never happen"), |
1a4d82fc | 580 | }; |
83c7162d XL |
581 | let name = names_pos[index]; |
582 | let span = spans_pos[index]; | |
cc61c64b | 583 | counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, name)); |
1a4d82fc JJ |
584 | } |
585 | ||
586 | // Now create a vector containing all the arguments | |
5bcae85e | 587 | let args = locals.into_iter().chain(counts.into_iter()); |
1a4d82fc JJ |
588 | |
589 | let args_array = self.ecx.expr_vec(self.fmtsp, args.collect()); | |
590 | ||
591 | // Constructs an AST equivalent to: | |
592 | // | |
593 | // match (&arg0, &arg1) { | |
594 | // (tmp0, tmp1) => args_array | |
595 | // } | |
596 | // | |
597 | // It was: | |
598 | // | |
599 | // let tmp0 = &arg0; | |
600 | // let tmp1 = &arg1; | |
601 | // args_array | |
602 | // | |
603 | // Because of #11585 the new temporary lifetime rule, the enclosing | |
604 | // statements for these temporaries become the let's themselves. | |
605 | // If one or more of them are RefCell's, RefCell borrow() will also | |
606 | // end there; they don't last long enough for args_array to use them. | |
607 | // The match expression solves the scope problem. | |
608 | // | |
609 | // Note, it may also very well be transformed to: | |
610 | // | |
611 | // match arg0 { | |
612 | // ref tmp0 => { | |
613 | // match arg1 => { | |
614 | // ref tmp1 => args_array } } } | |
615 | // | |
616 | // But the nested match expression is proved to perform not as well | |
617 | // as series of let's; the first approach does. | |
618 | let pat = self.ecx.pat_tuple(self.fmtsp, pats); | |
9e0c209e | 619 | let arm = self.ecx.arm(self.fmtsp, vec![pat], args_array); |
7453a54e | 620 | let head = self.ecx.expr(self.fmtsp, ast::ExprKind::Tup(heads)); |
9e0c209e | 621 | let result = self.ecx.expr_match(self.fmtsp, head, vec![arm]); |
1a4d82fc JJ |
622 | |
623 | let args_slice = self.ecx.expr_addr_of(self.fmtsp, result); | |
624 | ||
625 | // Now create the fmt::Arguments struct with all our locals we created. | |
626 | let (fn_name, fn_args) = if self.all_pieces_simple { | |
85aaf69f | 627 | ("new_v1", vec![pieces, args_slice]) |
1a4d82fc JJ |
628 | } else { |
629 | // Build up the static array which will store our precompiled | |
630 | // nonstandard placeholders, if there are any. | |
ea8adc8c | 631 | let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces); |
1a4d82fc | 632 | |
85aaf69f | 633 | ("new_v1_formatted", vec![pieces, args_slice, fmt]) |
1a4d82fc JJ |
634 | }; |
635 | ||
e9174d1e SL |
636 | let path = self.ecx.std_path(&["fmt", "Arguments", fn_name]); |
637 | self.ecx.expr_call_global(self.macsp, path, fn_args) | |
1a4d82fc JJ |
638 | } |
639 | ||
9e0c209e SL |
640 | fn format_arg(ecx: &ExtCtxt, |
641 | macsp: Span, | |
cc61c64b | 642 | mut sp: Span, |
9e0c209e | 643 | ty: &ArgumentType, |
cc61c64b | 644 | arg: ast::Ident) |
1a4d82fc | 645 | -> P<ast::Expr> { |
83c7162d | 646 | sp = sp.apply_mark(ecx.current_expansion.mark); |
cc61c64b | 647 | let arg = ecx.expr_ident(sp, arg); |
1a4d82fc | 648 | let trait_ = match *ty { |
5bcae85e | 649 | Placeholder(ref tyname) => { |
85aaf69f | 650 | match &tyname[..] { |
9e0c209e | 651 | "" => "Display", |
85aaf69f | 652 | "?" => "Debug", |
1a4d82fc JJ |
653 | "e" => "LowerExp", |
654 | "E" => "UpperExp", | |
655 | "o" => "Octal", | |
656 | "p" => "Pointer", | |
657 | "b" => "Binary", | |
658 | "x" => "LowerHex", | |
659 | "X" => "UpperHex", | |
660 | _ => { | |
9e0c209e | 661 | ecx.span_err(sp, &format!("unknown format trait `{}`", *tyname)); |
1a4d82fc JJ |
662 | "Dummy" |
663 | } | |
664 | } | |
665 | } | |
5bcae85e | 666 | Count => { |
e9174d1e | 667 | let path = ecx.std_path(&["fmt", "ArgumentV1", "from_usize"]); |
9e0c209e | 668 | return ecx.expr_call_global(macsp, path, vec![arg]); |
1a4d82fc JJ |
669 | } |
670 | }; | |
671 | ||
e9174d1e SL |
672 | let path = ecx.std_path(&["fmt", trait_, "fmt"]); |
673 | let format_fn = ecx.path_global(sp, path); | |
674 | let path = ecx.std_path(&["fmt", "ArgumentV1", "new"]); | |
675 | ecx.expr_call_global(macsp, path, vec![arg, ecx.expr_path(format_fn)]) | |
1a4d82fc JJ |
676 | } |
677 | } | |
678 | ||
9e0c209e | 679 | pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, |
041b39d2 | 680 | mut sp: Span, |
3157f602 | 681 | tts: &[tokenstream::TokenTree]) |
9e0c209e | 682 | -> Box<base::MacResult + 'cx> { |
83c7162d | 683 | sp = sp.apply_mark(ecx.current_expansion.mark); |
1a4d82fc | 684 | match parse_args(ecx, sp, tts) { |
5bcae85e | 685 | Some((efmt, args, names)) => { |
9e0c209e | 686 | MacEager::expr(expand_preparsed_format_args(ecx, sp, efmt, args, names)) |
1a4d82fc | 687 | } |
9e0c209e | 688 | None => DummyResult::expr(sp), |
1a4d82fc JJ |
689 | } |
690 | } | |
691 | ||
692 | /// Take the various parts of `format_args!(efmt, args..., name=names...)` | |
693 | /// and construct the appropriate formatting expression. | |
9e0c209e SL |
694 | pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, |
695 | sp: Span, | |
1a4d82fc JJ |
696 | efmt: P<ast::Expr>, |
697 | args: Vec<P<ast::Expr>>, | |
5bcae85e | 698 | names: HashMap<String, usize>) |
1a4d82fc | 699 | -> P<ast::Expr> { |
5bcae85e SL |
700 | // NOTE: this verbose way of initializing `Vec<Vec<ArgumentType>>` is because |
701 | // `ArgumentType` does not derive `Clone`. | |
702 | let arg_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect(); | |
703 | let arg_unique_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect(); | |
041b39d2 | 704 | let mut macsp = ecx.call_site(); |
83c7162d | 705 | macsp = macsp.apply_mark(ecx.current_expansion.mark); |
9e0c209e SL |
706 | let msg = "format argument must be a string literal."; |
707 | let fmt = match expr_to_spanned_string(ecx, efmt, msg) { | |
708 | Some(fmt) => fmt, | |
709 | None => return DummyResult::raw_expr(sp), | |
710 | }; | |
711 | ||
1a4d82fc | 712 | let mut cx = Context { |
3b2f2976 XL |
713 | ecx, |
714 | args, | |
715 | arg_types, | |
716 | arg_unique_types, | |
717 | names, | |
5bcae85e SL |
718 | curarg: 0, |
719 | arg_index_map: Vec::new(), | |
720 | count_args: Vec::new(), | |
721 | count_positions: HashMap::new(), | |
722 | count_positions_count: 0, | |
723 | count_args_index_offset: 0, | |
1a4d82fc JJ |
724 | literal: String::new(), |
725 | pieces: Vec::new(), | |
726 | str_pieces: Vec::new(), | |
727 | all_pieces_simple: true, | |
3b2f2976 | 728 | macsp, |
9e0c209e | 729 | fmtsp: fmt.span, |
abe05a73 | 730 | invalid_refs: Vec::new(), |
1a4d82fc JJ |
731 | }; |
732 | ||
476ff2be SL |
733 | let fmt_str = &*fmt.node.0.as_str(); |
734 | let mut parser = parse::Parser::new(fmt_str); | |
5bcae85e | 735 | let mut pieces = vec![]; |
85aaf69f | 736 | |
0531ce1d XL |
737 | while let Some(mut piece) = parser.next() { |
738 | if !parser.errors.is_empty() { | |
739 | break; | |
1a4d82fc | 740 | } |
0531ce1d XL |
741 | cx.verify_piece(&piece); |
742 | cx.resolve_name_inplace(&mut piece); | |
743 | pieces.push(piece); | |
1a4d82fc | 744 | } |
5bcae85e | 745 | |
abe05a73 XL |
746 | let numbered_position_args = pieces.iter().any(|arg: &parse::Piece| { |
747 | match *arg { | |
748 | parse::String(_) => false, | |
749 | parse::NextArgument(arg) => { | |
750 | match arg.position { | |
751 | parse::Position::ArgumentIs(_) => true, | |
752 | _ => false, | |
753 | } | |
754 | } | |
755 | } | |
756 | }); | |
757 | ||
5bcae85e SL |
758 | cx.build_index_map(); |
759 | ||
760 | let mut arg_index_consumed = vec![0usize; cx.arg_index_map.len()]; | |
761 | for piece in pieces { | |
762 | if let Some(piece) = cx.trans_piece(&piece, &mut arg_index_consumed) { | |
763 | let s = cx.trans_literal_string(); | |
764 | cx.str_pieces.push(s); | |
765 | cx.pieces.push(piece); | |
766 | } | |
767 | } | |
768 | ||
1a4d82fc | 769 | if !parser.errors.is_empty() { |
476ff2be SL |
770 | let (err, note) = parser.errors.remove(0); |
771 | let mut e = cx.ecx.struct_span_err(cx.fmtsp, &format!("invalid format string: {}", err)); | |
772 | if let Some(note) = note { | |
773 | e.note(¬e); | |
774 | } | |
775 | e.emit(); | |
1a4d82fc JJ |
776 | return DummyResult::raw_expr(sp); |
777 | } | |
778 | if !cx.literal.is_empty() { | |
779 | let s = cx.trans_literal_string(); | |
780 | cx.str_pieces.push(s); | |
781 | } | |
782 | ||
abe05a73 XL |
783 | if cx.invalid_refs.len() >= 1 { |
784 | cx.report_invalid_references(numbered_position_args); | |
785 | } | |
786 | ||
1a4d82fc | 787 | // Make sure that all arguments were used and all arguments have types. |
5bcae85e | 788 | let num_pos_args = cx.args.len() - cx.names.len(); |
476ff2be | 789 | let mut errs = vec![]; |
1a4d82fc | 790 | for (i, ty) in cx.arg_types.iter().enumerate() { |
5bcae85e SL |
791 | if ty.len() == 0 { |
792 | if cx.count_positions.contains_key(&i) { | |
793 | continue; | |
794 | } | |
795 | let msg = if i >= num_pos_args { | |
796 | // named argument | |
797 | "named argument never used" | |
798 | } else { | |
799 | // positional argument | |
800 | "argument never used" | |
801 | }; | |
476ff2be SL |
802 | errs.push((cx.args[i].span, msg)); |
803 | } | |
804 | } | |
805 | if errs.len() > 0 { | |
806 | let args_used = cx.arg_types.len() - errs.len(); | |
807 | let args_unused = errs.len(); | |
808 | ||
809 | let mut diag = { | |
810 | if errs.len() == 1 { | |
811 | let (sp, msg) = errs.into_iter().next().unwrap(); | |
812 | cx.ecx.struct_span_err(sp, msg) | |
813 | } else { | |
2c00a5a8 XL |
814 | let mut diag = cx.ecx.struct_span_err( |
815 | errs.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(), | |
816 | "multiple unused formatting arguments" | |
817 | ); | |
818 | diag.span_label(cx.fmtsp, "multiple unused arguments in this statement"); | |
476ff2be SL |
819 | diag |
820 | } | |
821 | }; | |
822 | ||
823 | // Decide if we want to look for foreign formatting directives. | |
824 | if args_used < args_unused { | |
825 | use super::format_foreign as foreign; | |
826 | ||
827 | // The set of foreign substitutions we've explained. This prevents spamming the user | |
828 | // with `%d should be written as {}` over and over again. | |
829 | let mut explained = HashSet::new(); | |
830 | ||
831 | // Used to ensure we only report translations for *one* kind of foreign format. | |
832 | let mut found_foreign = false; | |
833 | ||
834 | macro_rules! check_foreign { | |
835 | ($kind:ident) => {{ | |
836 | let mut show_doc_note = false; | |
837 | ||
838 | for sub in foreign::$kind::iter_subs(fmt_str) { | |
839 | let trn = match sub.translate() { | |
840 | Some(trn) => trn, | |
841 | ||
842 | // If it has no translation, don't call it out specifically. | |
843 | None => continue, | |
844 | }; | |
845 | ||
846 | let sub = String::from(sub.as_str()); | |
847 | if explained.contains(&sub) { | |
848 | continue; | |
849 | } | |
850 | explained.insert(sub.clone()); | |
851 | ||
852 | if !found_foreign { | |
853 | found_foreign = true; | |
854 | show_doc_note = true; | |
855 | } | |
856 | ||
857 | diag.help(&format!("`{}` should be written as `{}`", sub, trn)); | |
858 | } | |
859 | ||
860 | if show_doc_note { | |
861 | diag.note(concat!(stringify!($kind), " formatting not supported; see \ | |
862 | the documentation for `std::fmt`")); | |
863 | } | |
864 | }}; | |
865 | } | |
866 | ||
867 | check_foreign!(printf); | |
868 | if !found_foreign { | |
869 | check_foreign!(shell); | |
870 | } | |
1a4d82fc | 871 | } |
476ff2be SL |
872 | |
873 | diag.emit(); | |
1a4d82fc JJ |
874 | } |
875 | ||
876 | cx.into_expr() | |
877 | } |