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 XML Serialization classes #557

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage(
"Design",
"CA1002: Do not expose generic lists",
Scope = "namespaceAndDescendants",
Target = "~N:ClangSharp.XML",
Justification = nameof(System.Xml.Serialization)
)]

18 changes: 18 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/BindingsElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
[XmlRoot("bindings")]
public sealed class BindingsElement
{
[XmlElement("comment")]
public string? HeaderText { get; init; }

[XmlElement("namespace")]
public List<NamespaceElement> Namespaces { get; } = [];
}
15 changes: 15 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/CSharpCodeElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public sealed class CSharpCodeElement
{
[XmlText]
[XmlAnyElement]
public List<XmlNode> Segments { get; } = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class ConstantDescElement : FieldDescElement { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class DelegateDescElement : FunctionOrDelegateDescElement { }
10 changes: 10 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/EnumConstantDeclElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Diagnostics;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
[DebuggerDisplay($"{{{nameof(Name)},nq}}")]
public class EnumConstantDeclElement : FieldDescElement { }
19 changes: 19 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/EnumDescElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class EnumDescElement : NamedAccessDescElement
{
[XmlElement("attribute")]
public List<string> Attributes { get; } = [];

[XmlElement("type")]
public required string TypeName { get; init; }

[XmlElement("enumerator")]
public List<EnumConstantDeclElement> Enumerators { get; } = [];
}
54 changes: 54 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/FieldDescElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
[DebuggerDisplay($"{{{nameof(AccessSpecifier)},nq}} {{{nameof(Type)}}} {{{nameof(Name)},nq}}")]
public class FieldDescElement : NamedAccessDescElement
{
[XmlAttribute("inherited")]
public string? InheritedFrom { get; init; }

[XmlAttribute("offset")]
public string? OffsetValue { get; init; }

[XmlIgnore]
public int? Offset
{
get
{
return int.TryParse(
OffsetValue,
CultureInfo.InvariantCulture,
out var offset
) ? offset : null;
}

init
{
OffsetValue = value.HasValue
? value.Value.ToString(CultureInfo.InvariantCulture)
: null;
}
}

[XmlElement("attribute")]
public List<string> Attributes { get; } = [];

[XmlElement("type")]
public required FieldTypeElement Type { get; init; }

[XmlElement("value")]
public ValueDescValueElement? Initializer { get; init; }

[XmlElement("get")]
public PropertyAccessorElement? Getter { get; init; }

[XmlElement("set")]
public PropertyAccessorElement? Setter { get; init; }
}
15 changes: 15 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/FieldTypeElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class FieldTypeElement : TypeWithNativeTypeNameElement
{
[XmlAttribute("count")]
public string? Count { get; init; }

[XmlAttribute("fixed")]
public string? FixedName { get; init; }
}
12 changes: 12 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/FunctionBodyElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class FunctionBodyElement
{
[XmlElement("code")]
public CSharpCodeElement? Code { get; init; }
}
19 changes: 19 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/FunctionDescElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class FunctionDescElement : FunctionOrDelegateDescElement
{
[XmlElement("init")]
public List<TypeInitializationExpressionElement> TypeInitializationExpressions { get; } = [];

[XmlElement("body")]
public FunctionBodyElement? BlockBody { get; init; }

[XmlElement("code")]
public CSharpCodeElement? ExpressionBody { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public abstract class FunctionOrDelegateDescElement : NamedAccessDescElement
{
[XmlAttribute("lib")]
public string? LibraryPath { get; init; }

[XmlAttribute("convention")]
public string? CallingConvention { get; init; }

[XmlAttribute("entrypoint")]
public string? EntryPoint { get; init; }

[XmlAttribute("setlasterror")]
public bool SetLastError { get; init; }

[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool SetLastErrorSpecified => SetLastError;

[XmlAttribute("static")]
public bool IsStatic { get; init; }

[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsStaticSpecified => IsStatic;

[XmlAttribute("unsafe")]
public bool IsUnsafe { get; init; }

[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsUnsafeSpecified => IsUnsafe;

[XmlAttribute("vtblindex")]
[EditorBrowsable(EditorBrowsableState.Never)]
public string? VtblIndexValue { get; init; }

[XmlIgnore]
public long? VtblIndex
{
get
{
return !string.IsNullOrEmpty(VtblIndexValue)
&& long.TryParse(
VtblIndexValue,
CultureInfo.InvariantCulture,
out var vtblindex
) ? vtblindex : null;
}

init
{
VtblIndexValue = value.HasValue
? value.Value.ToString(CultureInfo.InvariantCulture)
: null;
}
}

[XmlElement("attribute")]
public List<string> Attributes { get; } = [];

[XmlElement("type")]
public required TypeWithNativeTypeNameElement ReturnType { get; init; }

[XmlElement("param")]
public List<ParameterDescElement> Parameters { get; } = [];
}
12 changes: 12 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/IidDescElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class IidDescElement : NamedDescElement
{
[XmlAttribute("value")]
public required string Value { get; init; }
}
29 changes: 29 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/IndexerDeclElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.ComponentModel;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class IndexerDeclElement
{
[XmlAttribute("access")]
public required string AccessSpecifier { get; init; }

[XmlAttribute("unsafe")]
public bool IsUnsafe { get; init; }

[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsUnsafeSpecified => IsUnsafe;

[XmlElement("type")]
public required string Type { get; init; }

[XmlElement("param")]
public required ParameterDescElement IndexerParameter { get; init; }

[XmlElement("get")]
public required PropertyAccessorElement Getter { get; init; }
}
15 changes: 15 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/InterfaceElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class InterfaceElement
{
[XmlElement("constant", typeof(ConstantDescElement))]
[XmlElement("field", typeof(FieldDescElement))]
[XmlElement("function", typeof(FunctionDescElement))]
public List<NamedAccessDescElement> Decls { get; } = [];
}
32 changes: 32 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/MethodClassElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;

namespace ClangSharp.XML;

[XmlType]
public class MethodClassElement : NamedAccessDescElement
{
[XmlAttribute("static")]
public bool IsStatic { get; init; }

[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsStaticSpecified => IsStatic;

[XmlAttribute("unsafe")]
public bool IsUnsafe { get; init; }

[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsUnsafeSpecified => IsUnsafe;

[XmlElement("constant", typeof(ConstantDescElement))]
[XmlElement("field", typeof(FieldDescElement))]
[XmlElement("function", typeof(FunctionDescElement))]
[XmlElement("delegate", typeof(DelegateDescElement))]
[XmlElement("iid", typeof(IidDescElement))]
public List<NamedDescElement> Decls { get; } = [];
}
12 changes: 12 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/NamedAccessDescElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.


using System.Xml.Serialization;

namespace ClangSharp.XML;

public abstract class NamedAccessDescElement : NamedDescElement
{
[XmlAttribute("access")]
public required string AccessSpecifier { get; init; }
}
12 changes: 12 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/XML/NamedDescElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.


using System.Xml.Serialization;

namespace ClangSharp.XML;

public abstract class NamedDescElement
{
[XmlAttribute("name")]
public required string Name { get; init; }
}
Loading