Skip to content

Commit

Permalink
(#72) CodeGen: support variables of typedef'd type
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Feb 16, 2022
1 parent bb3ca22 commit 9574253
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Module: Primary
Type: <Module>
Methods:
System.Int32 <Module>::main()
Locals:
foo V_0
IL_0000: ldc.i4 0
IL_0005: ret

Type: foo
Fields:
System.Int32 foo::x
Init with: []
7 changes: 4 additions & 3 deletions Cesium.CodeGen/Contexts/TranslationUnitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ public record TranslationUnitContext(AssemblyContext AssemblyContext)
public TypeDefinition ModuleType => Module.GetType("<Module>");
internal Dictionary<string, FunctionInfo> Functions => AssemblyContext.Functions;

private readonly Dictionary<INamedType, TypeReference> _generatedTypes = new();
private readonly Dictionary<IGeneratedType, TypeReference> _generatedTypes = new();
private readonly Dictionary<string, TypeReference> _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);
}
9 changes: 9 additions & 0 deletions Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Cesium.CodeGen.Ir.Types;

internal interface INamedType
internal interface IGeneratedType
{
TypeDefinition Emit(string name, TranslationUnitContext context);
}
10 changes: 10 additions & 0 deletions Cesium.CodeGen/Ir/Types/NamedType.cs
Original file line number Diff line number Diff line change
@@ -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}.");
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Types/StructType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Cesium.CodeGen.Ir.Types;

internal class StructType : IType, INamedType
internal class StructType : IType, IGeneratedType
{
private readonly IEnumerable<LocalDeclarationInfo> _members;
public StructType(IEnumerable<LocalDeclarationInfo> members)
Expand Down

0 comments on commit 9574253

Please sign in to comment.