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 LONG_ARRAY;
051
052 @NotNull
053 private static final NamePredicate ARRAY;
054
055 @NotNull
056 private static final NamePredicate ARRAYS;
057
058 @NotNull
059 private static final DescriptorPredicate ARRAY_FACTORY_METHODS;
060
061 static {
062 List<Name> arrayTypeNames = Lists.newArrayList();
063 List<Name> arrayFactoryMethodNames = Lists.newArrayList(Name.identifier("array"));
064 for (PrimitiveType type : PrimitiveType.values()) {
065 Name arrayTypeName = type.getArrayTypeName();
066 if (type != PrimitiveType.CHAR && type != PrimitiveType.BOOLEAN && type != PrimitiveType.LONG) {
067 arrayTypeNames.add(arrayTypeName);
068 }
069 arrayFactoryMethodNames.add(Name.identifier(decapitalize(arrayTypeName.asString())));
070 }
071
072 Name arrayName = Name.identifier("Array");
073 Name booleanArrayName = PrimitiveType.BOOLEAN.getArrayTypeName();
074 Name charArrayName = PrimitiveType.CHAR.getArrayTypeName();
075 Name longArrayName = PrimitiveType.LONG.getArrayTypeName();
076
077 NUMBER_ARRAY = new NamePredicate(arrayTypeNames);
078 CHAR_ARRAY = new NamePredicate(charArrayName);
079 BOOLEAN_ARRAY = new NamePredicate(booleanArrayName);
080 LONG_ARRAY = new NamePredicate(longArrayName);
081 ARRAY = new NamePredicate(arrayName);
082
083 arrayTypeNames.add(charArrayName);
084 arrayTypeNames.add(booleanArrayName);
085 arrayTypeNames.add(longArrayName);
086 arrayTypeNames.add(arrayName);
087 ARRAYS = new NamePredicate(arrayTypeNames);
088 ARRAY_FACTORY_METHODS = pattern(Namer.KOTLIN_LOWER_NAME, new NamePredicate(arrayFactoryMethodNames));
089 }
090
091 private static final FunctionIntrinsic ARRAY_INTRINSIC = new FunctionIntrinsic() {
092 @NotNull
093 @Override
094 public JsExpression apply(
095 @Nullable JsExpression receiver,
096 @NotNull List<JsExpression> arguments,
097 @NotNull TranslationContext context
098 ) {
099 assert arguments.size() == 1;
100 return arguments.get(0);
101 }
102 };
103
104 @NotNull
105 public static final FunctionIntrinsic GET_INTRINSIC = new FunctionIntrinsic() {
106 @NotNull
107 @Override
108 public JsExpression apply(@Nullable JsExpression receiver,
109 @NotNull List<JsExpression> arguments,
110 @NotNull TranslationContext context) {
111 assert receiver != null;
112 assert arguments.size() == 1 : "Array get expression must have one argument.";
113 JsExpression indexExpression = arguments.get(0);
114 return new JsArrayAccess(receiver, indexExpression);
115 }
116 };
117
118 @NotNull
119 public static final FunctionIntrinsic SET_INTRINSIC = new FunctionIntrinsic() {
120 @NotNull
121 @Override
122 public JsExpression apply(@Nullable JsExpression receiver,
123 @NotNull List<JsExpression> arguments,
124 @NotNull TranslationContext context) {
125 assert receiver != null;
126 assert arguments.size() == 2 : "Array set expression must have two arguments.";
127 JsExpression indexExpression = arguments.get(0);
128 JsExpression value = arguments.get(1);
129 JsArrayAccess arrayAccess = new JsArrayAccess(receiver, indexExpression);
130 return assignment(arrayAccess, value);
131 }
132 };
133
134 @NotNull
135 public static final FunctionIntrinsicFactory INSTANCE = new ArrayFIF();
136
137 private ArrayFIF() {
138 add(pattern(ARRAYS, "get"), GET_INTRINSIC);
139 add(pattern(ARRAYS, "set"), SET_INTRINSIC);
140 add(pattern(ARRAYS, "<get-size>"), LENGTH_PROPERTY_INTRINSIC);
141 add(pattern(ARRAYS, "<get-indices>"), new KotlinFunctionIntrinsic("arrayIndices"));
142 add(pattern(ARRAYS, "iterator"), new KotlinFunctionIntrinsic("arrayIterator"));
143 add(pattern(ARRAY, "<init>"), new KotlinFunctionIntrinsic("arrayFromFun"));
144 add(pattern(NUMBER_ARRAY, "<init>"),new KotlinFunctionIntrinsic("numberArrayOfSize"));
145 add(pattern(CHAR_ARRAY, "<init>"), new KotlinFunctionIntrinsic("charArrayOfSize"));
146 add(pattern(BOOLEAN_ARRAY, "<init>"), new KotlinFunctionIntrinsic("booleanArrayOfSize"));
147 add(pattern(LONG_ARRAY, "<init>"), new KotlinFunctionIntrinsic("longArrayOfSize"));
148 add(ARRAY_FACTORY_METHODS, ARRAY_INTRINSIC);
149 }
150 }