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