Skip to content
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

Provide LLVMTypeWrapper trait #223

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

vadorovsky
Copy link
Member

@vadorovsky vadorovsky commented Sep 9, 2024

Instead of keeping raw pointer fields (of types like LLVMValueRef, LLVMMetadataRef) public and defining constructors with different names, provide the LLVMTypeWrapper trait with from_ptr() as_ptr() method for all of them.

This will allow to convert all safe wrappers from and to raw pointers with one method, which is going to be helpful for building macros for them.


Depends on #222


This change is Reviewable

It isn't being used anywhere (aside from `push` and immediate `pop`) and
has no impact on the visitor algorithm.
Instead of keeping raw pointer fields (of types like `LLVMValueRef`,
`LLVMMetadataRef`) public and defining constructors with different names,
provide the `LLVMTypeWrapper` trait with `from_ptr()` `as_ptr()` method
for all of them.

This will allow to convert all safe wrappers from and to raw pointers
with one method, which is going to be helpful for building macros for
them.
Copy link
Member

@tamird tamird left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r1, 4 of 4 files at r2, all commit messages.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on @vadorovsky)


src/llvm/types/di.rs line 54 at r2 (raw file):

    type Target = LLVMMetadataRef;

    /// Constructs a new [`DIFile`] from the given `metadata`.

looks like this comment should go on the trait now.


src/llvm/types/di.rs line 131 at r2 (raw file):

    type Target = LLVMValueRef;

    /// Constructs a new [`DIType`] from the given `value`.

looks like this comment should go on the trait now.


src/llvm/types/di.rs line 190 at r2 (raw file):

    type Target = LLVMValueRef;

    /// Constructs a new [`DIDerivedType`] from the given `value`.

looks like this comment should go on the trait now.


src/llvm/types/di.rs line 259 at r2 (raw file):

    type Target = LLVMValueRef;

    /// Constructs a new [`DICompositeType`] from the given `value`.

looks like this comment should go on the trait now.


src/llvm/types/di.rs line 369 at r2 (raw file):

    type Target = LLVMValueRef;

    /// Constructs a new [`DISubprogram`] from the given `value`.

looks like this comment should go on the trait now.


src/llvm/types/ir.rs line 214 at r2 (raw file):

    type Target = LLVMValueRef;

    /// Constructs a new [`MDNode`] from the given `value`.

looks like this comment should go on the trait now.


src/llvm/types/ir.rs line 221 at r2 (raw file):

    /// instance of [LLVM `MDNode`](https://llvm.org/doxygen/classllvm_1_1MDNode.html).
    /// It's the caller's responsibility to ensure this invariant, as this
    /// method doesn't perform any valiation checks.

"valiation" typo throughout


src/llvm/types/ir.rs line 243 at r2 (raw file):

    /// It's the caller's responsibility to ensure this invariant, as this
    /// method doesn't perform any validation checks.
    pub(crate) unsafe fn from_metadata_ref(

could we just inline this method?


src/llvm/types/ir.rs line 251 at r2 (raw file):

    /// Constructs an empty metadata node.
    /// Constructs a new [`MDNode`] from the given `value`.

detritus


src/llvm/types/ir.rs line 333 at r2 (raw file):

    type Target = LLVMValueRef;

    /// Constructs a new [`Function`] from the given `value`.

looks like this comment should go on the trait now.


src/llvm/types/mod.rs line 7 at r2 (raw file):

    type Target: ?Sized;

    unsafe fn from_ptr(ptr: Self::Target) -> Self;

why is it unsafe?

@vadorovsky
Copy link
Member Author

src/llvm/types/mod.rs line 7 at r2 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

why is it unsafe?

The most of pointer types coming from LLVM-C API (through llvm-sys) are more vague than the C++ ones.

There is LLVMValueRef which can be pretty much anything - Value, Instruction, GlobalVariable or any of dozens of classes inheriting from Value. Therefore constructing a wrapper corresponding to a C++ type (e.g. Instruction) from LLVMValueRef is unsafe, unless you perform a direct check.

But good that you asked. I didn't think about it before, but there are functions like IsAMDNode or IsAInstruction for performing such checks. Let me check if they cover all the types. If yes, I could make it method safe and just make sure we have checks everywhere. If no, I would either insist on keeping this unsafe or provide a separate method/trait for types for which the check functions exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants