From 6d4ee31784545f2d9fb8ed783b981be70780c499 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Wed, 16 Feb 2022 23:25:56 +0700 Subject: [PATCH] (#72) CodeGen: support variables of typedef'd type --- ...CodeGenTypeTests.TypeDefStructUsage.verified.txt | 13 +++++++++++++ Cesium.CodeGen/Contexts/TranslationUnitContext.cs | 7 ++++--- .../Ir/Declarations/LocalDeclarationInfo.cs | 9 +++++++++ Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs | 2 +- .../Ir/Types/{INamedType.cs => IGeneratedType.cs} | 2 +- Cesium.CodeGen/Ir/Types/NamedType.cs | 10 ++++++++++ Cesium.CodeGen/Ir/Types/StructType.cs | 2 +- 7 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 Cesium.CodeGen.Tests/CodeGenTypeTests.TypeDefStructUsage.verified.txt rename Cesium.CodeGen/Ir/Types/{INamedType.cs => IGeneratedType.cs} (82%) create mode 100644 Cesium.CodeGen/Ir/Types/NamedType.cs diff --git a/Cesium.CodeGen.Tests/CodeGenTypeTests.TypeDefStructUsage.verified.txt b/Cesium.CodeGen.Tests/CodeGenTypeTests.TypeDefStructUsage.verified.txt new file mode 100644 index 00000000..cdc24b16 --- /dev/null +++ b/Cesium.CodeGen.Tests/CodeGenTypeTests.TypeDefStructUsage.verified.txt @@ -0,0 +1,13 @@ +Module: Primary + Type: + Methods: + System.Int32 ::main() + Locals: + foo V_0 + IL_0000: ldc.i4 0 + IL_0005: ret + + Type: foo + Fields: + System.Int32 foo::x + Init with: [] diff --git a/Cesium.CodeGen/Contexts/TranslationUnitContext.cs b/Cesium.CodeGen/Contexts/TranslationUnitContext.cs index 1996b9c0..e51a35b3 100644 --- a/Cesium.CodeGen/Contexts/TranslationUnitContext.cs +++ b/Cesium.CodeGen/Contexts/TranslationUnitContext.cs @@ -12,15 +12,16 @@ public record TranslationUnitContext(AssemblyContext AssemblyContext) public TypeDefinition ModuleType => Module.GetType(""); internal Dictionary Functions => AssemblyContext.Functions; - private readonly Dictionary _generatedTypes = new(); + private readonly Dictionary _generatedTypes = new(); private readonly Dictionary _types = new(); - internal void GenerateType(INamedType type, string name) + internal void GenerateType(IGeneratedType type, string name) { var typeReference = type.Emit(name, this); _generatedTypes.Add(type, typeReference); _types.Add(name, typeReference); } - internal TypeReference? GetTypeReference(INamedType type) => _generatedTypes.GetValueOrDefault(type); + internal TypeReference? GetTypeReference(IGeneratedType type) => _generatedTypes.GetValueOrDefault(type); + internal TypeReference? GetTypeReference(string typeName) => _types.GetValueOrDefault(typeName); } diff --git a/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs b/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs index 1c363556..2625e915 100644 --- a/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs +++ b/Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs @@ -109,6 +109,15 @@ private static (IType, string? cliImportMemberName) ProcessSpecifiers( }); break; + case NamedTypeSpecifier nt: + if (type != null) + throw new NotSupportedException( + $"Unsupported type name after already resolved type {type}: {nt}."); + + nt.Deconstruct(out var typeName); + type = new NamedType(typeName); + break; + case TypeQualifier tq: switch (tq.Name) { diff --git a/Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs b/Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs index 678fba8b..513cf6d4 100644 --- a/Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs +++ b/Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs @@ -127,7 +127,7 @@ private static void EmitTypeDef(TranslationUnitContext context, TypeDefDeclarati if (cliImportMemberName != null) throw new NotSupportedException($"typedef for CLI import not supported: {cliImportMemberName}."); - if (type is INamedType t) + if (type is IGeneratedType t) context.GenerateType(t, identifier); else throw new NotSupportedException($"Not supported type generation for type {type}."); diff --git a/Cesium.CodeGen/Ir/Types/INamedType.cs b/Cesium.CodeGen/Ir/Types/IGeneratedType.cs similarity index 82% rename from Cesium.CodeGen/Ir/Types/INamedType.cs rename to Cesium.CodeGen/Ir/Types/IGeneratedType.cs index 91ddbd67..45f2d312 100644 --- a/Cesium.CodeGen/Ir/Types/INamedType.cs +++ b/Cesium.CodeGen/Ir/Types/IGeneratedType.cs @@ -3,7 +3,7 @@ namespace Cesium.CodeGen.Ir.Types; -internal interface INamedType +internal interface IGeneratedType { TypeDefinition Emit(string name, TranslationUnitContext context); } diff --git a/Cesium.CodeGen/Ir/Types/NamedType.cs b/Cesium.CodeGen/Ir/Types/NamedType.cs new file mode 100644 index 00000000..3080cd3c --- /dev/null +++ b/Cesium.CodeGen/Ir/Types/NamedType.cs @@ -0,0 +1,10 @@ +using Cesium.CodeGen.Contexts; +using Mono.Cecil; + +namespace Cesium.CodeGen.Ir.Types; + +public record NamedType(string TypeName) : IType +{ + public TypeReference Resolve(TranslationUnitContext context) => + context.GetTypeReference(TypeName) ?? throw new NotSupportedException($"Type not found: {TypeName}."); +} diff --git a/Cesium.CodeGen/Ir/Types/StructType.cs b/Cesium.CodeGen/Ir/Types/StructType.cs index 9ff73155..da10c8e7 100644 --- a/Cesium.CodeGen/Ir/Types/StructType.cs +++ b/Cesium.CodeGen/Ir/Types/StructType.cs @@ -4,7 +4,7 @@ namespace Cesium.CodeGen.Ir.Types; -internal class StructType : IType, INamedType +internal class StructType : IType, IGeneratedType { private readonly IEnumerable _members; public StructType(IEnumerable members)