rustc_codegen_llvm: use safe references for Context and Module.
This commit is contained in:
parent
af04e9426c
commit
249d5acaec
13 changed files with 546 additions and 553 deletions
|
@ -20,9 +20,11 @@
|
|||
|
||||
#![feature(box_patterns)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(custom_attribute)]
|
||||
#![feature(extern_types)]
|
||||
#![feature(fs_read_write)]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![allow(unused_attributes)]
|
||||
#![feature(libc)]
|
||||
#![feature(quote)]
|
||||
|
@ -34,6 +36,7 @@
|
|||
#![feature(link_args)]
|
||||
#![feature(static_nobundle)]
|
||||
|
||||
use back::write::create_target_machine;
|
||||
use rustc::dep_graph::WorkProduct;
|
||||
use syntax_pos::symbol::Symbol;
|
||||
|
||||
|
@ -340,22 +343,41 @@ enum ModuleSource {
|
|||
Codegened(ModuleLlvm),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ModuleLlvm {
|
||||
llcx: llvm::ContextRef,
|
||||
llmod: llvm::ModuleRef,
|
||||
tm: llvm::TargetMachineRef,
|
||||
llcx: &'static mut llvm::Context,
|
||||
llmod_raw: *const llvm::Module,
|
||||
tm: &'static mut llvm::TargetMachine,
|
||||
}
|
||||
|
||||
unsafe impl Send for ModuleLlvm { }
|
||||
unsafe impl Sync for ModuleLlvm { }
|
||||
|
||||
impl ModuleLlvm {
|
||||
fn new(sess: &Session, mod_name: &str) -> Self {
|
||||
unsafe {
|
||||
let llcx = llvm::LLVMRustContextCreate(sess.fewer_names());
|
||||
let llmod_raw = context::create_module(sess, llcx, mod_name) as *const _;
|
||||
|
||||
ModuleLlvm {
|
||||
llmod_raw,
|
||||
llcx,
|
||||
tm: create_target_machine(sess, false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn llmod(&self) -> &llvm::Module {
|
||||
unsafe {
|
||||
&*self.llmod_raw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ModuleLlvm {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
llvm::LLVMDisposeModule(self.llmod);
|
||||
llvm::LLVMContextDispose(self.llcx);
|
||||
llvm::LLVMRustDisposeTargetMachine(self.tm);
|
||||
llvm::LLVMContextDispose(&mut *(self.llcx as *mut _));
|
||||
llvm::LLVMRustDisposeTargetMachine(&mut *(self.tm as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue