Skip to content

Commit be65543

Browse files
committed
Use new terse extraction functions where applicable
1 parent 03c8958 commit be65543

File tree

1 file changed

+67
-152
lines changed

1 file changed

+67
-152
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 67 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,7 @@ open class KotlinFileExtractor(
389389
addModifiers(obinitId, "private")
390390

391391
// add body:
392-
val blockId = tw.getFreshIdLabel<DbBlock>()
393-
tw.writeStmts_block(blockId, obinitId, 0, obinitId)
394-
tw.writeHasLocation(blockId, locId)
392+
val blockId = extractBlockBody(obinitId, locId)
395393

396394
extractDeclInitializers(c.declarations, false) { Pair(blockId, obinitId) }
397395
}
@@ -707,10 +705,7 @@ open class KotlinFileExtractor(
707705
tw.writeHasLocation(clinitId, locId)
708706

709707
// add and return body block:
710-
Pair(tw.getFreshIdLabel<DbBlock>().also({
711-
tw.writeStmts_block(it, clinitId, 0, clinitId)
712-
tw.writeHasLocation(it, locId)
713-
}), clinitId)
708+
Pair(extractBlockBody(clinitId, locId), clinitId)
714709
}
715710
}
716711
}
@@ -775,32 +770,18 @@ open class KotlinFileExtractor(
775770
val expr = initializer.expression
776771

777772
val declLocId = tw.getLocation(f)
778-
val stmtId = tw.getFreshIdLabel<DbExprstmt>()
779-
tw.writeStmts_exprstmt(stmtId, blockAndFunctionId.first, idx++, blockAndFunctionId.second)
780-
tw.writeHasLocation(stmtId, declLocId)
781-
val assignmentId = tw.getFreshIdLabel<DbAssignexpr>()
782-
val type = useType(if (isAnnotationClassField) kClassToJavaClass(expr.type) else expr.type)
783-
tw.writeExprs_assignexpr(assignmentId, type.javaResult.id, stmtId, 0)
784-
tw.writeExprsKotlinType(assignmentId, type.kotlinResult.id)
785-
tw.writeHasLocation(assignmentId, declLocId)
786-
tw.writeCallableEnclosingExpr(assignmentId, blockAndFunctionId.second)
787-
tw.writeStatementEnclosingExpr(assignmentId, stmtId)
788-
tw.writeKtInitializerAssignment(assignmentId)
789-
790-
val lhsId = tw.getFreshIdLabel<DbVaraccess>()
791-
tw.writeExprs_varaccess(lhsId, lhsType.javaResult.id, assignmentId, 0)
792-
tw.writeExprsKotlinType(lhsId, lhsType.kotlinResult.id)
793-
tw.writeHasLocation(lhsId, declLocId)
794-
tw.writeCallableEnclosingExpr(lhsId, blockAndFunctionId.second)
795-
tw.writeStatementEnclosingExpr(lhsId, stmtId)
796-
797-
tw.writeVariableBinding(lhsId, vId)
798-
799-
if (static) {
800-
extractStaticTypeAccessQualifier(f, lhsId, declLocId, blockAndFunctionId.second, stmtId)
773+
extractExpressionStmt(declLocId, blockAndFunctionId.first, idx++, blockAndFunctionId.second).also { stmtId ->
774+
val type = if (isAnnotationClassField) kClassToJavaClass(expr.type) else expr.type
775+
extractAssignExpr(type, declLocId, stmtId, 0, blockAndFunctionId.second, stmtId).also { assignmentId ->
776+
tw.writeKtInitializerAssignment(assignmentId)
777+
extractVariableAccess(vId, lhsType, declLocId, assignmentId, 0, blockAndFunctionId.second, stmtId).also { lhsId ->
778+
if (static) {
779+
extractStaticTypeAccessQualifier(f, lhsId, declLocId, blockAndFunctionId.second, stmtId)
780+
}
781+
}
782+
extractExpressionExpr(expr, blockAndFunctionId.second, assignmentId, 1, stmtId)
783+
}
801784
}
802-
803-
extractExpressionExpr(expr, blockAndFunctionId.second, assignmentId, 1, stmtId)
804785
}
805786

806787
for (decl in declarations) {
@@ -993,10 +974,7 @@ open class KotlinFileExtractor(
993974

994975
// Create a synthetic function body that calls the real function supplying default arguments where required:
995976
if (f is IrConstructor) {
996-
val blockId = tw.getFreshIdLabel<DbBlock>()
997-
tw.writeStmts_block(blockId, overloadId, 0, overloadId)
998-
tw.writeHasLocation(blockId, realFunctionLocId)
999-
977+
val blockId = extractBlockBody(overloadId, realFunctionLocId)
1000978
val constructorCallId = tw.getFreshIdLabel<DbConstructorinvocationstmt>()
1001979
tw.writeStmts_constructorinvocationstmt(constructorCallId, blockId, 0, overloadId)
1002980
tw.writeHasLocation(constructorCallId, realFunctionLocId)
@@ -1356,11 +1334,8 @@ open class KotlinFileExtractor(
13561334
}
13571335
}
13581336

1359-
fun extractExpressionBody(callable: Label<out DbCallable>, locId: Label<out DbLocation>): Label<out DbStmt> {
1360-
val blockId = tw.getFreshIdLabel<DbBlock>()
1361-
tw.writeStmts_block(blockId, callable, 0, callable)
1362-
tw.writeHasLocation(blockId, locId)
1363-
1337+
fun extractExpressionBody(callable: Label<out DbCallable>, locId: Label<DbLocation>): Label<out DbStmt> {
1338+
val blockId = extractBlockBody(callable, locId)
13641339
return tw.getFreshIdLabel<DbReturnstmt>().also { returnId ->
13651340
tw.writeStmts_returnstmt(returnId, blockId, 0, callable)
13661341
tw.writeHasLocation(returnId, locId)
@@ -2664,15 +2639,7 @@ open class KotlinFileExtractor(
26642639
tw.writeStatementEnclosingExpr(initId, enclosingStmt)
26652640
it.elements.forEachIndexed { i, arg -> extractVarargElement(arg, callable, initId, i, enclosingStmt) }
26662641

2667-
val dim = it.elements.size
2668-
val dimId = tw.getFreshIdLabel<DbIntegerliteral>()
2669-
val dimType = useType(pluginContext.irBuiltIns.intType)
2670-
tw.writeExprs_integerliteral(dimId, dimType.javaResult.id, id, 0)
2671-
tw.writeExprsKotlinType(dimId, dimType.kotlinResult.id)
2672-
tw.writeHasLocation(dimId, locId)
2673-
tw.writeCallableEnclosingExpr(dimId, callable)
2674-
tw.writeStatementEnclosingExpr(dimId, enclosingStmt)
2675-
tw.writeNamestrings(dim.toString(), dim.toString(), dimId)
2642+
extractConstantInteger(it.elements.size, locId, id, 0, callable, enclosingStmt)
26762643
}
26772644
}
26782645
}
@@ -2744,27 +2711,21 @@ open class KotlinFileExtractor(
27442711

27452712
if (array != null && arrayIdx != null && assignedValue != null) {
27462713

2747-
val assignId = tw.getFreshIdLabel<DbAssignexpr>()
2748-
val type = useType(c.type)
27492714
val locId = tw.getLocation(c)
2750-
tw.writeExprs_assignexpr(assignId, type.javaResult.id, parent, idx)
2751-
tw.writeExprsKotlinType(assignId, type.kotlinResult.id)
2752-
tw.writeHasLocation(assignId, locId)
2753-
tw.writeCallableEnclosingExpr(assignId, callable)
2754-
tw.writeStatementEnclosingExpr(assignId, enclosingStmt)
2755-
2756-
val arrayAccessId = tw.getFreshIdLabel<DbArrayaccess>()
2757-
val arrayType = useType(array.type)
2758-
tw.writeExprs_arrayaccess(arrayAccessId, arrayType.javaResult.id, assignId, 0)
2759-
tw.writeExprsKotlinType(arrayAccessId, arrayType.kotlinResult.id)
2760-
tw.writeHasLocation(arrayAccessId, locId)
2761-
tw.writeCallableEnclosingExpr(arrayAccessId, callable)
2762-
tw.writeStatementEnclosingExpr(arrayAccessId, enclosingStmt)
2763-
2764-
extractExpressionExpr(array, callable, arrayAccessId, 0, enclosingStmt)
2765-
extractExpressionExpr(arrayIdx, callable, arrayAccessId, 1, enclosingStmt)
2766-
2767-
extractExpressionExpr(assignedValue, callable, assignId, 1, enclosingStmt)
2715+
extractAssignExpr(c.type, locId, parent, idx, callable, enclosingStmt).also { assignId ->
2716+
tw.getFreshIdLabel<DbArrayaccess>().also { arrayAccessId ->
2717+
val arrayType = useType(array.type)
2718+
tw.writeExprs_arrayaccess(arrayAccessId, arrayType.javaResult.id, assignId, 0)
2719+
tw.writeExprsKotlinType(arrayAccessId, arrayType.kotlinResult.id)
2720+
tw.writeHasLocation(arrayAccessId, locId)
2721+
tw.writeCallableEnclosingExpr(arrayAccessId, callable)
2722+
tw.writeStatementEnclosingExpr(arrayAccessId, enclosingStmt)
2723+
2724+
extractExpressionExpr(array, callable, arrayAccessId, 0, enclosingStmt)
2725+
extractExpressionExpr(arrayIdx, callable, arrayAccessId, 1, enclosingStmt)
2726+
}
2727+
extractExpressionExpr(assignedValue, callable, assignId, 1, enclosingStmt)
2728+
}
27682729

27692730
} else {
27702731
logger.errorElement("Unexpected Array.set function signature", c)
@@ -3002,16 +2963,12 @@ open class KotlinFileExtractor(
30022963
}
30032964

30042965
inner class StmtParent(val parent: Label<out DbStmtparent>, val idx: Int): StmtExprParent() {
3005-
override fun stmt(e: IrExpression, callable: Label<out DbCallable>): StmtParent {
3006-
return this
3007-
}
3008-
override fun expr(e: IrExpression, callable: Label<out DbCallable>): ExprParent {
3009-
val id = tw.getFreshIdLabel<DbExprstmt>()
3010-
val locId = tw.getLocation(e)
3011-
tw.writeStmts_exprstmt(id, parent, idx, callable)
3012-
tw.writeHasLocation(id, locId)
3013-
return ExprParent(id, 0, id)
3014-
}
2966+
override fun stmt(e: IrExpression, callable: Label<out DbCallable>) = this
2967+
2968+
override fun expr(e: IrExpression, callable: Label<out DbCallable>) =
2969+
extractExpressionStmt(tw.getLocation(e), parent, idx, callable).let { id ->
2970+
ExprParent(id, 0, id)
2971+
}
30152972
}
30162973
inner class ExprParent(val parent: Label<out DbExprparent>, val idx: Int, val enclosingStmt: Label<out DbStmt>): StmtExprParent() {
30172974
override fun stmt(e: IrExpression, callable: Label<out DbCallable>): StmtParent {
@@ -3798,16 +3755,18 @@ open class KotlinFileExtractor(
37983755
extractTypeAccessRecursive(irType, locId, it, 0)
37993756
}
38003757

3801-
private fun extractThisAccess(irType: IrType, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>, locId: Label<out DbLocation>) =
3758+
private fun extractThisAccess(type: TypeResults, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>, locId: Label<out DbLocation>) =
38023759
tw.getFreshIdLabel<DbThisaccess>().also {
3803-
val type = useType(irType)
38043760
tw.writeExprs_thisaccess(it, type.javaResult.id, parent, idx)
38053761
tw.writeExprsKotlinType(it, type.kotlinResult.id)
38063762
tw.writeHasLocation(it, locId)
38073763
tw.writeCallableEnclosingExpr(it, callable)
38083764
tw.writeStatementEnclosingExpr(it, enclosingStmt)
38093765
}
38103766

3767+
private fun extractThisAccess(irType: IrType, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>, locId: Label<out DbLocation>) =
3768+
extractThisAccess(useType(irType), callable, parent, idx, enclosingStmt, locId)
3769+
38113770
private fun extractThisAccess(e: IrGetValue, thisParamParent: IrDeclarationParent, exprParent: ExprParent, callable: Label<out DbCallable>) {
38123771
val containingDeclaration = declarationStack.peek().first
38133772
val locId = tw.getLocation(e)
@@ -3866,9 +3825,8 @@ open class KotlinFileExtractor(
38663825
}
38673826
}
38683827

3869-
private fun extractVariableAccess(variable: Label<out DbVariable>?, irType: IrType, locId: Label<out DbLocation>, parent: Label<out DbExprparent>, idx: Int, callable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) =
3828+
private fun extractVariableAccess(variable: Label<out DbVariable>?, type: TypeResults, locId: Label<out DbLocation>, parent: Label<out DbExprparent>, idx: Int, callable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) =
38703829
tw.getFreshIdLabel<DbVaraccess>().also {
3871-
val type = useType(irType)
38723830
tw.writeExprs_varaccess(it, type.javaResult.id, parent, idx)
38733831
tw.writeExprsKotlinType(it, type.kotlinResult.id)
38743832
tw.writeHasLocation(it, locId)
@@ -3880,6 +3838,9 @@ open class KotlinFileExtractor(
38803838
}
38813839
}
38823840

3841+
private fun extractVariableAccess(variable: Label<out DbVariable>?, irType: IrType, locId: Label<out DbLocation>, parent: Label<out DbExprparent>, idx: Int, callable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) =
3842+
extractVariableAccess(variable, useType(irType), locId, parent, idx, callable, enclosingStmt)
3843+
38833844
private fun extractLoop(
38843845
loop: IrLoop,
38853846
stmtExprParent: StmtExprParent,
@@ -3953,33 +3914,16 @@ open class KotlinFileExtractor(
39533914
stmtIdx: Int
39543915
) {
39553916
val paramId = tw.getFreshIdLabel<DbParam>()
3956-
val paramTypeRes = extractValueParameter(paramId, paramType, paramName, locId, ids.constructor, paramIdx, paramId, syntheticParameterNames = false, isVararg = false, isNoinline = false, isCrossinline = false)
3957-
3958-
val assignmentStmtId = tw.getFreshIdLabel<DbExprstmt>()
3959-
tw.writeStmts_exprstmt(assignmentStmtId, ids.constructorBlock, stmtIdx, ids.constructor)
3960-
tw.writeHasLocation(assignmentStmtId, locId)
3961-
3962-
val assignmentId = tw.getFreshIdLabel<DbAssignexpr>()
3963-
tw.writeExprs_assignexpr(assignmentId, paramTypeRes.javaResult.id, assignmentStmtId, 0)
3964-
tw.writeExprsKotlinType(assignmentId, paramTypeRes.kotlinResult.id)
3965-
writeExpressionMetadataToTrapFile(assignmentId, ids.constructor, assignmentStmtId)
3966-
3967-
val lhsId = tw.getFreshIdLabel<DbVaraccess>()
3968-
tw.writeExprs_varaccess(lhsId, paramTypeRes.javaResult.id, assignmentId, 0)
3969-
tw.writeExprsKotlinType(lhsId, paramTypeRes.kotlinResult.id)
3970-
tw.writeVariableBinding(lhsId, fieldId)
3971-
writeExpressionMetadataToTrapFile(lhsId, ids.constructor, assignmentStmtId)
3972-
3973-
val thisId = tw.getFreshIdLabel<DbThisaccess>()
3974-
tw.writeExprs_thisaccess(thisId, ids.type.javaResult.id, lhsId, -1)
3975-
tw.writeExprsKotlinType(thisId, ids.type.kotlinResult.id)
3976-
writeExpressionMetadataToTrapFile(thisId, ids.constructor, assignmentStmtId)
3977-
3978-
val rhsId = tw.getFreshIdLabel<DbVaraccess>()
3979-
tw.writeExprs_varaccess(rhsId, paramTypeRes.javaResult.id, assignmentId, 1)
3980-
tw.writeExprsKotlinType(rhsId, paramTypeRes.kotlinResult.id)
3981-
tw.writeVariableBinding(rhsId, paramId)
3982-
writeExpressionMetadataToTrapFile(rhsId, ids.constructor, assignmentStmtId)
3917+
extractValueParameter(paramId, paramType, paramName, locId, ids.constructor, paramIdx, paramId, syntheticParameterNames = false, isVararg = false, isNoinline = false, isCrossinline = false)
3918+
3919+
extractExpressionStmt(locId, ids.constructorBlock, stmtIdx, ids.constructor).also { assignmentStmtId ->
3920+
extractAssignExpr(paramType, locId, assignmentStmtId, 0, ids.constructor, assignmentStmtId).also { assignmentId ->
3921+
extractVariableAccess(fieldId, paramType, locId, assignmentId, 0, ids.constructor, assignmentStmtId).also { lhsId ->
3922+
extractThisAccess(ids.type, ids.constructor, lhsId, -1, assignmentStmtId, locId)
3923+
}
3924+
extractVariableAccess(paramId, paramType, locId, assignmentId, 1, ids.constructor, assignmentStmtId)
3925+
}
3926+
}
39833927
}
39843928
}
39853929

@@ -4037,35 +3981,22 @@ open class KotlinFileExtractor(
40373981
}
40383982

40393983
protected fun writeThisAccess(parent: Label<out DbExprparent>, callable: Label<out DbCallable>, stmt: Label<out DbStmt>) {
4040-
val thisId = tw.getFreshIdLabel<DbThisaccess>()
4041-
tw.writeExprs_thisaccess(thisId, ids.type.javaResult.id, parent, -1)
4042-
tw.writeExprsKotlinType(thisId, ids.type.kotlinResult.id)
4043-
writeExpressionMetadataToTrapFile(thisId, callable, stmt)
3984+
extractThisAccess(ids.type, callable, parent, -1, stmt, locId)
40443985
}
40453986

40463987
fun extractFieldWriteOfReflectionTarget(
40473988
labels: FunctionLabels, // labels of the containing function
40483989
target: IrFieldSymbol, // the target field being accessed)
40493990
) {
4050-
// ...;
4051-
val exprStmtId = tw.getFreshIdLabel<DbExprstmt>()
4052-
tw.writeStmts_exprstmt(exprStmtId, labels.blockId, 0, labels.methodId)
4053-
tw.writeHasLocation(exprStmtId, locId)
4054-
40553991
val fieldType = useType(target.owner.type)
40563992

4057-
// ... = ...
4058-
val assignExprId = tw.getFreshIdLabel<DbAssignexpr>()
4059-
tw.writeExprs_assignexpr(assignExprId, fieldType.javaResult.id, exprStmtId, 0)
4060-
tw.writeExprsKotlinType(assignExprId, fieldType.kotlinResult.id)
4061-
writeExpressionMetadataToTrapFile(assignExprId, labels.methodId, exprStmtId)
4062-
4063-
// LHS
4064-
extractFieldAccess(fieldType, assignExprId, exprStmtId, labels, target)
4065-
4066-
// RHS
4067-
val p = labels.parameters.first()
4068-
writeVariableAccessInFunctionBody(p.second, 1, p.first, assignExprId, labels.methodId, exprStmtId)
3993+
extractExpressionStmt(locId, labels.blockId, 0, labels.methodId).also { exprStmtId ->
3994+
extractAssignExpr(target.owner.type, locId, exprStmtId, 0, labels.methodId, exprStmtId).also { assignExprId ->
3995+
extractFieldAccess(fieldType, assignExprId, exprStmtId, labels, target)
3996+
val p = labels.parameters.first()
3997+
writeVariableAccessInFunctionBody(p.second, 1, p.first, assignExprId, labels.methodId, exprStmtId)
3998+
}
3999+
}
40694000
}
40704001

40714002
fun extractFieldReturnOfReflectionTarget(
@@ -4641,12 +4572,7 @@ open class KotlinFileExtractor(
46414572
addModifiers(methodId, "public")
46424573
addModifiers(methodId, "override")
46434574

4644-
// Block
4645-
val blockId = tw.getFreshIdLabel<DbBlock>()
4646-
tw.writeStmts_block(blockId, methodId, 0, methodId)
4647-
tw.writeHasLocation(blockId, locId)
4648-
4649-
return FunctionLabels(methodId, blockId, parameters)
4575+
return FunctionLabels(methodId, extractBlockBody(methodId, locId), parameters)
46504576
}
46514577

46524578
/*
@@ -4688,10 +4614,7 @@ open class KotlinFileExtractor(
46884614
tw.writeCallableBinding(callId, calledMethodId)
46894615

46904616
// this access
4691-
val thisId = tw.getFreshIdLabel<DbThisaccess>()
4692-
tw.writeExprs_thisaccess(thisId, ids.type.javaResult.id, callId, -1)
4693-
tw.writeExprsKotlinType(thisId, ids.type.kotlinResult.id)
4694-
extractCommonExpr(thisId)
4617+
extractThisAccess(ids.type, funLabels.methodId, callId, -1, retId, locId)
46954618

46964619
addArgumentsToInvocationInInvokeNBody(parameters.map { it.type }, funLabels, retId, callId, locId, ::extractCommonExpr)
46974620
}
@@ -4953,13 +4876,7 @@ open class KotlinFileExtractor(
49534876
tw.writeExprsKotlinType(initId, at.kotlinResult.id)
49544877
extractCommonExpr(initId)
49554878

4956-
val dim = arraySize.toString()
4957-
val dimId = tw.getFreshIdLabel<DbIntegerliteral>()
4958-
val dimType = useType(pluginContext.irBuiltIns.intType)
4959-
tw.writeExprs_integerliteral(dimId, dimType.javaResult.id, arrayCreationId, 0)
4960-
tw.writeExprsKotlinType(dimId, dimType.kotlinResult.id)
4961-
extractCommonExpr(dimId)
4962-
tw.writeNamestrings(dim, dim, dimId)
4879+
extractConstantInteger(arraySize, locId, arrayCreationId, 0, enclosingCallable, enclosingStmt)
49634880

49644881
return initId
49654882
}
@@ -5158,9 +5075,7 @@ open class KotlinFileExtractor(
51585075
}
51595076

51605077
//body
5161-
val blockId = tw.getFreshIdLabel<DbBlock>()
5162-
tw.writeStmts_block(blockId, ids.function, 0, ids.function)
5163-
tw.writeHasLocation(blockId, locId)
5078+
val blockId = extractBlockBody(ids.function, locId)
51645079

51655080
//return stmt
51665081
val returnId = tw.getFreshIdLabel<DbReturnstmt>()

0 commit comments

Comments
 (0)