FIR: refactor property & initializer resolve

This commit is contained in:
Mikhail Glukhikh
2021-03-01 18:32:40 +03:00
parent 130e3552ea
commit eca1256d7e
3 changed files with 40 additions and 36 deletions
@@ -181,6 +181,10 @@ class BodyResolveContext(
updateLastScope { storeVariable(variable) } updateLastScope { storeVariable(variable) }
} }
fun storeBackingField(property: FirProperty) {
updateLastScope { storeBackingField(property) }
}
fun saveContextForAnonymousFunction(anonymousFunction: FirAnonymousFunction) { fun saveContextForAnonymousFunction(anonymousFunction: FirAnonymousFunction) {
towerDataContextForAnonymousFunctions[anonymousFunction.symbol] = towerDataContext towerDataContextForAnonymousFunctions[anonymousFunction.symbol] = towerDataContext
} }
@@ -59,6 +59,16 @@ abstract class FirAbstractBodyResolveTransformer(phase: FirResolvePhase) : FirAb
} }
} }
protected inline fun <T> withPrimaryConstructorParameters(includeProperties: Boolean, crossinline l: () -> T): T {
return context.withTowerDataCleanup {
addLocalScope(
if (includeProperties) context.getPrimaryConstructorAllParametersScope()
else context.getPrimaryConstructorPureParametersScope()
)
l()
}
}
protected fun addNewLocalScope() { protected fun addNewLocalScope() {
context.addLocalScope(FirLocalScope()) context.addLocalScope(FirLocalScope())
} }
@@ -141,34 +141,31 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
} }
dataFlowAnalyzer.enterProperty(property) dataFlowAnalyzer.enterProperty(property)
withFullBodyResolve { withFullBodyResolve {
withLocalScopeCleanup { context.withContainer(property) {
val primaryConstructorParametersScope = context.getPrimaryConstructorPureParametersScope() withPrimaryConstructorParameters(includeProperties = false) {
context.withContainer(property) {
if (property.delegate != null) { if (property.delegate != null) {
addLocalScope(primaryConstructorParametersScope)
transformPropertyWithDelegate(property) transformPropertyWithDelegate(property)
} else { } else {
withLocalScopeCleanup { property.transformChildrenWithoutAccessors(returnTypeRef)
addLocalScope(primaryConstructorParametersScope)
property.transformChildrenWithoutAccessors(returnTypeRef)
}
if (property.initializer != null) { if (property.initializer != null) {
storeVariableReturnType(property) storeVariableReturnType(property)
} }
withLocalScopeCleanup {
if (property.receiverTypeRef == null && property.returnTypeRef !is FirImplicitTypeRef) {
addLocalScope(FirLocalScope().storeBackingField(property))
}
property.transformAccessors()
}
} }
} }
transformer.replaceDeclarationResolvePhaseIfNeeded(property, transformerPhase) if (property.delegate == null) {
dataFlowAnalyzer.exitProperty(property)?.let { withNewLocalScope {
property.replaceControlFlowGraphReference(FirControlFlowGraphReferenceImpl(it)) if (property.receiverTypeRef == null && property.returnTypeRef !is FirImplicitTypeRef) {
context.storeBackingField(property)
}
property.transformAccessors()
}
} }
property.compose()
} }
transformer.replaceDeclarationResolvePhaseIfNeeded(property, transformerPhase)
dataFlowAnalyzer.exitProperty(property)?.let {
property.replaceControlFlowGraphReference(FirControlFlowGraphReferenceImpl(it))
}
property.compose()
} }
} }
} }
@@ -183,23 +180,19 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
} }
dataFlowAnalyzer.enterField(field) dataFlowAnalyzer.enterField(field)
return withFullBodyResolve { return withFullBodyResolve {
withLocalScopeCleanup { context.withTowerDataMode(FirTowerDataMode.CONSTRUCTOR_HEADER) {
val primaryConstructorParametersScope = context.getPrimaryConstructorAllParametersScope() context.withContainer(field) {
context.withTowerDataMode(FirTowerDataMode.CONSTRUCTOR_HEADER) { withPrimaryConstructorParameters(includeProperties = true) {
context.withContainer(field) { field.transformChildren(transformer, withExpectedType(returnTypeRef))
withLocalScopeCleanup { }
addLocalScope(primaryConstructorParametersScope) if (field.initializer != null) {
field.transformChildren(transformer, withExpectedType(returnTypeRef)) storeVariableReturnType(field)
}
if (field.initializer != null) {
storeVariableReturnType(field)
}
} }
} }
transformer.replaceDeclarationResolvePhaseIfNeeded(field, transformerPhase)
dataFlowAnalyzer.exitField(field)
field.compose()
} }
transformer.replaceDeclarationResolvePhaseIfNeeded(field, transformerPhase)
dataFlowAnalyzer.exitField(field)
field.compose()
} }
} }
@@ -702,11 +695,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 withLocalScopeCleanup { return withPrimaryConstructorParameters(includeProperties = false) {
dataFlowAnalyzer.enterInitBlock(anonymousInitializer) dataFlowAnalyzer.enterInitBlock(anonymousInitializer)
addLocalScope(
context.getPrimaryConstructorPureParametersScope()
)
addNewLocalScope() addNewLocalScope()
val result = val result =
transformDeclarationContent(anonymousInitializer, ResolutionMode.ContextIndependent).single as FirAnonymousInitializer transformDeclarationContent(anonymousInitializer, ResolutionMode.ContextIndependent).single as FirAnonymousInitializer