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;
018
019 import com.google.common.collect.Lists;
020 import gnu.trove.THashMap;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
024 import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsic;
025 import org.jetbrains.kotlin.js.translate.intrinsic.functions.factories.*;
026
027 import java.util.List;
028 import java.util.Map;
029
030 public final class FunctionIntrinsics {
031
032 @NotNull
033 private final Map<FunctionDescriptor, FunctionIntrinsic> intrinsicCache = new THashMap<FunctionDescriptor, FunctionIntrinsic>();
034
035 @NotNull
036 private final List<FunctionIntrinsicFactory> factories = Lists.newArrayList();
037
038 public FunctionIntrinsics() {
039 registerFactories();
040 }
041
042 private void registerFactories() {
043 register(LongOperationFIF.INSTANCE);
044 register(PrimitiveUnaryOperationFIF.INSTANCE);
045 register(PrimitiveBinaryOperationFIF.INSTANCE);
046 register(StringOperationFIF.INSTANCE);
047 register(ArrayFIF.INSTANCE);
048 register(TopLevelFIF.INSTANCE);
049 register(NumberAndCharConversionFIF.INSTANCE);
050 register(ProgressionCompanionFIF.INSTANCE);
051 }
052
053 private void register(@NotNull FunctionIntrinsicFactory instance) {
054 factories.add(instance);
055 }
056
057 @NotNull
058 public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
059 FunctionIntrinsic intrinsic = lookUpCache(descriptor);
060 if (intrinsic != null) {
061 return intrinsic;
062 }
063 intrinsic = computeAndCacheIntrinsic(descriptor);
064 return intrinsic;
065 }
066
067 @Nullable
068 private FunctionIntrinsic lookUpCache(@NotNull FunctionDescriptor descriptor) {
069 return intrinsicCache.get(descriptor);
070 }
071
072 @NotNull
073 private FunctionIntrinsic computeAndCacheIntrinsic(@NotNull FunctionDescriptor descriptor) {
074 FunctionIntrinsic result = computeIntrinsic(descriptor);
075 intrinsicCache.put(descriptor, result);
076 return result;
077 }
078
079 @NotNull
080 private FunctionIntrinsic computeIntrinsic(@NotNull FunctionDescriptor descriptor) {
081 for (FunctionIntrinsicFactory factory : factories) {
082 FunctionIntrinsic intrinsic = factory.getIntrinsic(descriptor);
083 if (intrinsic != null) {
084 return intrinsic;
085 }
086 }
087 return FunctionIntrinsic.NO_INTRINSIC;
088 }
089 }