Skip to content
Merged
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
82 changes: 81 additions & 1 deletion sources/LLVMSharp.Interop/Extensions/LLVMBinaryRef.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using System.Runtime.InteropServices;

namespace LLVMSharp.Interop;

public unsafe partial struct LLVMBinaryRef(IntPtr handle) : IEquatable<LLVMBinaryRef>
public unsafe partial struct LLVMBinaryRef(IntPtr handle) : IDisposable, IEquatable<LLVMBinaryRef>
{
public IntPtr Handle = handle;

public readonly LLVMBinaryType BinaryType => (Handle != IntPtr.Zero) ? LLVM.BinaryGetType(this) : default;

public static implicit operator LLVMBinaryRef(LLVMOpaqueBinary* Comdat) => new LLVMBinaryRef((IntPtr)Comdat);

public static implicit operator LLVMOpaqueBinary*(LLVMBinaryRef Comdat) => (LLVMOpaqueBinary*)Comdat.Handle;
Expand All @@ -16,11 +19,88 @@ public unsafe partial struct LLVMBinaryRef(IntPtr handle) : IEquatable<LLVMBinar

public static bool operator !=(LLVMBinaryRef left, LLVMBinaryRef right) => !(left == right);

public static LLVMBinaryRef Create(LLVMMemoryBufferRef MemBuf, LLVMContextRef Context)
{
if (!TryCreate(MemBuf, Context, out LLVMBinaryRef Binary, out string Message))
{
throw new ExternalException(Message);
}

return Binary;
}

public readonly LLVMMemoryBufferRef CopyMemoryBuffer() => (Handle != IntPtr.Zero) ? LLVM.BinaryCopyMemoryBuffer(this) : default;

public readonly LLVMSectionIteratorRef CopySectionIterator() => (Handle != IntPtr.Zero) ? LLVM.ObjectFileCopySectionIterator(this) : default;

public readonly LLVMSymbolIteratorRef CopySymbolIterator() => (Handle != IntPtr.Zero) ? LLVM.ObjectFileCopySymbolIterator(this) : default;

public void Dispose()
{
if (Handle != IntPtr.Zero)
{
LLVM.DisposeBinary(this);
Handle = IntPtr.Zero;
}
}

public override readonly bool Equals(object? obj) => (obj is LLVMBinaryRef other) && Equals(other);

public readonly bool Equals(LLVMBinaryRef other) => this == other;

public override readonly int GetHashCode() => Handle.GetHashCode();

public readonly bool IsSectionIteratorAtEnd(LLVMSectionIteratorRef SI) => (Handle != IntPtr.Zero) && LLVM.ObjectFileIsSectionIteratorAtEnd(this, SI) != 0;

public readonly bool IsSymbolIteratorAtEnd(LLVMSymbolIteratorRef SI) => (Handle != IntPtr.Zero) && LLVM.ObjectFileIsSymbolIteratorAtEnd(this, SI) != 0;

public readonly LLVMBinaryRef MachOUniversalBinaryCopyObjectForArch(ReadOnlySpan<char> Arch)
{
if (!TryMachOUniversalBinaryCopyObjectForArch(Arch, out LLVMBinaryRef Binary, out string Message))
{
throw new ExternalException(Message);
}

return Binary;
}

public override readonly string ToString() => $"{nameof(LLVMBinaryRef)}: {Handle:X}";

public static bool TryCreate(LLVMMemoryBufferRef MemBuf, LLVMContextRef Context, out LLVMBinaryRef OutBinary, out string OutMessage)
{
sbyte* pMessage = null;
OutBinary = LLVM.CreateBinary(MemBuf, Context, &pMessage);

if (pMessage == null)
{
OutMessage = string.Empty;
}
else
{
OutMessage = SpanExtensions.AsString(pMessage);
LLVM.DisposeMessage(pMessage);
}

return OutBinary.Handle != IntPtr.Zero;
}

public readonly bool TryMachOUniversalBinaryCopyObjectForArch(ReadOnlySpan<char> Arch, out LLVMBinaryRef OutBinary, out string OutMessage)
{
using var marshaledArch = new MarshaledString(Arch);

sbyte* pMessage = null;
OutBinary = LLVM.MachOUniversalBinaryCopyObjectForArch(this, marshaledArch, (nuint)marshaledArch.Length, &pMessage);

if (pMessage == null)
{
OutMessage = string.Empty;
}
else
{
OutMessage = SpanExtensions.AsString(pMessage);
LLVM.DisposeMessage(pMessage);
}

return OutBinary.Handle != IntPtr.Zero;
}
}
102 changes: 101 additions & 1 deletion sources/LLVMSharp.Interop/Extensions/LLVMMemoryBufferRef.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using System.Runtime.InteropServices;

