Skip to content

Commit

Permalink
overload resolution using named arguments + test
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Jun 19, 2024
1 parent e26c914 commit 68a5b6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Peachpie.CodeAnalysis/Symbols/OverloadsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public OverloadsList(List<MethodSymbol> methods)
// only visible methods:
RemoveInaccessible(result, scope);

// remove functions with no specified named parameters:
for (int i = result.Count - 1; i >= 0; i--)
{
foreach (var arg in args)
{
if (arg.ParameterName != null && result[i].Parameters.All(p => p.Name != arg.ParameterName))
{
// the named parameter does not exist in the method,
// it's not the matching overload
result.RemoveAt(i);
break;
}
}
}

//
if (result.Count == 0)
{
return new InaccessibleMethodSymbol(_methods.AsImmutable());
Expand Down
24 changes: 23 additions & 1 deletion src/Tests/Peachpie.App.Tests/CompiledILTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,36 @@ static bool DecompileFunction(IScript script, string fnname, out IMethod method,
return true;
}

public static void OverloadFoo(int i) { }
public static void OverloadFoo(double d) { }
public static void OverloadFoo(string s) { }

[TestMethod]
public void TestOverloadByNamedParameter()
{
var script = Compile($@"<?php
function test($value) {{
{typeof(CompiledILTest).FullName.Replace('.', '\\')}::{nameof(OverloadFoo)}( s: $value );
}}
");
DecompileFunction(script, "test", out var method, out var ast);
var bodyCode = ast.LastChild.LastChild.Descendants.ElementAt(1).ToString().Trim();

Assert.IsTrue(
// method body
$"{nameof(CompiledILTest)}.{nameof(OverloadFoo)}" == bodyCode
);
}

[TestMethod]
public void TestSimpleFunction()
{
var script = Compile(@"<?php
function test() { return 1; }
");
var testAst = DecompileFunction(script, "test", out var method, out var ast);
DecompileFunction(script, "test", out var method, out var ast);

Assert.IsTrue(method.ReturnType.FullName == typeof(long).FullName);

Expand Down

0 comments on commit 68a5b6b

Please sign in to comment.