Skip to content

Add Generic Annotations system #1049

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

Merged
merged 1 commit into from
Aug 20, 2022
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
45 changes: 36 additions & 9 deletions src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ private IEnumerable<Symbol> VisitClass(XmlElement @class)
);
return new[]
{
new ClassSymbol(TypeId.CreateNew(), new IdentifierSymbol(name), members.ToImmutableArray())
new ClassSymbol
(
TypeId.CreateNew(),
new IdentifierSymbol(name, ImmutableArray<ISymbolAnnotation>.Empty),
members.ToImmutableArray(),
ImmutableArray<ISymbolAnnotation>.Empty
)
};
}

Expand Down Expand Up @@ -112,14 +118,20 @@ private IEnumerable<Symbol> VisitFunction(XmlElement function)
if (paramType is not TypeReference pt)
throw new InvalidOperationException("VisitType needs to return single type reference");

return new Parameter(pt, new IdentifierSymbol(paramName));
return new Parameter(pt, new IdentifierSymbol(paramName, ImmutableArray<ISymbolAnnotation>.Empty));
}
)
.ToImmutableArray();

return new[]
{
new StaticExternalMethodSymbol(rt, parameters, new IdentifierSymbol(name))
new StaticExternalMethodSymbol
(
rt,
parameters,
new IdentifierSymbol(name, ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
)
};
}

Expand Down Expand Up @@ -171,7 +183,12 @@ private IEnumerable<Symbol> VisitField(XmlElement field)

return new[]
{
new FieldSymbol(finalType, new IdentifierSymbol(name))
new FieldSymbol
(
finalType,
new IdentifierSymbol(name, ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
)
};
}

Expand All @@ -180,7 +197,7 @@ private IEnumerable<Symbol> VisitType(XmlElement type)
{
return new[]
{
new UnresolvedTypeReference(type.InnerText)
new UnresolvedTypeReference(type.InnerText, ImmutableArray<ISymbolAnnotation>.Empty)
};
}

