Skip to content

Commit

Permalink
Allow using initializer lists on the primitive types (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 authored Nov 11, 2024
1 parent 97c0d7c commit a5e06d7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Cesium.CodeGen.Tests/CodeGenPrimitiveTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
using Cesium.CodeGen.Ir.Declarations;
using Cesium.CodeGen.Ir.Types;
using Cesium.Parser;
using JetBrains.Annotations;
using Yoakke.SynKit.C.Syntax;

namespace Cesium.CodeGen.Tests;

public class CodeGenPrimitiveTypeTests
public class CodeGenPrimitiveTypeTests : CodeGenTestBase
{
[MustUseReturnValue]
private static Task DoTest(string source)
{
var assembly = GenerateAssembly(default, source);

var moduleType = assembly.Modules.Single().GetType("<Module>");
return VerifyMethods(moduleType);
}

[Theory]
[InlineData("char", PrimitiveTypeKind.Char)]
[InlineData("int", PrimitiveTypeKind.Int)]
Expand All @@ -24,4 +34,16 @@ internal void Test(string typeString, PrimitiveTypeKind expectedKind)
var type = (PrimitiveType)item.Declaration.Type;
Assert.Equal(expectedKind, type.Kind);
}

[Fact]
public Task PrimitiveInitializer() => DoTest(@"int main() {
int a = { 10 };
return 0;
}");

[Fact]
public Task PrimitiveEmptyInitializer() => DoTest(@"int main() {
int a = { };
return 0;
}");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
System.Int32 <Module>::main()
Locals:
System.Int32 V_0
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldc.i4.0
IL_0003: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::main()
IL_0005: stloc.s V_0
IL_0007: ldloc.s V_0
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
IL_000e: ldloc.s V_0
IL_0010: ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
System.Int32 <Module>::main()
Locals:
System.Int32 V_0
IL_0000: ldc.i4.s 10
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::main()
IL_0005: stloc.s V_0
IL_0007: ldloc.s V_0
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
IL_000e: ldloc.s V_0
IL_0010: ret
8 changes: 8 additions & 0 deletions Cesium.CodeGen/Ir/Declarations/ScopedDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Cesium.Ast;
using Cesium.CodeGen.Extensions;
using Cesium.CodeGen.Ir.Expressions;
using Cesium.CodeGen.Ir.Expressions.Constants;
using Cesium.CodeGen.Ir.Types;
using Cesium.Core;

Expand Down Expand Up @@ -128,6 +129,13 @@ private static InitializableDeclarationInfo IdentifierOf(
return new CompoundObjectInitializationExpression(type, expr);
}

if (type is PrimitiveType primitiveType)
{
if (arrayInitializer.Initializers.Length == 0) return new Expressions.ConstantLiteralExpression(new IntegerConstant(0));
if (arrayInitializer.Initializers.Length == 1) return ConvertInitializer(type, arrayInitializer.Initializers[0]);
throw new CompilationException($"Primitive types cannot be initialized using more then one initializer list.");
}

if (arrayInitializer.Initializers.Length == 0)
{
return null;
Expand Down

0 comments on commit a5e06d7

Please sign in to comment.