IR: more consolidation of synthetic member generation for data class.

This commit is contained in:
Jinseong Jeon
2020-04-15 13:45:54 -07:00
committed by Dmitriy Novozhilov
parent 7f02d57d88
commit 9f1ecadd65
22 changed files with 354 additions and 403 deletions
@@ -17,13 +17,11 @@ import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.*
import org.jetbrains.kotlin.fir.expressions.impl.buildSingleExpressionBlock
import org.jetbrains.kotlin.fir.references.FirReference
import org.jetbrains.kotlin.fir.references.builder.*
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.buildImplicitTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl
@@ -662,6 +660,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
private fun generateComponentAccess(parameterSource: FirSourceElement?, firProperty: FirProperty) =
buildQualifiedAccessExpression {
source = parameterSource
typeRef = firProperty.returnTypeRef
dispatchReceiver = buildThisReceiverExpression {
calleeReference = buildImplicitThisReference {
boundSymbol = classBuilder.symbol
@@ -682,7 +681,6 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
val name = Name.identifier("component$componentIndex")
componentIndex++
val parameterSource = sourceNode?.toFirSourceElement()
val target = FirFunctionTarget(labelName = null, isLambda = false)
val componentFunction = buildSimpleFunction {
source = parameterSource
session = this@DataClassMembersGenerator.session
@@ -692,14 +690,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL)
symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, name))
val returnExpression = buildReturnExpression {
source = parameterSource
result = generateComponentAccess(parameterSource, firProperty)
this.target = target
}
body = buildSingleExpressionBlock(returnExpression)
}.also {
target.bind(it)
// Refer to FIR backend ClassMemberGenerator for body generation.
}
classBuilder.addDeclaration(componentFunction)
}
@@ -708,7 +699,6 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
private val copyName = Name.identifier("copy")
private fun generateCopyFunction() {
val target = FirFunctionTarget(labelName = null, isLambda = false)
classBuilder.addDeclaration(
buildSimpleFunction {
source = this@DataClassMembersGenerator.source.toFirSourceElement()
@@ -732,27 +722,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
isVararg = false
}
}
// TODO: Handle generic types.
val initCallExpression = buildFunctionCall {
argumentList = buildArgumentList {
for ((ktParameter, firProperty) in zippedParameters) {
val parameterSource = ktParameter?.toFirSourceElement()
arguments += generateComponentAccess(parameterSource, firProperty)
}
}
calleeReference = buildResolvedNamedReference {
name = primaryConstructor.symbol.callableId.callableName
resolvedSymbol = primaryConstructor.symbol
}
}
val returnExpression = buildReturnExpression {
result = initCallExpression
this.target = target
}
body = buildSingleExpressionBlock(returnExpression)
}.also {
target.bind(it)
// Refer to FIR backend ClassMemberGenerator for body generation.
}
)
}
@@ -25,9 +25,7 @@ FILE: annotated.kt
super<R|kotlin/Any|>()
}
public final fun copy(): R|Two| {
^copy R|/Two.Two|()
}
public final fun copy(): R|Two|
}
public? final? fun bar(two: Two): R|kotlin/Unit| {
@@ -13,21 +13,13 @@ FILE: destructuring.kt
public? final? val third: String = R|<local>/third|
public? get(): String
public final fun component1(): Int {
^component1 this@R|/Some|.R|/Some.first|
}
public final fun component1(): Int
public final fun component2(): Double {
^component2 this@R|/Some|.R|/Some.second|
}
public final fun component2(): Double
public final fun component3(): String {
^component3 this@R|/Some|.R|/Some.third|
}
public final fun component3(): String
public final fun copy(first: Int = this@R|/Some|.R|/Some.first|, second: Double = this@R|/Some|.R|/Some.second|, third: String = this@R|/Some|.R|/Some.third|): R|Some| {
^copy R|/Some.Some|(this@R|/Some|.R|/Some.first|, this@R|/Some|.R|/Some.second|, this@R|/Some|.R|/Some.third|)
}
public final fun copy(first: Int = this@R|/Some|.R|/Some.first|, second: Double = this@R|/Some|.R|/Some.second|, third: String = this@R|/Some|.R|/Some.third|): R|Some|
}
public? final? fun foo(some: Some): R|kotlin/Unit| {
@@ -48,17 +48,11 @@ FILE: for.kt
public? final? val y: Int = R|<local>/y|
public? get(): Int
public final fun component1(): Int {
^component1 this@R|/Some|.R|/Some.x|
}
public final fun component1(): Int
public final fun component2(): Int {
^component2 this@R|/Some|.R|/Some.y|
}
public final fun component2(): Int
public final fun copy(x: Int = this@R|/Some|.R|/Some.x|, y: Int = this@R|/Some|.R|/Some.y|): R|Some| {
^copy R|/Some.Some|(this@R|/Some|.R|/Some.x|, this@R|/Some|.R|/Some.y|)
}
public final fun copy(x: Int = this@R|/Some|.R|/Some.x|, y: Int = this@R|/Some|.R|/Some.y|): R|Some|
}
public? final? fun baz(set: Set<Some>): R|kotlin/Unit| {
@@ -10,17 +10,11 @@ FILE: lambda.kt
public? final? val y: Int = R|<local>/y|
public? get(): Int
public final fun component1(): Int {
^component1 this@R|/Tuple|.R|/Tuple.x|
}
public final fun component1(): Int
public final fun component2(): Int {
^component2 this@R|/Tuple|.R|/Tuple.y|
}
public final fun component2(): Int
public final fun copy(x: Int = this@R|/Tuple|.R|/Tuple.x|, y: Int = this@R|/Tuple|.R|/Tuple.y|): R|Tuple| {
^copy R|/Tuple.Tuple|(this@R|/Tuple|.R|/Tuple.x|, this@R|/Tuple|.R|/Tuple.y|)
}
public final fun copy(x: Int = this@R|/Tuple|.R|/Tuple.x|, y: Int = this@R|/Tuple|.R|/Tuple.y|): R|Tuple|
}
public? final? inline fun use(f: ( (Tuple) -> Int )): <implicit> {