]> git.lizzy.rs Git - rust.git/commitdiff
rustc_codegen_llvm: use safe references for OperandBundleDef.
authorIrina Popa <irinagpopa@gmail.com>
Mon, 16 Jul 2018 13:02:31 +0000 (16:02 +0300)
committerIrina Popa <irinagpopa@gmail.com>
Mon, 30 Jul 2018 17:10:37 +0000 (20:10 +0300)
src/librustc_codegen_llvm/builder.rs
src/librustc_codegen_llvm/common.rs
src/librustc_codegen_llvm/llvm/ffi.rs
src/librustc_codegen_llvm/llvm/mod.rs

index 676523b09f77278819b8ac981eb27dcc4d4092dd..2307ad49b5b49b17f4960a3bb9b913aadf096611 100644 (file)
@@ -23,7 +23,6 @@
 use std::ffi::CString;
 use std::ops::Range;
 use std::ptr;
-use std::ptr::NonNull;
 
 // All Builders must have an llfn associated with them
 #[must_use]
@@ -176,7 +175,7 @@ pub fn invoke(&self,
                   args: &[&'ll Value],
                   then: &'ll BasicBlock,
                   catch: &'ll BasicBlock,
-                  bundle: Option<&OperandBundleDef>) -> &'ll Value {
+                  bundle: Option<&OperandBundleDef<'ll>>) -> &'ll Value {
         self.count_insn("invoke");
 
         debug!("Invoke {:?} with args ({:?})",
@@ -184,7 +183,7 @@ pub fn invoke(&self,
                args);
 
         let args = self.check_call("invoke", llfn, args);
-        let bundle = bundle.as_ref().and_then(|b| NonNull::new(b.raw()));
+        let bundle = bundle.map(|b| &*b.raw);
 
         unsafe {
             llvm::LLVMRustBuildInvoke(self.llbuilder,
@@ -724,7 +723,7 @@ pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
     }
 
     pub fn call(&self, llfn: &'ll Value, args: &[&'ll Value],
-                bundle: Option<&OperandBundleDef>) -> &'ll Value {
+                bundle: Option<&OperandBundleDef<'ll>>) -> &'ll Value {
         self.count_insn("call");
 
         debug!("Call {:?} with args ({:?})",
@@ -732,7 +731,7 @@ pub fn call(&self, llfn: &'ll Value, args: &[&'ll Value],
                args);
 
         let args = self.check_call("call", llfn, args);
-        let bundle = bundle.as_ref().and_then(|b| NonNull::new(b.raw()));
+        let bundle = bundle.map(|b| &*b.raw);
 
         unsafe {
             llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),
index fffb3561744f7bcaec0a868ddd447e72f7500ae1..2d0ef7b3eef32b4bf2d728af4d72b8eac1fa42e8 100644 (file)
@@ -92,7 +92,7 @@ pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bo
 /// the `OperandBundleDef` value created for MSVC landing pads.
 pub struct Funclet<'ll> {
     cleanuppad: &'ll Value,
-    operand: OperandBundleDef,
+    operand: OperandBundleDef<'ll>,
 }
 
 impl Funclet<'ll> {
@@ -107,7 +107,7 @@ pub fn cleanuppad(&self) -> &'ll Value {
         self.cleanuppad
     }
 
-    pub fn bundle(&self) -> &OperandBundleDef {
+    pub fn bundle(&self) -> &OperandBundleDef<'ll> {
         &self.operand
     }
 }
index d55466726c2fa78fd34fd1c860cd4a9b7c9c0f37..02a7bc045d6fcb40a07afcebfe7dbe52c0eaa9fa 100644 (file)
@@ -24,6 +24,7 @@
 use libc::{c_uint, c_int, size_t, c_char};
 use libc::{c_ulonglong, c_void};
 
+use std::marker::PhantomData;
 use std::ptr::NonNull;
 
 use super::RustString;
@@ -381,6 +382,12 @@ pub enum ThreadLocalMode {
   LocalExec
 }
 
+extern { type Opaque; }
+struct InvariantOpaque<'a> {
+    _marker: PhantomData<&'a mut &'a ()>,
+    _opaque: Opaque,
+}
+
 // Opaque pointer types
 extern { pub type Module; }
 extern { pub type Context; }
