]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_codegen_llvm/common.rs
Use the method form for CodegenCx everywhere
[rust.git] / src / librustc_codegen_llvm / common.rs
index ba8d92d37acd8cefe626df96c5178c6209fd8520..569fbc9f0813efb8cf6a0765419bbb8c15cdd028 100644 (file)
@@ -50,7 +50,7 @@ pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bo
     ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP)
 }
 
-pub struct OperandBundleDef<'a, Value : 'a> {
+pub struct OperandBundleDef<'a, Value: 'a> {
     pub name: &'a str,
     pub val: Value
 }
@@ -200,7 +200,7 @@ impl Backend for CodegenCx<'ll, 'tcx> {
     type Context = &'ll llvm::Context;
 }
 
-impl<'ll, 'tcx : 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
+impl<'ll, 'tcx: 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
 
     // LLVM constant constructors.
     fn c_null(&self, t: &'ll Type) -> &'ll Value {
@@ -321,13 +321,13 @@ fn c_struct(
         &self.c_struct_in_context(&self.llcx, elts, packed)
     }
 
-    fn c_array(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
+    fn c_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
         unsafe {
             return llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint);
         }
     }
 
-    fn c_vector(elts: &[&'ll Value]) -> &'ll Value {
+    fn c_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
         unsafe {
             return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
         }
@@ -337,7 +337,7 @@ fn c_bytes(&self, bytes: &[u8]) -> &'ll Value {
         &self.c_bytes_in_context(&self.llcx, bytes)
     }
 
-    fn const_get_elt(v: &'ll Value, idx: u64) -> &'ll Value {
+    fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
         unsafe {
             assert_eq!(idx as c_uint as u64, idx);
             let us = &[idx as c_uint];
@@ -350,9 +350,9 @@ fn const_get_elt(v: &'ll Value, idx: u64) -> &'ll Value {
         }
     }
 
-    fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
+    fn const_get_real(&self, v: &'ll Value) -> Option<(f64, bool)> {
         unsafe {
-            if Self::is_const_real(v) {
+            if self.is_const_real(v) {
                 let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
                 let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
                 let loses_info = if loses_info == 1 { true } else { false };
@@ -363,27 +363,27 @@ fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
         }
     }
 
-    fn const_to_uint(v: &'ll Value) -> u64 {
+    fn const_to_uint(&self, v: &'ll Value) -> u64 {
         unsafe {
             llvm::LLVMConstIntGetZExtValue(v)
         }
     }
 
-    fn is_const_integral(v: &'ll Value) -> bool {
+    fn is_const_integral(&self, v: &'ll Value) -> bool {
         unsafe {
             llvm::LLVMIsAConstantInt(v).is_some()
         }
     }
 
-    fn is_const_real(v: &'ll Value) -> bool {
+    fn is_const_real(&self, v: &'ll Value) -> bool {
         unsafe {
             llvm::LLVMIsAConstantFP(v).is_some()
         }
     }
 
-    fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
+    fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
         unsafe {
-            if Self::is_const_integral(v) {
+            if self.is_const_integral(v) {
                 let (mut lo, mut hi) = (0u64, 0u64);
                 let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
                                                            &mut hi, &mut lo);
@@ -399,18 +399,38 @@ fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
     }
 }
 
-impl<'ll, 'tcx : 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx> {
+pub fn val_ty(v: &'ll Value) -> &'ll Type {
+    unsafe {
+        llvm::LLVMTypeOf(v)
+    }
+}
+
+pub fn c_bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
+    unsafe {
+        let ptr = bytes.as_ptr() as *const c_char;
+        return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
+    }
+}
+
+pub fn c_struct_in_context(
+    llcx: &'a llvm::Context,
+    elts: &[&'a Value],
+    packed: bool,
+) -> &'a Value {
+    unsafe {
+        llvm::LLVMConstStructInContext(llcx,
+                                       elts.as_ptr(), elts.len() as c_uint,
+                                       packed as Bool)
+    }
+}
+
+impl<'ll, 'tcx: 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx> {
     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
+        val_ty(v)
     }
 
     fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
-        unsafe {
-            let ptr = bytes.as_ptr() as *const c_char;
-            return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
-        }
+        c_bytes_in_context(llcx, bytes)
     }
 
     fn c_struct_in_context(
@@ -419,11 +439,7 @@ fn c_struct_in_context(
         elts: &[&'a Value],
         packed: bool,
     ) -> &'a Value {
-        unsafe {
-            llvm::LLVMConstStructInContext(llcx,
-                                           elts.as_ptr(), elts.len() as c_uint,
-                                           packed as Bool)
-        }
+        c_struct_in_context(llcx, elts, packed)
     }
 }