public void TestFunctionOverloadResolution1Arg(string type, string expectedMatchType) { var code = $@" struct MyStruct {{}}; int foo(int x) {{ return 1; }} int foo(float x) {{ return 2; }} int foo(double x) {{ return 3; }} int foo(int2 x) {{ return 4; }} int foo(float2 x) {{ return 5; }} int foo(double2 x) {{ return 6; }} int foo(float3x3 x) {{ return 7; }} void main() {{ foo(({type}) 0); }}"; var syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(code)); var syntaxTreeSource = syntaxTree.Root.ToFullString(); Assert.AreEqual(code, syntaxTreeSource, $"Source should have been {code} but is {syntaxTreeSource}."); var expression = (FunctionInvocationExpressionSyntax)syntaxTree.Root.ChildNodes .OfType <FunctionDefinitionSyntax>() .Where(x => x.Name.GetName() == "main") .Select(x => ((ExpressionStatementSyntax)x.Body.Statements[0]).Expression) .First(); var compilation = new HlslTools.Compilation.Compilation(syntaxTree); var semanticModel = compilation.GetSemanticModel(); var combinedDiagnostics = syntaxTree.GetDiagnostics().Concat(semanticModel.GetDiagnostics()).ToList(); foreach (var d in combinedDiagnostics) { Debug.WriteLine(d); } var invokedFunctionSymbol = (FunctionSymbol)semanticModel.GetSymbol(expression); var diagnostic = combinedDiagnostics.SingleOrDefault(x => x.Severity == Diagnostics.DiagnosticSeverity.Error); var result = diagnostic == null ? ExpressionTestUtility.GetExpressionTypeString(invokedFunctionSymbol.Parameters[0].ValueType) : ExpressionTestUtility.GetErrorString(diagnostic.DiagnosticId); Assert.AreEqual(expectedMatchType, result, $"Expression should have matched the function overload '{expectedMatchType}' but it actually matched '{result}'."); }
public void TestBinaryOperatorTypeConversions(string opText, string leftText, string rightText, string expectedResult) { var left = ExpressionTestUtility.GetValue(leftText); var right = ExpressionTestUtility.GetValue(rightText); var source = $"{left} {opText} {right}"; var syntaxTree = SyntaxFactory.ParseExpression(source); var syntaxTreeSource = syntaxTree.Root.ToString(); if (syntaxTreeSource != source) { Assert.Fail($"Source should have been {syntaxTreeSource} but is {source}"); } var expression = (BinaryExpressionSyntax)syntaxTree.Root; var compilation = new HlslTools.Compilation.Compilation(syntaxTree); var semanticModel = compilation.GetSemanticModel(); var leftType = ExpressionTestUtility.GetExpressionTypeString(semanticModel.GetExpressionType(expression.Left)); if (leftText != leftType) { Assert.Fail($"Left should be of type '{leftText}' but has type '{leftType}'"); } var rightType = ExpressionTestUtility.GetExpressionTypeString(semanticModel.GetExpressionType(expression.Right)); if (rightText != rightType) { Assert.Fail($"Right should be of type '{rightText}' but has type '{rightType}'"); } var diagnostic = syntaxTree.GetDiagnostics().Concat(semanticModel.GetDiagnostics()).SingleOrDefault(); var expressionType = semanticModel.GetExpressionType(expression); var result = diagnostic == null ? ExpressionTestUtility.GetExpressionTypeString(expressionType) : ExpressionTestUtility.GetErrorString(diagnostic.DiagnosticId); Assert.AreEqual(expectedResult, result, $"Expression {source} should have evaluated to '{expectedResult}' but was '{result}'"); }