diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/CodegenContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/CodegenContext.java index ce0eb62f800..a1135e9c65c 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/CodegenContext.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/CodegenContext.java @@ -10,6 +10,7 @@ import org.objectweb.asm.commons.InstructionAdapter; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; /* * @author max @@ -38,6 +39,8 @@ public abstract class CodegenContext { public final ObjectOrClosureCodegen closure; HashMap typeInfoConstants; + HashMap reverseTypeInfoConstants; + int typeInfoConstantsCount; HashMap accessors; protected StackValue outerExpression; @@ -177,13 +180,16 @@ public abstract class CodegenContext { if(parentContext != STATIC) return parentContext.getTypeInfoConstantIndex(type); - if(typeInfoConstants == null) - typeInfoConstants = new HashMap(); + if(typeInfoConstants == null) { + typeInfoConstants = new LinkedHashMap(); + reverseTypeInfoConstants = new LinkedHashMap(); + } Integer index = typeInfoConstants.get(type); if(index == null) { - index = typeInfoConstants.size(); + index = typeInfoConstantsCount++; typeInfoConstants.put(type, index); + reverseTypeInfoConstants.put(index, type); } return index; } diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index e69555a882b..ea4c1848f16 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -2068,14 +2068,14 @@ public class ExpressionCodegen extends JetVisitor { return type; } - private void generateNewArray(JetCallExpression expression, JetType arrayType) { + public void generateNewArray(JetCallExpression expression, JetType arrayType) { List args = expression.getValueArguments(); boolean isArray = state.getStandardLibrary().getArray().equals(arrayType.getConstructor().getDeclarationDescriptor()); if(isArray) { - if (args.size() != 2 && !arrayType.getArguments().get(0).getType().isNullable()) { - throw new CompilationException("array constructor of non-nullable type requires two arguments"); - } +// if (args.size() != 2 && !arrayType.getArguments().get(0).getType().isNullable()) { +// throw new CompilationException("array constructor of non-nullable type requires two arguments"); +// } } else { if (args.size() != 1) { diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java index 908c8d29867..7350a49cdbb 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java @@ -13,8 +13,10 @@ import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Type; import org.objectweb.asm.commons.InstructionAdapter; +import sun.jvm.hotspot.debugger.LongHashMap; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -114,11 +116,12 @@ public class NamespaceCodegen { private void generateTypeInfoFields(JetNamespace namespace, CodegenContext context) { if(context.typeInfoConstants != null) { String jvmClassName = getJVMClassName(namespace.getName()); - for(Map.Entry e : (context.typeInfoConstants != null ? context.typeInfoConstants : Collections.emptyMap()).entrySet()) { - String fieldName = "$typeInfoCache$" + e.getValue(); + for(int index = 0; index != context.typeInfoConstantsCount; index++) { + JetType type = context.reverseTypeInfoConstants.get(index); + String fieldName = "$typeInfoCache$" + index; v.newField(null, ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, fieldName, "Ljet/typeinfo/TypeInfo;", null, null); - MethodVisitor mmv = v.newMethod(null, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, "$getCachedTypeInfo$" + e.getValue(), "()Ljet/typeinfo/TypeInfo;", null, null); + MethodVisitor mmv = v.newMethod(null, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, "$getCachedTypeInfo$" + index, "()Ljet/typeinfo/TypeInfo;", null, null); InstructionAdapter v = new InstructionAdapter(mmv); v.visitFieldInsn(GETSTATIC, jvmClassName, fieldName, "Ljet/typeinfo/TypeInfo;"); v.visitInsn(DUP); @@ -126,7 +129,7 @@ public class NamespaceCodegen { v.visitJumpInsn(IFNONNULL, end); v.pop(); - generateTypeInfo(context, v, e.getKey(), state.getTypeMapper(), e.getKey()); + generateTypeInfo(context, v, type, state.getTypeMapper(), type); v.dup(); v.visitFieldInsn(PUTSTATIC, jvmClassName, fieldName, "Ljet/typeinfo/TypeInfo;"); diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java index a75db2d5a37..e86d46bfb8e 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java @@ -15,6 +15,7 @@ import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.jetbrains.jet.lang.types.TypeProjection; import org.jetbrains.jet.plugin.JetFileType; import org.objectweb.asm.Opcodes; +import sun.tools.tree.NewArrayExpression; import java.util.*; @@ -85,6 +86,7 @@ public class IntrinsicMethods { declareOverload(myStdLib.getLibraryScope().getFunctions("equals"), 1, EQUALS); declareOverload(myStdLib.getLibraryScope().getFunctions("identityEquals"), 1, EQUALS); declareOverload(myStdLib.getLibraryScope().getFunctions("plus"), 1, new StringPlus()); + declareOverload(myStdLib.getLibraryScope().getFunctions("Array"), 1, new NewArray()); declareIntrinsicFunction("ByteIterator", "next", 0, ITERATOR_NEXT); declareIntrinsicFunction("ShortIterator", "next", 0, ITERATOR_NEXT); diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/NewArray.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/NewArray.java new file mode 100644 index 00000000000..318dfcfdf7f --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/NewArray.java @@ -0,0 +1,24 @@ +package org.jetbrains.jet.codegen.intrinsics; + +import com.intellij.psi.PsiElement; +import org.jetbrains.jet.codegen.ExpressionCodegen; +import org.jetbrains.jet.codegen.JetTypeMapper; +import org.jetbrains.jet.codegen.StackValue; +import org.jetbrains.jet.lang.psi.JetCallExpression; +import org.jetbrains.jet.lang.psi.JetExpression; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.objectweb.asm.Type; +import org.objectweb.asm.commons.InstructionAdapter; + +import java.util.List; + +/** + * @author alex.tkachman + */ +public class NewArray implements IntrinsicMethod { + @Override + public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, PsiElement element, List arguments, StackValue receiver) { + codegen.generateNewArray((JetCallExpression) element, codegen.getBindingContext().get(BindingContext.EXPRESSION_TYPE, (JetExpression) element)); + return StackValue.onStack(expectedType); + } +} diff --git a/compiler/frontend/src/jet/Library.jet b/compiler/frontend/src/jet/Library.jet index 24a7f4e962f..279171d8776 100644 --- a/compiler/frontend/src/jet/Library.jet +++ b/compiler/frontend/src/jet/Library.jet @@ -105,7 +105,9 @@ trait Iterable { fun iterator() : Iterator } -class Array(val size : Int, init : fun(Int) : T = null ) { +fun Array(val size : Int) : Array + +class Array(val size : Int, init : fun(Int) : T) { fun get(index : Int) : T fun set(index : Int, value : T) : Unit diff --git a/compiler/testData/codegen/regressions/kt602.jet b/compiler/testData/codegen/regressions/kt602.jet new file mode 100644 index 00000000000..aae930a7328 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt602.jet @@ -0,0 +1 @@ +fun box() = if(Array(10) is Array) "OK" else "fail" diff --git a/compiler/tests/org/jetbrains/jet/codegen/ArrayGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/ArrayGenTest.java index 80063a36516..c40d7cffb60 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ArrayGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ArrayGenTest.java @@ -276,6 +276,10 @@ public class ArrayGenTest extends CodegenTestCase { blackBoxFile("regressions/kt503.jet"); } + public void testKt602() { + blackBoxFile("regressions/kt602.jet"); + } + public void testKt594() throws Exception { loadFile("regressions/kt594.jet"); System.out.println(generateToText());