Skip to content

Commit

Permalink
Fix class name mapping for WUX types (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 authored Jul 23, 2024
1 parent fa224c3 commit aa59310
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 65 deletions.
17 changes: 10 additions & 7 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ private static string ToVtableLookupString(ISymbol symbol, List<string> genericA
}
}

private static string GetRuntimeClassName(INamedTypeSymbol type, Func<ISymbol, bool> isWinRTType)
private static string GetRuntimeClassName(
INamedTypeSymbol type,
Func<ISymbol, TypeMapper, bool> isWinRTType,
TypeMapper mapper)
{
if (type == null)
{
Expand All @@ -300,7 +303,7 @@ private static string GetRuntimeClassName(INamedTypeSymbol type, Func<ISymbol, b
{
StringBuilder builder = new();

builder.Append(GetRuntimeClassName(type.OriginalDefinition, isWinRTType));
builder.Append(GetRuntimeClassName(type.OriginalDefinition, isWinRTType, mapper));
builder.Append("<");

bool first = true;
Expand All @@ -311,7 +314,7 @@ private static string GetRuntimeClassName(INamedTypeSymbol type, Func<ISymbol, b
builder.Append(", ");
}

builder.Append(GetRuntimeClassName(genericArg as INamedTypeSymbol, isWinRTType));
builder.Append(GetRuntimeClassName(genericArg as INamedTypeSymbol, isWinRTType, mapper));
first = false;
}

Expand All @@ -331,16 +334,16 @@ private static string GetRuntimeClassName(INamedTypeSymbol type, Func<ISymbol, b
{
return "Int8";
}
else if (GeneratorHelper.MappedCSharpTypes.ContainsKey(metadataName))
else if (mapper.HasMappingForType(metadataName))
{
var mapping = GeneratorHelper.MappedCSharpTypes[metadataName].GetMapping();
var mapping = mapper.GetMappedType(metadataName).GetMapping();
return mapping.Item1 + "." + mapping.Item2;
}
else if (type.SpecialType != SpecialType.None)
{
return type.Name;
}
else if (isWinRTType(type))
else if (isWinRTType(type, mapper))
{
return metadataName;
}
Expand Down Expand Up @@ -475,7 +478,7 @@ internal static VtableAttribute GetVtableAttributeToAdd(
symbol is IArrayTypeSymbol,
isDelegate,
symbol.DeclaredAccessibility == Accessibility.Public,
GetRuntimeClassName(interfaceToUseForRuntimeClassName, type => isWinRTType(type, mapper)));
GetRuntimeClassName(interfaceToUseForRuntimeClassName, isWinRTType, mapper));

void AddGenericInterfaceInstantiation(INamedTypeSymbol iface)
{
Expand Down
58 changes: 0 additions & 58 deletions src/Authoring/WinRT.SourceGenerator/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,63 +964,5 @@ 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) },
};
}
}

0 comments on commit aa59310

Please sign in to comment.