001 /*
002 * Copyright 2010-2015 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.kotlin.js.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.kotlin.builtins.KotlinBuiltIns;
025 import org.jetbrains.kotlin.builtins.PrimitiveType;
026 import org.jetbrains.kotlin.js.patterns.DescriptorPredicate;
027 import org.jetbrains.kotlin.js.patterns.NamePredicate;
028 import org.jetbrains.kotlin.js.translate.context.Namer;
029 import org.jetbrains.kotlin.js.translate.context.TranslationContext;
030 import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsic;
031 import org.jetbrains.kotlin.name.Name;
032
033 import java.util.Arrays;
034 import java.util.List;
035
036 import static com.intellij.openapi.util.text.StringUtil.decapitalize;
037 import static org.jetbrains.kotlin.js.patterns.PatternBuilder.pattern;
038 import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.assignment;
039
040 public final class ArrayFIF extends CompositeFIF {
041 private static final NamePredicate NUMBER_ARRAY;
042 private static final NamePredicate CHAR_ARRAY;
043 private static final NamePredicate BOOLEAN_ARRAY;
044 private static final NamePredicate LONG_ARRAY;
045 private static final NamePredicate ARRAY;
046 private static final NamePredicate ARRAYS;
047 private static final DescriptorPredicate ARRAY_FACTORY_METHODS;
048
049 static {
050 List<Name> arrayTypeNames = Lists.newArrayList();
051 List<Name> arrayFactoryMethodNames = Lists.newArrayList(Name.identifier("arrayOf"));
052 for (PrimitiveType type : PrimitiveType.values()) {
053 Name arrayTypeName = type.getArrayTypeName();
054 if (type != PrimitiveType.CHAR && type != PrimitiveType.BOOLEAN && type != PrimitiveType.LONG) {
055 arrayTypeNames.add(arrayTypeName);
056 }
057 arrayFactoryMethodNames.add(Name.identifier(decapitalize(arrayTypeName.asString() + "Of")));
058 }
059
060 Name arrayName = KotlinBuiltIns.FQ_NAMES.array.shortName();
061 Name booleanArrayName = PrimitiveType.BOOLEAN.getArrayTypeName();
062 Name charArrayName = PrimitiveType.CHAR.getArrayTypeName();
063 Name longArrayName = PrimitiveType.LONG.getArrayTypeName();
064
065 NUMBER_ARRAY = new NamePredicate(arrayTypeNames);
066 CHAR_ARRAY = new NamePredicate(charArrayName);
067 BOOLEAN_ARRAY = new NamePredicate(booleanArrayName);
068 LONG_ARRAY = new NamePredicate(longArrayName);
069 ARRAY = new NamePredicate(arrayName);
070
071 arrayTypeNames.add(charArrayName);
072 arrayTypeNames.add(booleanArrayName);
073 arrayTypeNames.add(longArrayName);
074 arrayTypeNames.add(arrayName);
075 ARRAYS = new NamePredicate(arrayTypeNames);
076 ARRAY_FACTORY_METHODS = pattern(Namer.KOTLIN_LOWER_NAME, new NamePredicate(arrayFactoryMethodNames));
077 }
078
079 private static final FunctionIntrinsic ARRAY_INTRINSIC = new FunctionIntrinsic() {
080 @NotNull
081 @Override
082 public JsExpression apply(
083 @Nullable JsExpression receiver,
084 @NotNull List<JsExpression> arguments,
085 @NotNull TranslationContext context
086 ) {
087 assert arguments.size() == 1;
088 return arguments.get(0);
089 }
090 };
091
092 @NotNull
093 public static final FunctionIntrinsic GET_INTRINSIC = new FunctionIntrinsic() {
094 @NotNull
095 @Override
096 public JsExpression apply(@Nullable JsExpression receiver,
097 @NotNull List<JsExpression> arguments,
098 @NotNull TranslationContext context) {
099 assert receiver != null;
100 assert arguments.size() == 1 : "Array get expression must have one argument.";
101 JsExpression indexExpression = arguments.get(0);
102 return new JsArrayAccess(receiver, indexExpression);
103 }
104 };
105
106 @NotNull
107 public static final FunctionIntrinsic SET_INTRINSIC = new FunctionIntrinsic() {
108 @NotNull
109 @Override
110 public JsExpression apply(@Nullable JsExpression receiver,
111 @NotNull List<JsExpression> arguments,
112 @NotNull TranslationContext context) {
113 assert receiver != null;
114 assert arguments.size() == 2 : "Array set expression must have two arguments.";
115 JsExpression indexExpression = arguments.get(0);
116 JsExpression value = arguments.get(1);
117 JsArrayAccess arrayAccess = new JsArrayAccess(receiver, indexExpression);
118 return assignment(arrayAccess, value);
119 }
120 };
121
122 @NotNull
123 public static final FunctionIntrinsicFactory INSTANCE = new ArrayFIF();
124
125 private ArrayFIF() {
126 add(pattern(ARRAYS, "get"), GET_INTRINSIC);
127 add(pattern(ARRAYS, "set"), SET_INTRINSIC);
128 add(pattern(ARRAYS, "<get-size>"), LENGTH_PROPERTY_INTRINSIC);
129 add(pattern(ARRAYS, "iterator"), new KotlinFunctionIntrinsic("arrayIterator"));
130
131 add(pattern(NUMBER_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("numberArrayOfSize"));
132 add(pattern(CHAR_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("charArrayOfSize"));
133 add(pattern(BOOLEAN_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("booleanArrayOfSize"));
134 add(pattern(LONG_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("longArrayOfSize"));
135
136 add(pattern(ARRAYS, "<init>(Int,Function1)"), new KotlinFunctionIntrinsic("arrayFromFun"));
137
138 add(ARRAY_FACTORY_METHODS, ARRAY_INTRINSIC);
139 }
140 }