[FIR2IR] Refactor signatures of Fir2IrConversionScope.withXxx functions

Usually functions like `withSomething` are considered to return the value
  of lambda which is passed to it, not one of its arguments
This commit is contained in:
Dmitriy Novozhilov
2023-07-17 16:55:21 +03:00
committed by Space Team
parent 0b502f08d5
commit ccb0d3cb0c
3 changed files with 35 additions and 24 deletions
@@ -20,12 +20,13 @@ class Fir2IrConversionScope {
private val containingFirClassStack = mutableListOf<FirClass>() private val containingFirClassStack = mutableListOf<FirClass>()
private val currentlyGeneratedDelegatedConstructors = mutableMapOf<IrClass, IrConstructor>() private val currentlyGeneratedDelegatedConstructors = mutableMapOf<IrClass, IrConstructor>()
fun <T : IrDeclarationParent?> withParent(parent: T, f: T.() -> Unit): T { fun <T : IrDeclarationParent, R> withParent(parent: T, f: T.() -> R): R {
if (parent == null) return parent
parentStack += parent parentStack += parent
parent.f() try {
parentStack.removeAt(parentStack.size - 1) return parent.f()
return parent } finally {
parentStack.removeAt(parentStack.size - 1)
}
} }
fun <T> forDelegatingConstructorCall(constructor: IrConstructor, irClass: IrClass, f: () -> T): T { fun <T> forDelegatingConstructorCall(constructor: IrConstructor, irClass: IrClass, f: () -> T): T {
@@ -67,29 +68,35 @@ class Fir2IrConversionScope {
private val functionStack = mutableListOf<IrFunction>() private val functionStack = mutableListOf<IrFunction>()
fun <T : IrFunction> withFunction(function: T, f: T.() -> Unit): T { fun <T : IrFunction, R> withFunction(function: T, f: T.() -> R): R {
functionStack += function functionStack += function
function.f() try {
functionStack.removeAt(functionStack.size - 1) return function.f()
return function } finally {
functionStack.removeAt(functionStack.size - 1)
}
} }
private val propertyStack = mutableListOf<Pair<IrProperty, FirProperty?>>() private val propertyStack = mutableListOf<Pair<IrProperty, FirProperty?>>()
fun withProperty(property: IrProperty, firProperty: FirProperty? = null, f: IrProperty.() -> Unit): IrProperty { fun <R> withProperty(property: IrProperty, firProperty: FirProperty? = null, f: IrProperty.() -> R): R {
propertyStack += (property to firProperty) propertyStack += (property to firProperty)
property.f() try {
propertyStack.removeAt(propertyStack.size - 1) return property.f()
return property } finally {
propertyStack.removeAt(propertyStack.size - 1)
}
} }
private val classStack = mutableListOf<IrClass>() private val classStack = mutableListOf<IrClass>()
fun withClass(klass: IrClass, f: IrClass.() -> Unit): IrClass { fun <R> withClass(klass: IrClass, f: IrClass.() -> R): R {
classStack += klass classStack += klass
klass.f() return try {
classStack.removeAt(classStack.size - 1) klass.f()
return klass } finally {
classStack.removeAt(classStack.size - 1)
}
} }
private val whenSubjectVariableStack = mutableListOf<IrVariable>() private val whenSubjectVariableStack = mutableListOf<IrVariable>()
@@ -105,13 +105,15 @@ class Fir2IrVisitor(
} }
override fun visitFile(file: FirFile, data: Any?): IrFile { override fun visitFile(file: FirFile, data: Any?): IrFile {
return conversionScope.withParent(declarationStorage.getIrFile(file)) { val irFile = declarationStorage.getIrFile(file)
conversionScope.withParent(irFile) {
file.declarations.forEach { file.declarations.forEach {
it.toIrDeclaration() it.toIrDeclaration()
} }
annotationGenerator.generate(this, file) annotationGenerator.generate(this, file)
metadata = FirMetadataSource.File(listOf(file)) metadata = FirMetadataSource.File(listOf(file))
} }
return irFile
} }
private fun FirDeclaration.toIrDeclaration(): IrDeclaration = private fun FirDeclaration.toIrDeclaration(): IrDeclaration =
@@ -188,9 +190,10 @@ class Fir2IrVisitor(
// NB: for implicit types it is possible that local class is already cached // NB: for implicit types it is possible that local class is already cached
val irClass = classifierStorage.getCachedIrClass(regularClass)?.apply { this.parent = irParent } val irClass = classifierStorage.getCachedIrClass(regularClass)?.apply { this.parent = irParent }
if (irClass != null) { if (irClass != null) {
return conversionScope.withParent(irClass) { conversionScope.withParent(irClass) {
memberGenerator.convertClassContent(irClass, regularClass) memberGenerator.convertClassContent(irClass, regularClass)
} }
return irClass
} }
converter.processLocalClassAndNestedClasses(regularClass, irParent) converter.processLocalClassAndNestedClasses(regularClass, irParent)
} }
@@ -198,9 +201,10 @@ class Fir2IrVisitor(
if (regularClass.isSealed) { if (regularClass.isSealed) {
irClass.sealedSubclasses = regularClass.getIrSymbolsForSealedSubclasses() irClass.sealedSubclasses = regularClass.getIrSymbolsForSealedSubclasses()
} }
return conversionScope.withParent(irClass) { conversionScope.withParent(irClass) {
memberGenerator.convertClassContent(irClass, regularClass) memberGenerator.convertClassContent(irClass, regularClass)
} }
return irClass
} }
override fun visitScript(script: FirScript, data: Any?): IrElement { override fun visitScript(script: FirScript, data: Any?): IrElement {
@@ -35,12 +35,12 @@ class FakeOverrideGenerator(
private val basePropertySymbols: MutableMap<IrProperty, List<FirPropertySymbol>> = mutableMapOf() private val basePropertySymbols: MutableMap<IrProperty, List<FirPropertySymbol>> = mutableMapOf()
private val baseStaticFieldSymbols: MutableMap<IrField, List<FirFieldSymbol>> = mutableMapOf() private val baseStaticFieldSymbols: MutableMap<IrField, List<FirFieldSymbol>> = mutableMapOf()
private fun IrSimpleFunction.withFunction(f: IrSimpleFunction.() -> Unit): IrSimpleFunction { private fun IrSimpleFunction.withFunction(f: IrSimpleFunction.() -> Unit) {
return conversionScope.withFunction(this, f) conversionScope.withFunction(this, f)
} }
private fun IrProperty.withProperty(f: IrProperty.() -> Unit): IrProperty { private fun IrProperty.withProperty(f: IrProperty.() -> Unit) {
return conversionScope.withProperty(this, firProperty = null, f) conversionScope.withProperty(this, firProperty = null, f)
} }
private fun IrType.containsErrorType(): Boolean { private fun IrType.containsErrorType(): Boolean {