Skip to content

Commit

Permalink
add missing bindings related to attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrahhal committed Aug 8, 2023
1 parent 0ef1598 commit 58ed9ba
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sources/LLVMSharp.Interop/Extensions/LLVMAttributeRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public LLVMAttributeRef(IntPtr handle)
Handle = handle;
}

public readonly uint Kind => LLVM.GetEnumAttributeKind(this);

public readonly ulong Value => LLVM.GetEnumAttributeValue(this);

public static implicit operator LLVMAttributeRef(LLVMOpaqueAttributeRef* value) => new LLVMAttributeRef((IntPtr)value);

public static implicit operator LLVMOpaqueAttributeRef*(LLVMAttributeRef value) => (LLVMOpaqueAttributeRef*)value.Handle;
Expand Down
5 changes: 5 additions & 0 deletions sources/LLVMSharp.Interop/Extensions/LLVMContextRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public LLVMTypeRef CreateNamedStruct(ReadOnlySpan<char> Name)
return LLVM.StructCreateNamed(this, marshaledName);
}

public LLVMAttributeRef CreateEnumAttribute(uint KindId, ulong Val)
{
return LLVM.CreateEnumAttribute(this, KindId, Val);
}

public void Dispose()
{
if (Handle != IntPtr.Zero)
Expand Down
5 changes: 5 additions & 0 deletions sources/LLVMSharp.Interop/Extensions/LLVMValueRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,11 @@ public string GetAsString(out UIntPtr Length)
}
}

public void AddAttributeAtIndex(LLVMAttributeIndex Idx, LLVMAttributeRef A)
{
LLVM.AddAttributeAtIndex(this, Idx, A);
}

public LLVMAttributeRef[] GetAttributesAtIndex(LLVMAttributeIndex Idx)
{
var Attrs = new LLVMAttributeRef[GetAttributeCountAtIndex(Idx)];
Expand Down
13 changes: 13 additions & 0 deletions tests/LLVMSharp.UnitTests/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@ public void ParamTypesRoundtrip()
var functionType = LLVMTypeRef.CreateFunction(returnType, parameterTypes);
Assert.AreEqual(parameterTypes, functionType.ParamTypes);
}

[Test]
public void AddsAttributeAtIndex()
{
var module = LLVMModuleRef.CreateWithName("Test Module");
var functionType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int8, new[] { LLVMTypeRef.Double });
var functionValue = module.AddFunction("test", functionType);
var attr = module.Context.CreateEnumAttribute((uint)AttributeKind.ByVal, default);
functionValue.AddAttributeAtIndex((LLVMAttributeIndex)1, attr);

var attrs = functionValue.GetAttributesAtIndex((LLVMAttributeIndex)1);
Assert.AreEqual(attrs[0].Kind, (uint)AttributeKind.ByVal);
}
}

0 comments on commit 58ed9ba

Please sign in to comment.