rustc: Parse and typecheck repeating vector expressions; e.g. [ 0, ..512 ]
This commit is contained in:
parent
4174de8bb9
commit
e02b1b1ec8
11 changed files with 142 additions and 40 deletions
|
|
@ -350,7 +350,10 @@ enum expr_ {
|
|||
// A struct literal expression.
|
||||
//
|
||||
// XXX: Add functional record update.
|
||||
expr_struct(@path, ~[field])
|
||||
expr_struct(@path, ~[field]),
|
||||
|
||||
// A vector literal constructed from one repeated element.
|
||||
expr_repeat(@expr /* element */, @expr /* count */, mutability)
|
||||
}
|
||||
|
||||
#[auto_serialize]
|
||||
|
|
|
|||
|
|
@ -402,6 +402,8 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
|
|||
expr_vec(exprs, mutt) {
|
||||
expr_vec(fld.map_exprs(|x| fld.fold_expr(x), exprs), mutt)
|
||||
}
|
||||
expr_repeat(expr, count, mutt) =>
|
||||
expr_repeat(fld.fold_expr(expr), fld.fold_expr(count), mutt),
|
||||
expr_rec(fields, maybe_expr) {
|
||||
expr_rec(vec::map(fields, fold_field),
|
||||
option::map(maybe_expr, |x| fld.fold_expr(x)))
|
||||
|
|
|
|||
|
|
@ -483,6 +483,10 @@ fn next_token_inner(rdr: string_reader) -> token::token {
|
|||
',' { bump(rdr); return token::COMMA; }
|
||||
'.' {
|
||||
bump(rdr);
|
||||
if rdr.curr == '.' && nextch(rdr) != '.' {
|
||||
bump(rdr);
|
||||
return token::DOTDOT;
|
||||
}
|
||||
if rdr.curr == '.' && nextch(rdr) == '.' {
|
||||
bump(rdr);
|
||||
bump(rdr);
|
||||
|
|
|
|||
|
|
@ -27,33 +27,34 @@ import ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute,
|
|||
expr_call, expr_cast, expr_copy, expr_do_body,
|
||||
expr_fail, expr_field, expr_fn, expr_fn_block, expr_if,
|
||||
expr_index, expr_lit, expr_log, expr_loop,
|
||||
expr_loop_body, expr_mac, expr_move, expr_path,
|
||||
expr_rec, expr_ret, expr_swap, expr_struct, expr_tup, expr_unary,
|
||||
expr_unary_move, expr_vec, expr_vstore, expr_while, extern_fn,
|
||||
field, fn_decl, foreign_item, foreign_item_fn, foreign_mod,
|
||||
ident, impure_fn, infer, inherited, init_assign, init_move,
|
||||
initializer, instance_var, item, item_, item_class, item_const,
|
||||
item_enum, item_fn, item_foreign_mod, item_impl, item_mac,
|
||||
item_mod, item_trait, item_ty, lit, lit_, lit_bool, lit_float,
|
||||
lit_int, lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local,
|
||||
m_const, m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_invoc,
|
||||
mac_invoc_tt, mac_var, matcher, match_nonterminal, match_seq,
|
||||
match_tok, method, mode, mt, mul, mutability, neg, noreturn, not,
|
||||
pat, pat_box, pat_enum, pat_ident, pat_lit, pat_range, pat_rec,
|
||||
pat_tup, pat_uniq, pat_wild, path, private, proto, proto_any,
|
||||
proto_bare, proto_block, proto_box, proto_uniq, provided, public,
|
||||
pure_fn, purity, re_anon, re_named, region, rem, required,
|
||||
ret_style, return_val, self_ty, shl, shr, stmt, stmt_decl,
|
||||
stmt_expr, stmt_semi, subtract, sty_box, sty_by_ref, sty_region,
|
||||
sty_uniq, sty_value, token_tree, trait_method, trait_ref,
|
||||
tt_delim, tt_seq, tt_tok, tt_nonterminal, ty, ty_, ty_bot,
|
||||
ty_box, ty_field, ty_fn, ty_infer, ty_mac, ty_method, ty_nil,
|
||||
ty_param, ty_path, ty_ptr, ty_rec, ty_rptr, ty_tup, ty_u32,
|
||||
ty_uniq, ty_vec, ty_fixed_length, unchecked_blk, uniq,
|
||||
unsafe_blk, unsafe_fn, variant, view_item, view_item_,
|
||||
view_item_export, view_item_import, view_item_use, view_path,
|
||||
view_path_glob, view_path_list, view_path_simple, visibility,
|
||||
vstore, vstore_box, vstore_fixed, vstore_slice, vstore_uniq};
|
||||
expr_loop_body, expr_mac, expr_move, expr_path, expr_rec,
|
||||
expr_repeat, expr_ret, expr_swap, expr_struct, expr_tup,
|
||||
expr_unary, expr_unary_move, expr_vec, expr_vstore, expr_while,
|
||||
extern_fn, field, fn_decl, foreign_item, foreign_item_fn,
|
||||
foreign_mod, ident, impure_fn, infer, inherited, init_assign,
|
||||
init_move, initializer, instance_var, item, item_, item_class,
|
||||
item_const, item_enum, item_fn, item_foreign_mod, item_impl,
|
||||
item_mac, item_mod, item_trait, item_ty, lit, lit_, lit_bool,
|
||||
lit_float, lit_int, lit_int_unsuffixed, lit_nil, lit_str,
|
||||
lit_uint, local, m_const, m_imm, m_mutbl, mac_, mac_aq,
|
||||
mac_ellipsis, mac_invoc, mac_invoc_tt, mac_var, matcher,
|
||||
match_nonterminal, match_seq, match_tok, method, mode, mt, mul,
|
||||
mutability, neg, noreturn, not, pat, pat_box, pat_enum,
|
||||
pat_ident, pat_lit, pat_range, pat_rec, pat_tup, pat_uniq,
|
||||
pat_wild, path, private, proto, proto_any, proto_bare,
|
||||
proto_block, proto_box, proto_uniq, provided, public, pure_fn,
|
||||
purity, re_anon, re_named, region, rem, required, ret_style,
|
||||
return_val, self_ty, shl, shr, stmt, stmt_decl, stmt_expr,
|
||||
stmt_semi, subtract, sty_box, sty_by_ref, sty_region, sty_uniq,
|
||||
sty_value, token_tree, trait_method, trait_ref, tt_delim, tt_seq,
|
||||
tt_tok, tt_nonterminal, ty, ty_, ty_bot, ty_box, ty_field, ty_fn,
|
||||
ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_path, ty_ptr,
|
||||
ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec,
|
||||
ty_fixed_length, unchecked_blk, uniq, unsafe_blk, unsafe_fn,
|
||||
variant, view_item, view_item_, view_item_export,
|
||||
view_item_import, view_item_use, view_path, view_path_glob,
|
||||
view_path_list, view_path_simple, visibility, vstore, vstore_box,
|
||||
vstore_fixed, vstore_slice, vstore_uniq};
|
||||
|
||||
export file_type;
|
||||
export parser;
|
||||
|
|
@ -365,6 +366,7 @@ class parser {
|
|||
// Parses something like "&x"
|
||||
fn parse_region() -> @region {
|
||||
self.expect(token::BINOP(token::AND));
|
||||
|
||||
alt copy self.token {
|
||||
token::IDENT(sid, _) {
|
||||
self.bump();
|
||||
|
|
@ -812,11 +814,36 @@ class parser {
|
|||
} else if self.token == token::LBRACKET {
|
||||
self.bump();
|
||||
let mutbl = self.parse_mutability();
|
||||
let es = self.parse_seq_to_end(
|
||||
token::RBRACKET, seq_sep_trailing_allowed(token::COMMA),
|
||||
|p| p.parse_expr());
|
||||
if self.token == token::RBRACKET {
|
||||
// Empty vector.
|
||||
self.bump();
|
||||
ex = expr_vec(~[], mutbl);
|
||||
} else {
|
||||
// Nonempty vector.
|
||||
let first_expr = self.parse_expr();
|
||||
if self.token == token::COMMA &&
|
||||
self.look_ahead(1) == token::DOTDOT {
|
||||
// Repeating vector syntax: [ 0, ..512 ]
|
||||
self.bump();
|
||||
self.bump();
|
||||
let count = self.parse_expr();
|
||||
self.expect(token::RBRACKET);
|
||||
ex = expr_repeat(first_expr, count, mutbl);
|
||||
} else if self.token == token::COMMA {
|
||||
// Vector with two or more elements.
|
||||
self.bump();
|
||||
let remaining_exprs =
|
||||
self.parse_seq_to_end(token::RBRACKET,
|
||||
seq_sep_trailing_allowed(token::COMMA),
|
||||
|p| p.parse_expr());
|
||||
ex = expr_vec(~[first_expr] + remaining_exprs, mutbl);
|
||||
} else {
|
||||
// Vector with one element.
|
||||
self.expect(token::RBRACKET);
|
||||
ex = expr_vec(~[first_expr], mutbl);
|
||||
}
|
||||
}
|
||||
hi = self.span.hi;
|
||||
ex = expr_vec(es, mutbl);
|
||||
} else if self.token == token::ELLIPSIS {
|
||||
self.bump();
|
||||
return pexpr(self.mk_mac_expr(lo, self.span.hi, mac_ellipsis));
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ enum token {
|
|||
/* Structural symbols */
|
||||
AT,
|
||||
DOT,
|
||||
DOTDOT,
|
||||
ELLIPSIS,
|
||||
COMMA,
|
||||
SEMI,
|
||||
|
|
@ -134,6 +135,7 @@ fn to_str(in: interner<@~str>, t: token) -> ~str {
|
|||
/* Structural symbols */
|
||||
AT { ~"@" }
|
||||
DOT { ~"." }
|
||||
DOTDOT { ~".." }
|
||||
ELLIPSIS { ~"..." }
|
||||
COMMA { ~"," }
|
||||
SEMI { ~";" }
|
||||
|
|
|
|||
|
|
@ -948,6 +948,22 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
|
|||
word(s.s, ~"]");
|
||||
end(s);
|
||||
}
|
||||
|
||||
ast::expr_repeat(element, count, mutbl) => {
|
||||
ibox(s, indent_unit);
|
||||
word(s.s, ~"[");
|
||||
if mutbl == ast::m_mutbl {
|
||||
word(s.s, ~"mut");
|
||||
nbsp(s);
|
||||
}
|
||||
print_expr(s, element);
|
||||
word(s.s, ~",");
|
||||
word(s.s, ~"..");
|
||||
print_expr(s, count);
|
||||
word(s.s, ~"]");
|
||||
end(s);
|
||||
}
|
||||
|
||||
ast::expr_rec(fields, wth) {
|
||||
word(s.s, ~"{");
|
||||
commasep_cmnt(s, consistent, fields, print_field, get_span);
|
||||
|
|
|
|||
|
|
@ -364,6 +364,10 @@ fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
|
|||
alt ex.node {
|
||||
expr_vstore(x, _) { v.visit_expr(x, e, v); }
|
||||
expr_vec(es, _) { visit_exprs(es, e, v); }
|
||||
expr_repeat(element, count, _) => {
|
||||
v.visit_expr(element, e, v);
|
||||
v.visit_expr(count, e, v);
|
||||
}
|
||||
expr_rec(flds, base) {
|
||||
for flds.each |f| { v.visit_expr(f.node.expr, e, v); }
|
||||
visit_expr_opt(base, e, v);
|
||||
|
|
|
|||
|
|
@ -182,7 +182,7 @@ impl public_methods for borrowck_ctxt {
|
|||
ast::expr_block(*) | ast::expr_loop(*) | ast::expr_alt(*) |
|
||||
ast::expr_lit(*) | ast::expr_break | ast::expr_mac(*) |
|
||||
ast::expr_again | ast::expr_rec(*) | ast::expr_struct(*) |
|
||||
ast::expr_unary_move(*) {
|
||||
ast::expr_unary_move(*) | ast::expr_repeat(*) {
|
||||
return self.cat_rvalue(expr, expr_ty);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -471,7 +471,8 @@ fn visit_expr(expr: @expr, &&self: @ir_maps, vt: vt<@ir_maps>) {
|
|||
expr_unary(*) | expr_fail(*) |
|
||||
expr_break | expr_again | expr_lit(_) | expr_ret(*) |
|
||||
expr_block(*) | expr_move(*) | expr_unary_move(*) | expr_assign(*) |
|
||||
expr_swap(*) | expr_assign_op(*) | expr_mac(*) | expr_struct(*) => {
|
||||
expr_swap(*) | expr_assign_op(*) | expr_mac(*) | expr_struct(*) |
|
||||
expr_repeat(*) => {
|
||||
visit::visit_expr(expr, self, vt);
|
||||
}
|
||||
}
|
||||
|
|
@ -1057,6 +1058,11 @@ class liveness {
|
|||
self.propagate_through_exprs(exprs, succ)
|
||||
}
|
||||
|
||||
expr_repeat(element, count, _) => {
|
||||
let succ = self.propagate_through_expr(count, succ);
|
||||
self.propagate_through_expr(element, succ)
|
||||
}
|
||||
|
||||
expr_rec(fields, with_expr) {
|
||||
let succ = self.propagate_through_opt_expr(with_expr, succ);
|
||||
do fields.foldr(succ) |field, succ| {
|
||||
|
|
@ -1468,7 +1474,7 @@ fn check_expr(expr: @expr, &&self: @liveness, vt: vt<@liveness>) {
|
|||
expr_cast(*) | expr_unary(*) | expr_fail(*) |
|
||||
expr_ret(*) | expr_break | expr_again | expr_lit(_) |
|
||||
expr_block(*) | expr_swap(*) | expr_mac(*) | expr_addr_of(*) |
|
||||
expr_struct(*) {
|
||||
expr_struct(*) | expr_repeat(*) {
|
||||
visit::visit_expr(expr, self, vt);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -158,7 +158,8 @@ fn mark_for_expr(cx: ctx, e: @expr) {
|
|||
expr_rec(_, _) | expr_struct(*) | expr_tup(_) |
|
||||
expr_unary(box(_), _) | expr_unary(uniq(_), _) |
|
||||
expr_binary(add, _, _) |
|
||||
expr_copy(_) | expr_move(_, _) | expr_unary_move(_) {
|
||||
expr_copy(_) | expr_move(_, _) | expr_unary_move(_) |
|
||||
expr_repeat(*) => {
|
||||
node_type_needs(cx, use_repr, e.id);
|
||||
}
|
||||
expr_cast(base, _) {
|
||||
|
|
|
|||
|
|
@ -1150,6 +1150,27 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
is_loop_body, some(fcx));
|
||||
}
|
||||
|
||||
fn eval_repeat_count(fcx: @fn_ctxt, count_expr: @ast::expr, span: span)
|
||||
-> uint {
|
||||
let tcx = fcx.ccx.tcx;
|
||||
match const_eval::eval_const_expr(tcx, count_expr) {
|
||||
const_eval::const_int(count) => return count as uint,
|
||||
const_eval::const_uint(count) => return count as uint,
|
||||
const_eval::const_float(count) => {
|
||||
tcx.sess.span_err(span,
|
||||
~"expected signed or unsigned integer for \
|
||||
repeat count but found float");
|
||||
return count as uint;
|
||||
}
|
||||
const_eval::const_str(_) => {
|
||||
tcx.sess.span_err(span,
|
||||
~"expected signed or unsigned integer for \
|
||||
repeat count but found string");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check field access expressions
|
||||
fn check_field(fcx: @fn_ctxt, expr: @ast::expr, is_callee: bool,
|
||||
base: @ast::expr, field: ast::ident, tys: ~[@ast::ty])
|
||||
|
|
@ -1252,19 +1273,26 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
alt expr.node {
|
||||
ast::expr_vstore(ev, vst) {
|
||||
let typ = alt ev.node {
|
||||
ast::expr_lit(@{node: ast::lit_str(s), span:_}) {
|
||||
ast::expr_lit(@{node: ast::lit_str(s), span:_}) => {
|
||||
let tt = ast_expr_vstore_to_vstore(fcx, ev, str::len(*s), vst);
|
||||
ty::mk_estr(tcx, tt)
|
||||
}
|
||||
ast::expr_vec(args, mutbl) {
|
||||
ast::expr_vec(args, mutbl) => {
|
||||
let tt = ast_expr_vstore_to_vstore(fcx, ev, vec::len(args), vst);
|
||||
let t: ty::t = fcx.infcx.next_ty_var();
|
||||
for args.each |e| { bot |= check_expr_with(fcx, e, t); }
|
||||
ty::mk_evec(tcx, {ty: t, mutbl: mutbl}, tt)
|
||||
}
|
||||
_ {
|
||||
tcx.sess.span_bug(expr.span, ~"vstore modifier on non-sequence")
|
||||
ast::expr_repeat(element, count_expr, mutbl) => {
|
||||
let count = eval_repeat_count(fcx, count_expr, expr.span);
|
||||
fcx.write_ty(count_expr.id, ty::mk_uint(tcx));
|
||||
let tt = ast_expr_vstore_to_vstore(fcx, ev, count, vst);
|
||||
let t: ty::t = fcx.infcx.next_ty_var();
|
||||
bot |= check_expr_with(fcx, element, t);
|
||||
ty::mk_evec(tcx, {ty: t, mutbl: mutbl}, tt)
|
||||
}
|
||||
_ =>
|
||||
tcx.sess.span_bug(expr.span, ~"vstore modifier on non-sequence")
|
||||
};
|
||||
fcx.write_ty(ev.id, typ);
|
||||
fcx.write_ty(id, typ);
|
||||
|
|
@ -1613,6 +1641,15 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
ty::vstore_fixed(args.len()));
|
||||
fcx.write_ty(id, typ);
|
||||
}
|
||||
ast::expr_repeat(element, count_expr, mutbl) {
|
||||
let count = eval_repeat_count(fcx, count_expr, expr.span);
|
||||
fcx.write_ty(count_expr.id, ty::mk_uint(tcx));
|
||||
let t: ty::t = fcx.infcx.next_ty_var();
|
||||
bot |= check_expr_with(fcx, element, t);
|
||||
let t = ty::mk_evec(tcx, {ty: t, mutbl: mutbl},
|
||||
ty::vstore_fixed(count));
|
||||
fcx.write_ty(id, t);
|
||||
}
|
||||
ast::expr_tup(elts) {
|
||||
let mut elt_ts = ~[];
|
||||
vec::reserve(elt_ts, vec::len(elts));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue