From 9c15a0429113e91b52112e6fcc0b1def22ef434e Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 18 Dec 2012 15:49:50 +0400 Subject: [PATCH] Pulled getUnsubstitutedPrimaryConstructor method to ClassDescriptor. Removed ClassDescriptorFromSource. Implemented method in inheritors. --- .../ClassDescriptorFromJvmBytecode.java | 7 +++++ .../scope/JavaClassNonStaticMembersScope.java | 28 +++++++++++++++++-- .../jet/lang/descriptors/ClassDescriptor.java | 3 ++ .../ClassDescriptorFromSource.java | 28 ------------------- .../lang/descriptors/ClassDescriptorImpl.java | 2 +- .../LazySubstitutingClassDescriptor.java | 7 +++++ .../descriptors/MutableClassDescriptor.java | 7 +++-- .../jet/lang/resolve/DeclarationsChecker.java | 2 +- .../resolve/lazy/LazyClassDescriptor.java | 2 +- .../k2js/translate/utils/BindingUtils.java | 3 +- 10 files changed, 50 insertions(+), 39 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorFromSource.java diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java index f91bd5ca0e3..b7a3a375a8d 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java @@ -17,6 +17,7 @@ package org.jetbrains.jet.lang.resolve.java.descriptor; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.ClassKind; import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; @@ -50,6 +51,12 @@ public class ClassDescriptorFromJvmBytecode extends MutableClassDescriptorLite { return scopeForConstructorResolve.getConstructors(); } + @Nullable + @Override + public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() { + return scopeForConstructorResolve.getPrimaryConstructor(); + } + public void setScopeForConstructorResolve(@NotNull JavaClassNonStaticMembersScope scopeForConstructorResolve) { this.scopeForConstructorResolve = scopeForConstructorResolve; } diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/scope/JavaClassNonStaticMembersScope.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/scope/JavaClassNonStaticMembersScope.java index 05c931ada09..0e9db8f86f5 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/scope/JavaClassNonStaticMembersScope.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/scope/JavaClassNonStaticMembersScope.java @@ -17,6 +17,7 @@ package org.jetbrains.jet.lang.resolve.java.scope; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices; @@ -27,6 +28,7 @@ import java.util.Collection; public final class JavaClassNonStaticMembersScope extends JavaClassMembersScope { private Collection constructors = null; + private ConstructorDescriptor primaryConstructor = null; @NotNull private final ClassDescriptor descriptor; @@ -42,9 +44,29 @@ public final class JavaClassNonStaticMembersScope extends JavaClassMembersScope @NotNull public Collection getConstructors() { - if (constructors == null) { - this.constructors = getResolver().resolveConstructors(declarationProvider, descriptor); - } + initConstructorsIfNeeded(); return constructors; } + + @Nullable + public ConstructorDescriptor getPrimaryConstructor() { + initConstructorsIfNeeded(); + return primaryConstructor; + } + + private void initConstructorsIfNeeded() { + if (constructors == null) { + constructors = getResolver().resolveConstructors(declarationProvider, descriptor); + + for (ConstructorDescriptor constructor : constructors) { + if (constructor.isPrimary()) { + if (primaryConstructor != null) { + throw new IllegalStateException( + "Class has more than one primary constructor: " + primaryConstructor + "\n" + constructor); + } + primaryConstructor = constructor; + } + } + } + } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java index ea82eb2c33e..9e039bad9ad 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java @@ -75,4 +75,7 @@ public interface ClassDescriptor extends ClassifierDescriptor, MemberDescriptor, @NotNull ReceiverParameterDescriptor getThisAsReceiverParameter(); + + @Nullable + ConstructorDescriptor getUnsubstitutedPrimaryConstructor(); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorFromSource.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorFromSource.java deleted file mode 100644 index 3513956a3f8..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorFromSource.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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; - -/** - * @author Stepan Koltsov - */ -public interface ClassDescriptorFromSource extends ClassDescriptor { - @Nullable - ConstructorDescriptor getUnsubstitutedPrimaryConstructor(); - -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java index 899dec8f2f4..538da037884 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java @@ -33,7 +33,7 @@ import java.util.Set; /** * @author abreslav */ -public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implements ClassDescriptorFromSource { +public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implements ClassDescriptor { private TypeConstructor typeConstructor; private JetScope memberDeclarations; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java index 58e9744f858..639a02b0743 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java @@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.descriptors; import com.google.common.collect.Lists; 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.JetScope; @@ -196,4 +197,10 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor { public JetScope getUnsubstitutedInnerClassesScope() { return original.getUnsubstitutedInnerClassesScope(); } + + @Nullable + @Override + public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() { + return original.getUnsubstitutedPrimaryConstructor(); + } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java index 7e131e04fed..c42494e2ba9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java @@ -36,7 +36,7 @@ import java.util.Set; /** * @author abreslav */ -public class MutableClassDescriptor extends MutableClassDescriptorLite implements ClassDescriptorFromSource { +public class MutableClassDescriptor extends MutableClassDescriptorLite { private final Set constructors = Sets.newLinkedHashSet(); private ConstructorDescriptor primaryConstructor; @@ -81,12 +81,13 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite implement ((ConstructorDescriptorImpl) constructorDescriptor).setReturnType(getDefaultType()); } - if (constructorDescriptor.isPrimary()) { + boolean primary = constructorDescriptor.isPrimary(); + if (primary) { setUpScopeForInitializers(constructorDescriptor); for (ValueParameterDescriptor valueParameterDescriptor : constructorDescriptor.getValueParameters()) { JetParameter parameter = (JetParameter) BindingContextUtils.descriptorToDeclaration(trace.getBindingContext(), valueParameterDescriptor); assert parameter != null; - if (parameter.getValOrVarNode() == null || !constructorDescriptor.isPrimary()) { + if (parameter.getValOrVarNode() == null) { getWritableScopeForInitializers().addVariableDescriptor(valueParameterDescriptor); } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationsChecker.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationsChecker.java index c8387e11037..ef061053ad9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationsChecker.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationsChecker.java @@ -323,7 +323,7 @@ public class DeclarationsChecker { private void checkEnumEntry(JetClass aClass, ClassDescriptor classDescriptor) { DeclarationDescriptor declaration = classDescriptor.getContainingDeclaration().getContainingDeclaration(); assert declaration instanceof ClassDescriptor; - ClassDescriptorFromSource enumClass = (ClassDescriptorFromSource) declaration; + ClassDescriptor enumClass = (ClassDescriptor) declaration; assert enumClass.getKind() == ClassKind.ENUM_CLASS; List delegationSpecifiers = aClass.getDelegationSpecifiers(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyClassDescriptor.java index a1fe0fc2202..bc0411986a7 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyClassDescriptor.java @@ -48,7 +48,7 @@ import static org.jetbrains.jet.lang.resolve.ModifiersChecker.*; /** * @author abreslav */ -public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDescriptor, ClassDescriptorFromSource { +public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDescriptor, ClassDescriptor { private static final Predicate ONLY_ENUM_ENTRIES = Predicates.instanceOf(JetEnumEntry.class); private static final Predicate VALID_SUPERTYPE = new Predicate() { diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/utils/BindingUtils.java b/js/js.translator/src/org/jetbrains/k2js/translate/utils/BindingUtils.java index 3ad647e5d7e..8327e53b57a 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/utils/BindingUtils.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/utils/BindingUtils.java @@ -282,8 +282,7 @@ public final class BindingUtils { public static ConstructorDescriptor getConstructor(@NotNull BindingContext bindingContext, @NotNull JetClassOrObject declaration) { - ConstructorDescriptor primaryConstructor = - ((ClassDescriptorFromSource) getClassDescriptor(bindingContext, declaration)).getUnsubstitutedPrimaryConstructor(); + ConstructorDescriptor primaryConstructor = getClassDescriptor(bindingContext, declaration).getUnsubstitutedPrimaryConstructor(); assert primaryConstructor != null : message(declaration, "Traits do not have initialize methods"); return primaryConstructor; }