Skip to content

Commit bedea18

Browse files
committed
---
yaml --- r: 951 b: refs/heads/master c: e2f9f74 h: refs/heads/master i: 949: ec8a2bc 947: 220e00a 943: 79b22de v: v3
1 parent d8b9668 commit bedea18

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: c1916adc7e16bd7ecd3ca8dbbe985ec75d0c825a
2+
refs/heads/master: e2f9f746ea5adcede229b5c9c14450a0057ed13b

trunk/src/comp/middle/trans.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,37 @@ impure fn trans_exprs(@block_ctxt cx, &vec[@ast.expr] es)
935935
ret tup(bcx, vs);
936936
}
937937

938+
impure fn trans_cast(@block_ctxt cx, &ast.expr e, &ast.ann ann) -> result {
939+
auto e_res = trans_expr(cx, e);
940+
auto llsrctype = val_ty(e_res.val);
941+
auto t = node_ann_type(cx.fcx.ccx, ann);
942+
auto lldsttype = type_of(cx.fcx.ccx, t);
943+
if (!typeck.type_is_fp(t)) {
944+
if (llvm.LLVMGetIntTypeWidth(lldsttype) >
945+
llvm.LLVMGetIntTypeWidth(llsrctype)) {
946+
if (typeck.type_is_signed(t)) {
947+
// Widening signed cast.
948+
e_res.val =
949+
e_res.bcx.build.SExtOrBitCast(e_res.val,
950+
lldsttype);
951+
} else {
952+
// Widening unsigned cast.
953+
e_res.val =
954+
e_res.bcx.build.ZExtOrBitCast(e_res.val,
955+
lldsttype);
956+
}
957+
} else {
958+
// Narrowing cast.
959+
e_res.val =
960+
e_res.bcx.build.TruncOrBitCast(e_res.val,
961+
lldsttype);
962+
}
963+
} else {
964+
cx.fcx.ccx.sess.unimpl("fp cast");
965+
}
966+
ret e_res;
967+
}
968+
938969
impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
939970
alt (e.node) {
940971
case (ast.expr_lit(?lit, _)) {
@@ -1002,34 +1033,7 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
10021033
}
10031034

10041035
case (ast.expr_cast(?e, _, ?ann)) {
1005-
auto e_res = trans_expr(cx, *e);
1006-
auto llsrctype = val_ty(e_res.val);
1007-
auto t = node_ann_type(cx.fcx.ccx, ann);
1008-
auto lldsttype = type_of(cx.fcx.ccx, t);
1009-
if (!typeck.type_is_fp(t)) {
1010-
if (llvm.LLVMGetIntTypeWidth(lldsttype) >
1011-
llvm.LLVMGetIntTypeWidth(llsrctype)) {
1012-
if (typeck.type_is_signed(t)) {
1013-
// Widening signed cast.
1014-
e_res.val =
1015-
e_res.bcx.build.SExtOrBitCast(e_res.val,
1016-
lldsttype);
1017-
} else {
1018-
// Widening unsigned cast.
1019-
e_res.val =
1020-
e_res.bcx.build.ZExtOrBitCast(e_res.val,
1021-
lldsttype);
1022-
}
1023-
} else {
1024-
// Narrowing cast.
1025-
e_res.val =
1026-
e_res.bcx.build.TruncOrBitCast(e_res.val,
1027-
lldsttype);
1028-
}
1029-
} else {
1030-
cx.fcx.ccx.sess.unimpl("fp cast");
1031-
}
1032-
ret e_res;
1036+
ret trans_cast(cx, *e, ann);
10331037
}
10341038
}
10351039
cx.fcx.ccx.sess.unimpl("expr variant in trans_expr");

0 commit comments

Comments
 (0)