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

Add support for trimmable vtbls, marker interfaces, and SupportedOSPlatformAttribute #287

Merged
merged 3 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ internal partial interface IOutputBuilder
void EndFunctionOrDelegate(bool isVirtual, bool isBodyless);

void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGeneratorData> info);
void BeginMarkerInterface(string[] baseTypeNames);
void EndMarkerInterface();
void BeginExplicitVtbl();
void EndExplicitVtbl();
void EndStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void WriteCustomAttribute(string attribute, Action callback = null)
{
AddUsingDirective("System.Runtime.InteropServices");
}
else if (attribute.StartsWith("SupportedOSPlatform("))
{
AddUsingDirective("System.Runtime.Versioning");
}

if (!_customAttrIsForParameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void BeginField(in FieldDesc desc)
AddNativeTypeNameAttribute(desc.NativeTypeName);
}

if (desc.Location is {} location)
if (desc.Location is { } location)
{
WriteSourceLocation(location, false);
}
Expand Down Expand Up @@ -368,31 +368,37 @@ public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDeleg
AddNativeTypeNameAttribute(desc.NativeTypeName, attributePrefix: "return: ");
}

WriteIndented(GetAccessSpecifierString(desc.AccessSpecifier, isNested: false));
if (_isInMarkerInterface)
{
WriteIndentation();
}
else
{
WriteIndented(GetAccessSpecifierString(desc.AccessSpecifier, isNested: false));
Write(' ');
}

if (!desc.IsMemberFunction)
{
if (desc.IsVirtual)
{
if (desc.IsUnsafe && !desc.IsCxxRecordCtxUnsafe)
{
Write(" unsafe");
Write("unsafe ");
}
Write(" delegate");
Write("delegate ");
}
else if (desc.IsStatic ?? (desc.IsDllImport || !desc.IsCxx))
{
Write(" static");
Write("static ");

if (desc.IsDllImport)
{
Write(" extern");
Write("extern ");
}
}
}

Write(' ');

if (!desc.IsVirtual)
{
//if (NeedsNewKeyword(escapedName, functionDecl.Parameters))
Expand Down Expand Up @@ -628,13 +634,43 @@ public void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGener

Write("partial struct ");
Write(desc.EscapedName);

if (desc.HasVtbl && _config.GenerateMarkerInterfaces)
{
Write(" : ");
Write(desc.EscapedName);
Write(".Interface");
}

WriteNewline();
WriteBlockStart();
}

public void BeginMarkerInterface(string[] baseTypeNames)
{
WriteIndented("public interface Interface");

if (baseTypeNames is not null)
{
Write(" : ");
Write(baseTypeNames[0]);
Write(".Interface");

for (var i = 1; i < baseTypeNames.Length; i++)
{
Write(", ");
Write(baseTypeNames[i]);
Write(".Interface");
}
}

WriteNewline();
WriteBlockStart();
_isInMarkerInterface = true;
}

public void BeginExplicitVtbl()
{
NeedsNewline = true;
WriteIndentedLine("public partial struct Vtbl");
WriteBlockStart();
}
Expand All @@ -651,6 +687,12 @@ public void EmitFnPtrSupport()

public void EndStruct() => WriteBlockEnd();

public void EndMarkerInterface()
{
_isInMarkerInterface = false;
WriteBlockEnd();
}

public void EndExplicitVtbl() => WriteBlockEnd();

public CSharpOutputBuilder BeginCSharpCode() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal sealed partial class CSharpOutputBuilder
private readonly bool _isTestOutput;

private int _indentationLevel;
private bool _isInMarkerInterface;
private readonly MarkerMode _markerMode;
private readonly bool _writeSourceLocation;

Expand Down