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) }
}
fun storeBackingField(property: FirProperty) {
updateLastScope { storeBackingField(property) }
}
fun saveContextForAnonymousFunction(anonymousFunction: FirAnonymousFunction) {
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() {
context.addLocalScope(FirLocalScope())
}
@@ -141,34 +141,31 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
}
dataFlowAnalyzer.enterProperty(property)
withFullBodyResolve {
withLocalScopeCleanup {
val primaryConstructorParametersScope = context.getPrimaryConstructorPureParametersScope()
context.withContainer(property) {
context.withContainer(property) {
withPrimaryConstructorParameters(includeProperties = false) {
if (property.delegate != null) {
addLocalScope(primaryConstructorParametersScope)
transformPropertyWithDelegate(property)
} else {
withLocalScopeCleanup {
addLocalScope(primaryConstructorParametersScope)
property.transformChildrenWithoutAccessors(returnTypeRef)
}
property.transformChildrenWithoutAccessors(returnTypeRef)
if (property.initializer != null) {
storeVariableReturnType(property)
}
withLocalScopeCleanup {
if (property.receiverTypeRef == null && property.returnTypeRef !is FirImplicitTypeRef) {
addLocalScope(FirLocalScope().storeBackingField(property))
}
property.transformAccessors()
}
}
}
transformer.replaceDeclarationResolvePhaseIfNeeded(property, transformerPhase)
dataFlowAnalyzer.exitProperty(property)?.let {
property.replaceControlFlowGraphReference(FirControlFlowGraphReferenceImpl(it))
if (property.delegate == null) {
withNewLocalScope {
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)
return withFullBodyResolve {
withLocalScopeCleanup {
val primaryConstructorParametersScope = context.getPrimaryConstructorAllParametersScope()
context.withTowerDataMode(FirTowerDataMode.CONSTRUCTOR_HEADER) {
context.withContainer(field) {
withLocalScopeCleanup {
addLocalScope(primaryConstructorParametersScope)
field.transformChildren(transformer, withExpectedType(returnTypeRef))
}
if (field.initializer != null) {
storeVariableReturnType(field)
}
context.withTowerDataMode(FirTowerDataMode.CONSTRUCTOR_HEADER) {
context.withContainer(field) {
withPrimaryConstructorParameters(includeProperties = true) {
field.transformChildren(transformer, withExpectedType(returnTypeRef))
}
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
): CompositeTransformResult<FirDeclaration> {
if (implicitTypeOnly) return anonymousInitializer.compose()
return withLocalScopeCleanup {
return withPrimaryConstructorParameters(includeProperties = false) {
dataFlowAnalyzer.enterInitBlock(anonymousInitializer)
addLocalScope(
context.getPrimaryConstructorPureParametersScope()
)
addNewLocalScope()
val result =
transformDeclarationContent(anonymousInitializer, ResolutionMode.ContextIndependent).single as FirAnonymousInitializer