[FIR] Adapt FIR utilities after FirExpression.coneTypeOrNull introduction

#KT-59855
This commit is contained in:
Kirill Rakhman
2023-08-04 10:40:47 +02:00
committed by Space Team
parent 7223cd1bf3
commit bc27feace4
4 changed files with 20 additions and 16 deletions
@@ -46,7 +46,7 @@ fun FirRegularClassBuilder.generateValuesFunction(
source = sourceElement source = sourceElement
this.origin = origin this.origin = origin
this.moduleData = moduleData this.moduleData = moduleData
returnTypeRef = buildResolvedTypeRef { val returnTypeRef = buildResolvedTypeRef {
source = sourceElement source = sourceElement
type = ConeClassLikeTypeImpl( type = ConeClassLikeTypeImpl(
StandardClassIds.Array.toLookupTag(), StandardClassIds.Array.toLookupTag(),
@@ -60,6 +60,7 @@ fun FirRegularClassBuilder.generateValuesFunction(
isNullable = false isNullable = false
) )
} }
this.returnTypeRef = returnTypeRef
name = ENUM_VALUES name = ENUM_VALUES
this.status = createStatus(this@generateValuesFunction.status).apply { this.status = createStatus(this@generateValuesFunction.status).apply {
isStatic = true isStatic = true
@@ -68,7 +69,7 @@ fun FirRegularClassBuilder.generateValuesFunction(
symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, ENUM_VALUES)) symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, ENUM_VALUES))
resolvePhase = this@generateValuesFunction.resolvePhase resolvePhase = this@generateValuesFunction.resolvePhase
body = buildEmptyExpressionBlock().also { body = buildEmptyExpressionBlock().also {
it.replaceTypeRef(returnTypeRef) it.replaceType(returnTypeRef.type)
} }
}.apply { }.apply {
containingClassForStaticMemberAttr = this@generateValuesFunction.symbol.toLookupTag() containingClassForStaticMemberAttr = this@generateValuesFunction.symbol.toLookupTag()
@@ -87,7 +88,7 @@ fun FirRegularClassBuilder.generateValueOfFunction(
source = sourceElement source = sourceElement
this.origin = origin this.origin = origin
this.moduleData = moduleData this.moduleData = moduleData
returnTypeRef = buildResolvedTypeRef { val returnTypeRef = buildResolvedTypeRef {
source = sourceElement source = sourceElement
type = ConeClassLikeTypeImpl( type = ConeClassLikeTypeImpl(
this@generateValueOfFunction.symbol.toLookupTag(), this@generateValueOfFunction.symbol.toLookupTag(),
@@ -95,6 +96,7 @@ fun FirRegularClassBuilder.generateValueOfFunction(
isNullable = false isNullable = false
) )
} }
this.returnTypeRef = returnTypeRef
name = ENUM_VALUE_OF name = ENUM_VALUE_OF
status = createStatus(this@generateValueOfFunction.status).apply { status = createStatus(this@generateValueOfFunction.status).apply {
@@ -107,7 +109,7 @@ fun FirRegularClassBuilder.generateValueOfFunction(
containingFunctionSymbol = this@buildSimpleFunction.symbol containingFunctionSymbol = this@buildSimpleFunction.symbol
this.origin = origin this.origin = origin
this.moduleData = moduleData this.moduleData = moduleData
returnTypeRef = buildResolvedTypeRef { this.returnTypeRef = buildResolvedTypeRef {
source = sourceElement source = sourceElement
type = ConeClassLikeTypeImpl( type = ConeClassLikeTypeImpl(
StandardClassIds.String.toLookupTag(), StandardClassIds.String.toLookupTag(),
@@ -124,7 +126,7 @@ fun FirRegularClassBuilder.generateValueOfFunction(
} }
resolvePhase = this@generateValueOfFunction.resolvePhase resolvePhase = this@generateValueOfFunction.resolvePhase
body = buildEmptyExpressionBlock().also { body = buildEmptyExpressionBlock().also {
it.replaceTypeRef(returnTypeRef) it.replaceType(returnTypeRef.type)
} }
}.apply { }.apply {
containingClassForStaticMemberAttr = this@generateValueOfFunction.symbol.toLookupTag() containingClassForStaticMemberAttr = this@generateValueOfFunction.symbol.toLookupTag()
@@ -22,6 +22,8 @@ import org.jetbrains.kotlin.fir.references.FirSuperReference
import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.coneTypeOrNull
import org.jetbrains.kotlin.fir.types.impl.FirImplicitTypeRefImplWithoutSource import org.jetbrains.kotlin.fir.types.impl.FirImplicitTypeRefImplWithoutSource
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.SpecialNames
@@ -36,7 +38,7 @@ fun FirVariable.toQualifiedAccess(
name = this@toQualifiedAccess.name name = this@toQualifiedAccess.name
resolvedSymbol = this@toQualifiedAccess.symbol resolvedSymbol = this@toQualifiedAccess.symbol
} }
this.typeRef = typeRef this.coneTypeOrNull = typeRef.coneTypeOrNull
} }
fun generateTemporaryVariable( fun generateTemporaryVariable(
@@ -85,7 +87,12 @@ fun generateExplicitReceiverTemporaryVariable(
source = source, source = source,
name = SpecialNames.RECEIVER, name = SpecialNames.RECEIVER,
initializer = receiver, initializer = receiver,
typeRef = receiver.typeRef.copyWithNewSource(source), typeRef = receiver.coneTypeOrNull?.let {
buildResolvedTypeRef {
type = it
this.source = source
}
}
).also { property -> ).also { property ->
// Change the expression from x.a to <receiver>.a // Change the expression from x.a to <receiver>.a
val newReceiverAccess = val newReceiverAccess =
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.fir.expressions.impl.FirSingleExpressionBlock
import org.jetbrains.kotlin.fir.references.* import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef
import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.TransformData import org.jetbrains.kotlin.fir.visitors.TransformData
import org.jetbrains.kotlin.fir.visitors.transformInplace import org.jetbrains.kotlin.fir.visitors.transformInplace
@@ -85,9 +84,7 @@ fun buildErrorLoop(source: KtSourceElement?, diagnostic: ConeDiagnostic): FirErr
this.source = source this.source = source
this.diagnostic = diagnostic this.diagnostic = diagnostic
}.also { }.also {
it.block.replaceTypeRef(buildErrorTypeRef { it.block.replaceConeTypeOrNull(ConeErrorType(diagnostic))
this.diagnostic = diagnostic
})
} }
} }
@@ -37,13 +37,11 @@ val FirTypeRef.coneType: ConeKotlinType
val FirTypeRef.coneTypeOrNull: ConeKotlinType? val FirTypeRef.coneTypeOrNull: ConeKotlinType?
get() = coneTypeSafe() get() = coneTypeSafe()
val FirExpression.coneType: ConeKotlinType get() = typeRef.coneType val FirExpression.coneType: ConeKotlinType get() = requireNotNull(coneTypeOrNull) { "Expected type to be resolved" }
val FirExpression.coneTypeOrNull: ConeKotlinType? get() = typeRef.coneTypeOrNull inline fun <reified T : ConeKotlinType> FirExpression.coneTypeSafe(): T? = (coneTypeOrNull as? T)
inline fun <reified T : ConeKotlinType> FirExpression.coneTypeSafe(): T? = typeRef.coneTypeSafe() inline fun <reified T : ConeKotlinType> FirExpression.coneTypeUnsafe(): T = coneTypeOrNull as T
inline fun <reified T : ConeKotlinType> FirExpression.coneTypeUnsafe(): T = typeRef.coneTypeUnsafe()
@RequiresOptIn( @RequiresOptIn(
"This type check never expands type aliases. Use with care (probably Ok for expression & constructor types). " + "This type check never expands type aliases. Use with care (probably Ok for expression & constructor types). " +