WrappedType: introduce WrappedTypeFactory to encapsulate wrapped types creation

This commit is contained in:
Pavel V. Talanov
2017-02-09 15:33:53 +03:00
parent 85420d1ffd
commit bbe3b3cabe
11 changed files with 80 additions and 28 deletions
@@ -85,6 +85,7 @@ public class DescriptorResolver {
private final FunctionsTypingVisitor functionsTypingVisitor; private final FunctionsTypingVisitor functionsTypingVisitor;
private final DestructuringDeclarationResolver destructuringDeclarationResolver; private final DestructuringDeclarationResolver destructuringDeclarationResolver;
private final ModifiersChecker modifiersChecker; private final ModifiersChecker modifiersChecker;
private final WrappedTypeFactory wrappedTypeFactory;
private final SyntheticResolveExtension syntheticResolveExtension; private final SyntheticResolveExtension syntheticResolveExtension;
public DescriptorResolver( public DescriptorResolver(
@@ -100,6 +101,7 @@ public class DescriptorResolver {
@NotNull FunctionsTypingVisitor functionsTypingVisitor, @NotNull FunctionsTypingVisitor functionsTypingVisitor,
@NotNull DestructuringDeclarationResolver destructuringDeclarationResolver, @NotNull DestructuringDeclarationResolver destructuringDeclarationResolver,
@NotNull ModifiersChecker modifiersChecker, @NotNull ModifiersChecker modifiersChecker,
@NotNull WrappedTypeFactory wrappedTypeFactory,
@NotNull Project project @NotNull Project project
) { ) {
this.annotationResolver = annotationResolver; this.annotationResolver = annotationResolver;
@@ -114,6 +116,7 @@ public class DescriptorResolver {
this.functionsTypingVisitor = functionsTypingVisitor; this.functionsTypingVisitor = functionsTypingVisitor;
this.destructuringDeclarationResolver = destructuringDeclarationResolver; this.destructuringDeclarationResolver = destructuringDeclarationResolver;
this.modifiersChecker = modifiersChecker; this.modifiersChecker = modifiersChecker;
this.wrappedTypeFactory = wrappedTypeFactory;
this.syntheticResolveExtension = SyntheticResolveExtension.Companion.getInstance(project); this.syntheticResolveExtension = SyntheticResolveExtension.Companion.getInstance(project);
} }
@@ -928,7 +931,7 @@ public class DescriptorResolver {
} }
@Nullable @NotNull
/*package*/ static KotlinType transformAnonymousTypeIfNeeded( /*package*/ static KotlinType transformAnonymousTypeIfNeeded(
@NotNull DeclarationDescriptorWithVisibility descriptor, @NotNull DeclarationDescriptorWithVisibility descriptor,
@NotNull KtDeclaration declaration, @NotNull KtDeclaration declaration,
@@ -1104,14 +1107,14 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
/*package*/ DeferredType inferReturnTypeFromExpressionBody( /*package*/ KotlinType inferReturnTypeFromExpressionBody(
@NotNull final BindingTrace trace, @NotNull final BindingTrace trace,
@NotNull final LexicalScope scope, @NotNull final LexicalScope scope,
@NotNull final DataFlowInfo dataFlowInfo, @NotNull final DataFlowInfo dataFlowInfo,
@NotNull final KtDeclarationWithBody function, @NotNull final KtDeclarationWithBody function,
@NotNull final FunctionDescriptor functionDescriptor @NotNull final FunctionDescriptor functionDescriptor
) { ) {
return DeferredType.createRecursionIntolerant(storageManager, trace, new Function0<KotlinType>() { return wrappedTypeFactory.createRecursionIntolerantDeferredType(trace, new Function0<KotlinType>() {
@Override @Override
public KotlinType invoke() { public KotlinType invoke() {
PreliminaryDeclarationVisitor.Companion.createForDeclaration(function, trace); PreliminaryDeclarationVisitor.Companion.createForDeclaration(function, trace);
@@ -51,7 +51,6 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findFirstFromMeAndParent
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.resolve.source.toSourceElement import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.Variance.* import org.jetbrains.kotlin.types.Variance.*
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
@@ -63,11 +62,11 @@ class TypeResolver(
private val qualifiedExpressionResolver: QualifiedExpressionResolver, private val qualifiedExpressionResolver: QualifiedExpressionResolver,
private val moduleDescriptor: ModuleDescriptor, private val moduleDescriptor: ModuleDescriptor,
private val typeTransformerForTests: TypeTransformerForTests, private val typeTransformerForTests: TypeTransformerForTests,
private val storageManager: StorageManager,
private val lazinessToken: TypeLazinessToken, private val lazinessToken: TypeLazinessToken,
private val dynamicTypesSettings: DynamicTypesSettings, private val dynamicTypesSettings: DynamicTypesSettings,
private val dynamicCallableDescriptors: DynamicCallableDescriptors, private val dynamicCallableDescriptors: DynamicCallableDescriptors,
private val identifierChecker: IdentifierChecker, private val identifierChecker: IdentifierChecker,
private val wrappedTypeFactory: WrappedTypeFactory,
private val platformToKotlinClassMap: PlatformToKotlinClassMap, private val platformToKotlinClassMap: PlatformToKotlinClassMap,
private val languageVersionSettings: LanguageVersionSettings private val languageVersionSettings: LanguageVersionSettings
) { ) {
@@ -119,7 +118,7 @@ class TypeResolver(
if (!c.allowBareTypes && !c.forceResolveLazyTypes && lazinessToken.isLazy()) { if (!c.allowBareTypes && !c.forceResolveLazyTypes && lazinessToken.isLazy()) {
// Bare types can be allowed only inside expressions; lazy type resolution is only relevant for declarations // Bare types can be allowed only inside expressions; lazy type resolution is only relevant for declarations
val lazyKotlinType = LazyWrappedType(storageManager) { val lazyKotlinType = wrappedTypeFactory.createLazyWrappedType {
doResolvePossiblyBareType(c, typeReference).actualType doResolvePossiblyBareType(c, typeReference).actualType
} }
c.trace.record(resolvedTypeSlice, typeReference, lazyKotlinType) c.trace.record(resolvedTypeSlice, typeReference, lazyKotlinType)
@@ -28,10 +28,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.DeferredType import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices
import org.jetbrains.kotlin.types.expressions.PreliminaryDeclarationVisitor import org.jetbrains.kotlin.types.expressions.PreliminaryDeclarationVisitor
@@ -40,7 +37,8 @@ class VariableTypeAndInitializerResolver(
private val expressionTypingServices: ExpressionTypingServices, private val expressionTypingServices: ExpressionTypingServices,
private val typeResolver: TypeResolver, private val typeResolver: TypeResolver,
private val constantExpressionEvaluator: ConstantExpressionEvaluator, private val constantExpressionEvaluator: ConstantExpressionEvaluator,
private val delegatedPropertyResolver: DelegatedPropertyResolver private val delegatedPropertyResolver: DelegatedPropertyResolver,
private val wrappedTypeFactory: WrappedTypeFactory
) { ) {
companion object { companion object {
@JvmField @JvmField
@@ -82,8 +80,7 @@ class VariableTypeAndInitializerResolver(
variable.hasInitializer() -> when { variable.hasInitializer() -> when {
!local -> !local ->
DeferredType.createRecursionIntolerant( wrappedTypeFactory.createRecursionIntolerantDeferredType(
storageManager,
trace trace
) { ) {
PreliminaryDeclarationVisitor.createForDeclaration(variable, trace) PreliminaryDeclarationVisitor.createForDeclaration(variable, trace)
@@ -133,7 +130,7 @@ class VariableTypeAndInitializerResolver(
scopeForInitializer: LexicalScope, scopeForInitializer: LexicalScope,
dataFlowInfo: DataFlowInfo, dataFlowInfo: DataFlowInfo,
trace: BindingTrace trace: BindingTrace
) = DeferredType.createRecursionIntolerant(storageManager, trace) { ) = wrappedTypeFactory.createRecursionIntolerantDeferredType(trace) {
val delegateExpression = property.delegateExpression!! val delegateExpression = property.delegateExpression!!
val type = delegatedPropertyResolver.resolveDelegateExpression( val type = delegatedPropertyResolver.resolveDelegateExpression(
delegateExpression, property, variableDescriptor, scopeForInitializer, trace, dataFlowInfo) delegateExpression, property, variableDescriptor, scopeForInitializer, trace, dataFlowInfo)
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.WrappedTypeFactory
interface LazyClassContext { interface LazyClassContext {
val declarationScopeProvider: DeclarationScopeProvider val declarationScopeProvider: DeclarationScopeProvider
@@ -41,4 +42,5 @@ interface LazyClassContext {
val languageVersionSettings: LanguageVersionSettings val languageVersionSettings: LanguageVersionSettings
val syntheticResolveExtension: SyntheticResolveExtension val syntheticResolveExtension: SyntheticResolveExtension
val delegationFilter: DelegationFilter val delegationFilter: DelegationFilter
val wrappedTypeFactory: WrappedTypeFactory
} }
@@ -47,6 +47,7 @@ import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyPackageDescriptor;
import org.jetbrains.kotlin.resolve.scopes.LexicalScope; import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
import org.jetbrains.kotlin.resolve.scopes.MemberScope; import org.jetbrains.kotlin.resolve.scopes.MemberScope;
import org.jetbrains.kotlin.storage.*; import org.jetbrains.kotlin.storage.*;
import org.jetbrains.kotlin.types.WrappedTypeFactory;
import org.jetbrains.kotlin.utils.SmartList; import org.jetbrains.kotlin.utils.SmartList;
import javax.inject.Inject; import javax.inject.Inject;
@@ -82,6 +83,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
private SupertypeLoopChecker supertypeLoopsResolver; private SupertypeLoopChecker supertypeLoopsResolver;
private LanguageVersionSettings languageVersionSettings; private LanguageVersionSettings languageVersionSettings;
private DelegationFilter delegationFilter; private DelegationFilter delegationFilter;
private WrappedTypeFactory wrappedTypeFactory;
private final SyntheticResolveExtension syntheticResolveExtension; private final SyntheticResolveExtension syntheticResolveExtension;
@@ -141,6 +143,11 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
this.delegationFilter = delegationFilter; this.delegationFilter = delegationFilter;
} }
@Inject
public void setWrappedTypeFactory(WrappedTypeFactory wrappedTypeFactory) {
this.wrappedTypeFactory = wrappedTypeFactory;
}
// Only calls from injectors expected // Only calls from injectors expected
@Deprecated @Deprecated
public ResolveSession( public ResolveSession(
@@ -463,4 +470,10 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
public SyntheticResolveExtension getSyntheticResolveExtension() { public SyntheticResolveExtension getSyntheticResolveExtension() {
return syntheticResolveExtension; return syntheticResolveExtension;
} }
@NotNull
@Override
public WrappedTypeFactory getWrappedTypeFactory() {
return wrappedTypeFactory;
}
} }
@@ -368,7 +368,7 @@ open class LazyClassMemberScope(
} }
protected fun setDeferredReturnType(descriptor: ClassConstructorDescriptorImpl) { protected fun setDeferredReturnType(descriptor: ClassConstructorDescriptorImpl) {
descriptor.returnType = DeferredType.create(c.storageManager, trace, { thisDescriptor.defaultType }) descriptor.returnType = c.wrappedTypeFactory.createDeferredType(trace, { thisDescriptor.defaultType })
} }
override fun recordLookup(name: Name, from: LookupLocation) { override fun recordLookup(name: Name, from: LookupLocation) {
@@ -45,7 +45,7 @@ public class DeferredType extends WrappedType {
}; };
@NotNull @NotNull
public static DeferredType create( /*package private*/ static DeferredType create(
@NotNull StorageManager storageManager, @NotNull StorageManager storageManager,
@NotNull BindingTrace trace, @NotNull BindingTrace trace,
@NotNull Function0<KotlinType> compute @NotNull Function0<KotlinType> compute
@@ -56,7 +56,7 @@ public class DeferredType extends WrappedType {
} }
@NotNull @NotNull
public static DeferredType createRecursionIntolerant( /*package private*/ static DeferredType createRecursionIntolerant(
@NotNull StorageManager storageManager, @NotNull StorageManager storageManager,
@NotNull BindingTrace trace, @NotNull BindingTrace trace,
@NotNull Function0<KotlinType> compute @NotNull Function0<KotlinType> compute
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2017 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.kotlin.types
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.storage.StorageManager
open class WrappedTypeFactory(private val storageManager: StorageManager) {
open fun createLazyWrappedType(computation: () -> KotlinType): KotlinType = LazyWrappedType(storageManager, computation)
open fun createDeferredType(trace: BindingTrace, computation: () -> KotlinType): KotlinType = DeferredType.create(storageManager, trace, computation)
open fun createRecursionIntolerantDeferredType(trace: BindingTrace, computation: () -> KotlinType): KotlinType = DeferredType.createRecursionIntolerant(storageManager, trace, computation)
}
@@ -731,14 +731,14 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
final ClassDescriptor descriptor final ClassDescriptor descriptor
) { ) {
if (slice == CLASS && declaration == expression.getObjectDeclaration()) { if (slice == CLASS && declaration == expression.getObjectDeclaration()) {
KotlinType defaultType = DeferredType.createRecursionIntolerant(components.globalContext.getStorageManager(), KotlinType defaultType = components.wrappedTypeFactory.createRecursionIntolerantDeferredType(
context.trace, context.trace,
new Function0<KotlinType>() { new Function0<KotlinType>() {
@Override @Override
public KotlinType invoke() { public KotlinType invoke() {
return descriptor.getDefaultType(); return descriptor.getDefaultType();
} }
}); });
result[0] = defaultType; result[0] = defaultType;
} }
} }
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.resolve.calls.CallResolver;
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker; import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker;
import org.jetbrains.kotlin.resolve.calls.checkers.RttiExpressionChecker; import org.jetbrains.kotlin.resolve.calls.checkers.RttiExpressionChecker;
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
import org.jetbrains.kotlin.types.WrappedTypeFactory;
import javax.inject.Inject; import javax.inject.Inject;
@@ -60,6 +61,7 @@ public class ExpressionTypingComponents {
/*package*/ OverloadChecker overloadChecker; /*package*/ OverloadChecker overloadChecker;
/*package*/ LanguageVersionSettings languageVersionSettings; /*package*/ LanguageVersionSettings languageVersionSettings;
/*package*/ Iterable<RttiExpressionChecker> rttiExpressionCheckers; /*package*/ Iterable<RttiExpressionChecker> rttiExpressionCheckers;
/*package*/ WrappedTypeFactory wrappedTypeFactory;
@Inject @Inject
public void setGlobalContext(@NotNull GlobalContext globalContext) { public void setGlobalContext(@NotNull GlobalContext globalContext) {
@@ -200,4 +202,9 @@ public class ExpressionTypingComponents {
public void setRttiExpressionCheckers(@NotNull Iterable<RttiExpressionChecker> rttiExpressionCheckers) { public void setRttiExpressionCheckers(@NotNull Iterable<RttiExpressionChecker> rttiExpressionCheckers) {
this.rttiExpressionCheckers = rttiExpressionCheckers; this.rttiExpressionCheckers = rttiExpressionCheckers;
} }
@Inject
public void setWrappedTypeFactory(WrappedTypeFactory wrappedTypeFactory) {
this.wrappedTypeFactory = wrappedTypeFactory;
}
} }
@@ -52,6 +52,7 @@ import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope import org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.WrappedTypeFactory
class LocalClassifierAnalyzer( class LocalClassifierAnalyzer(
private val globalContext: GlobalContext, private val globalContext: GlobalContext,
@@ -65,7 +66,8 @@ class LocalClassifierAnalyzer(
private val supertypeLoopChecker: SupertypeLoopChecker, private val supertypeLoopChecker: SupertypeLoopChecker,
private val targetPlatformVersion: TargetPlatformVersion, private val targetPlatformVersion: TargetPlatformVersion,
private val languageVersionSettings: LanguageVersionSettings, private val languageVersionSettings: LanguageVersionSettings,
private val delegationFilter: DelegationFilter private val delegationFilter: DelegationFilter,
private val wrappedTypeFactory: WrappedTypeFactory
) { ) {
fun processClassOrObject( fun processClassOrObject(
scope: LexicalWritableScope?, scope: LexicalWritableScope?,
@@ -98,7 +100,8 @@ class LocalClassifierAnalyzer(
supertypeLoopChecker, supertypeLoopChecker,
languageVersionSettings, languageVersionSettings,
SyntheticResolveExtension.getInstance(project), SyntheticResolveExtension.getInstance(project),
delegationFilter delegationFilter,
wrappedTypeFactory
) )
) )
@@ -124,7 +127,8 @@ class LocalClassDescriptorHolder(
val supertypeLoopChecker: SupertypeLoopChecker, val supertypeLoopChecker: SupertypeLoopChecker,
val languageVersionSettings: LanguageVersionSettings, val languageVersionSettings: LanguageVersionSettings,
val syntheticResolveExtension: SyntheticResolveExtension, val syntheticResolveExtension: SyntheticResolveExtension,
val delegationFilter: DelegationFilter val delegationFilter: DelegationFilter,
val wrappedTypeFactory: WrappedTypeFactory
) { ) {
// We do not need to synchronize here, because this code is used strictly from one thread // We do not need to synchronize here, because this code is used strictly from one thread
private var classDescriptor: ClassDescriptor? = null private var classDescriptor: ClassDescriptor? = null
@@ -163,6 +167,7 @@ class LocalClassDescriptorHolder(
override val languageVersionSettings = this@LocalClassDescriptorHolder.languageVersionSettings override val languageVersionSettings = this@LocalClassDescriptorHolder.languageVersionSettings
override val syntheticResolveExtension = this@LocalClassDescriptorHolder.syntheticResolveExtension override val syntheticResolveExtension = this@LocalClassDescriptorHolder.syntheticResolveExtension
override val delegationFilter: DelegationFilter = this@LocalClassDescriptorHolder.delegationFilter override val delegationFilter: DelegationFilter = this@LocalClassDescriptorHolder.delegationFilter
override val wrappedTypeFactory: WrappedTypeFactory = this@LocalClassDescriptorHolder.wrappedTypeFactory
}, },
containingDeclaration, containingDeclaration,
classOrObject.nameAsSafeName, classOrObject.nameAsSafeName,