Skip to content

Commit

Permalink
support for calling precompiled methods which take a variable number …
Browse files Browse the repository at this point in the history
…of arguments

git-svn-id: https://svn.codehaus.org/boo/trunk@1728 2c1201b4-01cd-e047-a400-b836ae1fbc61
  • Loading branch information
bamboo committed Jul 9, 2005
1 parent 6e931ac commit 09f1a04
Show file tree
Hide file tree
Showing 25 changed files with 117 additions and 67 deletions.
7 changes: 7 additions & 0 deletions src/Boo.Lang.Compiler/Steps/ExpandVarArgsMethodInvocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public override void LeaveMethodInvocationExpression(Boo.Lang.Compiler.Ast.Metho
int lenMinusOne = parameters.Length-1;
IType varArgType = parameters[lenMinusOne].Type;

/*
if (node.Arguments.Count == parameters.Length
&& varArgType == node.Arguments[-1].ExpressionType)
{
return;
}*/

ExpressionCollection varArgs = node.Arguments.PopRange(lenMinusOne);
node.Arguments.Add(CodeBuilder.CreateArray(varArgType, varArgs));
}
Expand Down
34 changes: 12 additions & 22 deletions src/Boo.Lang.Compiler/Steps/ProcessMethodBodies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,22 +1591,17 @@ void BindMultiDimensionalArraySlicing(SlicingExpression node)
// leave it to LeaveBinaryExpression to resolve
return;
}

IArrayType arrayType = (IArrayType)GetExpressionType(node.Target);
ArrayLiteralExpression ale = new ArrayLiteralExpression();
for (int i = 0; i < node.Indices.Count; i++)
{
ale.Items.Add(node.Indices[i].Begin);
}

MethodInvocationExpression mie = CodeBuilder.CreateMethodInvocation(
node.Target,
TypeSystemServices.Map(
typeof(Array).GetMethod("GetValue", new Type[] { typeof(int[]) })),
ale);
typeof(Array).GetMethod("GetValue", new Type[] { typeof(int[]) })));
for (int i = 0; i < node.Indices.Count; i++)
{
mie.Arguments.Add(node.Indices[i].Begin);
}

IType elementType = arrayType.GetElementType();
BindExpressionType(ale, TypeSystemServices.Map(typeof(int[])));
IType elementType = node.Target.ExpressionType.GetElementType();
node.ParentNode.Replace(node, CodeBuilder.CreateCast(elementType, mie));
}

Expand Down Expand Up @@ -3258,7 +3253,7 @@ void ApplyBuiltinMethodTypeInference(MethodInvocationExpression expression, IMet
IType type = TypeSystemServices.GetReferencedType(expression.Arguments[0]);
if (null != type)
{
inferredType = TypeSystemServices.GetArrayType(type, ((ArrayLiteralExpression)expression.Arguments[1]).Items.Count);
inferredType = TypeSystemServices.GetArrayType(type, expression.Arguments.Count-1);
}
}
else if (Array_EnumerableConstructor == method)
Expand Down Expand Up @@ -3687,20 +3682,15 @@ void BindAssignmentToSliceArray(BinaryExpression node)
void BindAssignmentToSimpleSliceArray(BinaryExpression node)
{
SlicingExpression slice = (SlicingExpression)node.Left;
ArrayLiteralExpression ale = new ArrayLiteralExpression();
for (int i = 0; i < slice.Indices.Count; i++)
{
ale.Items.Add(slice.Indices[i].Begin);
}

MethodInvocationExpression mie = CodeBuilder.CreateMethodInvocation(
slice.Target,
TypeSystemServices.Map(typeof(Array).GetMethod("SetValue", new Type[] { typeof(object), typeof(int[]) })),
node.Right,
ale);

node.Right);
for (int i = 0; i < slice.Indices.Count; i++)
{
mie.Arguments.Add(slice.Indices[i].Begin);
}
BindExpressionType(mie, TypeSystemServices.VoidType);
BindExpressionType(ale, TypeSystemServices.Map(typeof(int[])));
node.ParentNode.Replace(node, mie);
}

Expand Down
11 changes: 7 additions & 4 deletions src/Boo.Lang.Compiler/TypeSystem/CallableResolutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ private void CalculateScores(IEntity[] candidates, NodeCollection args)
public int CalculateVarArgsScore(IParameter[] parameters, NodeCollection args)
{
int lenMinusOne = parameters.Length-1;
if (args.Count < lenMinusOne) return -1;

IType lastParameterType = parameters[lenMinusOne].Type;
if (!lastParameterType.IsArray) return -1;

Expand All @@ -191,9 +193,10 @@ public int CalculateVarArgsScore(IParameter[] parameters, NodeCollection args)
{
int argumentScore = CalculateArgumentScore(varArgType, args.GetNodeAt(i));
if (argumentScore < 0) return -1;
score += (argumentScore - 3);
score += argumentScore;
}
return score;
// varargs should not be preferred over non varargs methods
return score - ((args.Count + 1)*3);
}

