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;
|
2014-01-08 12:35:15 -06:00
|
|
|
use parse::token::InternedString;
|
|
|
|
use parse::token;
|
|
|
|
use rsparse = parse;
|
2014-02-19 21:29:58 -06:00
|
|
|
|
2014-05-06 11:52:53 -05:00
|
|
|
use parse = fmt_macros;
|
2014-02-19 21:29:58 -06:00
|
|
|
use collections::{HashMap, HashSet};
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
#[deriving(Eq)]
|
|
|
|
enum ArgumentType {
|
2014-05-07 18:33:43 -05:00
|
|
|
Known(StrBuf),
|
2013-07-29 03:12:41 -05:00
|
|
|
Unsigned,
|
|
|
|
String,
|
|
|
|
}
|
|
|
|
|
2013-12-25 23:55:05 -06:00
|
|
|
enum Position {
|
|
|
|
Exact(uint),
|
2014-05-07 18:33:43 -05:00
|
|
|
Named(StrBuf),
|
2013-12-25 23:55:05 -06:00
|
|
|
}
|
|
|
|
|
2014-03-09 09:54:34 -05:00
|
|
|
struct Context<'a, 'b> {
|
|
|
|
ecx: &'a mut ExtCtxt<'b>,
|
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.
|
2014-02-28 15:09:09 -06:00
|
|
|
args: Vec<@ast::Expr>,
|
|
|
|
arg_types: Vec<Option<ArgumentType>>,
|
2014-02-27 19:07:27 -06:00
|
|
|
// Parsed named expressions and the types that we've found for them so far.
|
|
|
|
// Note that we keep a side-array of the ordering of the named arguments
|
|
|
|
// found to be sure that we can translate them in the same order that they
|
|
|
|
// were declared in.
|
2014-05-07 18:33:43 -05:00
|
|
|
names: HashMap<StrBuf, @ast::Expr>,
|
|
|
|
name_types: HashMap<StrBuf, ArgumentType>,
|
|
|
|
name_ordering: Vec<StrBuf>,
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Collection of the compiled `rt::Piece` structures
|
2014-02-28 15:09:09 -06:00
|
|
|
pieces: Vec<@ast::Expr> ,
|
2014-05-07 18:33:43 -05:00
|
|
|
name_positions: HashMap<StrBuf, uint>,
|
2014-02-28 15:09:09 -06:00
|
|
|
method_statics: Vec<@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,
|
|
|
|
}
|
|
|
|
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
pub enum Invocation {
|
|
|
|
Call(@ast::Expr),
|
|
|
|
MethodCall(@ast::Expr, ast::Ident),
|
|
|
|
}
|
|
|
|
|
2014-02-05 15:50:36 -06:00
|
|
|
/// Parses the arguments from the given list of tokens, returning None
|
|
|
|
/// if there's a parse error so we can continue parsing other format!
|
|
|
|
/// expressions.
|
|
|
|
///
|
|
|
|
/// If parsing succeeds, the second return value is:
|
|
|
|
///
|
2014-02-27 19:07:27 -06:00
|
|
|
/// Some((fmtstr, unnamed arguments, ordering of named arguments,
|
|
|
|
/// named arguments))
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool,
|
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> (Invocation, Option<(@ast::Expr, Vec<@ast::Expr>, Vec<StrBuf>,
|
2014-05-07 18:33:43 -05:00
|
|
|
HashMap<StrBuf, @ast::Expr>)>) {
|
2014-02-28 15:09:09 -06:00
|
|
|
let mut args = Vec::new();
|
2014-05-07 18:33:43 -05:00
|
|
|
let mut names = HashMap::<StrBuf, @ast::Expr>::new();
|
2014-02-28 14:54:01 -06:00
|
|
|
let mut order = Vec::new();
|
2014-02-05 15:50:36 -06:00
|
|
|
|
|
|
|
let mut p = rsparse::new_parser_from_tts(ecx.parse_sess(),
|
|
|
|
ecx.cfg(),
|
2014-02-28 14:54:01 -06:00
|
|
|
tts.iter()
|
|
|
|
.map(|x| (*x).clone())
|
|
|
|
.collect());
|
2014-02-05 15:50:36 -06:00
|
|
|
// Parse the leading function expression (maybe a block, maybe a path)
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
let invocation = if allow_method {
|
|
|
|
let e = p.parse_expr();
|
|
|
|
if !p.eat(&token::COMMA) {
|
|
|
|
ecx.span_err(sp, "expected token: `,`");
|
|
|
|
return (Call(e), None);
|
|
|
|
}
|
|
|
|
MethodCall(e, p.parse_ident())
|
|
|
|
} else {
|
|
|
|
Call(p.parse_expr())
|
|
|
|
};
|
2014-02-05 15:50:36 -06:00
|
|
|
if !p.eat(&token::COMMA) {
|
|
|
|
ecx.span_err(sp, "expected token: `,`");
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
return (invocation, None);
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
2013-08-20 02:40:27 -05:00
|
|
|
|
2014-02-05 15:50:36 -06:00
|
|
|
if p.token == token::EOF {
|
|
|
|
ecx.span_err(sp, "requires at least a format string argument");
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
return (invocation, None);
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
|
|
|
let fmtstr = p.parse_expr();
|
|
|
|
let mut named = false;
|
|
|
|
while p.token != token::EOF {
|
|
|
|
if !p.eat(&token::COMMA) {
|
|
|
|
ecx.span_err(sp, "expected token: `,`");
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
return (invocation, None);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2014-02-05 15:50:36 -06:00
|
|
|
if p.token == token::EOF { break } // accept trailing commas
|
|
|
|
if named || (token::is_ident(&p.token) &&
|
|
|
|
p.look_ahead(1, |t| *t == token::EQ)) {
|
|
|
|
named = true;
|
|
|
|
let ident = match p.token {
|
|
|
|
token::IDENT(i, _) => {
|
|
|
|
p.bump();
|
|
|
|
i
|
|
|
|
}
|
|
|
|
_ if named => {
|
|
|
|
ecx.span_err(p.span,
|
|
|
|
"expected ident, positional arguments \
|
|
|
|
cannot follow named arguments");
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
return (invocation, None);
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
ecx.span_err(p.span,
|
|
|
|
format!("expected ident for named argument, but found `{}`",
|
|
|
|
p.this_token_to_str()));
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
return (invocation, None);
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
|
|
|
};
|
2014-02-13 23:07:09 -06:00
|
|
|
let interned_name = token::get_ident(ident);
|
2014-02-05 15:50:36 -06:00
|
|
|
let name = interned_name.get();
|
|
|
|
p.expect(&token::EQ);
|
|
|
|
let e = p.parse_expr();
|
|
|
|
match names.find_equiv(&name) {
|
|
|
|
None => {}
|
|
|
|
Some(prev) => {
|
|
|
|
ecx.span_err(e.span, format!("duplicate argument named `{}`", name));
|
|
|
|
ecx.parse_sess.span_diagnostic.span_note(prev.span, "previously here");
|
|
|
|
continue
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-07 18:33:43 -05:00
|
|
|
order.push(name.to_strbuf());
|
|
|
|
names.insert(name.to_strbuf(), e);
|
2014-02-05 15:50:36 -06:00
|
|
|
} else {
|
|
|
|
args.push(p.parse_expr());
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
return (invocation, Some((fmtstr, args, order, names)));
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2014-03-09 09:54:34 -05:00
|
|
|
impl<'a, 'b> Context<'a, 'b> {
|
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),
|
2014-05-07 18:33:43 -05:00
|
|
|
parse::ArgumentNamed(s) => Named(s.to_strbuf()),
|
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 => {
|
2014-05-07 18:33:43 -05:00
|
|
|
let ty = Known(arg.format.ty.to_strbuf());
|
2013-09-26 15:44:54 -05:00
|
|
|
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) => {
|
2014-05-07 18:33:43 -05:00
|
|
|
self.verify_arg_type(Named(s.to_strbuf()), 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 \
|
2014-05-06 11:52:53 -05:00
|
|
|
`{}`", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 20:46:49 -05:00
|
|
|
self.verify_pieces(arm.result.as_slice());
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2014-04-08 20:46:49 -05:00
|
|
|
self.verify_pieces(default.as_slice());
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
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`");
|
|
|
|
}
|
2014-04-08 20:46:49 -05:00
|
|
|
self.verify_pieces(arm.result.as_slice());
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2014-04-08 20:46:49 -05:00
|
|
|
self.verify_pieces(default.as_slice());
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) => {
|
2014-05-04 13:48:16 -05:00
|
|
|
if 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;
|
|
|
|
}
|
2014-01-21 12:08:10 -06:00
|
|
|
{
|
2014-02-28 14:54:01 -06:00
|
|
|
let arg_type = match self.arg_types.get(arg) {
|
|
|
|
&None => None,
|
|
|
|
&Some(ref x) => Some(x)
|
2014-01-21 12:08:10 -06:00
|
|
|
};
|
2014-02-28 14:54:01 -06:00
|
|
|
self.verify_same(self.args.get(arg).span, &ty, arg_type);
|
2014-01-21 12:08:10 -06:00
|
|
|
}
|
2014-02-28 14:54:01 -06:00
|
|
|
if self.arg_types.get(arg).is_none() {
|
|
|
|
*self.arg_types.get_mut(arg) = Some(ty);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2014-01-21 12:08:10 -06:00
|
|
|
self.verify_same(span, &ty, self.name_types.find(&name));
|
2013-09-26 15:44:54 -05:00
|
|
|
if !self.name_types.contains_key(&name) {
|
2014-01-21 12:08:10 -06:00
|
|
|
self.name_types.insert(name.clone(), ty);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
// 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))`
|
2014-01-21 12:08:10 -06:00
|
|
|
fn verify_same(&self,
|
|
|
|
sp: Span,
|
|
|
|
ty: &ArgumentType,
|
|
|
|
before: Option<&ArgumentType>) {
|
2013-07-29 03:12:41 -05:00
|
|
|
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,
|
|
|
|
};
|
2014-01-21 12:08:10 -06:00
|
|
|
if *ty == *cur {
|
|
|
|
return
|
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
match (cur, ty) {
|
2014-01-21 12:08:10 -06:00
|
|
|
(&Known(ref cur), &Known(ref ty)) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.span_err(sp,
|
2013-09-27 23:01:58 -05:00
|
|
|
format!("argument redeclared with type `{}` when \
|
2014-01-21 12:08:10 -06:00
|
|
|
it was previously `{}`",
|
|
|
|
*ty,
|
|
|
|
*cur));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2014-01-21 12:08:10 -06:00
|
|
|
(&Known(ref cur), _) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
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",
|
2014-01-21 12:08:10 -06:00
|
|
|
*cur));
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2014-01-21 12:08:10 -06:00
|
|
|
(_, &Known(ref ty)) => {
|
2013-07-29 03:12:41 -05:00
|
|
|
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 `{}`",
|
2014-01-21 12:08:10 -06:00
|
|
|
*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.
|
2014-02-28 15:09:09 -06:00
|
|
|
fn static_attrs(&self) -> Vec<ast::Attribute> {
|
2013-09-30 13:01:23 -05:00
|
|
|
// Flag statics as `address_insignificant` so LLVM can merge duplicate
|
|
|
|
// globals as much as possible (which we're generating a whole lot of).
|
2014-01-08 12:35:15 -06:00
|
|
|
let unnamed = self.ecx
|
|
|
|
.meta_word(self.fmtsp,
|
|
|
|
InternedString::new(
|
|
|
|
"address_insignificant"));
|
2013-09-30 13:01:23 -05:00
|
|
|
let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
|
|
|
|
|
2013-12-08 01:55:27 -06:00
|
|
|
// Do not warn format string as dead code
|
2014-01-08 12:35:15 -06:00
|
|
|
let dead_code = self.ecx.meta_word(self.fmtsp,
|
|
|
|
InternedString::new("dead_code"));
|
2013-12-08 01:55:27 -06:00
|
|
|
let allow_dead_code = self.ecx.meta_list(self.fmtsp,
|
2014-01-08 12:35:15 -06:00
|
|
|
InternedString::new("allow"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(dead_code));
|
2013-12-08 01:55:27 -06:00
|
|
|
let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code);
|
2014-02-28 15:09:09 -06:00
|
|
|
return vec!(unnamed, allow_dead_code);
|
2013-09-30 13:01:23 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 15:09:09 -06:00
|
|
|
fn rtpath(&self, s: &str) -> Vec<ast::Ident> {
|
|
|
|
vec!(self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("rt"), self.ecx.ident_of(s))
|
2014-02-07 13:45:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn none(&self) -> @ast::Expr {
|
2014-02-28 15:09:09 -06:00
|
|
|
let none = self.ecx.path_global(self.fmtsp, vec!(
|
2013-08-28 04:22:45 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("option"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("None")));
|
2014-02-07 13:45:46 -06:00
|
|
|
self.ecx.expr_path(none)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn some(&self, e: @ast::Expr) -> @ast::Expr {
|
2014-02-28 15:09:09 -06:00
|
|
|
let p = self.ecx.path_global(self.fmtsp, vec!(
|
2013-08-28 04:22:45 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("option"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("Some")));
|
2014-02-07 13:45:46 -06:00
|
|
|
let p = self.ecx.expr_path(p);
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_call(self.fmtsp, p, vec!(e))
|
2014-02-07 13:45:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_count(&self, c: parse::Count) -> @ast::Expr {
|
|
|
|
let sp = self.fmtsp;
|
|
|
|
match c {
|
|
|
|
parse::CountIs(i) => {
|
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("CountIs"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(self.ecx.expr_uint(sp, i)))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2014-02-07 13:45:46 -06:00
|
|
|
parse::CountIsParam(i) => {
|
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(self.ecx.expr_uint(sp, i)))
|
2014-02-07 13:45:46 -06:00
|
|
|
}
|
|
|
|
parse::CountImplied => {
|
|
|
|
let path = self.ecx.path_global(sp, self.rtpath("CountImplied"));
|
|
|
|
self.ecx.expr_path(path)
|
|
|
|
}
|
|
|
|
parse::CountIsNextParam => {
|
|
|
|
let path = self.ecx.path_global(sp, self.rtpath("CountIsNextParam"));
|
|
|
|
self.ecx.expr_path(path)
|
|
|
|
}
|
|
|
|
parse::CountIsName(n) => {
|
|
|
|
let i = match self.name_positions.find_equiv(&n) {
|
|
|
|
Some(&i) => i,
|
|
|
|
None => 0, // error already emitted elsewhere
|
|
|
|
};
|
|
|
|
let i = i + self.args.len();
|
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(self.ecx.expr_uint(sp, i)))
|
2014-02-07 13:45:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_method(&mut self, method: &parse::Method) -> @ast::Expr {
|
|
|
|
let sp = self.fmtsp;
|
|
|
|
let method = match *method {
|
|
|
|
parse::Select(ref arms, ref default) => {
|
|
|
|
let arms = arms.iter().map(|arm| {
|
|
|
|
let p = self.ecx.path_global(sp, self.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();
|
2014-01-10 16:02:36 -06:00
|
|
|
let s = token::intern_and_get_ident(arm.selector);
|
2013-07-29 03:12:41 -05:00
|
|
|
let selector = self.ecx.expr_str(sp, s);
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_struct(sp, p, vec!(
|
2014-02-07 13:45:46 -06:00
|
|
|
self.ecx.field_imm(sp,
|
|
|
|
self.ecx.ident_of("selector"),
|
|
|
|
selector),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_vec_slice(sp, result))))
|
2013-07-29 03:12:41 -05:00
|
|
|
}).collect();
|
2014-02-07 13:45:46 -06:00
|
|
|
let default = default.iter().map(|p| {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("Select"), vec!(
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.expr_vec_slice(sp, arms),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_vec_slice(sp, default)))
|
2014-02-07 13:45:46 -06:00
|
|
|
}
|
|
|
|
parse::Plural(offset, ref arms, ref default) => {
|
|
|
|
let offset = match offset {
|
|
|
|
Some(i) => { self.some(self.ecx.expr_uint(sp, i)) }
|
|
|
|
None => { self.none() }
|
|
|
|
};
|
|
|
|
let arms = arms.iter().map(|arm| {
|
|
|
|
let p = self.ecx.path_global(sp, self.rtpath("PluralArm"));
|
2013-08-09 22:09:47 -05:00
|
|
|
let result = arm.result.iter().map(|p| {
|
2014-02-07 13:45:46 -06:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
2013-07-29 03:12:41 -05:00
|
|
|
let (lr, selarg) = match arm.selector {
|
2013-12-25 23:55:05 -06:00
|
|
|
parse::Keyword(t) => {
|
2014-05-06 11:52:53 -05:00
|
|
|
let p = self.rtpath(t.to_str());
|
2013-07-29 03:12:41 -05:00
|
|
|
let p = self.ecx.path_global(sp, p);
|
2014-02-07 13:45:46 -06:00
|
|
|
(self.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) => {
|
2014-02-07 13:45:46 -06:00
|
|
|
(self.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,
|
2014-02-28 15:09:09 -06:00
|
|
|
lr, vec!(selarg));
|
|
|
|
self.ecx.expr_struct(sp, p, vec!(
|
2014-02-07 13:45:46 -06:00
|
|
|
self.ecx.field_imm(sp,
|
|
|
|
self.ecx.ident_of("selector"),
|
|
|
|
selector),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_vec_slice(sp, result))))
|
2013-07-29 03:12:41 -05:00
|
|
|
}).collect();
|
2014-02-07 13:45:46 -06:00
|
|
|
let default = default.iter().map(|p| {
|
2013-07-29 03:12:41 -05:00
|
|
|
self.trans_piece(p)
|
|
|
|
}).collect();
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("Plural"), vec!(
|
2013-07-29 03:12:41 -05:00
|
|
|
offset,
|
|
|
|
self.ecx.expr_vec_slice(sp, arms),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_vec_slice(sp, default)))
|
2014-02-07 13:45:46 -06:00
|
|
|
}
|
|
|
|
};
|
2014-02-21 14:04:03 -06:00
|
|
|
let life = self.ecx.lifetime(sp, self.ecx.ident_of("static").name);
|
2014-02-07 13:45:46 -06:00
|
|
|
let ty = self.ecx.ty_path(self.ecx.path_all(
|
2013-07-29 03:12:41 -05:00
|
|
|
sp,
|
|
|
|
true,
|
2014-02-07 13:45:46 -06:00
|
|
|
self.rtpath("Method"),
|
2014-03-07 09:50:40 -06:00
|
|
|
vec!(life),
|
2014-02-28 15:09:09 -06:00
|
|
|
Vec::new()
|
2014-02-07 13:45:46 -06:00
|
|
|
), None);
|
|
|
|
let st = ast::ItemStatic(ty, ast::MutImmutable, method);
|
|
|
|
let static_name = self.ecx.ident_of(format!("__STATIC_METHOD_{}",
|
|
|
|
self.method_statics.len()));
|
|
|
|
let item = self.ecx.item(sp, static_name, self.static_attrs(), st);
|
|
|
|
self.method_statics.push(item);
|
|
|
|
self.ecx.expr_ident(sp, static_name)
|
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2014-02-07 13:45:46 -06:00
|
|
|
/// Translate a `parse::Piece` to a static `rt::Piece`
|
|
|
|
fn trans_piece(&mut self, piece: &parse::Piece) -> @ast::Expr {
|
|
|
|
let sp = self.fmtsp;
|
2013-07-29 03:12:41 -05:00
|
|
|
match *piece {
|
|
|
|
parse::String(s) => {
|
2014-01-10 16:02:36 -06:00
|
|
|
let s = token::intern_and_get_ident(s);
|
|
|
|
self.ecx.expr_call_global(sp,
|
2014-02-07 13:45:46 -06:00
|
|
|
self.rtpath("String"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(
|
2014-01-10 16:02:36 -06:00
|
|
|
self.ecx.expr_str(sp, s)
|
2014-02-28 15:09:09 -06:00
|
|
|
))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
parse::CurrentArgument => {
|
2014-01-09 07:05:33 -06:00
|
|
|
let nil = self.ecx.expr_lit(sp, ast::LitNil);
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("CurrentArgument"), vec!(nil))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
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,
|
2014-02-07 13:45:46 -06:00
|
|
|
self.rtpath("ArgumentNext"));
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.expr_path(path)
|
|
|
|
}
|
|
|
|
parse::ArgumentIs(i) => {
|
2014-02-07 13:45:46 -06:00
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(self.ecx.expr_uint(sp, i)))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
// Named arguments are converted to positional arguments at
|
|
|
|
// the end of the list of arguments
|
|
|
|
parse::ArgumentNamed(n) => {
|
2014-01-21 12:08:10 -06:00
|
|
|
let i = match self.name_positions.find_equiv(&n) {
|
|
|
|
Some(&i) => i,
|
2013-07-29 03:12:41 -05:00
|
|
|
None => 0, // error already emitted elsewhere
|
|
|
|
};
|
|
|
|
let i = i + self.args.len();
|
2014-02-07 13:45:46 -06:00
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(self.ecx.expr_uint(sp, i)))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Translate the format
|
|
|
|
let fill = match arg.format.fill { Some(c) => c, None => ' ' };
|
2014-05-01 08:35:06 -05:00
|
|
|
let fill = self.ecx.expr_lit(sp, ast::LitChar(fill));
|
2013-07-29 03:12:41 -05:00
|
|
|
let align = match arg.format.align {
|
2013-08-10 18:50:42 -05:00
|
|
|
parse::AlignLeft => {
|
2014-05-06 11:52:53 -05:00
|
|
|
self.ecx.path_global(sp, self.rtpath("AlignLeft"))
|
2013-08-10 18:50:42 -05:00
|
|
|
}
|
|
|
|
parse::AlignRight => {
|
2014-05-06 11:52:53 -05:00
|
|
|
self.ecx.path_global(sp, self.rtpath("AlignRight"))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-08-10 18:50:42 -05:00
|
|
|
parse::AlignUnknown => {
|
2014-05-06 11:52:53 -05:00
|
|
|
self.ecx.path_global(sp, self.rtpath("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);
|
2014-02-07 13:45:46 -06:00
|
|
|
let prec = self.trans_count(arg.format.precision);
|
|
|
|
let width = self.trans_count(arg.format.width);
|
|
|
|
let path = self.ecx.path_global(sp, self.rtpath("FormatSpec"));
|
2014-02-28 15:09:09 -06:00
|
|
|
let fmt = self.ecx.expr_struct(sp, path, vec!(
|
2013-07-29 03:12:41 -05:00
|
|
|
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),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("width"), width)));
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Translate the method (if any)
|
|
|
|
let method = match arg.method {
|
2014-02-07 13:45:46 -06:00
|
|
|
None => { self.none() }
|
2013-07-29 03:12:41 -05:00
|
|
|
Some(ref m) => {
|
2014-02-07 13:45:46 -06:00
|
|
|
let m = self.trans_method(*m);
|
|
|
|
self.some(self.ecx.expr_addr_of(sp, m))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
2014-02-07 13:45:46 -06:00
|
|
|
let path = self.ecx.path_global(sp, self.rtpath("Argument"));
|
2014-02-28 15:09:09 -06:00
|
|
|
let s = self.ecx.expr_struct(sp, path, vec!(
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos),
|
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.field_imm(sp, self.ecx.ident_of("method"), method)));
|
|
|
|
self.ecx.expr_call_global(sp, self.rtpath("Argument"), vec!(s))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
fn to_expr(&self, invocation: Invocation) -> @ast::Expr {
|
2014-02-28 15:09:09 -06:00
|
|
|
let mut lets = Vec::new();
|
|
|
|
let mut locals = Vec::new();
|
2014-04-17 17:59:07 -05:00
|
|
|
let mut names = Vec::from_fn(self.name_positions.len(), |_| None);
|
2014-02-28 15:09:09 -06:00
|
|
|
let mut pats = Vec::new();
|
|
|
|
let mut heads = Vec::new();
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// 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,
|
2014-02-28 15:09:09 -06:00
|
|
|
true, vec!(
|
2013-08-28 04:22:45 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("rt"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("Piece")),
|
2014-03-07 09:50:40 -06:00
|
|
|
vec!(self.ecx.lifetime(self.fmtsp,
|
|
|
|
self.ecx.ident_of("static").name)),
|
2014-02-28 15:09:09 -06:00
|
|
|
Vec::new()
|
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() {
|
2014-02-28 14:54:01 -06:00
|
|
|
if self.arg_types.get(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));
|
2014-02-17 13:32:12 -06:00
|
|
|
pats.push(self.ecx.pat_ident(e.span, name));
|
|
|
|
heads.push(self.ecx.expr_addr_of(e.span, 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
|
|
|
}
|
2014-02-27 19:07:27 -06:00
|
|
|
for name in self.name_ordering.iter() {
|
|
|
|
let e = match self.names.find(name) {
|
|
|
|
Some(&e) if self.name_types.contains_key(name) => e,
|
|
|
|
Some(..) | None => continue
|
|
|
|
};
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2014-01-21 12:08:10 -06:00
|
|
|
let lname = self.ecx.ident_of(format!("__arg{}", *name));
|
2014-02-17 13:32:12 -06:00
|
|
|
pats.push(self.ecx.pat_ident(e.span, lname));
|
|
|
|
heads.push(self.ecx.expr_addr_of(e.span, e));
|
2014-04-17 17:59:07 -05:00
|
|
|
*names.get_mut(*self.name_positions.get(name)) =
|
2014-01-21 12:08:10 -06:00
|
|
|
Some(self.format_arg(e.span,
|
|
|
|
Named((*name).clone()),
|
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);
|
2014-02-28 15:09:09 -06:00
|
|
|
let result = self.ecx.expr_call_global(self.fmtsp, vec!(
|
2013-09-12 21:36:58 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
|
|
|
self.ecx.ident_of("Arguments"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("new")), vec!(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 {
|
2014-02-28 15:09:09 -06:00
|
|
|
view_items: Vec::new(),
|
|
|
|
stmts: Vec::new(),
|
2013-09-12 21:36:58 -05:00
|
|
|
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);
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
let result = match invocation {
|
|
|
|
Call(e) => {
|
|
|
|
self.ecx.expr_call(e.span, e,
|
|
|
|
vec!(self.ecx.expr_addr_of(e.span, res)))
|
|
|
|
}
|
|
|
|
MethodCall(e, m) => {
|
|
|
|
self.ecx.expr_method_call(e.span, e, m,
|
|
|
|
vec!(self.ecx.expr_addr_of(e.span, res)))
|
|
|
|
}
|
|
|
|
};
|
2014-02-17 13:32:12 -06:00
|
|
|
let body = self.ecx.expr_block(self.ecx.block(self.fmtsp, lets,
|
|
|
|
Some(result)));
|
|
|
|
|
|
|
|
// Constructs an AST equivalent to:
|
|
|
|
//
|
|
|
|
// match (&arg0, &arg1) {
|
|
|
|
// (tmp0, tmp1) => body
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// It was:
|
|
|
|
//
|
|
|
|
// let tmp0 = &arg0;
|
|
|
|
// let tmp1 = &arg1;
|
|
|
|
// body
|
|
|
|
//
|
|
|
|
// Because of #11585 the new temporary lifetime rule, the enclosing
|
|
|
|
// statements for these temporaries become the let's themselves.
|
|
|
|
// If one or more of them are RefCell's, RefCell borrow() will also
|
|
|
|
// end there; they don't last long enough for body to use them. The
|
|
|
|
// match expression solves the scope problem.
|
|
|
|
//
|
|
|
|
// Note, it may also very well be transformed to:
|
|
|
|
//
|
|
|
|
// match arg0 {
|
|
|
|
// ref tmp0 => {
|
|
|
|
// match arg1 => {
|
|
|
|
// ref tmp1 => body } } }
|
|
|
|
//
|
|
|
|
// But the nested match expression is proved to perform not as well
|
|
|
|
// as series of let's; the first approach does.
|
|
|
|
let pat = self.ecx.pat(self.fmtsp, ast::PatTup(pats));
|
2014-02-28 15:09:09 -06:00
|
|
|
let arm = self.ecx.arm(self.fmtsp, vec!(pat), body);
|
2014-02-17 13:32:12 -06:00
|
|
|
let head = self.ecx.expr(self.fmtsp, ast::ExprTup(heads));
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.expr_match(self.fmtsp, head, vec!(arm))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
2014-01-21 12:08:10 -06:00
|
|
|
fn format_arg(&self, sp: Span, argno: Position, arg: @ast::Expr)
|
|
|
|
-> @ast::Expr {
|
2013-08-28 04:22:45 -05:00
|
|
|
let ty = match argno {
|
2014-02-28 14:54:01 -06:00
|
|
|
Exact(ref i) => self.arg_types.get(*i).get_ref(),
|
2014-01-21 12:08:10 -06:00
|
|
|
Named(ref s) => self.name_types.get(s)
|
2013-07-29 03:12:41 -05:00
|
|
|
};
|
2013-08-09 15:47:00 -05:00
|
|
|
|
2014-01-31 04:32:39 -06:00
|
|
|
let fmt_fn = match *ty {
|
2014-01-21 12:08:10 -06:00
|
|
|
Known(ref tyname) => {
|
2014-01-29 07:46:37 -06:00
|
|
|
match tyname.as_slice() {
|
|
|
|
"" => "secret_show",
|
|
|
|
"?" => "secret_poly",
|
|
|
|
"b" => "secret_bool",
|
|
|
|
"c" => "secret_char",
|
|
|
|
"d" | "i" => "secret_signed",
|
|
|
|
"e" => "secret_lower_exp",
|
|
|
|
"E" => "secret_upper_exp",
|
|
|
|
"f" => "secret_float",
|
|
|
|
"o" => "secret_octal",
|
|
|
|
"p" => "secret_pointer",
|
|
|
|
"s" => "secret_string",
|
|
|
|
"t" => "secret_binary",
|
|
|
|
"u" => "secret_unsigned",
|
|
|
|
"x" => "secret_lower_hex",
|
|
|
|
"X" => "secret_upper_hex",
|
2013-07-29 03:12:41 -05:00
|
|
|
_ => {
|
2014-01-31 04:32:39 -06:00
|
|
|
self.ecx.span_err(sp, format!("unknown format trait `{}`",
|
|
|
|
*tyname));
|
|
|
|
"dummy"
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-08-14 22:40:15 -05:00
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
String => {
|
2014-02-28 15:09:09 -06:00
|
|
|
return self.ecx.expr_call_global(sp, vec!(
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("argumentstr")), vec!(arg))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
Unsigned => {
|
2014-02-28 15:09:09 -06:00
|
|
|
return self.ecx.expr_call_global(sp, vec!(
|
2013-07-29 03:12:41 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("argumentuint")), vec!(arg))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
2013-08-14 22:40:15 -05:00
|
|
|
};
|
|
|
|
|
2014-02-28 15:09:09 -06:00
|
|
|
let format_fn = self.ecx.path_global(sp, vec!(
|
2013-08-14 22:40:15 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of(fmt_fn)));
|
|
|
|
self.ecx.expr_call_global(sp, vec!(
|
2013-08-14 22:40:15 -05:00
|
|
|
self.ecx.ident_of("std"),
|
|
|
|
self.ecx.ident_of("fmt"),
|
2014-02-28 15:09:09 -06:00
|
|
|
self.ecx.ident_of("argument")), vec!(self.ecx.expr_path(format_fn), arg))
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
pub fn expand_format_args(ecx: &mut ExtCtxt, sp: Span,
|
|
|
|
tts: &[ast::TokenTree]) -> Box<base::MacResult> {
|
|
|
|
|
|
|
|
match parse_args(ecx, sp, false, tts) {
|
|
|
|
(invocation, Some((efmt, args, order, names))) => {
|
|
|
|
MacExpr::new(expand_preparsed_format_args(ecx, sp, invocation, efmt,
|
|
|
|
args, order, names))
|
|
|
|
}
|
|
|
|
(_, None) => MacExpr::new(ecx.expr_uint(sp, 2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_format_args_method(ecx: &mut ExtCtxt, sp: Span,
|
|
|
|
tts: &[ast::TokenTree]) -> Box<base::MacResult> {
|
2014-02-05 15:50:36 -06:00
|
|
|
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
match parse_args(ecx, sp, true, tts) {
|
|
|
|
(invocation, Some((efmt, args, order, names))) => {
|
|
|
|
MacExpr::new(expand_preparsed_format_args(ecx, sp, invocation, efmt,
|
|
|
|
args, order, names))
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
2014-04-15 07:00:14 -05:00
|
|
|
(_, None) => MacExpr::new(ecx.expr_uint(sp, 2))
|
2014-02-05 15:50:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Take the various parts of `format_args!(extra, efmt, args...,
|
|
|
|
/// name=names...)` and construct the appropriate formatting
|
|
|
|
/// expression.
|
|
|
|
pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
invocation: Invocation,
|
2014-02-28 15:09:09 -06:00
|
|
|
efmt: @ast::Expr, args: Vec<@ast::Expr>,
|
2014-05-07 18:33:43 -05:00
|
|
|
name_ordering: Vec<StrBuf>,
|
|
|
|
names: HashMap<StrBuf, @ast::Expr>) -> @ast::Expr {
|
2014-02-28 14:54:01 -06:00
|
|
|
let arg_types = Vec::from_fn(args.len(), |_| None);
|
2013-07-29 03:12:41 -05:00
|
|
|
let mut cx = Context {
|
|
|
|
ecx: ecx,
|
2014-02-05 15:50:36 -06:00
|
|
|
args: args,
|
|
|
|
arg_types: arg_types,
|
|
|
|
names: names,
|
2013-07-29 03:12:41 -05:00
|
|
|
name_positions: HashMap::new(),
|
|
|
|
name_types: HashMap::new(),
|
2014-02-27 19:07:27 -06:00
|
|
|
name_ordering: name_ordering,
|
2013-07-29 03:12:41 -05:00
|
|
|
nest_level: 0,
|
|
|
|
next_arg: 0,
|
2014-02-28 15:09:09 -06:00
|
|
|
pieces: Vec::new(),
|
|
|
|
method_statics: Vec::new(),
|
2013-07-29 03:12:41 -05:00
|
|
|
fmtsp: sp,
|
|
|
|
};
|
|
|
|
cx.fmtsp = efmt.span;
|
2014-01-10 16:02:36 -06:00
|
|
|
let fmt = match expr_to_str(cx.ecx,
|
2014-03-02 15:38:44 -06:00
|
|
|
efmt,
|
2014-01-10 16:02:36 -06:00
|
|
|
"format argument must be a string literal.") {
|
2014-01-17 08:53:10 -06:00
|
|
|
Some((fmt, _)) => fmt,
|
2014-04-15 07:00:14 -05:00
|
|
|
None => return DummyResult::raw_expr(sp)
|
2014-01-17 08:53:10 -06:00
|
|
|
};
|
2013-07-29 03:12:41 -05:00
|
|
|
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 21:02:10 -06:00
|
|
|
let mut parser = parse::Parser::new(fmt.get());
|
|
|
|
loop {
|
|
|
|
match parser.next() {
|
|
|
|
Some(piece) => {
|
|
|
|
if parser.errors.len() > 0 { break }
|
2013-07-29 03:12:41 -05:00
|
|
|
cx.verify_piece(&piece);
|
|
|
|
let piece = cx.trans_piece(&piece);
|
|
|
|
cx.pieces.push(piece);
|
|
|
|
}
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 21:02:10 -06:00
|
|
|
None => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match parser.errors.shift() {
|
|
|
|
Some(error) => {
|
|
|
|
cx.ecx.span_err(efmt.span, "invalid format string: " + error);
|
2014-04-15 07:00:14 -05:00
|
|
|
return DummyResult::raw_expr(sp);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 21:02:10 -06:00
|
|
|
None => {}
|
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// 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() {
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.ecx.span_err(cx.args.get(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
syntax: Add a macro, format_args_method!()
Currently, the format_args!() macro takes as its first argument an expression
which is the callee of an ExprCall. This means that if format_args!() is used
with calling a method a closure must be used. Consider this code, however:
format_args!(|args| { foo.writer.write_fmt(args) }, "{}", foo.field)
The closure borrows the entire `foo` structure, disallowing the later borrow of
`foo.field`. To preserve the semantics of the `write!` macro, it is also
impossible to borrow specifically the `writer` field of the `foo` structure
because it must be borrowed mutably, but the `foo` structure is not guaranteed
to be mutable itself.
This new macro is invoked like:
format_args_method!(foo.writer, write_fmt, "{}", foo.field)
This macro will generate an ExprMethodCall which allows the borrow checker to
understand that `writer` and `field` should be borrowed separately.
This macro is not strictly necessary, with DST or possibly UFCS other
workarounds could be used. For now, though, it looks like this is required to
implement the `write!` macro.
2014-05-10 15:53:40 -05:00
|
|
|
cx.to_expr(invocation)
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|