mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-13 07:11:44 +01:00
Merge pull request #13 from Bertec/feature/output-typeid
Feature/output typeid
This commit is contained in:
commit
4830fa0e4a
27
Capnp.Net.Runtime/TypeIdAttribute.cs
Normal file
27
Capnp.Net.Runtime/TypeIdAttribute.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Capnp
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Annotates an enum, class or interface with its schema type identifier.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Class |AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
|
||||||
|
public class TypeIdAttribute : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs this attribute.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="typeId">The 64-bit type identifier from the schema file.</param>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="typeId"/> is zero.</exception>
|
||||||
|
public TypeIdAttribute(ulong typeId)
|
||||||
|
{
|
||||||
|
if (typeId == 0) throw new ArgumentOutOfRangeException(nameof(typeId), "The value cannot be zero.");
|
||||||
|
Id = typeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The schema type identifier.
|
||||||
|
/// </summary>
|
||||||
|
public ulong Id { get; }
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -71,6 +71,9 @@
|
|||||||
.AddConstraintClauses(MakeTypeParameterConstraints(def).ToArray());
|
.AddConstraintClauses(MakeTypeParameterConstraints(def).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
topDecl = topDecl.AddMembers(CommonSnippetGen.MakeTypeIdConst(def.Id, _names));
|
||||||
|
topDecl = topDecl.WithAttributeLists(CommonSnippetGen.MakeTypeIdAttributeLists(def.Id));
|
||||||
|
|
||||||
if (def.UnionInfo != null)
|
if (def.UnionInfo != null)
|
||||||
{
|
{
|
||||||
topDecl = topDecl.AddMembers(_commonGen.MakeUnionSelectorEnum(def));
|
topDecl = topDecl.AddMembers(_commonGen.MakeUnionSelectorEnum(def));
|
||||||
|
@ -52,6 +52,7 @@ namespace CapnpC.Generator
|
|||||||
public EnumDeclarationSyntax MakeEnum(TypeDefinition def)
|
public EnumDeclarationSyntax MakeEnum(TypeDefinition def)
|
||||||
{
|
{
|
||||||
var decl = EnumDeclaration(def.Name)
|
var decl = EnumDeclaration(def.Name)
|
||||||
|
.WithAttributeLists(MakeTypeIdAttributeLists(def.Id))
|
||||||
.AddModifiers(Public)
|
.AddModifiers(Public)
|
||||||
.AddBaseListTypes(SimpleBaseType(Type<ushort>()));
|
.AddBaseListTypes(SimpleBaseType(Type<ushort>()));
|
||||||
|
|
||||||
@ -89,5 +90,38 @@ namespace CapnpC.Generator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static LiteralExpressionSyntax HexLiteral(ulong id) =>
|
||||||
|
LiteralExpression(
|
||||||
|
SyntaxKind.NumericLiteralExpression,
|
||||||
|
Literal($"0x{id:x}UL", id));
|
||||||
|
|
||||||
|
public static FieldDeclarationSyntax MakeTypeIdConst(ulong id, GenNames names) =>
|
||||||
|
FieldDeclaration(
|
||||||
|
VariableDeclaration(
|
||||||
|
IdentifierName("UInt64"))
|
||||||
|
.WithVariables(
|
||||||
|
SingletonSeparatedList<VariableDeclaratorSyntax>(
|
||||||
|
VariableDeclarator(names.TypeIdField.Identifier)
|
||||||
|
.WithInitializer(
|
||||||
|
EqualsValueClause(HexLiteral(id))))))
|
||||||
|
.WithModifiers(
|
||||||
|
TokenList(
|
||||||
|
new[]{
|
||||||
|
Token(SyntaxKind.PublicKeyword),
|
||||||
|
Token(SyntaxKind.ConstKeyword)}));
|
||||||
|
|
||||||
|
public static AttributeSyntax MakeTypeIdAttribute(ulong id) =>
|
||||||
|
Attribute(
|
||||||
|
IdentifierName("TypeId"))
|
||||||
|
.WithArgumentList(
|
||||||
|
AttributeArgumentList(
|
||||||
|
SingletonSeparatedList<AttributeArgumentSyntax>(
|
||||||
|
AttributeArgument(HexLiteral(id)))));
|
||||||
|
|
||||||
|
public static SyntaxList<AttributeListSyntax> MakeTypeIdAttributeLists(ulong id) =>
|
||||||
|
SingletonList<AttributeListSyntax>(
|
||||||
|
AttributeList(
|
||||||
|
SingletonSeparatedList<AttributeSyntax>(
|
||||||
|
CommonSnippetGen.MakeTypeIdAttribute(id))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ namespace CapnpC.Generator
|
|||||||
public string MemberAccessPathNameFormat { get; }
|
public string MemberAccessPathNameFormat { get; }
|
||||||
public Name TaskParameter { get; }
|
public Name TaskParameter { get; }
|
||||||
public Name EagerMethod { get; }
|
public Name EagerMethod { get; }
|
||||||
|
public Name TypeIdField { get; }
|
||||||
|
|
||||||
public GenNames(GeneratorOptions options)
|
public GenNames(GeneratorOptions options)
|
||||||
{
|
{
|
||||||
@ -98,6 +99,7 @@ namespace CapnpC.Generator
|
|||||||
MemberAccessPathNameFormat = options.MemberAccessPathNameFormat;
|
MemberAccessPathNameFormat = options.MemberAccessPathNameFormat;
|
||||||
TaskParameter = new Name(options.TaskParameterName);
|
TaskParameter = new Name(options.TaskParameterName);
|
||||||
EagerMethod = new Name(options.EagerMethodName);
|
EagerMethod = new Name(options.EagerMethodName);
|
||||||
|
TypeIdField = new Name(options.TypeIdFieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Name MakeTypeName(TypeDefinition def, NameUsage usage = NameUsage.Default)
|
public Name MakeTypeName(TypeDefinition def, NameUsage usage = NameUsage.Default)
|
||||||
|
@ -38,5 +38,6 @@ namespace CapnpC.Generator
|
|||||||
public string MemberAccessPathNameFormat { get; set; } = "Path_{0}_{1}";
|
public string MemberAccessPathNameFormat { get; set; } = "Path_{0}_{1}";
|
||||||
public string TaskParameterName { get; set; } = "task";
|
public string TaskParameterName { get; set; } = "task";
|
||||||
public string EagerMethodName { get; set; } = "Eager";
|
public string EagerMethodName { get; set; } = "Eager";
|
||||||
|
public string TypeIdFieldName { get; set; } = "TypeId";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,7 @@ namespace CapnpC.Generator
|
|||||||
.AddAttributeLists(
|
.AddAttributeLists(
|
||||||
AttributeList()
|
AttributeList()
|
||||||
.AddAttributes(
|
.AddAttributes(
|
||||||
|
CommonSnippetGen.MakeTypeIdAttribute(type.Id),
|
||||||
Attribute(IdentifierName("Proxy"))
|
Attribute(IdentifierName("Proxy"))
|
||||||
.AddArgumentListArguments(
|
.AddArgumentListArguments(
|
||||||
AttributeArgument(
|
AttributeArgument(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user