[FIR2IR] Make some utility functions inline
Those utilities are commonly used and effectively just call passed lambda with some preparations
This commit is contained in:
committed by
Space Team
parent
ccb0d3cb0c
commit
45b1c33ed8
@@ -68,40 +68,46 @@ fun AbstractKtSourceElement?.startOffsetSkippingComments(): Int? {
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <T : IrElement> FirElement.convertWithOffsets(
|
||||
f: (startOffset: Int, endOffset: Int) -> T
|
||||
): T {
|
||||
internal inline fun <T : IrElement> FirElement.convertWithOffsets(f: (startOffset: Int, endOffset: Int) -> T): T {
|
||||
return source.convertWithOffsets(f)
|
||||
}
|
||||
|
||||
internal fun <T : IrElement> KtSourceElement?.convertWithOffsets(
|
||||
f: (startOffset: Int, endOffset: Int) -> T
|
||||
): T {
|
||||
if (psi is PsiCompiledElement) return f(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
|
||||
val startOffset = this?.startOffsetSkippingComments() ?: this?.startOffset ?: UNDEFINED_OFFSET
|
||||
val endOffset = this?.endOffset ?: UNDEFINED_OFFSET
|
||||
internal inline fun <T : IrElement> KtSourceElement?.convertWithOffsets(f: (startOffset: Int, endOffset: Int) -> T): T {
|
||||
val startOffset: Int
|
||||
val endOffset: Int
|
||||
|
||||
if (psi is PsiCompiledElement) {
|
||||
startOffset = UNDEFINED_OFFSET
|
||||
endOffset = UNDEFINED_OFFSET
|
||||
} else {
|
||||
startOffset = this?.startOffsetSkippingComments() ?: this?.startOffset ?: UNDEFINED_OFFSET
|
||||
endOffset = this?.endOffset ?: UNDEFINED_OFFSET
|
||||
}
|
||||
|
||||
return f(startOffset, endOffset)
|
||||
}
|
||||
|
||||
internal fun <T : IrElement> FirQualifiedAccessExpression.convertWithOffsets(
|
||||
f: (startOffset: Int, endOffset: Int) -> T
|
||||
): T {
|
||||
internal inline fun <T : IrElement> FirQualifiedAccessExpression.convertWithOffsets(f: (startOffset: Int, endOffset: Int) -> T): T {
|
||||
return convertWithOffsets(this.calleeReference, f)
|
||||
}
|
||||
|
||||
internal fun <T : IrElement> FirThisReceiverExpression.convertWithOffsets(
|
||||
f: (startOffset: Int, endOffset: Int) -> T
|
||||
): T {
|
||||
internal inline fun <T : IrElement> FirThisReceiverExpression.convertWithOffsets(f: (startOffset: Int, endOffset: Int) -> T): T {
|
||||
return source.convertWithOffsets(f)
|
||||
}
|
||||
|
||||
internal fun <T : IrElement> FirStatement.convertWithOffsets(
|
||||
internal inline fun <T : IrElement> FirStatement.convertWithOffsets(
|
||||
calleeReference: FirReference,
|
||||
f: (startOffset: Int, endOffset: Int) -> T
|
||||
): T {
|
||||
if (psi is PsiCompiledElement) return f(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
|
||||
val startOffset = calleeReference.source?.startOffsetSkippingComments() ?: calleeReference.source?.startOffset ?: UNDEFINED_OFFSET
|
||||
val endOffset = source?.endOffset ?: UNDEFINED_OFFSET
|
||||
val startOffset: Int
|
||||
val endOffset: Int
|
||||
if (psi is PsiCompiledElement) {
|
||||
startOffset = UNDEFINED_OFFSET
|
||||
endOffset = UNDEFINED_OFFSET
|
||||
} else {
|
||||
startOffset = calleeReference.source?.startOffsetSkippingComments() ?: calleeReference.source?.startOffset ?: UNDEFINED_OFFSET
|
||||
endOffset = source?.endOffset ?: UNDEFINED_OFFSET
|
||||
}
|
||||
return f(startOffset, endOffset)
|
||||
}
|
||||
|
||||
|
||||
@@ -13,14 +13,23 @@ import org.jetbrains.kotlin.fir.expressions.FirReturnExpression
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.util.isSetter
|
||||
import org.jetbrains.kotlin.ir.util.parentClassOrNull
|
||||
import org.jetbrains.kotlin.util.PrivateForInline
|
||||
|
||||
@OptIn(PrivateForInline::class)
|
||||
class Fir2IrConversionScope {
|
||||
private val parentStack = mutableListOf<IrDeclarationParent>()
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val parentStack = mutableListOf<IrDeclarationParent>()
|
||||
|
||||
private val containingFirClassStack = mutableListOf<FirClass>()
|
||||
private val currentlyGeneratedDelegatedConstructors = mutableMapOf<IrClass, IrConstructor>()
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val containingFirClassStack = mutableListOf<FirClass>()
|
||||
|
||||
fun <T : IrDeclarationParent, R> withParent(parent: T, f: T.() -> R): R {
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val currentlyGeneratedDelegatedConstructors = mutableMapOf<IrClass, IrConstructor>()
|
||||
|
||||
inline fun <T : IrDeclarationParent, R> withParent(parent: T, f: T.() -> R): R {
|
||||
parentStack += parent
|
||||
try {
|
||||
return parent.f()
|
||||
@@ -29,11 +38,13 @@ class Fir2IrConversionScope {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> forDelegatingConstructorCall(constructor: IrConstructor, irClass: IrClass, f: () -> T): T {
|
||||
internal fun <T> forDelegatingConstructorCall(constructor: IrConstructor, irClass: IrClass, f: () -> T): T {
|
||||
currentlyGeneratedDelegatedConstructors[irClass] = constructor
|
||||
val result = f()
|
||||
currentlyGeneratedDelegatedConstructors.remove(irClass)
|
||||
return result
|
||||
try {
|
||||
return f()
|
||||
} finally {
|
||||
currentlyGeneratedDelegatedConstructors.remove(irClass)
|
||||
}
|
||||
}
|
||||
|
||||
fun getConstructorForCurrentlyGeneratedDelegatedConstructor(itClass: IrClass): IrConstructor? =
|
||||
@@ -41,10 +52,13 @@ class Fir2IrConversionScope {
|
||||
|
||||
fun containingFileIfAny(): IrFile? = parentStack.getOrNull(0) as? IrFile
|
||||
|
||||
fun withContainingFirClass(containingFirClass: FirClass, f: () -> Unit) {
|
||||
inline fun withContainingFirClass(containingFirClass: FirClass, f: () -> Unit) {
|
||||
containingFirClassStack += containingFirClass
|
||||
f()
|
||||
containingFirClassStack.removeAt(containingFirClassStack.size - 1)
|
||||
try {
|
||||
f()
|
||||
} finally {
|
||||
containingFirClassStack.removeAt(containingFirClassStack.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun parentFromStack(): IrDeclarationParent = parentStack.last()
|
||||
@@ -66,9 +80,11 @@ class Fir2IrConversionScope {
|
||||
|
||||
fun containerFirClass(): FirClass? = containingFirClassStack.lastOrNull()
|
||||
|
||||
private val functionStack = mutableListOf<IrFunction>()
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val functionStack = mutableListOf<IrFunction>()
|
||||
|
||||
fun <T : IrFunction, R> withFunction(function: T, f: T.() -> R): R {
|
||||
inline fun <T : IrFunction, R> withFunction(function: T, f: T.() -> R): R {
|
||||
functionStack += function
|
||||
try {
|
||||
return function.f()
|
||||
@@ -77,9 +93,11 @@ class Fir2IrConversionScope {
|
||||
}
|
||||
}
|
||||
|
||||
private val propertyStack = mutableListOf<Pair<IrProperty, FirProperty?>>()
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val propertyStack = mutableListOf<Pair<IrProperty, FirProperty?>>()
|
||||
|
||||
fun <R> withProperty(property: IrProperty, firProperty: FirProperty? = null, f: IrProperty.() -> R): R {
|
||||
inline fun <R> withProperty(property: IrProperty, firProperty: FirProperty? = null, f: IrProperty.() -> R): R {
|
||||
propertyStack += (property to firProperty)
|
||||
try {
|
||||
return property.f()
|
||||
@@ -88,9 +106,11 @@ class Fir2IrConversionScope {
|
||||
}
|
||||
}
|
||||
|
||||
private val classStack = mutableListOf<IrClass>()
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val classStack = mutableListOf<IrClass>()
|
||||
|
||||
fun <R> withClass(klass: IrClass, f: IrClass.() -> R): R {
|
||||
inline fun <R> withClass(klass: IrClass, f: IrClass.() -> R): R {
|
||||
classStack += klass
|
||||
return try {
|
||||
klass.f()
|
||||
@@ -99,21 +119,30 @@ class Fir2IrConversionScope {
|
||||
}
|
||||
}
|
||||
|
||||
private val whenSubjectVariableStack = mutableListOf<IrVariable>()
|
||||
private val safeCallSubjectVariableStack = mutableListOf<IrVariable>()
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val whenSubjectVariableStack = mutableListOf<IrVariable>()
|
||||
|
||||
fun <T> withWhenSubject(subject: IrVariable?, f: () -> T): T {
|
||||
@PublishedApi
|
||||
@PrivateForInline
|
||||
internal val safeCallSubjectVariableStack = mutableListOf<IrVariable>()
|
||||
|
||||
inline fun <T> withWhenSubject(subject: IrVariable?, f: () -> T): T {
|
||||
if (subject != null) whenSubjectVariableStack += subject
|
||||
val result = f()
|
||||
if (subject != null) whenSubjectVariableStack.removeAt(whenSubjectVariableStack.size - 1)
|
||||
return result
|
||||
try {
|
||||
return f()
|
||||
} finally {
|
||||
if (subject != null) whenSubjectVariableStack.removeAt(whenSubjectVariableStack.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> withSafeCallSubject(subject: IrVariable?, f: () -> T): T {
|
||||
inline fun <T> withSafeCallSubject(subject: IrVariable?, f: () -> T): T {
|
||||
if (subject != null) safeCallSubjectVariableStack += subject
|
||||
val result = f()
|
||||
if (subject != null) safeCallSubjectVariableStack.removeAt(safeCallSubjectVariableStack.size - 1)
|
||||
return result
|
||||
try {
|
||||
return f()
|
||||
} finally {
|
||||
if (subject != null) safeCallSubjectVariableStack.removeAt(safeCallSubjectVariableStack.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun returnTarget(expression: FirReturnExpression, declarationStorage: Fir2IrDeclarationStorage): IrFunction {
|
||||
|
||||
+2
-2
@@ -35,11 +35,11 @@ class FakeOverrideGenerator(
|
||||
private val basePropertySymbols: MutableMap<IrProperty, List<FirPropertySymbol>> = mutableMapOf()
|
||||
private val baseStaticFieldSymbols: MutableMap<IrField, List<FirFieldSymbol>> = mutableMapOf()
|
||||
|
||||
private fun IrSimpleFunction.withFunction(f: IrSimpleFunction.() -> Unit) {
|
||||
private inline fun IrSimpleFunction.withFunction(f: IrSimpleFunction.() -> Unit) {
|
||||
conversionScope.withFunction(this, f)
|
||||
}
|
||||
|
||||
private fun IrProperty.withProperty(f: IrProperty.() -> Unit) {
|
||||
private inline fun IrProperty.withProperty(f: IrProperty.() -> Unit) {
|
||||
conversionScope.withProperty(this, firProperty = null, f)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user