generate GeneratedCodeAttribute

This commit is contained in:
Christian Köllner 2020-04-01 22:15:46 +02:00
parent 20c8523aae
commit 955bab45e1
9 changed files with 268 additions and 250 deletions

View File

@ -1,8 +1,7 @@
#pragma warning disable CS1591
using Capnp;
using Capnp.Rpc;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
@ -10,7 +9,7 @@ using System.Threading.Tasks;
namespace Capnp.Rpc
{
[TypeId(0x91b79f1f808db032UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x91b79f1f808db032UL)]
public class Message : ICapnpSerializable
{
public const UInt64 typeId = 0x91b79f1f808db032UL;
@ -483,7 +482,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xe94ccf8031176ec4UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xe94ccf8031176ec4UL)]
public class Bootstrap : ICapnpSerializable
{
public const UInt64 typeId = 0xe94ccf8031176ec4UL;
@ -558,7 +557,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0x836a53ce789d4cd4UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x836a53ce789d4cd4UL)]
public class Call : ICapnpSerializable
{
public const UInt64 typeId = 0x836a53ce789d4cd4UL;
@ -707,7 +706,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xdae8b0f61aab5f99UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xdae8b0f61aab5f99UL)]
public class sendResultsTo : ICapnpSerializable
{
public const UInt64 typeId = 0xdae8b0f61aab5f99UL;
@ -832,7 +831,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0x9e19b28d3db3573aUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x9e19b28d3db3573aUL)]
public class Return : ICapnpSerializable
{
public const UInt64 typeId = 0x9e19b28d3db3573aUL;
@ -1072,7 +1071,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xd37d2eb2c2f80e63UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xd37d2eb2c2f80e63UL)]
public class Finish : ICapnpSerializable
{
public const UInt64 typeId = 0xd37d2eb2c2f80e63UL;
@ -1148,7 +1147,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xbbc29655fa89086eUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xbbc29655fa89086eUL)]
public class Resolve : ICapnpSerializable
{
public const UInt64 typeId = 0xbbc29655fa89086eUL;
@ -1301,7 +1300,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xad1a6c0d7dd07497UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xad1a6c0d7dd07497UL)]
public class Release : ICapnpSerializable
{
public const UInt64 typeId = 0xad1a6c0d7dd07497UL;
@ -1376,7 +1375,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xf964368b0fbd3711UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xf964368b0fbd3711UL)]
public class Disembargo : ICapnpSerializable
{
public const UInt64 typeId = 0xf964368b0fbd3711UL;
@ -1449,7 +1448,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xd562b4df655bdd4dUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xd562b4df655bdd4dUL)]
public class context : ICapnpSerializable
{
public const UInt64 typeId = 0xd562b4df655bdd4dUL;
@ -1619,7 +1618,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0x9c6a046bfbc1ac5aUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x9c6a046bfbc1ac5aUL)]
public class Provide : ICapnpSerializable
{
public const UInt64 typeId = 0x9c6a046bfbc1ac5aUL;
@ -1709,7 +1708,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xd4c9b56290554016UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xd4c9b56290554016UL)]
public class Accept : ICapnpSerializable
{
public const UInt64 typeId = 0xd4c9b56290554016UL;
@ -1799,7 +1798,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xfbe1980490e001afUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xfbe1980490e001afUL)]
public class Join : ICapnpSerializable
{
public const UInt64 typeId = 0xfbe1980490e001afUL;
@ -1889,7 +1888,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0x95bc14545813fbc1UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x95bc14545813fbc1UL)]
public class MessageTarget : ICapnpSerializable
{
public const UInt64 typeId = 0x95bc14545813fbc1UL;
@ -2025,7 +2024,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0x9a0e61223d96743bUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x9a0e61223d96743bUL)]
public class Payload : ICapnpSerializable
{
public const UInt64 typeId = 0x9a0e61223d96743bUL;
@ -2100,7 +2099,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0x8523ddc40b86b8b0UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0x8523ddc40b86b8b0UL)]
public class CapDescriptor : ICapnpSerializable
{
public const UInt64 typeId = 0x8523ddc40b86b8b0UL;
@ -2326,7 +2325,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xd800b1d6cd6f1ca0UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xd800b1d6cd6f1ca0UL)]
public class PromisedAnswer : ICapnpSerializable
{
public const UInt64 typeId = 0xd800b1d6cd6f1ca0UL;
@ -2400,7 +2399,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xf316944415569081UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xf316944415569081UL)]
public class Op : ICapnpSerializable
{
public const UInt64 typeId = 0xf316944415569081UL;
@ -2517,7 +2516,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xd37007fde1f0027dUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xd37007fde1f0027dUL)]
public class ThirdPartyCapDescriptor : ICapnpSerializable
{
public const UInt64 typeId = 0xd37007fde1f0027dUL;
@ -2592,7 +2591,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xd625b7063acf691aUL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xd625b7063acf691aUL)]
public class Exception : ICapnpSerializable
{
public const UInt64 typeId = 0xd625b7063acf691aUL;
@ -2696,7 +2695,7 @@ namespace Capnp.Rpc
}
}
[TypeId(0xb28c96e23f4cbd58UL)]
[System.CodeDom.Compiler.GeneratedCode("capnpc-csharp", "1.3.0.0"), TypeId(0xb28c96e23f4cbd58UL)]
public enum Type : ushort
{
failed,

View File

@ -282,7 +282,7 @@ namespace Capnp
{
Allocate();
var targetCopy = new DynamicSerializerState(MsgBuilder);
var targetCopy = new DynamicSerializerState(MsgBuilder!);
Reserializing.DeepCopy(target, targetCopy);
target = targetCopy;
}

File diff suppressed because it is too large Load Diff

View File

@ -45,6 +45,7 @@ namespace CapnpC.CSharp.Generator.Tests.Util
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "mscorlib.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Core.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Diagnostics.Tools.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Runtime.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Private.CoreLib.dll")),
MetadataReference.CreateFromFile(capnpRuntimePath) },

