This commit is contained in:
Christian Köllner 2019-10-03 21:43:54 +02:00
commit d889bdb469
7 changed files with 418 additions and 18 deletions

View 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

View File

@ -70,6 +70,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));

View File

@ -50,6 +50,7 @@ namespace CapnpC.CSharp.Generator.CodeGen
public EnumDeclarationSyntax MakeEnum(TypeDefinition def)
{
var decl = EnumDeclaration(def.Name)
.WithAttributeLists(MakeTypeIdAttributeLists(def.Id))
.AddModifiers(Public)
.AddBaseListTypes(SimpleBaseType(Type<ushort>()));
@ -87,5 +88,38 @@ namespace CapnpC.CSharp.Generator.CodeGen
}
}
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))));
}
}

View File

@ -62,6 +62,7 @@ namespace CapnpC.CSharp.Generator.CodeGen
public string MemberAccessPathNameFormat { get; }
public Name TaskParameter { get; }
public Name EagerMethod { get; }
public Name TypeIdField { get; }
public GenNames(GeneratorOptions options)
{
@ -97,6 +98,7 @@ namespace CapnpC.CSharp.Generator.CodeGen
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)

View File

@ -34,5 +34,6 @@
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";
}
}

View File

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