From 78dfadb10bb28faadd2639d2bde0c45aaad9db61 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Sat, 24 Sep 2011 11:40:55 +0400 Subject: [PATCH] Object constructors fixed --- .../jet/lang/resolve/DeclarationResolver.java | 2 +- .../lang/resolve/TypeHierarchyResolver.java | 11 ++++++++--- .../jet/lang/types/JetTypeInferrer.java | 19 ++++++++----------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java index e15fa73e1cd..985064fd161 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java @@ -128,7 +128,7 @@ public class DeclarationResolver { } private void processPrimaryConstructor(MutableClassDescriptor classDescriptor, JetClass klass) { - if (!klass.hasPrimaryConstructor() && classDescriptor.getKind() != ClassKind.OBJECT) return; + if (!klass.hasPrimaryConstructor()) return; if (classDescriptor.getKind() == ClassKind.TRAIT) { // context.getTrace().getErrorHandler().genericError(klass.getPrimaryConstructorParameterList().getNode(), "A trait may not have a constructor"); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java index 56b18bbb19e..fbf91dd2808 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeHierarchyResolver.java @@ -7,6 +7,7 @@ import com.intellij.lang.ASTNode; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiNameIdentifierOwner; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.cfg.JetFlowInformationProvider; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; @@ -20,6 +21,7 @@ import org.jetbrains.jet.lexer.JetTokens; import java.util.*; import static org.jetbrains.jet.lang.diagnostics.Errors.*; +import static org.jetbrains.jet.lang.resolve.BindingContext.CONSTRUCTOR; import static org.jetbrains.jet.lang.resolve.BindingContext.DESCRIPTOR_TO_DECLARATION; import static org.jetbrains.jet.lang.resolve.BindingContext.TYPE; @@ -98,7 +100,7 @@ public class TypeHierarchyResolver { classObjectDescriptor.setModality(Modality.FINAL); classObjectDescriptor.setVisibility(ClassDescriptorResolver.resolveVisibilityFromModifiers(context.getTrace(), klass.getModifierList())); classObjectDescriptor.createTypeConstructor(); - createPrimaryConstructor(classObjectDescriptor); + createPrimaryConstructorForObject(null, classObjectDescriptor); mutableClassDescriptor.setClassObjectDescriptor(classObjectDescriptor); } visitClassOrObject( @@ -144,17 +146,20 @@ public class TypeHierarchyResolver { } }; visitClassOrObject(declaration, (Map) context.getObjects(), owner, outerScope, mutableClassDescriptor); - createPrimaryConstructor(mutableClassDescriptor); + createPrimaryConstructorForObject((JetDeclaration) declaration, mutableClassDescriptor); context.getTrace().record(BindingContext.CLASS, declaration, mutableClassDescriptor); return mutableClassDescriptor; } - private void createPrimaryConstructor(MutableClassDescriptor mutableClassDescriptor) { + private void createPrimaryConstructorForObject(@Nullable JetDeclaration object, MutableClassDescriptor mutableClassDescriptor) { ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(mutableClassDescriptor, Collections.emptyList(), true); constructorDescriptor.initialize(Collections.emptyList(), Collections.emptyList(), Modality.FINAL, Visibility.INTERNAL);//TODO check set mutableClassDescriptor.getVisibility() // TODO : make the constructor private? mutableClassDescriptor.setPrimaryConstructor(constructorDescriptor); + if (object != null) { + context.getTrace().record(CONSTRUCTOR, object, constructorDescriptor); + } } private void visitClassOrObject(@NotNull JetClassOrObject declaration, Map map, NamespaceLike owner, JetScope outerScope, MutableClassDescriptor mutableClassDescriptor) { diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java index b0d8058f0d7..7da4d79b325 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java @@ -875,30 +875,27 @@ public class JetTypeInferrer { @Override public JetType visitObjectLiteralExpression(final JetObjectLiteralExpression expression, final TypeInferenceContext context) { final JetType[] result = new JetType[1]; - BindingTraceAdapter.RecordHandler handler = new BindingTraceAdapter.RecordHandler() { + BindingTraceAdapter.RecordHandler handler = new BindingTraceAdapter.RecordHandler() { @Override - public void handleRecord(WritableSlice slice, PsiElement declaration, final DeclarationDescriptor descriptor) { - if (declaration == expression.getObjectDeclaration()) { + public void handleRecord(WritableSlice slice, PsiElement declaration, final ClassDescriptor descriptor) { + if (slice == CLASS && declaration == expression.getObjectDeclaration()) { JetType defaultType = new DeferredType(new LazyValue() { @Override protected JetType compute() { - return ((ClassDescriptor) descriptor).getDefaultType(); + return descriptor.getDefaultType(); } }); result[0] = defaultType; - if (!context.trace.get(BindingContext.PROCESSED, expression)) { - context.trace.record(BindingContext.EXPRESSION_TYPE, expression, defaultType); - context.trace.record(BindingContext.PROCESSED, expression); + if (!context.trace.get(PROCESSED, expression)) { + context.trace.record(EXPRESSION_TYPE, expression, defaultType); + context.trace.record(PROCESSED, expression); } } } }; BindingTraceAdapter traceAdapter = new BindingTraceAdapter(context.trace); - for (WritableSlice slice : BindingContext.DECLARATIONS_TO_DESCRIPTORS) { - //noinspection unchecked - traceAdapter.addHandler(slice, handler); - } + traceAdapter.addHandler(CLASS, handler); TopDownAnalyzer.processObject(semanticServices, traceAdapter, context.scope, context.scope.getContainingDeclaration(), expression.getObjectDeclaration()); return context.services.checkType(result[0], expression, context); }