about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorJed Davis <jld@panix.com>2013-02-28 11:43:20 -0800
committerJed Davis <jld@panix.com>2013-03-06 20:41:57 -0800
commitb673b26f03043f974682efceffbf40df9cf4bc84 (patch)
treeb015b9be8f950226ff12e35d35aa1ccadb5f01b0 /src
parenta5030e7615f7d37acaee69b13f97b9ffa23159a5 (diff)
downloadrust-b673b26f03043f974682efceffbf40df9cf4bc84.tar.gz
rust-b673b26f03043f974682efceffbf40df9cf4bc84.zip
Factor out discriminant loading more, for use in casts.
Diffstat (limited to 'src')
-rw-r--r--src/librustc/middle/trans/adt.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/librustc/middle/trans/adt.rs b/src/librustc/middle/trans/adt.rs
index c6ba0a7d81f..6aab2af567f 100644
--- a/src/librustc/middle/trans/adt.rs
+++ b/src/librustc/middle/trans/adt.rs
@@ -159,19 +159,27 @@ fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
     }
 }
 
-pub fn trans_switch(bcx: block, r: &Repr, scrutinee: ValueRef) ->
-    (_match::branch_kind, Option<ValueRef>) {
+pub fn trans_switch(bcx: block, r: &Repr, scrutinee: ValueRef)
+    -> (_match::branch_kind, Option<ValueRef>) {
     match *r {
-        CEnum(min, max) => {
-            (_match::switch, Some(load_discr(bcx, scrutinee, min, max)))
+        CEnum(*) | General(*) => {
+            (_match::switch, Some(trans_cast_to_int(bcx, r, scrutinee)))
         }
         Unit(*) | Univariant(*) => {
             (_match::single, None)
         }
-        General(ref cases) => {
-            (_match::switch, Some(load_discr(bcx, scrutinee, 0,
-                                             (cases.len() - 1) as int)))
-        }
+    }
+}
+
+pub fn trans_cast_to_int(bcx: block, r: &Repr, scrutinee: ValueRef)
+    -> ValueRef {
+    match *r {
+        Unit(the_disc) => C_int(bcx.ccx(), the_disc),
+        CEnum(min, max) => load_discr(bcx, scrutinee, min, max),
+        Univariant(*) => bcx.ccx().sess.bug(~"type has no explicit \
+                                              discriminant"),
+        General(ref cases) => load_discr(bcx, scrutinee, 0,
+                                         (cases.len() - 1) as int)
     }
 }