forked from Space-Station-Multiverse/RobustToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypes.cs
314 lines (263 loc) · 9.04 KB
/
Types.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Robust.Serialization.Generator;
internal static class Types
{
private const string DataDefinitionNamespace = "Robust.Shared.Serialization.Manager.Attributes.DataDefinitionAttribute";
private const string ImplicitDataDefinitionNamespace = "Robust.Shared.Serialization.Manager.Attributes.ImplicitDataDefinitionForInheritorsAttribute";
private const string DataFieldBaseNamespace = "Robust.Shared.Serialization.Manager.Attributes.DataFieldBaseAttribute";
private const string CopyByRefNamespace = "Robust.Shared.Serialization.Manager.Attributes.CopyByRefAttribute";
internal static bool IsPartial(TypeDeclarationSyntax type)
{
return type.Modifiers.IndexOf(SyntaxKind.PartialKeyword) != -1;
}
internal static bool IsDataDefinition(ITypeSymbol? type)
{
if (type == null)
return false;
return HasAttribute(type, DataDefinitionNamespace) ||
IsImplicitDataDefinition(type);
}
internal static bool IsDataField(ISymbol member, out ITypeSymbol type, out AttributeData attribute)
{
// TODO data records and other attributes
if (member is IFieldSymbol field)
{
foreach (var attr in field.GetAttributes())
{
if (attr.AttributeClass != null && Inherits(attr.AttributeClass, DataFieldBaseNamespace))
{
type = field.Type;
attribute = attr;
return true;
}
}
}
else if (member is IPropertySymbol property)
{
foreach (var attr in property.GetAttributes())
{
if (attr.AttributeClass != null && Inherits(attr.AttributeClass, DataFieldBaseNamespace))
{
type = property.Type;
attribute = attr;
return true;
}
}
}
type = null!;
attribute = null!;
return false;
}
internal static bool IsImplicitDataDefinition(ITypeSymbol type)
{
if (HasAttribute(type, ImplicitDataDefinitionNamespace))
return true;
foreach (var baseType in GetBaseTypes(type))
{
if (HasAttribute(baseType, ImplicitDataDefinitionNamespace))
return true;
}
foreach (var @interface in type.AllInterfaces)
{
if (IsImplicitDataDefinitionInterface(@interface))
return true;
}
return false;
}
internal static bool IsImplicitDataDefinitionInterface(ITypeSymbol @interface)
{
if (HasAttribute(@interface, ImplicitDataDefinitionNamespace))
return true;
foreach (var subInterface in @interface.AllInterfaces)
{
if (HasAttribute(subInterface, ImplicitDataDefinitionNamespace))
return true;
}
return false;
}
internal static IEnumerable<ITypeSymbol> GetImplicitDataDefinitionInterfaces(ITypeSymbol type, bool all)
{
var interfaces = all ? type.AllInterfaces : type.Interfaces;
foreach (var @interface in interfaces)
{
if (IsImplicitDataDefinitionInterface(@interface))
yield return @interface;
}
}
internal static bool IsNullableType(ITypeSymbol type)
{
if (type.NullableAnnotation == NullableAnnotation.Annotated)
return true;
if (type.OriginalDefinition.ToDisplayString() == "System.Nullable<T>")
return true;
return false;
}
internal static bool IsNullableValueType(ITypeSymbol type)
{
return type.IsValueType && IsNullableType(type);
}
internal static bool IsMultidimensionalArray(ITypeSymbol type)
{
return type is IArrayTypeSymbol { Rank: > 1 };
}
internal static bool CanBeCopiedByValue(ISymbol member, ITypeSymbol type)
{
if (type.OriginalDefinition.ToDisplayString() == "System.Nullable<T>")
return CanBeCopiedByValue(member, ((INamedTypeSymbol) type).TypeArguments[0]);
if (type.TypeKind == TypeKind.Enum)
return true;
switch (type.SpecialType)
{
case SpecialType.System_Enum:
case SpecialType.System_Boolean:
case SpecialType.System_Char:
case SpecialType.System_SByte:
case SpecialType.System_Byte:
case SpecialType.System_Int16:
case SpecialType.System_UInt16:
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
case SpecialType.System_Decimal:
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_String:
case SpecialType.System_DateTime:
return true;
}
if (HasAttribute(member, CopyByRefNamespace))
return true;
return false;
}
internal static string GetGenericTypeName(ITypeSymbol symbol)
{
var name = symbol.Name;
if (symbol is INamedTypeSymbol { TypeParameters: { Length: > 0 } parameters })
{
name += "<";
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
name += parameter.Name;
if (i < parameters.Length - 1)
{
name += ", ";
}
}
name += ">";
}
return name;
}
internal static string GetPartialTypeDefinitionLine(ITypeSymbol symbol)
{
var access = symbol.DeclaredAccessibility switch
{
Accessibility.Private => "private",
Accessibility.ProtectedAndInternal => "protected internal",
Accessibility.Protected => "protected",
Accessibility.Internal => "internal",
Accessibility.Public => "public",
_ => "public"
};
var typeKeyword = "partial ";
if (symbol.TypeKind == TypeKind.Interface)
{
typeKeyword += "interface";
}
else
{
if (symbol.IsRecord)
{
typeKeyword += symbol.IsValueType ? "record struct" : "record";
}
else
{
typeKeyword += symbol.IsValueType ? "struct" : "class";
}
if (symbol.IsAbstract)
{
typeKeyword = $"abstract {typeKeyword}";
}
}
var typeName = GetGenericTypeName(symbol);
return $"{access} {typeKeyword} {typeName}";
}
internal static bool Inherits(ITypeSymbol type, string parent)
{
foreach (var baseType in GetBaseTypes(type))
{
if (baseType.ToDisplayString() == parent)
return true;
}
return false;
}
internal static bool ImplementsInterface(ITypeSymbol type, string interfaceName)
{
foreach (var interfaceType in type.AllInterfaces)
{
if (interfaceType.ToDisplayString().Contains(interfaceName))
{
return true;
}
}
return false;
}
internal static bool IsReadOnlyMember(ITypeSymbol type, ISymbol member)
{
if (member is IFieldSymbol field)
{
return field.IsReadOnly;
}
else if (member is IPropertySymbol property)
{
if (property.SetMethod == null)
return true;
if (property.SetMethod.IsInitOnly)
return type.IsReferenceType;
return false;
}
return false;
}
internal static bool NeedsEmptyConstructor(ITypeSymbol type)
{
if (type is not INamedTypeSymbol named)
return false;
if (named.InstanceConstructors.Length == 0)
return true;
foreach (var constructor in named.InstanceConstructors)
{
if (constructor.Parameters.Length == 0 &&
!constructor.IsImplicitlyDeclared)
{
return false;
}
}
return true;
}
internal static bool IsVirtualClass(ITypeSymbol type)
{
return type.IsReferenceType && !type.IsSealed && type.TypeKind != TypeKind.Interface;
}
internal static bool HasAttribute(ISymbol symbol, string attributeName)
{
foreach (var attribute in symbol.GetAttributes())
{
if (attribute.AttributeClass?.ToDisplayString() == attributeName)
return true;
}
return false;
}
internal static IEnumerable<ITypeSymbol> GetBaseTypes(ITypeSymbol type)
{
var baseType = type.BaseType;
while (baseType != null)
{
yield return baseType;
baseType = baseType.BaseType;
}
}
}