ty: trans: added support for dropping trivial casts

This commit is contained in:
Stefan Plantikow 2011-11-30 17:58:08 +01:00 committed by Graydon Hoare
parent d116a6f2d3
commit 29f7cdffa4
4 changed files with 33 additions and 5 deletions

View file

@ -4086,7 +4086,14 @@ fn trans_expr(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
if !ty::expr_is_lval(tcx, a) { ret trans_expr(bcx, a, dest); }
else { ret lval_to_dps(bcx, a, dest); }
}
ast::expr_cast(val, _) { ret trans_cast(bcx, val, e.id, dest); }
ast::expr_cast(val, _) {
alt tcx.cast_map.find(e.id) {
option::none. { ret trans_cast(bcx, val, e.id, dest); }
some { alt option::get(some) {
ty::triv_cast. { ret trans_expr(bcx, val, dest); }
} }
}
}
ast::expr_anon_obj(anon_obj) {
ret trans_anon_obj(bcx, e.span, anon_obj, e.id, dest);
}

View file

@ -32,6 +32,7 @@ export ast_constr_to_constr;
export bind_params_in_type;
export block_ty;
export constr;
export cast_type;
export constr_general;
export constr_table;
export count_ty_params;
@ -102,6 +103,7 @@ export substitute_type_params;
export t;
export tag_variants;
export tag_variant_with_id;
export triv_cast;
export triv_eq_ty;
export ty_param_substs_opt_and_ty;
export ty_param_kinds_and_ty;
@ -201,6 +203,12 @@ type mt = {ty: t, mut: ast::mutability};
// the types of AST nodes.
type creader_cache = hashmap<{cnum: int, pos: uint, len: uint}, ty::t>;
tag cast_type {
/* cast may be ignored after substituting primitive with machine types
since expr already has the right type */
triv_cast;
}
type ctxt =
// constr_table fn_constrs,
// We need the ext_map just for printing the types of tags defined in
@ -209,6 +217,7 @@ type ctxt =
sess: session::session,
def_map: resolve::def_map,
ext_map: resolve::ext_map,
cast_map: hashmap<ast::node_id, cast_type>,
node_types: node_type_table,
items: ast_map::map,
freevars: freevars::freevar_map,
@ -396,6 +405,7 @@ fn mk_ctxt(s: session::session, dm: resolve::def_map,
sess: s,
def_map: dm,
ext_map: em,
cast_map: ast_util::new_node_hash(),
node_types: ntt,
items: amap,
freevars: freevars,

View file

@ -2101,15 +2101,19 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
ast::expr_cast(e, t) {
bot = check_expr(fcx, e);
let t_1 = ast_ty_to_ty_crate(fcx.ccx, t);
// FIXME: there are more forms of cast to support, eventually.
let t_e = expr_ty(tcx, e);
if !(type_is_scalar(fcx, expr.span, expr_ty(tcx, e)) &&
type_is_scalar(fcx, expr.span, t_1)) {
// FIXME there are more forms of cast to support, eventually.
if !( type_is_scalar(fcx, expr.span, t_e)
&& type_is_scalar(fcx, expr.span, t_1)) {
tcx.sess.span_err(expr.span,
"non-scalar cast: " +
ty_to_str(tcx, expr_ty(tcx, e)) + " as " +
ty_to_str(tcx, t_1));
}
if ty::triv_eq_ty(tcx, t_1, t_e)
{ tcx.cast_map.insert(expr.id, ty::triv_cast); }
write::ty_only_fixup(fcx, id, t_1);
}
ast::expr_vec(args, mut) {

View file

@ -1,4 +1,4 @@
import std::{str, option};
import std::{str, option, int, map};
import codemap::span;
import ast::*;
@ -6,6 +6,13 @@ fn respan<copy T>(sp: span, t: T) -> spanned<T> {
ret {node: t, span: sp};
}
fn new_node_hash<copy V>() -> map::hashmap<node_id, V> {
fn node_id_hash(&&i: node_id) -> uint { ret int::hash(i as int); }
fn node_id_eq(&&a: node_id, &&b: node_id) -> bool
{ ret int::eq(a as int, b as int); }
ret map::mk_hashmap(node_id_hash, node_id_eq);
}
/* assuming that we're not in macro expansion */
fn mk_sp(lo: uint, hi: uint) -> span {
ret {lo: lo, hi: hi, expanded_from: codemap::os_none};