001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.k2js.translate.intrinsic.functions.factories;
018
019 import com.google.common.collect.Lists;
020 import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
021 import com.google.dart.compiler.backend.js.ast.JsExpression;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.lang.resolve.name.Name;
025 import org.jetbrains.jet.lang.types.lang.PrimitiveType;
026 import org.jetbrains.k2js.translate.context.Namer;
027 import org.jetbrains.k2js.translate.context.TranslationContext;
028 import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
029 import org.jetbrains.k2js.translate.intrinsic.functions.patterns.DescriptorPredicate;
030 import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
031
032 import java.util.List;
033
034 import static com.intellij.openapi.util.text.StringUtil.decapitalize;
035 import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
036 import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
037
038 public final class ArrayFIF extends CompositeFIF {
039
040 @NotNull
041 private static final NamePredicate NUMBER_ARRAY;
042
043 @NotNull
044 private static final NamePredicate CHAR_ARRAY;
045
046 @NotNull
047 private static final NamePredicate BOOLEAN_ARRAY;
048
049 @NotNull
050 private static final NamePredicate ARRAY;
051
052 @NotNull
053 private static final NamePredicate ARRAYS;
054
055 @NotNull
056 private static final DescriptorPredicate ARRAY_FACTORY_METHODS;
057
058 static {
059 List<Name> arrayTypeNames = Lists.newArrayList();
060 List<Name> arrayFactoryMethodNames = Lists.newArrayList(Name.identifier("array"));
061 for (PrimitiveType type : PrimitiveType.values()) {
062 Name arrayTypeName = type.getArrayTypeName();
063 if (type != PrimitiveType.CHAR && type != PrimitiveType.BOOLEAN) {
064 arrayTypeNames.add(arrayTypeName);
065 }
066 arrayFactoryMethodNames.add(Name.identifier(decapitalize(arrayTypeName.asString())));
067 }
068
069 Name arrayName = Name.identifier("Array");
070 Name booleanArrayName = PrimitiveType.BOOLEAN.getArrayTypeName();
071 Name charArrayName = PrimitiveType.CHAR.getArrayTypeName();
072
073 NUMBER_ARRAY = new NamePredicate(arrayTypeNames);
074 CHAR_ARRAY = new NamePredicate(charArrayName);
075 BOOLEAN_ARRAY = new NamePredicate(booleanArrayName);
076 ARRAY = new NamePredicate(arrayName);
077
078 arrayTypeNames.add(charArrayName);
079 arrayTypeNames.add(booleanArrayName);
080 arrayTypeNames.add(arrayName);
081 ARRAYS = new NamePredicate(arrayTypeNames);
082 ARRAY_FACTORY_METHODS = pattern(Namer.KOTLIN_LOWER_NAME, new NamePredicate(arrayFactoryMethodNames));
083 }
084
085 private static final FunctionIntrinsic ARRAY_INTRINSIC = new FunctionIntrinsic() {
086 @NotNull
087 @Override
088 public JsExpression apply(
089 @Nullable JsExpression receiver,
090 @NotNull List<JsExpression> arguments,
091 @NotNull TranslationContext context
092 ) {
093 assert arguments.size() == 1;
094 return arguments.get(0);
095 }
096 };
097
098 @NotNull
099 public static final FunctionIntrinsic GET_INTRINSIC = new FunctionIntrinsic() {
100 @NotNull
101 @Override
102 public JsExpression apply(@Nullable JsExpression receiver,
103 @NotNull List<JsExpression> arguments,
104 @NotNull TranslationContext context) {
105 assert receiver != null;
106 assert arguments.size() == 1 : "Array get expression must have one argument.";
107 JsExpression indexExpression = arguments.get(0);
108 return new JsArrayAccess(receiver, indexExpression);
109 }
110 };
111
112 @NotNull
113 public static final FunctionIntrinsic SET_INTRINSIC = new FunctionIntrinsic() {
114 @NotNull
115 @Override
116 public JsExpression apply(@Nullable JsExpression receiver,
117 @NotNull List<JsExpression> arguments,
118 @NotNull TranslationContext context) {
119 assert receiver != null;
120 assert arguments.size() == 2 : "Array set expression must have two arguments.";
121 JsExpression indexExpression = arguments.get(0);
122 JsExpression value = arguments.get(1);
123 JsArrayAccess arrayAccess = new JsArrayAccess(receiver, indexExpression);
124 return assignment(arrayAccess, value);
125 }
126 };
127
128 @NotNull
129 public static final FunctionIntrinsicFactory INSTANCE = new ArrayFIF();
130
131 private ArrayFIF() {
132 add(pattern(ARRAYS, "get"), GET_INTRINSIC);
133 add(pattern(ARRAYS, "set"), SET_INTRINSIC);
134 add(pattern(ARRAYS, "<get-size>"), LENGTH_PROPERTY_INTRINSIC);
135 add(pattern(ARRAYS, "<get-indices>"), new KotlinFunctionIntrinsic("arrayIndices"));
136 add(pattern(ARRAYS, "iterator"), new KotlinFunctionIntrinsic("arrayIterator"));
137 add(pattern(ARRAY, "<init>"), new KotlinFunctionIntrinsic("arrayFromFun"));
138 add(pattern(NUMBER_ARRAY, "<init>"),new KotlinFunctionIntrinsic("numberArrayOfSize"));
139 add(pattern(CHAR_ARRAY, "<init>"), new KotlinFunctionIntrinsic("charArrayOfSize"));
140 add(pattern(BOOLEAN_ARRAY, "<init>"), new KotlinFunctionIntrinsic("booleanArrayOfSize"));
141 add(ARRAY_FACTORY_METHODS, ARRAY_INTRINSIC);
142 }
143 }