Skip to content

Commit

Permalink
(#72) CodeGen: implement for function types
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jul 23, 2022
1 parent e29d8b9 commit 57a23f2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
40 changes: 26 additions & 14 deletions Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public static LocalDeclarationInfo Of(IReadOnlyList<IDeclarationSpecifier> speci
type = new PointerType(type);
}

string? identifier = null;

(type, identifier) = ProcessDirectDeclarator(directDeclarator, type, identifier);
(type, var identifier) = ProcessDirectDeclarator(directDeclarator, type);

return new LocalDeclarationInfo(type, identifier, cliImportMemberName);
}
Expand Down Expand Up @@ -120,11 +118,10 @@ private static (IType, string? CliImportMemberName) ProcessSpecifiers(
return (isConst ? new ConstType(type) : type, cliImportMemberName);
}

private static (IType, string? Identifier) ProcessDirectDeclarator(
IDirectDeclarator directDeclarator,
IType type,
string? identifier)
private static (IType, string? Identifier) ProcessDirectDeclarator(IDirectDeclarator directDeclarator, IType type)
{
string? identifier = null;

var currentDirectDeclarator = directDeclarator;
while (currentDirectDeclarator != null)
{
Expand Down Expand Up @@ -191,13 +188,28 @@ private static (IType, string? Identifier) ProcessDirectDeclarator(
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;
// The only kind of nested direct declarator we support is this one:
if (nestedDirectDeclarator is IdentifierDirectDeclarator idd)
{
if (idd.Base != null)
throw new NotSupportedException(
$"Not supported nested direct declarator with base: {idd}.");

idd.Deconstruct(out var nestedIdentifier);
if (identifier != null && nestedIdentifier != null)
throw new NotSupportedException(
$"Identifier conflict: nested identifier \"{nestedIdentifier}\"" +
$" tries to override \"{identifier}.\"");

identifier = nestedIdentifier;
}
else
{
throw new NotSupportedException(
$"Not supported nested direct declarator: {nestedDirectDeclarator}.");
}

break;

default: throw new NotImplementedException($"Direct declarator not supported, yet: {currentDirectDeclarator}.");
}
Expand Down
39 changes: 37 additions & 2 deletions Cesium.CodeGen/Ir/Types/FunctionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,42 @@ namespace Cesium.CodeGen.Ir.Types;
internal record FunctionType(ParametersInfo? Parameters, IType ReturnType) : IType
{
public TypeReference Resolve(TranslationUnitContext context) =>
throw new NotImplementedException();
throw new NotSupportedException($"Function type {this} cannot be directly expressed in the byte code.");

public int SizeInBytes => throw new NotImplementedException("Could not calculate size yet.");
public TypeReference ResolvePointer(TranslationUnitContext context)
{
var pointer = new FunctionPointerType
{
ReturnType = ReturnType.Resolve(context)
};

if (Parameters is var (parameterInfos, isVoid, isVarArg))
{
if (isVoid)
{
if (parameterInfos.Count > 0 || isVarArg)
throw new NotSupportedException(
$"Invalid function pointer type {this}: declared as void " +
$"but has parameters or declared as vararg.");
}

if (isVarArg)
throw new NotImplementedException($"A pointer to a vararg function is not implemented, yet: {this}.");

foreach (var (type, name) in parameterInfos)
{
pointer.Parameters.Add(new ParameterDefinition(type.Resolve(context))
{
Name = name
});
}
}

return pointer;
}

public int SizeInBytes => throw new NotSupportedException($"Function type {this} has no defined size.");

public override string ToString() =>
$"FunctionType {{ {nameof(Parameters)} = {Parameters}, {nameof(ReturnType)} = {ReturnType} }}";
}
10 changes: 8 additions & 2 deletions Cesium.CodeGen/Ir/Types/PointerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ namespace Cesium.CodeGen.Ir.Types;

internal record PointerType(IType Base) : IType
{
public virtual TypeReference Resolve(TranslationUnitContext context) => Base.Resolve(context).MakePointerType();
public virtual TypeReference Resolve(TranslationUnitContext context)
{
if (Base is FunctionType ft)
return ft.ResolvePointer(context);

return Base.Resolve(context).MakePointerType();
}

public virtual int SizeInBytes => throw new NotImplementedException("Could not calculate size yet.");

// explicit impl while Size not implemented
public override string ToString()
=> $"PointerType {{ Base = {Base} }}";
}
}

0 comments on commit 57a23f2

Please sign in to comment.