FIR: extract BodyResolveContext.withAnonymousInitializer, withParameter
This commit is contained in:
+42
-12
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind
|
|||||||
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
|
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
|
||||||
import org.jetbrains.kotlin.fir.PrivateForInline
|
import org.jetbrains.kotlin.fir.PrivateForInline
|
||||||
import org.jetbrains.kotlin.fir.declarations.*
|
import org.jetbrains.kotlin.fir.declarations.*
|
||||||
|
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess
|
import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
||||||
import org.jetbrains.kotlin.fir.resolve.*
|
import org.jetbrains.kotlin.fir.resolve.*
|
||||||
@@ -281,7 +282,9 @@ class BodyResolveContext(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
withScopesForClass(regularClass, holder, f)
|
withScopesForClass(regularClass, holder) {
|
||||||
|
withContainer(regularClass, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,7 +293,9 @@ class BodyResolveContext(
|
|||||||
holder: SessionHolder,
|
holder: SessionHolder,
|
||||||
crossinline f: () -> T
|
crossinline f: () -> T
|
||||||
): T {
|
): T {
|
||||||
return withScopesForClass(anonymousObject, holder, f)
|
return withScopesForClass(anonymousObject, holder) {
|
||||||
|
withContainer(anonymousObject, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T> withScopesForClass(
|
inline fun <T> withScopesForClass(
|
||||||
@@ -383,7 +388,9 @@ class BodyResolveContext(
|
|||||||
withTowerDataCleanup {
|
withTowerDataCleanup {
|
||||||
addLocalScope(FirLocalScope())
|
addLocalScope(FirLocalScope())
|
||||||
val receiverTypeRef = simpleFunction.receiverTypeRef
|
val receiverTypeRef = simpleFunction.receiverTypeRef
|
||||||
withLabelAndReceiverType(simpleFunction.name, simpleFunction, receiverTypeRef?.coneType, holder, f)
|
withContainer(simpleFunction) {
|
||||||
|
withLabelAndReceiverType(simpleFunction.name, simpleFunction, receiverTypeRef?.coneType, holder, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -398,11 +405,13 @@ class BodyResolveContext(
|
|||||||
addLocalScope(FirLocalScope())
|
addLocalScope(FirLocalScope())
|
||||||
val receiverTypeRef = anonymousFunction.receiverTypeRef
|
val receiverTypeRef = anonymousFunction.receiverTypeRef
|
||||||
val labelName = anonymousFunction.label?.name?.let { Name.identifier(it) }
|
val labelName = anonymousFunction.label?.name?.let { Name.identifier(it) }
|
||||||
withLabelAndReceiverType(labelName, anonymousFunction, receiverTypeRef?.coneType, holder) {
|
withContainer(anonymousFunction) {
|
||||||
if (isInDependentContext) {
|
withLabelAndReceiverType(labelName, anonymousFunction, receiverTypeRef?.coneType, holder) {
|
||||||
withLambdaBeingAnalyzedInDependentContext(anonymousFunction.symbol, f)
|
if (isInDependentContext) {
|
||||||
} else {
|
withLambdaBeingAnalyzedInDependentContext(anonymousFunction.symbol, f)
|
||||||
f()
|
} else {
|
||||||
|
f()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -422,6 +431,25 @@ class BodyResolveContext(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <T> withAnonymousInitializer(
|
||||||
|
anonymousInitializer: FirAnonymousInitializer,
|
||||||
|
crossinline f: () -> T
|
||||||
|
): T {
|
||||||
|
return withTowerDataCleanup {
|
||||||
|
getPrimaryConstructorPureParametersScope()?.let { addLocalScope(it) }
|
||||||
|
addLocalScope(FirLocalScope())
|
||||||
|
withContainer(anonymousInitializer, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> withValueParameter(
|
||||||
|
valueParameter: FirValueParameter,
|
||||||
|
crossinline f: () -> T
|
||||||
|
): T {
|
||||||
|
storeVariable(valueParameter)
|
||||||
|
return withContainer(valueParameter, f)
|
||||||
|
}
|
||||||
|
|
||||||
inline fun <T> withProperty(
|
inline fun <T> withProperty(
|
||||||
property: FirProperty,
|
property: FirProperty,
|
||||||
crossinline f: () -> T
|
crossinline f: () -> T
|
||||||
@@ -433,9 +461,13 @@ class BodyResolveContext(
|
|||||||
|
|
||||||
inline fun <T> withPropertyAccessor(
|
inline fun <T> withPropertyAccessor(
|
||||||
property: FirProperty,
|
property: FirProperty,
|
||||||
|
accessor: FirPropertyAccessor,
|
||||||
holder: SessionHolder,
|
holder: SessionHolder,
|
||||||
crossinline f: () -> T
|
crossinline f: () -> T
|
||||||
): T {
|
): T {
|
||||||
|
if (accessor is FirDefaultPropertyAccessor || accessor.body == null) {
|
||||||
|
return withContainer(accessor, f)
|
||||||
|
}
|
||||||
return withTowerDataCleanup {
|
return withTowerDataCleanup {
|
||||||
val receiverTypeRef = property.receiverTypeRef
|
val receiverTypeRef = property.receiverTypeRef
|
||||||
addLocalScope(FirLocalScope())
|
addLocalScope(FirLocalScope())
|
||||||
@@ -444,10 +476,8 @@ class BodyResolveContext(
|
|||||||
) {
|
) {
|
||||||
storeBackingField(property)
|
storeBackingField(property)
|
||||||
}
|
}
|
||||||
if (receiverTypeRef != null) {
|
withContainer(accessor) {
|
||||||
withLabelAndReceiverType(property.name, property, receiverTypeRef.coneType, holder, f)
|
withLabelAndReceiverType(property.name, property, receiverTypeRef?.coneType, holder, f)
|
||||||
} else {
|
|
||||||
f()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-35
@@ -69,10 +69,8 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
declaration: FirDeclaration, data: ResolutionMode
|
declaration: FirDeclaration, data: ResolutionMode
|
||||||
): CompositeTransformResult<FirDeclaration> {
|
): CompositeTransformResult<FirDeclaration> {
|
||||||
transformer.onBeforeDeclarationContentResolve(declaration)
|
transformer.onBeforeDeclarationContentResolve(declaration)
|
||||||
return context.withContainer(declaration) {
|
transformer.replaceDeclarationResolvePhaseIfNeeded(declaration, transformerPhase)
|
||||||
transformer.replaceDeclarationResolvePhaseIfNeeded(declaration, transformerPhase)
|
return transformer.transformDeclarationContent(declaration, data)
|
||||||
transformer.transformDeclarationContent(declaration, data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun transformDeclarationStatus(
|
override fun transformDeclarationStatus(
|
||||||
@@ -327,24 +325,24 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
enhancedTypeRef: FirTypeRef,
|
enhancedTypeRef: FirTypeRef,
|
||||||
owner: FirProperty
|
owner: FirProperty
|
||||||
) {
|
) {
|
||||||
if (accessor is FirDefaultPropertyAccessor || accessor.body == null) {
|
context.withPropertyAccessor(owner, accessor, components) {
|
||||||
transformFunction(accessor, withExpectedType(enhancedTypeRef))
|
if (accessor is FirDefaultPropertyAccessor || accessor.body == null) {
|
||||||
return
|
transformFunction(accessor, withExpectedType(enhancedTypeRef))
|
||||||
}
|
} else {
|
||||||
val returnTypeRef = accessor.returnTypeRef
|
val returnTypeRef = accessor.returnTypeRef
|
||||||
val expectedReturnTypeRef = if (enhancedTypeRef is FirResolvedTypeRef && returnTypeRef !is FirResolvedTypeRef) {
|
val expectedReturnTypeRef = if (enhancedTypeRef is FirResolvedTypeRef && returnTypeRef !is FirResolvedTypeRef) {
|
||||||
enhancedTypeRef
|
enhancedTypeRef
|
||||||
} else {
|
} else {
|
||||||
returnTypeRef
|
returnTypeRef
|
||||||
}
|
}
|
||||||
val resolutionMode = if (expectedReturnTypeRef.coneTypeSafe<ConeKotlinType>() == session.builtinTypes.unitType.type) {
|
val resolutionMode = if (expectedReturnTypeRef.coneTypeSafe<ConeKotlinType>() == session.builtinTypes.unitType.type) {
|
||||||
ResolutionMode.ContextIndependent
|
ResolutionMode.ContextIndependent
|
||||||
} else {
|
} else {
|
||||||
withExpectedType(expectedReturnTypeRef)
|
withExpectedType(expectedReturnTypeRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
context.withPropertyAccessor(owner, components) {
|
transformFunctionWithGivenSignature(accessor, resolutionMode)
|
||||||
transformFunctionWithGivenSignature(accessor, resolutionMode)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -367,9 +365,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
}
|
}
|
||||||
|
|
||||||
doTransformTypeParameters(regularClass)
|
doTransformTypeParameters(regularClass)
|
||||||
return context.withRegularClass(regularClass, components) {
|
return doTransformRegularClass(regularClass, data)
|
||||||
doTransformRegularClass(regularClass, data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun doTransformRegularClass(
|
private fun doTransformRegularClass(
|
||||||
@@ -382,7 +378,9 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
dataFlowAnalyzer.enterClass()
|
dataFlowAnalyzer.enterClass()
|
||||||
}
|
}
|
||||||
|
|
||||||
val result = transformDeclarationContent(regularClass, data).single as FirRegularClass
|
val result = context.withRegularClass(regularClass, components) {
|
||||||
|
transformDeclarationContent(regularClass, data).single as FirRegularClass
|
||||||
|
}
|
||||||
|
|
||||||
if (notAnalyzed) {
|
if (notAnalyzed) {
|
||||||
if (!implicitTypeOnly) {
|
if (!implicitTypeOnly) {
|
||||||
@@ -393,8 +391,8 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (@Suppress("UNCHECKED_CAST")
|
(@Suppress("UNCHECKED_CAST")
|
||||||
result.compose())
|
return result.compose())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun transformAnonymousObject(
|
override fun transformAnonymousObject(
|
||||||
@@ -588,9 +586,8 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
data: ResolutionMode
|
data: ResolutionMode
|
||||||
): CompositeTransformResult<FirDeclaration> {
|
): CompositeTransformResult<FirDeclaration> {
|
||||||
if (implicitTypeOnly) return anonymousInitializer.compose()
|
if (implicitTypeOnly) return anonymousInitializer.compose()
|
||||||
return withPrimaryConstructorParameters(includeProperties = false) {
|
dataFlowAnalyzer.enterInitBlock(anonymousInitializer)
|
||||||
dataFlowAnalyzer.enterInitBlock(anonymousInitializer)
|
return context.withAnonymousInitializer(anonymousInitializer) {
|
||||||
addNewLocalScope()
|
|
||||||
val result =
|
val result =
|
||||||
transformDeclarationContent(anonymousInitializer, ResolutionMode.ContextIndependent).single as FirAnonymousInitializer
|
transformDeclarationContent(anonymousInitializer, ResolutionMode.ContextIndependent).single as FirAnonymousInitializer
|
||||||
val graph = dataFlowAnalyzer.exitInitBlock(result)
|
val graph = dataFlowAnalyzer.exitInitBlock(result)
|
||||||
@@ -600,20 +597,22 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun transformValueParameter(valueParameter: FirValueParameter, data: ResolutionMode): CompositeTransformResult<FirStatement> {
|
override fun transformValueParameter(valueParameter: FirValueParameter, data: ResolutionMode): CompositeTransformResult<FirStatement> {
|
||||||
context.storeVariable(valueParameter)
|
|
||||||
if (valueParameter.returnTypeRef is FirImplicitTypeRef) {
|
if (valueParameter.returnTypeRef is FirImplicitTypeRef) {
|
||||||
transformer.replaceDeclarationResolvePhaseIfNeeded(valueParameter, transformerPhase)
|
transformer.replaceDeclarationResolvePhaseIfNeeded(valueParameter, transformerPhase)
|
||||||
valueParameter.replaceReturnTypeRef(
|
valueParameter.replaceReturnTypeRef(
|
||||||
valueParameter.returnTypeRef.errorTypeFromPrototype(ConeSimpleDiagnostic("Unresolved value parameter type"))
|
valueParameter.returnTypeRef.errorTypeFromPrototype(ConeSimpleDiagnostic("Unresolved value parameter type"))
|
||||||
)
|
)
|
||||||
|
context.storeVariable(valueParameter)
|
||||||
return valueParameter.compose()
|
return valueParameter.compose()
|
||||||
}
|
}
|
||||||
|
|
||||||
dataFlowAnalyzer.enterValueParameter(valueParameter)
|
dataFlowAnalyzer.enterValueParameter(valueParameter)
|
||||||
val result = transformDeclarationContent(
|
val result = context.withValueParameter(valueParameter) {
|
||||||
valueParameter,
|
transformDeclarationContent(
|
||||||
withExpectedType(valueParameter.returnTypeRef)
|
valueParameter,
|
||||||
).single as FirValueParameter
|
withExpectedType(valueParameter.returnTypeRef)
|
||||||
|
).single as FirValueParameter
|
||||||
|
}
|
||||||
|
|
||||||
dataFlowAnalyzer.exitValueParameter(result)?.let { graph ->
|
dataFlowAnalyzer.exitValueParameter(result)?.let { graph ->
|
||||||
result.replaceControlFlowGraphReference(FirControlFlowGraphReferenceImpl(graph))
|
result.replaceControlFlowGraphReference(FirControlFlowGraphReferenceImpl(graph))
|
||||||
|
|||||||
Reference in New Issue
Block a user