rust_llvm: Add way to reflectively ask if a ValueRef is a known constant int.

Add option-returning variants to `const_to_int`/`const_to_uint` that
never assert fail. (These will be used for overflow checking from
rustc_trans::trans::consts.)
This commit is contained in:
Felix S. Klock II 2015-03-27 01:37:10 +01:00
parent 7875dae83f
commit 2e93e386fd
2 changed files with 27 additions and 0 deletions

View file

@ -1976,6 +1976,7 @@ pub fn LLVMDICompositeTypeSetTypeArray(Builder: DIBuilderRef,
pub fn LLVMIsAArgument(value_ref: ValueRef) -> ValueRef;
pub fn LLVMIsAAllocaInst(value_ref: ValueRef) -> ValueRef;
pub fn LLVMIsAConstantInt(value_ref: ValueRef) -> ValueRef;
pub fn LLVMInitializeX86TargetInfo();
pub fn LLVMInitializeX86Target();

View file

@ -963,6 +963,32 @@ pub fn const_to_uint(v: ValueRef) -> u64 {
}
}
fn is_const_integral(v: ValueRef) -> bool {
unsafe {
!llvm::LLVMIsAConstantInt(v).is_null()
}
}
pub fn const_to_opt_int(v: ValueRef) -> Option<i64> {
unsafe {
if is_const_integral(v) {
Some(llvm::LLVMConstIntGetSExtValue(v))
} else {
None
}
}
}
pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
unsafe {
if is_const_integral(v) {
Some(llvm::LLVMConstIntGetZExtValue(v))
} else {
None
}
}
}
pub fn is_undef(val: ValueRef) -> bool {
unsafe {
llvm::LLVMIsUndef(val) != False