private int CalculateExactArgsScore(IParameter[] parameters, NodeCollection args)
Expand All @@ -205,8 +208,8 @@ private int CalculateExactArgsScore(IParameter[] parameters, NodeCollection args
}

private int CalculateScore(IParameter[] parameters, NodeCollection args, int count)
{
int score = 0;
{
int score = 3;
for (int i=0; i<count; ++i)
{
IType parameterType = parameters[i].Type;
Expand Down
17 changes: 16 additions & 1 deletion src/Boo.Lang.Compiler/TypeSystem/ExternalMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class ExternalMethod : IMethod
IParameter[] _parameters;

ICallableType _type;

int _acceptVarArgs = -1;

internal ExternalMethod(TypeSystemServices manager, MethodBase mi)
{
Expand Down Expand Up @@ -122,7 +124,20 @@ public bool AcceptVarArgs
{
get
{
return false;
if (_acceptVarArgs == -1)
{
ParameterInfo[] parameters = _mi.GetParameters();
if (parameters.Length > 0
&& System.Attribute.IsDefined(parameters[parameters.Length-1], typeof(System.ParamArrayAttribute)))
{
_acceptVarArgs = 1;
}
else
{
_acceptVarArgs = 0;
}
}
return _acceptVarArgs == 1;
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/Boo.Lang/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,22 +327,21 @@ public static IEnumerable reversed(object enumerable)
return new List(iterator(enumerable)).Reversed;
}

public static ZipEnumerator zip(object first, object second)
public static ZipEnumerator zip(params object[] enumerables)
{
return new ZipEnumerator(GetEnumerator(first),
GetEnumerator(second));
IEnumerator[] enumerators = new IEnumerator[enumerables.Length];
for (int i=0; i<enumerables.Length; ++i)
{
enumerators[i] = GetEnumerator(enumerables[i]);
}
return new ZipEnumerator(enumerators);
}

public static ConcatEnumerator cat(params object[] args)
{
return new ConcatEnumerator(args);
}

public static ConcatEnumerator cat(object first, object second)
{
return new ConcatEnumerator(first, second);
}

private class MapEnumerable : IEnumerable
{
IEnumerable _enumerable;
Expand Down
2 changes: 1 addition & 1 deletion src/Boo.Lang/ICallable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ namespace Boo.Lang
{
public interface ICallable
{
object Call(params object[] args);
object Call(object[] args);
}
}
15 changes: 14 additions & 1 deletion tests/BooCompiler.Tests/SupportingClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public virtual void Method1()

public class DerivedClass : BaseClass
{
protected DerivedClass()
public DerivedClass()
{
}

Expand All @@ -347,6 +347,19 @@ public class ClassWithNewMethod : DerivedClass
}
}

public class VarArgs
{
public void Method()
{
Console.WriteLine("VarArgs.Method");
}

public void Method(params object[] args)
{
Console.WriteLine("VarArgs.Method({0})", Boo.Lang.Builtins.join(args, ", "));
}
}

public class Disposable : System.IDisposable
{
public Disposable()
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/integration/arrays-3.boo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
1, 2, 3
"""
print(string.Format("{0}, {1}, {2}", ("1", 2, 3)))
print(string.Format("{0}, {1}, {2}", "1", 2, 3))
2 changes: 1 addition & 1 deletion tests/testcases/integration/arrays-30.boo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ a1 = array(int, 1)
print(a1.GetType())

a2 as (int, 2)
a2 = System.Array.CreateInstance(int, (2, 2))
a2 = System.Array.CreateInstance(int, 2, 2)
print(a2.GetType())

6 changes: 3 additions & 3 deletions tests/testcases/integration/arrays-31.boo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import System

a as (int, 2) = Array.CreateInstance(int, (2, 2))
a as (int, 2) = Array.CreateInstance(int, 2, 2)

value = 0
for i in range(2):
for j in range(2):
a.SetValue(++value, (i, j))
assert value == a.GetValue((i, j))
a.SetValue(++value, i, j)
assert value == a.GetValue(i, j)
6 changes: 3 additions & 3 deletions tests/testcases/integration/arrays-32.boo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import System

def foo():
yield cast((int, 2), Array.CreateInstance(int, (2, 2)))
yield cast((int, 2), Array.CreateInstance(int, 2, 2))

// test type inference
for a in foo():
value = 0
for i in range(2):
for j in range(2):
a.SetValue(++value, (i, j))
assert value == a.GetValue((i, j))
a.SetValue(++value, i, j)
assert value == a.GetValue(i, j)
10 changes: 5 additions & 5 deletions tests/testcases/integration/arrays-33.boo
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
1 2 3 4
"""
t = matrix(int, (2, 2))
t = matrix(int, 2, 2)

t.SetValue(1, (0, 0))
t.SetValue(2, (0, 1))
t.SetValue(3, (1, 0))
t.SetValue(4, (1, 1))
t.SetValue(1, 0, 0)
t.SetValue(2, 0, 1)
t.SetValue(3, 1, 0)
t.SetValue(4, 1, 1)

print join(t)
12 changes: 6 additions & 6 deletions tests/testcases/integration/arrays-34.boo
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ System.String[,]
System.Object[,]
"""

t1 = matrix(int, (2,3))
t2 = matrix(string, (3,3))
t3 = matrix(object, (3,3))
t1 = matrix(int, 2, 3)
t2 = matrix(string, 3, 3)
t3 = matrix(object, 3, 3)

print (t1.GetType())
print (t2.GetType())
print (t3.GetType())
print(t1.GetType())
print(t2.GetType())
print(t3.GetType())
2 changes: 1 addition & 1 deletion tests/testcases/integration/arrays-35.boo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
8
"""

a = matrix(int, (5,2,4))
a = matrix(int, 5, 2, 4)

a[0,0,0]=1
a[0,0,1]=2
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/integration/arrays-36.boo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ t3 of type System.Int32[,,]
0
"""

t1 = matrix(int, (4,4,4))
t1 = matrix(int, 4, 4, 4)

t1[0,0,0]=1
t1[0,1,0]=2
Expand Down
4 changes: 2 additions & 2 deletions tests/testcases/integration/arrays-37.boo
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
0
"""

t1 = matrix(int, (2,4,2))
t2 = matrix(int, (1,3))
t1 = matrix(int, 2, 4, 2)
t2 = matrix(int, 1, 3)

t1[0,0,0]=5
t1[0,1,0]=5
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/integration/arrays-38.boo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

class Table:
_table = matrix(int, (2,2))
_table = matrix(int, 2, 2)

Item(x as int, y as int):
get:
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/integration/arrays-39.boo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import System.Reflection
[DefaultMember("Item")]
[EnumeratorItemType(int)]
class Table:
_table = matrix(int, (2,2))
_table = matrix(int, 2, 2)

Item(x as int, y as int):
get:
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/integration/compile-1.boo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module = Module()
module.Globals.Add(
MethodInvocationExpression(
ReferenceExpression("print"),
(StringLiteralExpression("it lives!"),)))
StringLiteralExpression("it lives!")))

cu = CompileUnit()
cu.Modules.Add(module)
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/integration/len-1.boo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
a1 = (1, 2, 3)
s1 = "Steppenwolf"
l1 = [5, 6]
a2 = matrix(int, (3, 2))
a2 = matrix(int, 3, 2)

assert 3 == len(a1)
assert 11 == len(s1)
Expand Down
11 changes: 11 additions & 0 deletions tests/testcases/integration/params-4.boo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
VarArgs.Method(1, 2)
VarArgs.Method(System.Object[])
VarArgs.Method
"""
import BooCompiler.Tests

d = VarArgs()
d.Method(1, 2)
d.Method((object(),))
d.Method()
12 changes: 12 additions & 0 deletions tests/testcases/integration/params-5.boo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
foo(*string)
foo(*object)
"""
def foo(*args):
print "foo(*object)"

def foo(*args as (string)):
print "foo(*string)"

foo("foo", "bar")
foo(("foo", "bar"))
2 changes: 1 addition & 1 deletion tests/testcases/regression/BOO-281-1.boo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
2
3
"""
m = matrix(int, (2, 2))
m = matrix(int, 2, 2)
value = 0
for i in range(2):
for j in range(2):
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/regression/BOO-281-2.boo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
21
"""
m = matrix(int, (2, 2))
m = matrix(int, 2, 2)
value = 0
for i in range(2):
for j in range(2):
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/stdlib/cat-1.boo
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ b = 4, 5, 6
c = 7, 8, 9

print join(cat(a, b))
print join(cat((a, b, c)))
print join(cat(a, b, c))

0 comments on commit 09f1a04

Please sign in to comment.