Skip to content

Commit

Permalink
(#72) CodeGen: preliminary changes to support function type generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Mar 28, 2022
1 parent 357dfb3 commit a01b895
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,10 @@ int main()
int exitCode = abs(-42);
exit(exitCode);
}");

[Fact]
public Task FunctionPtrTest() => DoTest(@"typedef int (*foo)(void);
int main() {
foo unused;
}");
}
3 changes: 3 additions & 0 deletions Cesium.CodeGen/Contexts/TranslationUnitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ internal void GenerateType(IGeneratedType type, string name)
_types.Add(name, typeReference);
}

// TODO[#72]: Unnecessary.
internal void AddType(TypeReference type, string name) => _types.Add(name, type);

internal TypeReference? GetTypeReference(IGeneratedType type) => _generatedTypes.GetValueOrDefault(type);
internal TypeReference? GetTypeReference(string typeName) => _types.GetValueOrDefault(typeName);
}
21 changes: 21 additions & 0 deletions Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ public static LocalDeclarationInfo Of(IReadOnlyList<IDeclarationSpecifier> speci
type = new PointerType(type);
break;

case DeclaratorDirectDeclarator ddd:
ddd.Deconstruct(out var nestedDeclarator);
var (nestedPointer, nestedDirectDeclarator) = nestedDeclarator;
if (nestedPointer != null)
{
var (nestedTypeQualifiers, nestedChildPointer) = nestedPointer;
if (nestedTypeQualifiers != null || nestedChildPointer != null)
throw new NotImplementedException(
$"Nested pointer of kind {nestedPointer} is not supported, yet.");

type = new PointerType(type);
}

// TODO[#72]: Rewrite this to append a pointer to the current type.
// TODO[#72]: "The current type", though, should be a function type already at this moment.
// TODO[#72]: This means that LocalDeclarationInfo should get rid of "Parameters" and they will
// become a part of the underlying type.
throw new NotImplementedException("TODO: This code is wrong.");
currentDirectDeclarator = nestedDirectDeclarator;
continue;

default: throw new NotImplementedException($"Direct declarator not supported, yet: {currentDirectDeclarator}.");
}

Expand Down
17 changes: 16 additions & 1 deletion Cesium.CodeGen/Ir/TopLevel/TopLevelDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Cesium.CodeGen.Extensions;
using Cesium.CodeGen.Ir.Declarations;
using Cesium.CodeGen.Ir.Types;
using Mono.Cecil;

namespace Cesium.CodeGen.Ir.TopLevel;

Expand Down Expand Up @@ -122,7 +123,7 @@ private static void EmitTypeDef(TranslationUnitContext context, TypeDefDeclarati
throw new NotSupportedException($"Anonymous typedef not supported: {type}.");

if (parametersInfo != null)
throw new NotImplementedException($"Function typedef not supported, yet: {identifier}.");
GenerateFunctionType(context, identifier, parametersInfo, type);

if (cliImportMemberName != null)
throw new NotSupportedException($"typedef for CLI import not supported: {cliImportMemberName}.");
Expand All @@ -133,4 +134,18 @@ private static void EmitTypeDef(TranslationUnitContext context, TypeDefDeclarati
throw new NotSupportedException($"Not supported type generation for type {type}.");
}
}

private static void GenerateFunctionType(
TranslationUnitContext context,
string identifier,
ParametersInfo parametersInfo,
IType returnType)
{
var type = new FunctionPointerType();

throw new NotImplementedException(
$"Function type not supported, yet: {identifier}: {parametersInfo}{returnType}");

context.AddType(type, identifier);
}
}

0 comments on commit a01b895

Please sign in to comment.