Skip to content

Commit

Permalink
Merge branch 'main' into feature/354.wide-arch
Browse files Browse the repository at this point in the history
# Conflicts:
#	Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs
#	Cesium.CodeGen.Tests/verified/ArchitectureDependentCodeTests.PointerArrayMemberAssign_arch=Bit32.verified.txt
#	Cesium.CodeGen.Tests/verified/ArchitectureDependentCodeTests.PointerArrayMemberAssign_arch=Bit64.verified.txt
#	Cesium.CodeGen.Tests/verified/ArchitectureDependentCodeTests.PointerArrayMemberAssign_arch=Dynamic.verified.txt
#	Cesium.CodeGen/Ir/BlockItems/FunctionDeclaration.cs
#	Cesium.CodeGen/Ir/BlockItems/FunctionDefinition.cs
#	Cesium.Runtime/RuntimeHelpers.cs
#	Cesium.Runtime/StdIoFunctions.cs
#	Cesium.Runtime/StdLibFunctions.cs
#	Cesium.Runtime/StringFunctions.cs
#	docs/architecture-sets.md
#	docs/type-system.md
  • Loading branch information
ForNeVeR committed Jul 29, 2023
2 parents b5512a0 + 29dcd88 commit 665c544
Show file tree
Hide file tree
Showing 337 changed files with 7,236 additions and 3,040 deletions.
40 changes: 0 additions & 40 deletions .github/workflows/run-build-and-integration-tests.yml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/run-build-and-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- macos-latest
- ubuntu-latest
- windows-latest
fail-fast: false
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
Expand Down
8 changes: 8 additions & 0 deletions Cesium.Ast/Declarations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;

namespace Cesium.Ast;
using SpecifierQualifierList = ImmutableArray<ISpecifierQualifierListItem>;

