Skip to content

Commit

Permalink
(#234, #72) CodeGen: process a complex struct with a function-typed f…
Browse files Browse the repository at this point in the history
…ield
  • Loading branch information
ForNeVeR committed Oct 19, 2022
1 parent a7d264a commit 95d83a9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Module: Primary
Type: <Module>

Type: foo
Nested types:
Type: foo/<SyntheticBuffer>b
Pack: 0
Size: 20
Custom attributes:
- CompilerGeneratedAttribute()
- UnsafeValueTypeAttribute()

Fields:
System.Int32 foo/<SyntheticBuffer>b::FixedElementField

Type: foo/<SyntheticBuffer>c
Pack: 0
Size: 64
Custom attributes:
- CompilerGeneratedAttribute()
- UnsafeValueTypeAttribute()

Fields:
System.Byte foo/<SyntheticBuffer>c::FixedElementField

Type: foo/<SyntheticBuffer>array
Pack: 0
Size: 1600
Custom attributes:
- CompilerGeneratedAttribute()
- UnsafeValueTypeAttribute()

Fields:
System.Int32* foo/<SyntheticBuffer>array::FixedElementField
Fields:
System.Int32 foo::a
foo/<SyntheticBuffer>b foo::b
Custom attributes:
- FixedBufferAttribute(System.Int32, 20)

foo/<SyntheticBuffer>c foo::c
Custom attributes:
- FixedBufferAttribute(System.Byte, 64)

method System.Void *(System.Int32,System.Int32*,System.Int32*) foo::func
foo/<SyntheticBuffer>array foo::array
Custom attributes:
- FixedBufferAttribute(System.Int32*, 1600)

57 changes: 48 additions & 9 deletions Cesium.CodeGen/Ir/Declarations/LocalDeclarationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ public static LocalDeclarationInfo Of(IReadOnlyList<IDeclarationSpecifier> speci
return new LocalDeclarationInfo(type, null, null);

var (pointer, directDeclarator) = declarator;
if (pointer != null)
{
var (typeQualifiers, childPointer) = pointer;
if (typeQualifiers != null || childPointer != null)
throw new WipException(215, $"Complex pointer type is not supported, yet: {pointer}.");

type = new PointerType(type);
}

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

return new LocalDeclarationInfo(type, identifier, cliImportMemberName);
}

public static LocalDeclarationInfo Of(
IReadOnlyList<IDeclarationSpecifier> specifiers,
AbstractDeclarator abstractDeclarator)
{
var (type, cliImportMemberName) = ProcessSpecifiers(specifiers);

var (pointer, directAbstractDeclarator) = abstractDeclarator;
type = ProcessPointer(pointer, type);
type = ProcessDirectAbstractDeclarator(directAbstractDeclarator, type);

return new LocalDeclarationInfo(type, Identifier: null, cliImportMemberName);
}

private static (IType, string? CliImportMemberName) ProcessSpecifiers(
IReadOnlyList<IDeclarationSpecifier> specifiers)
{
Expand Down Expand Up @@ -119,6 +124,19 @@ private static (IType, string? CliImportMemberName) ProcessSpecifiers(
return (isConst ? new ConstType(type) : type, cliImportMemberName);
}

private static IType ProcessPointer(Pointer? pointer, IType type)
{
if (pointer == null) return type;

var (typeQualifiers, childPointer) = pointer;
if (typeQualifiers != null || childPointer != null)
throw new WipException(215, $"Complex pointer type is not supported, yet: {pointer}.");

type = new PointerType(type);

return type;
}

private static (IType, string? Identifier) ProcessDirectDeclarator(IDirectDeclarator directDeclarator, IType type)
{
string? identifier = null;
Expand Down Expand Up @@ -224,6 +242,27 @@ private static (IType, string? Identifier) ProcessDirectDeclarator(IDirectDeclar
return (type, identifier);
}

private static IType ProcessDirectAbstractDeclarator(
IDirectAbstractDeclarator? directAbstractDeclarator,
IType type)
{
var current = directAbstractDeclarator;
while (current != null)
{
switch (current)
{
default:
throw new WipException(
332,
$"Direct abstract declarator is not supported, yet: {current}.");
}

current = current.Base;
}

return type;
}

private static IType CreateArrayType(IType type, int size)
{
if (type is InPlaceArrayType inplaceArrayType)
Expand Down
13 changes: 7 additions & 6 deletions Cesium.CodeGen/Ir/ParametersInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ internal record ParameterInfo(IType Type, string? Name)
public static ParameterInfo Of(ParameterDeclaration declaration)
{
var (specifiers, declarator, abstractDeclarator) = declaration;
if (abstractDeclarator != null)
throw new WipException(
234,
$"Parameter with abstract declarator is not supported, yet: {declaration}.");

var (type, identifier, cliImportMemberName) = LocalDeclarationInfo.Of(specifiers, declarator);
var (type, identifier, cliImportMemberName) = (declarator, abstractDeclarator) switch
{
(null, { }) => LocalDeclarationInfo.Of(specifiers, abstractDeclarator),
(_, null) => LocalDeclarationInfo.Of(specifiers, declarator),
_ => throw new AssertException(
$"Both declarator and abstract declarator found for declaration {declaration}.")
};

if (cliImportMemberName != null)
throw new CompilationException("CLI import specifier isn't supported for a parameter.");
Expand Down

0 comments on commit 95d83a9

Please sign in to comment.