Skip to content

Commit 6865a2f

Browse files
Provide more information in Desc structures (#228)
Drop the only use of OutputMode in PInvokeGenerator binding generation code. This is no place for hardcoding IOutputBuilder-specific logic. Co-authored-by: Tanner Gooding <[email protected]>
1 parent 6dd2e4e commit 6865a2f

File tree

8 files changed

+169
-94
lines changed

8 files changed

+169
-94
lines changed

sources/ClangSharp.PInvokeGenerator/Abstractions/FunctionOrDelegateDesc.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal struct FunctionOrDelegateDesc<TCustomAttrGeneratorData>
1313
public string EscapedName { get; set; }
1414
public string EntryPoint { get; set; }
1515
public string LibraryPath { get; set; }
16+
public string ReturnType { get; set; }
1617
public CallingConvention CallingConvention { get; set; }
1718
public FunctionOrDelegateFlags Flags { get; set; }
1819
public long? VtblIndex { get; set; }
@@ -197,6 +198,22 @@ public bool? IsStatic
197198
}
198199
}
199200

201+
public bool NeedsReturnFixup
202+
{
203+
get => (Flags & FunctionOrDelegateFlags.NeedsReturnFixup) != 0;
204+
set => Flags = value
205+
? Flags | FunctionOrDelegateFlags.NeedsReturnFixup
206+
: Flags & ~FunctionOrDelegateFlags.NeedsReturnFixup;
207+
}
208+
209+
public bool IsCxxConstructor
210+
{
211+
get => (Flags & FunctionOrDelegateFlags.IsCxxConstructor) != 0;
212+
set => Flags = value
213+
? Flags | FunctionOrDelegateFlags.IsCxxConstructor
214+
: Flags & ~FunctionOrDelegateFlags.IsCxxConstructor;
215+
}
216+
200217
public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
201218
public TCustomAttrGeneratorData CustomAttrGeneratorData { get; set; }
202219
}

sources/ClangSharp.PInvokeGenerator/Abstractions/IOutputBuilder.VisitDecl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ internal partial interface IOutputBuilder
3131

3232
void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDelegateDesc<TCustomAttrGeneratorData> info,
3333
ref bool isMethodClassUnsafe);
34-
void WriteReturnType(string typeString);
3534
void BeginFunctionInnerPrototype(string escapedName);
3635
void BeginParameter<TCustomAttrGeneratorData>(in ParameterDesc<TCustomAttrGeneratorData> info);
3736
void BeginParameterDefault();

sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 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.
22

33
using System;
4+
using System.Diagnostics;
45
using System.Runtime.InteropServices;
56
using ClangSharp.Interop;
67

@@ -12,7 +13,7 @@ internal struct StructDesc<TCustomAttrGeneratorData>
1213
public string EscapedName { get; set; }
1314
public string NativeType { get; set; }
1415
public string NativeInheritance { get; set; }
15-
public StructLayoutAttribute Layout { get; set; }
16+
public LayoutDesc Layout { get; set; }
1617
public Guid? Uuid { get; set; }
1718
public StructFlags Flags { get; set; }
1819
public CXSourceLocation? Location { get; set; }
@@ -43,7 +44,53 @@ public bool HasVtbl
4344
}
4445
}
4546

47+
public bool IsUnion
48+
{
49+
get => (Flags & StructFlags.IsUnion) != 0;
50+
set => Flags = value ? Flags | StructFlags.IsUnion : Flags & ~StructFlags.IsUnion;
51+
}
52+
4653
public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
4754
public TCustomAttrGeneratorData CustomAttrGeneratorData { get; set; }
55+
56+
public StructLayoutAttribute LayoutAttribute
57+
{
58+
get
59+
{
60+
var layout = Layout;
61+
62+
if (IsUnion)
63+
{
64+
Debug.Assert(layout.Kind == LayoutKind.Explicit);
65+
66+
StructLayoutAttribute attribute = new(layout.Kind);
67+
68+
if (layout.Pack < layout.MaxFieldAlignment)
69+
{
70+
attribute.Pack = (int)layout.Pack;
71+
}
72+
73+
return attribute;
74+
}
75+
76+
if (layout.Pack < layout.MaxFieldAlignment)
77+
{
78+
return new StructLayoutAttribute(layout.Kind) {Pack = (int)layout.Pack};
79+
}
80+
81+
return null;
82+
}
83+
}
84+
85+
public struct LayoutDesc
86+
{
87+
public long Alignment32 { get; set; }
88+
public long Alignment64 { get; set; }
89+
public long Size32 { get; set; }
90+
public long Size64 { get; set; }
91+
public long Pack { get; set; }
92+
public long MaxFieldAlignment { get; set; }
93+
public LayoutKind Kind { get; set; }
94+
}
4895
}
4996
}

sources/ClangSharp.PInvokeGenerator/Abstractions/StructFlags.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace ClangSharp.Abstractions
88
public enum StructFlags
99
{
1010
IsUnsafe = 1 << 0,
11-
HasVtbl = 1 << 1
11+
HasVtbl = 1 << 1,
12+
IsUnion = 1 << 2,
1213
}
1314
}

sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ public void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDeleg
279279
}
280280
}
281281
}
282+
283+
if (!desc.IsCxxConstructor)
284+
{
285+
Write(desc.ReturnType);
286+
Write(' ');
287+
}
282288
}
283289

284290
private void WriteSourceLocation(CXSourceLocation location, bool inline)
@@ -304,12 +310,6 @@ private void WriteSourceLocation(CXSourceLocation location, bool inline)
304310
Write(' ');
305311
}
306312

307-
public void WriteReturnType(string typeString)
308-
{
309-
Write(typeString);
310-
Write(' ');
311-
}
312-
313313
public void BeginFunctionInnerPrototype(string escapedName)
314314
{
315315
Write(escapedName);
@@ -433,15 +433,15 @@ public void EndFunctionOrDelegate(bool isVirtual, bool isBodyless)
433433

434434
public void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGeneratorData> info)
435435
{
436-
if (info.Layout is not null)
436+
if (info.LayoutAttribute is { } attribute)
437437
{
438438
AddUsingDirective("System.Runtime.InteropServices");
439439
WriteIndented("[StructLayout(LayoutKind.");
440-
Write(info.Layout.Value);
441-
if (info.Layout.Pack != default)
440+
Write(attribute.Value);
441+
if (attribute.Pack != default)
442442
{
443443
Write(", Pack = ");
444-
Write(info.Layout.Pack);
444+
Write(attribute.Pack);
445445
}
446446

447447
WriteLine(")]");

sources/ClangSharp.PInvokeGenerator/FunctionOrDelegateFlags.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public enum FunctionOrDelegateFlags
1717
IsCxxRecordCtxUnsafe = 1 << 9,
1818
IsMemberFunction = 1 << 10,
1919
IsStatic = 1 << 11,
20-
IsNotStatic = 1 << 12
20+
IsNotStatic = 1 << 12,
21+
NeedsReturnFixup = 1 << 13,
22+
IsCxxConstructor = 1 << 14,
2123
}
2224
}

0 commit comments

Comments
 (0)