Introducing ReceiverParameterDescriptor

This commit is contained in:
Andrey Breslav
2012-10-30 11:27:33 +04:00
parent 11355b3ea9
commit 5609850d1d
5 changed files with 255 additions and 0 deletions
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.descriptors;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.receivers.TransientReceiver;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import org.jetbrains.jet.lang.types.Variance;
import java.util.Collections;
public abstract class AbstractReceiverParameterDescriptor extends DeclarationDescriptorImpl implements ReceiverParameterDescriptor {
private static final Name RECEIVER_PARAMETER_NAME = Name.special("<this>");
public AbstractReceiverParameterDescriptor() {
super(Collections.<AnnotationDescriptor>emptyList(), RECEIVER_PARAMETER_NAME);
}
@Nullable
@Override
public ReceiverParameterDescriptor substitute(TypeSubstitutor substitutor) {
if (substitutor.isEmpty()) return this;
JetType substitutedType = substitutor.substitute(getType(), Variance.INVARIANT);
if (substitutedType == null) return null;
return new ReceiverParameterDescriptorImpl(getContainingDeclaration(), substitutedType, new TransientReceiver(substitutedType));
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitReceiverParameterDescriptor(this, data);
}
}
@@ -45,4 +45,6 @@ public interface DeclarationDescriptorVisitor<R, D> {
R visitPropertyGetterDescriptor(PropertyGetterDescriptor descriptor, D data);
R visitPropertySetterDescriptor(PropertySetterDescriptor descriptor, D data);
R visitReceiverParameterDescriptor(ReceiverParameterDescriptor descriptor, D data);
}
@@ -0,0 +1,95 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import java.util.Collections;
import java.util.List;
/*package*/ class NoReceiverParameter implements ReceiverParameterDescriptor {
/*package*/ static final ReceiverParameterDescriptor INSTANCE = new NoReceiverParameter();
private NoReceiverParameter() {}
@NotNull
@Override
public JetType getType() {
throw new UnsupportedOperationException("NO_RECEIVER_PARAMETER.getType()");
}
@NotNull
@Override
public ReceiverDescriptor getValue() {
return ReceiverDescriptor.NO_RECEIVER;
}
@NotNull
@Override
public DeclarationDescriptor getOriginal() {
return this;
}
@NotNull
@Override
public DeclarationDescriptor getContainingDeclaration() {
throw new UnsupportedOperationException("NO_RECEIVER_PARAMETER.getContainingDeclaration()");
}
@Nullable
@Override
public ReceiverParameterDescriptor substitute(TypeSubstitutor substitutor) {
return this;
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitReceiverParameterDescriptor(this, data);
}
@Override
public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
visitor.visitReceiverParameterDescriptor(this, null);
}
@Override
public boolean exists() {
return false;
}
@Override
public String toString() {
return "NO_RECEIVER_PARAMETER";
}
@Override
public List<AnnotationDescriptor> getAnnotations() {
return Collections.emptyList();
}
@NotNull
@Override
public Name getName() {
throw new UnsupportedOperationException("NO_RECEIVER_PARAMETER.getName()");
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
/**
* @author abreslav
*/
public interface ReceiverParameterDescriptor extends DeclarationDescriptor {
// NOTE: Instead of comparing to NO_RECEIVER_PARAMETER, call exists()
ReceiverParameterDescriptor NO_RECEIVER_PARAMETER = NoReceiverParameter.INSTANCE;
@NotNull
JetType getType();
@NotNull
ReceiverDescriptor getValue();
@Override
@NotNull
DeclarationDescriptor getContainingDeclaration();
boolean exists();
@Nullable
@Override
ReceiverParameterDescriptor substitute(TypeSubstitutor substitutor);
}
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
public class ReceiverParameterDescriptorImpl extends AbstractReceiverParameterDescriptor {
private final DeclarationDescriptor containingDeclaration;
private final JetType type;
private final ReceiverDescriptor value;
public ReceiverParameterDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull JetType type,
@NotNull ReceiverDescriptor value
) {
this.containingDeclaration = containingDeclaration;
this.type = type;
this.value = value;
}
@NotNull
@Override
public JetType getType() {
return type;
}
@NotNull
@Override
public ReceiverDescriptor getValue() {
return value;
}
@NotNull
@Override
public DeclarationDescriptor getContainingDeclaration() {
return containingDeclaration;
}
@Override
public boolean exists() {
return true;
}
}