]> git.lizzy.rs Git - rust.git/commitdiff
move ConstVal -> ValueRef translation to trans::consts
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 27 Nov 2015 16:18:28 +0000 (17:18 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 8 Dec 2015 13:59:45 +0000 (14:59 +0100)
src/librustc_trans/trans/consts.rs
src/librustc_trans/trans/mir/constant.rs

index 6f40283064bd05011a3bfb4c659649f963d8f8da..5284911340bd744084df505dc9ad268e946968f0 100644 (file)
@@ -31,6 +31,7 @@
 use trans::base::{self, push_ctxt};
 use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
 use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty};
+use trans::common::C_floating_f64;
 use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint};
 use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey};
 use trans::declare;
@@ -107,6 +108,39 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
     }
 }
 
+pub fn trans_constval<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
+                                cv: &ConstVal,
+                                ty: Ty<'tcx>,
+                                param_substs: &'tcx Substs<'tcx>)
+                                -> ValueRef
+{
+    let llty = type_of::type_of(ccx, ty);
+    match *cv {
+        ConstVal::Float(v) => C_floating_f64(v, llty),
+        ConstVal::Bool(v) => C_bool(ccx, v),
+        ConstVal::Int(v) => C_integral(llty, v as u64, true),
+        ConstVal::Uint(v) => C_integral(llty, v, false),
+        ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
+        ConstVal::ByteStr(ref v) => addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
+        ConstVal::Struct(id) | ConstVal::Tuple(id) => {
+            let expr = ccx.tcx().map.expect_expr(id);
+            match const_expr(ccx, expr, param_substs, None, TrueConst::Yes) {
+                Ok((val, _)) => val,
+                Err(e) => panic!("const eval failure: {}", e.description()),
+            }
+        },
+        ConstVal::Function(_) => {
+            unimplemented!()
+        },
+        ConstVal::Array(..) => {
+            unimplemented!()
+        },
+        ConstVal::Repeat(..) => {
+            unimplemented!()
+        },
+    }
+}
+
 pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
     unsafe {
         llvm::LLVMConstPointerCast(val, ty.to_ref())
index 4de586543e9df6893ba65467d236994f2b53f108..9c23d330136ee5d5be731072dc45a3d20dce6fff 100644 (file)
 use middle::ty::{Ty, HasTypeFlags};
 use rustc::middle::const_eval::ConstVal;
 use rustc::mir::repr as mir;
-use trans::consts::{self, TrueConst};
+use trans::consts;
 use trans::common::{self, Block};
-use trans::common::{C_bool, C_bytes, C_floating_f64, C_integral, C_str_slice};
-use trans::type_of;
 
 use super::operand::OperandRef;
 use super::MirContext;
@@ -29,45 +27,11 @@ pub fn trans_constval(&mut self,
         use super::operand::OperandValue::{Ref, Immediate};
 
         let ccx = bcx.ccx();
-        let llty = type_of::type_of(ccx, ty);
-        let val = match *cv {
-            ConstVal::Float(v) => Immediate(C_floating_f64(v, llty)),
-            ConstVal::Bool(v) => Immediate(C_bool(ccx, v)),
-            ConstVal::Int(v) => Immediate(C_integral(llty, v as u64, true)),
-            ConstVal::Uint(v) => Immediate(C_integral(llty, v, false)),
-            ConstVal::Str(ref v) => Immediate(C_str_slice(ccx, v.clone())),
-            ConstVal::ByteStr(ref v) => {
-                Immediate(consts::addr_of(ccx,
-                                          C_bytes(ccx, v),
-                                          1,
-                                          "byte_str"))
-            }
-
-            ConstVal::Struct(id) | ConstVal::Tuple(id) => {
-                let expr = bcx.tcx().map.expect_expr(id);
-                let (llval, _) = match consts::const_expr(ccx,
-                                                          expr,
-                                                          bcx.fcx.param_substs,
-                                                          None,
-                                                          TrueConst::Yes) {
-                    Ok(v) => v,
-                    Err(_) => panic!("constant eval failure"),
-                };
-                if common::type_is_immediate(bcx.ccx(), ty) {
-                    Immediate(llval)
-                } else {
-                    Ref(llval)
-                }
-            }
-            ConstVal::Function(_) => {
-                unimplemented!()
-            }
-            ConstVal::Array(..) => {
-                unimplemented!()
-            }
-            ConstVal::Repeat(..) => {
-                unimplemented!()
-            }
+        let val = consts::trans_constval(ccx, cv, ty, bcx.fcx.param_substs);
+        let val = if common::type_is_immediate(ccx, ty) {
+            Immediate(val)
+        } else {
+            Ref(val)
         };
 
         assert!(!ty.has_erasable_regions());