namespace LLVMSharp.Interop;

public unsafe partial struct LLVMMemoryBufferRef(IntPtr handle) : IEquatable<LLVMMemoryBufferRef>
public unsafe partial struct LLVMMemoryBufferRef(IntPtr handle) : IDisposable, IEquatable<LLVMMemoryBufferRef>
{
public IntPtr Handle = handle;

public readonly UIntPtr BufferSize => (Handle != IntPtr.Zero) ? LLVM.GetBufferSize(this) : default;

public readonly IntPtr BufferStart => (Handle != IntPtr.Zero) ? (IntPtr)LLVM.GetBufferStart(this) : IntPtr.Zero;

public static implicit operator LLVMMemoryBufferRef(LLVMOpaqueMemoryBuffer* MemoryBuffer) => new LLVMMemoryBufferRef((IntPtr)MemoryBuffer);

public static implicit operator LLVMOpaqueMemoryBuffer*(LLVMMemoryBufferRef MemoryBuffer) => (LLVMOpaqueMemoryBuffer*)MemoryBuffer.Handle;
Expand All @@ -16,11 +21,106 @@ public unsafe partial struct LLVMMemoryBufferRef(IntPtr handle) : IEquatable<LLV

public static bool operator !=(LLVMMemoryBufferRef left, LLVMMemoryBufferRef right) => !(left == right);

public static LLVMMemoryBufferRef CreateWithContentsOfFile(string Path) => CreateWithContentsOfFile(Path.AsSpan());

public static LLVMMemoryBufferRef CreateWithContentsOfFile(ReadOnlySpan<char> Path)
{
if (!TryCreateWithContentsOfFile(Path, out LLVMMemoryBufferRef MemBuf, out string Message))
{
throw new ExternalException(Message);
}

return MemBuf;
}

public static LLVMMemoryBufferRef CreateWithMemoryRange(ReadOnlySpan<byte> InputData, ReadOnlySpan<char> BufferName, bool RequiresNullTerminator)
{
using var marshaledBufferName = new MarshaledString(BufferName);

fixed (byte* pInputData = InputData)
{
return LLVM.CreateMemoryBufferWithMemoryRange((sbyte*)pInputData, (nuint)InputData.Length, marshaledBufferName, RequiresNullTerminator ? 1 : 0);
}
}

public static LLVMMemoryBufferRef CreateWithMemoryRangeCopy(ReadOnlySpan<byte> InputData, ReadOnlySpan<char> BufferName)
{
using var marshaledBufferName = new MarshaledString(BufferName);

fixed (byte* pInputData = InputData)
{
return LLVM.CreateMemoryBufferWithMemoryRangeCopy((sbyte*)pInputData, (nuint)InputData.Length, marshaledBufferName);
}
}

public static LLVMMemoryBufferRef CreateWithSTDIN()
{
if (!TryCreateWithSTDIN(out LLVMMemoryBufferRef MemBuf, out string Message))
{
throw new ExternalException(Message);
}

return MemBuf;
}

public void Dispose()
{
if (Handle != IntPtr.Zero)
{
LLVM.DisposeMemoryBuffer(this);
Handle = IntPtr.Zero;
}
}

public override readonly bool Equals(object? obj) => (obj is LLVMMemoryBufferRef other) && Equals(other);

public readonly bool Equals(LLVMMemoryBufferRef other) => this == other;

public override readonly int GetHashCode() => Handle.GetHashCode();

public override readonly string ToString() => $"{nameof(LLVMMemoryBufferRef)}: {Handle:X}";

public static bool TryCreateWithContentsOfFile(ReadOnlySpan<char> Path, out LLVMMemoryBufferRef OutMemBuf, out string OutMessage)
{
using var marshaledPath = new MarshaledString(Path);

fixed (LLVMMemoryBufferRef* pOutMemBuf = &OutMemBuf)
{
sbyte* pMessage = null;
var result = LLVM.CreateMemoryBufferWithContentsOfFile(marshaledPath, (LLVMOpaqueMemoryBuffer**)pOutMemBuf, &pMessage);

if (pMessage == null)
{
OutMessage = string.Empty;
}
else
{
OutMessage = SpanExtensions.AsString(pMessage);
LLVM.DisposeMessage(pMessage);
}

return result == 0;
}
}

public static bool TryCreateWithSTDIN(out LLVMMemoryBufferRef OutMemBuf, out string OutMessage)
{
fixed (LLVMMemoryBufferRef* pOutMemBuf = &OutMemBuf)
{
sbyte* pMessage = null;
var result = LLVM.CreateMemoryBufferWithSTDIN((LLVMOpaqueMemoryBuffer**)pOutMemBuf, &pMessage);

if (pMessage == null)
{
OutMessage = string.Empty;
}
else
{
OutMessage = SpanExtensions.AsString(pMessage);
LLVM.DisposeMessage(pMessage);
}

return result == 0;
}
}
}
19 changes: 18 additions & 1 deletion sources/LLVMSharp.Interop/Extensions/LLVMObjectFileRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace LLVMSharp.Interop;

