about summary refs log tree commit diff
path: root/src/comp
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2010-11-24 10:32:14 -0800
committerGraydon Hoare <graydon@mozilla.com>2010-11-24 16:56:01 -0800
commite2f9f746ea5adcede229b5c9c14450a0057ed13b (patch)
tree94ac76414143825e5e54854f4540f35cf9230715 /src/comp
parentc1916adc7e16bd7ecd3ca8dbbe985ec75d0c825a (diff)
downloadrust-e2f9f746ea5adcede229b5c9c14450a0057ed13b.tar.gz
rust-e2f9f746ea5adcede229b5c9c14450a0057ed13b.zip
Move expr_cast translation into helper function.
Diffstat (limited to 'src/comp')
-rw-r--r--src/comp/middle/trans.rs60
1 files changed, 32 insertions, 28 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index 1972a434fa1..893c5691964 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -935,6 +935,37 @@ impure fn trans_exprs(@block_ctxt cx, &vec[@ast.expr] es)
     ret tup(bcx, vs);
 }
 
+impure fn trans_cast(@block_ctxt cx, &ast.expr e, &ast.ann ann) -> result {
+    auto e_res = trans_expr(cx, e);
+    auto llsrctype = val_ty(e_res.val);
+    auto t = node_ann_type(cx.fcx.ccx, ann);
+    auto lldsttype = type_of(cx.fcx.ccx, t);
+    if (!typeck.type_is_fp(t)) {
+        if (llvm.LLVMGetIntTypeWidth(lldsttype) >
+            llvm.LLVMGetIntTypeWidth(llsrctype)) {
+            if (typeck.type_is_signed(t)) {
+                // Widening signed cast.
+                e_res.val =
+                    e_res.bcx.build.SExtOrBitCast(e_res.val,
+                                                  lldsttype);
+            } else {
+                // Widening unsigned cast.
+                e_res.val =
+                    e_res.bcx.build.ZExtOrBitCast(e_res.val,
+                                                  lldsttype);
+            }
+        } else {
+            // Narrowing cast.
+            e_res.val =
+                e_res.bcx.build.TruncOrBitCast(e_res.val,
+                                               lldsttype);
+        }
+    } else {
+        cx.fcx.ccx.sess.unimpl("fp cast");
+    }
+    ret e_res;
+}
+
 impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
     alt (e.node) {
         case (ast.expr_lit(?lit, _)) {
@@ -1002,34 +1033,7 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
         }
 
         case (ast.expr_cast(?e, _, ?ann)) {
-            auto e_res = trans_expr(cx, *e);
-            auto llsrctype = val_ty(e_res.val);
-            auto t = node_ann_type(cx.fcx.ccx, ann);
-            auto lldsttype = type_of(cx.fcx.ccx, t);
-            if (!typeck.type_is_fp(t)) {
-                if (llvm.LLVMGetIntTypeWidth(lldsttype) >
-                    llvm.LLVMGetIntTypeWidth(llsrctype)) {
-                    if (typeck.type_is_signed(t)) {
-                        // Widening signed cast.
-                        e_res.val =
-                            e_res.bcx.build.SExtOrBitCast(e_res.val,
-                                                          lldsttype);
-                    } else {
-                        // Widening unsigned cast.
-                        e_res.val =
-                            e_res.bcx.build.ZExtOrBitCast(e_res.val,
-                                                          lldsttype);
-                    }
-                } else {
-                    // Narrowing cast.
-                    e_res.val =
-                        e_res.bcx.build.TruncOrBitCast(e_res.val,
-                                                       lldsttype);
-                }
-            } else {
-                cx.fcx.ccx.sess.unimpl("fp cast");
-            }
-            ret e_res;
+            ret trans_cast(cx, *e, ann);
         }
     }
     cx.fcx.ccx.sess.unimpl("expr variant in trans_expr");