Skip to content

Commit

Permalink
ty: trans: added support for dropping trivial casts
Browse files Browse the repository at this point in the history
  • Loading branch information
boggle committed Dec 2, 2011
1 parent e3b9bf5 commit f724512
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/comp/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -200,6 +202,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
Expand All @@ -208,6 +216,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,
Expand Down Expand Up @@ -395,6 +404,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,
Expand Down
10 changes: 7 additions & 3 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,15 +2103,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) {
Expand Down
9 changes: 8 additions & 1 deletion src/comp/syntax/ast_util.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import std::{str, option};
import std::{str, option, int, map};
import codemap::span;
import ast::*;

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};
Expand Down

0 comments on commit f724512

Please sign in to comment.