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

Provide a few new features for C# 10 and .NET 6 #299

Merged
merged 4 commits into from
Nov 24, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ Options:
exclude-enum-operators Bindings for operators over enum types should not be generated. These are largely unnecessary in C# as the operators are available by default.
exclude-fnptr-codegen Generated bindings for latest or preview codegen should not use function pointers.
exclude-funcs-with-body Bindings for functions with bodies should not be generated.
preview-codegen-nint Generated bindings for latest or preview codegen should not use nint or nuint.
exclude-using-statics-for-enums Enum usages should be fully qualified and should not include a corresponding 'using static EnumName;'
explicit-vtbls VTBLs should have an explicit type generated with named fields per entry.
implicit-vtbls VTBLs should be implicit to reduce metadata bloat. This is the current default
Expand All @@ -195,6 +194,7 @@ Options:
generate-tests-xunit Basic tests validating size, blittability, and associated metadata should be generated for XUnit.
generate-aggressive-inlining [MethodImpl(MethodImplOptions.AggressiveInlining)] should be added to generated helper functions.
generate-cpp-attributes [CppAttributeList("")] should be generated to document the encountered C++ attributes.
generate-file-scoped-namespaces Namespaces should be scoped to the file to reduce nesting.
generate-helper-types Code files should be generated for various helper attributes and declared transparent structs.
generate-macro-bindings Bindings for macro-definitions should be generated. This currently only works with value like macros and not function-like ones.
generate-marker-interfaces Bindings for marker interfaces representing native inheritance hierarchies should be generated.
Expand Down
3 changes: 3 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/EnumDesc.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using ClangSharp.Interop;

namespace ClangSharp.Abstractions
Expand All @@ -23,5 +24,7 @@ public bool IsNested
Flags = value ? Flags | EnumFlags.Nested : Flags & ~EnumFlags.Nested;
}
}
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }
}
}
4 changes: 4 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/FieldDesc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using ClangSharp.Interop;

namespace ClangSharp.Abstractions
Expand All @@ -11,7 +12,10 @@ internal struct FieldDesc
public string EscapedName { get; set; }
public int? Offset { get; set; }
public bool NeedsNewKeyword { get; set; }
public bool HasBody { get; set; }
public string InheritedFrom { get; set; }
public CXSourceLocation? Location { get; set; }
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ClangSharp.Abstractions
{
internal struct FunctionOrDelegateDesc<TCustomAttrGeneratorData>
internal struct FunctionOrDelegateDesc
{
public AccessSpecifier AccessSpecifier { get; set; }
public string NativeTypeName { get; set; }
Expand All @@ -18,6 +18,7 @@ internal struct FunctionOrDelegateDesc<TCustomAttrGeneratorData>
public FunctionOrDelegateFlags Flags { get; set; }
public long? VtblIndex { get; set; }
public CXSourceLocation? Location { get; set; }
public bool HasBody { get; set; }

public bool IsVirtual
{
Expand Down Expand Up @@ -228,7 +229,7 @@ public bool IsCxxConstructor
}
}

public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
public TCustomAttrGeneratorData CustomAttrGeneratorData { get; set; }
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ internal partial interface IOutputBuilder
void EndValue(in ValueDesc desc);

void BeginEnum(in EnumDesc desc);
void EndEnum();
void EndEnum(in EnumDesc desc);

void BeginField(in FieldDesc desc);
void WriteFixedCountField(string typeName, string escapedName, string fixedName, string count);
void WriteRegularField(string typeName, string escapedName);
void EndField(bool isBodyless = true);
void EndField(in FieldDesc desc);

void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDelegateDesc<TCustomAttrGeneratorData> info, ref bool isMethodClassUnsafe);
void BeginFunctionOrDelegate(in FunctionOrDelegateDesc info, ref bool isMethodClassUnsafe);
void BeginFunctionInnerPrototype(string escapedName);
void BeginParameter<TCustomAttrGeneratorData>(in ParameterDesc<TCustomAttrGeneratorData> info);
void BeginParameter(in ParameterDesc info);
void BeginParameterDefault();
void EndParameterDefault();
void EndParameter();
void EndParameter(in ParameterDesc info);
void WriteParameterSeparator();
void EndFunctionInnerPrototype();
void BeginConstructorInitializer(string memberRefName, string memberInitName);
Expand All @@ -45,14 +45,14 @@ internal partial interface IOutputBuilder
void BeginInnerFunctionBody();
void EndInnerFunctionBody();
void EndBody(bool isExpressionBody = false);
void EndFunctionOrDelegate(bool isVirtual, bool isBodyless);
void EndFunctionOrDelegate(in FunctionOrDelegateDesc info);

void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGeneratorData> info);
void BeginStruct(in StructDesc info);
void BeginMarkerInterface(string[] baseTypeNames);
void EndMarkerInterface();
void BeginExplicitVtbl();
void EndExplicitVtbl();
void EndStruct();
void EndStruct(in StructDesc info);

