Lazy values unified in the project
This commit is contained in:
@@ -42,8 +42,8 @@ import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.lexer.JetKeywordToken;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
import org.jetbrains.jet.util.RecursionIntolerantLazyValueWithDefault;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueWithDefault;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
@@ -313,9 +313,10 @@ public class DescriptorResolver {
|
||||
JetExpression bodyExpression = function.getBodyExpression();
|
||||
if (bodyExpression != null) {
|
||||
returnType =
|
||||
DeferredType.create(trace, new RecursionIntolerantLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
DeferredType.create(trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
JetType type = expressionTypingServices.getBodyExpressionType(trace, scope, dataFlowInfo, function, functionDescriptor);
|
||||
return transformAnonymousTypeIfNeeded(functionDescriptor, function, type, trace);
|
||||
}
|
||||
@@ -1027,9 +1028,10 @@ public class DescriptorResolver {
|
||||
if (hasDelegate && variableDescriptor instanceof PropertyDescriptor) {
|
||||
final JetExpression propertyDelegateExpression = ((JetProperty) variable).getDelegateExpression();
|
||||
if (propertyDelegateExpression != null) {
|
||||
return DeferredType.create(trace, new RecursionIntolerantLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
return DeferredType.create(trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
return resolveDelegatedPropertyType((PropertyDescriptor) variableDescriptor, scope,
|
||||
propertyDelegateExpression, dataFlowInfo, trace);
|
||||
}
|
||||
@@ -1043,9 +1045,9 @@ public class DescriptorResolver {
|
||||
}
|
||||
else {
|
||||
if (notLocal) {
|
||||
return DeferredType.create(trace, new RecursionIntolerantLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
return DeferredType.create(trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
JetType type = resolveInitializerType(scope, initializer, dataFlowInfo, trace);
|
||||
|
||||
return transformAnonymousTypeIfNeeded(variableDescriptor, variable, type, trace);
|
||||
@@ -1368,9 +1370,10 @@ public class DescriptorResolver {
|
||||
final ClassDescriptor enumClassDescriptor = (ClassDescriptor) classObject.getContainingDeclaration();
|
||||
assert DescriptorUtils.isEnumClass(enumClassDescriptor) : "values should be created in enum class: " + enumClassDescriptor;
|
||||
return DescriptorFactory
|
||||
.createEnumClassObjectValuesMethod(classObject, DeferredType.create(trace, new RecursionIntolerantLazyValue<JetType>() {
|
||||
.createEnumClassObjectValuesMethod(classObject, DeferredType.create(trace, new NotNullLazyValueImpl<JetType>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
return KotlinBuiltIns.getInstance().getArrayType(enumClassDescriptor.getDefaultType());
|
||||
}
|
||||
}));
|
||||
@@ -1385,9 +1388,10 @@ public class DescriptorResolver {
|
||||
final ClassDescriptor enumClassDescriptor = (ClassDescriptor) classObject.getContainingDeclaration();
|
||||
assert DescriptorUtils.isEnumClass(enumClassDescriptor) : "valueOf should be created in enum class: " + enumClassDescriptor;
|
||||
return DescriptorFactory
|
||||
.createEnumClassObjectValueOfMethod(classObject, DeferredType.create(trace, new RecursionIntolerantLazyValue<JetType>() {
|
||||
.createEnumClassObjectValueOfMethod(classObject, DeferredType.create(trace, new NotNullLazyValueImpl<JetType>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
return enumClassDescriptor.getDefaultType();
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.LazyScopeAdapter;
|
||||
import org.jetbrains.jet.lang.types.*;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
@@ -239,9 +239,10 @@ public class TypeResolver {
|
||||
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
|
||||
}
|
||||
else {
|
||||
return new LazyScopeAdapter(new RecursionIntolerantLazyValue<JetScope>() {
|
||||
return new LazyScopeAdapter(new NotNullLazyValueImpl<JetScope>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetScope compute() {
|
||||
protected JetScope doCompute() {
|
||||
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
|
||||
}
|
||||
});
|
||||
|
||||
+5
-4
@@ -29,13 +29,13 @@ import org.jetbrains.jet.lang.resolve.*;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.declarations.ClassMemberDeclarationProvider;
|
||||
import org.jetbrains.jet.storage.NullableLazyValue;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.DeferredType;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
||||
import org.jetbrains.jet.storage.NullableLazyValue;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -378,9 +378,10 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
|
||||
}
|
||||
|
||||
private void setDeferredReturnType(@NotNull ConstructorDescriptorImpl descriptor) {
|
||||
descriptor.setReturnType(DeferredType.create(resolveSession.getTrace(), new RecursionIntolerantLazyValue<JetType>() {
|
||||
descriptor.setReturnType(DeferredType.create(resolveSession.getTrace(), new NotNullLazyValueImpl<JetType>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
return thisDescriptor.getDefaultType();
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -20,8 +20,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
||||
import org.jetbrains.jet.util.Box;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.utils.ReenteringLazyValueComputationException;
|
||||
|
||||
import java.util.List;
|
||||
@@ -30,15 +30,15 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.DEFERRED_TYPE;
|
||||
|
||||
public class DeferredType implements JetType {
|
||||
|
||||
public static DeferredType create(BindingTrace trace, RecursionIntolerantLazyValue<JetType> lazyValue) {
|
||||
public static DeferredType create(BindingTrace trace, NotNullLazyValueImpl<JetType> lazyValue) {
|
||||
DeferredType deferredType = new DeferredType(lazyValue);
|
||||
trace.record(DEFERRED_TYPE, new Box<DeferredType>(deferredType));
|
||||
return deferredType;
|
||||
}
|
||||
|
||||
private final RecursionIntolerantLazyValue<JetType> lazyValue;
|
||||
private final NotNullLazyValueImpl<JetType> lazyValue;
|
||||
|
||||
private DeferredType(RecursionIntolerantLazyValue<JetType> lazyValue) {
|
||||
private DeferredType(NotNullLazyValueImpl<JetType> lazyValue) {
|
||||
this.lazyValue = lazyValue;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class DeferredType implements JetType {
|
||||
|
||||
@NotNull
|
||||
public JetType getActualType() {
|
||||
return lazyValue.get();
|
||||
return lazyValue.compute();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+4
-3
@@ -33,7 +33,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.*;
|
||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.util.RecursionIntolerantLazyValueWithDefault;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueWithDefault;
|
||||
import org.jetbrains.jet.util.slicedmap.WritableSlice;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -66,9 +66,10 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
|
||||
@Override
|
||||
public void handleRecord(WritableSlice<PsiElement, ClassDescriptor> slice, PsiElement declaration, final ClassDescriptor descriptor) {
|
||||
if (slice == CLASS && declaration == expression.getObjectDeclaration()) {
|
||||
JetType defaultType = DeferredType.create(context.trace, new RecursionIntolerantLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
JetType defaultType = DeferredType.create(context.trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetType compute() {
|
||||
protected JetType doCompute() {
|
||||
return descriptor.getDefaultType();
|
||||
}
|
||||
});
|
||||
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
|
||||
public abstract class RecursionIntolerantLazyValueWithDefault<T> extends RecursionIntolerantLazyValue<T> {
|
||||
private final T defaultValue;
|
||||
|
||||
protected RecursionIntolerantLazyValueWithDefault(T defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T getValueOnErrorReentry() {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -23,8 +23,6 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor;
|
||||
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValue;
|
||||
import org.jetbrains.jet.storage.StorageManager;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.LazyScopeAdapter;
|
||||
@@ -32,7 +30,9 @@ import org.jetbrains.jet.lang.types.*;
|
||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
||||
import org.jetbrains.jet.storage.StorageManager;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -198,9 +198,10 @@ public abstract class AbstractLazyTypeParameterDescriptor implements TypeParamet
|
||||
|
||||
@NotNull
|
||||
private JetType createDefaultType() {
|
||||
return new JetTypeImpl(getTypeConstructor(), new LazyScopeAdapter(new RecursionIntolerantLazyValue<JetScope>() {
|
||||
return new JetTypeImpl(getTypeConstructor(), new LazyScopeAdapter(new NotNullLazyValueImpl<JetScope>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetScope compute() {
|
||||
protected JetScope doCompute() {
|
||||
return getUpperBoundsAsType().getMemberScope();
|
||||
}
|
||||
}));
|
||||
|
||||
+4
-3
@@ -30,7 +30,7 @@ import org.jetbrains.jet.lang.types.*;
|
||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -222,9 +222,10 @@ public class TypeParameterDescriptorImpl extends DeclarationDescriptorNonRootImp
|
||||
getTypeConstructor(),
|
||||
TypeUtils.hasNullableLowerBound(this),
|
||||
Collections.<TypeProjection>emptyList(),
|
||||
new LazyScopeAdapter(new RecursionIntolerantLazyValue<JetScope>() {
|
||||
new LazyScopeAdapter(new NotNullLazyValueImpl<JetScope>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetScope compute() {
|
||||
protected JetScope doCompute() {
|
||||
return getUpperBoundsAsType().getMemberScope();
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -17,19 +17,19 @@
|
||||
package org.jetbrains.jet.lang.resolve.scopes;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.utils.RecursionIntolerantLazyValue;
|
||||
import org.jetbrains.jet.storage.NotNullLazyValue;
|
||||
|
||||
public class LazyScopeAdapter extends AbstractScopeAdapter {
|
||||
|
||||
private final RecursionIntolerantLazyValue<JetScope> scope;
|
||||
private final NotNullLazyValue<JetScope> scope;
|
||||
|
||||
public LazyScopeAdapter(RecursionIntolerantLazyValue<JetScope> scope) {
|
||||
public LazyScopeAdapter(NotNullLazyValue<JetScope> scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetScope getWorkerScope() {
|
||||
return scope.get();
|
||||
return scope.compute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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.utils;
|
||||
|
||||
public abstract class RecursionIntolerantLazyValue<T> {
|
||||
|
||||
private enum State {
|
||||
NOT_COMPUTED,
|
||||
BEING_COMPUTED,
|
||||
COMPUTED,
|
||||
ERROR
|
||||
}
|
||||
|
||||
private State state = State.NOT_COMPUTED;
|
||||
private T value;
|
||||
|
||||
protected abstract T compute();
|
||||
|
||||
protected T getValueOnErrorReentry() {
|
||||
throw new ReenteringLazyValueComputationException();
|
||||
}
|
||||
|
||||
public boolean isComputed() {
|
||||
return state == State.ERROR || state == State.COMPUTED;
|
||||
}
|
||||
|
||||
public final T get() {
|
||||
switch (state) {
|
||||
case NOT_COMPUTED:
|
||||
state = State.BEING_COMPUTED;
|
||||
value = compute();
|
||||
state = State.COMPUTED;
|
||||
return value;
|
||||
case BEING_COMPUTED:
|
||||
state = State.ERROR;
|
||||
throw new ReenteringLazyValueComputationException();
|
||||
case COMPUTED:
|
||||
return value;
|
||||
case ERROR:
|
||||
return getValueOnErrorReentry();
|
||||
}
|
||||
throw new IllegalStateException("Unreachable");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user