// 6.7 Declarations
public record Declaration(
Expand All @@ -22,6 +23,9 @@ public record StructOrUnionSpecifier(
ComplexTypeKind TypeKind,
string? Identifier,
ImmutableArray<StructDeclaration> StructDeclarations) : ITypeSpecifier;
public record EnumSpecifier(
string? Identifier,
ImmutableArray<EnumDeclaration>? StructDeclarations) : ITypeSpecifier;

public record NamedTypeSpecifier(string TypeDefName) : ITypeSpecifier;

Expand All @@ -39,10 +43,13 @@ public interface ISpecifierQualifierListItem : IDeclarationSpecifier {}

public record StructDeclarator(Declarator Declarator);

public record EnumDeclaration(string Identifier, Expression? Constant);

// 6.7.3 Type qualifiers
public record TypeQualifier(string Name) : ISpecifierQualifierListItem;

// 6.7.7 Type names
public record TypeName(SpecifierQualifierList SpecifierQualifierList, AbstractDeclarator? AbstractDeclarator = null);
public record AbstractDeclarator(Pointer? Pointer = null, IDirectAbstractDeclarator? DirectAbstractDeclarator = null);
public interface IDirectAbstractDeclarator
{
Expand Down Expand Up @@ -93,6 +100,7 @@ public record ParameterDeclaration(
// 6.7.9 Initialization
public abstract record Initializer;
public record AssignmentInitializer(Expression Expression) : Initializer;
public record ArrayInitializer(ImmutableArray<Initializer> Initializers) : Initializer;

// CLI extensions
public record CliImportSpecifier(string MemberName) : IDeclarationSpecifier;
8 changes: 5 additions & 3 deletions Cesium.Ast/Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public abstract record Expression;
// 6.5.1 Primary expressions
public record IdentifierExpression(string Identifier) : Expression;
public record ConstantLiteralExpression(IToken<CTokenType> Constant) : Expression;
public record ParenExpression(Expression Contents) : Expression;

// 6.5.2 Postfix operators
public record SubscriptingExpression(Expression Base, Expression Index) : Expression;
public record FunctionCallExpression(Expression Function, ImmutableArray<Expression>? Arguments) : Expression;
public record TypeCastOrNamedFunctionCallExpression(string TypeOrFunctionName, ImmutableArray<Expression> Arguments) : Expression;
public record MemberAccessExpression(Expression Target, IdentifierExpression Identifier) : Expression;
public record PointerMemberAccessExpression(Expression Target, IdentifierExpression Identifier) : Expression;
public record PostfixIncrementDecrementExpression(IToken<CTokenType> PrefixOperator, Expression Target) : Expression;
Expand All @@ -22,6 +24,9 @@ public record PrefixIncrementDecrementExpression(IToken<CTokenType> PrefixOperat
public record UnaryOperatorExpression(string Operator, Expression Target) : Expression;
public record IndirectionExpression(Expression Target) : Expression;

// 6.5.4 Cast expression
public record CastExpression(TypeName TypeName, Expression Target) : Expression;

// 6.5.5–6.5.14: Various binary operators
public record BinaryOperatorExpression(Expression Left, string Operator, Expression Right) : Expression;
public record LogicalBinaryOperatorExpression(Expression Left, string Operator, Expression Right)
Expand All @@ -43,6 +48,3 @@ public record AssignmentExpression(Expression Left, string Operator, Expression

// 6.5.17 Comma operator
public record CommaExpression(Expression Left, Expression Right) : Expression;

// 6.6 Constant expressions
public record ConstantExpression(Expression Expression) : Expression;
3 changes: 2 additions & 1 deletion Cesium.Ast/Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract record Statement : IBlockItem;
// 6.8.1 Labeled statement
public record LabelStatement(string Identifier, Statement Body) : Statement;

public record CaseStatement(ConstantExpression? Constant, Statement Body) : Statement;
public record CaseStatement(Expression? Constant, Statement Body) : Statement;

// 6.8.2 Compound statement
public record CompoundStatement(ImmutableArray<IBlockItem> Block) : Statement;
Expand Down Expand Up @@ -41,6 +41,7 @@ public record DoWhileStatement(
IBlockItem Body) : Statement;

public record ForStatement(
IBlockItem? InitDeclaration,
Expression? InitExpression,
Expression? TestExpression,
Expression? UpdateExpression,
Expand Down
23 changes: 23 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public Task ArrayAssignment() => DoTest(@"int main() {
return a[1];
}");

[Fact]
public Task SymmetricArrayAssignment() => DoTest(@"int main() {
int a[10];
1[a] = 2;
return a[1];
}");

[Fact]
public Task GlobalArrayAssignment() => DoTest(@"
int a[10];
Expand Down Expand Up @@ -75,5 +82,21 @@ public Task ArrayElementIndexViaVariable() => DoTest(@"int main() {
int i = 0;
a[i][0] = 13;
return 0;
}");

[Fact]
public Task ArrayInitialization() => DoTest(@"int main() {
int a[4] = { 1, 2, 3, 4, };
a[1] = 2;
return a[1];
}");

[Fact]
public Task GlobalArrayInitialization() => DoTest(@"
int a[4] = { 1, 2, 3, 4, };
int main() {
a[1] = 2;
return a[1];
}");
}
31 changes: 29 additions & 2 deletions Cesium.CodeGen.Tests/CodeGenContinueStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ private static Task DoTest(string source)
[Fact]
public Task ContinueInFor() => DoTest(@"int main()
{
int i;
int i;
for(i = 0; i < 10; ++i) continue;
}");

[Fact]
public Task ContinueInWhile() => DoTest(@"int main()
{
int i = 0;
int i = 0;
while (i < 10) {
++i;
if (i == 0) continue;
Expand All @@ -48,4 +48,31 @@ public Task ContinueNotInFor() => Assert.ThrowsAsync<CompilationException>(
{
continue;
}"));

[Fact]
public void ContinueInSwitch() => DoesNotCompile(@"int main()
{
switch (1)
{
default:
continue;
}
}", "Can't use continue outside of a loop construct.");

[Fact]
public Task ContinueInSwitchEnclosedWithLoop() => DoTest(@"int main(int argc, char *argv[])
{
for (;;)
{
switch (1)
{
case 42:
break;
default:
continue;
}
break;
}
}");
}
43 changes: 43 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenEnumTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using JetBrains.Annotations;

namespace Cesium.CodeGen.Tests;

public class CodeGenEnumTests : CodeGenTestBase
{
[MustUseReturnValue]
private static Task DoTest(string source)
{
var assembly = GenerateAssembly(default, source);

var moduleType = assembly.Modules.Single().GetType("<Module>");
return VerifyMethods(moduleType);
}

[Fact]
public Task EnumDeclarationTest() => DoTest(@"
enum Colour { Red, Green, Blue };
void test()
{
enum Colour x = Green;
}");

[Fact]
public Task EnumDeclarationIntTest() => DoTest(@"
enum Colour { Red, Green, Blue };
void test()
{
enum Colour x = 42;
}");

[Fact]
public Task EnumUsageInIf() => DoTest(@"
enum Colour { Red, Green, Blue };
void test()
{
enum Colour x = 42;
if (x == Green) ;
}");
}
7 changes: 7 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenForTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ public Task For_Empty() => DoTest(
int i;
for(;;) ++i;
}");

[Fact]
public Task For_WithDeclaration() => DoTest(
@"int main()
{
for(int i = 0; i < 10; ++i) ++i;
}");
}
96 changes: 94 additions & 2 deletions Cesium.CodeGen.Tests/CodeGenMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ private static Task DoTest(string source)
{
var assembly = GenerateAssembly(default, source);

var moduleType = assembly.Modules.Single().GetType("<Module>");
return VerifyMethods(moduleType);
var module = assembly.Modules.Single();
var moduleType = module.GetType("<Module>");
var staticType = module.GetType("testInput<Statics>");
return VerifyMethods(new[] { moduleType, staticType });
}

[Fact]
Expand Down Expand Up @@ -144,6 +146,15 @@ void test()
console_read(5, 67.44);
}");

[Fact]
public Task TypeDefDeclaration() => DoTest(@"typedef void FILE;
FILE* console_read(FILE* stream);
FILE* console_read(FILE* stream)
{
return 0;
}");

[Fact]
public Task ImplicitVarargDeclarationCanBeIgnored() => DoTest(@"void console_read();
Expand Down Expand Up @@ -337,4 +348,85 @@ int main()
}
");

[Fact]
public Task ValidPointerSubtractionTest() => DoTest(@"int main() {
int foo[10];
return &foo[10] - &foo[1];
}");

[Fact]
public void InvalidPointerWithIntSubtractionTest() => DoesNotCompile(@"int main() {
int foo[10];
return &foo[10] - 123;
}", "Operator Subtract is not supported for pointer/value operands");

[Fact]
public void PointerSubtractionWithTypeMismatchTest() => DoesNotCompile(@"typedef struct {
int a;
} bar;
int main() {
int foo[10];
bar* qux = (bar*) 123;
return &foo[10] - qux;
}", "Invalid pointer subtraction - pointers are referencing different base types");

[Fact]
public Task StaticMethod() => DoTest(@"static int main()
{
int x = 0;
++x;
return x + 1;
}");

[Fact]
public Task StaticSpecificedDuringDeclaration() => DoTest(@"static int main();
int main()
{
int x = 0;
++x;
return x + 1;
}");

[Fact]
public Task StaticSpecificedDuringDefinition() => DoTest(@"int main();
static int main()
{
int x = 0;
++x;
return x + 1;
}");

[Fact]
public Task FunctionPointerCallTest() => DoTest(@"int foo(int a) { return a; }
int main()
{
int (*fooptr)(int) = &foo;
return fooptr(123);
}");

[Fact]
public void NonFunctionPointerCallTest() => DoesNotCompile(@"int foo(int a) { return a; }
int main()
{
void *fooptr = &foo;
return fooptr(123);
}", "Attempted to call non-function pointer");

// TODO [#196]
/* [Fact]
public Task VarargFunctionPointerCallTest() => DoTest(@"int foo(int a, ...) { return a; }
int main()
{
int (*fooptr)(int, ...) = &foo;
return fooptr(123, 456);
}"); */
}
Loading

0 comments on commit 665c544

Please sign in to comment.