From 93d5a3b5ed2e5c5c76f8ecd1ec4758d52a8f9273 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Tue, 1 Nov 2011 17:11:06 +0100 Subject: [PATCH] lazy initialization of classes --- compiler/frontend/src/jet/Library.jet | 4 + .../jet/lang/types/JetStandardLibrary.java | 175 ++++++++++++------ .../org/jetbrains/jet/codegen/VarArgTest.java | 10 + 3 files changed, 131 insertions(+), 58 deletions(-) diff --git a/compiler/frontend/src/jet/Library.jet b/compiler/frontend/src/jet/Library.jet index b88e5eb0287..4012dbc5795 100644 --- a/compiler/frontend/src/jet/Library.jet +++ b/compiler/frontend/src/jet/Library.jet @@ -40,6 +40,10 @@ fun Any?.equals(other : Any?) : Boolean// = this === other // Returns "null" for null fun Any?.toString() : String// = this === other +// fun array(vararg elements : T) : Array + +// fun array(vararg elements : Int) : IntArray + fun String?.plus(other: Any?) : String trait Iterator { diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java index 7b35d6f582e..4bc50d96b78 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java @@ -47,68 +47,70 @@ public class JetStandardLibrary { // return standardLibrary; } - private final JetScope libraryScope; + private JetScope libraryScope; - private final ClassDescriptor byteClass; - private final ClassDescriptor charClass; - private final ClassDescriptor shortClass; - private final ClassDescriptor intClass; - private final ClassDescriptor longClass; - private final ClassDescriptor floatClass; - private final ClassDescriptor doubleClass; - private final ClassDescriptor booleanClass; - private final ClassDescriptor stringClass; - private final ClassDescriptor arrayClass; - private final ClassDescriptor iterableClass; - private final ClassDescriptor typeInfoClass; + private ClassDescriptor byteClass; + private ClassDescriptor charClass; + private ClassDescriptor shortClass; + private ClassDescriptor intClass; + private ClassDescriptor longClass; + private ClassDescriptor floatClass; + private ClassDescriptor doubleClass; + private ClassDescriptor booleanClass; - private final JetType byteType; - private final JetType charType; - private final JetType shortType; - private final JetType intType; - private final JetType longType; - private final JetType floatType; - private final JetType doubleType; - private final JetType booleanType; - private final JetType stringType; + private ClassDescriptor stringClass; + private ClassDescriptor arrayClass; + private ClassDescriptor iterableClass; + private ClassDescriptor typeInfoClass; - private final JetType nullableByteType; - private final JetType nullableCharType; - private final JetType nullableShortType; - private final JetType nullableIntType; - private final JetType nullableLongType; - private final JetType nullableFloatType; - private final JetType nullableDoubleType; - private final JetType nullableBooleanType; - private final JetType nullableTuple0Type; + private JetType byteType; + private JetType charType; + private JetType shortType; + private JetType intType; + private JetType longType; + private JetType floatType; + private JetType doubleType; + private JetType booleanType; - private final ClassDescriptor byteArrayClass; - private final ClassDescriptor charArrayClass; - private final ClassDescriptor shortArrayClass; - private final ClassDescriptor intArrayClass; - private final ClassDescriptor longArrayClass; - private final ClassDescriptor floatArrayClass; - private final ClassDescriptor doubleArrayClass; - private final ClassDescriptor booleanArrayClass; + private JetType stringType; - private final JetType byteArrayType; - private final JetType charArrayType; - private final JetType shortArrayType; - private final JetType intArrayType; - private final JetType longArrayType; - private final JetType floatArrayType; - private final JetType doubleArrayType; - private final JetType booleanArrayType; + private JetType nullableByteType; + private JetType nullableCharType; + private JetType nullableShortType; + private JetType nullableIntType; + private JetType nullableLongType; + private JetType nullableFloatType; + private JetType nullableDoubleType; + private JetType nullableBooleanType; + private JetType nullableTuple0Type; + + private ClassDescriptor byteArrayClass; + private ClassDescriptor charArrayClass; + private ClassDescriptor shortArrayClass; + private ClassDescriptor intArrayClass; + private ClassDescriptor longArrayClass; + private ClassDescriptor floatArrayClass; + private ClassDescriptor doubleArrayClass; + private ClassDescriptor booleanArrayClass; + + private JetType byteArrayType; + private JetType charArrayType; + private JetType shortArrayType; + private JetType intArrayType; + private JetType longArrayType; + private JetType floatArrayType; + private JetType doubleArrayType; + private JetType booleanArrayType; public JetType getTuple0Type() { return tuple0Type; } - private final JetType tuple0Type; - private final JetType nullableStringType; + private JetType tuple0Type; + private JetType nullableStringType; - private final NamespaceDescriptor typeInfoNamespace; - private final Set typeInfoFunction; + private NamespaceDescriptor typeInfoNamespace; + private Set typeInfoFunction; private JetStandardLibrary(@NotNull Project project) { // TODO : review @@ -124,10 +126,23 @@ public class JetStandardLibrary { // this.libraryScope = bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations()); // bootstrappingTDA.process(writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, file.getRootNamespace().getDeclarations()); TopDownAnalyzer.processStandardLibraryNamespace(bootstrappingSemanticServices, bindingTraceContext, writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, file.getRootNamespace()); - this.libraryScope = JetStandardClasses.STANDARD_CLASSES_NAMESPACE.getMemberScope(); +// this.libraryScope = JetStandardClasses.STANDARD_CLASSES_NAMESPACE.getMemberScope(); AnalyzingUtils.throwExceptionOnErrors(bindingTraceContext.getBindingContext()); + initStdClasses(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + public JetScope getLibraryScope() { + initStdClasses(); + return libraryScope; + } + + private void initStdClasses() { + if(libraryScope == null) { + this.libraryScope = JetStandardClasses.STANDARD_CLASSES_NAMESPACE.getMemberScope(); this.byteClass = (ClassDescriptor) libraryScope.getClassifier("Byte"); this.charClass = (ClassDescriptor) libraryScope.getClassifier("Char"); this.shortClass = (ClassDescriptor) libraryScope.getClassifier("Short"); @@ -138,6 +153,7 @@ public class JetStandardLibrary { this.booleanClass = (ClassDescriptor) libraryScope.getClassifier("Boolean"); this.stringClass = (ClassDescriptor) libraryScope.getClassifier("String"); this.arrayClass = (ClassDescriptor) libraryScope.getClassifier("Array"); + this.iterableClass = (ClassDescriptor) libraryScope.getClassifier("Iterable"); typeInfoNamespace = libraryScope.getNamespace("typeinfo"); this.typeInfoClass = (ClassDescriptor) typeInfoNamespace.getMemberScope().getClassifier("TypeInfo"); @@ -182,79 +198,87 @@ public class JetStandardLibrary { this.nullableBooleanType = TypeUtils.makeNullable(booleanType); this.nullableStringType = TypeUtils.makeNullable(stringType); this.nullableTuple0Type = TypeUtils.makeNullable(tuple0Type); - } catch (IOException e) { - throw new IllegalStateException(e); } } - public JetScope getLibraryScope() { - return libraryScope; - } - @NotNull public ClassDescriptor getByte() { + initStdClasses(); return byteClass; } @NotNull public ClassDescriptor getChar() { + initStdClasses(); return charClass; } @NotNull public ClassDescriptor getShort() { + initStdClasses(); return shortClass; } @NotNull public ClassDescriptor getInt() { + initStdClasses(); return intClass; } @NotNull public ClassDescriptor getLong() { + initStdClasses(); return longClass; } @NotNull public ClassDescriptor getFloat() { + initStdClasses(); return floatClass; } @NotNull public ClassDescriptor getDouble() { + initStdClasses(); return doubleClass; } @NotNull public ClassDescriptor getBoolean() { + initStdClasses(); return booleanClass; } @NotNull public ClassDescriptor getString() { + initStdClasses(); return stringClass; } @NotNull public ClassDescriptor getArray() { + initStdClasses(); return arrayClass; } @NotNull public ClassDescriptor getIterable() { + initStdClasses(); return iterableClass; } public NamespaceDescriptor getTypeInfoNamespace() { + initStdClasses(); return typeInfoNamespace; } public ClassDescriptor getTypeInfo() { + initStdClasses(); return typeInfoClass; } public Set getTypeInfoFunctions() { + initStdClasses(); return typeInfoFunction; } @@ -267,46 +291,55 @@ public class JetStandardLibrary { @NotNull public JetType getIntType() { + initStdClasses(); return intType; } @NotNull public JetType getLongType() { + initStdClasses(); return longType; } @NotNull public JetType getDoubleType() { + initStdClasses(); return doubleType; } @NotNull public JetType getFloatType() { + initStdClasses(); return floatType; } @NotNull public JetType getCharType() { + initStdClasses(); return charType; } @NotNull public JetType getBooleanType() { + initStdClasses(); return booleanType; } @NotNull public JetType getStringType() { + initStdClasses(); return stringType; } @NotNull public JetType getByteType() { + initStdClasses(); return byteType; } @NotNull public JetType getShortType() { + initStdClasses(); return shortType; } @@ -340,106 +373,132 @@ public class JetStandardLibrary { } public JetType getNullableStringType() { + initStdClasses(); return nullableStringType; } public JetType getNullableByteType() { + initStdClasses(); return nullableByteType; } public JetType getNullableCharType() { + initStdClasses(); return nullableCharType; } public JetType getNullableShortType() { + initStdClasses(); return nullableShortType; } public JetType getNullableIntType() { + initStdClasses(); return nullableIntType; } public JetType getNullableLongType() { + initStdClasses(); return nullableLongType; } public JetType getNullableFloatType() { + initStdClasses(); return nullableFloatType; } public JetType getNullableDoubleType() { + initStdClasses(); return nullableDoubleType; } public JetType getNullableBooleanType() { + initStdClasses(); return nullableBooleanType; } public JetType getNullableTuple0Type() { + initStdClasses(); return nullableTuple0Type; } public JetType getBooleanArrayType() { + initStdClasses(); return booleanArrayType; } public JetType getByteArrayType() { + initStdClasses(); return byteArrayType; } public JetType getCharArrayType() { + initStdClasses(); return charArrayType; } public JetType getShortArrayType() { + initStdClasses(); return shortArrayType; } public JetType getIntArrayType() { + initStdClasses(); return intArrayType; } public JetType getLongArrayType() { + initStdClasses(); return longArrayType; } public JetType getFloatArrayType() { + initStdClasses(); return floatArrayType; } public JetType getDoubleArrayType() { + initStdClasses(); return doubleArrayType; } public ClassDescriptor getByteArrayClass() { + initStdClasses(); return byteArrayClass; } public ClassDescriptor getCharArrayClass() { + initStdClasses(); return charArrayClass; } public ClassDescriptor getShortArrayClass() { + initStdClasses(); return shortArrayClass; } public ClassDescriptor getIntArrayClass() { + initStdClasses(); return intArrayClass; } public ClassDescriptor getLongArrayClass() { + initStdClasses(); return longArrayClass; } public ClassDescriptor getFloatArrayClass() { + initStdClasses(); return floatArrayClass; } public ClassDescriptor getDoubleArrayClass() { + initStdClasses(); return doubleArrayClass; } public ClassDescriptor getBooleanArrayClass() { + initStdClasses(); return booleanArrayClass; } } diff --git a/compiler/tests/org/jetbrains/jet/codegen/VarArgTest.java b/compiler/tests/org/jetbrains/jet/codegen/VarArgTest.java index ea4174110bf..6c5cf3a4973 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/VarArgTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/VarArgTest.java @@ -59,4 +59,14 @@ public class VarArgTest extends CodegenTestCase { assertTrue(((String[])res).length == 1); assertTrue(((String[])res)[0].equals("239")); } + + public void testArrayT () throws InvocationTargetException, IllegalAccessException { + loadText("fun test() = _array(2, 4); fun _array(vararg elements : T) = elements"); + System.out.println(generateToText()); + final Method main = generateFunction(); + Object res = main.invoke(null, new Object[]{}); + assertTrue(((Integer[])res).length == 2); + assertTrue(((Integer[])res)[0].equals(2)); + assertTrue(((Integer[])res)[1].equals(4)); + } }