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.resolve.calls.model;
018    
019    import com.google.common.collect.Maps;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.psi.Call;
022    import org.jetbrains.kotlin.psi.ValueArgument;
023    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
024    
025    import java.util.Iterator;
026    import java.util.List;
027    import java.util.Map;
028    
029    public class DataFlowInfoForArgumentsImpl implements MutableDataFlowInfoForArguments {
030        private final Call call; //for better debug messages only
031        private final Map<ValueArgument, DataFlowInfo>  infoMap = Maps.newHashMap();
032        private final Map<ValueArgument, ValueArgument> nextArgument = Maps.newHashMap();
033        private DataFlowInfo initialInfo;
034        private DataFlowInfo resultInfo;
035    
036        public DataFlowInfoForArgumentsImpl(@NotNull Call call) {
037            this.call = call;
038            initNextArgMap(call.getValueArguments());
039        }
040    
041        private void initNextArgMap(@NotNull List<? extends ValueArgument> valueArguments) {
042            Iterator<? extends ValueArgument> iterator = valueArguments.iterator();
043            ValueArgument prev = null;
044            while (iterator.hasNext()) {
045                ValueArgument argument = iterator.next();
046                if (prev != null) {
047                    nextArgument.put(prev, argument);
048                }
049                prev = argument;
050            }
051        }
052    
053        @Override
054        public void setInitialDataFlowInfo(@NotNull DataFlowInfo dataFlowInfo) {
055            //TODO assert initialInfo == null
056            initialInfo = dataFlowInfo;
057        }
058    
059        @NotNull
060        @Override
061        public DataFlowInfo getInfo(@NotNull ValueArgument valueArgument) {
062            DataFlowInfo infoForArgument = infoMap.get(valueArgument);
063            if (infoForArgument == null) {
064                return initialInfo;
065            }
066            return initialInfo.and(infoForArgument);
067        }
068    
069        @Override
070        public void updateInfo(@NotNull ValueArgument valueArgument, @NotNull DataFlowInfo dataFlowInfo) {
071            ValueArgument next = nextArgument.get(valueArgument);
072            if (next != null) {
073                infoMap.put(next, dataFlowInfo);
074                return;
075            }
076            //TODO assert resultInfo == null
077            resultInfo = dataFlowInfo;
078        }
079    
080        @NotNull
081        @Override
082        public DataFlowInfo getResultInfo() {
083            if (resultInfo == null) return initialInfo;
084            assert initialInfo != null : "Initial data flow info was not set for call: " + call;
085            return initialInfo.and(resultInfo);
086        }
087    }