Skip to content
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.
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 @@ -200,18 +200,32 @@ public bool? IsStatic

public bool NeedsReturnFixup
{
get => (Flags & FunctionOrDelegateFlags.NeedsReturnFixup) != 0;
set => Flags = value
? Flags | FunctionOrDelegateFlags.NeedsReturnFixup
: Flags & ~FunctionOrDelegateFlags.NeedsReturnFixup;
get
{
return (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;
get
{
return (Flags & FunctionOrDelegateFlags.IsCxxConstructor) != 0;
}

set
{
Flags = value
? Flags | FunctionOrDelegateFlags.IsCxxConstructor
: Flags & ~FunctionOrDelegateFlags.IsCxxConstructor;
}
}

public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +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;

namespace ClangSharp.Abstractions
{
internal partial interface IOutputBuilder
Expand All @@ -8,7 +10,7 @@ internal partial interface IOutputBuilder
void SuppressDivider();

void WriteCustomAttribute(string attribute);
void WriteIid(string iidName, string iidValue);
void WriteIid(string name, Guid value);
void EmitUsingDirective(string directive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace ClangSharp.Abstractions
{
internal partial interface IOutputBuilder
{
bool IsUncheckedContext { get; }

void BeginInnerValue();
void EndInnerValue();

Expand All @@ -14,12 +16,10 @@ internal partial interface IOutputBuilder
void BeginUnchecked();
void EndUnchecked();

void BeginConstant(in ConstantDesc desc);
void BeginConstantValue(bool isGetOnlyProperty = false);
void BeginValue(in ValueDesc desc);
void WriteConstantValue(long value);
void WriteConstantValue(ulong value);
void EndConstantValue();
void EndConstant(bool isConstant);
void EndValue(in ValueDesc desc);

void BeginEnum(in EnumDesc desc);
void EndEnum();
Expand All @@ -29,8 +29,7 @@ internal partial interface IOutputBuilder
void WriteRegularField(string typeName, string escapedName);
void EndField(bool isBodyless = true);

void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDelegateDesc<TCustomAttrGeneratorData> info,
ref bool isMethodClassUnsafe);
void BeginFunctionOrDelegate<TCustomAttrGeneratorData>(in FunctionOrDelegateDesc<TCustomAttrGeneratorData> info, ref bool isMethodClassUnsafe);
void BeginFunctionInnerPrototype(string escapedName);
void BeginParameter<TCustomAttrGeneratorData>(in ParameterDesc<TCustomAttrGeneratorData> info);
void BeginParameterDefault();
Expand Down
11 changes: 9 additions & 2 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ public bool HasVtbl

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

set
{
Flags = value ? Flags | StructFlags.IsUnion : Flags & ~StructFlags.IsUnion;
}
}

public Action<TCustomAttrGeneratorData> WriteCustomAttrs { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

namespace ClangSharp.Abstractions
{
internal struct ConstantDesc
internal struct ValueDesc
{
public AccessSpecifier AccessSpecifier { get; set; }
public string TypeName { get; set; }
public string EscapedName { get; set; }
public string NativeTypeName { get; set; }
public ConstantKind Kind { get; set; }
public ValueKind Kind { get; set; }
public ValueFlags Flags { get; set; }
public CXSourceLocation? Location { get; set; }

public bool HasInitializer => Flags.HasFlag(ValueFlags.Initializer);
public bool IsArray => Flags.HasFlag(ValueFlags.Array);
public bool IsConstant => Flags.HasFlag(ValueFlags.Constant);
public bool IsCopy => Flags.HasFlag(ValueFlags.Copy);
}
}
13 changes: 13 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/ValueKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

namespace ClangSharp.Abstractions
{
internal enum ValueKind
{
Unknown,
Primitive,
Enumerator,
Unmanaged,
String,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace ClangSharp.Abstractions
{
[Flags]
internal enum ConstantKind
internal enum ValueFlags
{
None = 0,
ReadOnly = 1 << 0,
Enumerator = 1 << 1,
PrimitiveConstant = 1 << 2,
NonPrimitiveConstant = 1 << 3
Initializer = 1 << 0,
Constant = 1 << 1,
Copy = 1 << 2,
Array = 1 << 3,
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// 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.Runtime.CompilerServices;

namespace ClangSharp.CSharp
{
internal partial class CSharpOutputBuilder
Expand Down Expand Up @@ -49,13 +52,72 @@ public void WriteDivider(bool force = false)

public void SuppressDivider() => NeedsNewline = false;

public void WriteIid(string iidName, string iidValue)
public void WriteIid(string name, Guid value)
{
if (_config.GenerateUnmanagedConstants)
{
AddUsingDirective("System");
AddUsingDirective("System.Diagnostics");
AddUsingDirective("System.Runtime.CompilerServices");
AddUsingDirective("System.Runtime.InteropServices");

WriteIndented("public static ref readonly Guid ");
WriteLine(name);
WriteBlockStart();

WriteIndentedLine("get");
WriteBlockStart();

WriteIndentedLine("ReadOnlySpan<byte> data = new byte[] {");
IncreaseIndentation();
WriteIndentation();

WriteValueAsBytes(Unsafe.As<Guid, uint>(ref value), 4);

WriteLine(',');
WriteIndentation();

WriteValueAsBytes(Unsafe.As<Guid, ushort>(ref Unsafe.AddByteOffset(ref value, (nint)4)), 2);

WriteLine(',');
WriteIndentation();

WriteValueAsBytes(Unsafe.As<Guid, ushort>(ref Unsafe.AddByteOffset(ref value, (nint)6)), 2);

for (var i = 8; i < 16; i++)
{
WriteLine(',');
WriteIndentation();

WriteValueAsBytes(Unsafe.As<Guid, ushort>(ref Unsafe.AddByteOffset(ref value, (nint)i)), 1);
}

WriteNewline();
DecreaseIndentation();
WriteIndentedLine("};");

NeedsNewline = true;

WriteIndentedLine("Debug.Assert(data.Length == Unsafe.SizeOf<Guid>());");
WriteIndentedLine("return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));");

WriteBlockEnd();
WriteBlockEnd();
}
else
{
var valueString = value.ToString("X").ToUpperInvariant().Replace("{", "").Replace("}", "").Replace('X', 'x').Replace(",", ", ");
WriteIid(name, valueString);
}
}

public void WriteIid(string name, string value)
{
AddUsingDirective("System");
WriteIndented("public static readonly Guid ");
Write(iidName);
Write(name);
Write(" = new Guid(");
Write(iidValue);
Write(value);
Write(")");
WriteSemicolon();
WriteNewline();
Expand Down
Loading