]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_codegen_llvm/common.rs
Remove is_const_integral method from ConstMethods
[rust.git] / src / librustc_codegen_llvm / common.rs
index b0c94a139be0807763442c582c9b0719b6f12511..cc2f1b6776e457b002a489c0e9751206a0aa493d 100644 (file)
@@ -17,7 +17,7 @@
 
 use libc::{c_uint, c_char};
 
-use syntax::symbol::LocalInternedString;
+use syntax::symbol::Symbol;
 use syntax::ast::Mutability;
 
 pub use crate::context::CodegenCx;
@@ -86,6 +86,8 @@ pub fn bundle(&self) -> &OperandBundleDef<'ll> {
 
 impl BackendTypes for CodegenCx<'ll, 'tcx> {
     type Value = &'ll Value;
+    type FuncId = &'ll Value;
+
     type BasicBlock = &'ll BasicBlock;
     type Type = &'ll Type;
     type Funclet = Funclet<'ll>;
@@ -122,7 +124,7 @@ pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
 
     fn const_cstr(
         &self,
-        s: LocalInternedString,
+        s: Symbol,
         null_terminated: bool,
     ) -> &'ll Value {
         unsafe {
@@ -130,9 +132,10 @@ fn const_cstr(
                 return llval;
             }
 
+            let s_str = s.as_str();
             let sc = llvm::LLVMConstStringInContext(self.llcx,
-                                                    s.as_ptr() as *const c_char,
-                                                    s.len() as c_uint,
+                                                    s_str.as_ptr() as *const c_char,
+                                                    s_str.len() as c_uint,
                                                     !null_terminated as Bool);
             let sym = self.generate_local_symbol_name("str");
             let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
@@ -147,8 +150,8 @@ fn const_cstr(
         }
     }
 
-    pub fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
-        let len = s.len();
+    pub fn const_str_slice(&self, s: Symbol) -> &'ll Value {
+        let len = s.as_str().len();
         let cs = consts::ptrcast(self.const_cstr(s, false),
             self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
         self.const_fat_ptr(cs, self.const_usize(len as u64))
@@ -242,21 +245,19 @@ fn const_struct(
         struct_in_context(self.llcx, elts, packed)
     }
 
-    fn const_to_uint(&self, v: &'ll Value) -> u64 {
-        unsafe {
-            llvm::LLVMConstIntGetZExtValue(v)
-        }
-    }
-
-    fn is_const_integral(&self, v: &'ll Value) -> bool {
-        unsafe {
-            llvm::LLVMIsAConstantInt(v).is_some()
+    fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
+        if is_const_integral(v) {
+            unsafe {
+                Some(llvm::LLVMConstIntGetZExtValue(v))
+            }
+        } else {
+            None
         }
     }
 
     fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
         unsafe {
-            if self.is_const_integral(v) {
+            if is_const_integral(v) {
                 let (mut lo, mut hi) = (0u64, 0u64);
                 let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
                                                            &mut hi, &mut lo);
@@ -333,16 +334,22 @@ fn from_const_alloc(
         offset: Size,
     ) -> PlaceRef<'tcx, &'ll Value> {
         assert_eq!(alloc.align, layout.align.abi);
-        let init = const_alloc_to_llvm(self, alloc);
-        let base_addr = self.static_addr_of(init, alloc.align, None);
-
-        let llval = unsafe { llvm::LLVMConstInBoundsGEP(
-            self.const_bitcast(base_addr, self.type_i8p()),
-            &self.const_usize(offset.bytes()),
-            1,
-        )};
-        let llval = self.const_bitcast(llval, self.type_ptr_to(layout.llvm_type(self)));
-        PlaceRef::new_sized(llval, layout, alloc.align)
+        let llty = self.type_ptr_to(layout.llvm_type(self));
+        let llval = if layout.size == Size::ZERO {
+            let llval = self.const_usize(alloc.align.bytes());
+            unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
+        } else {
+            let init = const_alloc_to_llvm(self, alloc);
+            let base_addr = self.static_addr_of(init, alloc.align, None);
+
+            let llval = unsafe { llvm::LLVMConstInBoundsGEP(
+                self.const_bitcast(base_addr, self.type_i8p()),
+                &self.const_usize(offset.bytes()),
+                1,
+            )};
+            self.const_bitcast(llval, llty)
+        };
+        PlaceRef::new_sized(llval, layout)
     }
 
     fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
@@ -379,3 +386,9 @@ pub fn struct_in_context(
 fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
     ((hi as u128) << 64) | (lo as u128)
 }
+
+fn is_const_integral(v: &'ll Value) -> bool {
+    unsafe {
+        llvm::LLVMIsAConstantInt(v).is_some()
+    }
+}