org.jetbrains.jet.lang.descriptors.impl.AbstractClassDescriptor Maven / Gradle / Ivy
/*
* Copyright 2010-2013 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.impl;
import kotlin.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.InnerClassesScopeWrapper;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.SubstitutingScope;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isEnumClass;
public abstract class AbstractClassDescriptor implements ClassDescriptor {
private final Name name;
protected final NotNullLazyValue defaultType;
private final NotNullLazyValue unsubstitutedInnerClassesScope;
private final NotNullLazyValue thisAsReceiverParameter;
public AbstractClassDescriptor(@NotNull StorageManager storageManager, @NotNull Name name) {
this.name = name;
this.defaultType = storageManager.createLazyValue(new Function0() {
@Override
public JetType invoke() {
return TypeUtils.makeUnsubstitutedType(AbstractClassDescriptor.this, getScopeForMemberLookup());
}
});
this.unsubstitutedInnerClassesScope = storageManager.createLazyValue(new Function0() {
@Override
public JetScope invoke() {
return new InnerClassesScopeWrapper(getScopeForMemberLookup());
}
});
this.thisAsReceiverParameter = storageManager.createLazyValue(new Function0() {
@Override
public ReceiverParameterDescriptor invoke() {
return new LazyClassReceiverParameterDescriptor(AbstractClassDescriptor.this);
}
});
}
@NotNull
@Override
public Name getName() {
return name;
}
@NotNull
@Override
public DeclarationDescriptor getOriginal() {
return this;
}
@NotNull
protected abstract JetScope getScopeForMemberLookup();
@Nullable
@Override
public JetType getClassObjectType() {
if (getKind() == ClassKind.OBJECT) {
return getDefaultType();
}
if (getKind() == ClassKind.ENUM_ENTRY) {
DeclarationDescriptor enumClass = getContainingDeclaration();
assert isEnumClass(enumClass) : "Enum entry should be declared in enum class: " + this;
return ((ClassDescriptor) enumClass).getDefaultType();
}
ClassDescriptor classObject = getClassObjectDescriptor();
return classObject == null ? null : classObject.getDefaultType();
}
@NotNull
@Override
public JetScope getUnsubstitutedInnerClassesScope() {
return unsubstitutedInnerClassesScope.invoke();
}
@NotNull
@Override
public ReceiverParameterDescriptor getThisAsReceiverParameter() {
return thisAsReceiverParameter.invoke();
}
@NotNull
@Override
public JetScope getMemberScope(@NotNull List typeArguments) {
assert typeArguments.size() == getTypeConstructor().getParameters().size() : "Illegal number of type arguments: expected "
+ getTypeConstructor().getParameters().size() + " but was " + typeArguments.size()
+ " for " + getTypeConstructor() + " " + getTypeConstructor().getParameters();
if (typeArguments.isEmpty()) return getScopeForMemberLookup();
List typeParameters = getTypeConstructor().getParameters();
Map substitutionContext = TypeSubstitutor.buildSubstitutionContext(typeParameters, typeArguments);
TypeSubstitutor substitutor = TypeSubstitutor.create(substitutionContext);
return new SubstitutingScope(getScopeForMemberLookup(), substitutor);
}
@NotNull
@Override
public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
if (substitutor.isEmpty()) {
return this;
}
return new LazySubstitutingClassDescriptor(this, substitutor);
}
@NotNull
@Override
public JetType getDefaultType() {
return defaultType.invoke();
}
@Override
public void acceptVoid(DeclarationDescriptorVisitor visitor) {
visitor.visitClassDescriptor(this, null);
}
@Override
public R accept(DeclarationDescriptorVisitor visitor, D data) {
return visitor.visitClassDescriptor(this, data);
}
}