View File

@ -81,8 +81,9 @@
.AddConstraintClauses(MakeTypeParameterConstraints(def).ToArray());
}
topDecl = topDecl.AddMembers(CommonSnippetGen.MakeTypeIdConst(def.Id, _names));
topDecl = topDecl.WithAttributeLists(CommonSnippetGen.MakeTypeIdAttributeLists(def.Id));
topDecl = topDecl
.AddMembers(_names.MakeTypeIdConst(def.Id))
.AddAttributeLists(_names.MakeTypeDecorationAttributes(def.Id));
if (def.UnionInfo != null)
{
@ -244,6 +245,7 @@
UsingDirective(ParseName("Capnp")),
UsingDirective(ParseName("Capnp.Rpc")),
UsingDirective(ParseName("System")),
UsingDirective(ParseName("System.CodeDom.Compiler")),
UsingDirective(ParseName("System.Collections.Generic")));
if (_names.NullableEnable)

View File

@ -50,7 +50,7 @@ namespace CapnpC.CSharp.Generator.CodeGen
public EnumDeclarationSyntax MakeEnum(TypeDefinition def)
{
var decl = EnumDeclaration(_names.GetCodeIdentifier(def))
.WithAttributeLists(MakeTypeIdAttributeLists(def.Id))
.AddAttributeLists(_names.MakeTypeDecorationAttributes(def.Id))
.AddModifiers(_names.TypeVisibilityModifier)
.AddBaseListTypes(SimpleBaseType(_names.Type<ushort>(Nullability.NonNullable)));
@ -87,39 +87,5 @@ namespace CapnpC.CSharp.Generator.CodeGen
yield return expr;
}
}
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

@ -77,6 +77,9 @@ namespace CapnpC.CSharp.Generator.CodeGen
public bool NullableEnable { get; set; }
public bool EmitDomainClassesAndInterfaces { get; set; }
public SupportedAnnotations.TypeVisibility TypeVisibility { get; set; }
public string GeneratorToolName { get; }
public string GeneratorToolVersion { get; }
public GenNames(GeneratorOptions options)
{
TopNamespace = new Name(options.TopNamespaceName).IdentifierName;
@ -115,6 +118,8 @@ namespace CapnpC.CSharp.Generator.CodeGen
ProxyClassFormat = options.ProxyClassFormat;
SkeletonClassFormat = options.SkeletonClassFormat;
AwaitProxy = new Name(options.AwaitProxyName);
GeneratorToolName = options.GeneratorToolName;
GeneratorToolVersion = options.GeneratorToolVersion;
}
public Name MakeTypeName(TypeDefinition def, NameUsage usage = NameUsage.Default)
@ -729,5 +734,48 @@ namespace CapnpC.CSharp.Generator.CodeGen
}
}
}
static LiteralExpressionSyntax HexLiteral(ulong id) =>
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal($"0x{id:x}UL", id));
static LiteralExpressionSyntax StringLiteral(string text) =>
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(text));
public FieldDeclarationSyntax MakeTypeIdConst(ulong id) =>
FieldDeclaration(
VariableDeclaration(
IdentifierName("UInt64"))
.WithVariables(
SingletonSeparatedList<VariableDeclaratorSyntax>(
VariableDeclarator(TypeIdField.Identifier)
.WithInitializer(
EqualsValueClause(HexLiteral(id))))))
.WithModifiers(
TokenList(
new[]{
Token(SyntaxKind.PublicKeyword),
Token(SyntaxKind.ConstKeyword)}));
static AttributeSyntax MakeTypeIdAttribute(ulong id) =>
Attribute(
IdentifierName("TypeId"))
.WithArgumentList(
AttributeArgumentList(
SingletonSeparatedList<AttributeArgumentSyntax>(
AttributeArgument(HexLiteral(id)))));
public AttributeSyntax MakeGeneratedCodeAttribute() =>
Attribute(
IdentifierName("System.CodeDom.Compiler.GeneratedCode"))
.AddArgumentListArguments(
AttributeArgument(StringLiteral(GeneratorToolName)),
AttributeArgument(StringLiteral(GeneratorToolVersion)));
public AttributeListSyntax MakeTypeDecorationAttributes(ulong typeId) =>
AttributeList().AddAttributes(MakeGeneratedCodeAttribute(), MakeTypeIdAttribute(typeId));
}
}

View File

@ -39,5 +39,7 @@
public string TypeIdFieldName { get; set; } = "typeId";
public string AwaitProxyName { get; set; } = "AwaitProxy";
public bool NullableEnableDefault { get; set; } = false;
public string GeneratorToolName { get; set; } = "capnpc-csharp";
public string GeneratorToolVersion = ThisAssembly.AssemblyVersion;
}
}

View File

@ -99,9 +99,8 @@ namespace CapnpC.CSharp.Generator.CodeGen
var ifaceDecl = InterfaceDeclaration(_names.MakeTypeName(type, NameUsage.Interface).Identifier)
.AddModifiers(Public)
.AddAttributeLists(
AttributeList()
_names.MakeTypeDecorationAttributes(type.Id)
.AddAttributes(
CommonSnippetGen.MakeTypeIdAttribute(type.Id),
Attribute(IdentifierName("Proxy"))
.AddArgumentListArguments(
AttributeArgument(