Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator modifications so the VTable is successfully created #1922

Open
wants to merge 7 commits into
base: user/chenss3/abstractclass
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ private static (VtableAttribute, EquatableArray<VtableAttribute>) GetVtableAttri
{
var isManagedOnlyType = GeneratorHelper.IsManagedOnlyType(compilation);
var isWinRTTypeFunc = GeneratorHelper.IsWinRTType(compilation, checkForComponentTypes);

//This conditional will need to go somewhere else, but it is here for now to show the logic
if (symbol.IsAbstract && isWinRTTypeFunc(symbol, typeMapper))
{
Console.WriteLine($"HelloWorld: Abstract type {symbol} is correctly labeled as detected as abstract");
}

var vtableAttribute = GetVtableAttributeToAdd(symbol, isManagedOnlyType, isWinRTTypeFunc, typeMapper, compilation, false);
if (vtableAttribute != default)
{
Expand Down
78 changes: 76 additions & 2 deletions src/Authoring/WinRT.SourceGenerator/WinRTTypeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,39 @@ void CheckAndMarkSymbolForAttributes(ISymbol symbol)
currentTypeDeclaration.SymbolsWithAttributes.Add(symbol);
}
}
bool IsMatchingMember(ISymbol symbol, ISymbol memberNode)
{
return symbol.Name == memberNode.Name &&
symbol.Kind == memberNode.Kind;
}

Dictionary<ISymbol, List<MethodDefinitionHandle>> GetOrderedMethods(INamedTypeSymbol classSymbol, TypeDeclaration classDeclaration)
{
var methodDefs = classDeclaration.MethodDefinitions;
var baseAbstractClass = classSymbol.BaseType;
if (baseAbstractClass != null && baseAbstractClass.IsAbstract && GeneratorHelper.IsWinRTType(baseAbstractClass, mapper))
{
var projectedAttr = baseAbstractClass.BaseType.GetAttributes().FirstOrDefault(attr => attr.AttributeClass?.Name == "ProjectedRuntimeClassAttribute");

if (projectedAttr != null && projectedAttr.ConstructorArguments.Length == 1 && projectedAttr.ConstructorArguments[0].Value is INamedTypeSymbol projectedType)
{
var orderedMembers = projectedType.GetMembers().ToList();
var orderedMethodDefinitions = classDeclaration.MethodDefinitions
.Where(def => orderedMembers.Any(member => IsMatchingMember(def.Key, member)))
.OrderBy(def => orderedMembers.FindIndex(member => IsMatchingMember(def.Key, member)))
.ToList();

var extraMethodDefinitions = classDeclaration.MethodDefinitions
.Where(def => !orderedMembers.Any(member => IsMatchingMember(def.Key, member)))
.ToList();

orderedMethodDefinitions.AddRange(extraMethodDefinitions);

methodDefs = orderedMethodDefinitions.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, SymbolEqualityComparer.Default);
}
}
return methodDefs;
}

void AddSynthesizedInterface(
TypeDeclaration classDeclaration,
Expand All @@ -2381,9 +2414,13 @@ void AddSynthesizedInterface(
bool hasTypes = false;
INamedTypeSymbol classSymbol = classDeclaration.Node as INamedTypeSymbol;

// If it is the case of our special abstract class, we will order the methods per the order in the projections.
var methodDefs = GetOrderedMethods(classSymbol, classDeclaration);

// Each class member results in some form of method definition,
// so using that to our advantage to get public members.
foreach (var classMember in classDeclaration.MethodDefinitions)

foreach (var classMember in methodDefs)
{
if (interfaceType == SynthesizedInterfaceType.Factory &&
classMember.Key is IMethodSymbol constructorMethod &&
Expand Down Expand Up @@ -2454,7 +2491,12 @@ classMember.Key is IMethodSymbol constructorMethod &&
}

AddDefaultVersionAttribute(typeDefinitionHandle, GetVersion(classSymbol, true));
AddGuidAttribute(typeDefinitionHandle, interfaceName);

if (!GetProjectionsDefaultInterfaceGuid(classSymbol, typeDefinitionHandle, classDeclaration))
{
AddGuidAttribute(typeDefinitionHandle, interfaceName);
}

AddExclusiveToAttribute(typeDefinitionHandle, classSymbol.ToString());
AddOverloadAttributeForInterfaceMethods(typeDeclaration);

Expand Down Expand Up @@ -2716,6 +2758,38 @@ public void FinalizeGeneration()
}
}

public bool GetProjectionsDefaultInterfaceGuid(INamedTypeSymbol symbol, TypeDefinitionHandle handle, TypeDeclaration classDeclaration)
{
Guid guid = new Guid();

var baseAbstractClass = symbol.BaseType;
if (baseAbstractClass != null && baseAbstractClass.IsAbstract && GeneratorHelper.IsWinRTType(baseAbstractClass, mapper))
{
if (classDeclaration.DefaultInterface != null)
{
// Get the guid of the projections class
var projectedAttr = baseAbstractClass.BaseType.GetAttributes().FirstOrDefault(attr => attr.AttributeClass?.Name == "ProjectedRuntimeClassAttribute");

if (projectedAttr != null && projectedAttr.ConstructorArguments.Length == 1 && projectedAttr.ConstructorArguments[0].Value is INamedTypeSymbol projectedType)
{
projectedType.DeclaringSyntaxReferences.FirstOrDefault();
projectedType.TryGetAttributeWithType(Model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.GuidAttribute"), out AttributeData guidAttr);
if (guidAttr != null && guidAttr.ConstructorArguments.Length == 1)
{
string guidString = guidAttr.ConstructorArguments[0].Value as string;
if (!string.IsNullOrEmpty(guidString) && Guid.TryParse(guidString, out Guid parsedGuid))
{
guid = parsedGuid;
AddGuidAttribute(handle, guid);
return true;
}
}
}
}
}

return false;
}
public void GenerateWinRTExposedClassAttributes(GeneratorExecutionContext context)
{
bool IsWinRTType(ISymbol symbol, TypeMapper mapper)
Expand Down
13 changes: 11 additions & 2 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8731,8 +8731,10 @@ bind_list<write_projection_parameter>(", ", signature.params()));
auto type_name = write_type_name_temp(w, type);
auto abstract_type_name = "Abstract" + type_name;
w.write(R"(
%% % class % : % {
%%
% % class % : %, ICustomQueryInterface {
)",
"[global::WinRT.WindowsRuntimeType]",
bind<write_type_custom_attributes>(type, true),
"public",
"abstract",
Expand Down Expand Up @@ -8765,8 +8767,15 @@ public %() : base(WinRT.DerivedComposed.Instance)
abstract_type_name
);
}


w.write(R"(
global::System.Runtime.InteropServices.CustomQueryInterfaceResult global::System.Runtime.InteropServices.ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv)
{
ppv = IntPtr.Zero;
return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.NotHandled;
}
)");

for (auto&& ii : type.InterfaceImpl())
{
auto is_overridable = has_attribute(ii, "Windows.Foundation.Metadata", "OverridableAttribute");
Expand Down