Refactoring duplication in array intrinsics.

This commit is contained in:
pTalanov
2012-02-28 18:30:14 +04:00
parent fe9bb67970
commit 6bbc0d2a6e
5 changed files with 39 additions and 156 deletions
@@ -26,7 +26,10 @@ import org.jetbrains.jet.lang.types.JetStandardLibrary;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.PrimitiveType; import org.jetbrains.jet.lang.types.PrimitiveType;
import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.k2js.translate.intrinsic.array.*; import org.jetbrains.k2js.translate.intrinsic.array.ArrayGetIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.array.ArraySetIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.array.ArraySizeIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.array.CallStandardMethodIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.primitive.*; import org.jetbrains.k2js.translate.intrinsic.primitive.*;
import org.jetbrains.k2js.translate.intrinsic.string.CharAtIntrinsic; import org.jetbrains.k2js.translate.intrinsic.string.CharAtIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.string.LengthIntrinsic; import org.jetbrains.k2js.translate.intrinsic.string.LengthIntrinsic;
@@ -94,9 +97,8 @@ public final class Intrinsics {
} }
private void declareNullConstructorIntrinsic() { private void declareNullConstructorIntrinsic() {
//TODO:
FunctionDescriptor nullArrayConstructor = library.getLibraryScope().getFunctions("Array").iterator().next(); FunctionDescriptor nullArrayConstructor = library.getLibraryScope().getFunctions("Array").iterator().next();
functionIntrinsics.put(nullArrayConstructor, ArrayNullConstructorIntrinsic.INSTANCE); functionIntrinsics.put(nullArrayConstructor, new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1));
} }
//TODO: some dangerous operation unchecked here //TODO: some dangerous operation unchecked here
@@ -109,12 +111,12 @@ public final class Intrinsics {
PropertyDescriptor sizeProperty = getPropertyByName(arrayMemberScope, "size"); PropertyDescriptor sizeProperty = getPropertyByName(arrayMemberScope, "size");
functionIntrinsics.put(sizeProperty.getGetter(), ArraySizeIntrinsic.INSTANCE); functionIntrinsics.put(sizeProperty.getGetter(), ArraySizeIntrinsic.INSTANCE);
PropertyDescriptor indicesProperty = getPropertyByName(arrayMemberScope, "indices"); PropertyDescriptor indicesProperty = getPropertyByName(arrayMemberScope, "indices");
functionIntrinsics.put(indicesProperty.getGetter(), ArrayIndicesIntrinsic.INSTANCE); functionIntrinsics.put(indicesProperty.getGetter(), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0));
FunctionDescriptor iteratorFunction = getFunctionByName(arrayMemberScope, "iterator"); FunctionDescriptor iteratorFunction = getFunctionByName(arrayMemberScope, "iterator");
functionIntrinsics.put(iteratorFunction, ArrayIteratorIntrinsic.INSTANCE); functionIntrinsics.put(iteratorFunction, new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0));
ConstructorDescriptor arrayConstructor = ConstructorDescriptor arrayConstructor =
((ClassDescriptor) arrayMemberScope.getContainingDeclaration()).getConstructors().iterator().next(); ((ClassDescriptor) arrayMemberScope.getContainingDeclaration()).getConstructors().iterator().next();
functionIntrinsics.put(arrayConstructor, ArrayFunctionConstructorIntrinsic.INSTANCE); functionIntrinsics.put(arrayConstructor, new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2));
} }
private List<JetType> getLibraryArrayTypes() { private List<JetType> getLibraryArrayTypes() {
@@ -1,47 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.translate.intrinsic.array;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
import java.util.List;
/**
* @author Pavel Talanov
*/
public enum ArrayIndicesIntrinsic implements FunctionIntrinsic {
INSTANCE;
@NotNull
@Override
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert receiver != null;
assert arguments.size() == 0;
//TODO: provide better mechanism
JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef("Kotlin.arrayIndices");
return AstUtil.newInvocation(iteratorFunName, receiver);
}
}
@@ -1,46 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.translate.intrinsic.array;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
import java.util.List;
/**
* @author Pavel Talanov
*/
public enum ArrayIteratorIntrinsic implements FunctionIntrinsic {
INSTANCE;
@NotNull
@Override
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert receiver != null;
assert arguments.size() == 0;
//TODO: provide better mechanism
JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef("Kotlin.arrayIterator");
return AstUtil.newInvocation(iteratorFunName, receiver);
}
}
@@ -1,49 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.translate.intrinsic.array;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation;
/**
* @author Pavel Talanov
*/
public enum ArrayNullConstructorIntrinsic implements FunctionIntrinsic {
INSTANCE;
//TODO: implement function passing to array constructor
@NotNull
@Override
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert receiver == null;
assert arguments.size() == 1;
//TODO: provide better mechanism
JsNameRef nullArrayFunName = AstUtil.newQualifiedNameRef("Kotlin.nullArray");
return newInvocation(nullArrayFunName, arguments);
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.k2js.translate.intrinsic.array; package org.jetbrains.k2js.translate.intrinsic.array;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsExpression; import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef; import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil; import com.google.dart.compiler.util.AstUtil;
@@ -31,20 +32,42 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
*/ */
public enum ArrayFunctionConstructorIntrinsic implements FunctionIntrinsic { public final class CallStandardMethodIntrinsic implements FunctionIntrinsic {
INSTANCE; @NotNull
private final String methodName;
private final boolean receiverShouldBeNotNull;
private final int expectedParamsNumber;
public CallStandardMethodIntrinsic(@NotNull String methodName, boolean receiverShouldBeNotNull, int expectedParamsNumber) {
this.methodName = methodName;
this.receiverShouldBeNotNull = receiverShouldBeNotNull;
this.expectedParamsNumber = expectedParamsNumber;
}
@NotNull @NotNull
@Override @Override
public JsExpression apply(@Nullable JsExpression receiver, public JsExpression apply(@Nullable JsExpression receiver,
@NotNull List<JsExpression> arguments, @NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
assert receiver == null; assert (receiver != null == receiverShouldBeNotNull);
assert arguments.size() == 2; assert arguments.size() == expectedParamsNumber;
//TODO: provide better mechanism List<JsExpression> args = composeArguments(receiver, arguments);
JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef("Kotlin.arrayFromFun"); JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef(methodName);
return newInvocation(iteratorFunName, arguments); return newInvocation(iteratorFunName, composeArguments(receiver, arguments));
} }
}
@NotNull
private static List<JsExpression> composeArguments(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments) {
if (receiver != null) {
List<JsExpression> args = Lists.newArrayList();
args.add(receiver);
args.addAll(arguments);
return args;
}
else {
return arguments;
}
}
}