-
Notifications
You must be signed in to change notification settings - Fork 264
Fix #672: Implement TryFrom<PointerValue> for GlobalValue #676
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ use llvm_sys::core::{ | |
| use llvm_sys::LLVMThreadLocalMode; | ||
| use llvm_sys::core::{LLVMGetUnnamedAddress, LLVMSetUnnamedAddress}; | ||
| use llvm_sys::prelude::LLVMValueRef; | ||
| use std::convert::TryFrom; | ||
|
|
||
| use llvm_sys::LLVMUnnamedAddr; | ||
|
|
||
|
|
@@ -281,6 +282,19 @@ unsafe impl AsValueRef for GlobalValue<'_> { | |
| } | ||
| } | ||
|
|
||
| impl<'ctx> TryFrom<PointerValue<'ctx>> for GlobalValue<'ctx> { | ||
| type Error = (); | ||
|
|
||
| fn try_from(value: PointerValue<'ctx>) -> Result<Self, Self::Error> { | ||
| let is_global = unsafe { !llvm_sys::core::LLVMIsAGlobalVariable(value.as_value_ref()).is_null() }; | ||
| if is_global { | ||
| unsafe { Ok(GlobalValue::new(value.as_value_ref())) } | ||
| } else { | ||
| Err(()) | ||
|
Comment on lines
+285
to
+293
|
||
| } | ||
| } | ||
| } | ||
|
|
||
|
meftunca marked this conversation as resolved.
|
||
| impl Display for GlobalValue<'_> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "{}", self.print_to_string()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it would make sense to have a
GlobalValue::from_pointer_valuefunction? That way it's more apparent in the API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, PointerValue implements Copy, so it's not really necessary to return the value on error.