Expand All @@ -206,8 +223,13 @@ private IEnumerable<Symbol> VisitStruct(XmlElement @struct)
new StructSymbol
(
TypeId.CreateNew(),
new IdentifierSymbol(@struct.Attributes?["name"]?.Value ?? throw new InvalidOperationException()),
fields.ToImmutableArray()
new IdentifierSymbol
(
@struct.Attributes?["name"]?.Value ?? throw new InvalidOperationException(),
ImmutableArray<ISymbolAnnotation>.Empty
),
fields.ToImmutableArray(),
ImmutableArray<ISymbolAnnotation>.Empty
)
)
};
Expand All @@ -224,7 +246,11 @@ private IEnumerable<Symbol> VisitNamespace(XmlElement @namespace)
{
new NamespaceSymbol
(
new IdentifierSymbol(@namespace.Attributes?["name"]?.Value ?? throw new InvalidOperationException()),
new IdentifierSymbol
(
@namespace.Attributes?["name"]?.Value ?? throw new InvalidOperationException(),
ImmutableArray<ISymbolAnnotation>.Empty
),
@namespace.ChildNodes.Cast<XmlNode>()
.SelectMany(Visit)
.Select
Expand All @@ -235,7 +261,8 @@ private IEnumerable<Symbol> VisitNamespace(XmlElement @namespace)
return ts;
}
)
.ToImmutableArray()
.ToImmutableArray(),
ImmutableArray<ISymbolAnnotation>.Empty
)
};
}
Expand Down
9 changes: 7 additions & 2 deletions src/generators/Silk.NET.SilkTouch.Symbols/ClassSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ namespace Silk.NET.SilkTouch.Symbols;
/// <summary>
/// A <see cref="TypeReference"/> representing a <c>class</c>
/// </summary>
public sealed record ClassSymbol(TypeId Id, IdentifierSymbol Identifier, ImmutableArray<MethodSymbol> Methods)
: TypeSymbol(Id, Identifier);
public sealed record ClassSymbol
(
TypeId Id,
IdentifierSymbol Identifier,
ImmutableArray<MethodSymbol> Methods,
ImmutableArray<ISymbolAnnotation> Annotations
) : TypeSymbol(Id, Identifier, Annotations);
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// Represents a reference to an external type
/// </summary>
public sealed record ExternalTypeReference(IdentifierSymbol? Namespace, IdentifierSymbol TypeIdentifier) : TypeReference
{
}
public sealed record ExternalTypeReference
(
IdentifierSymbol? Namespace,
IdentifierSymbol TypeIdentifier,
ImmutableArray<ISymbolAnnotation> Annotations
) : TypeReference(Annotations);
9 changes: 7 additions & 2 deletions src/generators/Silk.NET.SilkTouch.Symbols/FieldSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// A <see cref="FieldSymbol"/>. A field is simply a named location that can hold some type.
/// </summary>
/// <param name="Identifier">The identifier of this field</param>
/// <param name="Type">The <see cref="TypeReference"/> of the data stored in this field</param>
/// <param name="Identifier">The Identifier of this field</param>
/// <param name="Annotations">The annotations of this symbol</param>
/// <seealso cref="MemberSymbol"/>
public sealed record FieldSymbol(TypeReference Type, IdentifierSymbol Identifier) : MemberSymbol(Identifier);
public sealed record FieldSymbol
(TypeReference Type, IdentifierSymbol Identifier, ImmutableArray<ISymbolAnnotation> Annotations)
: MemberSymbol(Identifier, Annotations);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Silk.NET.SilkTouch.Symbols;
/// <summary>
/// A <see cref="TypeReference"/> representing a function pointer.
/// </summary>
public sealed record FunctionPointerTypeReference(TypeReference ReturnType, ImmutableArray<TypeReference> ParameterTypes) : TypeReference
{
}
public sealed record FunctionPointerTypeReference
(
TypeReference ReturnType,
ImmutableArray<TypeReference> ParameterTypes,
ImmutableArray<ISymbolAnnotation> Annotations
) : TypeReference(Annotations);
15 changes: 15 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Symbols/ISymbolAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// A symbol annotation.
/// Symbol annotations are a way for <see cref="SymbolVisitor"/>s to share auxiliary information.
/// </summary>
/// <seealso cref="Symbol"/>
/// <seealso cref="SymbolVisitor"/>
public interface ISymbolAnnotation
{

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// An Identifier. Generally used to identify another symbol
/// </summary>
/// <param name="Value">The String Value of this identifier</param>
/// <param name="Annotations">The annotations of this symbol</param>
/// <seealso cref="TypeSymbol"/>
public sealed record IdentifierSymbol(string Value) : Symbol
public sealed record IdentifierSymbol(string Value, ImmutableArray<ISymbolAnnotation> Annotations) : Symbol(Annotations)
{
/// <inheritdoc cref="object.ToString"/>
public override string ToString() => Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// Represents a reference to a type that is also defined as part of this symbol tree.
/// </summary>
/// <param name="ReferencedTypeId">The <see cref="TypeSymbol.Id"/> of the <see cref="TypeSymbol"/> referenced.</param>
public sealed record InternalTypeReference(TypeId ReferencedTypeId) : TypeReference()
{
}
/// <param name="Annotations">The annotations of this symbol</param>
public sealed record InternalTypeReference
(TypeId ReferencedTypeId, ImmutableArray<ISymbolAnnotation> Annotations) : TypeReference(Annotations);
7 changes: 6 additions & 1 deletion src/generators/Silk.NET.SilkTouch.Symbols/MemberSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// A <see cref="MemberSymbol"/>, representing a generic member of some <see cref="TypeSymbol"/>
/// </summary>
/// <param name="Identifier">The Identifier of this member</param>
/// <param name="Annotations">The annotations of this symbol</param>
/// <seealso cref="FieldSymbol"/>
public abstract record MemberSymbol(IdentifierSymbol Identifier) : Symbol;
public abstract record MemberSymbol
(IdentifierSymbol Identifier, ImmutableArray<ISymbolAnnotation> Annotations) : Symbol(Annotations);
8 changes: 6 additions & 2 deletions src/generators/Silk.NET.SilkTouch.Symbols/MethodSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ namespace Silk.NET.SilkTouch.Symbols;
/// A <see cref="MemberSymbol"/> representing a method with a signature.
/// </summary>
public abstract record MethodSymbol
(TypeReference ReturnType, ImmutableArray<Parameter> Parameters, IdentifierSymbol Identifier)
: MemberSymbol(Identifier);
(
TypeReference ReturnType,
ImmutableArray<Parameter> Parameters,
IdentifierSymbol Identifier,
ImmutableArray<ISymbolAnnotation> Annotations
) : MemberSymbol(Identifier, Annotations);

/// <summary>
/// Represents a Parameter to a <see cref="MethodSymbol"/>
Expand Down
10 changes: 7 additions & 3 deletions src/generators/Silk.NET.SilkTouch.Symbols/NamespaceSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace Silk.NET.SilkTouch.Symbols;
/// </summary>
/// <param name="Identifier">The Identifier of this Namespace</param>
/// <param name="Types">The types in this namespace</param>
public sealed record NamespaceSymbol(IdentifierSymbol Identifier, ImmutableArray<TypeSymbol> Types) : Symbol
{
}
/// <param name="Annotations">The annotations of this symbol</param>
public sealed record NamespaceSymbol
(
IdentifierSymbol Identifier,
ImmutableArray<TypeSymbol> Types,
ImmutableArray<ISymbolAnnotation> Annotations
) : Symbol(Annotations);
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// A <see cref="TypeReference"/> representing a pointer type
/// </summary>
/// <param name="Underlying">A reference to the pointed to type</param>
public sealed record PointerTypeReference(TypeReference Underlying) : TypeReference;
/// <param name="Annotations">The annotations of this symbol</param>
public sealed record PointerTypeReference
(TypeReference Underlying, ImmutableArray<ISymbolAnnotation> Annotations) : TypeReference(Annotations);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace Silk.NET.SilkTouch.Symbols;
/// External meaning it must be loaded
/// </summary>
public sealed record StaticExternalMethodSymbol
(TypeReference ReturnType, ImmutableArray<Parameter> Parameters, IdentifierSymbol Identifier)
: MethodSymbol(ReturnType, Parameters, Identifier)
{
}
(
TypeReference ReturnType,
ImmutableArray<Parameter> Parameters,
IdentifierSymbol Identifier,
ImmutableArray<ISymbolAnnotation> Annotations
) : MethodSymbol(ReturnType, Parameters, Identifier, Annotations);
8 changes: 7 additions & 1 deletion src/generators/Silk.NET.SilkTouch.Symbols/StructSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Silk.NET.SilkTouch.Symbols;
/// <param name="Id">An Identifier used for referencing types globally</param>
/// <param name="Identifier">The Identifier of this struct</param>
/// <param name="Fields">The fields of this struct</param>
/// <param name="Annotations">The annotations of this symbol</param>
/// <remarks>
/// In this context, a Struct means a type that represents the layout of a continuous block of memory.
/// </remarks>
Expand All @@ -19,4 +20,9 @@ namespace Silk.NET.SilkTouch.Symbols;
/// For types that would require such behavior there are separate symbols that may be defined.
/// </remarks>
public sealed record StructSymbol
(TypeId Id, IdentifierSymbol Identifier, ImmutableArray<FieldSymbol> Fields) : TypeSymbol(Id, Identifier);
(
TypeId Id,
IdentifierSymbol Identifier,
ImmutableArray<FieldSymbol> Fields,
ImmutableArray<ISymbolAnnotation> Annotations
) : TypeSymbol(Id, Identifier, Annotations);
5 changes: 4 additions & 1 deletion src/generators/Silk.NET.SilkTouch.Symbols/Symbol.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// The base Symbol. Represents shared properties of all Symbols. Primarily used with <see cref="SymbolVisitor"/>
/// </summary>
public abstract record Symbol;
/// <param name="Annotations">The annotations of this symbol</param>
public abstract record Symbol(ImmutableArray<ISymbolAnnotation> Annotations);
Loading