From 67ca049683e58cedbf75dfbd12002e2e3e621cb3 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 19 Dec 2011 21:36:03 +0400 Subject: [PATCH] TypeParameterDescriptor.initialized Internal state checks Also add a missing initialization --- .../resolve/java/JavaDescriptorResolver.java | 11 ++-- .../descriptors/TypeParameterDescriptor.java | 51 ++++++++++++++++++- .../jet/lang/resolve/DescriptorResolver.java | 6 +-- .../jet/lang/types/DescriptorSubstitutor.java | 1 + 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java index 9897b8a3717..33063ff9b18 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java @@ -334,6 +334,7 @@ public class JavaDescriptorResolver { typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedType)); } } + typeParameterDescriptor.setInitialized(); } private void initializeTypeParameters(PsiTypeParameterListOwner typeParameterListOwner) { @@ -625,7 +626,9 @@ public class JavaDescriptorResolver { if (attributeValue != null) { String typeParametersString = (String) attributeValue.getValue(); if (typeParametersString != null) { - return resolveMethodTypeParametersFromJetSignature(typeParametersString, functionDescriptorImpl); + List r = resolveMethodTypeParametersFromJetSignature(typeParametersString, method, functionDescriptorImpl); + initializeTypeParameters(method); + return r; } } } @@ -637,9 +640,9 @@ public class JavaDescriptorResolver { } /** - * @see #resolveClassTypeParametersFromJetSignature(String, JavaClassDescriptor) + * @see #resolveClassTypeParametersFromJetSignature(String, com.intellij.psi.PsiClass, JavaClassDescriptor) */ - private List resolveMethodTypeParametersFromJetSignature(String jetSignature, final FunctionDescriptor functionDescriptor) { + private List resolveMethodTypeParametersFromJetSignature(String jetSignature, final PsiMethod method, final FunctionDescriptor functionDescriptor) { final List r = new ArrayList(); new JetSignatureReader(jetSignature).acceptFormalTypeParametersOnly(new JetSignatureExceptionsAdapter() { @Override @@ -677,6 +680,8 @@ public class JavaDescriptorResolver { JetSignatureUtils.translateVariance(variance), name, ++index); + PsiTypeParameter psiTypeParameter = getPsiTypeParameterByName(method, method.getName(), name); + typeParameterDescriptorCache.put(psiTypeParameter, typeParameter); r.add(typeParameter); } }; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/TypeParameterDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/TypeParameterDescriptor.java index 1edf93c8743..bbd1e6899b9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/TypeParameterDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/TypeParameterDescriptor.java @@ -28,6 +28,7 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement int index) { TypeParameterDescriptor typeParameterDescriptor = createForFurtherModification(containingDeclaration, annotations, reified, variance, name, index); typeParameterDescriptor.addUpperBound(JetStandardClasses.getDefaultBound()); + typeParameterDescriptor.setInitialized(); return typeParameterDescriptor; } @@ -52,6 +53,8 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement private final boolean reified; + private boolean initialized = false; + private TypeParameterDescriptor( @NotNull DeclarationDescriptor containingDeclaration, @NotNull List annotations, @@ -74,25 +77,61 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement upperBounds); } + public void setInitialized() { + if (initialized) { + throw new IllegalStateException(); + } + initialized = true; + } + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException(); + } + } + + private void checkUninitialized() { + if (initialized) { + throw new IllegalStateException(); + } + } + public boolean isReified() { + checkInitialized(); return reified; } public Variance getVariance() { + checkInitialized(); return variance; } public void addUpperBound(@NotNull JetType bound) { + checkUninitialized(); + doAddUpperBound(bound); + } + + private void doAddUpperBound(JetType bound) { upperBounds.add(bound); // TODO : Duplicates? } + public void addDefaultUpperBound() { + checkUninitialized(); + + if (upperBounds.isEmpty()) { + doAddUpperBound(JetStandardClasses.getDefaultBound()); + } + } + @NotNull public Set getUpperBounds() { + checkInitialized(); return upperBounds; } @NotNull public JetType getUpperBoundsAsType() { + checkInitialized(); if (upperBoundsAsType == null) { assert upperBounds != null : "Upper bound list is null in " + getName(); assert upperBounds.size() > 0 : "Upper bound list is empty in " + getName(); @@ -106,11 +145,13 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement @NotNull public Set getLowerBounds() { + //checkInitialized(); return Collections.singleton(JetStandardClasses.getNothingType()); } @NotNull public JetType getLowerBoundsAsType() { + checkInitialized(); return JetStandardClasses.getNothingType(); } @@ -118,6 +159,7 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement @NotNull @Override public TypeConstructor getTypeConstructor() { + //checkInitialized(); return typeConstructor; } @@ -135,12 +177,14 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement @Override public R accept(DeclarationDescriptorVisitor visitor, D data) { + checkInitialized(); return visitor.visitTypeParameterDescriptor(this, data); } @NotNull @Override public JetType getDefaultType() { + //checkInitialized(); if (defaultType == null) { defaultType = new JetTypeImpl( Collections.emptyList(), @@ -159,6 +203,7 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement @Override public JetType getClassObjectType() { + checkInitialized(); if (classObjectUpperBounds.isEmpty()) return null; if (classObjectBoundsAsType == null) { @@ -176,15 +221,19 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement } public void addClassObjectBound(@NotNull JetType bound) { + checkUninitialized(); classObjectUpperBounds.add(bound); // TODO : Duplicates? } public int getIndex() { + checkInitialized(); return index; } @NotNull public TypeParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner) { - return new TypeParameterDescriptor(newOwner, Lists.newArrayList(getAnnotations()), reified, variance, getName(), index); + TypeParameterDescriptor copy = new TypeParameterDescriptor(newOwner, Lists.newArrayList(getAnnotations()), reified, variance, getName(), index); + copy.initialized = this.initialized; + return copy; } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java index c5df77d3682..740ff3ed692 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java @@ -381,9 +381,9 @@ public class DescriptorResolver { } for (TypeParameterDescriptor parameter : parameters) { - if (parameter.getUpperBounds().isEmpty()) { - parameter.addUpperBound(JetStandardClasses.getDefaultBound()); - } + parameter.addDefaultUpperBound(); + + parameter.setInitialized(); if (JetStandardClasses.isNothing(parameter.getUpperBoundsAsType())) { PsiElement nameIdentifier = typeParameters.get(parameter.getIndex()).getNameIdentifier(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/DescriptorSubstitutor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/DescriptorSubstitutor.java index b31f101c402..7101dfda584 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/DescriptorSubstitutor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/DescriptorSubstitutor.java @@ -44,6 +44,7 @@ public class DescriptorSubstitutor { descriptor.getVariance(), descriptor.getName(), descriptor.getIndex()); + substituted.setInitialized(); mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjection(substituted.getDefaultType()));