Skip to content

Commit

Permalink
Merge pull request #218 from mrahhal/add-param-attr
Browse files Browse the repository at this point in the history
Add missing unmanaged wrappers related to attributes
  • Loading branch information
tannergooding authored Aug 16, 2023
2 parents ea05882 + 58ed9ba commit 482af28
Show file tree
Hide file tree
Showing 5 changed files with 128 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
101 changes: 101 additions & 0 deletions sources/LLVMSharp/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,104 @@ public sealed class Attribute : IEquatable<Attribute>

public override string ToString() => Handle.ToString();
}

public enum AttributeKind
{
#pragma warning disable CA1069 // Enums values should not be duplicated
None,

FirstEnumAttr = 1,
AllocAlign = 1,
AllocatedPointer = 2,
AlwaysInline = 3,
Builtin = 4,
Cold = 5,
Convergent = 6,
DisableSanitizerInstrumentation = 7,
FnRetThunkExtern = 8,
Hot = 9,
ImmArg = 10,
InReg = 11,
InlineHint = 12,
JumpTable = 13,
MinSize = 14,
MustProgress = 15,
Naked = 16,
Nest = 17,
NoAlias = 18,
NoBuiltin = 19,
NoCallback = 20,
NoCapture = 21,
NoCfCheck = 22,
NoDuplicate = 23,
NoFree = 24,
NoImplicitFloat = 25,
NoInline = 26,
NoMerge = 27,
NoProfile = 28,
NoRecurse = 29,
NoRedZone = 30,
NoReturn = 31,
NoSanitizeBounds = 32,
NoSanitizeCoverage = 33,
NoSync = 34,
NoUndef = 35,
NoUnwind = 36,
NonLazyBind = 37,
NonNull = 38,
NullPointerIsValid = 39,
OptForFuzzing = 40,
OptimizeForSize = 41,
OptimizeNone = 42,
PresplitCoroutine = 43,
ReadNone = 44,
ReadOnly = 45,
Returned = 46,
ReturnsTwice = 47,
SExt = 48,
SafeStack = 49,
SanitizeAddress = 50,
SanitizeHWAddress = 51,
SanitizeMemTag = 52,
SanitizeMemory = 53,
SanitizeThread = 54,
ShadowCallStack = 55,
SkipProfile = 56,
Speculatable = 57,
SpeculativeLoadHardening = 58,
StackProtect = 59,
StackProtectReq = 60,
StackProtectStrong = 61,
StrictFP = 62,
SwiftAsync = 63,
SwiftError = 64,
SwiftSelf = 65,
WillReturn = 66,
WriteOnly = 67,
ZExt = 68,
LastEnumAttr = 68,
FirstTypeAttr = 69,
ByRef = 69,
ByVal = 70,
ElementType = 71,
InAlloca = 72,
Preallocated = 73,
StructRet = 74,
LastTypeAttr = 74,
FirstIntAttr = 75,
Alignment = 75,
AllocKind = 76,
AllocSize = 77,
Dereferenceable = 78,
DereferenceableOrNull = 79,
Memory = 80,
StackAlignment = 81,
UWTable = 82,
VScaleRange = 83,
LastIntAttr = 83,

EndAttrKinds,
EmptyKey,
TombstoneKey,
#pragma warning restore CA1069 // Enums values should not be duplicated
}
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 482af28

Please sign in to comment.