-
Notifications
You must be signed in to change notification settings - Fork 878
Expand file tree
/
Copy pathSymbolMatchingHelpers.cs
More file actions
375 lines (335 loc) · 12.4 KB
/
Copy pathSymbolMatchingHelpers.cs
File metadata and controls
375 lines (335 loc) · 12.4 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
namespace Uno.WinAppSDKSyncGenerator.Helpers;
internal static class SymbolMatchingHelpers
{
public static bool AreMatching(ISymbol uapSymbol, ISymbol unoSymbol)
{
if (ShouldSkipSymbol(uapSymbol))
{
return true;
}
if (uapSymbol is IEventSymbol uapEvent)
{
var result = unoSymbol is IEventSymbol unoEvent && AreEventsMatching(uapEvent, unoEvent);
return result;
}
else if (uapSymbol is IFieldSymbol uapField)
{
var result = unoSymbol is IFieldSymbol unoField && AreFieldsMatching(uapField, unoField);
return result;
}
else if (uapSymbol is INamedTypeSymbol uapNamedType)
{
var result = unoSymbol is INamedTypeSymbol unoNamedType &&
(
(AreMatchingCommon(uapNamedType, unoNamedType) && uapNamedType.Name == unoNamedType.Name) ||
// This happens for not implemented symbols that are annotated as nullable.
// Since the compiler can't find the type, it considers it as value type, hence wraps it in Nullable<T>.
unoNamedType.Name == "Nullable"
);
return result;
}
else if (uapSymbol is IPropertySymbol uapProperty)
{
var result = unoSymbol is IPropertySymbol unoProperty && ArePropertiesMatching(uapProperty, unoProperty);
return result;
}
else if (uapSymbol is IMethodSymbol uapMethod)
{
var result = unoSymbol is IMethodSymbol unoMethod && AreMethodsMatching(uapMethod, unoMethod);
return result;
}
else if (uapSymbol is ITypeParameterSymbol uapTypeParameter)
{
var result = unoSymbol is ITypeParameterSymbol unoTypeParameter && AreTypeParametersMatching(uapTypeParameter, unoTypeParameter);
return result;
}
else if (uapSymbol is IArrayTypeSymbol uapArrayTypeSymbol)
{
var result = unoSymbol is IArrayTypeSymbol unoArrayTypeSymbol && AreMatching(uapArrayTypeSymbol.ElementType, unoArrayTypeSymbol.ElementType);
return result;
}
else
{
throw new ArgumentException($"Unexpected symbol '{uapSymbol?.Kind.ToString() ?? "<null>"}'");
}
}
private static bool ShouldSkipSymbol(ISymbol uapSymbol)
{
if (uapSymbol.Name == "TimeSpan" && uapSymbol.ContainingSymbol.Name == "Duration")
{
// field vs property difference between Uno and WinUI.
return true;
}
if (uapSymbol.Name is "Left" or "Top" or "Right" or "Bottom" && uapSymbol.ContainingSymbol.Name == "Thickness")
{
// field vs property difference between Uno and WinUI.
return true;
}
if (uapSymbol.Name is "Value" or "GridUnitType" && uapSymbol.ContainingSymbol.Name == "GridLength")
{
// field vs property difference between Uno and WinUI.
return true;
}
if (uapSymbol.Name is "TopLeft" or "TopRight" or "BottomRight" or "BottomLeft" && uapSymbol.ContainingSymbol.Name == "CornerRadius")
{
// field vs property difference between Uno and WinUI.
return true;
}
if (uapSymbol.ContainingSymbol?.Name is
"ColorKeyFrameCollection" or
"Matrix" or
"KeyTime" or
"PointCollection" or
"RepeatBehavior" or
"Matrix3D" or
"InlineCollection" or
"GridLength" or
"GeneratorPosition" or
"ToggleSwitch")
{
return true;
}
return false;
}
private static bool AreMatchingCommon(ISymbol uapSymbol, ISymbol unoSymbol)
{
if (unoSymbol.Kind == SymbolKind.ErrorType)
{
// Some events are marked not implemented, but used in implemented signatures.
// For example, `HoldingEventHandler` and `UIElement.Holding`.
// When we're matching `UIElement.Holding`, we get here with `HoldingEventHandler`
// being an error symbol.
// TODO: Move such symbols to non-generated files and remove this condition.
return true;
}
if (uapSymbol.Name == "DependencyObject" || uapSymbol.ContainingSymbol.Name == "DependencyObject")
{
// We define DependencyObject as an interface, diverging from UWP.
// This causes checks like IsAbstract to diverge for the type itself
// as well as its members. Ignore them.
return true;
}
if (uapSymbol.Name == "IGeometrySource2D" ||
uapSymbol.Name == "SizeInt32" ||
uapSymbol.Name == "PointInt32")
{
// For some reason, these are marked with Kind=ErrorType.
// This means the matching then fails.
return true;
}
if (uapSymbol.Name == "Transform" && uapSymbol.Kind == SymbolKind.NamedType)
{
// In Uno, it's abstract to force all derived classes to implement a specific method.
// In UWP/WinUI, it's not abstract but it doesn't have any accessible constructors.
// The divergence here shouldn't be problematic/noticeable.
return true;
}
// Skipping accessibility check for now. It causes two issues:
// 1. For some reason, Roslyn is returning private accessibility for some public properties (Specifically, for some interface implementations).
// 2. For types declared without explicit accessibility, it's going to be considered internal (however, the generated file later will have the correct accessibility)
var result = /*uapSymbol.DeclaredAccessibility == unoSymbol.DeclaredAccessibility &&*/
uapSymbol.IsAbstract == unoSymbol.IsAbstract &&
uapSymbol.IsOverride == unoSymbol.IsOverride &&
StripGlobal(uapSymbol.Name) == StripGlobal(unoSymbol.Name) &&
// Temporary skip named type: Until we match seal-ness and static-ness with UWP.
(uapSymbol.IsSealed == unoSymbol.IsSealed || uapSymbol.Kind == SymbolKind.NamedType) &&
(uapSymbol.IsStatic == unoSymbol.IsStatic) &&
uapSymbol.IsVirtual == unoSymbol.IsVirtual;
return result;
}
private static string StripGlobal(string s)
{
if (s.StartsWith("global::", StringComparison.Ordinal))
{
return s.Substring("global::".Length);
}
return s;
}
private static bool AreEventsMatching(IEventSymbol uapEvent, IEventSymbol unoEvent)
{
if (uapEvent.ContainingType.Name == "Window")
{
// TODO: Match API with WinUI.
return true;
}
var result = AreMatchingCommon(uapEvent, unoEvent) && AreMatching(uapEvent.Type, unoEvent.Type);
return result;
}
private static bool AreFieldsMatching(IFieldSymbol uapField, IFieldSymbol unoField) =>
AreMatchingCommon(uapField, unoField) &&
uapField.IsVolatile == unoField.IsVolatile &&
(uapField.ConstantValue?.Equals(unoField.ConstantValue) ?? unoField.ConstantValue == null) &&
uapField.IsConst == unoField.IsConst &&
uapField.IsReadOnly == unoField.IsReadOnly &&
AreMatching(uapField.Type, unoField.Type);
private static bool ArePropertiesMatching(IPropertySymbol uapProperty, IPropertySymbol unoProperty)
{
if (uapProperty.Name == "WindowActivationState" && uapProperty.ContainingType.Name == "WindowActivatedEventArgs")
{
// TODO: Match API with WinUI.
return true;
}
if (uapProperty.Name == "Name" && uapProperty.ContainingType.Name == "FrameworkElement")
{
// TODO: Name shouldn't be virtual.
return true;
}
if (!AreMatchingCommon(uapProperty, unoProperty))
{
return false;
}
// TODO:
//if (uapProperty.IsReadOnly != unoProperty.IsReadOnly)
//{
// return false;
//}
//if (uapProperty.IsWriteOnly != unoProperty.IsWriteOnly)
//{
// return false;
//}
if (uapProperty.IsIndexer != unoProperty.IsIndexer)
{
return false;
}
if (!AreParametersMatching(uapProperty.Parameters, unoProperty.Parameters))
{
return false;
}
// Property type of ContentTemplateRoot is diverging from UWP.
// In Uno we use the native view for each platform Android.Views.View, UIKit.UIView, AppKit.NSView
if (!AreMatching(uapProperty.Type, unoProperty.Type) && uapProperty.Name != "ContentTemplateRoot")
{
return false;
}
return true;
}
private static bool AreMethodsMatching(IMethodSymbol uapMethod, IMethodSymbol unoMethod)
{
if (uapMethod == null)
{
return unoMethod == null;
}
else if (unoMethod == null)
{
return false;
}
if (uapMethod.Name == "ToString" && uapMethod.Parameters.IsEmpty)
{
return true;
}
if (uapMethod.Name is "LoadContent" or "Measure" or "Arrange")
{
return true;
}
return AreMatchingCommon(uapMethod, unoMethod) &&
(uapMethod.AssociatedSymbol != null || AreParametersMatching(uapMethod.Parameters, unoMethod.Parameters)) &&
uapMethod.Arity == unoMethod.Arity &&
uapMethod.IsExtensionMethod == unoMethod.IsExtensionMethod &&
uapMethod.IsReadOnly == unoMethod.IsReadOnly &&
uapMethod.IsVararg == unoMethod.IsVararg &&
uapMethod.MethodKind == unoMethod.MethodKind &&
AreMatching(uapMethod.ReturnType, unoMethod.ReturnType) &&
AreTypeArgumentsMatching(uapMethod.TypeArguments, unoMethod.TypeArguments);
}
private static bool AreTypeParametersMatching(ITypeParameterSymbol uapTypeParameter, ITypeParameterSymbol unoTypeParameter)
{
return AreMatchingCommon(uapTypeParameter, unoTypeParameter) &&
uapTypeParameter.Ordinal == unoTypeParameter.Ordinal &&
uapTypeParameter.Variance == unoTypeParameter.Variance &&
uapTypeParameter.TypeParameterKind == unoTypeParameter.TypeParameterKind &&
uapTypeParameter.HasReferenceTypeConstraint == unoTypeParameter.HasReferenceTypeConstraint &&
uapTypeParameter.HasValueTypeConstraint == unoTypeParameter.HasValueTypeConstraint &&
uapTypeParameter.HasUnmanagedTypeConstraint == unoTypeParameter.HasUnmanagedTypeConstraint &&
uapTypeParameter.HasNotNullConstraint == unoTypeParameter.HasNotNullConstraint &&
uapTypeParameter.HasConstructorConstraint == unoTypeParameter.HasConstructorConstraint;
}
private static bool AreParametersMatching(ImmutableArray<IParameterSymbol> uapParameters, ImmutableArray<IParameterSymbol> unoParameters)
{
if (uapParameters.Length != unoParameters.Length)
{
return false;
}
for (int i = 0; i < uapParameters.Length; i++)
{
if (!AreParametersMatching(uapParameters[i], unoParameters[i]))
{
return false;
}
}
return true;
}
private static bool AreTypeArgumentsMatching(ImmutableArray<ITypeSymbol> uapTypeArguments, ImmutableArray<ITypeSymbol> unoTypeArguments)
{
if (uapTypeArguments.Length != unoTypeArguments.Length)
{
return false;
}
for (int i = 0; i < uapTypeArguments.Length; i++)
{
if (uapTypeArguments[i].Name != unoTypeArguments[i].Name)
{
return false;
}
}
return true;
}
private static bool AreParametersMatching(IParameterSymbol uapParameters, IParameterSymbol unoParameters) =>
uapParameters.IsOptional == unoParameters.IsOptional &&
(uapParameters.Name == unoParameters.Name || IgnoreParameterName(uapParameters)) &&
uapParameters.IsParams == unoParameters.IsParams &&
(uapParameters.RefKind == unoParameters.RefKind || IgnoreRefKind(uapParameters)) &&
AreMatching(uapParameters.Type, unoParameters.Type);
private static bool IgnoreRefKind(IParameterSymbol uapParameter)
{
if (uapParameter.ContainingSymbol.Name == "Equals" && uapParameter.ContainingType.Name == "GuidHelper")
{
// GuidHelpers.Equals uses "in" RefKind in WinUI, while it uses "ref" RefKind in UWP.
// In Uno, we use "ref" in both flavors.
return true;
}
return false;
}
private static bool IgnoreParameterName(IParameterSymbol uapParameter)
{
var name = uapParameter.Name;
if (name.StartsWith('_'))
{
// The '_' check is to ignore parameter name differences for badly named WinUI parameters.
// For example, FontWeight constructor parameter in WinUI is called "_Weight", while we name it "weight"
// Our naming makes more sense, and we want to avoid this breaking change for now.
// We can revisit in the future if we want to match WinUI naming.
return true;
}
else if (name == "windowsruntimeStream")
{
// In Uno, we name it windowsRuntimeStream.
// Skip for now to avoid breaking changes.
return true;
}
else if (uapParameter.ContainingSymbol.Name == "Equals")
{
// Some object.Equals overrides in WinUI use parameter name as "o" while uno uses "obj".
// There is also CornerRadius.Equals where we name the parameter as "other" while WinUI name it "cornerRadius"
// Also, Duration.Equals(Duration,Duration) in Uno names the parameters as "first"/"second" while WinUI name it as "t1"/"t2"
// Skip for now to avoid breaking changes.
return true;
}
else if (uapParameter.ContainingSymbol.Name == "Compare" && uapParameter.ContainingType.Name == "Duration")
{
// Duration.Compare(Duration,Duration) in Uno names the parameters as "first"/"second" while WinUI name it as "t1"/"t2"
// Skip for now to avoid breaking changes.
return true;
}
else if (name == "location" && uapParameter.ContainingType.Name == "Rect")
{
// Rect(Point,Size) constructor names the Point parameter as location in WinUI while we name it point in Uno.
// Skip for now to avoid breaking changes.
return true;
}
return false;
}
}