FIR builder: do various stub-based optimizations #KT-24090 Fixed
This commit is contained in:
@@ -24,14 +24,13 @@ import org.jetbrains.kotlin.fir.types.FirType
|
|||||||
import org.jetbrains.kotlin.fir.types.FirTypeProjection
|
import org.jetbrains.kotlin.fir.types.FirTypeProjection
|
||||||
import org.jetbrains.kotlin.fir.types.impl.*
|
import org.jetbrains.kotlin.fir.types.impl.*
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||||
import org.jetbrains.kotlin.name.ClassId
|
import org.jetbrains.kotlin.name.ClassId
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
|
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
|
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.modalityModifierType
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifierType
|
|
||||||
import org.jetbrains.kotlin.types.Variance
|
import org.jetbrains.kotlin.types.Variance
|
||||||
|
|
||||||
class RawFirBuilder(val session: FirSession) {
|
class RawFirBuilder(val session: FirSession) {
|
||||||
@@ -43,26 +42,24 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val KtModifierListOwner.visibility: Visibility
|
private val KtModifierListOwner.visibility: Visibility
|
||||||
get() {
|
get() = with(modifierList) {
|
||||||
val modifierType = visibilityModifierType()
|
when {
|
||||||
return when (modifierType) {
|
this == null -> Visibilities.UNKNOWN
|
||||||
KtTokens.PUBLIC_KEYWORD -> Visibilities.PUBLIC
|
hasModifier(PRIVATE_KEYWORD) -> Visibilities.PRIVATE
|
||||||
KtTokens.PROTECTED_KEYWORD -> Visibilities.PROTECTED
|
hasModifier(PUBLIC_KEYWORD) -> Visibilities.PUBLIC
|
||||||
KtTokens.INTERNAL_KEYWORD -> Visibilities.INTERNAL
|
hasModifier(PROTECTED_KEYWORD) -> Visibilities.PROTECTED
|
||||||
KtTokens.PRIVATE_KEYWORD -> Visibilities.PRIVATE
|
else -> if (hasModifier(INTERNAL_KEYWORD)) Visibilities.INTERNAL else Visibilities.UNKNOWN
|
||||||
else -> Visibilities.UNKNOWN
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val KtDeclaration.modality: Modality?
|
private val KtDeclaration.modality: Modality?
|
||||||
get() {
|
get() = with(modifierList) {
|
||||||
val modifierType = modalityModifierType()
|
when {
|
||||||
return when (modifierType) {
|
this == null -> null
|
||||||
KtTokens.FINAL_KEYWORD -> Modality.FINAL
|
hasModifier(FINAL_KEYWORD) -> Modality.FINAL
|
||||||
KtTokens.SEALED_KEYWORD -> Modality.SEALED
|
hasModifier(SEALED_KEYWORD) -> Modality.SEALED
|
||||||
KtTokens.ABSTRACT_KEYWORD -> Modality.ABSTRACT
|
hasModifier(ABSTRACT_KEYWORD) -> Modality.ABSTRACT
|
||||||
KtTokens.OPEN_KEYWORD -> Modality.OPEN
|
else -> if (hasModifier(OPEN_KEYWORD)) Modality.OPEN else null
|
||||||
else -> null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,11 +88,16 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
private fun KtTypeReference?.toFirOrErrorType(): FirType =
|
private fun KtTypeReference?.toFirOrErrorType(): FirType =
|
||||||
convertSafe() ?: FirErrorTypeImpl(session, this, if (this == null) "Incomplete code" else "Conversion failed")
|
convertSafe() ?: FirErrorTypeImpl(session, this, if (this == null) "Incomplete code" else "Conversion failed")
|
||||||
|
|
||||||
private fun KtExpression?.toFirBody(): FirBody? =
|
private fun KtDeclarationWithBody.buildFirBody(): FirBody? =
|
||||||
convertSafe<FirExpression>()?.let { it as? FirBody ?: FirExpressionBodyImpl(session, it) }
|
when {
|
||||||
|
!hasBody() -> null
|
||||||
|
hasBlockBody() -> FirBlockBodyImpl(session, this)
|
||||||
|
else -> FirExpressionBodyImpl(session, FirExpressionStub(session, null))
|
||||||
|
}
|
||||||
|
|
||||||
private fun KtExpression?.toFirExpression(): FirExpression =
|
private fun ValueArgument?.toFirExpression(): FirExpression = with(this as? KtElement) {
|
||||||
convertSafe() ?: FirErrorExpressionImpl(session, this)
|
convertSafe() ?: FirErrorExpressionImpl(session, this)
|
||||||
|
}
|
||||||
|
|
||||||
private fun KtPropertyAccessor?.toFirPropertyAccessor(
|
private fun KtPropertyAccessor?.toFirPropertyAccessor(
|
||||||
property: KtProperty,
|
property: KtProperty,
|
||||||
@@ -119,7 +121,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
} else {
|
} else {
|
||||||
returnTypeReference.toFirOrUnitType()
|
returnTypeReference.toFirOrUnitType()
|
||||||
},
|
},
|
||||||
bodyExpression.toFirBody()
|
this.buildFirBody()
|
||||||
)
|
)
|
||||||
extractAnnotationsTo(firAccessor)
|
extractAnnotationsTo(firAccessor)
|
||||||
extractValueParametersTo(firAccessor, propertyType)
|
extractValueParametersTo(firAccessor, propertyType)
|
||||||
@@ -139,7 +141,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
defaultType != null -> defaultType
|
defaultType != null -> defaultType
|
||||||
else -> null.toFirOrErrorType()
|
else -> null.toFirOrErrorType()
|
||||||
},
|
},
|
||||||
defaultValue?.convert(),
|
if (hasDefaultValue()) FirExpressionStub(session, this) else null,
|
||||||
isCrossinline = hasModifier(KtTokens.CROSSINLINE_KEYWORD),
|
isCrossinline = hasModifier(KtTokens.CROSSINLINE_KEYWORD),
|
||||||
isNoinline = hasModifier(KtTokens.NOINLINE_KEYWORD),
|
isNoinline = hasModifier(KtTokens.NOINLINE_KEYWORD),
|
||||||
isVararg = isVarArg
|
isVararg = isVarArg
|
||||||
@@ -163,7 +165,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
isLateInit = false,
|
isLateInit = false,
|
||||||
receiverType = null,
|
receiverType = null,
|
||||||
returnType = type,
|
returnType = type,
|
||||||
isVar = valOrVarKeyword?.node?.elementType == KtTokens.VAR_KEYWORD,
|
isVar = isMutable,
|
||||||
initializer = null,
|
initializer = null,
|
||||||
getter = FirDefaultPropertyGetter(session, this, type),
|
getter = FirDefaultPropertyGetter(session, this, type),
|
||||||
setter = FirDefaultPropertySetter(session, this, type),
|
setter = FirDefaultPropertySetter(session, this, type),
|
||||||
@@ -196,7 +198,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
|
|
||||||
private fun KtCallElement.extractArgumentsTo(container: FirAbstractCall) {
|
private fun KtCallElement.extractArgumentsTo(container: FirAbstractCall) {
|
||||||
for (argument in this.valueArguments) {
|
for (argument in this.valueArguments) {
|
||||||
container.arguments += argument.getArgumentExpression().toFirExpression()
|
container.arguments += argument.toFirExpression()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +217,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
val type = superTypeListEntry.typeReference.toFirOrErrorType()
|
val type = superTypeListEntry.typeReference.toFirOrErrorType()
|
||||||
container.superTypes += FirDelegatedTypeImpl(
|
container.superTypes += FirDelegatedTypeImpl(
|
||||||
type,
|
type,
|
||||||
superTypeListEntry.delegateExpression.convertSafe()
|
FirExpressionStub(session, superTypeListEntry)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,7 +235,8 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
FirErrorTypeImpl(session, constructorCallee, "Not implemented yet"),
|
FirErrorTypeImpl(session, constructorCallee, "Not implemented yet"),
|
||||||
isThis = false
|
isThis = false
|
||||||
).apply {
|
).apply {
|
||||||
superTypeCallEntry.extractArgumentsTo(this)
|
// TODO: arguments are not needed for light classes, but will be needed later
|
||||||
|
//superTypeCallEntry.extractArgumentsTo(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val firConstructor = FirPrimaryConstructorImpl(
|
val firConstructor = FirPrimaryConstructorImpl(
|
||||||
@@ -385,7 +388,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
} else {
|
} else {
|
||||||
typeReference.toFirOrImplicitType()
|
typeReference.toFirOrImplicitType()
|
||||||
},
|
},
|
||||||
function.bodyExpression.toFirBody()
|
function.buildFirBody()
|
||||||
)
|
)
|
||||||
function.extractAnnotationsTo(firFunction)
|
function.extractAnnotationsTo(firFunction)
|
||||||
function.extractTypeParametersTo(firFunction)
|
function.extractTypeParametersTo(firFunction)
|
||||||
@@ -401,7 +404,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
constructor,
|
constructor,
|
||||||
constructor.visibility,
|
constructor.visibility,
|
||||||
constructor.getDelegationCall().convert(),
|
constructor.getDelegationCall().convert(),
|
||||||
constructor.bodyExpression.toFirBody()
|
constructor.buildFirBody()
|
||||||
)
|
)
|
||||||
constructor.extractAnnotationsTo(firConstructor)
|
constructor.extractAnnotationsTo(firConstructor)
|
||||||
constructor.extractValueParametersTo(firConstructor)
|
constructor.extractValueParametersTo(firConstructor)
|
||||||
@@ -415,7 +418,8 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
FirErrorTypeImpl(session, call, "Not implemented yet"),
|
FirErrorTypeImpl(session, call, "Not implemented yet"),
|
||||||
call.isCallToThis || call.isImplicit
|
call.isCallToThis || call.isImplicit
|
||||||
)
|
)
|
||||||
call.extractArgumentsTo(firConstructorCall)
|
// TODO: arguments are not needed for light classes, but will be needed later
|
||||||
|
// call.extractArgumentsTo(firConstructorCall)
|
||||||
return firConstructorCall
|
return firConstructorCall
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,7 +427,7 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
return FirAnonymousInitializerImpl(
|
return FirAnonymousInitializerImpl(
|
||||||
session,
|
session,
|
||||||
initializer,
|
initializer,
|
||||||
initializer.body.toFirBody()
|
FirBlockBodyImpl(session, initializer)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,10 +446,10 @@ class RawFirBuilder(val session: FirSession) {
|
|||||||
property.receiverTypeReference.convertSafe(),
|
property.receiverTypeReference.convertSafe(),
|
||||||
propertyType,
|
propertyType,
|
||||||
property.isVar,
|
property.isVar,
|
||||||
property.initializer?.convert(),
|
if (property.hasInitializer()) FirExpressionStub(session, property) else null,
|
||||||
property.getter.toFirPropertyAccessor(property, propertyType, isGetter = true),
|
property.getter.toFirPropertyAccessor(property, propertyType, isGetter = true),
|
||||||
property.setter.toFirPropertyAccessor(property, propertyType, isGetter = false),
|
property.setter.toFirPropertyAccessor(property, propertyType, isGetter = false),
|
||||||
property.delegateExpression?.convert()
|
if (property.hasDelegate()) FirExpressionStub(session, property) else null
|
||||||
)
|
)
|
||||||
property.extractAnnotationsTo(firProperty)
|
property.extractAnnotationsTo(firProperty)
|
||||||
property.extractTypeParametersTo(firProperty)
|
property.extractTypeParametersTo(firProperty)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ FILE: derivedClass.kt
|
|||||||
|
|
||||||
}
|
}
|
||||||
<T : Any> public? final class Derived : Base<T> {
|
<T : Any> public? final class Derived : Base<T> {
|
||||||
public? constructor(x: T): super(STUB)
|
public? constructor(x: T): super()
|
||||||
|
|
||||||
}
|
}
|
||||||
<T : Any> public? final? function create(x: T): Derived<T> {
|
<T : Any> public? final? function create(x: T): Derived<T> {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ FILE: nestedClass.kt
|
|||||||
}
|
}
|
||||||
public? final class Outer {
|
public? final class Outer {
|
||||||
public? final class Derived : Base {
|
public? final class Derived : Base {
|
||||||
public? constructor(s: String): super(STUB)
|
public? constructor(s: String): super()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ FILE: derivedClass.kt
|
|||||||
|
|
||||||
}
|
}
|
||||||
<T : R|kotlin/Any|> public? final class Derived : R|Base<T>| {
|
<T : R|kotlin/Any|> public? final class Derived : R|Base<T>| {
|
||||||
public? constructor(x: R|T|): super(STUB)
|
public? constructor(x: R|T|): super()
|
||||||
|
|
||||||
}
|
}
|
||||||
<T : R|kotlin/Any|> public? final? function create(x: R|T|): R|Derived<T>| {
|
<T : R|kotlin/Any|> public? final? function create(x: R|T|): R|Derived<T>| {
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@ FILE: Annotations.kt
|
|||||||
STUB
|
STUB
|
||||||
}
|
}
|
||||||
|
|
||||||
@R|annotations/WithString|(STUB) public? constructor(): this(STUB)
|
@R|annotations/WithString|(STUB) public? constructor(): this()
|
||||||
|
|
||||||
}
|
}
|
||||||
@R|annotations/WithInt|(STUB) @R|annotations/WithInt|(STUB) public? final typealias Third = @R|annotations/Simple|() R|test/Second|
|
@R|annotations/WithInt|(STUB) @R|annotations/WithInt|(STUB) public? final typealias Third = @R|annotations/Simple|() R|test/Second|
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ FILE: nestedClass.kt
|
|||||||
}
|
}
|
||||||
public? final class Outer {
|
public? final class Outer {
|
||||||
public? final class Derived : R|Base| {
|
public? final class Derived : R|Base| {
|
||||||
public? constructor(s: R|kotlin/String|): super(STUB)
|
public? constructor(s: R|kotlin/String|): super()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user