Skip to content

librustc_llvm: encapsulate "as" cast from/to a bool #32700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ pub type Bool = c_uint;
pub const True: Bool = 1 as Bool;
pub const False: Bool = 0 as Bool;

#[inline(always)]
pub fn as_bool(b: Bool) -> bool { False != b }
#[inline(always)]
pub fn as_llvm_bool(b: bool) -> Bool { b as Bool }

// Consts for the LLVM CallConv type, pre-cast to usize.

#[derive(Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -2178,13 +2183,13 @@ pub fn SetDLLStorageClass(global: ValueRef, class: DLLStorageClassTypes) {

pub fn SetUnnamedAddr(global: ValueRef, unnamed: bool) {
unsafe {
LLVMSetUnnamedAddr(global, unnamed as Bool);
LLVMSetUnnamedAddr(global, as_llvm_bool(unnamed));
}
}

pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
unsafe {
LLVMSetThreadLocal(global, is_thread_local as Bool);
LLVMSetThreadLocal(global, as_llvm_bool(is_thread_local));
}
}

Expand Down Expand Up @@ -2443,3 +2448,18 @@ impl Drop for OperandBundleDef {
mod llvmdeps {
include! { env!("CFG_LLVM_LINKAGE_FILE") }
}

#[cfg(test)]
mod tests {
use super::{True, False};

#[test]
fn test_bool_cast() {
assert_eq!( True, super::as_llvm_bool(true));
assert_eq!(False, super::as_llvm_bool(false));

assert!( super::as_bool(True));
assert!( super::as_bool(12345));
assert!(!super::as_bool(False));
}
}
4 changes: 2 additions & 2 deletions src/librustc_metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ use rustc::session::search_paths::PathKind;
use rustc::util::common;

use rustc_llvm as llvm;
use rustc_llvm::{False, ObjectFile, mk_section_iter};
use rustc_llvm::{ObjectFile, mk_section_iter};
use rustc_llvm::archive_ro::ArchiveRO;
use syntax::codemap::Span;
use syntax::errors::DiagnosticBuilder;
Expand Down Expand Up @@ -793,7 +793,7 @@ fn get_metadata_section_imp(target: &Target, filename: &Path)
}
};
let si = mk_section_iter(of.llof);
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
while !llvm::as_bool(llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi)) {
let mut name_buf = ptr::null();
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
let name = slice::from_raw_parts(name_buf as *const u8,
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ pub fn load_ty_builder<'a, 'tcx>(b: &Builder<'a, 'tcx>, ptr: ValueRef, t: Ty<'tc

unsafe {
let global = llvm::LLVMIsAGlobalVariable(ptr);
if !global.is_null() && llvm::LLVMIsGlobalConstant(global) == llvm::True {
if !global.is_null() && llvm::as_bool(llvm::LLVMIsGlobalConstant(global)) {
let val = llvm::LLVMGetInitializer(global);
if !val.is_null() {
if t.is_bool() {
Expand Down Expand Up @@ -2530,7 +2530,7 @@ fn internalize_symbols(cx: &SharedCrateContext, reachable: &HashSet<&str>) {
// We only care about external declarations (not definitions)
// and available_externally definitions.
if !(linkage == llvm::ExternalLinkage as c_uint &&
llvm::LLVMIsDeclaration(val) != 0) &&
llvm::as_bool(llvm::LLVMIsDeclaration(val))) &&
!(linkage == llvm::AvailableExternallyLinkage as c_uint) {
continue;
}
Expand All @@ -2551,7 +2551,7 @@ fn internalize_symbols(cx: &SharedCrateContext, reachable: &HashSet<&str>) {
// We only care about external definitions.
if !((linkage == llvm::ExternalLinkage as c_uint ||
linkage == llvm::WeakODRLinkage as c_uint) &&
llvm::LLVMIsDeclaration(val) == 0) {
!llvm::as_bool(llvm::LLVMIsDeclaration(val))) {
continue;
}

Expand Down Expand Up @@ -2590,7 +2590,7 @@ fn create_imps(cx: &SharedCrateContext) {
.filter(|&val| {
llvm::LLVMGetLinkage(val) ==
llvm::ExternalLinkage as c_uint &&
llvm::LLVMIsDeclaration(val) == 0
!llvm::as_bool(llvm::LLVMIsDeclaration(val))
})
.collect();

Expand Down Expand Up @@ -2715,7 +2715,7 @@ pub fn trans_crate<'tcx>(tcx: &TyCtxt<'tcx>,
static INIT: Once = Once::new();
static mut POISONED: bool = false;
INIT.call_once(|| {
if llvm::LLVMStartMultithreaded() != 1 {
if !llvm::as_bool(llvm::LLVMStartMultithreaded()) {
// use an extra bool to make sure that all future usage of LLVM
// cannot proceed despite the Once not running more than once.
POISONED = true;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn Switch(cx: Block, v: ValueRef, else_: BasicBlockRef, num_cases: usize)

pub fn AddCase(s: ValueRef, on_val: ValueRef, dest: BasicBlockRef) {
unsafe {
if llvm::LLVMIsUndef(s) == llvm::True { return; }
if llvm::as_bool(llvm::LLVMIsUndef(s)) { return; }
llvm::LLVMAddCase(s, on_val, dest);
}
}
Expand Down Expand Up @@ -933,7 +933,7 @@ pub fn Phi(cx: Block, ty: Type, vals: &[ValueRef],

pub fn AddIncomingToPhi(phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
unsafe {
if llvm::LLVMIsUndef(phi) == llvm::True { return; }
if llvm::as_bool(llvm::LLVMIsUndef(phi)) { return; }
llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use session::Session;
use llvm;
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef, TypeKind};
use llvm::{True, False, Bool, OperandBundleDef};
use llvm::{True, OperandBundleDef};
use rustc::cfg;
use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;
Expand Down Expand Up @@ -810,7 +810,7 @@ pub fn C_undef(t: Type) -> ValueRef {

pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
unsafe {
llvm::LLVMConstInt(t.to_ref(), u, sign_extend as Bool)
llvm::LLVMConstInt(t.to_ref(), u, llvm::as_llvm_bool(sign_extend))
}
}

Expand Down Expand Up @@ -902,7 +902,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> Va
let sc = llvm::LLVMConstStringInContext(cx.llcx(),
s.as_ptr() as *const c_char,
s.len() as c_uint,
!null_terminated as Bool);
llvm::as_llvm_bool(!null_terminated));

let gsym = token::gensym("str");
let sym = format!("str{}", gsym.0);
Expand Down Expand Up @@ -934,7 +934,7 @@ pub fn C_struct_in_context(llcx: ContextRef, elts: &[ValueRef], packed: bool) ->
unsafe {
llvm::LLVMConstStructInContext(llcx,
elts.as_ptr(), elts.len() as c_uint,
packed as Bool)
llvm::as_llvm_bool(packed))
}
}

Expand Down Expand Up @@ -1018,16 +1018,20 @@ pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
}

pub fn is_undef(val: ValueRef) -> bool {
unsafe {
llvm::LLVMIsUndef(val) != False
}
llvm::as_bool(
unsafe {
llvm::LLVMIsUndef(val)
}
)
}

#[allow(dead_code)] // potentially useful
pub fn is_null(val: ValueRef) -> bool {
unsafe {
llvm::LLVMIsNull(val) != False
}
llvm::as_bool(
unsafe {
llvm::LLVMIsNull(val)
}
)
}

pub fn monomorphize_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, t: Ty<'tcx>) -> Ty<'tcx> {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use llvm;
use llvm::{ConstFCmp, ConstICmp, SetLinkage, SetUnnamedAddr};
use llvm::{InternalLinkage, ValueRef, Bool, True};
use llvm::{InternalLinkage, ValueRef, True};
use middle::const_qualif::ConstQualif;
use rustc_const_eval::{ConstEvalErr, lookup_const_fn_by_id, lookup_const_by_id, ErrKind};
use rustc_const_eval::eval_repeat_count;
Expand Down Expand Up @@ -756,11 +756,11 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
let repr = adt::represent_type(cx, t_expr);
let discr = adt::const_get_discrim(&repr, v);
let iv = C_integral(cx.int_type(), discr.0, false);
let s = adt::is_discr_signed(&repr) as Bool;
let s = llvm::as_llvm_bool(adt::is_discr_signed(&repr));
llvm::LLVMConstIntCast(iv, llty.to_ref(), s)
},
(CastTy::Int(_), CastTy::Int(_)) => {
let s = t_expr.is_signed() as Bool;
let s = llvm::as_llvm_bool(t_expr.is_signed());
llvm::LLVMConstIntCast(v, llty.to_ref(), s)
},
(CastTy::Int(_), CastTy::Float) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn get_declared_value(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
pub fn get_defined_value(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
get_declared_value(ccx, name).and_then(|val|{
let declaration = unsafe {
llvm::LLVMIsDeclaration(val) != 0
llvm::as_bool(llvm::LLVMIsDeclaration(val))
};
if !declaration {
Some(val)
Expand Down
14 changes: 8 additions & 6 deletions src/librustc_trans/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![allow(non_upper_case_globals)]

use llvm;
use llvm::{TypeRef, Bool, False, True, TypeKind};
use llvm::{TypeRef, False, True, TypeKind};
use llvm::{Float, Double, X86_FP80, PPC_FP128, FP128};

use context::CrateContext;
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Type {
let els: &[TypeRef] = Type::to_ref_slice(els);
ty!(llvm::LLVMStructTypeInContext(ccx.llcx(), els.as_ptr(),
els.len() as c_uint,
packed as Bool))
llvm::as_llvm_bool(packed)))
}

pub fn named_struct(ccx: &CrateContext, name: &str) -> Type {
Expand Down Expand Up @@ -215,7 +215,7 @@ impl Type {
let slice: &[TypeRef] = Type::to_ref_slice(els);
unsafe {
llvm::LLVMStructSetBody(self.to_ref(), slice.as_ptr(),
els.len() as c_uint, packed as Bool)
els.len() as c_uint, llvm::as_llvm_bool(packed))
}
}

Expand All @@ -231,9 +231,11 @@ impl Type {
}

pub fn is_packed(&self) -> bool {
unsafe {
llvm::LLVMIsPackedStruct(self.to_ref()) == True
}
llvm::as_bool(
unsafe {
llvm::LLVMIsPackedStruct(self.to_ref())
}
)
}

pub fn element_type(&self) -> Type {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/execution-engine/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl ExecutionEngine {

let res = unsafe { llvm::LLVMRustLoadDynamicLibrary(cs.as_ptr()) };

if res == 0 {
if !llvm::as_bool(res) {
panic!("Failed to load crate {:?}: {}",
path.display(), llvm_error());
}
Expand Down