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 org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.psi.Call;
021    import org.jetbrains.kotlin.psi.ValueArgument;
022    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
023    
024    import java.util.HashMap;
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 Map<ValueArgument, DataFlowInfo> infoMap = null;
032        private Map<ValueArgument, ValueArgument> nextArgument = null;
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                    if (nextArgument == null) {
048                        nextArgument = new HashMap<ValueArgument, ValueArgument>();
049                    }
050                    nextArgument.put(prev, argument);
051                }
052                prev = argument;
053            }
054        }
055    
056        @Override
057        public void setInitialDataFlowInfo(@NotNull DataFlowInfo dataFlowInfo) {
058            //TODO assert initialInfo == null
059            initialInfo = dataFlowInfo;
060        }
061    
062        @NotNull
063        @Override
064        public DataFlowInfo getInfo(@NotNull ValueArgument valueArgument) {
065            DataFlowInfo infoForArgument = infoMap == null ? null : infoMap.get(valueArgument);
066            if (infoForArgument == null) {
067                return initialInfo;
068            }
069            return initialInfo.and(infoForArgument);
070        }
071    
072        @Override
073        public void updateInfo(@NotNull ValueArgument valueArgument, @NotNull DataFlowInfo dataFlowInfo) {
074            ValueArgument next = nextArgument == null ? null : nextArgument.get(valueArgument);
075            if (next != null) {
076                if (infoMap == null) {
077                    infoMap = new HashMap<ValueArgument, DataFlowInfo>();
078                }
079                infoMap.put(next, dataFlowInfo);
080                return;
081            }
082            //TODO assert resultInfo == null
083            resultInfo = dataFlowInfo;
084        }
085    
086        @NotNull
087        @Override
088        public DataFlowInfo getResultInfo() {
089            if (resultInfo == null) return initialInfo;
090            assert initialInfo != null : "Initial data flow info was not set for call: " + call;
091            return initialInfo.and(resultInfo);
092        }
093    }