forked from MochiLibraries/Biohazrd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSharpLibraryGenerator.ConstantArrayTypeDeclaration.cs
194 lines (174 loc) · 9.21 KB
/
CSharpLibraryGenerator.ConstantArrayTypeDeclaration.cs
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
using static Biohazrd.CSharp.CSharpCodeWriter;
namespace Biohazrd.CSharp
{
partial class CSharpLibraryGenerator
{
private bool HasWrittenAnyConstantArrayTypeDeclarations = false;
private const string ConstantArrayEnumeratorName = "ConstantArrayEnumerator";
private const string ConstantArrayOfPointersEnumeratorName = "ConstantArrayOfPointersEnumerator";
protected override void VisitConstantArrayType(VisitorContext context, ConstantArrayTypeDeclaration declaration)
{
const string element0Name = "Element0";
const string element0PointerName = "Element0Pointer";
// If this is the first constant array type we've written out, write out the enumerator helpers
if (!HasWrittenAnyConstantArrayTypeDeclarations)
{
HasWrittenAnyConstantArrayTypeDeclarations = true;
WriteOutConstantArrayEnumerators();
}
Writer.Using("System"); // IndexOutOfRangeException, IntPtr
Writer.Using("System.Runtime.InteropServices"); // StructLayoutAttribute, FieldOffsetAttribute
Writer.EnsureSeparation();
Writer.WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {declaration.SizeBytes})]");
Writer.WriteLine($"{declaration.Accessibility.ToCSharpKeyword()} unsafe partial struct {SanitizeIdentifier(declaration.Name)}");
using (Writer.Block())
{
// Write out the 0th element reference field
string elementType = GetTypeAsString(context, declaration, declaration.Type);
int elementCount = declaration.ElementCount;
Writer.EnsureSeparation();
Writer.WriteLine($"[FieldOffset(0)] private {elementType} {element0Name};");
// Write out the 0th element pointer getter
// (This assumes that these unmanaged constant arrays are never stored on the managed heap.)
Writer.EnsureSeparation();
Writer.WriteLine($"private {elementType}* {element0PointerName}");
using (Writer.Block())
{
Writer.WriteLine("get");
using (Writer.Block())
{
Writer.WriteLine($"fixed ({elementType}* p{element0Name} = &{element0Name})");
Writer.WriteLine($"{{ return p{element0Name}; }}");
}
}
// Write out the indexer
Writer.EnsureSeparation();
Writer.WriteLine($"public ref {elementType} this[int index]");
using (Writer.Block())
{
Writer.WriteLine("get");
using (Writer.Block())
{
Writer.WriteLine($"if ((uint)index < {elementCount})");
Writer.WriteLine($"{{ return ref {element0PointerName}[index]; }}");
Writer.WriteLine("else");
Writer.WriteLine("{ throw new IndexOutOfRangeException(); }");
}
}
// Write out the length
// This used to be a constant, but C# doesn't let you reference constants as instance members and these infrastructure types shouldn't really ever be referenced directly.
Writer.EnsureSeparation();
Writer.WriteLine($"public int Length => {elementCount};");
// Write out ToString implementaiton
Writer.EnsureSeparation();
Writer.WriteLine($"public override string ToString()");
Writer.WriteLineIndented($"=> $\"{{typeof({elementType})}}[{elementCount}]\";");
// Write out ToArray
Writer.EnsureSeparation();
Writer.WriteLine($"public {elementType}[] ToArray()");
using (Writer.Block())
{
Writer.WriteLine($"{elementType}[] result = new {elementType}[{elementCount}];");
Writer.WriteLine();
Writer.WriteLine($"for (int i = 0; i < {elementCount}; i++)");
Writer.WriteLine("{ result[i] = this[i]; }");
Writer.WriteLine();
Writer.WriteLine("return result;");
}
// Write out AsSpan
// (Can't do this when the element type is a pointer because you can't use pointers as generic type arguments.)
if (declaration.Type is not PointerTypeReference)
{
Writer.EnsureSeparation();
Writer.WriteLine($"public Span<{elementType}> AsSpan()");
Writer.WriteLineIndented($"=> new Span<{elementType}>({element0PointerName}, {elementCount});");
}
// Write out GetEnumerator
// (Can't do this when the element type is a double pointer)
if (declaration.Type is not PointerTypeReference { Inner: PointerTypeReference })
{
string enumeratorType;
if (declaration.Type is PointerTypeReference pointerElementType)
{ enumeratorType = $"{ConstantArrayOfPointersEnumeratorName}<{GetTypeAsString(context, declaration, pointerElementType.Inner)}>"; }
else
{ enumeratorType = $"{ConstantArrayEnumeratorName}<{elementType}>"; }
Writer.EnsureSeparation();
Writer.WriteLine($"public {enumeratorType} GetEnumerator()");
Writer.WriteLineIndented($"=> new {enumeratorType}({element0PointerName}, {elementCount});");
}
}
}
private void WriteOutConstantArrayEnumerators()
{
const string element0Name = "Element0";
const string countName = "Count";
const string indexName = "Index";
Writer.EnsureSeparation();
Writer.WriteLine($"public unsafe ref struct {ConstantArrayEnumeratorName}<T>");
Writer.WriteLineIndented($"where T : unmanaged");
using (Writer.Block())
{
Writer.WriteLine($"private readonly T* {element0Name};");
Writer.WriteLine($"private readonly int {countName};");
Writer.WriteLine($"private int {indexName};");
Writer.WriteLine();
Writer.WriteLine($"public T Current => (uint){indexName} < {countName} ? {element0Name}[{indexName}] : default;");
Writer.EnsureSeparation();
Writer.WriteLine($"internal {ConstantArrayEnumeratorName}(T* element0, int count)");
using (Writer.Block())
{
Writer.WriteLine($"{element0Name} = element0;");
Writer.WriteLine($"{countName} = count;");
Writer.WriteLine($"{indexName} = -1;");
}
Writer.EnsureSeparation();
Writer.WriteLine("public bool MoveNext()");
using (Writer.Block())
{
Writer.WriteLine($"int index = {indexName} + 1;");
Writer.WriteLine($"if (index < {countName})");
using (Writer.Block())
{
Writer.WriteLine($"{indexName} = index;");
Writer.WriteLine("return true;");
}
Writer.WriteLine();
Writer.WriteLine("return false;");
}
}
Writer.EnsureSeparation();
Writer.WriteLine($"public unsafe ref struct {ConstantArrayOfPointersEnumeratorName}<T>");
Writer.WriteLineIndented($"where T : unmanaged");
using (Writer.Block())
{
Writer.WriteLine($"private readonly T** {element0Name};");
Writer.WriteLine($"private readonly int {countName};");
Writer.WriteLine($"private int {indexName};");
Writer.WriteLine();
Writer.WriteLine($"public T* Current => (uint){indexName} < {countName} ? {element0Name}[{indexName}] : default;");
Writer.EnsureSeparation();
Writer.WriteLine($"internal {ConstantArrayOfPointersEnumeratorName}(T** element0, int count)");
using (Writer.Block())
{
Writer.WriteLine($"{element0Name} = element0;");
Writer.WriteLine($"{countName} = count;");
Writer.WriteLine($"{indexName} = -1;");
}
Writer.EnsureSeparation();
Writer.WriteLine("public bool MoveNext()");
using (Writer.Block())
{
Writer.WriteLine($"int index = {indexName} + 1;");
Writer.WriteLine($"if (index < {countName})");
using (Writer.Block())
{
Writer.WriteLine($"{indexName} = index;");
Writer.WriteLine("return true;");
}
Writer.WriteLine();
Writer.WriteLine("return false;");
}
}
}
}
}