@@ -408,8 +415,7 @@ pub enum ThreadLocalMode {
 extern { pub type SMDiagnostic; }
 extern { pub type RustArchiveMember; }
 pub type RustArchiveMemberRef = *mut RustArchiveMember;
-extern { pub type OperandBundleDef; }
-pub type OperandBundleDefRef = *mut OperandBundleDef;
+pub struct OperandBundleDef<'a>(InvariantOpaque<'a>);
 extern { pub type Linker; }
 pub type LinkerRef = *mut Linker;
 
@@ -706,7 +712,7 @@ pub fn LLVMRustBuildInvoke(B: &'a Builder,
                                NumArgs: c_uint,
                                Then: &'a BasicBlock,
                                Catch: &'a BasicBlock,
-                               Bundle: Option<NonNull<OperandBundleDef>>,
+                               Bundle: Option<&OperandBundleDef<'a>>,
                                Name: *const c_char)
                                -> &'a Value;
     pub fn LLVMBuildLandingPad(B: &'a Builder,
@@ -975,7 +981,7 @@ pub fn LLVMRustBuildCall(B: &'a Builder,
                              Fn: &'a Value,
                              Args: *const &'a Value,
                              NumArgs: c_uint,
-                             Bundle: Option<NonNull<OperandBundleDef>>,
+                             Bundle: Option<&OperandBundleDef<'a>>,
                              Name: *const c_char)
                              -> &'a Value;
     pub fn LLVMBuildSelect(B: &'a Builder,
@@ -1520,10 +1526,10 @@ pub fn LLVMRustArchiveMemberNew(Filename: *const c_char,
     pub fn LLVMRustSetDataLayoutFromTargetMachine(M: &'a Module, TM: &'a TargetMachine);
 
     pub fn LLVMRustBuildOperandBundleDef(Name: *const c_char,
-                                         Inputs: *const &Value,
+                                         Inputs: *const &'a Value,
                                          NumInputs: c_uint)
-                                         -> OperandBundleDefRef;
-    pub fn LLVMRustFreeOperandBundleDef(Bundle: OperandBundleDefRef);
+                                         -> &'a mut OperandBundleDef<'a>;
+    pub fn LLVMRustFreeOperandBundleDef(Bundle: &'a mut OperandBundleDef<'a>);
 
     pub fn LLVMRustPositionBuilderAtStart(B: &'a Builder, BB: &'a BasicBlock);
 
index c27e0d2ae8681d60e89fffebe058297d3b55e666..93dd98c62dc3edd28afd61561218f4cfe4aa51a2 100644 (file)
@@ -258,28 +258,24 @@ pub fn last_error() -> Option<String> {
     }
 }
 
-pub struct OperandBundleDef {
-    inner: OperandBundleDefRef,
+pub struct OperandBundleDef<'a> {
+    pub raw: &'a mut ffi::OperandBundleDef<'a>,
 }
 
-impl OperandBundleDef {
-    pub fn new(name: &str, vals: &[&'a Value]) -> OperandBundleDef {
+impl OperandBundleDef<'a> {
+    pub fn new(name: &str, vals: &[&'a Value]) -> Self {
         let name = CString::new(name).unwrap();
         let def = unsafe {
             LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
         };
-        OperandBundleDef { inner: def }
-    }
-
-    pub fn raw(&self) -> OperandBundleDefRef {
-        self.inner
+        OperandBundleDef { raw: def }
     }
 }
 
-impl Drop for OperandBundleDef {
+impl Drop for OperandBundleDef<'a> {
     fn drop(&mut self) {
         unsafe {
-            LLVMRustFreeOperandBundleDef(self.inner);
+            LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));
         }
     }
 }