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.descriptors.impl;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.descriptors.*;
022 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023 import org.jetbrains.kotlin.name.Name;
024 import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver;
025 import org.jetbrains.kotlin.types.KotlinType;
026 import org.jetbrains.kotlin.types.TypeSubstitutor;
027 import org.jetbrains.kotlin.types.Variance;
028
029 import java.util.Collection;
030 import java.util.Collections;
031 import java.util.List;
032
033 public abstract class AbstractReceiverParameterDescriptor extends DeclarationDescriptorImpl implements ReceiverParameterDescriptor {
034 private static final Name RECEIVER_PARAMETER_NAME = Name.special("<this>");
035
036 public AbstractReceiverParameterDescriptor() {
037 super(Annotations.Companion.getEMPTY(), RECEIVER_PARAMETER_NAME);
038 }
039
040 @Nullable
041 @Override
042 public ReceiverParameterDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
043 if (substitutor.isEmpty()) return this;
044
045 KotlinType substitutedType;
046 if (getContainingDeclaration() instanceof ClassDescriptor) {
047 // Due to some reasons we check that receiver value type is a subtype of dispatch parameter
048 // (although we get members exactly from it's scope)
049 // So to make receiver with projections be a subtype of parameter's type with captured type arguments,
050 // we approximate latter to it's upper bound.
051 // See approximateDispatchReceiver.kt test for clarification
052 substitutedType = substitutor.substitute(getType(), Variance.OUT_VARIANCE);
053 }
054 else {
055 substitutedType = substitutor.substitute(getType(), Variance.INVARIANT);
056 }
057
058 if (substitutedType == null) return null;
059
060 return new ReceiverParameterDescriptorImpl(getContainingDeclaration(), new TransientReceiver(substitutedType));
061 }
062
063 @Override
064 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
065 return visitor.visitReceiverParameterDescriptor(this, data);
066 }
067
068 @Nullable
069 @Override
070 public ReceiverParameterDescriptor getExtensionReceiverParameter() {
071 return null;
072 }
073
074 @Nullable
075 @Override
076 public ReceiverParameterDescriptor getDispatchReceiverParameter() {
077 return null;
078 }
079
080 @NotNull
081 @Override
082 public List<TypeParameterDescriptor> getTypeParameters() {
083 return Collections.emptyList();
084 }
085
086 @Nullable
087 @Override
088 public KotlinType getReturnType() {
089 return getType();
090 }
091
092 @NotNull
093 @Override
094 public KotlinType getType() {
095 return getValue().getType();
096 }
097
098 @NotNull
099 @Override
100 public List<ValueParameterDescriptor> getValueParameters() {
101 return Collections.emptyList();
102 }
103
104 @Override
105 public boolean hasStableParameterNames() {
106 return false;
107 }
108
109 @Override
110 public boolean hasSynthesizedParameterNames() {
111 return false;
112 }
113
114 @NotNull
115 @Override
116 public Collection<? extends CallableDescriptor> getOverriddenDescriptors() {
117 return Collections.emptySet();
118 }
119
120 @NotNull
121 @Override
122 public Visibility getVisibility() {
123 return Visibilities.LOCAL;
124 }
125
126 @NotNull
127 @Override
128 public ParameterDescriptor getOriginal() {
129 return this;
130 }
131
132 @NotNull
133 @Override
134 public SourceElement getSource() {
135 return SourceElement.NO_SOURCE;
136 }
137 }