use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.
the class AbstractClassTypeConstructor method hashCode.
@Override
public final int hashCode() {
int currentHashCode = hashCode;
if (currentHashCode != 0)
return currentHashCode;
ClassifierDescriptor descriptor = getDeclarationDescriptor();
if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
currentHashCode = DescriptorUtils.getFqName(descriptor).hashCode();
} else {
currentHashCode = System.identityHashCode(this);
}
hashCode = currentHashCode;
return currentHashCode;
}
use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.
the class AbstractClassTypeConstructor method equals.
@Override
public boolean equals(Object other) {
if (!(other instanceof TypeConstructor))
return false;
// performance optimization: getFqName is slow method
if (other.hashCode() != hashCode())
return false;
// To avoid problems in type checker we suppose that it is different type constructors.
if (((TypeConstructor) other).getParameters().size() != getParameters().size())
return false;
ClassifierDescriptor myDescriptor = getDeclarationDescriptor();
ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor();
// descriptor for type is created once per module
if (myDescriptor == otherDescriptor)
return true;
// All error types have the same descriptor
if (!hasMeaningfulFqName(myDescriptor) || otherDescriptor != null && !hasMeaningfulFqName(otherDescriptor)) {
return this == other;
}
if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) {
FqNameUnsafe otherFqName = DescriptorUtils.getFqName(otherDescriptor);
FqNameUnsafe myFqName = DescriptorUtils.getFqName(myDescriptor);
return myFqName.equals(otherFqName);
}
return false;
}
use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.
the class CommonSupertypes method computeSupertypeProjections.
// constructor - type constructor of a supertype to be instantiated
// types - instantiations of constructor occurring as supertypes of classes we are trying to intersect
@NotNull
private static SimpleType computeSupertypeProjections(@NotNull TypeConstructor constructor, @NotNull Set<SimpleType> types, int recursionDepth, int maxDepth) {
assert !types.isEmpty();
if (types.size() == 1) {
return types.iterator().next();
}
List<TypeParameterDescriptor> parameters = constructor.getParameters();
List<TypeProjection> newProjections = new ArrayList<TypeProjection>(parameters.size());
for (TypeParameterDescriptor parameterDescriptor : parameters) {
Set<TypeProjection> typeProjections = new HashSet<TypeProjection>();
for (KotlinType type : types) {
typeProjections.add(type.getArguments().get(parameterDescriptor.getIndex()));
}
newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth));
}
boolean nullable = false;
for (KotlinType type : types) {
nullable |= type.isMarkedNullable();
}
ClassifierDescriptor classifier = constructor.getDeclarationDescriptor();
MemberScope newScope;
if (classifier instanceof ClassDescriptor) {
newScope = ((ClassDescriptor) classifier).getMemberScope(newProjections);
} else if (classifier instanceof TypeParameterDescriptor) {
newScope = classifier.getDefaultType().getMemberScope();
} else {
newScope = ErrorUtils.createErrorScope("A scope for common supertype which is not a normal classifier", true);
}
return KotlinTypeFactory.simpleType(Annotations.Companion.getEMPTY(), constructor, newProjections, nullable, newScope);
}
use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.
the class TypeSubstitutorTest method stringsToSubstitutionMap.
private Map<TypeConstructor, TypeProjection> stringsToSubstitutionMap(Pair<String, String>[] substitutionStrs) {
Map<TypeConstructor, TypeProjection> map = Maps.newHashMap();
for (Pair<String, String> pair : substitutionStrs) {
String typeParameterName = pair.first;
String replacementProjectionString = pair.second;
ClassifierDescriptor classifier = ScopeUtilsKt.findClassifier(scope, Name.identifier(typeParameterName), NoLookupLocation.FROM_TEST);
assertNotNull("No type parameter named " + typeParameterName, classifier);
assertTrue(typeParameterName + " is not a type parameter: " + classifier, classifier instanceof TypeParameterDescriptor);
String typeStr = "C<" + replacementProjectionString + ">";
KotlinType typeWithArgument = resolveType(typeStr);
assert !typeWithArgument.getArguments().isEmpty() : "No arguments: " + typeWithArgument + " from " + typeStr;
map.put(classifier.getTypeConstructor(), typeWithArgument.getArguments().get(0));
}
return map;
}
use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.
the class TypeSubstitutorTest method getContextScope.
private LexicalScope getContextScope() throws IOException {
// todo comments
String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
AnalysisResult analysisResult = JvmResolveUtil.analyze(ktFile, getEnvironment());
ModuleDescriptor module = analysisResult.getModuleDescriptor();
LexicalScope topLevelScope = analysisResult.getBindingContext().get(BindingContext.LEXICAL_SCOPE, ktFile);
final ClassifierDescriptor contextClass = ScopeUtilsKt.findClassifier(topLevelScope, Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
assert contextClass instanceof ClassDescriptor;
LocalRedeclarationChecker redeclarationChecker = new ThrowingLocalRedeclarationChecker(new OverloadChecker(TypeSpecificityComparator.NONE.INSTANCE));
LexicalScope typeParameters = new LexicalScopeImpl(topLevelScope, module, false, null, LexicalScopeKind.SYNTHETIC, redeclarationChecker, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
for (TypeParameterDescriptor parameterDescriptor : contextClass.getTypeConstructor().getParameters()) {
handler.addClassifierDescriptor(parameterDescriptor);
}
return Unit.INSTANCE;
}
});
return new LexicalChainedScope(typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC, Arrays.asList(contextClass.getDefaultType().getMemberScope(), module.getBuiltIns().getBuiltInsPackageScope()));
}
Aggregations