From 0f0eff4cd0081301e0ed9469b40b3bae62ae0303 Mon Sep 17 00:00:00 2001 From: aboulart <37553604+aboulart@users.noreply.github.com> Date: Tue, 10 Dec 2019 19:07:49 +0000 Subject: [PATCH 1/5] Add compile step to all unit tests --- .../CodeGeneratorUnitTests.cs | 33 +++++++++---------- .../Util/InlineAssemblyCompiler.cs | 10 ++++++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs b/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs index bfec1b7..b105a3a 100644 --- a/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs +++ b/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs @@ -12,8 +12,6 @@ namespace CapnpC.CSharp.Generator.Tests [TestClass] public class CodeGeneratorUnitTests { - static readonly Dictionary GeneratedCode = new Dictionary(); - [TestMethod] public void Test00Enumerant() { @@ -24,7 +22,7 @@ namespace CapnpC.CSharp.Generator.Tests [TestMethod] public void Test01NestedClash() { - var run = LoadAndGenerate("UnitTest1.capnp.bin", 1); + var run = LoadAndGenerate("UnitTest1.capnp.bin"); var structFoo = GetTypeDef(0x93db6ba5509bac24, run.Model); var names = run.CodeGen.GetNames(); var fieldName = names.GetCodeIdentifier(structFoo.Fields[0]).ToString(); @@ -35,28 +33,25 @@ namespace CapnpC.CSharp.Generator.Tests [TestMethod] public void Test02ForwardInheritance() { - LoadAndGenerate("UnitTest2.capnp.bin", 2); - // Should not throw + LoadAndGenerate("UnitTest2.capnp.bin"); } [TestMethod] public void Test03NonGeneratedNodeSkip() { - LoadAndGenerate("UnitTest3.capnp.bin", 3); - // Should not throw + LoadAndGenerate("UnitTest3.capnp.bin"); } [TestMethod] public void Test04MutualDependencies() { - LoadAndGenerate("UnitTest4.capnp.bin", 4); - // Should not throw + LoadAndGenerate("UnitTest4.capnp.bin"); } [TestMethod] public void Test10ImportedNamespaces() { - var run = LoadAndGenerate("UnitTest10.capnp.bin", 10); + var run = LoadAndGenerate("UnitTest10.capnp.bin"); var outerTypeDef = run.FirstFile.NestedTypes.First(); var outerType = Model.Types.FromDefinition(outerTypeDef); var innerType = outerTypeDef.Fields[0].Type; @@ -77,22 +72,25 @@ namespace CapnpC.CSharp.Generator.Tests [TestMethod] public void Test11ImportedConst() { - LoadAndGenerate("UnitTest11.capnp.bin", 11); - // Should not throw + LoadAndGenerate("UnitTest11.capnp.bin"); + } + + [TestMethod] + public void Test12ConstEnum() + { + LoadAndGenerate("UnitTest12.capnp.bin"); } [TestMethod] public void Test20AnnotationAndConst() { - LoadAndGenerate("UnitTest20.capnp.bin", 20); - // Should not throw + LoadAndGenerate("UnitTest20.capnp.bin"); } [TestMethod] public void Test30SchemaCapnp() { LoadAndGenerate("schema-with-offsets.capnp.bin"); - // Should not throw } struct Run @@ -106,15 +104,14 @@ namespace CapnpC.CSharp.Generator.Tests static CodeGen.CodeGenerator NewGeneratorFor(Model.SchemaModel model) => new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions()); - Run LoadAndGenerate(string inputName, int? testNum = null) + Run LoadAndGenerate(string inputName) { var run = new Run(); run.Model = Load(inputName); run.CodeGen = NewGeneratorFor(run.Model); run.FirstFile = run.Model.FilesToGenerate.First(); run.Code = run.CodeGen.Transform(run.FirstFile); - if (testNum is int num) - GeneratedCode[num] = run.Code; + Assert.IsTrue(Util.InlineAssemblyCompiler.TryCompileCapnp(run.Code), "Compilation was not successful"); return run; } diff --git a/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs b/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs index 7abc30f..8cd3cb0 100644 --- a/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs +++ b/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs @@ -42,6 +42,16 @@ namespace CapnpC.CSharp.Generator.Tests.Util { var emitResult = compilation.Emit(stream); + foreach (var diag in emitResult.Diagnostics) + Console.WriteLine($"{diag}"); + + if (!emitResult.Success) + { + string path = Path.ChangeExtension(Path.GetTempFileName(), ".capnp.cs"); + File.WriteAllText(path, code); + Console.WriteLine($"[See {path} for generated code]"); + } + return emitResult.Success; } } From 2ac731eec640414dcc638e5e986549e704ce0225 Mon Sep 17 00:00:00 2001 From: aboulart <37553604+aboulart@users.noreply.github.com> Date: Tue, 10 Dec 2019 19:14:39 +0000 Subject: [PATCH 2/5] Fix spelling --- CapnpC.CSharp.Generator/CodeGen/GenNames.cs | 4 ++-- CapnpC.CSharp.Generator/CodeGen/GeneratorOptions.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CapnpC.CSharp.Generator/CodeGen/GenNames.cs b/CapnpC.CSharp.Generator/CodeGen/GenNames.cs index 1587449..42c5938 100644 --- a/CapnpC.CSharp.Generator/CodeGen/GenNames.cs +++ b/CapnpC.CSharp.Generator/CodeGen/GenNames.cs @@ -78,9 +78,9 @@ namespace CapnpC.CSharp.Generator.CodeGen ContextParameter = new Name(options.ContextParameterName); GroupReaderContextArg = new Name(options.GroupReaderContextArgName); GroupWriterContextArg = new Name(options.GroupWriterContextArgName); - UnionDiscriminatorEnum = new Name(options.UnionDisciminatorEnumName); + UnionDiscriminatorEnum = new Name(options.UnionDiscriminatorEnumName); UnionDiscriminatorProp = new Name(options.UnionDiscriminatorPropName); - UnionDiscriminatorUndefined = new Name(options.UnionDisciminatorUndefinedName); + UnionDiscriminatorUndefined = new Name(options.UnionDiscriminatorUndefinedName); UnionDiscriminatorField = new Name(options.UnionDiscriminatorFieldName); UnionContentField = new Name(options.UnionContentFieldName); SerializeMethod = new Name(options.SerializeMethodName); diff --git a/CapnpC.CSharp.Generator/CodeGen/GeneratorOptions.cs b/CapnpC.CSharp.Generator/CodeGen/GeneratorOptions.cs index 329f553..8357cee 100644 --- a/CapnpC.CSharp.Generator/CodeGen/GeneratorOptions.cs +++ b/CapnpC.CSharp.Generator/CodeGen/GeneratorOptions.cs @@ -12,10 +12,10 @@ public string ContextParameterName { get; set; } = "ctx"; public string GroupReaderContextArgName { get; set; } = "ctx"; public string GroupWriterContextArgName { get; set; } = "ctx"; - public string UnionDisciminatorEnumName { get; set; } = "WHICH"; + public string UnionDiscriminatorEnumName { get; set; } = "WHICH"; public string UnionDiscriminatorPropName { get; set; } = "which"; public string UnionDiscriminatorFieldName { get; set; } = "_which"; - public string UnionDisciminatorUndefinedName { get; set; } = "undefined"; + public string UnionDiscriminatorUndefinedName { get; set; } = "undefined"; public string UnionContentFieldName { get; set; } = "_content"; public string SerializeMethodName { get; set; } = "serialize"; public string ApplyDefaultsMethodName { get; set; } = "applyDefaults"; From cb69c52cc7fc9e302b01c74babeb3f4a089427cf Mon Sep 17 00:00:00 2001 From: aboulart <37553604+aboulart@users.noreply.github.com> Date: Wed, 11 Dec 2019 11:14:46 +0000 Subject: [PATCH 3/5] Include all generated files in unit test binary schemas --- .../CodeGeneratorUnitTests.cs | 40 ++++++++---------- .../Embedded Resources/UnitTest10.capnp.bin | Bin 1616 -> 1600 bytes .../Embedded Resources/UnitTest11.capnp.bin | Bin 2080 -> 2408 bytes .../Embedded Resources/UnitTest4.capnp.bin | Bin 3304 -> 3600 bytes .../Util/InlineAssemblyCompiler.cs | 22 ++++++---- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs b/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs index b105a3a..162e09a 100644 --- a/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs +++ b/CapnpC.CSharp.Generator.Tests/CodeGeneratorUnitTests.cs @@ -22,9 +22,9 @@ namespace CapnpC.CSharp.Generator.Tests [TestMethod] public void Test01NestedClash() { - var run = LoadAndGenerate("UnitTest1.capnp.bin"); - var structFoo = GetTypeDef(0x93db6ba5509bac24, run.Model); - var names = run.CodeGen.GetNames(); + var (model, codegen) = LoadAndGenerate("UnitTest1.capnp.bin"); + var structFoo = GetTypeDef(0x93db6ba5509bac24, model); + var names = codegen.GetNames(); var fieldName = names.GetCodeIdentifier(structFoo.Fields[0]).ToString(); Assert.AreEqual("Foo", structFoo.Name); Assert.AreNotEqual(structFoo.Name, fieldName); @@ -51,12 +51,12 @@ namespace CapnpC.CSharp.Generator.Tests [TestMethod] public void Test10ImportedNamespaces() { - var run = LoadAndGenerate("UnitTest10.capnp.bin"); - var outerTypeDef = run.FirstFile.NestedTypes.First(); + var (model, codegen) = LoadAndGenerate("UnitTest10.capnp.bin"); + var outerTypeDef = GetGeneratedFile("UnitTest10.capnp", model).NestedTypes.First(); var outerType = Model.Types.FromDefinition(outerTypeDef); var innerType = outerTypeDef.Fields[0].Type; var innerTypeDef = innerType.Definition; - var names = run.CodeGen.GetNames(); + var names = codegen.GetNames(); var outerNameSyntax = names.GetQName(outerType, outerTypeDef); var innerNameSyntax = names.GetQName(innerType, outerTypeDef); string[] outerNamespace = { "Foo", "Bar", "Baz" }; @@ -93,26 +93,22 @@ namespace CapnpC.CSharp.Generator.Tests LoadAndGenerate("schema-with-offsets.capnp.bin"); } - struct Run + static (Model.SchemaModel, CodeGen.CodeGenerator) LoadAndGenerate(string inputName) { - public Model.SchemaModel Model; - public CodeGen.CodeGenerator CodeGen; - public Model.GenFile FirstFile; - public string Code; + var model = Load(inputName); + var codegen = new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions()); + + var code = model.FilesToGenerate.Select(f => codegen.Transform(f)).ToArray(); + Assert.IsTrue(Util.InlineAssemblyCompiler.TryCompileCapnp(code), "Compilation was not successful"); + + return (model, codegen); } - static CodeGen.CodeGenerator NewGeneratorFor(Model.SchemaModel model) - => new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions()); - - Run LoadAndGenerate(string inputName) + static Model.GenFile GetGeneratedFile(string name, Model.SchemaModel model) { - var run = new Run(); - run.Model = Load(inputName); - run.CodeGen = NewGeneratorFor(run.Model); - run.FirstFile = run.Model.FilesToGenerate.First(); - run.Code = run.CodeGen.Transform(run.FirstFile); - Assert.IsTrue(Util.InlineAssemblyCompiler.TryCompileCapnp(run.Code), "Compilation was not successful"); - return run; + var file = model.FilesToGenerate.SingleOrDefault(f => f.Name.EndsWith(name)); + Assert.IsNotNull(file, $"Could not find '{name}' in generated files"); + return file; } static Model.TypeDefinition GetTypeDef(ulong id, Model.SchemaModel model) diff --git a/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest10.capnp.bin b/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest10.capnp.bin index dade3aa90ae451daf5849f33963c81461de09820..03a927de9b626f46d3f8e8f5e44809dcc7b397d7 100644 GIT binary patch delta 576 zcmcb>bAZQ@0SJzR2ryt~;04m+ObiSYf!G`>$_S>}z?1}#&C0;W;QgCzWy|HLU2>CG zGRn(-1d1vFn-DNvvZC}0Vs)q!*V3ttQPk#X$-tD>BMYoS-4e2{hsoP(Tz&L-YeVKp-&jqWEM7 zMh!tmphOc;fE7rq18GH&PR^YzJr4v6ASVRfD#N0lh3jgPyWH=G5H4bipd&mf|KX5a7!@)!v$Tnm49hT zYSH98rY@i|iOC7f73`pZVq{>NIA3z&1dhoM7$p?JMj{a~SHXoQA7qk`h4~C73Unm{ nKM;c=1QI1+1~h`0;1R(J6b8isFA#%565WYFA)tqzvPJ^{gr;S` diff --git a/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest11.capnp.bin b/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest11.capnp.bin index 536360967d118087b8d9a5df18835172038d5730..1d702abb501b8d84ceee9d791605ae3d1828508f 100644 GIT binary patch literal 2408 zcmeHJy=xRf6o0wZ(}03nXikU`R7@~lJd^~2mD(I)f|rPBAzYRfl49@J+e1*a6O0f+ zyGRTnpoNNsT8K75tS$5p(1-*rYyyG`N`7x<-`?DCb7=3vn>X{m=e;*GOGMP)fkAy9 zZ3gb~h)%5|8cL{|omFTrXzM9UQ#S^0-JB1W4`R)Xh3x3*fh%|CGPEJl(x?vRL%L_)Qs}v)!o{cDKS0=S8sSz4eC<_nkZW@&E`Z z1xftE7tH(T{M!1PVAsP=yMQxl$68}`W%hRe@pm6NUZ|z<+I8dommtuc#3%l8ShuR1 z<6EIbuagDNw|eSisr(2F_8aRiv-;F>b{rew=ePv$D`71zhw&KZ{5kBl z>;snP9%Q?6ud>}^0JB9r+`Rm;oWp*Lxbt>sO5f}aWz-s6e1~6iUq-gIQI7Y2U+Y{r z@|V=D(bsa{OJB?Rk-oMYQ>~42*WS;tEuQwD+>3T!7mfb-*FH?cN?TD|U`1nvH7uIOLoI zH)~E0hXr>Kj$E^_>=3Z4rO+m=gh8X{O!?&@dC^<;K!JzpZeEGz&~bsGhM}PZY4T83 v^|(POikzpc4xmFb&@F%icZzu~BU$uoRrH97`~-9jdVV^by8M6lw0rCuSF>j> diff --git a/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest4.capnp.bin b/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest4.capnp.bin index f93feb27a4e5c2b709d77a3cafed0b0d25619de7..edcead06ed05aaaaf80654caff95a2119c09bd73 100644 GIT binary patch literal 3600 zcmcImU5Hgx6y7seCzFImf)Sz8RQ#*?5h)tbARp?+IORBkLL$dIx44k^+~(Y=rXOm? zgvKD4mq;JVheU`1MT(*%4EiB49Ur0>A3}R@kf5QU2%_&hdw+BG-JLt553S2SYp=D} zT6^vFv+p*>n6oYTq);$RfL~o;Or_132OQTfx20$^cneLN+4km&KNegn{odhZnxQqV z#!O?e2blJD0pHzB82qi*zqoK{e9`xJVP3%a?)7)xdu!E>u~20X>@0>y6=zrL6QFMa zAuRq_fA^;^z4GUo#bPwU&q9A3Ya_sGfhU3I@>l*XYhq9ABd{|A;y5s4c4{8G7drad zC*R+4>`urs4vIIX4e&ASEJYl?2Ijd1XF)##;#$Cee7dLa$B(}Kkan3+*==pmHfGA% z{W;LT4ElL(;^C>kmvyu+ANXJmcqkt;@YTdGS58e`^+eL?gE-8K*mygz@9(3a3-D+V zxHa|6YTmo=wJ+cO>zmJR2e}Y@%Ay z_x93**TcMp{*FLz1MJF<_8kL1FK@rb_ZKg|aq!$d5QLfszE{RvhW_D30Z;b7Q{{gp zmRj&rd>c<|%z5W8?fd7w20Aak{qc%{auSaWm&!@ku4<(|SWfDjJ4;cmTB8@Yx&72X z?VvYcsP1{ckybo!#r$E6{&=ViWQx_NV&ZMjL`hOk(pqjoMXC;2+pNz!CI)~~>=TP199Q0dp<~f){okRZb9N7=9g#tQ=znd&B z=t8&1@pVqx<2inq{OzC76GJnk8MkV5pq$jAQW`8r12v74y%+Q2bQF52AHBU%wHnu> zdL^zVw3fk~@q!rh;nx@AzZ^r6wCn$O{#^PLeC$u4U)*1DCr|yRdSqUi$7aBcu@j#z z*HxOJY$MWsetc9;u9%&1$}`V})GNnywnVMRgn1UM#)S8BH!$m-J7>E~%9#5w_0O>m zH1+sipkCGW7tc#je_AU0YCYrFS;_Sy{|5-_UrS|Qt!ErN%a2pmcb#aI&f0KXkL#l~ zSKK_U?A_dcHbPmXGH-&u>ka#flH51ES27EBHt%_K0eFhEYwM9;fqbS?zp#F{2KkpAgUj5rOd1oos zzpoe8f2GIDDK5IlEjRmWJ>%F}WmCwxIeN}GG3K4_hFSeYG#nk6y@m7j8vBNxC_j1! zva0z15wz3|)t`U=g!MNR)g$iLSheIdLbG++6(1h*@sUYSOUon#lx+N604Oxv>rg5OXgb@9-a{ z<{sSIb^MB-r;>)~1~Pr{d`dsOJ(cNTm==@*<@8=!Q;Rem43QFuQ`y@}RqwhTXS_ig z^q!@%51enE*05(;k5E;gve&7AM!kWey`E7unp00l`^QEnPmWAX9z3&uIGY>G*}sb3 zcqYPpYa|BVaq9Bh`?YtkFvfOz?rNcKsaeIa;oK&B2Y+bXAG?&oA_Q}0$O%Rg#=O&1 z<`{=y%y(5^SFYu?kIXR=Fz%K!P39PTVcZz!J~78=@ZES83|z+s{ukF}QfT%<(w9KA eXXE~jb+{eQ-^LrW-;WN@H~(KF>wTn`!GXWhjj^%- diff --git a/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs b/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs index 8cd3cb0..8b00616 100644 --- a/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs +++ b/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs @@ -11,21 +11,22 @@ namespace CapnpC.CSharp.Generator.Tests.Util class InlineAssemblyCompiler { public static bool TryCompileCapnp(string code) + { + return TryCompileCapnp(new[] {code}); + } + + public static bool TryCompileCapnp(string[] code) { var options = new CSharpCompilationOptions( OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug); - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code); - string assemblyRoot = Path.GetDirectoryName(typeof(object).Assembly.Location); string capnpRuntimePath = Path.GetFullPath(Path.Combine( Assembly.GetExecutingAssembly().Location, @"..\..\..\..\..\Capnp.Net.Runtime\bin\Debug\netcoreapp2.1\Capnp.Net.Runtime.dll")); - var capnpRuntimeMetadataRef = MetadataReference.CreateFromFile(capnpRuntimePath); - var compilation = CSharpCompilation.Create( "CompilationTestAssembly", options: options, @@ -35,8 +36,8 @@ namespace CapnpC.CSharp.Generator.Tests.Util MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Core.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Runtime.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Private.CoreLib.dll")), - capnpRuntimeMetadataRef }, - syntaxTrees: new SyntaxTree[] { syntaxTree }); + MetadataReference.CreateFromFile(capnpRuntimePath) }, + syntaxTrees: Array.ConvertAll(code, new Converter(c => CSharpSyntaxTree.ParseText(c)))); using (var stream = new MemoryStream()) { @@ -47,9 +48,12 @@ namespace CapnpC.CSharp.Generator.Tests.Util if (!emitResult.Success) { - string path = Path.ChangeExtension(Path.GetTempFileName(), ".capnp.cs"); - File.WriteAllText(path, code); - Console.WriteLine($"[See {path} for generated code]"); + foreach (var c in code) + { + string path = Path.ChangeExtension(Path.GetTempFileName(), ".capnp.cs"); + File.WriteAllText(path, c); + Console.WriteLine($"[See {path} for generated code]"); + } } return emitResult.Success; From 449af92753247a828ed868a3306ed3a82bd3e44f Mon Sep 17 00:00:00 2001 From: aboulart <37553604+aboulart@users.noreply.github.com> Date: Wed, 11 Dec 2019 12:32:31 +0000 Subject: [PATCH 4/5] Simplify makefile --- .../CapnpC.CSharp.Generator.Tests.csproj | 39 ++----------------- 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/CapnpC.CSharp.Generator.Tests/CapnpC.CSharp.Generator.Tests.csproj b/CapnpC.CSharp.Generator.Tests/CapnpC.CSharp.Generator.Tests.csproj index 410f90a..e9f6fe6 100644 --- a/CapnpC.CSharp.Generator.Tests/CapnpC.CSharp.Generator.Tests.csproj +++ b/CapnpC.CSharp.Generator.Tests/CapnpC.CSharp.Generator.Tests.csproj @@ -22,45 +22,12 @@ - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - + From e339ac3c8c83f47286495c85203de8286dce939b Mon Sep 17 00:00:00 2001 From: aboulart <37553604+aboulart@users.noreply.github.com> Date: Wed, 11 Dec 2019 12:33:42 +0000 Subject: [PATCH 5/5] Fix setter of enums inside unions --- .../Embedded Resources/UnitTest12.capnp.bin | Bin 0 -> 1392 bytes .../No Resources/UnitTest12.capnp | 17 +++++++++++++++++ .../CodeGen/DomainClassSnippetGen.cs | 5 ++--- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest12.capnp.bin create mode 100644 CapnpC.CSharp.Generator.Tests/No Resources/UnitTest12.capnp diff --git a/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest12.capnp.bin b/CapnpC.CSharp.Generator.Tests/Embedded Resources/UnitTest12.capnp.bin new file mode 100644 index 0000000000000000000000000000000000000000..988e7d07aeb96d5f12ed22220f20c4bbacc394ca GIT binary patch literal 1392 zcmb7EJxIe)5WZNg3W_L#F8(ZnKTul{3dKPX6&w`Qy2l!%(k7)z5ClxD*@|7eRFKXRv%$QIM(FSB847-4L~8HPd^qE zMDB|5p}CXrxl=7OZxJ6w*%Pow6a(L&+a6z-2F0V{ug^p7%Y0>Dk>OwxJ-47~sF&{s zeCC@2N*1zDZ)KY*Pet83RGJJry{up?uWEC zDEAlV(Fvc=Q@lU+d7k2(v!8(SHJruS!YqrQxpOm#0{2f;c3~cTPxwC~g!k(b4p4k$ et8ZO(bJksS?xp-6@jUoFb3cA}fxHlBVLrc&3Cs-u literal 0 HcmV?d00001 diff --git a/CapnpC.CSharp.Generator.Tests/No Resources/UnitTest12.capnp b/CapnpC.CSharp.Generator.Tests/No Resources/UnitTest12.capnp new file mode 100644 index 0000000..c687260 --- /dev/null +++ b/CapnpC.CSharp.Generator.Tests/No Resources/UnitTest12.capnp @@ -0,0 +1,17 @@ +@0xb7069c462537ddd6; + +enum TestEnum { + a @0; + b @1; +} + +const globalConstant: TestEnum = b; + +struct Struct { + const structConstant: TestEnum = a; + + union { + enumValue @0: TestEnum; + intValue @1: Int64; + } +} diff --git a/CapnpC.CSharp.Generator/CodeGen/DomainClassSnippetGen.cs b/CapnpC.CSharp.Generator/CodeGen/DomainClassSnippetGen.cs index fb41086..a255b84 100644 --- a/CapnpC.CSharp.Generator/CodeGen/DomainClassSnippetGen.cs +++ b/CapnpC.CSharp.Generator/CodeGen/DomainClassSnippetGen.cs @@ -343,10 +343,9 @@ namespace CapnpC.CSharp.Generator.CodeGen return LiteralExpression(SyntaxKind.FalseLiteralExpression); case TypeTag.Enum: - return MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, + return CastExpression( _names.MakeTypeSyntax(field.Type, field.DeclaringType, TypeUsage.NotRelevant), - _names.UnionDiscriminatorUndefined.IdentifierName); + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); case TypeTag.F32: return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0.0f));