public unsafe partial struct LLVMObjectFileRef(IntPtr handle) : IEquatable<LLVMObjectFileRef>
public unsafe partial struct LLVMObjectFileRef(IntPtr handle) : IDisposable, IEquatable<LLVMObjectFileRef>
{
public IntPtr Handle = handle;

Expand All @@ -16,11 +16,28 @@ public unsafe partial struct LLVMObjectFileRef(IntPtr handle) : IEquatable<LLVMO

public static bool operator !=(LLVMObjectFileRef left, LLVMObjectFileRef right) => !(left == right);

public void Dispose()
{
if (Handle != IntPtr.Zero)
{
LLVM.DisposeObjectFile(this);
Handle = IntPtr.Zero;
}
}

public override readonly bool Equals(object? obj) => (obj is LLVMObjectFileRef other) && Equals(other);

public readonly bool Equals(LLVMObjectFileRef other) => this == other;

public override readonly int GetHashCode() => Handle.GetHashCode();

public readonly LLVMSectionIteratorRef GetSections() => (Handle != IntPtr.Zero) ? LLVM.GetSections(this) : default;

public readonly LLVMSymbolIteratorRef GetSymbols() => (Handle != IntPtr.Zero) ? LLVM.GetSymbols(this) : default;

public readonly bool IsSectionIteratorAtEnd(LLVMSectionIteratorRef SI) => (Handle != IntPtr.Zero) && LLVM.IsSectionIteratorAtEnd(this, SI) != 0;

public readonly bool IsSymbolIteratorAtEnd(LLVMSymbolIteratorRef SI) => (Handle != IntPtr.Zero) && LLVM.IsSymbolIteratorAtEnd(this, SI) != 0;

public override readonly string ToString() => $"{nameof(LLVMObjectFileRef)}: {Handle:X}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,44 @@

namespace LLVMSharp.Interop;

public unsafe partial struct LLVMRelocationIteratorRef(IntPtr handle) : IEquatable<LLVMRelocationIteratorRef>
public unsafe partial struct LLVMRelocationIteratorRef(IntPtr handle) : IDisposable, IEquatable<LLVMRelocationIteratorRef>
{
public IntPtr Handle = handle;

public readonly ulong Offset => (Handle != IntPtr.Zero) ? LLVM.GetRelocationOffset(this) : default;

public readonly LLVMSymbolIteratorRef Symbol => (Handle != IntPtr.Zero) ? LLVM.GetRelocationSymbol(this) : default;

public readonly ulong Type => (Handle != IntPtr.Zero) ? LLVM.GetRelocationType(this) : default;

public readonly string TypeName
{
get
{
if (Handle == IntPtr.Zero)
{
return string.Empty;
}

var pTypeName = LLVM.GetRelocationTypeName(this);
return (pTypeName != null) ? SpanExtensions.AsString(pTypeName) : string.Empty;
}
}

public readonly string ValueString
{
get
{
if (Handle == IntPtr.Zero)
{
return string.Empty;
}

var pValueString = LLVM.GetRelocationValueString(this);
return (pValueString != null) ? SpanExtensions.AsString(pValueString) : string.Empty;
}
}

public static implicit operator LLVMRelocationIteratorRef(LLVMOpaqueRelocationIterator* value) => new LLVMRelocationIteratorRef((IntPtr)value);

public static implicit operator LLVMOpaqueRelocationIterator*(LLVMRelocationIteratorRef value) => (LLVMOpaqueRelocationIterator*)value.Handle;
Expand All @@ -16,11 +50,28 @@ public unsafe partial struct LLVMRelocationIteratorRef(IntPtr handle) : IEquatab

public static bool operator !=(LLVMRelocationIteratorRef left, LLVMRelocationIteratorRef right) => !(left == right);

public void Dispose()
{
if (Handle != IntPtr.Zero)
{
LLVM.DisposeRelocationIterator(this);
Handle = IntPtr.Zero;
}
}

public override readonly bool Equals(object? obj) => (obj is LLVMRelocationIteratorRef other) && Equals(other);

public readonly bool Equals(LLVMRelocationIteratorRef other) => this == other;

public override readonly int GetHashCode() => Handle.GetHashCode();

public readonly void MoveToNextRelocation()
{
if (Handle != IntPtr.Zero)
{
LLVM.MoveToNextRelocation(this);
}
}

public override readonly string ToString() => $"{nameof(LLVMRelocationIteratorRef)}: {Handle:X}";
}
Loading
Loading