Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal struct FunctionOrDelegateDesc<TCustomAttrGeneratorData>
public string EscapedName { get; set; }
public string EntryPoint { get; set; }
public string LibraryPath { get; set; }
public string ReturnType { get; set; }
public CallingConvention CallingConvention { get; set; }
public FunctionOrDelegateFlags Flags { get; set; }
public long? VtblIndex { get; set; }
Expand Down Expand Up @@ -197,6 +198,22 @@ public bool? IsStatic
}
}

public bool NeedsReturnFixup
{
get => (Flags & FunctionOrDelegateFlags.NeedsReturnFixup) != 0;
set => Flags = value
? Flags | FunctionOrDelegateFlags.NeedsReturnFixup
: Flags & ~FunctionOrDelegateFlags.NeedsReturnFixup;
}

public bool IsCxxConstructor
{
get => (Flags & FunctionOrDelegateFlags.IsCxxConstructor) != 0;
set => Flags = value
? Flags | FunctionOrDelegateFlags.IsCxxConstructor
: Flags & ~FunctionOrDelegateFlags.IsCxxConstructor;
}

public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
public TCustomAttrGeneratorData CustomAttrGeneratorData { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ internal partial interface IOutputBuilder

void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDelegateDesc<TCustomAttrGeneratorData> info,
ref bool isMethodClassUnsafe);
void WriteReturnType(string typeString);
void BeginFunctionInnerPrototype(string escapedName);
void BeginParameter<TCustomAttrGeneratorData>(in ParameterDesc<TCustomAttrGeneratorData> info);
void BeginParameterDefault();
Expand Down
49 changes: 48 additions & 1 deletion sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using ClangSharp.Interop;

Expand All @@ -12,7 +13,7 @@ internal struct StructDesc<TCustomAttrGeneratorData>
public string EscapedName { get; set; }
public string NativeType { get; set; }
public string NativeInheritance { get; set; }
public StructLayoutAttribute Layout { get; set; }
public LayoutDesc Layout { get; set; }
public Guid? Uuid { get; set; }
public StructFlags Flags { get; set; }
public CXSourceLocation? Location { get; set; }
Expand Down Expand Up @@ -43,7 +44,53 @@ public bool HasVtbl
}
}

public bool IsUnion
{
get => (Flags & StructFlags.IsUnion) != 0;
set => Flags = value ? Flags | StructFlags.IsUnion : Flags & ~StructFlags.IsUnion;
}

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

public StructLayoutAttribute LayoutAttribute
{
get
{
var layout = Layout;

if (IsUnion)
{
Debug.Assert(layout.Kind == LayoutKind.Explicit);

StructLayoutAttribute attribute = new(layout.Kind);

if (layout.Pack < layout.MaxFieldAlignment)
{
attribute.Pack = (int)layout.Pack;
}

return attribute;
}

if (layout.Pack < layout.MaxFieldAlignment)
{
return new StructLayoutAttribute(layout.Kind) {Pack = (int)layout.Pack};
}

return null;
}
}

public struct LayoutDesc
{
public long Alignment32 { get; set; }
public long Alignment64 { get; set; }
public long Size32 { get; set; }
public long Size64 { get; set; }
public long Pack { get; set; }
public long MaxFieldAlignment { get; set; }
public LayoutKind Kind { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace ClangSharp.Abstractions
public enum StructFlags
{
IsUnsafe = 1 << 0,
HasVtbl = 1 << 1
HasVtbl = 1 << 1,
IsUnion = 1 << 2,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDeleg
}
}
}

if (!desc.IsCxxConstructor)
{
Write(desc.ReturnType);
Write(' ');
}
}

private void WriteSourceLocation(CXSourceLocation location, bool inline)
Expand All @@ -304,12 +310,6 @@ private void WriteSourceLocation(CXSourceLocation location, bool inline)
Write(' ');
}

public void WriteReturnType(string typeString)
{
Write(typeString);
Write(' ');
}

public void BeginFunctionInnerPrototype(string escapedName)
{
Write(escapedName);
Expand Down Expand Up @@ -433,15 +433,15 @@ public void EndFunctionOrDelegate(bool isVirtual, bool isBodyless)

public void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGeneratorData> info)
{
if (info.Layout is not null)
if (info.LayoutAttribute is { } attribute)
{
AddUsingDirective("System.Runtime.InteropServices");
WriteIndented("[StructLayout(LayoutKind.");
Write(info.Layout.Value);
if (info.Layout.Pack != default)
Write(attribute.Value);
if (attribute.Pack != default)
{
Write(", Pack = ");
Write(info.Layout.Pack);
Write(attribute.Pack);
}

WriteLine(")]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum FunctionOrDelegateFlags
IsCxxRecordCtxUnsafe = 1 << 9,
IsMemberFunction = 1 << 10,
IsStatic = 1 << 11,
IsNotStatic = 1 << 12
IsNotStatic = 1 << 12,
NeedsReturnFixup = 1 << 13,
IsCxxConstructor = 1 << 14,
}
}
Loading