Merge pull request from Bertec/feature/output-typeid

Feature/output typeid
This commit is contained in:
c80k 2019-10-02 13:37:57 +02:00 committed by GitHub
commit 4830fa0e4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 418 additions and 18 deletions
Capnp.Net.Runtime
capnpc-csharp.tests/Embedded Resources
capnpc-csharp/Generator

@ -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());
}
topDecl = topDecl.AddMembers(CommonSnippetGen.MakeTypeIdConst(def.Id, _names));
topDecl = topDecl.WithAttributeLists(CommonSnippetGen.MakeTypeIdAttributeLists(def.Id));
if (def.UnionInfo != null)
{
topDecl = topDecl.AddMembers(_commonGen.MakeUnionSelectorEnum(def));

@ -52,6 +52,7 @@ namespace CapnpC.Generator
public EnumDeclarationSyntax MakeEnum(TypeDefinition def)
{
var decl = EnumDeclaration(def.Name)
.WithAttributeLists(MakeTypeIdAttributeLists(def.Id))
.AddModifiers(Public)
.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 Name TaskParameter { get; }
public Name EagerMethod { get; }
public Name TypeIdField { get; }
public GenNames(GeneratorOptions options)
{
@ -98,6 +99,7 @@ namespace CapnpC.Generator
MemberAccessPathNameFormat = options.MemberAccessPathNameFormat;
TaskParameter = new Name(options.TaskParameterName);
EagerMethod = new Name(options.EagerMethodName);
TypeIdField = new Name(options.TypeIdFieldName);
}
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 TaskParameterName { get; set; } = "task";
public string EagerMethodName { get; set; } = "Eager";
public string TypeIdFieldName { get; set; } = "TypeId";
}
}

@ -97,6 +97,7 @@ namespace CapnpC.Generator
.AddAttributeLists(
AttributeList()
.AddAttributes(
CommonSnippetGen.MakeTypeIdAttribute(type.Id),
Attribute(IdentifierName("Proxy"))
.AddArgumentListArguments(
AttributeArgument(