001 /*
002 * Copyright 2010-2013 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.jet.lang.resolve.calls.autocasts;
018
019 import com.intellij.openapi.util.Pair;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.JetNodeTypes;
023 import org.jetbrains.jet.lang.descriptors.*;
024 import org.jetbrains.jet.lang.psi.*;
025 import org.jetbrains.jet.lang.resolve.BindingContext;
026 import org.jetbrains.jet.lang.resolve.JetModuleUtil;
027 import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
028 import org.jetbrains.jet.lang.resolve.scopes.receivers.*;
029 import org.jetbrains.jet.lang.types.JetType;
030 import org.jetbrains.jet.lang.types.TypeUtils;
031 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
032
033 import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET;
034 import static org.jetbrains.jet.lang.resolve.BindingContext.RESOLVED_CALL;
035
036 public class DataFlowValueFactory {
037 private DataFlowValueFactory() {}
038
039 @NotNull
040 public static DataFlowValue createDataFlowValue(
041 @NotNull JetExpression expression,
042 @NotNull JetType type,
043 @NotNull BindingContext bindingContext
044 ) {
045 if (expression instanceof JetConstantExpression) {
046 JetConstantExpression constantExpression = (JetConstantExpression) expression;
047 if (constantExpression.getNode().getElementType() == JetNodeTypes.NULL) return DataFlowValue.NULL;
048 }
049 if (TypeUtils.equalTypes(type, KotlinBuiltIns.getInstance().getNullableNothingType())) return DataFlowValue.NULL; // 'null' is the only inhabitant of 'Nothing?'
050 IdentifierInfo result = getIdForStableIdentifier(expression, bindingContext);
051 return new DataFlowValue(result == NO_IDENTIFIER_INFO ? expression : result.id, type, result.isStable, getImmanentNullability(type));
052 }
053
054 @NotNull
055 public static DataFlowValue createDataFlowValue(@NotNull ThisReceiver receiver) {
056 JetType type = receiver.getType();
057 return new DataFlowValue(receiver, type, true, getImmanentNullability(type));
058 }
059
060 @NotNull
061 public static DataFlowValue createDataFlowValue(@NotNull ReceiverValue receiverValue, @NotNull BindingContext bindingContext) {
062 if (receiverValue instanceof TransientReceiver || receiverValue instanceof ScriptReceiver) {
063 JetType type = receiverValue.getType();
064 boolean nullable = type.isNullable() || TypeUtils.hasNullableSuperType(type);
065 return new DataFlowValue(receiverValue, type, nullable, Nullability.NOT_NULL);
066 }
067 else if (receiverValue instanceof ClassReceiver || receiverValue instanceof ExtensionReceiver) {
068 return createDataFlowValue((ThisReceiver) receiverValue);
069 }
070 else if (receiverValue instanceof ExpressionReceiver) {
071 return createDataFlowValue(((ExpressionReceiver) receiverValue).getExpression(), receiverValue.getType(), bindingContext);
072 }
073 else if (receiverValue == ReceiverValue.NO_RECEIVER) {
074 throw new IllegalArgumentException("No DataFlowValue exists for ReceiverValue.NO_RECEIVER");
075 }
076 else {
077 throw new UnsupportedOperationException("Unsupported receiver value: " + receiverValue.getClass().getName());
078 }
079 }
080
081 @NotNull
082 private static Nullability getImmanentNullability(@NotNull JetType type) {
083 return type.isNullable() || TypeUtils.hasNullableSuperType(type) ? Nullability.UNKNOWN : Nullability.NOT_NULL;
084 }
085
086 private static class IdentifierInfo {
087 public final Object id;
088 public final boolean isStable;
089 public final boolean isPackage;
090
091 private IdentifierInfo(Object id, boolean isStable, boolean isPackage) {
092 this.id = id;
093 this.isStable = isStable;
094 this.isPackage = isPackage;
095 }
096 }
097
098 private static final IdentifierInfo NO_IDENTIFIER_INFO = new IdentifierInfo(null, false, false) {
099 @Override
100 public String toString() {
101 return "NO_IDENTIFIER_INFO";
102 }
103 };
104
105 @NotNull
106 private static IdentifierInfo createInfo(Object id, boolean isStable) {
107 return new IdentifierInfo(id, isStable, false);
108 }
109
110 @NotNull
111 private static IdentifierInfo createPackageInfo(Object id) {
112 return new IdentifierInfo(id, true, true);
113 }
114
115 @NotNull
116 private static IdentifierInfo combineInfo(@Nullable IdentifierInfo receiverInfo, @NotNull IdentifierInfo selectorInfo) {
117 if (selectorInfo.id == null) {
118 return NO_IDENTIFIER_INFO;
119 }
120 if (receiverInfo == null || receiverInfo == NO_IDENTIFIER_INFO || receiverInfo.isPackage) {
121 return selectorInfo;
122 }
123 return createInfo(Pair.create(receiverInfo.id, selectorInfo.id), receiverInfo.isStable && selectorInfo.isStable);
124 }
125
126 @NotNull
127 private static IdentifierInfo getIdForStableIdentifier(
128 @Nullable JetExpression expression,
129 @NotNull BindingContext bindingContext
130 ) {
131 if (expression != null) {
132 JetExpression deparenthesized = JetPsiUtil.deparenthesize(expression);
133 if (expression != deparenthesized) {
134 return getIdForStableIdentifier(deparenthesized, bindingContext);
135 }
136 }
137 if (expression instanceof JetQualifiedExpression) {
138 JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) expression;
139 JetExpression receiverExpression = qualifiedExpression.getReceiverExpression();
140 JetExpression selectorExpression = qualifiedExpression.getSelectorExpression();
141 IdentifierInfo receiverId = getIdForStableIdentifier(receiverExpression, bindingContext);
142 IdentifierInfo selectorId = getIdForStableIdentifier(selectorExpression, bindingContext);
143
144 return combineInfo(receiverId, selectorId);
145 }
146 if (expression instanceof JetSimpleNameExpression) {
147 return getIdForSimpleNameExpression((JetSimpleNameExpression) expression, bindingContext);
148 }
149 else if (expression instanceof JetThisExpression) {
150 JetThisExpression thisExpression = (JetThisExpression) expression;
151 DeclarationDescriptor declarationDescriptor = bindingContext.get(REFERENCE_TARGET, thisExpression.getInstanceReference());
152
153 return getIdForThisReceiver(declarationDescriptor);
154 }
155 else if (expression instanceof JetRootPackageExpression) {
156 return createPackageInfo(JetModuleUtil.getRootPackageType(expression));
157 }
158 return NO_IDENTIFIER_INFO;
159 }
160
161 @NotNull
162 private static IdentifierInfo getIdForSimpleNameExpression(
163 @NotNull JetSimpleNameExpression simpleNameExpression,
164 @NotNull BindingContext bindingContext
165 ) {
166 DeclarationDescriptor declarationDescriptor = bindingContext.get(REFERENCE_TARGET, simpleNameExpression);
167 if (declarationDescriptor instanceof VariableDescriptor) {
168 ResolvedCall<?> resolvedCall = bindingContext.get(RESOLVED_CALL, simpleNameExpression);
169 // todo uncomment assert
170 // KT-4113
171 // for now it fails for resolving 'invoke' convention, return it after 'invoke' algorithm changes
172 // assert resolvedCall != null : "Cannot create right identifier info if the resolved call is not known yet for " + declarationDescriptor;
173
174 IdentifierInfo receiverInfo = resolvedCall != null ? getIdForImplicitReceiver(resolvedCall.getThisObject(), simpleNameExpression) : null;
175
176 VariableDescriptor variableDescriptor = (VariableDescriptor) declarationDescriptor;
177 return combineInfo(receiverInfo, createInfo(variableDescriptor, isStableVariable(variableDescriptor)));
178 }
179 if (declarationDescriptor instanceof PackageViewDescriptor) {
180 return createPackageInfo(declarationDescriptor);
181 }
182 return NO_IDENTIFIER_INFO;
183 }
184
185 @Nullable
186 private static IdentifierInfo getIdForImplicitReceiver(@NotNull ReceiverValue receiverValue, @Nullable JetExpression expression) {
187 if (receiverValue instanceof ThisReceiver) {
188 return getIdForThisReceiver(((ThisReceiver) receiverValue).getDeclarationDescriptor());
189 }
190 else {
191 assert !(receiverValue instanceof TransientReceiver)
192 : "Transient receiver is implicit for an explicit expression: " + expression + ". Receiver: " + receiverValue;
193 // For ExpressionReceiver there is an explicit "this" expression and it was analyzed earlier
194 return null;
195 }
196 }
197
198 @NotNull
199 private static IdentifierInfo getIdForThisReceiver(@Nullable DeclarationDescriptor descriptorOfThisReceiver) {
200 if (descriptorOfThisReceiver instanceof CallableDescriptor) {
201 ReceiverParameterDescriptor receiverParameter = ((CallableDescriptor) descriptorOfThisReceiver).getReceiverParameter();
202 assert receiverParameter != null : "'This' refers to the callable member without a receiver parameter: " + descriptorOfThisReceiver;
203 return createInfo(receiverParameter.getValue(), true);
204 }
205 if (descriptorOfThisReceiver instanceof ClassDescriptor) {
206 return createInfo(((ClassDescriptor) descriptorOfThisReceiver).getThisAsReceiverParameter().getValue(), true);
207 }
208 return NO_IDENTIFIER_INFO;
209 }
210
211 public static boolean isStableVariable(@NotNull VariableDescriptor variableDescriptor) {
212 if (variableDescriptor.isVar()) return false;
213 if (variableDescriptor instanceof PropertyDescriptor) {
214 PropertyDescriptor propertyDescriptor = (PropertyDescriptor) variableDescriptor;
215 if (!invisibleFromOtherModules(propertyDescriptor)) return false;
216 if (!isFinal(propertyDescriptor)) return false;
217 if (!hasDefaultGetter(propertyDescriptor)) return false;
218 }
219 return true;
220 }
221
222 private static boolean isFinal(PropertyDescriptor propertyDescriptor) {
223 DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
224 if (containingDeclaration instanceof ClassDescriptor) {
225 ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
226 if (classDescriptor.getModality().isOverridable() && propertyDescriptor.getModality().isOverridable()) return false;
227 }
228 else {
229 if (propertyDescriptor.getModality().isOverridable()) {
230 throw new IllegalStateException("Property outside a class must not be overridable: " + propertyDescriptor.getName());
231 }
232 }
233 return true;
234 }
235
236 private static boolean invisibleFromOtherModules(@NotNull DeclarationDescriptorWithVisibility descriptor) {
237 if (Visibilities.INVISIBLE_FROM_OTHER_MODULES.contains(descriptor.getVisibility())) return true;
238
239 DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
240 if (!(containingDeclaration instanceof DeclarationDescriptorWithVisibility)) {
241 return false;
242 }
243
244 return invisibleFromOtherModules((DeclarationDescriptorWithVisibility) containingDeclaration);
245 }
246
247 private static boolean hasDefaultGetter(PropertyDescriptor propertyDescriptor) {
248 PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
249 return getter == null || getter.isDefault();
250 }
251 }