Skip to content

Commit 482af28

Browse files
Merge pull request #218 from mrahhal/add-param-attr
Add missing unmanaged wrappers related to attributes
2 parents ea05882 + 58ed9ba commit 482af28

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

sources/LLVMSharp.Interop/Extensions/LLVMAttributeRef.cs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public LLVMAttributeRef(IntPtr handle)
1313
Handle = handle;
1414
}
1515

16+
public readonly uint Kind => LLVM.GetEnumAttributeKind(this);
17+
18+
public readonly ulong Value => LLVM.GetEnumAttributeValue(this);
19+
1620
public static implicit operator LLVMAttributeRef(LLVMOpaqueAttributeRef* value) => new LLVMAttributeRef((IntPtr)value);
1721

1822
public static implicit operator LLVMOpaqueAttributeRef*(LLVMAttributeRef value) => (LLVMOpaqueAttributeRef*)value.Handle;

sources/LLVMSharp.Interop/Extensions/LLVMContextRef.cs

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public LLVMTypeRef CreateNamedStruct(ReadOnlySpan<char> Name)
8686
return LLVM.StructCreateNamed(this, marshaledName);
8787
}
8888

89+
public LLVMAttributeRef CreateEnumAttribute(uint KindId, ulong Val)
90+
{
91+
return LLVM.CreateEnumAttribute(this, KindId, Val);
92+
}
93+
8994
public void Dispose()
9095
{
9196
if (Handle != IntPtr.Zero)

sources/LLVMSharp.Interop/Extensions/LLVMValueRef.cs

+5
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,11 @@ public string GetAsString(out UIntPtr Length)
953953
}
954954
}
955955

956+
public void AddAttributeAtIndex(LLVMAttributeIndex Idx, LLVMAttributeRef A)
957+
{
958+
LLVM.AddAttributeAtIndex(this, Idx, A);
959+
}
960+
956961
public LLVMAttributeRef[] GetAttributesAtIndex(LLVMAttributeIndex Idx)
957962
{
958963
var Attrs = new LLVMAttributeRef[GetAttributeCountAtIndex(Idx)];

sources/LLVMSharp/Attribute.cs

+101
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,104 @@ public sealed class Attribute : IEquatable<Attribute>
2121

2222
public override string ToString() => Handle.ToString();
2323
}
24+
25+
public enum AttributeKind
26+
{
27+
#pragma warning disable CA1069 // Enums values should not be duplicated
28+
None,
29+
30+
FirstEnumAttr = 1,
31+
AllocAlign = 1,
32+
AllocatedPointer = 2,
33+
AlwaysInline = 3,
34+
Builtin = 4,
35+
Cold = 5,
36+
Convergent = 6,
37+
DisableSanitizerInstrumentation = 7,
38+
FnRetThunkExtern = 8,
39+
Hot = 9,
40+
ImmArg = 10,
41+
InReg = 11,
42+
InlineHint = 12,
43+
JumpTable = 13,
44+
MinSize = 14,
45+
MustProgress = 15,
46+
Naked = 16,
47+
Nest = 17,
48+
NoAlias = 18,
49+
NoBuiltin = 19,
50+
NoCallback = 20,
51+
NoCapture = 21,
52+
NoCfCheck = 22,
53+
NoDuplicate = 23,
54+
NoFree = 24,
55+
NoImplicitFloat = 25,
56+
NoInline = 26,
57+
NoMerge = 27,
58+
NoProfile = 28,
59+
NoRecurse = 29,
60+
NoRedZone = 30,
61+
NoReturn = 31,
62+
NoSanitizeBounds = 32,
63+
NoSanitizeCoverage = 33,
64+
NoSync = 34,
65+
NoUndef = 35,
66+
NoUnwind = 36,
67+
NonLazyBind = 37,
68+
NonNull = 38,
69+
NullPointerIsValid = 39,
70+
OptForFuzzing = 40,
71+
OptimizeForSize = 41,
72+
OptimizeNone = 42,
73+
PresplitCoroutine = 43,
74+
ReadNone = 44,
75+
ReadOnly = 45,
76+
Returned = 46,
77+
ReturnsTwice = 47,
78+
SExt = 48,
79+
SafeStack = 49,
80+
SanitizeAddress = 50,
81+
SanitizeHWAddress = 51,
82+
SanitizeMemTag = 52,
83+
SanitizeMemory = 53,
84+
SanitizeThread = 54,
85+
ShadowCallStack = 55,
86+
SkipProfile = 56,
87+
Speculatable = 57,
88+
SpeculativeLoadHardening = 58,
89+
StackProtect = 59,
90+
StackProtectReq = 60,
91+
StackProtectStrong = 61,
92+
StrictFP = 62,
93+
SwiftAsync = 63,
94+
SwiftError = 64,
95+
SwiftSelf = 65,
96+
WillReturn = 66,
97+
WriteOnly = 67,
98+
ZExt = 68,
99+
LastEnumAttr = 68,
100+
FirstTypeAttr = 69,
101+
ByRef = 69,
102+
ByVal = 70,
103+
ElementType = 71,
104+
InAlloca = 72,
105+
Preallocated = 73,
106+
StructRet = 74,
107+
LastTypeAttr = 74,
108+
FirstIntAttr = 75,
109+
Alignment = 75,
110+
AllocKind = 76,
111+
AllocSize = 77,
112+
Dereferenceable = 78,
113+
DereferenceableOrNull = 79,
114+
Memory = 80,
115+
StackAlignment = 81,
116+
UWTable = 82,
117+
VScaleRange = 83,
118+
LastIntAttr = 83,
119+
120+
EndAttrKinds,
121+
EmptyKey,
122+
TombstoneKey,
123+
#pragma warning restore CA1069 // Enums values should not be duplicated
124+
}

tests/LLVMSharp.UnitTests/Functions.cs

+13
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,17 @@ public void ParamTypesRoundtrip()
1515
var functionType = LLVMTypeRef.CreateFunction(returnType, parameterTypes);
1616
Assert.AreEqual(parameterTypes, functionType.ParamTypes);
1717
}
18+
19+
[Test]
20+
public void AddsAttributeAtIndex()
21+
{
22+
var module = LLVMModuleRef.CreateWithName("Test Module");
23+
var functionType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int8, new[] { LLVMTypeRef.Double });
24+
var functionValue = module.AddFunction("test", functionType);
25+
var attr = module.Context.CreateEnumAttribute((uint)AttributeKind.ByVal, default);
26+
functionValue.AddAttributeAtIndex((LLVMAttributeIndex)1, attr);
27+
28+
var attrs = functionValue.GetAttributesAtIndex((LLVMAttributeIndex)1);
29+
Assert.AreEqual(attrs[0].Kind, (uint)AttributeKind.ByVal);
30+
}
1831
}

0 commit comments

Comments
 (0)