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 extends MutableDataFlowInfoForArguments {
031 @Nullable private Map<ValueArgument, DataFlowInfo> infoMap = null;
032 @Nullable private Map<ValueArgument, ValueArgument> nextArgument = null;
033 @Nullable private DataFlowInfo resultInfo;
034
035 public DataFlowInfoForArgumentsImpl(@NotNull DataFlowInfo initialInfo, @NotNull Call call) {
036 super(initialInfo);
037 initNextArgMap(call.getValueArguments());
038 }
039
040 private void initNextArgMap(@NotNull List<? extends ValueArgument> valueArguments) {
041 Iterator<? extends ValueArgument> iterator = valueArguments.iterator();
042 ValueArgument prev = null;
043 while (iterator.hasNext()) {
044 ValueArgument argument = iterator.next();
045 if (prev != null) {
046 if (nextArgument == null) {
047 nextArgument = new HashMap<ValueArgument, ValueArgument>();
048 }
049 nextArgument.put(prev, argument);
050 }
051 prev = argument;
052 }
053 }
054
055 @NotNull
056 @Override
057 public DataFlowInfo getInfo(@NotNull ValueArgument valueArgument) {
058 DataFlowInfo infoForArgument = infoMap == null ? null : infoMap.get(valueArgument);
059 if (infoForArgument == null) {
060 return initialDataFlowInfo;
061 }
062 return initialDataFlowInfo.and(infoForArgument);
063 }
064
065 @Override
066 public void updateInfo(@NotNull ValueArgument valueArgument, @NotNull DataFlowInfo dataFlowInfo) {
067 ValueArgument next = nextArgument == null ? null : nextArgument.get(valueArgument);
068 if (next != null) {
069 if (infoMap == null) {
070 infoMap = new HashMap<ValueArgument, DataFlowInfo>();
071 }
072 infoMap.put(next, dataFlowInfo);
073 return;
074 }
075 //TODO assert resultInfo == null
076 resultInfo = dataFlowInfo;
077 }
078
079 @NotNull
080 @Override
081 public DataFlowInfo getResultInfo() {
082 if (resultInfo == null) return initialDataFlowInfo;
083 return initialDataFlowInfo.and(resultInfo);
084 }
085 }