diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 59ecaca0820..51be0ea12eb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -12,18 +12,17 @@ - - - + + + + - - - - - - - + + + + + @@ -123,13 +122,40 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -137,19 +163,10 @@ - - + + - - - - - - - - - - + @@ -157,64 +174,52 @@ - - - - - - - - - - - - - - - - - - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -263,7 +268,7 @@ - + @@ -341,14 +346,6 @@ @@ -439,7 +444,7 @@ - + @@ -613,6 +618,46 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -854,6 +981,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1227,72 +1410,16 @@ - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1304,16 +1431,9 @@ - + - - - - - - - - + @@ -1325,9 +1445,9 @@ - + - + @@ -1339,13 +1459,81 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/js/src/com/google/dart/compiler/util/AstUtil.java b/js/src/com/google/dart/compiler/util/AstUtil.java index b05cf4711f7..c635d696c54 100644 --- a/js/src/com/google/dart/compiler/util/AstUtil.java +++ b/js/src/com/google/dart/compiler/util/AstUtil.java @@ -353,6 +353,8 @@ public class AstUtil { public JsNode mutate(JsNode node); } + + //TODO: move somewhere //TODO: refactor and review public static JsNode mutateLastExpression(JsNode node, Mutator mutator) { if (node instanceof JsBlock) { diff --git a/jslib/src/core/javalang.kt b/jslib/src/core/javalang.kt new file mode 100644 index 00000000000..d41bced218d --- /dev/null +++ b/jslib/src/core/javalang.kt @@ -0,0 +1,9 @@ +package java.lang + +import java.util.Iterator; +import js.annotations.LibraryClass + +LibraryClass +trait Iterable { + fun iterator() : java.util.Iterator {} +} \ No newline at end of file diff --git a/jslib/src/core/javautil.kt b/jslib/src/core/javautil.kt index f9987cf76a6..288446b236c 100644 --- a/jslib/src/core/javautil.kt +++ b/jslib/src/core/javautil.kt @@ -1,106 +1,194 @@ package java.util -public open class ArrayList(initialCapacity : Int = 0) : List { - override public fun size() : Int - override public fun isEmpty() : Boolean - override public fun contains(o : Any?) : Boolean - override public fun containsAll(c : Collection<*>?) : Boolean - override public fun indexOf(o : Any?) : Int - override public fun lastIndexOf(o : Any?) : Int - override public fun toArray() : Array - override public fun toArray(a : Array) : Array - override public fun get(index : Int) : E - override public fun set(index : Int, element : E) : E - override public fun add(e : E) : Boolean - override public fun add(index : Int, element : E) : Unit - override public fun remove(index : Int) : E? - override public fun clear() : Unit - override public fun addAll(c : Collection?) : Boolean - override public fun addAll(index : Int, c : Collection?) : Boolean - override public fun removeAll(c : Collection<*>?) : Boolean - override public fun retainAll(c : Collection<*>?) : Boolean - override public fun iterator() : Iterator - override public fun subList(fromIndex : Int, toIndex : Int) : List? - override public fun remove(o : Any?) : Boolean +import java.lang; +import js.annotations.LibraryClass + +LibraryClass +public trait Iterator { + fun hasNext() : Boolean + fun next() : T } -public trait Collection { - open public fun size() : Int - open public fun isEmpty() : Boolean - open public fun contains(o : Any?) : Boolean - open public fun iterator() : Iterator - open public fun toArray() : Array - open public fun toArray(a : Array) : Array - open public fun add(e : E) : Boolean - open public fun containsAll(c : Collection<*>?) : Boolean - open public fun addAll(c : Collection?) : Boolean - open public fun removeAll(c : Collection<*>?) : Boolean - open public fun retainAll(c : Collection<*>?) : Boolean - open public fun clear() : Unit - open public fun remove(o : Any?) : Boolean +LibraryClass +public open class ArrayList() : java.util.List, java.util.AbstractList() { + override public fun size() : Int {} + override public fun isEmpty() : Boolean {} + override public fun contains(o : Any?) : Boolean {} + override public fun indexOf(o : Any?) : Int {} + override public fun lastIndexOf(o : Any?) : Int {} + override public fun toArray() : Array {} + override public fun toArray(a : Array) : Array {} + override public fun get(index : Int) : E {} + override public fun set(index : Int, element : E) : E {} + override public fun add(e : E) : Boolean {} + override public fun add(index : Int, element : E) : Unit {} + override public fun remove(index : Int) : E {} + override public fun remove(o : Any?) : Boolean {} + override public fun clear() : Unit {} + override public fun addAll(c : java.util.Collection) : Boolean {} + override public fun addAll(index : Int, c : java.util.Collection) : Boolean {} } - -public trait List : Collection { - override public fun size() : Int - override public fun isEmpty() : Boolean - override public fun contains(o : Any?) : Boolean - override public fun iterator() : Iterator - override public fun toArray() : Array - override public fun toArray(a : Array) : Array - override public fun add(e : E) : Boolean - override public fun containsAll(c : Collection<*>?) : Boolean - override public fun addAll(c : Collection?) : Boolean - open public fun addAll(index : Int, c : Collection?) : Boolean - override public fun removeAll(c : Collection<*>?) : Boolean - override public fun retainAll(c : Collection<*>?) : Boolean - override public fun clear() : Unit - open public fun get(index : Int) : E - open public fun set(index : Int, element : E) : E - open public fun add(index : Int, element : E) : Unit - js_name("remove_foo") - open public fun remove(index : Int) : E? - open public fun indexOf(o : Any?) : Int - open public fun lastIndexOf(o : Any?) : Int - open public fun subList(fromIndex : Int, toIndex : Int) : List? - js_name("remove_foo") - override public fun remove(o : Any?) : Boolean +LibraryClass +public trait Collection : java.lang.Iterable { + open fun size() : Int + open fun isEmpty() : Boolean + open fun contains(o : Any?) : Boolean + override fun iterator() : Iterator + open fun toArray() : Array + open fun toArray(a : Array) : Array + open fun add(e : E) : Boolean + open fun remove(o : Any?) : Boolean + open fun containsAll(c : java.util.Collection<*>) : Boolean + open fun addAll(c : java.util.Collection) : Boolean + open fun removeAll(c : java.util.Collection<*>) : Boolean + open fun retainAll(c : java.util.Collection<*>) : Boolean + open fun clear() : Unit } -annotation class js_name(s : String) - -trait Set : Collection - -public trait Map { - open public fun size() : Int - open public fun isEmpty() : Boolean - open public fun containsKey(key : Any?) : Boolean - open public fun containsValue(value : Any?) : Boolean - open public fun get(key : Any?) : V - open public fun put(key : K?, value : V?) : V? - open public fun remove(key : Any?) : V? - open public fun putAll(m : Map?) : Unit - open public fun clear() : Unit - open public fun keySet() : Set - open public fun values() : Collection? +LibraryClass +public trait List : java.util.Collection { + override fun size() : Int + override fun isEmpty() : Boolean + override fun contains(o : Any?) : Boolean + override fun iterator() : Iterator + override fun toArray() : Array + // Simulate Java's array covariance + override fun toArray(a : Array) : Array + override fun add(e : E) : Boolean + override fun remove(o : Any?) : Boolean + override fun containsAll(c : java.util.Collection<*>) : Boolean + override fun addAll(c : java.util.Collection) : Boolean + open fun addAll(index : Int, c : java.util.Collection) : Boolean + override fun removeAll(c : java.util.Collection<*>) : Boolean + override fun retainAll(c : java.util.Collection<*>) : Boolean + override fun clear() : Unit + open fun get(index : Int) : E + open fun set(index : Int, element : E) : E + open fun add(index : Int, element : E) : Unit + open fun remove(index : Int) : E + open fun indexOf(o : Any?) : Int + open fun lastIndexOf(o : Any?) : Int } -public open class HashMap() : Map{ - override public fun size() : Int - override public fun isEmpty() : Boolean - override public fun get(key : Any?) : V - override public fun containsKey(key : Any?) : Boolean - override public fun put(key : K?, value : V?) : V? - override public fun putAll(m : Map?) : Unit - override public fun remove(key : Any?) : V? - override public fun clear() : Unit - override public fun containsValue(value : Any?) : Boolean - override public fun keySet() : Set - override public fun values() : Collection? +LibraryClass +abstract public class AbstractCollection protected () : java.util.Collection { + abstract override public fun iterator() : java.util.Iterator + abstract override public fun size() : Int + override public fun isEmpty() : Boolean {} + override public fun contains(o : Any?) : Boolean {} + override public fun toArray() : Array {} + override public fun toArray(a : Array) : Array {} + override public fun add(e : E) : Boolean {} + override public fun remove(o : Any?) : Boolean {} + override public fun containsAll(c : java.util.Collection<*>) : Boolean {} + override public fun addAll(c : java.util.Collection) : Boolean {} + override public fun removeAll(c : java.util.Collection<*>) : Boolean {} + override public fun retainAll(c : java.util.Collection<*>) : Boolean {} + override public fun clear() : Unit {} } +LibraryClass +abstract public open class AbstractList protected () : java.util.AbstractCollection(), java.util.List { + override public fun add(e : E) : Boolean {} + abstract override public fun get(index : Int) : E + override public fun set(index : Int, element : E) : E {} + override public fun add(index : Int, element : E) : Unit {} + override public fun remove(index : Int) : E {} + override public fun indexOf(o : Any?) : Int {} + override public fun lastIndexOf(o : Any?) : Int {} + override public fun clear() : Unit {} + override public fun addAll(index : Int, c : java.util.Collection) : Boolean {} + override public fun iterator() : Iterator {} + open protected fun removeRange(fromIndex : Int, toIndex : Int) : Unit {} +} + +LibraryClass +public trait Set : java.util.Collection { + override fun size() : Int + override fun isEmpty() : Boolean + override fun contains(o : Any?) : Boolean + override fun iterator() : Iterator + override fun toArray() : Array + override fun toArray(a : Array) : Array + override fun add(e : E) : Boolean + override fun remove(o : Any?) : Boolean + override fun containsAll(c : java.util.Collection<*>) : Boolean + override fun addAll(c : java.util.Collection) : Boolean + override fun retainAll(c : java.util.Collection<*>) : Boolean + override fun removeAll(c : java.util.Collection<*>) : Boolean + override fun clear() : Unit +} + +LibraryClass +abstract public class AbstractSet protected () : java.util.AbstractCollection(), java.util.Set { + override public fun removeAll(c : java.util.Collection<*>) : Boolean {} +} + +LibraryClass +public open class HashSet() : java.util.Set { + override public fun iterator() : Iterator {} + override public fun size() : Int {} + override public fun isEmpty() : Boolean {} + override public fun contains(o : Any?) : Boolean {} + override public fun add(e : E) : Boolean {} + override public fun remove(o : Any?) : Boolean {} + override fun toArray() : Array {} + override fun toArray(a : Array) : Array {} + override fun containsAll(c : java.util.Collection<*>) : Boolean {} + override fun addAll(c : java.util.Collection) : Boolean {} + override fun retainAll(c : java.util.Collection<*>) : Boolean {} + override fun removeAll(c : java.util.Collection<*>) : Boolean {} + override fun clear() : Unit {} +} + +LibraryClass +public trait Map { + open fun size() : Int + open fun isEmpty() : Boolean + open fun containsKey(key : Any?) : Boolean + open fun containsValue(value : Any?) : Boolean + open fun get(key : Any?) : V + open fun put(key : K, value : V) : V? + open fun remove(key : Any?) : V? + open fun putAll(m : java.util.Map) : Unit + open fun clear() : Unit + open fun keySet() : java.util.Set + open fun values() : java.util.Collection +} + +LibraryClass +abstract public open class AbstractMap protected () : java.util.Map { + override public fun size() : Int {} + override public fun isEmpty() : Boolean {} + override public fun containsValue(value : Any?) : Boolean {} + override public fun containsKey(key : Any?) : Boolean {} + override public fun get(key : Any?) : V {} + override public fun put(key : K, value : V) : V? {} + override public fun remove(key : Any?) : V? {} + override public fun putAll(m : java.util.Map) : Unit {} + override public fun clear() : Unit {} + override public fun keySet() : java.util.Set {} + override public fun values() : java.util.Collection {} +} + +LibraryClass +public open class HashMap() : java.util.Map { + override public fun size() : Int {} + override public fun isEmpty() : Boolean {} + override public fun get(key : Any?) : V {} + override public fun containsKey(key : Any?) : Boolean {} + override public fun put(key : K, value : V) : V? {} + override public fun putAll(m : java.util.Map) : Unit {} + override public fun remove(key : Any?) : V? {} + override public fun clear() : Unit {} + override public fun containsValue(value : Any?) : Boolean {} + override public fun keySet() : java.util.Set {} + override public fun values() : java.util.Collection {} +} + +LibraryClass public class StringBuilder() { public fun append(obj : Any) : StringBuilder public fun toString() : String } - diff --git a/translator/src/org/jetbrains/k2js/config/Config.java b/translator/src/org/jetbrains/k2js/config/Config.java index d912ef32c0b..394e6e91ca5 100644 --- a/translator/src/org/jetbrains/k2js/config/Config.java +++ b/translator/src/org/jetbrains/k2js/config/Config.java @@ -27,6 +27,7 @@ public abstract class Config { PATH_TO_JS_LIB_SRC + "\\core\\annotations.kt", PATH_TO_JS_LIB_SRC + "\\jquery\\common.kt", PATH_TO_JS_LIB_SRC + "\\core\\javautil.kt", + PATH_TO_JS_LIB_SRC + "\\core\\javalang.kt", PATH_TO_JS_LIB_SRC + "\\core\\core.kt" ); diff --git a/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotatedDeclarationVisitor.java b/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotatedDeclarationVisitor.java new file mode 100644 index 00000000000..093cfe442fb --- /dev/null +++ b/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotatedDeclarationVisitor.java @@ -0,0 +1,138 @@ +package org.jetbrains.k2js.translate.context.declaration; + +import com.google.dart.compiler.backend.js.ast.JsName; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; +import org.jetbrains.k2js.translate.context.NamingScope; + +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.annotationStringParameter; +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.getAnnotationByName; + +/** + * @author Pavel Talanov + */ +public class AnnotatedDeclarationVisitor extends AbstractDeclarationVisitor { + + @NotNull + private final String classAnnotationFQName; + + @NotNull + private final String funAnnotationFQName; + + /*package*/ AnnotatedDeclarationVisitor(@NotNull Declarations declarations, + @NotNull String classAnnotationFQName, + @NotNull String funAnnotationFQName) { + super(declarations); + this.classAnnotationFQName = classAnnotationFQName; + this.funAnnotationFQName = funAnnotationFQName; + } + + @NotNull + @Override + protected NamingScope doDeclareScope(@NotNull DeclarationDescriptor descriptor, @NotNull DeclarationContext context, + @NotNull String recommendedName) { + //TODO: probably need to keep track + if (!(descriptor instanceof ClassDescriptor)) { + return context.getScope(); + } + NamingScope innerScope = context.getScope().innerScope(recommendedName); + declarations().putScope(descriptor, innerScope); + return innerScope; + } + + @NotNull + @Override + protected JsName doDeclareName(@NotNull DeclarationDescriptor descriptor, @NotNull DeclarationContext context, + @NotNull String recommendedName) { + String nativeName = getName(descriptor); + JsName jsName = context.getScope(). + declareVariable(descriptor, nativeName, false); + jsName.setObfuscatable(false); + declarations().putName(descriptor, jsName); + declarations().putQualifier(descriptor, context.getQualifier()); + return jsName; + } + + @Override + protected boolean accept(@NotNull DeclarationDescriptor descriptor) { + if (descriptor instanceof NamespaceDescriptor) { + return true; + } + if (descriptor instanceof FunctionDescriptor) { + return isAnnotatedFunction((FunctionDescriptor) descriptor); + } + if (descriptor instanceof ClassDescriptor) { + return isAnnotatedClass((ClassDescriptor) descriptor); + } + return false; + } + + @Override + public void traverseNamespace(@NotNull NamespaceDescriptor namespace, @NotNull DeclarationContext context) { + declareMembers(namespace, context); + } + + + @NotNull + public String getName(@NotNull DeclarationDescriptor descriptor) { + if (descriptor instanceof FunctionDescriptor) { + return getNameForFunction((FunctionDescriptor) descriptor); + } + if (descriptor instanceof ClassDescriptor) { + return getNameForClass((ClassDescriptor) descriptor); + } + throw new AssertionError(); + } + + @NotNull + private String getNameForFunction(@NotNull FunctionDescriptor descriptor) { + if (hasFunctionAnnotation(descriptor)) { + return annotationStringParameter(descriptor, funAnnotationFQName); + } + if (declaredInAnnotatedClass(descriptor)) { + return descriptor.getName(); + } + throw new AssertionError("Use isAnnotatedFunction to check"); + } + + @NotNull + private String getNameForClass(@NotNull ClassDescriptor descriptor) { + if (hasClassAnnotation(descriptor)) { + return descriptor.getName(); + } + throw new AssertionError("Use isAnnotatedClass to check"); + } + + public boolean isAnnotatedDeclaration(@NotNull DeclarationDescriptor descriptor) { + return hasClassAnnotation(descriptor) || hasFunctionAnnotation(descriptor) + || declaredInAnnotatedClass(descriptor); + } + + public boolean isAnnotatedFunction(@NotNull FunctionDescriptor functionDescriptor) { + return hasFunctionAnnotation(functionDescriptor) || declaredInAnnotatedClass(functionDescriptor); + } + + public boolean isAnnotatedClass(@NotNull ClassDescriptor classDescriptor) { + return hasClassAnnotation(classDescriptor); + } + + private boolean declaredInAnnotatedClass(@NotNull DeclarationDescriptor descriptor) { + DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); + if (!(containingDeclaration instanceof ClassDescriptor)) return false; + return hasClassAnnotation(containingDeclaration); + } + + + private boolean hasFunctionAnnotation(@NotNull DeclarationDescriptor descriptor) { + return (getAnnotationByName(descriptor, funAnnotationFQName) != null); + } + + private boolean hasClassAnnotation(@NotNull DeclarationDescriptor descriptor) { + return (getAnnotationByName(descriptor, classAnnotationFQName) != null); + } + + +} diff --git a/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotationsUtils.java b/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotationsUtils.java index 3db71a0252c..f92fec84269 100644 --- a/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotationsUtils.java +++ b/translator/src/org/jetbrains/k2js/translate/context/declaration/AnnotationsUtils.java @@ -1,88 +1,55 @@ package org.jetbrains.k2js.translate.context.declaration; +import com.google.common.collect.Sets; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.resolve.DescriptorUtils; +import java.util.Set; + /** * @author Pavel Talanov */ public final class AnnotationsUtils { @NotNull - private static final String NATIVE_FUNCTION_ANNOTATION_FQNAME = "js.annotations.NativeFun"; + public static final String NATIVE_FUNCTION_ANNOTATION_FQNAME = "js.annotations.NativeFun"; @NotNull - private static final String NATIVE_CLASS_ANNOTATION_FQNAME = "js.annotations.NativeClass"; + public static final String NATIVE_CLASS_ANNOTATION_FQNAME = "js.annotations.NativeClass"; + @NotNull + public static final String LIBRARY_FUNCTION_ANNOTATION_FQNAME = "js.annotations.NativeFun"; + @NotNull + public static final String LIBRARY_CLASS_ANNOTATION_FQNAME = "js.annotations.NativeClass"; + @NotNull + public static Set INTERNAL_ANNOTATIONS_FQNAMES = Sets.newHashSet( + NATIVE_CLASS_ANNOTATION_FQNAME, NATIVE_FUNCTION_ANNOTATION_FQNAME, + LIBRARY_CLASS_ANNOTATION_FQNAME, LIBRARY_FUNCTION_ANNOTATION_FQNAME); private AnnotationsUtils() { } - @NotNull - public static String getNativeName(@NotNull DeclarationDescriptor descriptor) { - if (descriptor instanceof FunctionDescriptor) { - return getNativeNameForFunction((FunctionDescriptor) descriptor); + public static boolean doesNotHaveInternalAnnotations(@NotNull DeclarationDescriptor descriptor) { + for (String annotationFQNAme : INTERNAL_ANNOTATIONS_FQNAMES) { + if (hasAnnotation(descriptor, annotationFQNAme)) { + return false; + } } - if (descriptor instanceof ClassDescriptor) { - return getNativeNameForClass((ClassDescriptor) descriptor); - } - throw new AssertionError("Use isNativeDeclaration to check"); + return true; + } + + private static boolean hasAnnotation(@NotNull DeclarationDescriptor descriptor, + @NotNull String annotationFQNAme) { + return getAnnotationByName(descriptor, annotationFQNAme) != null; } @NotNull - private static String getNativeNameForFunction(@NotNull FunctionDescriptor descriptor) { - if (hasNativeFunctionAnnotation(descriptor)) { - return getNativeFunctionName(descriptor); - } - if (declaredInNativeClass(descriptor)) { - return descriptor.getName(); - } - throw new AssertionError("Use isNativeFunction to check"); - } - - @NotNull - private static String getNativeNameForClass(@NotNull ClassDescriptor descriptor) { - if (hasNativeClassAnnotation(descriptor)) { - return descriptor.getName(); - } - throw new AssertionError("Use isNativeClass to check"); - } - - public static boolean isNativeDeclaration(@NotNull DeclarationDescriptor descriptor) { - return hasNativeClassAnnotation(descriptor) || hasNativeFunctionAnnotation(descriptor) - || declaredInNativeClass(descriptor); - } - - public static boolean isNativeFunction(@NotNull FunctionDescriptor functionDescriptor) { - return hasNativeFunctionAnnotation(functionDescriptor) || declaredInNativeClass(functionDescriptor); - } - - public static boolean isNativeClass(@NotNull ClassDescriptor classDescriptor) { - return hasNativeClassAnnotation(classDescriptor); - } - - private static boolean declaredInNativeClass(@NotNull DeclarationDescriptor descriptor) { - DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); - if (!(containingDeclaration instanceof ClassDescriptor)) return false; - return hasNativeClassAnnotation(containingDeclaration); - } - - //TODO: constants - private static boolean hasNativeFunctionAnnotation(@NotNull DeclarationDescriptor descriptor) { - return (getAnnotationByName(descriptor, NATIVE_FUNCTION_ANNOTATION_FQNAME) != null); - } - - private static boolean hasNativeClassAnnotation(@NotNull DeclarationDescriptor descriptor) { - return (getAnnotationByName(descriptor, NATIVE_CLASS_ANNOTATION_FQNAME) != null); - } - - @NotNull - private static String getNativeFunctionName(@NotNull FunctionDescriptor declarationDescriptor) { + public static String annotationStringParameter(@NotNull FunctionDescriptor declarationDescriptor, + @NotNull String annotationFQName) { AnnotationDescriptor annotationDescriptor = - getAnnotationByName(declarationDescriptor, NATIVE_FUNCTION_ANNOTATION_FQNAME); + getAnnotationByName(declarationDescriptor, annotationFQName); assert annotationDescriptor != null; Object value = annotationDescriptor.getValueArguments().iterator().next().getValue(); assert value instanceof String : "Native function annotation should have one String parameter"; @@ -90,8 +57,8 @@ public final class AnnotationsUtils { } @Nullable - private static AnnotationDescriptor getAnnotationByName(@NotNull DeclarationDescriptor descriptor, - @NotNull String FQName) { + public static AnnotationDescriptor getAnnotationByName(@NotNull DeclarationDescriptor descriptor, + @NotNull String FQName) { for (AnnotationDescriptor annotationDescriptor : descriptor.getAnnotations()) { String annotationClassFQName = getAnnotationClassFQName(annotationDescriptor); if (annotationClassFQName.equals(FQName)) { diff --git a/translator/src/org/jetbrains/k2js/translate/context/declaration/KotlinDeclarationVisitor.java b/translator/src/org/jetbrains/k2js/translate/context/declaration/KotlinDeclarationVisitor.java index 1aa74e0e79c..f73c8bf3d36 100644 --- a/translator/src/org/jetbrains/k2js/translate/context/declaration/KotlinDeclarationVisitor.java +++ b/translator/src/org/jetbrains/k2js/translate/context/declaration/KotlinDeclarationVisitor.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.k2js.translate.context.NamingScope; -import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.isNativeDeclaration; +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.doesNotHaveInternalAnnotations; /** * @author Pavel Talanov @@ -41,6 +41,6 @@ public final class KotlinDeclarationVisitor extends AbstractDeclarationVisitor { @Override protected boolean accept(@NotNull DeclarationDescriptor descriptor) { - return (!isNativeDeclaration(descriptor)); + return (doesNotHaveInternalAnnotations(descriptor)); } } diff --git a/translator/src/org/jetbrains/k2js/translate/context/declaration/LibraryDeclarationVisitor.java b/translator/src/org/jetbrains/k2js/translate/context/declaration/LibraryDeclarationVisitor.java new file mode 100644 index 00000000000..1dfc2f0e3a9 --- /dev/null +++ b/translator/src/org/jetbrains/k2js/translate/context/declaration/LibraryDeclarationVisitor.java @@ -0,0 +1,16 @@ +package org.jetbrains.k2js.translate.context.declaration; + +import org.jetbrains.annotations.NotNull; + +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.LIBRARY_CLASS_ANNOTATION_FQNAME; +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.LIBRARY_FUNCTION_ANNOTATION_FQNAME; + +/** + * @author Pavel Talanov + */ +public final class LibraryDeclarationVisitor extends AnnotatedDeclarationVisitor { + + /*package*/ LibraryDeclarationVisitor(@NotNull Declarations nativeDeclarations) { + super(nativeDeclarations, LIBRARY_CLASS_ANNOTATION_FQNAME, LIBRARY_FUNCTION_ANNOTATION_FQNAME); + } +} diff --git a/translator/src/org/jetbrains/k2js/translate/context/declaration/NativeDeclarationVisitor.java b/translator/src/org/jetbrains/k2js/translate/context/declaration/NativeDeclarationVisitor.java index 4302260c8e4..6e95a67aab7 100644 --- a/translator/src/org/jetbrains/k2js/translate/context/declaration/NativeDeclarationVisitor.java +++ b/translator/src/org/jetbrains/k2js/translate/context/declaration/NativeDeclarationVisitor.java @@ -1,69 +1,16 @@ package org.jetbrains.k2js.translate.context.declaration; -import com.google.dart.compiler.backend.js.ast.JsName; import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; -import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; -import org.jetbrains.k2js.translate.context.NamingScope; -import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.isNativeClass; -import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.isNativeFunction; +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.NATIVE_CLASS_ANNOTATION_FQNAME; +import static org.jetbrains.k2js.translate.context.declaration.AnnotationsUtils.NATIVE_FUNCTION_ANNOTATION_FQNAME; /** * @author Pavel Talanov */ -public final class NativeDeclarationVisitor extends AbstractDeclarationVisitor { +public final class NativeDeclarationVisitor extends AnnotatedDeclarationVisitor { /*package*/ NativeDeclarationVisitor(@NotNull Declarations nativeDeclarations) { - super(nativeDeclarations); + super(nativeDeclarations, NATIVE_CLASS_ANNOTATION_FQNAME, NATIVE_FUNCTION_ANNOTATION_FQNAME); } - - @NotNull - @Override - protected NamingScope doDeclareScope(@NotNull DeclarationDescriptor descriptor, @NotNull DeclarationContext context, - @NotNull String recommendedName) { - //TODO: probably need to keep track - if (!(descriptor instanceof ClassDescriptor)) { - return context.getScope(); - } - NamingScope innerScope = context.getScope().innerScope(recommendedName); - declarations().putScope(descriptor, innerScope); - return innerScope; - } - - @NotNull - @Override - protected JsName doDeclareName(@NotNull DeclarationDescriptor descriptor, @NotNull DeclarationContext context, - @NotNull String recommendedName) { - String nativeName = AnnotationsUtils.getNativeName(descriptor); - JsName jsName = context.getScope(). - declareVariable(descriptor, nativeName, false); - jsName.setObfuscatable(false); - declarations().putName(descriptor, jsName); - declarations().putQualifier(descriptor, context.getQualifier()); - return jsName; - } - - @Override - protected boolean accept(@NotNull DeclarationDescriptor descriptor) { - if (descriptor instanceof NamespaceDescriptor) { - return true; - } - if (descriptor instanceof FunctionDescriptor) { - return isNativeFunction((FunctionDescriptor) descriptor); - } - if (descriptor instanceof ClassDescriptor) { - return isNativeClass((ClassDescriptor) descriptor); - } - return false; - } - - @Override - public void traverseNamespace(@NotNull NamespaceDescriptor namespace, @NotNull DeclarationContext context) { - declareMembers(namespace, context); - } - - }