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;
018
019 import com.google.common.collect.Lists;
020 import com.google.common.collect.Maps;
021 import com.google.common.collect.Sets;
022 import com.intellij.psi.PsiElement;
023 import kotlin.Pair;
024 import kotlin.collections.CollectionsKt;
025 import kotlin.collections.SetsKt;
026 import kotlin.jvm.functions.Function0;
027 import kotlin.jvm.functions.Function1;
028 import org.jetbrains.annotations.NotNull;
029 import org.jetbrains.annotations.Nullable;
030 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
031 import org.jetbrains.kotlin.descriptors.*;
032 import org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter;
033 import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget;
034 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
035 import org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations;
036 import org.jetbrains.kotlin.descriptors.impl.*;
037 import org.jetbrains.kotlin.diagnostics.Errors;
038 import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
039 import org.jetbrains.kotlin.lexer.KtTokens;
040 import org.jetbrains.kotlin.name.FqName;
041 import org.jetbrains.kotlin.name.Name;
042 import org.jetbrains.kotlin.psi.*;
043 import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
044 import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
045 import org.jetbrains.kotlin.resolve.dataClassUtils.DataClassUtilsKt;
046 import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil;
047 import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
048 import org.jetbrains.kotlin.resolve.scopes.LexicalScopeKind;
049 import org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope;
050 import org.jetbrains.kotlin.resolve.scopes.ScopeUtils;
051 import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
052 import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt;
053 import org.jetbrains.kotlin.storage.StorageManager;
054 import org.jetbrains.kotlin.types.*;
055 import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
056
057 import java.util.*;
058
059 import static org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*;
060 import static org.jetbrains.kotlin.diagnostics.Errors.*;
061 import static org.jetbrains.kotlin.lexer.KtTokens.*;
062 import static org.jetbrains.kotlin.resolve.BindingContext.CONSTRUCTOR;
063 import static org.jetbrains.kotlin.resolve.BindingContext.PACKAGE_TO_FILES;
064 import static org.jetbrains.kotlin.resolve.DescriptorUtils.*;
065 import static org.jetbrains.kotlin.resolve.ModifiersChecker.resolveModalityFromModifiers;
066 import static org.jetbrains.kotlin.resolve.ModifiersChecker.resolveVisibilityFromModifiers;
067
068 public class DescriptorResolver {
069 public static final Name COPY_METHOD_NAME = Name.identifier("copy");
070
071 @NotNull private final TypeResolver typeResolver;
072 @NotNull private final AnnotationResolver annotationResolver;
073 @NotNull private final StorageManager storageManager;
074 @NotNull private final KotlinBuiltIns builtIns;
075 @NotNull private final SupertypeLoopChecker supertypeLoopsResolver;
076 @NotNull private final VariableTypeResolver variableTypeResolver;
077
078 public DescriptorResolver(
079 @NotNull AnnotationResolver annotationResolver,
080 @NotNull KotlinBuiltIns builtIns,
081 @NotNull StorageManager storageManager,
082 @NotNull TypeResolver typeResolver,
083 @NotNull SupertypeLoopChecker supertypeLoopsResolver,
084 @NotNull VariableTypeResolver variableTypeResolver
085 ) {
086 this.annotationResolver = annotationResolver;
087 this.builtIns = builtIns;
088 this.storageManager = storageManager;
089 this.typeResolver = typeResolver;
090 this.supertypeLoopsResolver = supertypeLoopsResolver;
091 this.variableTypeResolver = variableTypeResolver;
092 }
093
094 public List<KotlinType> resolveSupertypes(
095 @NotNull LexicalScope scope,
096 @NotNull ClassDescriptor classDescriptor,
097 @NotNull KtClassOrObject jetClass,
098 BindingTrace trace
099 ) {
100 List<KotlinType> supertypes = Lists.newArrayList();
101 List<KtSuperTypeListEntry> delegationSpecifiers = jetClass.getSuperTypeListEntries();
102 Collection<KotlinType> declaredSupertypes = resolveSuperTypeListEntries(
103 scope,
104 delegationSpecifiers,
105 typeResolver, trace, false);
106
107 for (KotlinType declaredSupertype : declaredSupertypes) {
108 addValidSupertype(supertypes, declaredSupertype);
109 }
110
111 if (classDescriptor.getKind() == ClassKind.ENUM_CLASS && !containsClass(supertypes)) {
112 supertypes.add(0, builtIns.getEnumType(classDescriptor.getDefaultType()));
113 }
114
115 if (supertypes.isEmpty()) {
116 KotlinType defaultSupertype = getDefaultSupertype(jetClass, trace, classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS);
117 addValidSupertype(supertypes, defaultSupertype);
118 }
119
120 return supertypes;
121 }
122
123 private static void addValidSupertype(List<KotlinType> supertypes, KotlinType declaredSupertype) {
124 if (!declaredSupertype.isError()) {
125 supertypes.add(declaredSupertype);
126 }
127 }
128
129 private boolean containsClass(Collection<KotlinType> result) {
130 for (KotlinType type : result) {
131 ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
132 if (descriptor instanceof ClassDescriptor && ((ClassDescriptor) descriptor).getKind() != ClassKind.INTERFACE) {
133 return true;
134 }
135 }
136 return false;
137 }
138
139 private KotlinType getDefaultSupertype(KtClassOrObject jetClass, BindingTrace trace, boolean isAnnotation) {
140 // TODO : beautify
141 if (jetClass instanceof KtEnumEntry) {
142 KtClassOrObject parent = KtStubbedPsiUtil.getContainingDeclaration(jetClass, KtClassOrObject.class);
143 ClassDescriptor parentDescriptor = trace.getBindingContext().get(BindingContext.CLASS, parent);
144 if (parentDescriptor.getTypeConstructor().getParameters().isEmpty()) {
145 return parentDescriptor.getDefaultType();
146 }
147 else {
148 trace.report(NO_GENERICS_IN_SUPERTYPE_SPECIFIER.on(jetClass.getNameIdentifier()));
149 return ErrorUtils.createErrorType("Supertype not specified");
150 }
151 }
152 else if (isAnnotation) {
153 return builtIns.getAnnotationType();
154 }
155 return builtIns.getAnyType();
156 }
157
158 public Collection<KotlinType> resolveSuperTypeListEntries(
159 LexicalScope extensibleScope,
160 List<KtSuperTypeListEntry> delegationSpecifiers,
161 @NotNull TypeResolver resolver,
162 BindingTrace trace,
163 boolean checkBounds
164 ) {
165 if (delegationSpecifiers.isEmpty()) {
166 return Collections.emptyList();
167 }
168 Collection<KotlinType> result = Lists.newArrayList();
169 for (KtSuperTypeListEntry delegationSpecifier : delegationSpecifiers) {
170 KtTypeReference typeReference = delegationSpecifier.getTypeReference();
171 if (typeReference != null) {
172 KotlinType supertype = resolver.resolveType(extensibleScope, typeReference, trace, checkBounds);
173 if (DynamicTypesKt.isDynamic(supertype)) {
174 trace.report(DYNAMIC_SUPERTYPE.on(typeReference));
175 }
176 else {
177 result.add(supertype);
178 KtTypeElement bareSuperType = checkNullableSupertypeAndStripQuestionMarks(trace, typeReference.getTypeElement());
179 checkProjectionsInImmediateArguments(trace, bareSuperType);
180 }
181 }
182 else {
183 result.add(ErrorUtils.createErrorType("No type reference"));
184 }
185 }
186 return result;
187 }
188
189 @Nullable
190 private static KtTypeElement checkNullableSupertypeAndStripQuestionMarks(@NotNull BindingTrace trace, @Nullable KtTypeElement typeElement) {
191 while (typeElement instanceof KtNullableType) {
192 KtNullableType nullableType = (KtNullableType) typeElement;
193 typeElement = nullableType.getInnerType();
194 // report only for innermost '?', the rest gets a 'redundant' warning
195 if (!(typeElement instanceof KtNullableType) && typeElement != null) {
196 trace.report(NULLABLE_SUPERTYPE.on(nullableType));
197 }
198 }
199 return typeElement;
200 }
201
202 private static void checkProjectionsInImmediateArguments(@NotNull BindingTrace trace, @Nullable KtTypeElement typeElement) {
203 if (typeElement instanceof KtUserType) {
204 KtUserType userType = (KtUserType) typeElement;
205 List<KtTypeProjection> typeArguments = userType.getTypeArguments();
206 for (KtTypeProjection typeArgument : typeArguments) {
207 if (typeArgument.getProjectionKind() != KtProjectionKind.NONE) {
208 trace.report(PROJECTION_IN_IMMEDIATE_ARGUMENT_TO_SUPERTYPE.on(typeArgument));
209 }
210 }
211 }
212 }
213
214 @NotNull
215 public static SimpleFunctionDescriptor createComponentFunctionDescriptor(
216 int parameterIndex,
217 @NotNull PropertyDescriptor property,
218 @NotNull ValueParameterDescriptor parameter,
219 @NotNull ClassDescriptor classDescriptor,
220 @NotNull BindingTrace trace
221 ) {
222 Name functionName = DataClassUtilsKt.createComponentName(parameterIndex);
223 KotlinType returnType = property.getType();
224
225 SimpleFunctionDescriptorImpl functionDescriptor = SimpleFunctionDescriptorImpl.create(
226 classDescriptor,
227 Annotations.Companion.getEMPTY(),
228 functionName,
229 CallableMemberDescriptor.Kind.SYNTHESIZED,
230 parameter.getSource()
231 );
232
233 functionDescriptor.initialize(
234 null,
235 classDescriptor.getThisAsReceiverParameter(),
236 Collections.<TypeParameterDescriptor>emptyList(),
237 Collections.<ValueParameterDescriptor>emptyList(),
238 returnType,
239 Modality.FINAL,
240 property.getVisibility()
241 );
242 functionDescriptor.setOperator(true);
243
244 trace.record(BindingContext.DATA_CLASS_COMPONENT_FUNCTION, parameter, functionDescriptor);
245
246 return functionDescriptor;
247 }
248
249 @NotNull
250 public static SimpleFunctionDescriptor createCopyFunctionDescriptor(
251 @NotNull Collection<ValueParameterDescriptor> constructorParameters,
252 @NotNull ClassDescriptor classDescriptor,
253 @NotNull BindingTrace trace
254 ) {
255 KotlinType returnType = classDescriptor.getDefaultType();
256
257 SimpleFunctionDescriptorImpl functionDescriptor = SimpleFunctionDescriptorImpl.create(
258 classDescriptor,
259 Annotations.Companion.getEMPTY(),
260 COPY_METHOD_NAME,
261 CallableMemberDescriptor.Kind.SYNTHESIZED,
262 classDescriptor.getSource()
263 );
264
265 List<ValueParameterDescriptor> parameterDescriptors = Lists.newArrayList();
266
267 for (ValueParameterDescriptor parameter : constructorParameters) {
268 PropertyDescriptor propertyDescriptor = trace.getBindingContext().get(BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameter);
269 // If parameter hasn't corresponding property, so it mustn't have default value as a parameter in copy function for data class
270 boolean declaresDefaultValue = propertyDescriptor != null;
271 ValueParameterDescriptorImpl parameterDescriptor =
272 new ValueParameterDescriptorImpl(functionDescriptor, null, parameter.getIndex(), parameter.getAnnotations(),
273 parameter.getName(), parameter.getType(),
274 declaresDefaultValue,
275 parameter.isCrossinline(),
276 parameter.isNoinline(),
277 parameter.getVarargElementType(), parameter.getSource());
278 parameterDescriptors.add(parameterDescriptor);
279 if (declaresDefaultValue) {
280 trace.record(BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameterDescriptor, propertyDescriptor);
281 }
282 }
283
284 functionDescriptor.initialize(
285 null,
286 classDescriptor.getThisAsReceiverParameter(),
287 Collections.<TypeParameterDescriptor>emptyList(),
288 parameterDescriptors,
289 returnType,
290 Modality.FINAL,
291 Visibilities.PUBLIC
292 );
293
294 trace.record(BindingContext.DATA_CLASS_COPY_FUNCTION, classDescriptor, functionDescriptor);
295 return functionDescriptor;
296 }
297
298 public static Visibility getDefaultVisibility(KtModifierListOwner modifierListOwner, DeclarationDescriptor containingDescriptor) {
299 Visibility defaultVisibility;
300 if (containingDescriptor instanceof ClassDescriptor) {
301 KtModifierList modifierList = modifierListOwner.getModifierList();
302 defaultVisibility = modifierList != null && modifierList.hasModifier(OVERRIDE_KEYWORD)
303 ? Visibilities.INHERITED
304 : Visibilities.DEFAULT_VISIBILITY;
305 }
306 else if (containingDescriptor instanceof FunctionDescriptor || containingDescriptor instanceof PropertyDescriptor) {
307 defaultVisibility = Visibilities.LOCAL;
308 }
309 else {
310 defaultVisibility = Visibilities.DEFAULT_VISIBILITY;
311 }
312 return defaultVisibility;
313 }
314
315 public static Modality getDefaultModality(DeclarationDescriptor containingDescriptor, Visibility visibility, boolean isBodyPresent) {
316 Modality defaultModality;
317 if (containingDescriptor instanceof ClassDescriptor) {
318 boolean isTrait = ((ClassDescriptor) containingDescriptor).getKind() == ClassKind.INTERFACE;
319 boolean isDefinitelyAbstract = isTrait && !isBodyPresent;
320 Modality basicModality = isTrait && !Visibilities.isPrivate(visibility) ? Modality.OPEN : Modality.FINAL;
321 defaultModality = isDefinitelyAbstract ? Modality.ABSTRACT : basicModality;
322 }
323 else {
324 defaultModality = Modality.FINAL;
325 }
326 return defaultModality;
327 }
328
329 @NotNull
330 public ValueParameterDescriptorImpl resolveValueParameterDescriptor(
331 LexicalScope scope, FunctionDescriptor owner, KtParameter valueParameter, int index, KotlinType type, BindingTrace trace
332 ) {
333 KotlinType varargElementType = null;
334 KotlinType variableType = type;
335 if (valueParameter.hasModifier(VARARG_KEYWORD)) {
336 varargElementType = type;
337 variableType = getVarargParameterType(type);
338 }
339
340 KtModifierList modifierList = valueParameter.getModifierList();
341
342 Annotations allAnnotations =
343 annotationResolver.resolveAnnotationsWithoutArguments(scope, valueParameter.getModifierList(), trace);
344 Annotations valueParameterAnnotations = Annotations.Companion.getEMPTY();
345
346 if (modifierList != null) {
347 if (valueParameter.hasValOrVar()) {
348 AnnotationSplitter annotationSplitter = AnnotationSplitter.create(
349 storageManager, allAnnotations, SetsKt.setOf(CONSTRUCTOR_PARAMETER));
350 valueParameterAnnotations = annotationSplitter.getAnnotationsForTarget(CONSTRUCTOR_PARAMETER);
351 }
352 else {
353 valueParameterAnnotations = allAnnotations;
354 }
355 }
356
357 ValueParameterDescriptorImpl valueParameterDescriptor = new ValueParameterDescriptorImpl(
358 owner,
359 null,
360 index,
361 valueParameterAnnotations,
362 KtPsiUtil.safeName(valueParameter.getName()),
363 variableType,
364 valueParameter.hasDefaultValue(),
365 valueParameter.hasModifier(CROSSINLINE_KEYWORD),
366 valueParameter.hasModifier(NOINLINE_KEYWORD),
367 varargElementType,
368 KotlinSourceElementKt.toSourceElement(valueParameter)
369 );
370
371 trace.record(BindingContext.VALUE_PARAMETER, valueParameter, valueParameterDescriptor);
372 return valueParameterDescriptor;
373 }
374
375 @NotNull
376 private KotlinType getVarargParameterType(@NotNull KotlinType elementType) {
377 KotlinType primitiveArrayType = builtIns.getPrimitiveArrayKotlinTypeByPrimitiveKotlinType(elementType);
378 if (primitiveArrayType != null) {
379 return primitiveArrayType;
380 }
381 return builtIns.getArrayType(Variance.OUT_VARIANCE, elementType);
382 }
383
384 public List<TypeParameterDescriptorImpl> resolveTypeParametersForCallableDescriptor(
385 DeclarationDescriptor containingDescriptor,
386 LexicalWritableScope extensibleScope,
387 LexicalScope scopeForAnnotationsResolve,
388 List<KtTypeParameter> typeParameters,
389 BindingTrace trace
390 ) {
391 List<TypeParameterDescriptorImpl> result = new ArrayList<TypeParameterDescriptorImpl>();
392 for (int i = 0, typeParametersSize = typeParameters.size(); i < typeParametersSize; i++) {
393 KtTypeParameter typeParameter = typeParameters.get(i);
394 result.add(resolveTypeParameterForCallableDescriptor(
395 containingDescriptor, extensibleScope, scopeForAnnotationsResolve, typeParameter, i, trace));
396 }
397 return result;
398 }
399
400 private TypeParameterDescriptorImpl resolveTypeParameterForCallableDescriptor(
401 DeclarationDescriptor containingDescriptor,
402 LexicalWritableScope extensibleScope,
403 LexicalScope scopeForAnnotationsResolve,
404 final KtTypeParameter typeParameter,
405 int index,
406 final BindingTrace trace
407 ) {
408 if (typeParameter.getVariance() != Variance.INVARIANT) {
409 assert !(containingDescriptor instanceof ClassifierDescriptor) : "This method is intended for functions/properties";
410 trace.report(VARIANCE_ON_TYPE_PARAMETER_OF_FUNCTION_OR_PROPERTY.on(typeParameter));
411 }
412
413 Annotations annotations =
414 annotationResolver.resolveAnnotationsWithArguments(scopeForAnnotationsResolve, typeParameter.getModifierList(), trace);
415
416 TypeParameterDescriptorImpl typeParameterDescriptor = TypeParameterDescriptorImpl.createForFurtherModification(
417 containingDescriptor,
418 annotations,
419 typeParameter.hasModifier(KtTokens.REIFIED_KEYWORD),
420 typeParameter.getVariance(),
421 KtPsiUtil.safeName(typeParameter.getName()),
422 index,
423 KotlinSourceElementKt.toSourceElement(typeParameter),
424 new Function1<KotlinType, Void>() {
425 @Override
426 public Void invoke(KotlinType type) {
427 trace.report(Errors.CYCLIC_GENERIC_UPPER_BOUND.on(typeParameter));
428 return null;
429 }
430 },
431 supertypeLoopsResolver
432 );
433 trace.record(BindingContext.TYPE_PARAMETER, typeParameter, typeParameterDescriptor);
434 extensibleScope.addClassifierDescriptor(typeParameterDescriptor);
435 return typeParameterDescriptor;
436 }
437
438 @NotNull
439 public static ConstructorDescriptorImpl createAndRecordPrimaryConstructorForObject(
440 @Nullable KtClassOrObject object,
441 @NotNull ClassDescriptor classDescriptor,
442 @NotNull BindingTrace trace
443 ) {
444 ConstructorDescriptorImpl constructorDescriptor =
445 DescriptorFactory.createPrimaryConstructorForObject(classDescriptor, KotlinSourceElementKt.toSourceElement(object));
446 if (object != null) {
447 KtPrimaryConstructor primaryConstructor = object.getPrimaryConstructor();
448 trace.record(CONSTRUCTOR, primaryConstructor != null ? primaryConstructor : object, constructorDescriptor);
449 }
450 return constructorDescriptor;
451 }
452
453 static final class UpperBoundCheckRequest {
454 public final Name typeParameterName;
455 public final KtTypeReference upperBound;
456 public final KotlinType upperBoundType;
457
458 UpperBoundCheckRequest(Name typeParameterName, KtTypeReference upperBound, KotlinType upperBoundType) {
459 this.typeParameterName = typeParameterName;
460 this.upperBound = upperBound;
461 this.upperBoundType = upperBoundType;
462 }
463 }
464
465 public void resolveGenericBounds(
466 @NotNull KtTypeParameterListOwner declaration,
467 @NotNull DeclarationDescriptor descriptor,
468 LexicalScope scope,
469 List<TypeParameterDescriptorImpl> parameters,
470 BindingTrace trace
471 ) {
472 List<UpperBoundCheckRequest> upperBoundCheckRequests = Lists.newArrayList();
473
474 List<KtTypeParameter> typeParameters = declaration.getTypeParameters();
475 Map<Name, TypeParameterDescriptorImpl> parameterByName = Maps.newHashMap();
476 for (int i = 0; i < typeParameters.size(); i++) {
477 KtTypeParameter ktTypeParameter = typeParameters.get(i);
478 TypeParameterDescriptorImpl typeParameterDescriptor = parameters.get(i);
479
480 parameterByName.put(typeParameterDescriptor.getName(), typeParameterDescriptor);
481
482 KtTypeReference extendsBound = ktTypeParameter.getExtendsBound();
483 if (extendsBound != null) {
484 KotlinType type = typeResolver.resolveType(scope, extendsBound, trace, false);
485 typeParameterDescriptor.addUpperBound(type);
486 upperBoundCheckRequests.add(new UpperBoundCheckRequest(ktTypeParameter.getNameAsName(), extendsBound, type));
487 }
488 }
489 for (KtTypeConstraint constraint : declaration.getTypeConstraints()) {
490 KtSimpleNameExpression subjectTypeParameterName = constraint.getSubjectTypeParameterName();
491 if (subjectTypeParameterName == null) {
492 continue;
493 }
494 Name referencedName = subjectTypeParameterName.getReferencedNameAsName();
495 TypeParameterDescriptorImpl typeParameterDescriptor = parameterByName.get(referencedName);
496 KtTypeReference boundTypeReference = constraint.getBoundTypeReference();
497 KotlinType bound = null;
498 if (boundTypeReference != null) {
499 bound = typeResolver.resolveType(scope, boundTypeReference, trace, false);
500 upperBoundCheckRequests.add(new UpperBoundCheckRequest(referencedName, boundTypeReference, bound));
501 }
502
503 if (typeParameterDescriptor != null) {
504 trace.record(BindingContext.REFERENCE_TARGET, subjectTypeParameterName, typeParameterDescriptor);
505 if (bound != null) {
506 typeParameterDescriptor.addUpperBound(bound);
507 }
508 }
509 }
510
511 for (TypeParameterDescriptorImpl parameter : parameters) {
512 parameter.addDefaultUpperBound();
513 parameter.setInitialized();
514 }
515
516 for (TypeParameterDescriptorImpl parameter : parameters) {
517 checkConflictingUpperBounds(trace, parameter, typeParameters.get(parameter.getIndex()));
518 }
519
520 if (!(declaration instanceof KtClass)) {
521 checkUpperBoundTypes(trace, upperBoundCheckRequests);
522 checkNamesInConstraints(declaration, descriptor, scope, trace);
523 }
524 }
525
526 public static void checkUpperBoundTypes(@NotNull BindingTrace trace, @NotNull List<UpperBoundCheckRequest> requests) {
527 if (requests.isEmpty()) return;
528
529 Set<Name> classBoundEncountered = new HashSet<Name>();
530 Set<Pair<Name, TypeConstructor>> allBounds = new HashSet<Pair<Name, TypeConstructor>>();
531
532 for (UpperBoundCheckRequest request : requests) {
533 Name typeParameterName = request.typeParameterName;
534 KotlinType upperBound = request.upperBoundType;
535 KtTypeReference upperBoundElement = request.upperBound;
536
537 if (!upperBound.isError()) {
538 if (!allBounds.add(new Pair<Name, TypeConstructor>(typeParameterName, upperBound.getConstructor()))) {
539 trace.report(REPEATED_BOUND.on(upperBoundElement));
540 }
541 else {
542 ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(upperBound);
543 if (classDescriptor != null) {
544 ClassKind kind = classDescriptor.getKind();
545 if (kind == ClassKind.CLASS || kind == ClassKind.ENUM_CLASS || kind == ClassKind.OBJECT) {
546 if (!classBoundEncountered.add(typeParameterName)) {
547 trace.report(ONLY_ONE_CLASS_BOUND_ALLOWED.on(upperBoundElement));
548 }
549 }
550 }
551 }
552 }
553
554 checkUpperBoundType(upperBoundElement, upperBound, trace);
555 }
556 }
557
558 public static void checkConflictingUpperBounds(
559 @NotNull BindingTrace trace,
560 @NotNull TypeParameterDescriptor parameter,
561 @NotNull KtTypeParameter typeParameter
562 ) {
563 if (KotlinBuiltIns.isNothing(TypeIntersector.getUpperBoundsAsType(parameter))) {
564 trace.report(CONFLICTING_UPPER_BOUNDS.on(typeParameter, parameter));
565 }
566 }
567
568 public void checkNamesInConstraints(
569 @NotNull KtTypeParameterListOwner declaration,
570 @NotNull DeclarationDescriptor descriptor,
571 @NotNull LexicalScope scope,
572 @NotNull BindingTrace trace
573 ) {
574 for (KtTypeConstraint constraint : declaration.getTypeConstraints()) {
575 KtSimpleNameExpression nameExpression = constraint.getSubjectTypeParameterName();
576 if (nameExpression == null) continue;
577
578 Name name = nameExpression.getReferencedNameAsName();
579
580 ClassifierDescriptor classifier = ScopeUtilsKt.findClassifier(scope, name, NoLookupLocation.FOR_NON_TRACKED_SCOPE);
581 if (classifier instanceof TypeParameterDescriptor && classifier.getContainingDeclaration() == descriptor) continue;
582
583 if (classifier != null) {
584 // To tell the user that we look only for locally defined type parameters
585 trace.report(NAME_IN_CONSTRAINT_IS_NOT_A_TYPE_PARAMETER.on(nameExpression, constraint, declaration));
586 trace.record(BindingContext.REFERENCE_TARGET, nameExpression, classifier);
587 }
588 else {
589 trace.report(UNRESOLVED_REFERENCE.on(nameExpression, nameExpression));
590 }
591
592 KtTypeReference boundTypeReference = constraint.getBoundTypeReference();
593 if (boundTypeReference != null) {
594 typeResolver.resolveType(scope, boundTypeReference, trace, true);
595 }
596 }
597 }
598
599 public static void checkUpperBoundType(
600 KtTypeReference upperBound,
601 @NotNull KotlinType upperBoundType,
602 BindingTrace trace
603 ) {
604 if (DeclarationsCheckerKt.checkNotEnumEntry(upperBound, trace)) return;
605 if (!TypeUtils.canHaveSubtypes(KotlinTypeChecker.DEFAULT, upperBoundType)) {
606 trace.report(FINAL_UPPER_BOUND.on(upperBound, upperBoundType));
607 }
608 if (DynamicTypesKt.isDynamic(upperBoundType)) {
609 trace.report(DYNAMIC_UPPER_BOUND.on(upperBound));
610 }
611 if (KotlinBuiltIns.isExactExtensionFunctionType(upperBoundType)) {
612 trace.report(UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE.on(upperBound));
613 }
614 }
615
616 @NotNull
617 public VariableDescriptor resolveLocalVariableDescriptor(
618 @NotNull LexicalScope scope,
619 @NotNull KtParameter parameter,
620 BindingTrace trace
621 ) {
622 KotlinType type = resolveParameterType(scope, parameter, trace);
623 return resolveLocalVariableDescriptor(parameter, type, trace, scope);
624 }
625
626 private KotlinType resolveParameterType(LexicalScope scope, KtParameter parameter, BindingTrace trace) {
627 KtTypeReference typeReference = parameter.getTypeReference();
628 KotlinType type;
629 if (typeReference != null) {
630 type = typeResolver.resolveType(scope, typeReference, trace, true);
631 }
632 else {
633 // Error is reported by the parser
634 type = ErrorUtils.createErrorType("Annotation is absent");
635 }
636 if (parameter.hasModifier(VARARG_KEYWORD)) {
637 return getVarargParameterType(type);
638 }
639 return type;
640 }
641
642 public VariableDescriptor resolveLocalVariableDescriptor(
643 @NotNull KtParameter parameter,
644 @NotNull KotlinType type,
645 BindingTrace trace,
646 @NotNull LexicalScope scope
647 ) {
648 VariableDescriptor variableDescriptor = new LocalVariableDescriptor(
649 scope.getOwnerDescriptor(),
650 annotationResolver.resolveAnnotationsWithArguments(scope, parameter.getModifierList(), trace),
651 KtPsiUtil.safeName(parameter.getName()),
652 type,
653 false,
654 KotlinSourceElementKt.toSourceElement(parameter)
655 );
656 trace.record(BindingContext.VALUE_PARAMETER, parameter, variableDescriptor);
657 // Type annotations also should be resolved
658 ForceResolveUtil.forceResolveAllContents(type.getAnnotations());
659 return variableDescriptor;
660 }
661
662 @NotNull
663 public PropertyDescriptor resolvePropertyDescriptor(
664 @NotNull DeclarationDescriptor containingDeclaration,
665 @NotNull LexicalScope scope,
666 @NotNull KtProperty property,
667 @NotNull final BindingTrace trace,
668 @NotNull DataFlowInfo dataFlowInfo
669 ) {
670 KtModifierList modifierList = property.getModifierList();
671 boolean isVar = property.isVar();
672
673 boolean hasBody = hasBody(property);
674 Visibility visibility = resolveVisibilityFromModifiers(property, getDefaultVisibility(property, containingDeclaration));
675 Modality modality = containingDeclaration instanceof ClassDescriptor
676 ? resolveModalityFromModifiers(property, getDefaultModality(containingDeclaration, visibility, hasBody))
677 : Modality.FINAL;
678
679 final AnnotationSplitter.PropertyWrapper wrapper = new AnnotationSplitter.PropertyWrapper(property);
680
681 Annotations allAnnotations = annotationResolver.resolveAnnotationsWithoutArguments(scope, modifierList, trace);
682 AnnotationSplitter annotationSplitter =
683 new AnnotationSplitter(storageManager, allAnnotations, new Function0<Set<AnnotationUseSiteTarget>>() {
684 @Override
685 public Set<AnnotationUseSiteTarget> invoke() {
686 return AnnotationSplitter.getTargetSet(false, trace.getBindingContext(), wrapper);
687 }
688 });
689
690 Annotations propertyAnnotations = new CompositeAnnotations(CollectionsKt.listOf(
691 annotationSplitter.getAnnotationsForTargets(PROPERTY, FIELD, PROPERTY_DELEGATE_FIELD),
692 annotationSplitter.getOtherAnnotations()));
693
694 PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(
695 containingDeclaration,
696 propertyAnnotations,
697 modality,
698 visibility,
699 isVar,
700 KtPsiUtil.safeName(property.getName()),
701 CallableMemberDescriptor.Kind.DECLARATION,
702 KotlinSourceElementKt.toSourceElement(property),
703 modifierList != null && modifierList.hasModifier(KtTokens.LATEINIT_KEYWORD),
704 modifierList != null && modifierList.hasModifier(KtTokens.CONST_KEYWORD)
705 );
706 wrapper.setDescriptor(propertyDescriptor);
707
708 List<TypeParameterDescriptorImpl> typeParameterDescriptors;
709 LexicalScope scopeWithTypeParameters;
710 KotlinType receiverType = null;
711
712 {
713 List<KtTypeParameter> typeParameters = property.getTypeParameters();
714 if (typeParameters.isEmpty()) {
715 scopeWithTypeParameters = scope;
716 typeParameterDescriptors = Collections.emptyList();
717 }
718 else {
719 LexicalWritableScope writableScope = new LexicalWritableScope(
720 scope, containingDeclaration, false, null, new TraceBasedRedeclarationHandler(trace),
721 LexicalScopeKind.PROPERTY_HEADER);
722 typeParameterDescriptors = resolveTypeParametersForCallableDescriptor(
723 propertyDescriptor, writableScope, scope, typeParameters, trace);
724 writableScope.changeLockLevel(LexicalWritableScope.LockLevel.READING);
725 resolveGenericBounds(property, propertyDescriptor, writableScope, typeParameterDescriptors, trace);
726 scopeWithTypeParameters = writableScope;
727 }
728
729 KtTypeReference receiverTypeRef = property.getReceiverTypeReference();
730 if (receiverTypeRef != null) {
731 receiverType = typeResolver.resolveType(scopeWithTypeParameters, receiverTypeRef, trace, true);
732 }
733 }
734
735 ReceiverParameterDescriptor receiverDescriptor =
736 DescriptorFactory.createExtensionReceiverParameterForCallable(propertyDescriptor, receiverType);
737
738 KotlinType type = variableTypeResolver.process(
739 propertyDescriptor, ScopeUtils.makeScopeForPropertyInitializer(scopeWithTypeParameters, propertyDescriptor),
740 property, dataFlowInfo, true, trace
741 );
742
743 propertyDescriptor.setType(type, typeParameterDescriptors, getDispatchReceiverParameterIfNeeded(containingDeclaration),
744 receiverDescriptor);
745
746 PropertyGetterDescriptorImpl getter = resolvePropertyGetterDescriptor(
747 scopeWithTypeParameters, property, propertyDescriptor, annotationSplitter, trace);
748 PropertySetterDescriptor setter = resolvePropertySetterDescriptor(
749 scopeWithTypeParameters, property, propertyDescriptor, annotationSplitter, trace);
750
751 propertyDescriptor.initialize(getter, setter);
752
753 trace.record(BindingContext.VARIABLE, property, propertyDescriptor);
754 return propertyDescriptor;
755 }
756
757 /*package*/
758 static boolean hasBody(KtProperty property) {
759 boolean hasBody = property.hasDelegateExpressionOrInitializer();
760 if (!hasBody) {
761 KtPropertyAccessor getter = property.getGetter();
762 if (getter != null && getter.hasBody()) {
763 hasBody = true;
764 }
765 KtPropertyAccessor setter = property.getSetter();
766 if (!hasBody && setter != null && setter.hasBody()) {
767 hasBody = true;
768 }
769 }
770 return hasBody;
771 }
772
773
774 @Nullable
775 /*package*/ static KotlinType transformAnonymousTypeIfNeeded(
776 @NotNull DeclarationDescriptorWithVisibility descriptor,
777 @NotNull KtNamedDeclaration declaration,
778 @NotNull KotlinType type,
779 @NotNull BindingTrace trace
780 ) {
781 ClassifierDescriptor classifier = type.getConstructor().getDeclarationDescriptor();
782 if (classifier == null || !DescriptorUtils.isAnonymousObject(classifier) || DescriptorUtils.isLocal(descriptor)) {
783 return type;
784 }
785
786 boolean definedInClass = DescriptorUtils.getParentOfType(descriptor, ClassDescriptor.class) != null;
787 if (!definedInClass || !Visibilities.isPrivate(descriptor.getVisibility())) {
788 if (type.getConstructor().getSupertypes().size() == 1) {
789 return type.getConstructor().getSupertypes().iterator().next();
790 }
791 else {
792 trace.report(AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.on(declaration, type.getConstructor().getSupertypes()));
793 }
794 }
795
796 return type;
797 }
798
799
800 @Nullable
801 private PropertySetterDescriptor resolvePropertySetterDescriptor(
802 @NotNull LexicalScope scopeWithTypeParameters,
803 @NotNull KtProperty property,
804 @NotNull PropertyDescriptor propertyDescriptor,
805 @NotNull AnnotationSplitter annotationSplitter,
806 BindingTrace trace
807 ) {
808 KtPropertyAccessor setter = property.getSetter();
809 PropertySetterDescriptorImpl setterDescriptor = null;
810 if (setter != null) {
811 Annotations annotations = new CompositeAnnotations(CollectionsKt.listOf(
812 annotationSplitter.getAnnotationsForTarget(PROPERTY_SETTER),
813 annotationResolver.resolveAnnotationsWithoutArguments(scopeWithTypeParameters, setter.getModifierList(), trace)));
814 KtParameter parameter = setter.getParameter();
815
816 setterDescriptor = new PropertySetterDescriptorImpl(propertyDescriptor, annotations,
817 resolveModalityFromModifiers(setter, propertyDescriptor.getModality()),
818 resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()),
819 setter.hasBody(), false, setter.hasModifier(EXTERNAL_KEYWORD),
820 CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt
821 .toSourceElement(setter));
822 KtTypeReference returnTypeReference = setter.getReturnTypeReference();
823 if (returnTypeReference != null) {
824 KotlinType returnType = typeResolver.resolveType(scopeWithTypeParameters, returnTypeReference, trace, true);
825 if (!KotlinBuiltIns.isUnit(returnType)) {
826 trace.report(WRONG_SETTER_RETURN_TYPE.on(returnTypeReference));
827 }
828 }
829
830 if (parameter != null) {
831
832 // This check is redundant: the parser does not allow a default value, but we'll keep it just in case
833 if (parameter.hasDefaultValue()) {
834 trace.report(SETTER_PARAMETER_WITH_DEFAULT_VALUE.on(parameter.getDefaultValue()));
835 }
836
837 KotlinType type;
838 KtTypeReference typeReference = parameter.getTypeReference();
839 if (typeReference == null) {
840 type = propertyDescriptor.getType(); // TODO : this maybe unknown at this point
841 }
842 else {
843 type = typeResolver.resolveType(scopeWithTypeParameters, typeReference, trace, true);
844 KotlinType inType = propertyDescriptor.getType();
845 if (inType != null) {
846 if (!TypeUtils.equalTypes(type, inType)) {
847 trace.report(WRONG_SETTER_PARAMETER_TYPE.on(typeReference, inType, type));
848 }
849 }
850 else {
851 // TODO : the same check may be needed later???
852 }
853 }
854
855 ValueParameterDescriptorImpl valueParameterDescriptor =
856 resolveValueParameterDescriptor(scopeWithTypeParameters, setterDescriptor, parameter, 0, type, trace);
857 setterDescriptor.initialize(valueParameterDescriptor);
858 }
859 else {
860 setterDescriptor.initializeDefault();
861 }
862
863 trace.record(BindingContext.PROPERTY_ACCESSOR, setter, setterDescriptor);
864 }
865 else if (property.isVar()) {
866 Annotations setterAnnotations = annotationSplitter.getAnnotationsForTarget(PROPERTY_SETTER);
867 setterDescriptor = DescriptorFactory.createSetter(propertyDescriptor, setterAnnotations, !property.hasDelegate(),
868 /* isExternal = */ false, propertyDescriptor.getSource());
869 }
870
871 if (!property.isVar()) {
872 if (setter != null) {
873 // trace.getErrorHandler().genericError(setter.asElement().getNode(), "A 'val'-property cannot have a setter");
874 trace.report(VAL_WITH_SETTER.on(setter));
875 }
876 }
877 return setterDescriptor;
878 }
879
880 @NotNull
881 private PropertyGetterDescriptorImpl resolvePropertyGetterDescriptor(
882 @NotNull LexicalScope scopeWithTypeParameters,
883 @NotNull KtProperty property,
884 @NotNull PropertyDescriptor propertyDescriptor,
885 @NotNull AnnotationSplitter annotationSplitter,
886 BindingTrace trace
887 ) {
888 PropertyGetterDescriptorImpl getterDescriptor;
889 KtPropertyAccessor getter = property.getGetter();
890 if (getter != null) {
891 Annotations getterAnnotations = new CompositeAnnotations(CollectionsKt.listOf(
892 annotationSplitter.getAnnotationsForTarget(PROPERTY_GETTER),
893 annotationResolver.resolveAnnotationsWithoutArguments(scopeWithTypeParameters, getter.getModifierList(), trace)));
894
895 KotlinType outType = propertyDescriptor.getType();
896 KotlinType returnType = outType;
897 KtTypeReference returnTypeReference = getter.getReturnTypeReference();
898 if (returnTypeReference != null) {
899 returnType = typeResolver.resolveType(scopeWithTypeParameters, returnTypeReference, trace, true);
900 if (outType != null && !TypeUtils.equalTypes(returnType, outType)) {
901 trace.report(WRONG_GETTER_RETURN_TYPE.on(returnTypeReference, propertyDescriptor.getReturnType(), outType));
902 }
903 }
904
905 getterDescriptor = new PropertyGetterDescriptorImpl(propertyDescriptor, getterAnnotations,
906 resolveModalityFromModifiers(getter, propertyDescriptor.getModality()),
907 resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()),
908 getter.hasBody(), false, getter.hasModifier(EXTERNAL_KEYWORD),
909 CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt
910 .toSourceElement(getter));
911 getterDescriptor.initialize(returnType);
912 trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor);
913 }
914 else {
915 Annotations getterAnnotations = annotationSplitter.getAnnotationsForTarget(PROPERTY_GETTER);
916 getterDescriptor = DescriptorFactory.createGetter(propertyDescriptor, getterAnnotations, !property.hasDelegate(),
917 /* isExternal = */ false);
918 getterDescriptor.initialize(propertyDescriptor.getType());
919 }
920 return getterDescriptor;
921 }
922
923 @NotNull
924 public PropertyDescriptor resolvePrimaryConstructorParameterToAProperty(
925 @NotNull ClassDescriptor classDescriptor,
926 @NotNull ValueParameterDescriptor valueParameter,
927 @NotNull LexicalScope scope,
928 @NotNull KtParameter parameter, final BindingTrace trace
929 ) {
930 KotlinType type = resolveParameterType(scope, parameter, trace);
931 Name name = parameter.getNameAsSafeName();
932 boolean isMutable = parameter.isMutable();
933 KtModifierList modifierList = parameter.getModifierList();
934
935 if (modifierList != null) {
936 if (modifierList.hasModifier(KtTokens.ABSTRACT_KEYWORD)) {
937 trace.report(ABSTRACT_PROPERTY_IN_PRIMARY_CONSTRUCTOR_PARAMETERS.on(parameter));
938 }
939 }
940
941 final AnnotationSplitter.PropertyWrapper propertyWrapper = new AnnotationSplitter.PropertyWrapper(parameter);
942 Annotations allAnnotations = annotationResolver.resolveAnnotationsWithoutArguments(scope, parameter.getModifierList(), trace);
943 AnnotationSplitter annotationSplitter =
944 new AnnotationSplitter(storageManager, allAnnotations, new Function0<Set<AnnotationUseSiteTarget>>() {
945 @Override
946 public Set<AnnotationUseSiteTarget> invoke() {
947 return AnnotationSplitter.getTargetSet(true, trace.getBindingContext(), propertyWrapper);
948 }
949 });
950
951 Annotations propertyAnnotations = new CompositeAnnotations(
952 annotationSplitter.getAnnotationsForTargets(PROPERTY, FIELD),
953 annotationSplitter.getOtherAnnotations());
954
955 PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(
956 classDescriptor,
957 propertyAnnotations,
958 resolveModalityFromModifiers(parameter, Modality.FINAL),
959 resolveVisibilityFromModifiers(parameter, getDefaultVisibility(parameter, classDescriptor)),
960 isMutable,
961 name,
962 CallableMemberDescriptor.Kind.DECLARATION,
963 KotlinSourceElementKt.toSourceElement(parameter),
964 /* lateInit = */ false,
965 /* isConst = */ false
966 );
967 propertyWrapper.setDescriptor(propertyDescriptor);
968 propertyDescriptor.setType(type, Collections.<TypeParameterDescriptor>emptyList(),
969 getDispatchReceiverParameterIfNeeded(classDescriptor), (ReceiverParameterDescriptor) null);
970
971 Annotations setterAnnotations = annotationSplitter.getAnnotationsForTarget(PROPERTY_SETTER);
972 Annotations getterAnnotations = new CompositeAnnotations(CollectionsKt.listOf(
973 annotationSplitter.getAnnotationsForTarget(PROPERTY_GETTER)));
974
975 PropertyGetterDescriptorImpl getter = DescriptorFactory.createDefaultGetter(propertyDescriptor, getterAnnotations);
976 PropertySetterDescriptor setter =
977 propertyDescriptor.isVar() ? DescriptorFactory.createDefaultSetter(propertyDescriptor, setterAnnotations) : null;
978
979 propertyDescriptor.initialize(getter, setter);
980 getter.initialize(propertyDescriptor.getType());
981
982 trace.record(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter, propertyDescriptor);
983 trace.record(BindingContext.VALUE_PARAMETER_AS_PROPERTY, valueParameter, propertyDescriptor);
984 return propertyDescriptor;
985 }
986
987 public static void checkBounds(@NotNull KtTypeReference typeReference, @NotNull KotlinType type, @NotNull BindingTrace trace) {
988 if (type.isError()) return;
989
990 KtTypeElement typeElement = typeReference.getTypeElement();
991 if (typeElement == null) return;
992
993 List<TypeParameterDescriptor> parameters = type.getConstructor().getParameters();
994 List<TypeProjection> arguments = type.getArguments();
995 assert parameters.size() == arguments.size();
996
997 List<KtTypeReference> jetTypeArguments = typeElement.getTypeArgumentsAsTypes();
998
999 // A type reference from Kotlin code can yield a flexible type only if it's `ft<T1, T2>`, whose bounds should not be checked
1000 if (FlexibleTypesKt.isFlexible(type) && !DynamicTypesKt.isDynamic(type)) {
1001 assert jetTypeArguments.size() == 2
1002 : "Flexible type cannot be denoted in Kotlin otherwise than as ft<T1, T2>, but was: "
1003 + PsiUtilsKt.getElementTextWithContext(typeReference);
1004 // it's really ft<Foo, Bar>
1005 Flexibility flexibility = FlexibleTypesKt.flexibility(type);
1006 checkBounds(jetTypeArguments.get(0), flexibility.getLowerBound(), trace);
1007 checkBounds(jetTypeArguments.get(1), flexibility.getUpperBound(), trace);
1008 return;
1009 }
1010
1011 assert jetTypeArguments.size() <= arguments.size() : typeElement.getText() + ": " + jetTypeArguments + " - " + arguments;
1012
1013 TypeSubstitutor substitutor = TypeSubstitutor.create(type);
1014 for (int i = 0; i < jetTypeArguments.size(); i++) {
1015 KtTypeReference jetTypeArgument = jetTypeArguments.get(i);
1016
1017 if (jetTypeArgument == null) continue;
1018
1019 KotlinType typeArgument = arguments.get(i).getType();
1020 checkBounds(jetTypeArgument, typeArgument, trace);
1021
1022 TypeParameterDescriptor typeParameterDescriptor = parameters.get(i);
1023 checkBounds(jetTypeArgument, typeArgument, typeParameterDescriptor, substitutor, trace);
1024 }
1025 }
1026
1027 public static void checkBounds(
1028 @NotNull KtTypeReference jetTypeArgument,
1029 @NotNull KotlinType typeArgument,
1030 @NotNull TypeParameterDescriptor typeParameterDescriptor,
1031 @NotNull TypeSubstitutor substitutor,
1032 @NotNull BindingTrace trace
1033 ) {
1034 for (KotlinType bound : typeParameterDescriptor.getUpperBounds()) {
1035 KotlinType substitutedBound = substitutor.safeSubstitute(bound, Variance.INVARIANT);
1036 if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(typeArgument, substitutedBound)) {
1037 trace.report(UPPER_BOUND_VIOLATED.on(jetTypeArgument, substitutedBound, typeArgument));
1038 }
1039 }
1040 }
1041
1042 public static boolean checkHasOuterClassInstance(
1043 @NotNull LexicalScope scope,
1044 @NotNull BindingTrace trace,
1045 @NotNull PsiElement reportErrorsOn,
1046 @NotNull ClassDescriptor target
1047 ) {
1048 ClassDescriptor classDescriptor = getContainingClass(scope);
1049
1050 if (!isInsideOuterClassOrItsSubclass(classDescriptor, target)) {
1051 return true;
1052 }
1053
1054 while (classDescriptor != null) {
1055 if (isSubclass(classDescriptor, target)) {
1056 return true;
1057 }
1058
1059 if (isStaticNestedClass(classDescriptor)) {
1060 trace.report(INACCESSIBLE_OUTER_CLASS_EXPRESSION.on(reportErrorsOn, classDescriptor));
1061 return false;
1062 }
1063 classDescriptor = getParentOfType(classDescriptor, ClassDescriptor.class, true);
1064 }
1065 return true;
1066 }
1067
1068 private static boolean isInsideOuterClassOrItsSubclass(@Nullable DeclarationDescriptor nested, @NotNull ClassDescriptor outer) {
1069 if (nested == null) return false;
1070
1071 if (nested instanceof ClassDescriptor && isSubclass((ClassDescriptor) nested, outer)) return true;
1072
1073 return isInsideOuterClassOrItsSubclass(nested.getContainingDeclaration(), outer);
1074 }
1075
1076 @Nullable
1077 public static ClassDescriptor getContainingClass(@NotNull LexicalScope scope) {
1078 return getParentOfType(scope.getOwnerDescriptor(), ClassDescriptor.class, false);
1079 }
1080
1081 public static void registerFileInPackage(@NotNull BindingTrace trace, @NotNull KtFile file) {
1082 // Register files corresponding to this package
1083 // The trace currently does not support bi-di multimaps that would handle this task nicer
1084 FqName fqName = file.getPackageFqName();
1085 Collection<KtFile> files = trace.get(PACKAGE_TO_FILES, fqName);
1086 if (files == null) {
1087 files = Sets.newIdentityHashSet();
1088 }
1089 files.add(file);
1090 trace.record(BindingContext.PACKAGE_TO_FILES, fqName, files);
1091 }
1092 }