forked from dotnet/LLVMSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLVMTypeRef.cs
More file actions
268 lines (183 loc) · 10 KB
/
Copy pathLLVMTypeRef.cs
File metadata and controls
268 lines (183 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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.Diagnostics;
namespace LLVMSharp.Interop;
public unsafe partial struct LLVMTypeRef(IntPtr handle) : IEquatable<LLVMTypeRef>
{
public IntPtr Handle = handle;
public static LLVMTypeRef BFloat => LLVM.BFloatType();
public static LLVMTypeRef Double => LLVM.DoubleType();
public static LLVMTypeRef Float => LLVM.FloatType();
public static LLVMTypeRef FP128 => LLVM.FP128Type();
public static LLVMTypeRef Half => LLVM.HalfType();
public static LLVMTypeRef Int1 => LLVM.Int1Type();
public static LLVMTypeRef Int8 => LLVM.Int8Type();
public static LLVMTypeRef Int16 => LLVM.Int16Type();
public static LLVMTypeRef Int32 => LLVM.Int32Type();
public static LLVMTypeRef Int64 => LLVM.Int64Type();
public static LLVMTypeRef Label => LLVM.LabelType();
public static LLVMTypeRef PPCFP128 => LLVM.PPCFP128Type();
public static LLVMTypeRef Void => LLVM.VoidType();
public static LLVMTypeRef X86FP80 => LLVM.X86FP80Type();
public static LLVMTypeRef X86AMX => LLVM.X86AMXType();
[DebuggerBrowsable(DebuggerBrowsableState.Never)] // Justification: causes native allocation
public readonly LLVMValueRef AlignOf => (Handle != IntPtr.Zero) ? LLVM.AlignOf(this) : default;
public readonly uint ArrayLength => (Kind == LLVMTypeKind.LLVMArrayTypeKind) ? LLVM.GetArrayLength(this) : default;
public readonly ulong ArrayLength2 => (Kind == LLVMTypeKind.LLVMArrayTypeKind) ? LLVM.GetArrayLength2(this) : default;
public readonly LLVMContextRef Context => (Handle != IntPtr.Zero) ? LLVM.GetTypeContext(this) : default;
public readonly LLVMTypeRef ElementType => (((Kind == LLVMTypeKind.LLVMPointerTypeKind) && (SubtypesCount != 0)) || (Kind == LLVMTypeKind.LLVMArrayTypeKind) || (Kind == LLVMTypeKind.LLVMVectorTypeKind)) ? LLVM.GetElementType(this) : default;
public readonly uint IntWidth => (Kind == LLVMTypeKind.LLVMIntegerTypeKind) ? LLVM.GetIntTypeWidth(this) : default;
public readonly bool IsFunctionVarArg => (Kind == LLVMTypeKind.LLVMFunctionTypeKind) && LLVM.IsFunctionVarArg(this) != 0;
public readonly bool IsOpaqueStruct => (Kind == LLVMTypeKind.LLVMStructTypeKind) && LLVM.IsOpaqueStruct(this) != 0;
public readonly bool IsPackedStruct => (Kind == LLVMTypeKind.LLVMStructTypeKind) && LLVM.IsPackedStruct(this) != 0;
public readonly bool IsSized => (Handle != IntPtr.Zero) && LLVM.TypeIsSized(this) != 0;
public readonly LLVMTypeKind Kind => (Handle != IntPtr.Zero) ? LLVM.GetTypeKind(this) : default;
public readonly uint ParamTypesCount => (Kind == LLVMTypeKind.LLVMFunctionTypeKind) ? LLVM.CountParamTypes(this) : default;
public readonly uint PointerAddressSpace => (Kind == LLVMTypeKind.LLVMPointerTypeKind) ? LLVM.GetPointerAddressSpace(this) : default;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] // Justification: causes native allocation
public readonly LLVMValueRef Poison => (Handle != IntPtr.Zero) ? LLVM.GetPoison(this) : default;
public readonly LLVMTypeRef ReturnType => (Kind == LLVMTypeKind.LLVMFunctionTypeKind) ? LLVM.GetReturnType(this) : default;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] // Justification: causes native allocation
public readonly LLVMValueRef SizeOf => (Handle != IntPtr.Zero) ? LLVM.SizeOf(this) : default;
public readonly uint StructElementTypesCount => (Kind == LLVMTypeKind.LLVMStructTypeKind) ? LLVM.CountStructElementTypes(this) : default;
public readonly string StructName
{
get
{
if (Kind != LLVMTypeKind.LLVMStructTypeKind)
{
return string.Empty;
}
var pStructName = LLVM.GetStructName(this);
if (pStructName == null)
{
return string.Empty;
}
return SpanExtensions.AsString(pStructName);
}
}
public readonly uint SubtypesCount => (Handle != IntPtr.Zero) ? LLVM.GetNumContainedTypes(this) : default;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] // Justification: causes native allocation
public readonly LLVMValueRef Undef => (Handle != IntPtr.Zero) ? LLVM.GetUndef(this) : default;
public readonly uint VectorSize => (Kind == LLVMTypeKind.LLVMVectorTypeKind) ? LLVM.GetVectorSize(this) : default;
public static implicit operator LLVMTypeRef(LLVMOpaqueType* value) => new LLVMTypeRef((IntPtr)value);
public static implicit operator LLVMOpaqueType*(LLVMTypeRef value) => (LLVMOpaqueType*)value.Handle;
public static bool operator ==(LLVMTypeRef left, LLVMTypeRef right) => left.Handle == right.Handle;
public static bool operator !=(LLVMTypeRef left, LLVMTypeRef right) => !(left == right);
public static LLVMTypeRef CreateFunction(LLVMTypeRef ReturnType, LLVMTypeRef[] ParamTypes, bool IsVarArg = false) => CreateFunction(ReturnType, ParamTypes.AsSpan(), IsVarArg);
public static LLVMTypeRef CreateFunction(LLVMTypeRef ReturnType, ReadOnlySpan<LLVMTypeRef> ParamTypes, bool IsVarArg)
{
fixed (LLVMTypeRef* pParamTypes = ParamTypes)
{
return LLVM.FunctionType(ReturnType, (LLVMOpaqueType**)pParamTypes, (uint)ParamTypes.Length, IsVarArg ? 1 : 0);
}
}
public static LLVMTypeRef CreateArray(LLVMTypeRef ElementType, uint ElementCount) => LLVM.ArrayType(ElementType, ElementCount);
public static LLVMTypeRef CreateArray2(LLVMTypeRef ElementType, ulong ElementCount) => LLVM.ArrayType2(ElementType, ElementCount);
public static LLVMTypeRef CreateInt(uint NumBits) => LLVM.IntType(NumBits);
public static LLVMTypeRef CreateIntPtr(LLVMTargetDataRef TD) => LLVM.IntPtrType(TD);
public static LLVMTypeRef CreateIntPtrForAS(LLVMTargetDataRef TD, uint AS) => LLVM.IntPtrTypeForAS(TD, AS);
public static LLVMTypeRef CreatePointer(LLVMTypeRef ElementType, uint AddressSpace) => LLVM.PointerType(ElementType, AddressSpace);
public static LLVMTypeRef CreateStruct(LLVMTypeRef[] ElementTypes, bool Packed) => CreateStruct(ElementTypes.AsSpan(), Packed);
public static LLVMTypeRef CreateStruct(ReadOnlySpan<LLVMTypeRef> ElementTypes, bool Packed)
{
fixed (LLVMTypeRef* pElementTypes = ElementTypes)
{
return LLVM.StructType((LLVMOpaqueType**)pElementTypes, (uint)ElementTypes.Length, Packed ? 1 : 0);
}
}
public static LLVMTypeRef CreateVector(LLVMTypeRef ElementType, uint ElementCount) => LLVM.VectorType(ElementType, ElementCount);
public static LLVMTypeRef CreateScalableVector(LLVMTypeRef ElementType, uint ElementCount) => LLVM.ScalableVectorType(ElementType, ElementCount);
public readonly void Dump() => LLVM.DumpType(this);
public override readonly bool Equals(object? obj) => (obj is LLVMTypeRef other) && Equals(other);
public readonly bool Equals(LLVMTypeRef other) => this == other;
public readonly double GenericValueToFloat(LLVMGenericValueRef GenVal) => LLVM.GenericValueToFloat(this, GenVal);
public override readonly int GetHashCode() => Handle.GetHashCode();
public readonly LLVMTypeRef[] GetParamTypes()
{
if (Kind != LLVMTypeKind.LLVMFunctionTypeKind)
{
return [];
}
var destination = new LLVMTypeRef[ParamTypesCount];
GetParamTypes(destination);
return destination;
}
public readonly void GetParamTypes(Span<LLVMTypeRef> destination)
{
if (Kind != LLVMTypeKind.LLVMFunctionTypeKind)
{
return;
}
ArgumentOutOfRangeException.ThrowIfLessThan((uint)destination.Length, ParamTypesCount);
fixed (LLVMTypeRef* pDest = destination)
{
LLVM.GetParamTypes(this, (LLVMOpaqueType**)pDest);
}
}
public readonly LLVMTypeRef[] GetStructElementTypes()
{
if (Kind != LLVMTypeKind.LLVMStructTypeKind)
{
return [];
}
var destination = new LLVMTypeRef[StructElementTypesCount];
GetStructElementTypes(destination);
return destination;
}
public readonly void GetStructElementTypes(Span<LLVMTypeRef> destination)
{
if (Kind != LLVMTypeKind.LLVMStructTypeKind)
{
return;
}
ArgumentOutOfRangeException.ThrowIfLessThan((uint)destination.Length, StructElementTypesCount);
fixed (LLVMTypeRef* pDest = destination)
{
LLVM.GetStructElementTypes(this, (LLVMOpaqueType**)pDest);
}
}
public readonly LLVMTypeRef[] GetSubtypes()
{
if (Handle == IntPtr.Zero)
{
return [];
}
var destination = new LLVMTypeRef[SubtypesCount];
GetSubtypes(destination);
return destination;
}
public readonly void GetSubtypes(Span<LLVMTypeRef> destination)
{
if (Handle == IntPtr.Zero)
{
return;
}
ArgumentOutOfRangeException.ThrowIfLessThan((uint)destination.Length, SubtypesCount);
fixed (LLVMTypeRef* pArr = destination)
{
LLVM.GetSubtypes(this, (LLVMOpaqueType**)pArr);
}
}
public readonly string PrintToString()
{
var pStr = LLVM.PrintTypeToString(this);
if (pStr == null)
{
return string.Empty;
}
var result = SpanExtensions.AsString(pStr);
LLVM.DisposeMessage(pStr);
return result;
}
public readonly LLVMTypeRef StructGetTypeAtIndex(uint index) => LLVM.StructGetTypeAtIndex(this, index);
public readonly void StructSetBody(LLVMTypeRef[] ElementTypes, bool Packed) => StructSetBody(ElementTypes.AsSpan(), Packed);
public readonly void StructSetBody(ReadOnlySpan<LLVMTypeRef> ElementTypes, bool Packed)
{
fixed (LLVMTypeRef* pElementTypes = ElementTypes)
{
LLVM.StructSetBody(this, (LLVMOpaqueType**)pElementTypes, (uint)ElementTypes.Length, Packed ? 1 : 0);
}
}
public override readonly string ToString() => (Handle != IntPtr.Zero) ? PrintToString() : string.Empty;
}