From c27885b74f1e6d20e872583012b9fc4d1f33bb8a Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Tue, 20 Dec 2011 10:28:47 +0300 Subject: [PATCH 01/68] Stable classnames for object declarations whose type is exposed to java clients. --- .../jetbrains/jet/codegen/JetTypeMapper.java | 61 ++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java index ef2cef70364..7bcb0463f3c 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java @@ -1,5 +1,6 @@ package org.jetbrains.jet.codegen; +import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; import jet.JetObject; import jet.typeinfo.TypeInfo; @@ -180,7 +181,39 @@ public class JetTypeMapper { } private HashMap> naming = new HashMap>(); - + + private String getStableNameForObject(JetObjectDeclaration object, DeclarationDescriptor descriptor) { + String local = getLocalNameForObject(object); + if (local == null) return null; + + DeclarationDescriptor containingClass = getContainingClass(descriptor); + if (containingClass != null) { + return getFQName(containingClass) + "$" + local; + } + else { + return getFQName(getContainingNamespace(descriptor)) + "/" + local; + } + } + + private static String getLocalNameForObject(JetObjectDeclaration object) { + PsiElement parent = object.getParent(); + if (parent instanceof JetObjectLiteralExpression) { + PsiElement expressionParent = parent.getParent(); + + if (expressionParent instanceof JetProperty) { + JetProperty property = (JetProperty) expressionParent; + if (property.getInitializer() == parent) { + return property.getName(); + } + } + } + else if (parent instanceof JetClassObject) { + return "$ClassObject"; + } + + return null; + } + public String getFQName(DeclarationDescriptor descriptor) { descriptor = descriptor.getOriginal(); @@ -200,7 +233,17 @@ public class JetTypeMapper { name = map.get(descriptor); if(name == null) { - name = getFQName(container) + "$" + (map.size()+1); + PsiElement declaration = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, descriptor); + if (declaration instanceof JetObjectDeclaration) { + String stable = getStableNameForObject((JetObjectDeclaration) declaration, descriptor); + if (stable != null) { + name = stable; + } + } + + if (name == null) { + name = getFQName(container) + "$" + (map.size()+1); + } map.put(descriptor, name); } return name; @@ -222,7 +265,19 @@ public class JetTypeMapper { return name; } - + + private static ClassDescriptor getContainingClass(DeclarationDescriptor descriptor) { + DeclarationDescriptor parent = descriptor.getContainingDeclaration(); + if (parent == null || parent instanceof ClassDescriptor) return (ClassDescriptor) parent; + return getContainingClass(parent); + } + + private static NamespaceDescriptor getContainingNamespace(DeclarationDescriptor descriptor) { + DeclarationDescriptor parent = descriptor.getContainingDeclaration(); + if (parent == null || parent instanceof NamespaceDescriptor) return (NamespaceDescriptor) parent; + return getContainingNamespace(parent); + } + @NotNull public Type mapType(final JetType jetType) { return mapType(jetType, (BothSignatureWriter) null); } From de4b0a4601e3b01ebcf93aca75090a649edde445 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 20 Dec 2011 17:27:22 +0000 Subject: [PATCH 02/68] added Alex's to(collection: Collection) helper method and simple 1 liners for toList / toLinkedList / toSet etc --- stdlib/ktSrc/JavaUtil.kt | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/stdlib/ktSrc/JavaUtil.kt b/stdlib/ktSrc/JavaUtil.kt index a96ac23b357..46cbda926e0 100644 --- a/stdlib/ktSrc/JavaUtil.kt +++ b/stdlib/ktSrc/JavaUtil.kt @@ -123,17 +123,22 @@ inline fun java.util.Collection.map(result: Collection = ArrayList< return result } -inline fun > java.lang.Iterable.toSortedList() : List { - val answer = this.toList() - answer.sort() - return answer + +inline fun > java.lang.Iterable.to(result: C) : C { + for (elem in this) + result.add(elem) + return result } -inline fun > java.lang.Iterable.toSortedList(comparator: java.util.Comparator) : List { - val answer = this.toList() - answer.sort(comparator) - return answer -} +inline fun java.lang.Iterable.toLinkedList() : LinkedList = this.to(LinkedList()) + +inline fun java.lang.Iterable.toList() : List = this.to(ArrayList()) + +inline fun java.lang.Iterable.toSet() : Set = this.to(HashSet()) + +inline fun > java.lang.Iterable.toSortedList() : List = toList().sort() + +inline fun > java.lang.Iterable.toSortedList(comparator: java.util.Comparator) : List = toList().sort(comparator) /** TODO figure out necessary variance/generics ninja stuff... :) @@ -144,17 +149,6 @@ inline fun java.lang.Iterable.toSortedList(transform: fun(T) : java.la } */ -inline fun java.lang.Iterable.toList() : List { - if (this is List) - return this - else { - val list = ArrayList() - for (elem in this) - list.add(elem) - return list - } -} - inline fun java.util.Collection.toArray() : Array { val answer = Array(this.size) var idx = 0 @@ -166,12 +160,14 @@ inline fun java.util.Collection.toArray() : Array { // List APIs -inline fun > List.sort() : Unit { +inline fun > List.sort() : List { Collections.sort(this) + return this } -inline fun > List.sort(comparator: java.util.Comparator) : Unit { +inline fun > List.sort(comparator: java.util.Comparator) : List { Collections.sort(this, comparator) + return this } /** From 212160fa47f03d0c38b66d206ab2042f8256972d Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 20 Dec 2011 17:46:26 +0000 Subject: [PATCH 03/68] added Array.to(collection) too, along with simpler more DRY one liners for arrayList(), linkedList() and hashSet() helper functions --- stdlib.ipr | 3 +++ stdlib/ktSrc/JavaUtil.kt | 28 ++++++++++------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/stdlib.ipr b/stdlib.ipr index 30c354500c5..693c766ab77 100644 --- a/stdlib.ipr +++ b/stdlib.ipr @@ -27,6 +27,9 @@