]> git.lizzy.rs Git - rust.git/commitdiff
Removed code duplication for CommonWriteMethods
authorDenis Merigoux <denis.merigoux@gmail.com>
Thu, 30 Aug 2018 12:24:41 +0000 (14:24 +0200)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Fri, 16 Nov 2018 12:11:59 +0000 (14:11 +0200)
src/librustc_codegen_llvm/back/write.rs
src/librustc_codegen_llvm/common.rs
src/librustc_codegen_llvm/lib.rs

index 3c32da4303170348cd7d8157c270094d909d148b..24f58ac59c15ca244880c284de920531d9c9e178 100644 (file)
@@ -24,7 +24,7 @@
 use rustc::session::Session;
 use rustc::util::nodemap::FxHashMap;
 use time_graph::{self, TimeGraph, Timeline};
-use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock, True};
+use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock};
 use llvm_util;
 use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
      CachedModuleCodegen};
@@ -47,6 +47,7 @@
 use type_::Type;
 use context::{is_pie_binary, get_reloc_model};
 use interfaces::{Backend, CommonWriteMethods};
+use common;
 use jobserver::{Client, Acquired};
 use rustc_demangle;
 use value::Value;
@@ -437,16 +438,11 @@ impl<'ll> Backend for CodegenContext<'ll> {
 
 impl CommonWriteMethods for CodegenContext<'ll> {
     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
+        common::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);
-        }
+        common::c_bytes_in_context(llcx, bytes)
     }
 
     fn c_struct_in_context(
@@ -455,11 +451,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 llvm::Bool)
-        }
+        common::c_struct_in_context(llcx, elts, packed)
     }
 }
 
index ba8d92d37acd8cefe626df96c5178c6209fd8520..80f8bbeabda38c063250581716cd521f5f6cb52b 100644 (file)
@@ -399,18 +399,38 @@ fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
     }
 }
 
+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)
     }
 }
 
index 2ef90c03c67ec777a25ff9782c2743d16141b569..e228dc044f17700932801aa23adddcc8f3574474 100644 (file)
@@ -76,7 +76,6 @@
 use std::any::Any;
 use std::sync::mpsc;
 use std::marker::PhantomData;
-use libc::{c_uint, c_char};
 use rustc_data_structures::sync::Lrc;
 
 use rustc::dep_graph::DepGraph;
@@ -361,20 +360,11 @@ fn llmod(&self) -> &llvm::Module {
 
 impl CommonWriteMethods for ModuleLlvm<'ll> {
     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
+        common::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,
-                llvm::True);
-        }
+        common::c_bytes_in_context(llcx, bytes)
     }
 
     fn c_struct_in_context(
@@ -383,11 +373,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 llvm::Bool)
-        }
+        common::c_struct_in_context(llcx, elts, packed)
     }
 }