-
Notifications
You must be signed in to change notification settings - Fork 104
/
Helper.cs
760 lines (694 loc) · 33.6 KB
/
Helper.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Generator
{
public static class Helper
{
public static Guid EncodeGuid(byte[] data)
{
if (BitConverter.IsLittleEndian)
{
// swap bytes of int a
byte t = data[0];
data[0] = data[3];
data[3] = t;
t = data[1];
data[1] = data[2];
data[2] = t;
// swap bytes of short b
t = data[4];
data[4] = data[5];
data[5] = t;
// swap bytes of short c and encode rfc time/version field
t = data[6];
data[6] = data[7];
data[7] = (byte)((t & 0x0f) | (5 << 4));
// encode rfc clock/reserved field
data[8] = (byte)((data[8] & 0x3f) | 0x80);
}
return new Guid(data.Take(16).ToArray());
}
}
class AttributeDataComparer : IEqualityComparer<AttributeData>
{
public bool Equals(AttributeData x, AttributeData y)
{
return string.CompareOrdinal(x.ToString(), y.ToString()) == 0;
}
public int GetHashCode(AttributeData obj)
{
return obj.ToString().GetHashCode();
}
}
static class GeneratorExecutionContextHelper
{
public static string GetAssemblyName(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.AssemblyName", out var assemblyName);
return assemblyName;
}
public static string GetAssemblyVersion(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.AssemblyVersion", out var assemblyVersion);
return assemblyVersion;
}
public static string GetGeneratedFilesDir(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTGeneratedFilesDir", out var generatedFilesDir);
Directory.CreateDirectory(generatedFilesDir);
return generatedFilesDir;
}
public static bool IsCsWinRTComponent(this GeneratorExecutionContext context)
{
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTComponent", out var isCsWinRTComponentStr))
{
return bool.TryParse(isCsWinRTComponentStr, out var isCsWinRTComponent) && isCsWinRTComponent;
}
return false;
}
public static bool IsCsWinRTComponent(this AnalyzerConfigOptionsProvider provider)
{
if (provider.GlobalOptions.TryGetValue("build_property.CsWinRTComponent", out var isCsWinRTComponentStr))
{
return bool.TryParse(isCsWinRTComponentStr, out var isCsWinRTComponent) && isCsWinRTComponent;
}
return false;
}
public static bool IsCsWinRTAotOptimizerEnabled(this AnalyzerConfigOptionsProvider provider)
{
if (provider.GlobalOptions.TryGetValue("build_property.CsWinRTAotOptimizerEnabled", out var isCsWinRTAotOptimizerEnabledStr))
{
return bool.TryParse(isCsWinRTAotOptimizerEnabledStr, out var isCsWinRTAotOptimizerEnabled) && isCsWinRTAotOptimizerEnabled;
}
return false;
}
public static bool ShouldGenerateWinMDOnly(this GeneratorExecutionContext context)
{
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTGenerateWinMDOnly", out var CsWinRTGenerateWinMDOnlyStr))
{
return bool.TryParse(CsWinRTGenerateWinMDOnlyStr, out var CsWinRTGenerateWinMDOnly) && CsWinRTGenerateWinMDOnly;
}
return false;
}
public static string GetCsWinRTExe(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTExe", out var cswinrtExe);
return cswinrtExe;
}
public static bool GetKeepGeneratedSources(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTKeepGeneratedSources", out var keepGeneratedSourcesStr);
return keepGeneratedSourcesStr != null && bool.TryParse(keepGeneratedSourcesStr, out var keepGeneratedSources) && keepGeneratedSources;
}
public static string GetCsWinRTWindowsMetadata(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTWindowsMetadata", out var cswinrtWindowsMetadata);
return cswinrtWindowsMetadata;
}
public static string GetCsWinRTDependentMetadata(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTAuthoringInputs", out var winmds);
return winmds;
}
public static string GetWinmdOutputFile(this GeneratorExecutionContext context)
{
var fileName = context.GetAssemblyName();
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTWinMDOutputFile", out var ret))
{
fileName = ret!;
}
return Path.Combine(context.GetGeneratedFilesDir(), fileName + ".winmd");
}
}
static class GeneratorHelper
{
private static bool IsFundamentalType(ISymbol type)
{
if (type is INamedTypeSymbol namedTypeSymbol)
{
switch (namedTypeSymbol.SpecialType)
{
case SpecialType.System_Boolean:
case SpecialType.System_String:
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
case SpecialType.System_Int16:
case SpecialType.System_Int32:
case SpecialType.System_Int64:
case SpecialType.System_Char:
case SpecialType.System_Byte:
case SpecialType.System_Object:
return true;
}
}
return type.ToDisplayString() == "System.Guid";
}
public static bool IsWinRTType(ISymbol type)
{
bool isProjectedType = type.GetAttributes().
Any(attribute => string.CompareOrdinal(attribute.AttributeClass.Name, "WindowsRuntimeTypeAttribute") == 0) ||
IsFundamentalType(type);
if (!isProjectedType & type.ContainingNamespace != null)
{
isProjectedType = MappedCSharpTypes.ContainsKey(string.Join(".", type.ContainingNamespace.ToDisplayString(), type.MetadataName));
}
// Ensure all generic parameters are WinRT types.
if (isProjectedType && type is INamedTypeSymbol namedType && namedType.IsGenericType && !namedType.IsDefinition)
{
isProjectedType = namedType.TypeArguments.All(IsWinRTType);
}
return isProjectedType;
}
// Checks if the interface references any internal types (either the interface itself or within its generic types).
public static bool IsInternalInterfaceFromReferences(INamedTypeSymbol iface, IAssemblySymbol currentAssembly)
{
return (iface.DeclaredAccessibility == Accessibility.Internal && !SymbolEqualityComparer.Default.Equals(iface.ContainingAssembly, currentAssembly)) ||
(iface.IsGenericType && iface.TypeArguments.Any(typeArgument => IsInternalInterfaceFromReferences(typeArgument as INamedTypeSymbol, currentAssembly)));
}
// Checks whether the symbol references any generic that hasn't been instantiated.
// For instance, List<T> where T is a generic.
public static bool HasNonInstantiatedGeneric(ITypeSymbol symbol)
{
return symbol is INamedTypeSymbol namedType &&
(namedType.TypeKind == TypeKind.TypeParameter ||
namedType.TypeArguments.Any(argument => argument.TypeKind == TypeKind.TypeParameter));
}
public static bool HasPrivateclass(ITypeSymbol symbol)
{
return symbol is INamedTypeSymbol namedType &&
(namedType.DeclaredAccessibility == Accessibility.Private ||
namedType.TypeArguments.Any(argument => argument.DeclaredAccessibility == Accessibility.Private));
}
public static bool HasWinRTExposedTypeAttribute(ISymbol type)
{
return type.GetAttributes().
Any(attribute => string.CompareOrdinal(attribute.AttributeClass.Name, "WinRTExposedTypeAttribute") == 0);
}
public static bool IsWinRTType(MemberDeclarationSyntax node)
{
bool isProjectedType = node.AttributeLists.SelectMany(list => list.Attributes).
Any(attribute => string.CompareOrdinal(attribute.Name.NormalizeWhitespace().ToFullString(), "WindowsRuntimeTypeAttribute") == 0);
return isProjectedType;
}
private static string GetAbiTypeForFundamentalType(ISymbol type)
{
if (type is INamedTypeSymbol namedTypeSymbol)
{
switch (namedTypeSymbol.SpecialType)
{
case SpecialType.System_Boolean:
return "byte";
case SpecialType.System_String:
return "IntPtr";
case SpecialType.System_Char:
return "ushort";
case SpecialType.System_Object:
return "IntPtr";
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
case SpecialType.System_Int16:
case SpecialType.System_Int32:
case SpecialType.System_Int64:
case SpecialType.System_Byte:
return type.ToDisplayString();
}
}
return type.ToDisplayString();
}
public static bool IsBlittableValueType(ITypeSymbol type)
{
if (!type.IsValueType)
{
return false;
}
if (type.SpecialType != SpecialType.None)
{
switch (type.SpecialType)
{
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
case SpecialType.System_Int16:
case SpecialType.System_Int32:
case SpecialType.System_Int64:
case SpecialType.System_Byte:
case SpecialType.System_SByte:
case SpecialType.System_IntPtr:
case SpecialType.System_UIntPtr:
return true;
default:
return false;
}
}
string customTypeMapKey = string.Join(".", type.ContainingNamespace.ToDisplayString(), type.MetadataName);
if (MappedCSharpTypes.ContainsKey(customTypeMapKey))
{
return MappedCSharpTypes[customTypeMapKey].IsBlittable();
}
if (type.TypeKind == TypeKind.Enum)
{
return true;
}
if (type.TypeKind == TypeKind.Struct)
{
foreach (var typeMember in type.GetMembers())
{
if (typeMember is IFieldSymbol field && !IsBlittableValueType(field.Type))
{
return false;
}
}
}
return true;
}
public static string GetAbiType(ITypeSymbol type)
{
if (IsFundamentalType(type))
{
return GetAbiTypeForFundamentalType(type);
}
var typeStr = type.ToDisplayString();
if (typeStr == "System.Type")
{
return "ABI.System.Type";
}
else if (typeStr.StartsWith("System.Collections.Generic.KeyValuePair<"))
{
return "IntPtr";
}
if (type.IsValueType)
{
string customTypeMapKey = string.Join(".", type.ContainingNamespace.ToDisplayString(), type.MetadataName);
if (MappedCSharpTypes.ContainsKey(customTypeMapKey))
{
string prefix = MappedCSharpTypes[customTypeMapKey].IsBlittable() ? "" : "ABI.";
return prefix + typeStr;
}
var winrtHelperAttribute = type.GetAttributes().
Where(attribute => string.CompareOrdinal(attribute.AttributeClass.Name, "WindowsRuntimeHelperTypeAttribute") == 0).
FirstOrDefault();
if (winrtHelperAttribute != null &&
winrtHelperAttribute.ConstructorArguments.Any())
{
return winrtHelperAttribute.ConstructorArguments[0].Value.ToString();
}
// Handling authoring scenario where Impl type has the attributes and
// if the current component is the one being authored, it may not be
// generated yet to check given it is the same compilation.
else if (!IsBlittableValueType(type))
{
return "ABI." + typeStr;
}
else
{
return typeStr;
}
}
return "IntPtr";
}
public static string GetMarshalerClass(string type, string abiType, TypeKind kind, bool isArray, bool useGenericMarshaler = false)
{
if (type == "System.String" || type == "string")
{
return "MarshalString";
}
else if (type == "System.Type" || type == "Type")
{
if (isArray)
{
return "MarshalNonBlittable<global::System.Type>";
}
else
{
return "global::ABI.System.Type";
}
}
else if (type == "System.Object" || type == "object")
{
return "MarshalInspectable<object>";
}
else if (type.StartsWith("System.Collections.Generic.KeyValuePair<"))
{
return $$"""MarshalInterface<{{type}}>""";
}
else if (kind == TypeKind.Enum)
{
if (isArray)
{
return $$"""MarshalBlittable<{{type}}>""";
}
else
{
return "";
}
}
else if (kind == TypeKind.Struct)
{
if (type == abiType)
{
if (isArray)
{
return $$"""MarshalBlittable<{{type}}>""";
}
else
{
return "";
}
}
else
{
return "global::ABI." + type;
}
}
else if (kind == TypeKind.Interface)
{
return $$"""MarshalInterface<{{type}}>""";
}
else if (kind == TypeKind.Class || kind == TypeKind.Delegate)
{
return useGenericMarshaler ? "MarshalInspectable<object>" : "global::ABI." + type;
}
throw new ArgumentException();
}
public static string GetFromAbiMarshaler(GenericParameter genericParameter)
{
return GetFromAbiMarshaler(
genericParameter.ProjectedType,
genericParameter.AbiType,
genericParameter.TypeKind);
}
public static string GetFromAbiMarshaler(string type, string abiType, TypeKind kind)
{
string marshalerType = GetMarshalerClass(type, abiType, kind, false);
if (kind == TypeKind.Enum || (kind == TypeKind.Struct && type == abiType))
{
return "";
}
else if (type == "bool")
{
return "(bool)";
}
else if (type == "char")
{
return "(char)";
}
else
{
return marshalerType + ".FromAbi";
}
}
public static string GetFromManagedMarshaler(GenericParameter genericParameter)
{
return GetFromManagedMarshaler(
genericParameter.ProjectedType,
genericParameter.AbiType,
genericParameter.TypeKind);
}
public static string GetFromManagedMarshaler(string type, string abiType, TypeKind kind)
{
string marshalerType = GetMarshalerClass(type, abiType, kind, false);
if (kind == TypeKind.Enum || (kind == TypeKind.Struct && type == abiType))
{
return "";
}
else if (type == "bool")
{
return "(byte)";
}
else if (type == "char")
{
return "(ushort)";
}
else
{
return marshalerType + ".FromManaged";
}
}
public static string GetCopyManagedArrayMarshaler(string type, string abiType, TypeKind kind)
{
if (kind == TypeKind.Class || kind == TypeKind.Delegate)
{
// TODO: Classes and delegates are missing CopyManagedArray.
return $$"""Marshaler<{{type}}>""";
}
else
{
return GetMarshalerClass(type, abiType, kind, true);
}
}
public static string GetCreateMarshaler(GenericParameter genericParameter, string arg)
{
return GetCreateMarshaler(
genericParameter.ProjectedType,
genericParameter.AbiType,
genericParameter.TypeKind,
arg);
}
public static string GetCreateMarshaler(string type, string abiType, TypeKind kind, string arg)
{
if (kind == TypeKind.Enum || (kind == TypeKind.Struct && type == abiType) ||
type == "bool" ||
type == "char")
{
return "";
}
else if (type == "System.String" || type == "string")
{
// TODO: Consider switching to pinning
return $$"""__{{arg}} = MarshalString.CreateMarshaler({{arg}});""";
}
else
{
string marshalerClass = GetMarshalerClass(type, abiType, kind, false);
return $$"""__{{arg}} = {{marshalerClass}}.CreateMarshaler2({{arg}});""";
}
}
public static string GetDisposeMarshaler(GenericParameter genericParameter, string arg)
{
return GetDisposeMarshaler(
genericParameter.ProjectedType,
genericParameter.AbiType,
genericParameter.TypeKind,
arg);
}
public static string GetDisposeMarshaler(string type, string abiType, TypeKind kind, string arg)
{
if (kind == TypeKind.Enum || (kind == TypeKind.Struct && type == abiType) ||
type == "bool" ||
type == "char")
{
return "";
}
else
{
string marshalerClass = GetMarshalerClass(type, abiType, kind, false, true);
return $$"""{{marshalerClass}}.DisposeMarshaler(__{{arg}});""";
}
}
public static string GetAbiFromMarshaler(GenericParameter genericParameter, string arg)
{
return GetAbiFromMarshaler(
genericParameter.ProjectedType,
genericParameter.AbiType,
genericParameter.TypeKind,
arg);
}
public static string GetAbiFromMarshaler(string type, string abiType, TypeKind kind, string arg)
{
if (kind == TypeKind.Enum || (kind == TypeKind.Struct && type == abiType))
{
return arg;
}
else if (type == "bool")
{
return $"(byte){arg}";
}
else if (type == "char")
{
return $"(ushort){arg}";
}
else
{
string marshalerClass = GetMarshalerClass(type, abiType, kind, false, true);
return $"{marshalerClass}.GetAbi(__{arg})";
}
}
public static string GetMarshalerDeclaration(GenericParameter genericParameter, string arg)
{
return GetMarshalerDeclaration(
genericParameter.ProjectedType,
genericParameter.AbiType,
genericParameter.TypeKind,
arg);
}
public static string GetMarshalerDeclaration(string type, string abiType, TypeKind kind, string arg)
{
if (kind == TypeKind.Enum || (kind == TypeKind.Struct && type == abiType) ||
type == "bool" ||
type == "char")
{
return "";
}
else
{
return $"{GetAbiMarshalerType(type, abiType, kind, false)} __{arg} = default;";
}
}
public static string GetAbiMarshalerType(string type, string abiType, TypeKind kind, bool isArray)
{
if (type == "System.String" || type == "string")
{
return isArray ? "MarshalString.MarshalerArray" : "MarshalString";
}
else if (type == "System.Type" || type == "Type")
{
if (isArray)
{
return "MarshalNonBlittable<global::System.Type>.MarshalerArray";
}
else
{
return "global::ABI.System.Type.Marshaler";
}
}
else if (type.StartsWith("System.Collections.Generic.KeyValuePair<"))
{
return isArray ? $$"""MarshalInterfaceHelper<{{type}}>.MarshalerArray""" : "ObjectReferenceValue";
}
else if (kind == TypeKind.Enum)
{
return isArray ? $$"""MarshalBlittable<{{type}}>.MarshalerArray""" : type;
}
else if (kind == TypeKind.Struct)
{
if (type == abiType)
{
return isArray ? $$"""MarshalBlittable<{{type}}>.MarshalerArray""" : type;
}
else
{
return isArray ? $$"""MarshalNonBlittable<{{type}}>.MarshalerArray""" : "ABI." + type;
}
}
else if (type == "System.Object" || type == "object" || kind == TypeKind.Class || kind == TypeKind.Interface || kind == TypeKind.Delegate)
{
return isArray ? $$"""MarshalInterfaceHelper<{{type}}>.MarshalerArray""" : "ObjectReferenceValue";
}
throw new ArgumentException();
}
public static string EscapeTypeNameForIdentifier(string typeName)
{
return Regex.Replace(typeName, """[(\ |:<>,\.)]""", "_");
}
public readonly struct MappedType
{
private readonly string @namespace;
private readonly string name;
private readonly string assembly;
private readonly bool isSystemType;
private readonly bool isValueType;
private readonly bool isBlittable;
private readonly Func<ISymbol, (string, string, string, bool, bool)> multipleMappingFunc;
public MappedType(string @namespace, string name, string assembly, bool isValueType = false, bool isBlittable = false)
{
this.@namespace = @namespace;
this.name = name;
this.assembly = assembly;
isSystemType = string.CompareOrdinal(this.assembly, "mscorlib") == 0;
this.isValueType = isValueType;
this.isBlittable = isBlittable;
multipleMappingFunc = null;
}
public MappedType(Func<ISymbol, (string, string, string, bool, bool)> multipleMappingFunc)
{
@namespace = null;
name = null;
assembly = null;
isSystemType = false;
isValueType = false;
this.multipleMappingFunc = multipleMappingFunc;
}
public (string, string, string, bool, bool) GetMapping(ISymbol containingType = null)
{
return multipleMappingFunc != null ?
multipleMappingFunc(containingType) : (@namespace, name, assembly, isSystemType, isValueType);
}
public bool IsBlittable()
{
return isValueType && isBlittable;
}
}
// Based on whether System.Type is used in an attribute declaration or elsewhere, we need to choose the correct custom mapping
// as attributes don't use the TypeName mapping.
internal static (string, string, string, bool, bool) GetSystemTypeCustomMapping(ISymbol containingSymbol)
{
bool isDefinedInAttribute =
containingSymbol != null &&
string.CompareOrdinal((containingSymbol as INamedTypeSymbol).BaseType?.ToString(), "System.Attribute") == 0;
return isDefinedInAttribute ?
("System", "Type", "mscorlib", true, false) :
("Windows.UI.Xaml.Interop", "TypeName", "Windows.Foundation.UniversalApiContract", false, true);
}
// This should be in sync with the reverse mapping from WinRT.Runtime/Projections.cs and cswinrt/helpers.h.
public static readonly Dictionary<string, MappedType> MappedCSharpTypes = new(StringComparer.Ordinal)
{
{ "System.DateTimeOffset", new MappedType("Windows.Foundation", "DateTime", "Windows.Foundation.FoundationContract", true, false) },
{ "System.Exception", new MappedType("Windows.Foundation", "HResult", "Windows.Foundation.FoundationContract", true, false) },
{ "System.EventHandler`1", new MappedType("Windows.Foundation", "EventHandler`1", "Windows.Foundation.FoundationContract") },
{ "System.FlagsAttribute", new MappedType("System", "FlagsAttribute", "mscorlib" ) },
{ "System.IDisposable", new MappedType("Windows.Foundation", "IClosable", "Windows.Foundation.FoundationContract") },
{ "System.IServiceProvider", new MappedType("Microsoft.UI.Xaml", "IXamlServiceProvider", "Microsoft.UI") },
{ "System.Nullable`1", new MappedType("Windows.Foundation", "IReference`1", "Windows.Foundation.FoundationContract" ) },
{ "System.Object", new MappedType("System", "Object", "mscorlib" ) },
{ "System.TimeSpan", new MappedType("Windows.Foundation", "TimeSpan", "Windows.Foundation.FoundationContract", true, false) },
{ "System.Uri", new MappedType("Windows.Foundation", "Uri", "Windows.Foundation.FoundationContract") },
{ "System.ComponentModel.DataErrorsChangedEventArgs", new MappedType("Microsoft.UI.Xaml.Data", "DataErrorsChangedEventArgs", "Microsoft.UI") },
{ "System.ComponentModel.INotifyDataErrorInfo", new MappedType("Microsoft.UI.Xaml.Data", "INotifyDataErrorInfo", "Microsoft.UI") },
{ "System.ComponentModel.INotifyPropertyChanged", new MappedType("Microsoft.UI.Xaml.Data", "INotifyPropertyChanged", "Microsoft.UI") },
{ "System.ComponentModel.PropertyChangedEventArgs", new MappedType("Microsoft.UI.Xaml.Data", "PropertyChangedEventArgs", "Microsoft.UI") },
{ "System.ComponentModel.PropertyChangedEventHandler", new MappedType("Microsoft.UI.Xaml.Data", "PropertyChangedEventHandler", "Microsoft.UI") },
{ "System.Windows.Input.ICommand", new MappedType("Microsoft.UI.Xaml.Input", "ICommand", "Microsoft.UI") },
{ "System.Collections.IEnumerable", new MappedType("Microsoft.UI.Xaml.Interop", "IBindableIterable", "Microsoft.UI") },
{ "System.Collections.IList", new MappedType("Microsoft.UI.Xaml.Interop", "IBindableVector", "Microsoft.UI") },
{ "System.Collections.Specialized.INotifyCollectionChanged", new MappedType("Microsoft.UI.Xaml.Interop", "INotifyCollectionChanged", "Microsoft.UI") },
{ "System.Collections.Specialized.NotifyCollectionChangedAction", new MappedType("Microsoft.UI.Xaml.Interop", "NotifyCollectionChangedAction", "Microsoft.UI") },
{ "System.Collections.Specialized.NotifyCollectionChangedEventArgs", new MappedType("Microsoft.UI.Xaml.Interop", "NotifyCollectionChangedEventArgs", "Microsoft.UI") },
{ "System.Collections.Specialized.NotifyCollectionChangedEventHandler", new MappedType("Microsoft.UI.Xaml.Interop", "NotifyCollectionChangedEventHandler", "Microsoft.UI") },
{ "WinRT.EventRegistrationToken", new MappedType("Windows.Foundation", "EventRegistrationToken", "Windows.Foundation.FoundationContract", true, true) },
{ "System.AttributeTargets", new MappedType("Windows.Foundation.Metadata", "AttributeTargets", "Windows.Foundation.FoundationContract", true, true) },
{ "System.AttributeUsageAttribute", new MappedType("Windows.Foundation.Metadata", "AttributeUsageAttribute", "Windows.Foundation.FoundationContract") },
{ "System.Numerics.Matrix3x2", new MappedType("Windows.Foundation.Numerics", "Matrix3x2", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Numerics.Matrix4x4", new MappedType("Windows.Foundation.Numerics", "Matrix4x4", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Numerics.Plane", new MappedType("Windows.Foundation.Numerics", "Plane", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Numerics.Quaternion", new MappedType("Windows.Foundation.Numerics", "Quaternion", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Numerics.Vector2", new MappedType("Windows.Foundation.Numerics", "Vector2", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Numerics.Vector3", new MappedType("Windows.Foundation.Numerics", "Vector3", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Numerics.Vector4", new MappedType("Windows.Foundation.Numerics", "Vector4", "Windows.Foundation.FoundationContract", true, true) },
{ "System.Type", new MappedType(GetSystemTypeCustomMapping) },
{ "System.Collections.Generic.IEnumerable`1", new MappedType("Windows.Foundation.Collections", "IIterable`1", "Windows.Foundation.FoundationContract") },
{ "System.Collections.Generic.IEnumerator`1", new MappedType("Windows.Foundation.Collections", "IIterator`1", "Windows.Foundation.FoundationContract") },
{ "System.Collections.Generic.KeyValuePair`2", new MappedType("Windows.Foundation.Collections", "IKeyValuePair`2", "Windows.Foundation.FoundationContract") },
{ "System.Collections.Generic.IReadOnlyDictionary`2", new MappedType("Windows.Foundation.Collections", "IMapView`2", "Windows.Foundation.FoundationContract") },
{ "System.Collections.Generic.IDictionary`2", new MappedType("Windows.Foundation.Collections", "IMap`2", "Windows.Foundation.FoundationContract") },
{ "System.Collections.Generic.IReadOnlyList`1", new MappedType("Windows.Foundation.Collections", "IVectorView`1", "Windows.Foundation.FoundationContract") },
{ "System.Collections.Generic.IList`1", new MappedType("Windows.Foundation.Collections", "IVector`1", "Windows.Foundation.FoundationContract") },
{ "Windows.UI.Color", new MappedType("Windows.UI", "Color", "Windows.Foundation.UniversalApiContract", true, true) },
};
}
}