From cc7c949b7dd645f84457ffc002b0b5d22d09c1f3 Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Sun, 27 Oct 2024 14:35:41 +0100 Subject: [PATCH] Refactored syntax factory --- .../Semantics/ControlFlowGraphTests.cs | 4 +- .../Semantics/DefiniteAssignmentTests.cs | 30 +-- .../Semantics/DocumentationCommentsTests.cs | 8 +- .../Semantics/LocalFunctionsTests.cs | 8 +- .../Semantics/SemanticModelTests.cs | 14 +- .../Semantics/SingleAssignmentTests.cs | 14 +- .../Semantics/SymbolResolutionTests.cs | 174 +++++++++--------- .../Semantics/TypeCheckingTests.cs | 88 ++++----- .../Api/Syntax/SyntaxFactory.cs | 60 ++++-- .../AutoProperty/AutoPropertyGetterSymbol.cs | 2 +- 10 files changed, 217 insertions(+), 185 deletions(-) diff --git a/src/Draco.Compiler.Tests/Semantics/ControlFlowGraphTests.cs b/src/Draco.Compiler.Tests/Semantics/ControlFlowGraphTests.cs index 50583b8fa..22016e326 100644 --- a/src/Draco.Compiler.Tests/Semantics/ControlFlowGraphTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/ControlFlowGraphTests.cs @@ -119,7 +119,7 @@ public async Task WhileLoop() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("i", null, LiteralExpression(0))), + DeclarationStatement(VarDeclaration("i", null, LiteralExpression(0))), ExpressionStatement(WhileExpression( RelationalExpression(NameExpression("i"), ComparisonElement(LessThan, LiteralExpression(10))), BlockExpression( @@ -184,7 +184,7 @@ public async Task ConditionalGotoInAssignment() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, IfExpression( + DeclarationStatement(VarDeclaration("x", null, IfExpression( NameExpression("b"), GotoExpression(NameLabel("noassign")), LiteralExpression(1)))), diff --git a/src/Draco.Compiler.Tests/Semantics/DefiniteAssignmentTests.cs b/src/Draco.Compiler.Tests/Semantics/DefiniteAssignmentTests.cs index 546027ccc..7170fd5d1 100644 --- a/src/Draco.Compiler.Tests/Semantics/DefiniteAssignmentTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/DefiniteAssignmentTests.cs @@ -20,8 +20,8 @@ public void UseOfUnassignedVariable() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), - DeclarationStatement(VariableDeclaration("y", NameType("int32"), NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("y", NameType("int32"), NameExpression("x"))))))); // Act var compilation = CreateCompilation(tree); @@ -46,8 +46,8 @@ public void UseOfAssignedVariable() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"), LiteralExpression(0))), - DeclarationStatement(VariableDeclaration("y", NameType("int32"), NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("x", NameType("int32"), LiteralExpression(0))), + DeclarationStatement(VarDeclaration("y", NameType("int32"), NameExpression("x"))))))); // Act var compilation = CreateCompilation(tree); @@ -72,12 +72,12 @@ public void UseOfConditionallyAssignedVariable() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("x", NameType("int32"))), ExpressionStatement(IfExpression( NameExpression("b"), StatementExpression(ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(42)))), null as ExpressionSyntax)), - DeclarationStatement(VariableDeclaration("y", NameType("int32"), NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("y", NameType("int32"), NameExpression("x"))))))); // Act var compilation = CreateCompilation(tree); @@ -103,12 +103,12 @@ public void UseOfConditionallyAssignedVariableOnBothBranches() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("x", NameType("int32"))), ExpressionStatement(IfExpression( NameExpression("b"), BinaryExpression(NameExpression("x"), Assign, LiteralExpression(42)), BinaryExpression(NameExpression("x"), Assign, LiteralExpression(0)))), - DeclarationStatement(VariableDeclaration("y", NameType("int32"), NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("y", NameType("int32"), NameExpression("x"))))))); // Act var compilation = CreateCompilation(tree); @@ -133,12 +133,12 @@ public void JumpingMidAssignment() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"), IfExpression( + DeclarationStatement(VarDeclaration("x", NameType("int32"), IfExpression( NameExpression("b"), GotoExpression(NameLabel("end")), LiteralExpression(42)))), DeclarationStatement(LabelDeclaration("end")), - DeclarationStatement(VariableDeclaration("y", NameType("int32"), NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("y", NameType("int32"), NameExpression("x"))))))); // Act var compilation = CreateCompilation(tree); @@ -163,7 +163,7 @@ public void CompoundAssigningUnassignedVariable() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("x", NameType("int32"))), ExpressionStatement(BinaryExpression(NameExpression("x"), PlusAssign, LiteralExpression(42))))))); // Act @@ -195,8 +195,8 @@ public void UseVariableAssignedLater() ExpressionStatement(WhileExpression( NameExpression("b"), BlockExpression( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), - DeclarationStatement(VariableDeclaration("y", NameType("int32"), NameExpression("x"))), + DeclarationStatement(VarDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("y", NameType("int32"), NameExpression("x"))), ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(42)))))))))); // Act @@ -215,7 +215,7 @@ public void GlobalImmutableInitialized() // Arrange // val x: int32 = 0; var tree = SyntaxTree.Create(CompilationUnit( - ImmutableVariableDeclaration("x", NameType("int32"), LiteralExpression(0)))); + ValDeclaration("x", NameType("int32"), LiteralExpression(0)))); // Act var compilation = CreateCompilation(tree); @@ -232,7 +232,7 @@ public void GlobalImmutableNotInitialized() // Arrange // val x: int32; var tree = SyntaxTree.Create(CompilationUnit( - ImmutableVariableDeclaration("x", NameType("int32")))); + ValDeclaration("x", NameType("int32")))); // Act var compilation = CreateCompilation(tree); diff --git a/src/Draco.Compiler.Tests/Semantics/DocumentationCommentsTests.cs b/src/Draco.Compiler.Tests/Semantics/DocumentationCommentsTests.cs index 6805bcd87..4e42761ba 100644 --- a/src/Draco.Compiler.Tests/Semantics/DocumentationCommentsTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/DocumentationCommentsTests.cs @@ -93,7 +93,7 @@ public void VariableDocumentationComment(string docComment) // Arrange var tree = SyntaxTree.Create(CompilationUnit( - WithDocumentation(VariableDeclaration( + WithDocumentation(VarDeclaration( "x", null, LiteralExpression(0)), @@ -169,14 +169,14 @@ public void ModuleDocumentationComment(string docComment) var tree = SyntaxTree.Create(CompilationUnit( WithDocumentation(ModuleDeclaration( "documentedModule", - VariableDeclaration(Api.Semantics.Visibility.Public, "Foo", null, LiteralExpression(0))), + VarDeclaration(Api.Semantics.Visibility.Public, "Foo", null, LiteralExpression(0))), docComment), FunctionDeclaration( "foo", ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("documentedModule"), "Foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("documentedModule"), "Foo"))))))); var moduleRef = tree.GetNode(0).Accessed; @@ -283,7 +283,7 @@ public void StaticTypeDocumentationFromMetadata() "main", ParameterList(), null, - BlockFunctionBody(DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("TestClass"), "foo"))))))); + BlockFunctionBody(DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("TestClass"), "foo"))))))); var docs = " Documentation for TestClass "; diff --git a/src/Draco.Compiler.Tests/Semantics/LocalFunctionsTests.cs b/src/Draco.Compiler.Tests/Semantics/LocalFunctionsTests.cs index de8bf9388..07fe37876 100644 --- a/src/Draco.Compiler.Tests/Semantics/LocalFunctionsTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/LocalFunctionsTests.cs @@ -66,7 +66,7 @@ public void UndefinedReference() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("y", null, NameExpression("x")))))))))); + DeclarationStatement(VarDeclaration("y", null, NameExpression("x")))))))))); // Act var compilation = CreateCompilation(tree); @@ -97,7 +97,7 @@ public void LocalVariableIncompatibleType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", NameType("int32"), StringExpression("Hello")))))))))); @@ -160,8 +160,8 @@ public void ReferenceUninitializedVariable() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), - DeclarationStatement(VariableDeclaration("y", null, NameExpression("x")))))))))); + DeclarationStatement(VarDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("y", null, NameExpression("x")))))))))); // Act var compilation = CreateCompilation(tree); diff --git a/src/Draco.Compiler.Tests/Semantics/SemanticModelTests.cs b/src/Draco.Compiler.Tests/Semantics/SemanticModelTests.cs index 593b78582..59e11f8f3 100644 --- a/src/Draco.Compiler.Tests/Semantics/SemanticModelTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/SemanticModelTests.cs @@ -25,7 +25,7 @@ public void RequestingDiagnosticsAfterSymbolDoesNotDiscardDiagnostic() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")))))); + DeclarationStatement(VarDeclaration("x")))))); var xDecl = tree.GetNode(0); @@ -55,7 +55,7 @@ public void RequestingSymbolAfterDiagnosticsDoesNotDiscardDiagnostic() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")))))); + DeclarationStatement(VarDeclaration("x")))))); var xDecl = tree.GetNode(0); @@ -85,7 +85,7 @@ public void RequestingSymbolMultipleTimesDoesNotMultiplyDiagnostic() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")))))); + DeclarationStatement(VarDeclaration("x")))))); var xDecl = tree.GetNode(0); @@ -117,8 +117,8 @@ public void RequestingFunctionBodyDoesNotDiscardFlowAnalysisDiagnostic() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))), - DeclarationStatement(VariableDeclaration("y", value: NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("x", NameType("int32"))), + DeclarationStatement(VarDeclaration("y", value: NameExpression("x"))))))); var mainDecl = tree.GetNode(0); @@ -312,7 +312,7 @@ public void GetReferencedSymbolFromTypeMemberAccess() null, BlockFunctionBody( DeclarationStatement(ImportDeclaration("System", "Text")), - DeclarationStatement(VariableDeclaration("builder", null, CallExpression(NameExpression("StringBuilder")))), + DeclarationStatement(VarDeclaration("builder", null, CallExpression(NameExpression("StringBuilder")))), ExpressionStatement(CallExpression(MemberExpression(NameExpression("builder"), "AppendLine"))))))); var memberExprSyntax = tree.GetNode(0); @@ -350,7 +350,7 @@ public void GetReferencedSymbolFromTypeMemberAccessWithNonExistingMember() null, BlockFunctionBody( DeclarationStatement(ImportDeclaration("System", "Text")), - DeclarationStatement(VariableDeclaration("builder", null, CallExpression(NameExpression("StringBuilder")))), + DeclarationStatement(VarDeclaration("builder", null, CallExpression(NameExpression("StringBuilder")))), ExpressionStatement(CallExpression(MemberExpression(NameExpression("builder"), "Ap"))))))); var memberExprSyntax = tree.GetNode(0); diff --git a/src/Draco.Compiler.Tests/Semantics/SingleAssignmentTests.cs b/src/Draco.Compiler.Tests/Semantics/SingleAssignmentTests.cs index ff8c226dc..eb443d43a 100644 --- a/src/Draco.Compiler.Tests/Semantics/SingleAssignmentTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/SingleAssignmentTests.cs @@ -19,7 +19,7 @@ public void AssignImmutableOnce() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration("x", NameType("int32"), LiteralExpression(0))))))); + DeclarationStatement(ValDeclaration("x", NameType("int32"), LiteralExpression(0))))))); // Act var compilation = CreateCompilation(tree); @@ -43,7 +43,7 @@ public void AssignImmutableTwice() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration("x", NameType("int32"), LiteralExpression(0))), + DeclarationStatement(ValDeclaration("x", NameType("int32"), LiteralExpression(0))), ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(1))))))); // Act @@ -69,7 +69,7 @@ public void AssignImmutableInMutuallyExclusiveBranches() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration("x", NameType("int32"))), + DeclarationStatement(ValDeclaration("x", NameType("int32"))), ExpressionStatement(IfExpression( NameExpression("b"), StatementExpression(ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(1)))), @@ -98,7 +98,7 @@ public void ConditionallyAndThenUnconditionallyAssignImmutable() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration("x", NameType("int32"))), + DeclarationStatement(ValDeclaration("x", NameType("int32"))), ExpressionStatement(IfExpression( NameExpression("b"), StatementExpression(ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(1)))))), @@ -127,7 +127,7 @@ public void AssignImmutableInLoop() ParameterList(Parameter("b", NameType("bool"))), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration("x", NameType("int32"))), + DeclarationStatement(ValDeclaration("x", NameType("int32"))), ExpressionStatement(WhileExpression( NameExpression("b"), StatementExpression(ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(1)))))))))); @@ -146,12 +146,12 @@ public void AssignImmutableInLoop() public void GlobalImmutableReassigned() { // Arrange - // val x: int32 = 0; + // field val x: int32 = 0; // func foo() { // x = 1; // } var tree = SyntaxTree.Create(CompilationUnit( - ImmutableVariableDeclaration("x", NameType("int32"), LiteralExpression(0)), + FieldValDeclaration("x", NameType("int32"), LiteralExpression(0)), FunctionDeclaration( "foo", ParameterList(), diff --git a/src/Draco.Compiler.Tests/Semantics/SymbolResolutionTests.cs b/src/Draco.Compiler.Tests/Semantics/SymbolResolutionTests.cs index 4efb33d18..ccfd9c7ef 100644 --- a/src/Draco.Compiler.Tests/Semantics/SymbolResolutionTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/SymbolResolutionTests.cs @@ -43,14 +43,14 @@ public void BasicScopeTree() Parameter("n", NameType("int32"))), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x1")), + DeclarationStatement(VarDeclaration("x1")), ExpressionStatement(BlockExpression( - DeclarationStatement(VariableDeclaration("x2")), - ExpressionStatement(BlockExpression(DeclarationStatement(VariableDeclaration("x3")))))), + DeclarationStatement(VarDeclaration("x2")), + ExpressionStatement(BlockExpression(DeclarationStatement(VarDeclaration("x3")))))), ExpressionStatement(BlockExpression( - DeclarationStatement(VariableDeclaration("x4")), - ExpressionStatement(BlockExpression(DeclarationStatement(VariableDeclaration("x5")))), - ExpressionStatement(BlockExpression(DeclarationStatement(VariableDeclaration("x6")))))))))); + DeclarationStatement(VarDeclaration("x4")), + ExpressionStatement(BlockExpression(DeclarationStatement(VarDeclaration("x5")))), + ExpressionStatement(BlockExpression(DeclarationStatement(VarDeclaration("x6")))))))))); var foo = tree.GetNode(); var n = tree.GetNode(); @@ -107,10 +107,10 @@ public void LocalShadowing() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, LiteralExpression(0))), - DeclarationStatement(VariableDeclaration("x", null, BinaryExpression(NameExpression("x"), Plus, LiteralExpression(1)))), - DeclarationStatement(VariableDeclaration("x", null, BinaryExpression(NameExpression("x"), Plus, LiteralExpression(1)))), - DeclarationStatement(VariableDeclaration("x", null, BinaryExpression(NameExpression("x"), Plus, LiteralExpression(1)))))))); + DeclarationStatement(VarDeclaration("x", null, LiteralExpression(0))), + DeclarationStatement(VarDeclaration("x", null, BinaryExpression(NameExpression("x"), Plus, LiteralExpression(1)))), + DeclarationStatement(VarDeclaration("x", null, BinaryExpression(NameExpression("x"), Plus, LiteralExpression(1)))), + DeclarationStatement(VarDeclaration("x", null, BinaryExpression(NameExpression("x"), Plus, LiteralExpression(1)))))))); var x0 = tree.GetNode(0); var x1 = tree.GetNode(1); @@ -213,9 +213,9 @@ public void OrderDependentReferencing() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")), - DeclarationStatement(VariableDeclaration("y", value: BinaryExpression(NameExpression("x"), Plus, NameExpression("z")))), - DeclarationStatement(VariableDeclaration("z")))))); + DeclarationStatement(VarDeclaration("x")), + DeclarationStatement(VarDeclaration("y", value: BinaryExpression(NameExpression("x"), Plus, NameExpression("z")))), + DeclarationStatement(VarDeclaration("z")))))); var xDecl = tree.GetNode(0); var yDecl = tree.GetNode(1); @@ -264,15 +264,15 @@ public void OrderDependentReferencingWithNesting() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")), + DeclarationStatement(VarDeclaration("x")), ExpressionStatement(BlockExpression( - DeclarationStatement(VariableDeclaration("y")), - DeclarationStatement(VariableDeclaration("z", value: BinaryExpression(NameExpression("x"), Plus, NameExpression("y")))), - DeclarationStatement(VariableDeclaration("x")), + DeclarationStatement(VarDeclaration("y")), + DeclarationStatement(VarDeclaration("z", value: BinaryExpression(NameExpression("x"), Plus, NameExpression("y")))), + DeclarationStatement(VarDeclaration("x")), ExpressionStatement(BlockExpression( - DeclarationStatement(VariableDeclaration("k", value: BinaryExpression(NameExpression("x"), Plus, NameExpression("w")))))), - DeclarationStatement(VariableDeclaration("w")))), - DeclarationStatement(VariableDeclaration("k", value: NameExpression("w"))))))); + DeclarationStatement(VarDeclaration("k", value: BinaryExpression(NameExpression("x"), Plus, NameExpression("w")))))), + DeclarationStatement(VarDeclaration("w")))), + DeclarationStatement(VarDeclaration("k", value: NameExpression("w"))))))); var x1Decl = tree.GetNode(0); var y1Decl = tree.GetNode(1); @@ -365,7 +365,7 @@ public void RedefinedParameterReference() Parameter("x", NameType("int32"))), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("y", null, NameExpression("x"))))))); + DeclarationStatement(VarDeclaration("y", null, NameExpression("x"))))))); var x1Decl = tree.GetNode(0); var x2Decl = tree.GetNode(1); @@ -415,7 +415,7 @@ public void FuncOverloadsGlobalVar() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("b", NameType("int32")), + VarDeclaration("b", NameType("int32")), FunctionDeclaration( "b", ParameterList(Parameter("b", NameType("int32"))), @@ -455,8 +455,8 @@ public void GlobalVariableDefinedLater() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("y", value: NameExpression("x"))))), - VariableDeclaration("x"))); + DeclarationStatement(VarDeclaration("y", value: NameExpression("x"))))), + VarDeclaration("x"))); var localVarDecl = tree.GetNode(0); var globalVarDecl = tree.GetNode(1); @@ -560,8 +560,8 @@ public void GlobalCanNotReferenceGlobal() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("x", null, LiteralExpression(0)), - VariableDeclaration("y", null, NameExpression("x")))); + VarDeclaration("x", null, LiteralExpression(0)), + VarDeclaration("y", null, NameExpression("x")))); var xDecl = tree.GetNode(0); var xRef = tree.GetNode(0); @@ -588,7 +588,7 @@ public void GlobalCanReferenceFunction() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("x", null, CallExpression(NameExpression("foo"))), + VarDeclaration("x", null, CallExpression(NameExpression("foo"))), FunctionDeclaration( "foo", ParameterList(), @@ -828,7 +828,7 @@ public void ModuleIsIllegalInExpressionContext() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("a", null, NameExpression("System"))))))); + DeclarationStatement(VarDeclaration("a", null, NameExpression("System"))))))); var varDecl = tree.GetNode(0); var moduleRef = tree.GetNode(0); @@ -940,7 +940,7 @@ public void ImportIsNotAtTheTopOfFunctionBody() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, LiteralExpression(0))), + DeclarationStatement(VarDeclaration("x", null, LiteralExpression(0))), DeclarationStatement(ImportDeclaration("System")))))); var importPath = tree.GetNode(0); @@ -1002,7 +1002,7 @@ public void ModuleAsVariableType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("System"), LiteralExpression(0))))))); + DeclarationStatement(VarDeclaration("x", NameType("System"), LiteralExpression(0))))))); var varTypeSyntax = tree.GetNode(0); @@ -1213,13 +1213,13 @@ public void NotVisibleGlobalVariableFullyQualified() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", type: null, value: MemberExpression(NameExpression("BarModule"), "bar")))))), + DeclarationStatement(VarDeclaration("x", type: null, value: MemberExpression(NameExpression("BarModule"), "bar")))))), ToPath("Tests", "main.draco")); // var bar = 0; var foo = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("bar", type: null, value: LiteralExpression(0))), + VarDeclaration("bar", type: null, value: LiteralExpression(0))), ToPath("Tests", "BarModule", "bar.draco")); // Act @@ -1598,14 +1598,14 @@ public void InCodeModuleImports() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("sb", null, CallExpression(NameExpression("StringBuilder")))), + DeclarationStatement(VarDeclaration("sb", null, CallExpression(NameExpression("StringBuilder")))), ExpressionStatement(CallExpression(NameExpression("WriteLine"), CallExpression(MemberExpression(NameExpression("sb"), "ToString"))))))), FunctionDeclaration( "baz", ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("sb", null, CallExpression(NameExpression("StringBuilder")))), + DeclarationStatement(VarDeclaration("sb", null, CallExpression(NameExpression("StringBuilder")))), ExpressionStatement(CallExpression(NameExpression("WriteLine"))))))); // Act @@ -1700,7 +1700,7 @@ public void ReadingAndSettingStaticFieldFullyQualified() null, BlockFunctionBody( ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("FooModule"), "foo"), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class FooModule{ @@ -1747,7 +1747,7 @@ public void ReadingAndSettingStaticFieldImported() null, BlockFunctionBody( ExpressionStatement(BinaryExpression(NameExpression("foo"), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, NameExpression("foo"))))))); + DeclarationStatement(VarDeclaration("x", null, NameExpression("foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class FooModule{ @@ -1792,9 +1792,9 @@ public void ReadingAndSettingNonStaticField() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("fooType"), "foo"), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -1921,7 +1921,7 @@ public void SettingReadonlyNonStaticField() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("fooType"), "foo"), Assign, LiteralExpression(5))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -1964,8 +1964,8 @@ public void ReadingNonExistingNonStaticField() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType { } @@ -2008,7 +2008,7 @@ public void SettingNonExistingNonStaticField() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("fooType"), "foo"), Assign, LiteralExpression(5))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -2087,7 +2087,7 @@ public void ReadingNonExistingStaticField() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class FooModule { } @@ -2131,7 +2131,7 @@ public void ReadingAndSettingStaticPropertyFullyQualified() null, BlockFunctionBody( ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("FooModule"), "foo"), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class FooModule{ @@ -2178,7 +2178,7 @@ public void ReadingAndSettingStaticPropertyImported() null, BlockFunctionBody( ExpressionStatement(BinaryExpression(NameExpression("foo"), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, NameExpression("foo"))))))); + DeclarationStatement(VarDeclaration("x", null, NameExpression("foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class FooModule{ @@ -2223,9 +2223,9 @@ public void ReadingAndSettingNonStaticProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("fooType"), "foo"), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -2311,7 +2311,7 @@ public void GettingSetOnlyStaticProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class FooModule{ @@ -2353,7 +2353,7 @@ public void SettingGetOnlyNonStaticProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("fooType"), "foo"), Assign, LiteralExpression(5))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -2396,8 +2396,8 @@ public void GettingSetOnlyNonStaticProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("fooType"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -2442,7 +2442,7 @@ public void CompoundAssignmentNonStaticProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(MemberExpression(NameExpression("fooType"), "foo"), PlusAssign, LiteralExpression(2))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -2525,9 +2525,9 @@ public void ReadingAndSettingIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(IndexExpression(NameExpression("fooType"), LiteralExpression(0)), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, IndexExpression(NameExpression("fooType"), LiteralExpression(0)))))))); + DeclarationStatement(VarDeclaration("x", null, IndexExpression(NameExpression("fooType"), LiteralExpression(0)))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -2575,7 +2575,7 @@ public void SettingGetOnlyIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(IndexExpression(NameExpression("fooType"), LiteralExpression(0)), Assign, LiteralExpression(5))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -2616,8 +2616,8 @@ public void GettingSetOnlyIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), - DeclarationStatement(VariableDeclaration("x", null, IndexExpression(NameExpression("fooType"), LiteralExpression(0)))))))); + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("x", null, IndexExpression(NameExpression("fooType"), LiteralExpression(0)))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -2663,9 +2663,9 @@ public void ReadingAndSettingMemberAccessIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(IndexExpression(MemberExpression(NameExpression("fooType"), "foo"), LiteralExpression(0)), Assign, LiteralExpression(5))), - DeclarationStatement(VariableDeclaration("x", null, IndexExpression(MemberExpression(NameExpression("fooType"), "foo"), LiteralExpression(0)))))))); + DeclarationStatement(VarDeclaration("x", null, IndexExpression(MemberExpression(NameExpression("fooType"), "foo"), LiteralExpression(0)))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -2718,7 +2718,7 @@ public void SettingGetOnlyMemberAccessIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(IndexExpression(MemberExpression(NameExpression("fooType"), "foo"), LiteralExpression(0)), Assign, LiteralExpression(5))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -2762,8 +2762,8 @@ public void GettingSetOnlyMemberAccessIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), - DeclarationStatement(VariableDeclaration("x", null, IndexExpression(MemberExpression(NameExpression("fooType"), "foo"), LiteralExpression(0)))))))); + DeclarationStatement(VarDeclaration("fooType", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("x", null, IndexExpression(MemberExpression(NameExpression("fooType"), "foo"), LiteralExpression(0)))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType{ @@ -2813,8 +2813,8 @@ public void GettingNonExistingIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("FooType")))), - DeclarationStatement(VariableDeclaration("x", null, IndexExpression(NameExpression("foo"), LiteralExpression(0)))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("x", null, IndexExpression(NameExpression("foo"), LiteralExpression(0)))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType { } @@ -2857,7 +2857,7 @@ public void SettingNonExistingIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(IndexExpression(NameExpression("foo"), LiteralExpression(0)), Assign, LiteralExpression(5))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -2896,7 +2896,7 @@ public void CompoundAssignmentIndexer() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("FooType")))), ExpressionStatement(BinaryExpression(IndexExpression(NameExpression("foo"), LiteralExpression(0)), PlusAssign, LiteralExpression(2))))))); var fooRef = CompileCSharpToMetadataReference(""" @@ -3061,7 +3061,7 @@ public void NestedTypeWithStaticParentStaticMemberAccess() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(MemberExpression(NameExpression("ParentType"), "FooType"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(MemberExpression(NameExpression("ParentType"), "FooType"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class ParentType @@ -3106,7 +3106,7 @@ public void NestedTypeWithNonStaticParentStaticMemberAccess() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(MemberExpression(NameExpression("ParentType"), "FooType"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(MemberExpression(NameExpression("ParentType"), "FooType"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class ParentType @@ -3152,8 +3152,8 @@ public void NestedTypeWithStaticParentNonStaticMemberAccess() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(MemberExpression(NameExpression("ParentType"), "FooType")))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("foo"), "member"))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(MemberExpression(NameExpression("ParentType"), "FooType")))), + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("foo"), "member"))))))); var fooRef = CompileCSharpToMetadataReference(""" public static class ParentType @@ -3202,8 +3202,8 @@ public void NestedTypeWithNonStaticParentNonStaticMemberAccess() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(MemberExpression(NameExpression("ParentType"), "FooType")))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("foo"), "member"))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(MemberExpression(NameExpression("ParentType"), "FooType")))), + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("foo"), "member"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class ParentType @@ -3288,7 +3288,7 @@ public void InheritanceFromObject() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType { } @@ -3329,7 +3329,7 @@ public void InheritanceFromTypeDefinition() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var fooRef = CompileCSharpToMetadataReference(""" public class ParentType { } @@ -3372,7 +3372,7 @@ public void InheritanceFromNestedTypeReference() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var baseStream = CompileCSharpToStream(""" public class ParentType @@ -3425,7 +3425,7 @@ public void InheritanceFromTypeSpecification() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var fooRef = CompileCSharpToMetadataReference(""" public class ParentType { } @@ -3473,7 +3473,7 @@ public void InheritingInterfacesFromTypeDefinition() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var fooRef = CompileCSharpToMetadataReference(""" public interface ParentInterface { } @@ -3517,7 +3517,7 @@ public void InheritingInterfacesFromTypeReference() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var fooRef = CompileCSharpToMetadataReference(""" public class FooType : System.ICloneable @@ -3562,7 +3562,7 @@ public void InheretingInterfaceFromTypeSpecification() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression((NameExpression("FooType"))))))))); var fooRef = CompileCSharpToMetadataReference(""" public interface ParentInterface { } @@ -3612,8 +3612,8 @@ public void AccessingMemberOfBaseType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("FooType")))), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("foo"), "Field"))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("FooType")))), + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("foo"), "Field"))))))); var fooRef = CompileCSharpToMetadataReference(""" public class ParentType @@ -3659,7 +3659,7 @@ public void ImplicitOverrideFunction() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); var fooRef = CompileCSharpToMetadataReference(""" public class Base @@ -3708,7 +3708,7 @@ public void ExplicitOverrideFunctionInSameAssembly() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); var fooRef = CompileCSharpToMetadataReference(""" public class Base @@ -3757,7 +3757,7 @@ public void ExplicitOverrideFunctionInDifferentAssembly() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); var baseStream = CompileCSharpToStream(""" public class Base @@ -3812,7 +3812,7 @@ public void ImplicitOverrideProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); var fooRef = CompileCSharpToMetadataReference(""" public class Base @@ -3861,7 +3861,7 @@ public void ExplicitOverrideProperty() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); + DeclarationStatement(VarDeclaration("foo", null, CallExpression(NameExpression("Derived")))))))); var fooRef = CompileCSharpToMetadataReference(""" public class Base @@ -4064,7 +4064,7 @@ public void ReturningInGlobalBlockIsIllegal() // val a = { return 4; }; var main = SyntaxTree.Create(CompilationUnit( - ImmutableVariableDeclaration( + ValDeclaration( "a", null, BlockExpression(ExpressionStatement(ReturnExpression(LiteralExpression(4))))))); diff --git a/src/Draco.Compiler.Tests/Semantics/TypeCheckingTests.cs b/src/Draco.Compiler.Tests/Semantics/TypeCheckingTests.cs index d3a45490d..0bc1ddf71 100644 --- a/src/Draco.Compiler.Tests/Semantics/TypeCheckingTests.cs +++ b/src/Draco.Compiler.Tests/Semantics/TypeCheckingTests.cs @@ -21,7 +21,7 @@ public void LocalVariableExplicitlyTyped() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"), LiteralExpression(0))))))); + DeclarationStatement(VarDeclaration("x", NameType("int32"), LiteralExpression(0))))))); var xDecl = tree.GetNode(0); @@ -49,7 +49,7 @@ public void LocalVariableTypeInferredFromValue() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", value: LiteralExpression(0))))))); + DeclarationStatement(VarDeclaration("x", value: LiteralExpression(0))))))); var xDecl = tree.GetNode(0); @@ -77,7 +77,7 @@ public void LocalVariableExplicitlyTypedWithoutValue() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("int32"))))))); + DeclarationStatement(VarDeclaration("x", NameType("int32"))))))); var xDecl = tree.GetNode(0); @@ -106,7 +106,7 @@ public void LocalVariableTypeInferredFromLaterAssignment() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")), + DeclarationStatement(VarDeclaration("x")), ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(0))))))); var xDecl = tree.GetNode(0); @@ -135,7 +135,7 @@ public void LocalVariableTypeCanNotBeInferred() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")))))); + DeclarationStatement(VarDeclaration("x")))))); var xDecl = tree.GetNode(0); @@ -165,7 +165,7 @@ public void LocalVariableIncompatibleType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", NameType("int32"), StringExpression("Hello"))))))); @@ -200,7 +200,7 @@ public void LocalVariableIncompatibleTypeInferredFromUsage() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x")), + DeclarationStatement(VarDeclaration("x")), ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, LiteralExpression(0))), ExpressionStatement(BinaryExpression(NameExpression("x"), Assign, StringExpression("Hello"))))))); @@ -226,7 +226,7 @@ public void GlobalVariableExplicitlyTyped() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("x", NameType("int32"), LiteralExpression(0)))); + VarDeclaration("x", NameType("int32"), LiteralExpression(0)))); var xDecl = tree.GetNode(0); @@ -248,7 +248,7 @@ public void GlobalVariableTypeInferredFromValue() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("x", value: LiteralExpression(0)))); + VarDeclaration("x", value: LiteralExpression(0)))); var xDecl = tree.GetNode(0); @@ -270,7 +270,7 @@ public void GlobalVariableExplicitlyTypedWithoutValue() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("x", NameType("int32")))); + VarDeclaration("x", NameType("int32")))); var xDecl = tree.GetNode(0); @@ -292,7 +292,7 @@ public void GlobalVariableTypeCanNotBeInferred() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration("x"))); + VarDeclaration("x"))); var xDecl = tree.GetNode(0); @@ -316,7 +316,7 @@ public void GlobalVariableIncompatibleType() // Arrange var tree = SyntaxTree.Create(CompilationUnit( - VariableDeclaration( + VarDeclaration( "x", NameType("int32"), StringExpression("Hello")))); @@ -494,7 +494,7 @@ public void IfElseTypeMismatch() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", value: IfExpression( condition: LiteralExpression(true), @@ -568,8 +568,8 @@ public void NeverTypeCompatibility() null, BlockFunctionBody( DeclarationStatement(LabelDeclaration("start")), - DeclarationStatement(VariableDeclaration("x", value: IfExpression(LiteralExpression(true), LiteralExpression(0), ReturnExpression()))), - DeclarationStatement(VariableDeclaration("y", value: IfExpression(LiteralExpression(true), LiteralExpression(0), GotoExpression("start")))))))); + DeclarationStatement(VarDeclaration("x", value: IfExpression(LiteralExpression(true), LiteralExpression(0), ReturnExpression()))), + DeclarationStatement(VarDeclaration("y", value: IfExpression(LiteralExpression(true), LiteralExpression(0), GotoExpression("start")))))))); var xDecl = tree.GetNode(0); var yDecl = tree.GetNode(1); @@ -602,7 +602,7 @@ public void NoOverloadForOperator() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", value: BinaryExpression(LiteralExpression(1), Plus, StringExpression("Hello")))))))); @@ -1056,7 +1056,7 @@ public void AccessingField() null, BlockFunctionBody( DeclarationStatement(ImportDeclaration("System")), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("String"), "Empty"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("String"), "Empty"))))))); var xDecl = tree.GetNode(0); var consoleRef = tree.GetNode(0).Accessed; @@ -1089,7 +1089,7 @@ public void AccessingProperty() null, BlockFunctionBody( DeclarationStatement(ImportDeclaration("System")), - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("Console"), "WindowWidth"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("Console"), "WindowWidth"))))))); var xDecl = tree.GetNode(0); var consoleRef = tree.GetNode(0).Accessed; @@ -1123,8 +1123,8 @@ public void AccessingIndexer() null, BlockFunctionBody( DeclarationStatement(ImportDeclaration("System", "Collections", "Generic")), - DeclarationStatement(VariableDeclaration("list", null, CallExpression(GenericExpression(NameExpression("List"), NameType("int32"))))), - DeclarationStatement(VariableDeclaration("x", null, IndexExpression(NameExpression("list"), LiteralExpression(0)))))))); + DeclarationStatement(VarDeclaration("list", null, CallExpression(GenericExpression(NameExpression("List"), NameType("int32"))))), + DeclarationStatement(VarDeclaration("x", null, IndexExpression(NameExpression("list"), LiteralExpression(0)))))))); var xDecl = tree.GetNode(1); var listRef = tree.GetNode(0).Indexed; @@ -1157,7 +1157,7 @@ public void IllegalCallToNonFunctionType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("a", null, LiteralExpression(0))), + DeclarationStatement(VarDeclaration("a", null, LiteralExpression(0))), ExpressionStatement(CallExpression(NameExpression("a"))))))); // Act @@ -1195,19 +1195,19 @@ public void ExplicitGenericFunction() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "a", null, CallExpression( GenericExpression(NameExpression("identity"), NameType("int32")), LiteralExpression(0)))), - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "b", null, CallExpression( GenericExpression(NameExpression("identity"), NameType("string")), StringExpression("foo")))), - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "c", null, CallExpression( @@ -1273,7 +1273,7 @@ public void ExplicitGenericFunctionWithWrongNumberOfArgs() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "a", null, CallExpression( @@ -1306,7 +1306,7 @@ public void InstantiateIllegalConstruct() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("a", null, LiteralExpression(0))), + DeclarationStatement(VarDeclaration("a", null, LiteralExpression(0))), ExpressionStatement(CallExpression(GenericExpression(NameExpression("a"), NameType("int32")))))))); // Act @@ -1329,7 +1329,7 @@ public void ExplicitGenericTypeWithWrongNumberOfArgs() // Arrange var tree = SyntaxTree.Create(CompilationUnit( ImportDeclaration("System", "Collections", "Generic"), - VariableDeclaration( + VarDeclaration( "l", GenericType(NameType("List"), NameType("int32"), NameType("int32"))))); @@ -1366,7 +1366,7 @@ public void InferGenericFunctionParameterTypeFromUse() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", null, CallExpression(NameExpression("identity"), LiteralExpression(0)))))))); @@ -1409,7 +1409,7 @@ public void InferGenericCollectionTypeFromUse() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("s", null, CallExpression(NameExpression("Stack")))), + DeclarationStatement(VarDeclaration("s", null, CallExpression(NameExpression("Stack")))), ExpressionStatement(CallExpression( MemberExpression(NameExpression("s"), "Push"), LiteralExpression(0))))))); @@ -1452,7 +1452,7 @@ public void CanNotInferGenericCollectionTypeFromUse() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("s", null, CallExpression(NameExpression("Stack")))))))); + DeclarationStatement(VarDeclaration("s", null, CallExpression(NameExpression("Stack")))))))); var callSyntax = tree.GetNode(); var varSyntax = tree.GetNode(); @@ -1809,7 +1809,7 @@ public void AssignDerivedTypeToBaseType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("Object"), CallExpression(NameExpression("Random")))))))); + DeclarationStatement(VarDeclaration("x", NameType("Object"), CallExpression(NameExpression("Random")))))))); var xDecl = main.GetNode(0); @@ -1842,7 +1842,7 @@ public void AssignBaseTypeToDerivedType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", NameType("String"), CallExpression(NameExpression("Object")))))))); + DeclarationStatement(VarDeclaration("x", NameType("String"), CallExpression(NameExpression("Object")))))))); var xDecl = main.GetNode(0); @@ -2055,7 +2055,7 @@ public void IfStatementCommonTypeResult() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", null, IfExpression( @@ -2095,11 +2095,11 @@ public void InferSwappedArrayElement() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "a", null, CallExpression(NameExpression("Array"), LiteralExpression(5)))), - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "b", null, CallExpression(NameExpression("Array"), LiteralExpression(5)))), @@ -2107,7 +2107,7 @@ public void InferSwappedArrayElement() IndexExpression(NameExpression("a"), LiteralExpression(0)), Assign, LiteralExpression(1))), - DeclarationStatement(ImmutableVariableDeclaration("tmp", null, NameExpression("a"))), + DeclarationStatement(ValDeclaration("tmp", null, NameExpression("a"))), ExpressionStatement(BinaryExpression(NameExpression("a"), Assign, NameExpression("b"))), ExpressionStatement(BinaryExpression(NameExpression("b"), Assign, NameExpression("tmp"))))))); @@ -2150,7 +2150,7 @@ public void IndexerWitingOverloadedCall() ParameterList(), NameType("int32"), BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration( + DeclarationStatement(ValDeclaration( "line", null, CallExpression(MemberExpression(NameExpression("Console"), "ReadLine")))), @@ -2184,7 +2184,7 @@ public void GettingTypeFromReference() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); + DeclarationStatement(VarDeclaration("x", null, MemberExpression(NameExpression("FooModule"), "foo"))))))); var fooRef = CompileCSharpToMetadataReference(""" using System; @@ -2222,7 +2222,7 @@ public void StringAssignableToObject() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", NameType("object"), StringExpression("Hello"))))))); @@ -2251,7 +2251,7 @@ public void InferredArrayElementTypeFromUsage() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration( + DeclarationStatement(ValDeclaration( "a", null, CallExpression( @@ -2290,11 +2290,11 @@ public void ArrayIndexResultHasTheRightType() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration( + DeclarationStatement(ValDeclaration( "a", null, CallExpression(GenericExpression(NameExpression("Array"), NameType("int32")), LiteralExpression(3)))), - DeclarationStatement(ImmutableVariableDeclaration( + DeclarationStatement(ValDeclaration( "x", null, IndexExpression(NameExpression("a"), LiteralExpression(0)))))))); @@ -2326,7 +2326,7 @@ public void ArrayIteratorVariableCorrectlyInferredInForLoop() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(ImmutableVariableDeclaration( + DeclarationStatement(ValDeclaration( "a", null, CallExpression(GenericExpression(NameExpression("Array"), NameType("int32")), LiteralExpression(3)))), @@ -2484,7 +2484,7 @@ public void AssigningListToArrayTypeIsIllegal() ParameterList(), null, BlockFunctionBody( - DeclarationStatement(VariableDeclaration( + DeclarationStatement(VarDeclaration( "x", GenericType(NameType("Array"), NameType("int32")), CallExpression(NameExpression("List")))))))); diff --git a/src/Draco.Compiler/Api/Syntax/SyntaxFactory.cs b/src/Draco.Compiler/Api/Syntax/SyntaxFactory.cs index 0578349aa..593758a7b 100644 --- a/src/Draco.Compiler/Api/Syntax/SyntaxFactory.cs +++ b/src/Draco.Compiler/Api/Syntax/SyntaxFactory.cs @@ -67,11 +67,12 @@ public static SyntaxToken Missing(TokenKind kind) => public static SyntaxToken Identifier(string text) => Token(TokenKind.Identifier, text); public static SyntaxToken Integer(int value) => Token(TokenKind.LiteralInteger, value.ToString(), value); - public static TokenKind? Visibility(Visibility visibility) => visibility switch + public static TokenKind? Visibility(Visibility? visibility) => visibility switch { Semantics.Visibility.Private => null, Semantics.Visibility.Internal => TokenKind.KeywordInternal, Semantics.Visibility.Public => TokenKind.KeywordPublic, + null => null, _ => throw new ArgumentOutOfRangeException(nameof(visibility)), }; @@ -168,37 +169,68 @@ public static FunctionDeclarationSyntax FunctionDeclaration( returnType is null ? null : TypeSpecifier(returnType), body); - public static VariableDeclarationSyntax VariableDeclaration( + public static VariableDeclarationSyntax ValDeclaration( string name, TypeSyntax? type = null, - ExpressionSyntax? value = null) => VariableDeclaration(null, false, true, name, type, value); + ExpressionSyntax? value = null) => + VariableDeclaration([], null, false, false, name, type, value); - public static VariableDeclarationSyntax VariableDeclaration( - Visibility visibility, + public static VariableDeclarationSyntax VarDeclaration( string name, TypeSyntax? type = null, - ExpressionSyntax? value = null) => VariableDeclaration(Visibility(visibility), false, true, name, type, value); + ExpressionSyntax? value = null) => + VariableDeclaration([], null, false, true, name, type, value); - public static VariableDeclarationSyntax ImmutableVariableDeclaration( + public static VariableDeclarationSyntax FieldValDeclaration( string name, TypeSyntax? type = null, - ExpressionSyntax? value = null) => VariableDeclaration(null, false, false, name, type, value); + ExpressionSyntax? value = null) => + VariableDeclaration([], null, true, false, name, type, value); - public static VariableDeclarationSyntax ImmutableVariableDeclaration( - Visibility visibility, + public static VariableDeclarationSyntax FieldVarDeclaration( + string name, + TypeSyntax? type = null, + ExpressionSyntax? value = null) => + VariableDeclaration([], null, true, true, name, type, value); + + public static VariableDeclarationSyntax ValDeclaration( + Visibility? visibility, + string name, + TypeSyntax? type = null, + ExpressionSyntax? value = null) => + VariableDeclaration([], visibility, false, false, name, type, value); + + public static VariableDeclarationSyntax VarDeclaration( + Visibility? visibility, string name, TypeSyntax? type = null, - ExpressionSyntax? value = null) => VariableDeclaration(Visibility(visibility), false, false, name, type, value); + ExpressionSyntax? value = null) => + VariableDeclaration([], visibility, false, true, name, type, value); + + public static VariableDeclarationSyntax FieldValDeclaration( + Visibility? visibility, + string name, + TypeSyntax? type = null, + ExpressionSyntax? value = null) => + VariableDeclaration([], visibility, true, false, name, type, value); + + public static VariableDeclarationSyntax FieldVarDeclaration( + Visibility? visibility, + string name, + TypeSyntax? type = null, + ExpressionSyntax? value = null) => + VariableDeclaration([], visibility, true, true, name, type, value); public static VariableDeclarationSyntax VariableDeclaration( - TokenKind? visibility, + IEnumerable attributes, + Visibility? visibility, bool isField, bool isMutable, string name, TypeSyntax? type = null, ExpressionSyntax? value = null) => VariableDeclaration( - [], - visibility, + attributes, + Visibility(visibility), isField ? TokenKind.KeywordField : null, isMutable ? TokenKind.KeywordVar : TokenKind.KeywordVal, name, diff --git a/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs index e6484baa8..a78df4280 100644 --- a/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Synthetized/AutoProperty/AutoPropertyGetterSymbol.cs @@ -21,7 +21,7 @@ internal sealed class AutoPropertyGetterSymbol( public override Api.Semantics.Visibility Visibility => this.Property.Visibility; public override bool IsSpecialName => true; - public override ImmutableArray Parameters => ImmutableArray.Empty; + public override ImmutableArray Parameters => []; public override TypeSymbol ReturnType => this.Property.Type; public override BoundStatement Body => LazyInitializer.EnsureInitialized(ref this.body, this.BuildBody);