void EmitCompatibleCodeSupport();
void EmitFnPtrSupport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace ClangSharp.Abstractions
{
internal struct ParameterDesc<TCustomAttrGeneratorData>
internal struct ParameterDesc
{
public string Type { get; set; }
public string Name { get; set; }
public string NativeTypeName { get; set; }
public IEnumerable<string> CppAttributes { get; set; }
public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
public TCustomAttrGeneratorData CustomAttrGeneratorData { get; set; }
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }
public CXSourceLocation? Location { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ClangSharp.Abstractions
{
internal struct StructDesc<TCustomAttrGeneratorData>
internal struct StructDesc
{
public AccessSpecifier AccessSpecifier { get; set; }
public string EscapedName { get; set; }
Expand Down Expand Up @@ -70,8 +70,8 @@ public bool IsUnion
}
}

public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
public TCustomAttrGeneratorData CustomAttrGeneratorData { get; set; }
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }

public StructLayoutAttribute LayoutAttribute
{
Expand Down
3 changes: 3 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/ValueDesc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using ClangSharp.Interop;

namespace ClangSharp.Abstractions
Expand All @@ -18,5 +19,7 @@ internal struct ValueDesc
public bool IsArray => Flags.HasFlag(ValueFlags.Array);
public bool IsConstant => Flags.HasFlag(ValueFlags.Constant);
public bool IsCopy => Flags.HasFlag(ValueFlags.Copy);
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public void BeginValue(in ValueDesc desc)
WriteSourceLocation(location, false);
}

desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);

var isProperty = false;
var isExpressionBody = false;

Expand Down Expand Up @@ -212,6 +214,8 @@ public void BeginEnum(in EnumDesc desc)
WriteSourceLocation(location, false);
}

desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);

WriteIndented(GetAccessSpecifierString(desc.AccessSpecifier, desc.IsNested));
Write(" enum ");
Write(desc.EscapedName);
Expand All @@ -226,7 +230,7 @@ public void BeginEnum(in EnumDesc desc)
WriteBlockStart();
}

public void EndEnum() => WriteBlockEnd();
public void EndEnum(in EnumDesc desc) => WriteBlockEnd();

public void BeginField(in FieldDesc desc)
{
Expand All @@ -245,6 +249,8 @@ public void BeginField(in FieldDesc desc)
WriteSourceLocation(location, false);
}

desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);

WriteIndented(GetAccessSpecifierString(desc.AccessSpecifier, isNested: true));
Write(' ');

Expand Down Expand Up @@ -281,20 +287,18 @@ public void WriteRegularField(string typeName, string escapedName)
Write(escapedName);
}

public void EndField(bool isBodyless = true)
public void EndField(in FieldDesc desc)
{
if (isBodyless)
if (!desc.HasBody)
{
WriteSemicolon();
WriteNewline();
NeedsNewline = true;
}
}

public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDelegateDesc<TCustomAttrGeneratorData> desc, ref bool isMethodClassUnsafe)
public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isMethodClassUnsafe)
{
desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);

if (desc.IsVirtual)
{
Debug.Assert(!desc.HasFnPtrCodeGen);
Expand Down Expand Up @@ -338,10 +342,12 @@ public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDeleg
}

Write("ExactSpelling = true");
if (desc.SetLastError)

if (desc.SetLastError && !_config.GenerateSetsLastSystemErrorAttribute)
{
Write(", SetLastError = true");
}

WriteLine(")]");
}

Expand All @@ -350,6 +356,12 @@ public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDeleg
WriteSourceLocation(location, false);
}

if (desc.SetLastError && _config.GenerateSetsLastSystemErrorAttribute)
{
WriteIndentedLine("[SetsLastSystemError]");
}
// GenerateSetsLastSystemErrorAttribute

if (desc.IsAggressivelyInlined)
{
AddUsingDirective("System.Runtime.CompilerServices");
Expand All @@ -368,6 +380,8 @@ public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDeleg
AddNativeTypeNameAttribute(desc.NativeTypeName, attributePrefix: "return: ");
}

desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);

if (_isInMarkerInterface)
{
WriteIndentation();
Expand Down Expand Up @@ -466,7 +480,7 @@ public void BeginFunctionInnerPrototype(string escapedName)
Write('(');
}

public void BeginParameter<TCustomAttrGeneratorData>(in ParameterDesc<TCustomAttrGeneratorData> info)
public void BeginParameter(in ParameterDesc info)
{
if (info.NativeTypeName is not null)
{
Expand Down Expand Up @@ -498,7 +512,7 @@ public void EndParameterDefault()
// nop, used only by XML
}

public void EndParameter()
public void EndParameter(in ParameterDesc info)
{
// nop, used only by XML
}
Expand Down Expand Up @@ -572,9 +586,9 @@ public void EndBody(bool isExpressionBody = false)
}
}

public void EndFunctionOrDelegate(bool isVirtual, bool isBodyless)
public void EndFunctionOrDelegate(in FunctionOrDelegateDesc desc)
{
if (isBodyless)
if (!desc.HasBody || desc.IsVirtual)
{
WriteSemicolon();
WriteNewline();
Expand All @@ -583,7 +597,7 @@ public void EndFunctionOrDelegate(bool isVirtual, bool isBodyless)
NeedsNewline = true;
}

public void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGeneratorData> desc)
public void BeginStruct(in StructDesc desc)
{
if (desc.LayoutAttribute is not null)
{
Expand Down Expand Up @@ -624,6 +638,8 @@ public void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGener
WriteSourceLocation(location, false);
}

desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);

WriteIndented(GetAccessSpecifierString(desc.AccessSpecifier, desc.IsNested));
Write(' ');

Expand Down Expand Up @@ -685,7 +701,7 @@ public void EmitFnPtrSupport()

public void EmitSystemSupport() => AddUsingDirective("System");

public void EndStruct() => WriteBlockEnd();
public void EndStruct(in StructDesc desc) => WriteBlockEnd();

public void EndMarkerInterface()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace ClangSharp.CSharp
Expand Down Expand Up @@ -66,11 +67,7 @@ internal sealed partial class CSharpOutputBuilder

public void DecreaseIndentation()
{
if (_indentationLevel == 0)
{
throw new InvalidOperationException();
}

Debug.Assert(_indentationLevel > 0);
_indentationLevel--;
}

Expand Down