Browse Source

Fix varargs for no argument and in static methods

kindlich 4 years ago
parent
commit
c4be2b92fa
No known key found for this signature in database

+ 12
- 6
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallArguments.java View File

8
 import java.util.Arrays;
8
 import java.util.Arrays;
9
 import org.openzen.zencode.shared.CodePosition;
9
 import org.openzen.zencode.shared.CodePosition;
10
 import org.openzen.zencode.shared.CompileExceptionCode;
10
 import org.openzen.zencode.shared.CompileExceptionCode;
11
-import org.openzen.zenscript.codemodel.FunctionHeader;
11
+import org.openzen.zenscript.codemodel.*;
12
 import org.openzen.zenscript.codemodel.scope.TypeScope;
12
 import org.openzen.zenscript.codemodel.scope.TypeScope;
13
-import org.openzen.zenscript.codemodel.type.StoredType;
13
+import org.openzen.zenscript.codemodel.type.*;
14
 
14
 
15
 /**
15
 /**
16
  *
16
  *
64
 		if (arguments.length < header.parameters.length) {
64
 		if (arguments.length < header.parameters.length) {
65
 			Expression[] newArguments = Arrays.copyOf(arguments, header.parameters.length);
65
 			Expression[] newArguments = Arrays.copyOf(arguments, header.parameters.length);
66
 			for (int i = arguments.length; i < header.parameters.length; i++) {
66
 			for (int i = arguments.length; i < header.parameters.length; i++) {
67
-				if (header.parameters[i].defaultValue == null)
68
-					newArguments[i] = new InvalidExpression(position, header.parameters[i].type, CompileExceptionCode.MISSING_PARAMETER, "Parameter missing and no default value specified");
69
-				else
70
-					newArguments[i] = header.parameters[i].defaultValue;
67
+                final FunctionParameter parameter = header.parameters[i];
68
+                if (parameter.defaultValue == null) {
69
+				    if(parameter.variadic) {
70
+				        newArguments[i] = new ArrayExpression(position, Expression.NONE, parameter.type);
71
+                    } else {
72
+                        newArguments[i] = new InvalidExpression(position, parameter.type, CompileExceptionCode.MISSING_PARAMETER, "Parameter missing and no default value specified");
73
+                    }
74
+                } else {
75
+                    newArguments[i] = parameter.defaultValue;
76
+                }
71
 			}
77
 			}
72
 			result = new CallArguments(typeArguments, newArguments);
78
 			result = new CallArguments(typeArguments, newArguments);
73
 		}
79
 		}

+ 26
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

1213
 
1213
 
1214
 	@Override
1214
 	@Override
1215
 	public Void visitCallStatic(CallStaticExpression expression) {
1215
 	public Void visitCallStatic(CallStaticExpression expression) {
1216
-		for (Expression argument : expression.arguments.arguments)
1217
-			argument.accept(this);
1216
+        final Expression[] arguments = expression.arguments.arguments;
1217
+        final FunctionParameter[] parameters = expression.instancedHeader.parameters;
1218
+        final boolean variadic = expression.instancedHeader.isVariadicCall(expression.arguments) && ((arguments.length != parameters.length) || !parameters[parameters.length - 1].type.type
1219
+                .equals(arguments[arguments.length - 1].type.type));
1220
+        
1221
+        if(variadic) {
1222
+            for (int i = 0; i < parameters.length - 1; i++) {
1223
+                arguments[i].accept(this);
1224
+            }
1225
+            
1226
+            final int arrayCount = (arguments.length - parameters.length) + 1;
1227
+            javaWriter.constant(arrayCount);
1228
+            javaWriter.newArray(context.getType(parameters[parameters.length - 1].type).getElementType());
1229
+            for (int i = 0; i < arrayCount; i++) {
1230
+                javaWriter.dup();
1231
+                javaWriter.constant(i);
1232
+                arguments[i + parameters.length - 1].accept(this);
1233
+                javaWriter.arrayStore(context.getType(arguments[i].type));
1234
+            }
1235
+            
1236
+            
1237
+        } else {
1238
+            for (Expression argument : arguments) {
1239
+                argument.accept(this);
1240
+            }
1241
+        }
1218
 
1242
 
1219
 		BuiltinID builtin = expression.member.getBuiltin();
1243
 		BuiltinID builtin = expression.member.getBuiltin();
1220
 		if (builtin == null) {
1244
 		if (builtin == null) {

Loading…
Cancel
Save