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        }
051    
052        private void register(@NotNull FunctionIntrinsicFactory instance) {
053            factories.add(instance);
054        }
055    
056        @NotNull
057        public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
058            FunctionIntrinsic intrinsic = lookUpCache(descriptor);
059            if (intrinsic != null) {
060                return intrinsic;
061            }
062            intrinsic = computeAndCacheIntrinsic(descriptor);
063            return intrinsic;
064        }
065    
066        @Nullable
067        private FunctionIntrinsic lookUpCache(@NotNull FunctionDescriptor descriptor) {
068            return intrinsicCache.get(descriptor);
069        }
070    
071        @NotNull
072        private FunctionIntrinsic computeAndCacheIntrinsic(@NotNull FunctionDescriptor descriptor) {
073            FunctionIntrinsic result = computeIntrinsic(descriptor);
074            intrinsicCache.put(descriptor, result);
075            return result;
076        }
077    
078        @NotNull
079        private FunctionIntrinsic computeIntrinsic(@NotNull FunctionDescriptor descriptor) {
080            for (FunctionIntrinsicFactory factory : factories) {
081                FunctionIntrinsic intrinsic = factory.getIntrinsic(descriptor);
082                if (intrinsic != null) {
083                    return intrinsic;
084                }
085            }
086            return FunctionIntrinsic.NO_INTRINSIC;
087        }
088    }