Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter abstract declarators #333

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,15 @@ int main(void) {
foo x;
return x.nonExisting;
}", "\"foo\" has no member named \"nonExisting\"");

[Fact]
public Task ComplexStructDefinition() => DoTest(@"typedef void(*function)(int, const int*, const int*);
typedef struct {
int a;
int b[5];
unsigned char c[64];
function func;

int array[80][5];
} foo;");
}
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