2013-07-29 03:12:41 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use ast;
|
2013-11-30 16:00:39 -06:00
|
|
|
use ast::P;
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::{Span, respan};
|
2013-07-29 03:12:41 -05:00
|
|
|
use ext::base::*;
|
|
|
|
use ext::base;
|
|
|
|
use ext::build::AstBuilder;
|
|
|
|
use rsparse = parse;
|
|
|
|
use parse::token;
|
2013-10-29 05:03:32 -05:00
|
|
|
use opt_vec;
|
2013-07-29 03:12:41 -05:00
|
|
|
use std::fmt::parse;
|
|
|
|
use std::hashmap::{HashMap, HashSet};
|
|
|
|
use std::vec;
|
|
|
|
|
|
|
|
#[deriving(Eq)]
|
|
|
|
enum ArgumentType {
|
|
|
|
Known(@str),
|
|
|
|
Unsigned,
|
|
|
|
String,
|
|
|
|
}
|
|
|
|
|
2013-12-25 23:55:05 -06:00
|
|
|
enum Position {
|
|
|
|
Exact(uint),
|
|
|
|
Named(@str),
|
|
|
|
}
|
|
|
|
|
2013-12-27 18:17:36 -06:00
|
|
|
struct Context<'a> {
|
2013-12-25 12:10:33 -06:00
|
|
|
ecx: &'a mut ExtCtxt<'a>,
|
2013-08-31 11:13:04 -05:00
|
|
|
fmtsp: Span,
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Parsed argument expressions and the types that we've found so far for
|
|
|
|
// them.
|
2013-09-01 20:45:37 -05:00
|
|
|
args: ~[@ast::Expr],
|
2013-07-29 03:12:41 -05:00
|
|
|
arg_types: ~[Option<ArgumentType>],
|
|
|
|
// Parsed named expressions and the types that we've found for them so far
|
2013-09-01 20:45:37 -05:00
|
|
|
names: HashMap<@str, @ast::Expr>,
|
2013-07-29 03:12:41 -05:00
|
|
|
name_types: HashMap<@str, ArgumentType>,
|
|
|
|
|
|
|
|
// Collection of the compiled `rt::Piece` structures
|
2013-09-01 20:45:37 -05:00
|
|
|
pieces: ~[@ast::Expr],
|
2013-07-29 03:12:41 -05:00
|
|
|
name_positions: HashMap<@str, uint>,
|
2014-01-09 07:05:33 -06:00
|
|
|
method_statics: ~[@ast::Item],
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Updated as arguments are consumed or methods are entered
|
|
|
|
nest_level: uint,
|
|
|
|
next_arg: uint,
|
|
|
|
}
|
|
|
|
|
2013-12-27 18:17:36 -06:00
|
|
|
impl<'a> Context<'a> {
|
2013-07-29 03:12:41 -05:00
|
|
|
/// Parses the arguments from the given list of tokens, returning None if
|
2013-09-27 23:01:58 -05:00
|
|
|
/// there's a parse error so we can continue parsing other format! expressions.
|
2014-01-09 07:05:33 -06:00
|
|
|
fn parse_args(&mut self, sp: Span, tts: &[ast::TokenTree])
|
2013-12-30 16:04:00 -06:00
|
|
|
-> (@ast::Expr, Option<@ast::Expr>) {
|
|
|
|
let mut p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
|
|
|
|
self.ecx.cfg(),
|
|
|
|
tts.to_owned());
|
2013-09-12 21:36:58 -05:00
|
|
|
// Parse the leading function expression (maybe a block, maybe a path)
|
|
|
|
let extra = p.parse_expr();
|
|
|
|
if !p.eat(&token::COMMA) {
|
|
|
|
self.ecx.span_err(sp, "expected token: `,`");
|
|
|
|
return (extra, None);
|
|
|
|
}
|
2013-08-20 02:40:27 -05:00
|
|
|
|
2013-12-30 17:09:41 -06:00
|
|
|
if p.token == token::EOF {
|
2013-08-20 02:40:27 -05:00
|
|
|
self.ecx.span_err(sp, "requires at least a format string argument");
|
|
|
|
return (extra, None);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
let fmtstr = p.parse_expr();
|
|
|
|
let mut named = false;
|
2013-12-30 17:09:41 -06:00
|
|
|
while p.token != token::EOF {
|
2013-07-29 03:12:41 -05:00
|
|
|
if !p.eat(&token::COMMA) {
|
|
|
|
self.ecx.span_err(sp, "expected token: `,`");
|
2013-08-20 02:40:27 -05:00
|
|
|
return (extra, None);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-12-30 17:09:41 -06:00
|
|
|
if p.token == token::EOF { break } // accept trailing commas
|
|
|
|
if named || (token::is_ident(&p.token) &&
|
2013-07-29 03:12:41 -05:00
|
|
|
p.look_ahead(1, |t| *t == token::EQ)) {
|
|
|
|
named = true;
|
2013-12-30 17:09:41 -06:00
|
|
|
let ident = match p.token {
|
2013-07-29 03:12:41 -05:00
|
|
|
token::IDENT(i, _) => {
|
|
|
|
p.bump();
|
|
|
|
i
|
|
|
|
}
|
|
|
|
_ if named => {
|
2013-12-30 17:17:53 -06:00
|
|
|
self.ecx.span_err(p.span,
|
2013-07-29 03:12:41 -05:00
|
|
|
"expected ident, positional arguments \
|
|
|
|
cannot follow named arguments");
|
2013-08-20 02:40:27 -05:00
|
|
|
return (extra, None);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2013-12-30 17:17:53 -06:00
|
|
|
self.ecx.span_err(p.span,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("expected ident for named \
|
|
|
|
argument, but found `{}`",
|
2013-07-29 03:12:41 -05:00
|
|
|
p.this_token_to_str()));
|
2013-08-20 02:40:27 -05:00
|
|
|
return (extra, None);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = self.ecx.str_of(ident);
|
|
|
|
p.expect(&token::EQ);
|
|
|
|
let e = p.parse_expr();
|
|
|
|
match self.names.find(&name) {
|
|
|
|
None => {}
|
|
|
|
Some(prev) => {
|
2013-09-27 23:01:58 -05:00
|
|
|
self.ecx.span_err(e.span, format!("duplicate argument \
|
|
|
|
named `{}`", name));
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.parse_sess.span_diagnostic.span_note(
|
|
|
|
prev.span, "previously here");
|
2013-10-01 16:31:03 -05:00
|
|
|
continue
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.names.insert(name, e);
|
|
|
|
} else {
|
|
|
|
self.args.push(p.parse_expr());
|
|
|
|
self.arg_types.push(None);
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 02:40:27 -05:00
|
|
|
return (extra, Some(fmtstr));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verifies one piece of a parse string. All errors are not emitted as
|
|
|
|
/// fatal so we can continue giving errors about this and possibly other
|
|
|
|
/// format strings.
|
|
|
|
fn verify_piece(&mut self, p: &parse::Piece) {
|
|
|
|
match *p {
|
2013-11-28 14:22:53 -06:00
|
|
|
parse::String(..) => {}
|
2013-07-29 03:12:41 -05:00
|
|
|
parse::CurrentArgument => {
|
|
|
|
if self.nest_level == 0 {
|
|
|
|
self.ecx.span_err(self.fmtsp,
|
|
|
|
"`#` reference used with nothing to \
|
|
|
|
reference back to");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parse::Argument(ref arg) => {
|
2013-08-10 02:28:47 -05:00
|
|
|
// width/precision first, if they have implicit positional
|
|
|
|
// parameters it makes more sense to consume them first.
|
|
|
|
self.verify_count(arg.format.width);
|
|
|
|
self.verify_count(arg.format.precision);
|
|
|
|
|
|
|
|
// argument second, if it's an implicit positional parameter
|
|
|
|
// it's written second, so it should come after width/precision.
|
2013-07-29 03:12:41 -05:00
|
|
|
let pos = match arg.position {
|
|
|
|
parse::ArgumentNext => {
|
|
|
|
let i = self.next_arg;
|
|
|
|
if self.check_positional_ok() {
|
|
|
|
self.next_arg += 1;
|
|
|
|
}
|
2013-12-25 23:55:05 -06:00
|
|
|
Exact(i)
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-12-25 23:55:05 -06:00
|
|
|
parse::ArgumentIs(i) => Exact(i),
|
|
|
|
parse::ArgumentNamed(s) => Named(s.to_managed()),
|
2013-07-29 03:12:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// and finally the method being applied
|
|
|
|
match arg.method {
|
2013-09-26 15:44:54 -05:00
|
|
|
None => {
|
|
|
|
let ty = Known(arg.format.ty.to_managed());
|
|
|
|
self.verify_arg_type(pos, ty);
|
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
Some(ref method) => { self.verify_method(pos, *method); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_pieces(&mut self, pieces: &[parse::Piece]) {
|
|
|
|
for piece in pieces.iter() {
|
|
|
|
self.verify_piece(piece);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_count(&mut self, c: parse::Count) {
|
|
|
|
match c {
|
2013-11-28 14:22:53 -06:00
|
|
|
parse::CountImplied | parse::CountIs(..) => {}
|
2013-07-29 03:12:41 -05:00
|
|
|
parse::CountIsParam(i) => {
|
2013-12-25 23:55:05 -06:00
|
|
|
self.verify_arg_type(Exact(i), Unsigned);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-10-12 22:00:58 -05:00
|
|
|
parse::CountIsName(s) => {
|
2013-12-25 23:55:05 -06:00
|
|
|
self.verify_arg_type(Named(s.to_managed()), Unsigned);
|
2013-10-12 22:00:58 -05:00
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
parse::CountIsNextParam => {
|
|
|
|
if self.check_positional_ok() {
|
2013-12-25 23:55:05 -06:00
|
|
|
self.verify_arg_type(Exact(self.next_arg), Unsigned);
|
2013-07-29 03:12:41 -05:00
|
|
|
self.next_arg += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_positional_ok(&mut self) -> bool {
|
|
|
|
if self.nest_level != 0 {
|
|
|
|
self.ecx.span_err(self.fmtsp, "cannot use implicit positional \
|
|
|
|
arguments nested inside methods");
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 23:55:05 -06:00
|
|
|
fn verify_method(&mut self, pos: Position, m: &parse::Method) {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.nest_level += 1;
|
|
|
|
match *m {
|
|
|
|
parse::Plural(_, ref arms, ref default) => {
|
|
|
|
let mut seen_cases = HashSet::new();
|
|
|
|
self.verify_arg_type(pos, Unsigned);
|
|
|
|
for arm in arms.iter() {
|
|
|
|
if !seen_cases.insert(arm.selector) {
|
|
|
|
match arm.selector {
|
2013-12-25 23:55:05 -06:00
|
|
|
parse::Keyword(name) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.span_err(self.fmtsp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("duplicate selector \
|
|
|
|
`{:?}`", name));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-12-25 23:55:05 -06:00
|
|
|
parse::Literal(idx) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.span_err(self.fmtsp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("duplicate selector \
|
|
|
|
`={}`", idx));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.verify_pieces(arm.result);
|
|
|
|
}
|
|
|
|
self.verify_pieces(*default);
|
|
|
|
}
|
|
|
|
parse::Select(ref arms, ref default) => {
|
|
|
|
self.verify_arg_type(pos, String);
|
|
|
|
let mut seen_cases = HashSet::new();
|
|
|
|
for arm in arms.iter() {
|
|
|
|
if !seen_cases.insert(arm.selector) {
|
|
|
|
self.ecx.span_err(self.fmtsp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("duplicate selector `{}`",
|
2013-07-29 03:12:41 -05:00
|
|
|
arm.selector));
|
|
|
|
} else if arm.selector == "" {
|
|
|
|
self.ecx.span_err(self.fmtsp,
|
|
|
|
"empty selector in `select`");
|
|
|
|
}
|
|
|
|
self.verify_pieces(arm.result);
|
|
|
|
}
|
|
|
|
self.verify_pieces(*default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.nest_level -= 1;
|
|
|
|
}
|
|
|
|
|
2013-12-25 23:55:05 -06:00
|
|
|
fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
|
2013-07-29 03:12:41 -05:00
|
|
|
match arg {
|
2013-12-25 23:55:05 -06:00
|
|
|
Exact(arg) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
if arg < 0 || self.args.len() <= arg {
|
2013-09-27 23:01:58 -05:00
|
|
|
let msg = format!("invalid reference to argument `{}` (there \
|
|
|
|
are {} arguments)", arg, self.args.len());
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.span_err(self.fmtsp, msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.verify_same(self.args[arg].span, ty, self.arg_types[arg]);
|
2013-09-26 15:44:54 -05:00
|
|
|
if self.arg_types[arg].is_none() {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.arg_types[arg] = Some(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 23:55:05 -06:00
|
|
|
Named(name) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
let span = match self.names.find(&name) {
|
|
|
|
Some(e) => e.span,
|
|
|
|
None => {
|
2013-09-27 23:01:58 -05:00
|
|
|
let msg = format!("there is no argument named `{}`", name);
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.span_err(self.fmtsp, msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.verify_same(span, ty,
|
2013-09-20 01:08:47 -05:00
|
|
|
self.name_types.find(&name).map(|&x| x));
|
2013-09-26 15:44:54 -05:00
|
|
|
if !self.name_types.contains_key(&name) {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.name_types.insert(name, ty);
|
|
|
|
}
|
|
|
|
// Assign this named argument a slot in the arguments array if
|
|
|
|
// it hasn't already been assigned a slot.
|
|
|
|
if !self.name_positions.contains_key(&name) {
|
|
|
|
let slot = self.name_positions.len();
|
|
|
|
self.name_positions.insert(name, slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// When we're keeping track of the types that are declared for certain
|
|
|
|
/// arguments, we assume that `None` means we haven't seen this argument
|
|
|
|
/// yet, `Some(None)` means that we've seen the argument, but no format was
|
|
|
|
/// specified, and `Some(Some(x))` means that the argument was declared to
|
|
|
|
/// have type `x`.
|
|
|
|
///
|
|
|
|
/// Obviously `Some(Some(x)) != Some(Some(y))`, but we consider it true
|
|
|
|
/// that: `Some(None) == Some(Some(x))`
|
2013-08-31 11:13:04 -05:00
|
|
|
fn verify_same(&self, sp: Span, ty: ArgumentType,
|
2013-07-29 03:12:41 -05:00
|
|
|
before: Option<ArgumentType>) {
|
|
|
|
let cur = match before {
|
2013-09-26 15:44:54 -05:00
|
|
|
None => return,
|
2013-07-29 03:12:41 -05:00
|
|
|
Some(t) => t,
|
|
|
|
};
|
|
|
|
if ty == cur { return }
|
|
|
|
match (cur, ty) {
|
|
|
|
(Known(cur), Known(ty)) => {
|
|
|
|
self.ecx.span_err(sp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("argument redeclared with type `{}` when \
|
|
|
|
it was previously `{}`", ty, cur));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
(Known(cur), _) => {
|
|
|
|
self.ecx.span_err(sp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("argument used to format with `{}` was \
|
|
|
|
attempted to not be used for formatting",
|
|
|
|
cur));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
(_, Known(ty)) => {
|
|
|
|
self.ecx.span_err(sp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("argument previously used as a format \
|
|
|
|
argument attempted to be used as `{}`",
|
|
|
|
ty));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
(_, _) => {
|
|
|
|
self.ecx.span_err(sp, "argument declared with multiple formats");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 13:01:23 -05:00
|
|
|
/// These attributes are applied to all statics that this syntax extension
|
|
|
|
/// will generate.
|
|
|
|
fn static_attrs(&self) -> ~[ast::Attribute] {
|
|
|
|
// Flag statics as `address_insignificant` so LLVM can merge duplicate
|
|
|
|
// globals as much as possible (which we're generating a whole lot of).
|
|
|
|
let unnamed = self.ecx.meta_word(self.fmtsp, @"address_insignificant");
|
|
|
|
let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
|
|
|
|
|
2013-12-08 01:55:27 -06:00
|
|
|
// Do not warn format string as dead code
|
|
|
|
let dead_code = self.ecx.meta_word(self.fmtsp, @"dead_code");
|
|
|
|
let allow_dead_code = self.ecx.meta_list(self.fmtsp,
|
|
|
|
@"allow", ~[dead_code]);
|
|
|
|
let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code);
|
|
|
|
return ~[unnamed, allow_dead_code];
|
2013-09-30 13:01:23 -05:00
|
|
|
}
|
|
|
|
|
2013-07-29 03:12:41 -05:00
|
|
|
/// Translate a `parse::Piece` to a static `rt::Piece`
|
2013-09-01 20:45:37 -05:00
|
|
|
fn trans_piece(&mut self, piece: &parse::Piece) -> @ast::Expr {
|
2013-07-29 03:12:41 -05:00
|
|
|
let sp = self.fmtsp;
|
2013-08-10 18:50:42 -05:00
|
|
|
let parsepath = |s: &str| {
|
|
|
|
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("parse"), self.ecx.ident_of(s)]
|
|
|
|
};
|
2013-07-29 03:12:41 -05:00
|
|
|
let rtpath = |s: &str| {
|
|
|
|
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("rt"), self.ecx.ident_of(s)]
|
|
|
|
};
|
|
|
|
let ctpath = |s: &str| {
|
|
|
|
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("parse"), self.ecx.ident_of(s)]
|
|
|
|
};
|
2013-08-28 04:22:45 -05:00
|
|
|
let none = self.ecx.path_global(sp, ~[
|
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("option"),
|
|
|
|
self.ecx.ident_of("None")]);
|
|
|
|
let none = self.ecx.expr_path(none);
|
2013-09-01 20:45:37 -05:00
|
|
|
let some = |e: @ast::Expr| {
|
2013-08-28 04:22:45 -05:00
|
|
|
let p = self.ecx.path_global(sp, ~[
|
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("option"),
|
|
|
|
self.ecx.ident_of("Some")]);
|
|
|
|
let p = self.ecx.expr_path(p);
|
|
|
|
self.ecx.expr_call(sp, p, ~[e])
|
2013-07-29 03:12:41 -05:00
|
|
|
};
|
|
|
|
let trans_count = |c: parse::Count| {
|
|
|
|
match c {
|
|
|
|
parse::CountIs(i) => {
|
2013-10-12 22:00:58 -05:00
|
|
|
self.ecx.expr_call_global(sp, rtpath("CountIs"),
|
2013-07-29 03:12:41 -05:00
|
|
|
~[self.ecx.expr_uint(sp, i)])
|
|
|
|
}
|
|
|
|
parse::CountIsParam(i) => {
|
2013-10-12 22:00:58 -05:00
|
|
|
self.ecx.expr_call_global(sp, rtpath("CountIsParam"),
|
2013-07-29 03:12:41 -05:00
|
|
|
~[self.ecx.expr_uint(sp, i)])
|
|
|
|
}
|
|
|
|
parse::CountImplied => {
|
2013-10-12 22:00:58 -05:00
|
|
|
let path = self.ecx.path_global(sp, rtpath("CountImplied"));
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.expr_path(path)
|
|
|
|
}
|
|
|
|
parse::CountIsNextParam => {
|
2013-10-12 22:00:58 -05:00
|
|
|
let path = self.ecx.path_global(sp, rtpath("CountIsNextParam"));
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.expr_path(path)
|
|
|
|
}
|
2013-10-12 22:00:58 -05:00
|
|
|
parse::CountIsName(n) => {
|
|
|
|
let n = n.to_managed();
|
|
|
|
let i = match self.name_positions.find_copy(&n) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 0, // error already emitted elsewhere
|
|
|
|
};
|
|
|
|
let i = i + self.args.len();
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("CountIsParam"),
|
|
|
|
~[self.ecx.expr_uint(sp, i)])
|
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let trans_method = |method: &parse::Method| {
|
|
|
|
let method = match *method {
|
|
|
|
parse::Select(ref arms, ref default) => {
|
2013-08-09 22:09:47 -05:00
|
|
|
let arms = arms.iter().map(|arm| {
|
2013-07-29 03:12:41 -05:00
|
|
|
let p = self.ecx.path_global(sp, rtpath("SelectArm"));
|
2013-08-09 22:09:47 -05:00
|
|
|
let result = arm.result.iter().map(|p| {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
|
|
|
let s = arm.selector.to_managed();
|
|
|
|
let selector = self.ecx.expr_str(sp, s);
|
|
|
|
self.ecx.expr_struct(sp, p, ~[
|
|
|
|
self.ecx.field_imm(sp,
|
|
|
|
self.ecx.ident_of("selector"),
|
|
|
|
selector),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
|
|
|
|
self.ecx.expr_vec_slice(sp, result)),
|
|
|
|
])
|
|
|
|
}).collect();
|
2013-08-09 22:09:47 -05:00
|
|
|
let default = default.iter().map(|p| {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("Select"), ~[
|
|
|
|
self.ecx.expr_vec_slice(sp, arms),
|
|
|
|
self.ecx.expr_vec_slice(sp, default),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
parse::Plural(offset, ref arms, ref default) => {
|
|
|
|
let offset = match offset {
|
|
|
|
Some(i) => { some(self.ecx.expr_uint(sp, i)) }
|
2013-08-28 04:22:45 -05:00
|
|
|
None => { none.clone() }
|
2013-07-29 03:12:41 -05:00
|
|
|
};
|
2013-08-09 22:09:47 -05:00
|
|
|
let arms = arms.iter().map(|arm| {
|
2013-07-29 03:12:41 -05:00
|
|
|
let p = self.ecx.path_global(sp, rtpath("PluralArm"));
|
2013-08-09 22:09:47 -05:00
|
|
|
let result = arm.result.iter().map(|p| {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
|
|
|
let (lr, selarg) = match arm.selector {
|
2013-12-25 23:55:05 -06:00
|
|
|
parse::Keyword(t) => {
|
2013-09-27 23:01:58 -05:00
|
|
|
let p = ctpath(format!("{:?}", t));
|
2013-07-29 03:12:41 -05:00
|
|
|
let p = self.ecx.path_global(sp, p);
|
2013-12-25 23:55:05 -06:00
|
|
|
(rtpath("Keyword"), self.ecx.expr_path(p))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-12-25 23:55:05 -06:00
|
|
|
parse::Literal(i) => {
|
|
|
|
(rtpath("Literal"), self.ecx.expr_uint(sp, i))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
2013-12-25 23:55:05 -06:00
|
|
|
let selector = self.ecx.expr_call_global(sp,
|
2013-07-29 03:12:41 -05:00
|
|
|
lr, ~[selarg]);
|
|
|
|
self.ecx.expr_struct(sp, p, ~[
|
|
|
|
self.ecx.field_imm(sp,
|
|
|
|
self.ecx.ident_of("selector"),
|
|
|
|
selector),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
|
|
|
|
self.ecx.expr_vec_slice(sp, result)),
|
|
|
|
])
|
|
|
|
}).collect();
|
2013-08-09 22:09:47 -05:00
|
|
|
let default = default.iter().map(|p| {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("Plural"), ~[
|
|
|
|
offset,
|
|
|
|
self.ecx.expr_vec_slice(sp, arms),
|
|
|
|
self.ecx.expr_vec_slice(sp, default),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let life = self.ecx.lifetime(sp, self.ecx.ident_of("static"));
|
|
|
|
let ty = self.ecx.ty_path(self.ecx.path_all(
|
|
|
|
sp,
|
|
|
|
true,
|
|
|
|
rtpath("Method"),
|
2013-10-29 05:03:32 -05:00
|
|
|
opt_vec::with(life),
|
2013-07-29 03:12:41 -05:00
|
|
|
~[]
|
|
|
|
), None);
|
2014-01-09 07:05:33 -06:00
|
|
|
let st = ast::ItemStatic(ty, ast::MutImmutable, method);
|
2013-09-30 13:01:23 -05:00
|
|
|
let static_name = self.ecx.ident_of(format!("__STATIC_METHOD_{}",
|
2013-07-29 03:12:41 -05:00
|
|
|
self.method_statics.len()));
|
2013-09-30 13:01:23 -05:00
|
|
|
let item = self.ecx.item(sp, static_name, self.static_attrs(), st);
|
2013-07-29 03:12:41 -05:00
|
|
|
self.method_statics.push(item);
|
|
|
|
self.ecx.expr_ident(sp, static_name)
|
|
|
|
};
|
|
|
|
|
|
|
|
match *piece {
|
|
|
|
parse::String(s) => {
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("String"),
|
|
|
|
~[self.ecx.expr_str(sp, s.to_managed())])
|
|
|
|
}
|
|
|
|
parse::CurrentArgument => {
|
2014-01-09 07:05:33 -06:00
|
|
|
let nil = self.ecx.expr_lit(sp, ast::LitNil);
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.expr_call_global(sp, rtpath("CurrentArgument"), ~[nil])
|
|
|
|
}
|
|
|
|
parse::Argument(ref arg) => {
|
|
|
|
// Translate the position
|
|
|
|
let pos = match arg.position {
|
|
|
|
// These two have a direct mapping
|
|
|
|
parse::ArgumentNext => {
|
|
|
|
let path = self.ecx.path_global(sp,
|
|
|
|
rtpath("ArgumentNext"));
|
|
|
|
self.ecx.expr_path(path)
|
|
|
|
}
|
|
|
|
parse::ArgumentIs(i) => {
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("ArgumentIs"),
|
|
|
|
~[self.ecx.expr_uint(sp, i)])
|
|
|
|
}
|
|
|
|
// Named arguments are converted to positional arguments at
|
|
|
|
// the end of the list of arguments
|
|
|
|
parse::ArgumentNamed(n) => {
|
|
|
|
let n = n.to_managed();
|
|
|
|
let i = match self.name_positions.find_copy(&n) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 0, // error already emitted elsewhere
|
|
|
|
};
|
|
|
|
let i = i + self.args.len();
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("ArgumentIs"),
|
|
|
|
~[self.ecx.expr_uint(sp, i)])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Translate the format
|
|
|
|
let fill = match arg.format.fill { Some(c) => c, None => ' ' };
|
2014-01-09 07:05:33 -06:00
|
|
|
let fill = self.ecx.expr_lit(sp, ast::LitChar(fill as u32));
|
2013-07-29 03:12:41 -05:00
|
|
|
let align = match arg.format.align {
|
2013-08-10 18:50:42 -05:00
|
|
|
parse::AlignLeft => {
|
|
|
|
self.ecx.path_global(sp, parsepath("AlignLeft"))
|
|
|
|
}
|
|
|
|
parse::AlignRight => {
|
|
|
|
self.ecx.path_global(sp, parsepath("AlignRight"))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-08-10 18:50:42 -05:00
|
|
|
parse::AlignUnknown => {
|
|
|
|
self.ecx.path_global(sp, parsepath("AlignUnknown"))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
2013-08-10 18:50:42 -05:00
|
|
|
let align = self.ecx.expr_path(align);
|
2013-07-29 03:12:41 -05:00
|
|
|
let flags = self.ecx.expr_uint(sp, arg.format.flags);
|
|
|
|
let prec = trans_count(arg.format.precision);
|
|
|
|
let width = trans_count(arg.format.width);
|
|
|
|
let path = self.ecx.path_global(sp, rtpath("FormatSpec"));
|
|
|
|
let fmt = self.ecx.expr_struct(sp, path, ~[
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("fill"), fill),
|
2013-08-10 18:50:42 -05:00
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("align"), align),
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("flags"), flags),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("precision"), prec),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("width"), width),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Translate the method (if any)
|
|
|
|
let method = match arg.method {
|
2013-08-28 04:22:45 -05:00
|
|
|
None => { none.clone() }
|
2013-07-29 03:12:41 -05:00
|
|
|
Some(ref m) => {
|
|
|
|
let m = trans_method(*m);
|
|
|
|
some(self.ecx.expr_addr_of(sp, m))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let path = self.ecx.path_global(sp, rtpath("Argument"));
|
|
|
|
let s = self.ecx.expr_struct(sp, path, ~[
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("method"), method),
|
|
|
|
]);
|
|
|
|
self.ecx.expr_call_global(sp, rtpath("Argument"), ~[s])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 23:01:58 -05:00
|
|
|
/// Actually builds the expression which the iformat! block will be expanded
|
2013-07-29 03:12:41 -05:00
|
|
|
/// to
|
2013-09-12 21:36:58 -05:00
|
|
|
fn to_expr(&self, extra: @ast::Expr) -> @ast::Expr {
|
2013-07-29 03:12:41 -05:00
|
|
|
let mut lets = ~[];
|
|
|
|
let mut locals = ~[];
|
|
|
|
let mut names = vec::from_fn(self.name_positions.len(), |_| None);
|
|
|
|
|
|
|
|
// First, declare all of our methods that are statics
|
|
|
|
for &method in self.method_statics.iter() {
|
2013-09-01 20:45:37 -05:00
|
|
|
let decl = respan(self.fmtsp, ast::DeclItem(method));
|
2013-07-29 03:12:41 -05:00
|
|
|
lets.push(@respan(self.fmtsp,
|
2013-09-06 21:11:55 -05:00
|
|
|
ast::StmtDecl(@decl, ast::DUMMY_NODE_ID)));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, build up the static array which will become our precompiled
|
|
|
|
// format "string"
|
|
|
|
let fmt = self.ecx.expr_vec(self.fmtsp, self.pieces.clone());
|
2013-08-28 04:22:45 -05:00
|
|
|
let piece_ty = self.ecx.ty_path(self.ecx.path_all(
|
|
|
|
self.fmtsp,
|
|
|
|
true, ~[
|
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("rt"),
|
|
|
|
self.ecx.ident_of("Piece"),
|
|
|
|
],
|
2013-10-29 05:03:32 -05:00
|
|
|
opt_vec::with(
|
|
|
|
self.ecx.lifetime(self.fmtsp, self.ecx.ident_of("static"))),
|
2013-08-28 04:22:45 -05:00
|
|
|
~[]
|
|
|
|
), None);
|
2014-01-09 07:05:33 -06:00
|
|
|
let ty = ast::TyFixedLengthVec(
|
2013-12-16 12:01:40 -06:00
|
|
|
piece_ty,
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.expr_uint(self.fmtsp, self.pieces.len())
|
|
|
|
);
|
|
|
|
let ty = self.ecx.ty(self.fmtsp, ty);
|
2014-01-09 07:05:33 -06:00
|
|
|
let st = ast::ItemStatic(ty, ast::MutImmutable, fmt);
|
2013-09-30 13:01:23 -05:00
|
|
|
let static_name = self.ecx.ident_of("__STATIC_FMTSTR");
|
|
|
|
let item = self.ecx.item(self.fmtsp, static_name,
|
|
|
|
self.static_attrs(), st);
|
2013-09-01 20:45:37 -05:00
|
|
|
let decl = respan(self.fmtsp, ast::DeclItem(item));
|
2013-09-06 21:11:55 -05:00
|
|
|
lets.push(@respan(self.fmtsp, ast::StmtDecl(@decl, ast::DUMMY_NODE_ID)));
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Right now there is a bug such that for the expression:
|
|
|
|
// foo(bar(&1))
|
|
|
|
// the lifetime of `1` doesn't outlast the call to `bar`, so it's not
|
|
|
|
// vald for the call to `foo`. To work around this all arguments to the
|
2013-09-27 23:01:58 -05:00
|
|
|
// format! string are shoved into locals. Furthermore, we shove the address
|
2013-09-03 01:53:13 -05:00
|
|
|
// of each variable because we don't want to move out of the arguments
|
|
|
|
// passed to this function.
|
2013-07-29 03:12:41 -05:00
|
|
|
for (i, &e) in self.args.iter().enumerate() {
|
2013-10-01 16:31:03 -05:00
|
|
|
if self.arg_types[i].is_none() { continue } // error already generated
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2013-09-27 23:01:58 -05:00
|
|
|
let name = self.ecx.ident_of(format!("__arg{}", i));
|
2013-09-03 01:53:13 -05:00
|
|
|
let e = self.ecx.expr_addr_of(e.span, e);
|
2013-07-29 03:12:41 -05:00
|
|
|
lets.push(self.ecx.stmt_let(e.span, false, name, e));
|
2013-12-25 23:55:05 -06:00
|
|
|
locals.push(self.format_arg(e.span, Exact(i),
|
2013-08-28 04:22:45 -05:00
|
|
|
self.ecx.expr_ident(e.span, name)));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
for (&name, &e) in self.names.iter() {
|
2013-10-01 16:31:03 -05:00
|
|
|
if !self.name_types.contains_key(&name) { continue }
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2013-09-27 23:01:58 -05:00
|
|
|
let lname = self.ecx.ident_of(format!("__arg{}", name));
|
2013-09-03 01:53:13 -05:00
|
|
|
let e = self.ecx.expr_addr_of(e.span, e);
|
2013-07-29 03:12:41 -05:00
|
|
|
lets.push(self.ecx.stmt_let(e.span, false, lname, e));
|
|
|
|
names[*self.name_positions.get(&name)] =
|
2013-12-25 23:55:05 -06:00
|
|
|
Some(self.format_arg(e.span, Named(name),
|
2013-08-28 04:22:45 -05:00
|
|
|
self.ecx.expr_ident(e.span, lname)));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// Now create a vector containing all the arguments
|
|
|
|
let slicename = self.ecx.ident_of("__args_vec");
|
|
|
|
{
|
|
|
|
let args = names.move_iter().map(|a| a.unwrap());
|
|
|
|
let mut args = locals.move_iter().chain(args);
|
|
|
|
let args = self.ecx.expr_vec_slice(self.fmtsp, args.collect());
|
|
|
|
lets.push(self.ecx.stmt_let(self.fmtsp, false, slicename, args));
|
|
|
|
}
|
|
|
|
|
2013-09-12 21:36:58 -05:00
|
|
|
// Now create the fmt::Arguments struct with all our locals we created.
|
|
|
|
let fmt = self.ecx.expr_ident(self.fmtsp, static_name);
|
2014-01-15 13:39:08 -06:00
|
|
|
let args_slice = self.ecx.expr_ident(self.fmtsp, slicename);
|
2013-09-12 21:36:58 -05:00
|
|
|
let result = self.ecx.expr_call_global(self.fmtsp, ~[
|
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("Arguments"),
|
|
|
|
self.ecx.ident_of("new"),
|
2014-01-15 13:39:08 -06:00
|
|
|
], ~[fmt, args_slice]);
|
2013-09-12 21:36:58 -05:00
|
|
|
|
|
|
|
// We did all the work of making sure that the arguments
|
|
|
|
// structure is safe, so we can safely have an unsafe block.
|
2013-11-30 16:00:39 -06:00
|
|
|
let result = self.ecx.expr_block(P(ast::Block {
|
2013-09-12 21:36:58 -05:00
|
|
|
view_items: ~[],
|
|
|
|
stmts: ~[],
|
|
|
|
expr: Some(result),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
rules: ast::UnsafeBlock(ast::CompilerGenerated),
|
|
|
|
span: self.fmtsp,
|
2013-11-30 16:00:39 -06:00
|
|
|
}));
|
2013-09-12 21:36:58 -05:00
|
|
|
let resname = self.ecx.ident_of("__args");
|
|
|
|
lets.push(self.ecx.stmt_let(self.fmtsp, false, resname, result));
|
|
|
|
let res = self.ecx.expr_ident(self.fmtsp, resname);
|
|
|
|
let result = self.ecx.expr_call(extra.span, extra, ~[
|
|
|
|
self.ecx.expr_addr_of(extra.span, res)]);
|
2013-08-28 04:22:45 -05:00
|
|
|
self.ecx.expr_block(self.ecx.block(self.fmtsp, lets,
|
|
|
|
Some(result)))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
2013-12-25 23:55:05 -06:00
|
|
|
fn format_arg(&self, sp: Span, argno: Position,
|
2013-08-28 04:22:45 -05:00
|
|
|
arg: @ast::Expr) -> @ast::Expr {
|
|
|
|
let ty = match argno {
|
2013-12-25 23:55:05 -06:00
|
|
|
Exact(i) => self.arg_types[i].unwrap(),
|
|
|
|
Named(s) => *self.name_types.get(&s)
|
2013-07-29 03:12:41 -05:00
|
|
|
};
|
2013-08-09 15:47:00 -05:00
|
|
|
|
2013-08-14 22:40:15 -05:00
|
|
|
let fmt_trait = match ty {
|
2013-07-29 03:12:41 -05:00
|
|
|
Known(tyname) => {
|
2013-08-14 22:40:15 -05:00
|
|
|
match tyname.as_slice() {
|
2013-09-26 15:44:54 -05:00
|
|
|
"" => "Default",
|
2013-07-29 03:12:41 -05:00
|
|
|
"?" => "Poly",
|
|
|
|
"b" => "Bool",
|
|
|
|
"c" => "Char",
|
2013-08-10 20:46:44 -05:00
|
|
|
"d" | "i" => "Signed",
|
|
|
|
"f" => "Float",
|
2013-07-29 03:12:41 -05:00
|
|
|
"o" => "Octal",
|
|
|
|
"p" => "Pointer",
|
2013-08-10 20:46:44 -05:00
|
|
|
"s" => "String",
|
2013-08-10 15:38:32 -05:00
|
|
|
"t" => "Binary",
|
2013-08-10 20:46:44 -05:00
|
|
|
"u" => "Unsigned",
|
|
|
|
"x" => "LowerHex",
|
|
|
|
"X" => "UpperHex",
|
2013-07-29 03:12:41 -05:00
|
|
|
_ => {
|
2013-09-27 23:01:58 -05:00
|
|
|
self.ecx.span_err(sp, format!("unknown format trait \
|
|
|
|
`{}`", tyname));
|
2013-07-29 03:12:41 -05:00
|
|
|
"Dummy"
|
|
|
|
}
|
2013-08-14 22:40:15 -05:00
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
String => {
|
2013-08-14 22:40:15 -05:00
|
|
|
return self.ecx.expr_call_global(sp, ~[
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("argumentstr"),
|
2013-08-28 04:22:45 -05:00
|
|
|
], ~[arg])
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
Unsigned => {
|
2013-08-14 22:40:15 -05:00
|
|
|
return self.ecx.expr_call_global(sp, ~[
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("argumentuint"),
|
2013-08-28 04:22:45 -05:00
|
|
|
], ~[arg])
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-08-14 22:40:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let format_fn = self.ecx.path_global(sp, ~[
|
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of(fmt_trait),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
]);
|
|
|
|
self.ecx.expr_call_global(sp, ~[
|
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("argument"),
|
2013-08-28 04:22:45 -05:00
|
|
|
], ~[self.ecx.expr_path(format_fn), arg])
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 23:06:22 -06:00
|
|
|
pub fn expand_args(ecx: &mut ExtCtxt, sp: Span,
|
2014-01-09 07:05:33 -06:00
|
|
|
tts: &[ast::TokenTree]) -> base::MacResult {
|
2013-07-29 03:12:41 -05:00
|
|
|
let mut cx = Context {
|
|
|
|
ecx: ecx,
|
|
|
|
args: ~[],
|
|
|
|
arg_types: ~[],
|
|
|
|
names: HashMap::new(),
|
|
|
|
name_positions: HashMap::new(),
|
|
|
|
name_types: HashMap::new(),
|
|
|
|
nest_level: 0,
|
|
|
|
next_arg: 0,
|
|
|
|
pieces: ~[],
|
|
|
|
method_statics: ~[],
|
|
|
|
fmtsp: sp,
|
|
|
|
};
|
2013-09-12 21:36:58 -05:00
|
|
|
let (extra, efmt) = match cx.parse_args(sp, tts) {
|
2013-08-20 02:40:27 -05:00
|
|
|
(extra, Some(e)) => (extra, e),
|
2013-12-28 23:06:22 -06:00
|
|
|
(_, None) => { return MRExpr(cx.ecx.expr_uint(sp, 2)); }
|
2013-07-29 03:12:41 -05:00
|
|
|
};
|
|
|
|
cx.fmtsp = efmt.span;
|
2013-10-05 23:15:46 -05:00
|
|
|
// Be sure to recursively expand macros just in case the format string uses
|
|
|
|
// a macro to build the format expression.
|
2013-12-28 23:06:22 -06:00
|
|
|
let expr = cx.ecx.expand_expr(efmt);
|
2014-01-17 08:53:10 -06:00
|
|
|
let fmt = match expr_to_str(cx.ecx, expr,
|
|
|
|
"format argument must be a string literal.") {
|
|
|
|
Some((fmt, _)) => fmt,
|
|
|
|
None => return MacResult::dummy_expr()
|
|
|
|
};
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
let mut err = false;
|
2013-11-20 18:23:04 -06:00
|
|
|
parse::parse_error::cond.trap(|m| {
|
2013-07-29 03:12:41 -05:00
|
|
|
if !err {
|
|
|
|
err = true;
|
2013-12-28 23:06:22 -06:00
|
|
|
cx.ecx.span_err(efmt.span, m);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
}).inside(|| {
|
2013-07-29 03:12:41 -05:00
|
|
|
for piece in parse::Parser::new(fmt) {
|
|
|
|
if !err {
|
|
|
|
cx.verify_piece(&piece);
|
|
|
|
let piece = cx.trans_piece(&piece);
|
|
|
|
cx.pieces.push(piece);
|
|
|
|
}
|
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
});
|
2013-07-29 03:12:41 -05:00
|
|
|
if err { return MRExpr(efmt) }
|
|
|
|
|
|
|
|
// Make sure that all arguments were used and all arguments have types.
|
|
|
|
for (i, ty) in cx.arg_types.iter().enumerate() {
|
|
|
|
if ty.is_none() {
|
2013-12-28 23:06:22 -06:00
|
|
|
cx.ecx.span_err(cx.args[i].span, "argument never used");
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (name, e) in cx.names.iter() {
|
|
|
|
if !cx.name_types.contains_key(name) {
|
2013-12-28 23:06:22 -06:00
|
|
|
cx.ecx.span_err(e.span, "named argument never used");
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 21:36:58 -05:00
|
|
|
MRExpr(cx.to_expr(extra))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|