Skip to content

Commit d1ee489

Browse files
committed
Removed code duplication for CommonWriteMethods
1 parent beaf35a commit d1ee489

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

src/librustc_codegen_llvm/back/write.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Sanitize
2626
use rustc::session::Session;
2727
use rustc::util::nodemap::FxHashMap;
2828
use time_graph::{self, TimeGraph, Timeline};
29-
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock, True};
29+
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock};
3030
use llvm_util;
3131
use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
3232
CachedModuleCodegen};
@@ -46,6 +46,7 @@ use syntax_pos::symbol::Symbol;
4646
use type_::Type;
4747
use context::{is_pie_binary, get_reloc_model};
4848
use interfaces::{Backend, CommonWriteMethods};
49+
use common;
4950
use jobserver::{Client, Acquired};
5051
use rustc_demangle;
5152
use value::Value;
@@ -428,16 +429,11 @@ impl<'ll> Backend for CodegenContext<'ll> {
428429

429430
impl CommonWriteMethods for CodegenContext<'ll> {
430431
fn val_ty(&self, v: &'ll Value) -> &'ll Type {
431-
unsafe {
432-
llvm::LLVMTypeOf(v)
433-
}
432+
common::val_ty(v)
434433
}
435434

436435
fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
437-
unsafe {
438-
let ptr = bytes.as_ptr() as *const c_char;
439-
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
440-
}
436+
common::c_bytes_in_context(llcx, bytes)
441437
}
442438

443439
fn c_struct_in_context(
@@ -446,11 +442,7 @@ impl CommonWriteMethods for CodegenContext<'ll> {
446442
elts: &[&'a Value],
447443
packed: bool,
448444
) -> &'a Value {
449-
unsafe {
450-
llvm::LLVMConstStructInContext(llcx,
451-
elts.as_ptr(), elts.len() as c_uint,
452-
packed as llvm::Bool)
453-
}
445+
common::c_struct_in_context(llcx, elts, packed)
454446
}
455447
}
456448

src/librustc_codegen_llvm/common.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,38 @@ impl<'ll, 'tcx : 'll> CommonMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
401401
}
402402
}
403403

404+
pub fn val_ty(v: &'ll Value) -> &'ll Type {
405+
unsafe {
406+
llvm::LLVMTypeOf(v)
407+
}
408+
}
409+
410+
pub fn c_bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
411+
unsafe {
412+
let ptr = bytes.as_ptr() as *const c_char;
413+
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
414+
}
415+
}
416+
417+
pub fn c_struct_in_context(
418+
llcx: &'a llvm::Context,
419+
elts: &[&'a Value],
420+
packed: bool,
421+
) -> &'a Value {
422+
unsafe {
423+
llvm::LLVMConstStructInContext(llcx,
424+
elts.as_ptr(), elts.len() as c_uint,
425+
packed as Bool)
426+
}
427+
}
428+
404429
impl<'ll, 'tcx : 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
405430
fn val_ty(&self, v: &'ll Value) -> &'ll Type {
406-
unsafe {
407-
llvm::LLVMTypeOf(v)
408-
}
431+
val_ty(v)
409432
}
410433

411434
fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
412-
unsafe {
413-
let ptr = bytes.as_ptr() as *const c_char;
414-
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
415-
}
435+
c_bytes_in_context(llcx, bytes)
416436
}
417437

418438
fn c_struct_in_context(
@@ -421,11 +441,7 @@ impl<'ll, 'tcx : 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx, &'ll Value> {
421441
elts: &[&'a Value],
422442
packed: bool,
423443
) -> &'a Value {
424-
unsafe {
425-
llvm::LLVMConstStructInContext(llcx,
426-
elts.as_ptr(), elts.len() as c_uint,
427-
packed as Bool)
428-
}
444+
c_struct_in_context(llcx, elts, packed)
429445
}
430446
}
431447

src/librustc_codegen_llvm/lib.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ use std::any::Any;
7878
use std::path::{PathBuf};
7979
use std::sync::mpsc;
8080
use std::marker::PhantomData;
81-
use libc::{c_uint, c_char};
8281
use rustc_data_structures::sync::Lrc;
8382

8483
use rustc::dep_graph::DepGraph;
@@ -379,20 +378,11 @@ impl ModuleLlvm<'ll> {
379378

380379
impl CommonWriteMethods for ModuleLlvm<'ll> {
381380
fn val_ty(&self, v: &'ll Value) -> &'ll Type {
382-
unsafe {
383-
llvm::LLVMTypeOf(v)
384-
}
381+
common::val_ty(v)
385382
}
386383

387384
fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
388-
unsafe {
389-
let ptr = bytes.as_ptr() as *const c_char;
390-
return llvm::LLVMConstStringInContext(
391-
llcx,
392-
ptr,
393-
bytes.len() as c_uint,
394-
llvm::True);
395-
}
385+
common::c_bytes_in_context(llcx, bytes)
396386
}
397387

398388
fn c_struct_in_context(
@@ -401,11 +391,7 @@ impl CommonWriteMethods for ModuleLlvm<'ll> {
401391
elts: &[&'a Value],
402392
packed: bool,
403393
) -> &'a Value {
404-
unsafe {
405-
llvm::LLVMConstStructInContext(llcx,
406-
elts.as_ptr(), elts.len() as c_uint,
407-
packed as llvm::Bool)
408-
}
394+
common::c_struct_in_context(llcx, elts, packed)
409395
}
410396
}
411397

0 commit comments

Comments
 (0)