[FIR] Set delegate field receiver properly

This commit is contained in:
Mikhail Glukhikh
2020-04-01 00:26:18 +03:00
parent 036b6c63f6
commit f3f7bf70f6
65 changed files with 167 additions and 152 deletions
@@ -42,18 +42,18 @@ FILE: delegateInference.kt
public final var x: R|kotlin/Boolean|by R|/FreezableVar.FreezableVar|<R|kotlin/Boolean|>(Boolean(true))
public get(): R|kotlin/Boolean| {
^ D|/Test.x|.R|FakeOverride</FreezableVar.getValue: R|kotlin/Boolean|>|(this@R|/Test|, ::R|/Test.x|)
^ this@R|/Test|.D|/Test.x|.R|FakeOverride</FreezableVar.getValue: R|kotlin/Boolean|>|(this@R|/Test|, ::R|/Test.x|)
}
public set(<set-?>: R|kotlin/Boolean|): R|kotlin/Unit| {
D|/Test.x|.R|FakeOverride</FreezableVar.setValue: R|kotlin/Unit|>|(this@R|/Test|, ::R|/Test.x|, R|<local>/x|)
this@R|/Test|.D|/Test.x|.R|FakeOverride</FreezableVar.setValue: R|kotlin/Unit|>|(this@R|/Test|, ::R|/Test.x|, R|<local>/x|)
}
public final var y: R|kotlin/String|by R|/FreezableVar.FreezableVar|<R|kotlin/String|>(String())
public get(): R|kotlin/String| {
^ D|/Test.y|.R|FakeOverride</FreezableVar.getValue: R|kotlin/String|>|(this@R|/Test|, ::R|/Test.y|)
^ this@R|/Test|.D|/Test.y|.R|FakeOverride</FreezableVar.getValue: R|kotlin/String|>|(this@R|/Test|, ::R|/Test.y|)
}
public set(<set-?>: R|kotlin/String|): R|kotlin/Unit| {
D|/Test.y|.R|FakeOverride</FreezableVar.setValue: R|kotlin/Unit|>|(this@R|/Test|, ::R|/Test.y|, R|<local>/y|)
this@R|/Test|.D|/Test.y|.R|FakeOverride</FreezableVar.setValue: R|kotlin/Unit|>|(this@R|/Test|, ::R|/Test.y|, R|<local>/y|)
}
}
@@ -37,7 +37,7 @@ FILE: delegateWithLambda.kt
}
)
public get(): R|kotlin/String| {
^ D|/Test.x|.R|FakeOverride</LazyDelegate.getValue: R|kotlin/String|>|(this@R|/Test|, ::R|/Test.x|)
^ this@R|/Test|.D|/Test.x|.R|FakeOverride</LazyDelegate.getValue: R|kotlin/String|>|(this@R|/Test|, ::R|/Test.x|)
}
}
@@ -40,7 +40,7 @@ FILE: provideDelegate.kt
public final val x: R|kotlin/Int|by R|/delegate|<R|kotlin/Int|>(Int(1)).R|FakeOverride</DelegateProvider.provideDelegate: R|Delegate<kotlin/Int>|>|(this@R|/A|, ::R|/A.x|)
public get(): R|kotlin/Int| {
^ D|/A.x|.R|FakeOverride</Delegate.getValue: R|kotlin/Int|>|(this@R|/A|, ::R|/A.x|)
^ this@R|/A|.D|/A.x|.R|FakeOverride</Delegate.getValue: R|kotlin/Int|>|(this@R|/A|, ::R|/A.x|)
}
}
@@ -35,10 +35,10 @@ FILE: delegateTypeMismatch.kt
public final var classifierNamePolicy: R|ClassifierNamePolicy|by this@R|/A|.R|/A.property|<R|ClassifierNamePolicy|>(Q|ClassifierNamePolicy.SOURCE_CODE_QUALIFIED|)
public get(): R|ClassifierNamePolicy| {
^ D|/A.classifierNamePolicy|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|ClassifierNamePolicy|>|(this@R|/A|, ::R|/A.classifierNamePolicy|)
^ this@R|/A|.D|/A.classifierNamePolicy|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|ClassifierNamePolicy|>|(this@R|/A|, ::R|/A.classifierNamePolicy|)
}
public set(<set-?>: R|ClassifierNamePolicy|): R|kotlin/Unit| {
D|/A.classifierNamePolicy|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.classifierNamePolicy|, R|<local>/classifierNamePolicy|)
this@R|/A|.D|/A.classifierNamePolicy|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.classifierNamePolicy|, R|<local>/classifierNamePolicy|)
}
public final var typeNormalizer: R|(KotlinType) -> KotlinType|by this@R|/A|.R|/A.property|<R|(KotlinType) -> KotlinType|>(property@fun <anonymous>(it: R|KotlinType|): R|KotlinType| {
@@ -46,10 +46,10 @@ FILE: delegateTypeMismatch.kt
}
)
public get(): R|(KotlinType) -> KotlinType| {
^ D|/A.typeNormalizer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|(KotlinType) -> KotlinType|>|(this@R|/A|, ::R|/A.typeNormalizer|)
^ this@R|/A|.D|/A.typeNormalizer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|(KotlinType) -> KotlinType|>|(this@R|/A|, ::R|/A.typeNormalizer|)
}
public set(<set-?>: R|(KotlinType) -> KotlinType|): R|kotlin/Unit| {
D|/A.typeNormalizer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.typeNormalizer|, R|<local>/typeNormalizer|)
this@R|/A|.D|/A.typeNormalizer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.typeNormalizer|, R|<local>/typeNormalizer|)
}
}
@@ -124,7 +124,7 @@ digraph delegateWithAnonymousObject_kt {
50 [label="Access variable D|/IssuesListUserProfile.issueListView|"];
51 [label="Access variable this@R|/IssuesListUserProfile|"];
52 [label="Access variable R|<local>/issueListView|"];
53 [label="Function call: D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(...)"];
53 [label="Function call: this@R|/IssuesListUserProfile|.D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(...)"];
49 [label="Exit function setter" style="filled" fillcolor=red];
}
subgraph cluster_13 {
@@ -132,8 +132,8 @@ digraph delegateWithAnonymousObject_kt {
41 [label="Enter function getter" style="filled" fillcolor=red];
43 [label="Access variable D|/IssuesListUserProfile.issueListView|"];
44 [label="Access variable this@R|/IssuesListUserProfile|"];
45 [label="Function call: D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(...)"];
46 [label="Jump: ^ D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)"];
45 [label="Function call: this@R|/IssuesListUserProfile|.D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(...)"];
46 [label="Jump: ^ this@R|/IssuesListUserProfile|.D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)"];
47 [label="Stub" style="filled" fillcolor=gray];
42 [label="Exit function getter" style="filled" fillcolor=red];
}
@@ -41,10 +41,10 @@ FILE: delegateWithAnonymousObject.kt
}
)
public get(): R|IssueListView| {
^ D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)
^ this@R|/IssuesListUserProfile|.D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)
}
public set(<set-?>: R|IssueListView|): R|kotlin/Unit| {
D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|, R|<local>/issueListView|)
this@R|/IssuesListUserProfile|.D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|, R|<local>/issueListView|)
}
}
@@ -15,10 +15,10 @@ FILE: propertyWithFunctionalType.kt
}
)
public get(): R|(B) -> B| {
^ D|/A.conventer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|(B) -> B|>|(this@R|/A|, ::R|/A.conventer|)
^ this@R|/A|.D|/A.conventer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|(B) -> B|>|(this@R|/A|, ::R|/A.conventer|)
}
public set(<set-?>: R|(B) -> B|): R|kotlin/Unit| {
D|/A.conventer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.conventer|, R|<local>/conventer|)
this@R|/A|.D|/A.conventer|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.conventer|, R|<local>/conventer|)
}
public final var conventerWithExpectedType: R|(B) -> B|by this@R|/A|.R|/A.property|<R|(B) -> B|>(property@fun <anonymous>(it: R|B|): R|B| {
@@ -26,10 +26,10 @@ FILE: propertyWithFunctionalType.kt
}
)
public get(): R|(B) -> B| {
^ D|/A.conventerWithExpectedType|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|(B) -> B|>|(this@R|/A|, ::R|/A.conventerWithExpectedType|)
^ this@R|/A|.D|/A.conventerWithExpectedType|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|(B) -> B|>|(this@R|/A|, ::R|/A.conventerWithExpectedType|)
}
public set(<set-?>: R|(B) -> B|): R|kotlin/Unit| {
D|/A.conventerWithExpectedType|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.conventerWithExpectedType|, R|<local>/conventerWithExpectedType|)
this@R|/A|.D|/A.conventerWithExpectedType|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/A|, ::R|/A.conventerWithExpectedType|, R|<local>/conventerWithExpectedType|)
}
}
@@ -9,10 +9,10 @@ FILE: simpleDelegatedToMap.kt
public final var foo: <ERROR TYPE REF: Ambiguity: getValue, [kotlin/collections/getValue, kotlin/collections/getValue, kotlin/collections/getValue]>by R|<local>/map|
public get(): <ERROR TYPE REF: Ambiguity: getValue, [kotlin/collections/getValue, kotlin/collections/getValue, kotlin/collections/getValue]> {
^ D|/C.foo|.<Ambiguity: getValue, [kotlin/collections/getValue, kotlin/collections/getValue, kotlin/collections/getValue]>#(this@R|/C|, ::R|/C.foo|)
^ this@R|/C|.D|/C.foo|.<Ambiguity: getValue, [kotlin/collections/getValue, kotlin/collections/getValue, kotlin/collections/getValue]>#(this@R|/C|, ::R|/C.foo|)
}
public set(<set-?>: <ERROR TYPE REF: Ambiguity: getValue, [kotlin/collections/getValue, kotlin/collections/getValue, kotlin/collections/getValue]>): R|kotlin/Unit| {
D|/C.foo|.<Inapplicable(INAPPLICABLE): [kotlin/collections/setValue]>#(this@R|/C|, ::R|/C.foo|, R|<local>/foo|)
this@R|/C|.D|/C.foo|.<Inapplicable(INAPPLICABLE): [kotlin/collections/setValue]>#(this@R|/C|, ::R|/C.foo|, R|<local>/foo|)
}
}
@@ -24,7 +24,7 @@ FILE: simpleLazy.kt
}
)
public get(): R|kotlin/String| {
^ D|/Some.z|.R|kotlin/getValue|<R|kotlin/String|>(this@R|/Some|, ::R|/Some.z|)
^ this@R|/Some|.D|/Some.z|.R|kotlin/getValue|<R|kotlin/String|>(this@R|/Some|, ::R|/Some.z|)
}
public final fun foo(): R|kotlin/Unit| {
@@ -11,29 +11,22 @@ import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirVariable
import org.jetbrains.kotlin.fir.declarations.builder.FirPropertyBuilder
import org.jetbrains.kotlin.fir.declarations.builder.buildProperty
import org.jetbrains.kotlin.fir.declarations.builder.buildPropertyAccessor
import org.jetbrains.kotlin.fir.declarations.builder.buildValueParameter
import org.jetbrains.kotlin.fir.declarations.builder.*
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.*
import org.jetbrains.kotlin.fir.expressions.impl.FirSingleExpressionBlock
import org.jetbrains.kotlin.fir.expressions.impl.buildSingleExpressionBlock
import org.jetbrains.kotlin.fir.references.builder.buildDelegateFieldReference
import org.jetbrains.kotlin.fir.references.builder.buildExplicitThisReference
import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference
import org.jetbrains.kotlin.fir.references.builder.buildSimpleNamedReference
import org.jetbrains.kotlin.fir.references.builder.*
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
import org.jetbrains.kotlin.fir.symbols.impl.FirDelegateFieldSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertyAccessorSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.fir.symbols.constructStarProjectedType
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.ConeStarProjection
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.FirUserTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildImplicitTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildTypeProjectionWithVariance
import org.jetbrains.kotlin.fir.types.builder.buildUserTypeRef
import org.jetbrains.kotlin.fir.types.impl.*
@@ -298,31 +291,52 @@ fun generateTemporaryVariable(
fun FirPropertyBuilder.generateAccessorsByDelegate(
delegateBuilder: FirWrappedDelegateExpressionBuilder?,
ownerClassBuilder: FirClassBuilder?,
session: FirSession,
member: Boolean,
extension: Boolean,
isExtension: Boolean,
stubMode: Boolean,
receiver: FirExpression?
) {
if (delegateBuilder == null) return
val delegateFieldSymbol = FirDelegateFieldSymbol<FirProperty>(symbol.callableId).also {
this.delegateFieldSymbol = it
}
val ownerSymbol = when (ownerClassBuilder) {
is FirAnonymousObjectBuilder -> ownerClassBuilder.symbol
is AbstractFirRegularClassBuilder -> ownerClassBuilder.symbol
else -> null
}
val isMember = ownerSymbol != null
fun thisRef(): FirExpression =
when {
ownerSymbol != null -> buildThisReceiverExpression {
source = delegateBuilder.source
calleeReference = buildImplicitThisReference {
boundSymbol = ownerSymbol
}
typeRef = buildResolvedTypeRef {
val typeParameterNumber = (ownerClassBuilder as? AbstractFirRegularClassBuilder)?.typeParameters?.size ?: 0
type = ownerSymbol.constructStarProjectedType(typeParameterNumber)
}
}
// ???
isExtension -> buildQualifiedAccessExpression {
source = delegateBuilder.source
calleeReference = buildExplicitThisReference {}
}
else -> buildConstExpression(null, FirConstKind.Null, null)
}
fun delegateAccess() = buildQualifiedAccessExpression {
source = delegateBuilder.source
calleeReference = buildDelegateFieldReference {
resolvedSymbol = delegateFieldSymbol
}
}
fun thisRef(): FirExpression =
if (member || extension) buildQualifiedAccessExpression {
source = delegateBuilder.source
calleeReference = buildExplicitThisReference {}
if (ownerSymbol != null) {
dispatchReceiver = thisRef()
}
else buildConstExpression(null, FirConstKind.Null, null)
}
val isVar = this@generateAccessorsByDelegate.isVar
fun propertyRef() = buildCallableReferenceAccess {
@@ -333,12 +347,12 @@ fun FirPropertyBuilder.generateAccessorsByDelegate(
resolvedSymbol = this@generateAccessorsByDelegate.symbol
}
typeRef = when {
!member && !extension -> if (isVar) {
!isMember && !isExtension -> if (isVar) {
FirImplicitKMutableProperty0TypeRef(null, ConeStarProjection)
} else {
FirImplicitKProperty0TypeRef(null, ConeStarProjection)
}
member && extension -> if (isVar) {
isMember && isExtension -> if (isVar) {
FirImplicitKMutableProperty2TypeRef(null, ConeStarProjection, ConeStarProjection, ConeStarProjection)
} else {
FirImplicitKProperty2TypeRef(null, ConeStarProjection, ConeStarProjection, ConeStarProjection)
@@ -429,12 +429,13 @@ class DeclarationsConverter(
val secondaryConstructors = classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR)
val classWrapper = ClassWrapper(
className, modifiers, classKind, primaryConstructor != null,
secondaryConstructors.isNotEmpty(),
if (primaryConstructor != null) !primaryConstructor!!.hasValueParameters()
className, modifiers, classKind, classBuilder,
hasPrimaryConstructor = primaryConstructor != null,
hasSecondaryConstructor = secondaryConstructors.isNotEmpty(),
hasDefaultConstructor = if (primaryConstructor != null) !primaryConstructor!!.hasValueParameters()
else secondaryConstructors.isEmpty() || secondaryConstructors.any { !it.hasValueParameters() },
selfType,
delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry
delegatedSelfTypeRef = selfType,
delegatedSuperTypeRef = delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry = superTypeCallEntry
)
//parse primary constructor
val primaryConstructorWrapper = convertPrimaryConstructor(primaryConstructor, classWrapper, delegatedConstructorSource)
@@ -517,7 +518,8 @@ class DeclarationsConverter(
typeRef = delegatedSelfType
val classWrapper = ClassWrapper(
SpecialNames.NO_NAME_PROVIDED, modifiers, ClassKind.OBJECT, hasPrimaryConstructor = false,
SpecialNames.NO_NAME_PROVIDED, modifiers, ClassKind.OBJECT, this,
hasPrimaryConstructor = false,
hasSecondaryConstructor = classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
hasDefaultConstructor = false,
delegatedSelfTypeRef = delegatedSelfType,
@@ -576,7 +578,8 @@ class DeclarationsConverter(
symbol = FirAnonymousObjectSymbol()
annotations += modifiers.annotations
val enumClassWrapper = ClassWrapper(
enumEntryName, modifiers, ClassKind.ENUM_ENTRY, hasPrimaryConstructor = true,
enumEntryName, modifiers, ClassKind.ENUM_ENTRY, this,
hasPrimaryConstructor = true,
hasSecondaryConstructor = classBodyNode.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
hasDefaultConstructor = false,
delegatedSelfTypeRef = buildResolvedTypeRef {
@@ -623,7 +626,7 @@ class DeclarationsConverter(
ENUM_ENTRY -> container += convertEnumEntry(node, classWrapper)
CLASS -> container += convertClass(node)
FUN -> container += convertFunctionDeclaration(node)
PROPERTY -> container += convertPropertyDeclaration(node)
PROPERTY -> container += convertPropertyDeclaration(node, classWrapper)
TYPEALIAS -> container += convertTypeAlias(node)
OBJECT_DECLARATION -> container += convertClass(node)
CLASS_INITIALIZER -> container += convertAnonymousInitializer(node) //anonymousInitializer
@@ -820,7 +823,7 @@ class DeclarationsConverter(
/**
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseProperty
*/
fun convertPropertyDeclaration(property: LighterASTNode): FirDeclaration {
fun convertPropertyDeclaration(property: LighterASTNode, classWrapper: ClassWrapper? = null): FirDeclaration {
var modifiers = Modifier()
var identifier: String? = null
val firTypeParameters = mutableListOf<FirTypeParameter>()
@@ -884,9 +887,9 @@ class DeclarationsConverter(
}
generateAccessorsByDelegate(
delegateBuilder,
classWrapper?.classBuilder,
baseSession,
member = false,
extension = false,
isExtension = false,
stubMode,
receiver
)
@@ -916,9 +919,10 @@ class DeclarationsConverter(
expressionConverter.getAsFirExpression<FirExpression>(it, "Should have delegate")
}
generateAccessorsByDelegate(
delegateBuilder, baseSession,
member = parentNode?.tokenType != KT_FILE,
extension = receiverType != null,
delegateBuilder,
classWrapper?.classBuilder,
baseSession,
isExtension = receiverType != null,
stubMode,
receiver
)
@@ -9,8 +9,10 @@ import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.declarations.builder.FirClassBuilder
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
@@ -19,12 +21,13 @@ class ClassWrapper(
private val className: Name,
private val modifiers: Modifier,
private val classKind: ClassKind,
val classBuilder: FirClassBuilder,
val hasPrimaryConstructor: Boolean,
val hasSecondaryConstructor: Boolean,
val hasDefaultConstructor: Boolean,
val delegatedSelfTypeRef: FirTypeRef,
val delegatedSuperTypeRef: FirTypeRef,
val superTypeCallEntry: MutableList<FirExpression>
val superTypeCallEntry: MutableList<FirExpression>,
) {
fun isObjectLiteral(): Boolean {
return className == SpecialNames.NO_NAME_PROVIDED && isObject()
@@ -156,7 +156,8 @@ class RawFirBuilder(
convert()
private fun KtDeclaration.toFirDeclaration(
delegatedSuperType: FirTypeRef, delegatedSelfType: FirResolvedTypeRef, owner: KtClassOrObject, hasPrimaryConstructor: Boolean,
delegatedSuperType: FirTypeRef, delegatedSelfType: FirResolvedTypeRef,
owner: KtClassOrObject, ownerClassBuilder: FirClassBuilder, hasPrimaryConstructor: Boolean,
): FirDeclaration {
return when (this) {
is KtSecondaryConstructor -> {
@@ -175,6 +176,9 @@ class RawFirBuilder(
}
toFirEnumEntry(delegatedSelfType, ownerClassHasDefaultConstructor)
}
is KtProperty -> {
toFirProperty(ownerClassBuilder)
}
else -> convert()
}
}
@@ -588,6 +592,7 @@ class RawFirBuilder(
correctedEnumSelfTypeRef,
delegatedSelfType = delegatedEntrySelfType,
ktEnumEntry,
ownerClassBuilder = this,
hasPrimaryConstructor = true,
)
}
@@ -651,7 +656,8 @@ class RawFirBuilder(
for (declaration in classOrObject.declarations) {
addDeclaration(
declaration.toFirDeclaration(
delegatedSuperType, delegatedSelfType, classOrObject, hasPrimaryConstructor = primaryConstructor != null,
delegatedSuperType, delegatedSelfType, classOrObject, classBuilder,
hasPrimaryConstructor = primaryConstructor != null,
),
)
}
@@ -696,6 +702,7 @@ class RawFirBuilder(
delegatedSuperType,
delegatedSelfType,
owner = objectDeclaration,
ownerClassBuilder = this,
hasPrimaryConstructor = false,
)
}
@@ -923,23 +930,15 @@ class RawFirBuilder(
}
}
override fun visitAnonymousInitializer(initializer: KtAnonymousInitializer, data: Unit): FirElement {
return buildAnonymousInitializer {
source = initializer.toFirSourceElement()
session = baseSession
body = if (stubMode) buildEmptyExpressionBlock() else initializer.body.toFirBlock()
}
}
override fun visitProperty(property: KtProperty, data: Unit): FirElement {
val propertyType = property.typeReference.toFirOrImplicitType()
val propertyName = property.nameAsSafeName
val isVar = property.isVar
val propertyInitializer = if (property.hasInitializer()) {
{ property.initializer }.toFirExpression("Should have initializer")
private fun KtProperty.toFirProperty(ownerClassBuilder: FirClassBuilder?): FirProperty {
val propertyType = typeReference.toFirOrImplicitType()
val propertyName = nameAsSafeName
val isVar = isVar
val propertyInitializer = if (hasInitializer()) {
{ initializer }.toFirExpression("Should have initializer")
} else null
val delegateExpression by lazy { property.delegate?.expression }
val propertySource = property.toFirSourceElement()
val delegateExpression by lazy { delegate?.expression }
val propertySource = toFirSourceElement()
return buildProperty {
source = propertySource
@@ -949,7 +948,7 @@ class RawFirBuilder(
this.isVar = isVar
initializer = propertyInitializer
if (property.isLocal) {
if (this@toFirProperty.isLocal) {
isLocal = true
symbol = FirPropertySymbol(propertyName)
val delegateBuilder = delegateExpression?.let {
@@ -962,43 +961,57 @@ class RawFirBuilder(
status = FirDeclarationStatusImpl(Visibilities.LOCAL, Modality.FINAL)
val receiver = delegateExpression?.toFirExpression("Incorrect delegate expression")
generateAccessorsByDelegate(delegateBuilder, baseSession, member = false, extension = false, stubMode, receiver)
generateAccessorsByDelegate(delegateBuilder, null, baseSession, isExtension = false, stubMode, receiver)
} else {
isLocal = false
receiverTypeRef = property.receiverTypeReference.convertSafe()
receiverTypeRef = receiverTypeReference.convertSafe()
symbol = FirPropertySymbol(callableIdForName(propertyName))
val delegateBuilder = if (property.hasDelegate()) {
val delegateBuilder = if (hasDelegate()) {
FirWrappedDelegateExpressionBuilder().apply {
source = if (stubMode) null else delegateExpression?.toFirSourceElement()
expression = { delegateExpression }.toFirExpression("Should have delegate")
}
} else null
status = FirDeclarationStatusImpl(property.visibility, property.modality).apply {
isExpect = property.hasExpectModifier()
isActual = property.hasActualModifier()
isOverride = property.hasModifier(OVERRIDE_KEYWORD)
isConst = property.hasModifier(CONST_KEYWORD)
isLateInit = property.hasModifier(LATEINIT_KEYWORD)
status = FirDeclarationStatusImpl(visibility, modality).apply {
isExpect = hasExpectModifier()
isActual = hasActualModifier()
isOverride = hasModifier(OVERRIDE_KEYWORD)
isConst = hasModifier(CONST_KEYWORD)
isLateInit = hasModifier(LATEINIT_KEYWORD)
}
property.extractTypeParametersTo(this)
extractTypeParametersTo(this)
getter = property.getter.toFirPropertyAccessor(property, propertyType, isGetter = true)
setter = if (isVar) property.setter.toFirPropertyAccessor(property, propertyType, isGetter = false) else null
getter = this@toFirProperty.getter.toFirPropertyAccessor(this@toFirProperty, propertyType, isGetter = true)
setter = if (isVar) {
this@toFirProperty.setter.toFirPropertyAccessor(this@toFirProperty, propertyType, isGetter = false)
} else null
val receiver = delegateExpression?.toFirExpression("Should have delegate")
generateAccessorsByDelegate(
delegateBuilder,
ownerClassBuilder,
baseSession,
member = !property.isTopLevel,
extension = property.receiverTypeReference != null,
isExtension = receiverTypeReference != null,
stubMode,
receiver
)
}
property.extractAnnotationsTo(this)
extractAnnotationsTo(this)
}
}
override fun visitAnonymousInitializer(initializer: KtAnonymousInitializer, data: Unit): FirElement {
return buildAnonymousInitializer {
source = initializer.toFirSourceElement()
session = baseSession
body = if (stubMode) buildEmptyExpressionBlock() else initializer.body.toFirBlock()
}
}
override fun visitProperty(property: KtProperty, data: Unit): FirElement {
return property.toFirProperty(ownerClassBuilder = null)
}
override fun visitTypeReference(typeReference: KtTypeReference, data: Unit): FirElement {
val typeElement = typeReference.typeElement
val source = typeReference.toFirSourceElement()
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.symbols
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLookupTagWithFixedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.ConeStarProjection
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
fun FirClassSymbol<*>.constructStarProjectedType(typeParameterNumber: Int): ConeClassLikeType {
return ConeClassLikeTypeImpl(
if (classId.isLocal) ConeClassLookupTagWithFixedSymbol(classId, this)
else ConeClassLikeLookupTagImpl(classId),
Array(typeParameterNumber) { ConeStarProjection },
isNullable = false
)
}