Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91eafaf

Browse files
tamasvajkigfoo
authored andcommitted
Extract delegating constructor calls
1 parent 6619584 commit 91eafaf

1 file changed

Lines changed: 60 additions & 28 deletions

File tree

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

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
464464
tw.writeHasLocation(id, locId)
465465
val body = f.body
466466
if(body != null) {
467-
extractBody(body, id)
467+
extractBody(body, id, f)
468468
}
469469
f.valueParameters.forEachIndexed { i, vp ->
470470
extractValueParameter(vp, id, i)
@@ -502,28 +502,28 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
502502
}
503503
}
504504

505-
fun extractBody(b: IrBody, callable: Label<out DbCallable>) {
505+
fun extractBody(b: IrBody, callable: Label<out DbCallable>, irCallable: IrFunction) {
506506
when(b) {
507-
is IrBlockBody -> extractBlockBody(b, callable, callable, 0)
507+
is IrBlockBody -> extractBlockBody(b, callable, irCallable, callable, 0)
508508
else -> logger.warnElement(Severity.ErrorSevere, "Unrecognised IrBody: " + b.javaClass, b)
509509
}
510510
}
511511

512-
fun extractBlockBody(b: IrBlockBody, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
512+
fun extractBlockBody(b: IrBlockBody, callable: Label<out DbCallable>, irCallable: IrFunction, parent: Label<out DbStmtparent>, idx: Int) {
513513
val id = tw.getFreshIdLabel<DbBlock>()
514514
val locId = tw.getLocation(b)
515515
tw.writeStmts_block(id, parent, idx, callable)
516516
tw.writeHasLocation(id, locId)
517517
for((sIdx, stmt) in b.statements.withIndex()) {
518-
extractStatement(stmt, callable, id, sIdx)
518+
extractStatement(stmt, callable, irCallable, id, sIdx)
519519
}
520520
}
521521

522522
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
523523
return tw.getVariableLabelFor<DbLocalvar>(v)
524524
}
525525

526-
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>) {
526+
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>, irCallable: IrFunction) {
527527
val id = useVariable(v)
528528
val locId = tw.getLocation(v)
529529
val typeId = useType(v.type)
@@ -534,17 +534,17 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
534534
tw.writeHasLocation(id, locId)
535535
val i = v.initializer
536536
if(i != null) {
537-
extractExpression(i, callable, decId, 0)
537+
extractExpression(i, callable, irCallable, decId, 0)
538538
}
539539
}
540540

541-
fun extractStatement(s: IrStatement, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
541+
fun extractStatement(s: IrStatement, callable: Label<out DbCallable>, irCallable: IrFunction, parent: Label<out DbStmtparent>, idx: Int) {
542542
when(s) {
543543
is IrExpression -> {
544-
extractExpression(s, callable, parent, idx)
544+
extractExpression(s, callable, irCallable, parent, idx)
545545
}
546546
is IrVariable -> {
547-
extractVariable(s, callable)
547+
extractVariable(s, callable, irCallable)
548548
}
549549
else -> {
550550
logger.warnElement(Severity.ErrorSevere, "Unrecognised IrStatement: " + s.javaClass, s)
@@ -567,7 +567,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
567567
}
568568
}
569569

570-
fun extractCall(c: IrCall, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
570+
fun extractCall(c: IrCall, callable: Label<out DbCallable>, irCallable: IrFunction, parent: Label<out DbExprparent>, idx: Int) {
571571
val exprId: Label<out DbExpr> = when {
572572
c.origin == PLUS -> {
573573
val id = tw.getFreshIdLabel<DbAddexpr>()
@@ -653,12 +653,12 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
653653
val dr = c.dispatchReceiver
654654
val offset = if(dr == null) 0 else 1
655655
if(dr != null) {
656-
extractExpression(dr, callable, exprId, 0) // todo: should this be at index -1 instead?
656+
extractExpression(dr, callable, irCallable, exprId, 0) // todo: should this be at index -1 instead?
657657
}
658658
for(i in 0 until c.valueArgumentsCount) {
659659
val arg = c.getValueArgument(i)
660660
if(arg != null) {
661-
extractExpression(arg, callable, exprId, i + offset)
661+
extractExpression(arg, callable, irCallable, exprId, i + offset)
662662
}
663663
}
664664

@@ -667,8 +667,40 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
667667

668668
private val loopIdMap: MutableMap<IrLoop, Label<out DbKtloopstmt>> = mutableMapOf()
669669

670-
fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
670+
fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, irCallable: IrFunction, parent: Label<out DbExprparent>, idx: Int) {
671671
when(e) {
672+
is IrDelegatingConstructorCall -> {
673+
val delegatingClass = e.symbol.owner.parent as IrClass
674+
val currentClass = (irCallable as IrDeclaration).parent as IrClass
675+
676+
val id: Label<out DbStmt>
677+
if (delegatingClass != currentClass) {
678+
id = tw.getFreshIdLabel<DbSuperconstructorinvocationstmt>()
679+
tw.writeStmts_superconstructorinvocationstmt(id, parent, idx, callable)
680+
} else {
681+
id = tw.getFreshIdLabel<DbConstructorinvocationstmt>()
682+
tw.writeStmts_constructorinvocationstmt(id, parent, idx, callable)
683+
}
684+
685+
val locId = tw.getLocation(e)
686+
val methodId = useFunction(e.symbol.owner)
687+
688+
tw.writeHasLocation(id, locId)
689+
@Suppress("UNCHECKED_CAST")
690+
tw.writeCallableBinding(id as Label<DbCaller>, methodId)
691+
for (i in 0 until e.valueArgumentsCount) {
692+
val arg = e.getValueArgument(i)
693+
if (arg != null) {
694+
extractExpression(arg, callable, irCallable, id, i)
695+
}
696+
}
697+
val dr = e.dispatchReceiver
698+
if (dr != null) {
699+
extractExpression(dr, callable, irCallable, id, -1)
700+
}
701+
702+
// todo: type arguments at index -2, -3, ...
703+
}
672704
is IrConstructorCall -> {
673705
val id = tw.getFreshIdLabel<DbNewexpr>()
674706
val typeId = useType(e.type)
@@ -680,17 +712,17 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
680712
for (i in 0 until e.valueArgumentsCount) {
681713
val arg = e.getValueArgument(i)
682714
if (arg != null) {
683-
extractExpression(arg, callable, id, i)
715+
extractExpression(arg, callable, irCallable, id, i)
684716
}
685717
}
686718
val dr = e.dispatchReceiver
687719
if (dr != null) {
688-
extractExpression(dr, callable, id, -3)
720+
extractExpression(dr, callable, irCallable, id, -3)
689721
}
690722

691723
// todo: type arguments at index -4, -5, ...
692724
}
693-
is IrCall -> extractCall(e, callable, parent, idx)
725+
is IrCall -> extractCall(e, callable, irCallable, parent, idx)
694726
is IrConst<*> -> {
695727
val v = e.value
696728
when(v) {
@@ -772,14 +804,14 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
772804
val vId = useValueDeclaration(e.symbol.owner)
773805
tw.writeVariableBinding(lhsId, vId)
774806

775-
extractExpression(e.value, callable, id, 1)
807+
extractExpression(e.value, callable, irCallable, id, 1)
776808
}
777809
is IrThrow -> {
778810
val id = tw.getFreshIdLabel<DbThrowstmt>()
779811
val locId = tw.getLocation(e)
780812
tw.writeStmts_throwstmt(id, parent, idx, callable)
781813
tw.writeHasLocation(id, locId)
782-
extractExpression(e.value, callable, id, 0)
814+
extractExpression(e.value, callable, irCallable, id, 0)
783815
}
784816
is IrBreak -> {
785817
val id = tw.getFreshIdLabel<DbBreakstmt>()
@@ -796,15 +828,15 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
796828
val locId = tw.getLocation(e)
797829
tw.writeStmts_returnstmt(id, parent, idx, callable)
798830
tw.writeHasLocation(id, locId)
799-
extractExpression(e.value, callable, id, 0)
831+
extractExpression(e.value, callable, irCallable, id, 0)
800832
}
801833
is IrContainerExpression -> {
802834
val id = tw.getFreshIdLabel<DbBlock>()
803835
val locId = tw.getLocation(e)
804836
tw.writeStmts_block(id, parent, idx, callable)
805837
tw.writeHasLocation(id, locId)
806838
e.statements.forEachIndexed { i, s ->
807-
extractStatement(s, callable, id, i)
839+
extractStatement(s, callable, irCallable, id, i)
808840
}
809841
}
810842
is IrWhileLoop -> {
@@ -813,10 +845,10 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
813845
val locId = tw.getLocation(e)
814846
tw.writeStmts_whilestmt(id, parent, idx, callable)
815847
tw.writeHasLocation(id, locId)
816-
extractExpression(e.condition, callable, id, 0)
848+
extractExpression(e.condition, callable, irCallable, id, 0)
817849
val body = e.body
818850
if(body != null) {
819-
extractExpression(body, callable, id, 1)
851+
extractExpression(body, callable, irCallable, id, 1)
820852
}
821853
loopIdMap.remove(e)
822854
}
@@ -826,10 +858,10 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
826858
val locId = tw.getLocation(e)
827859
tw.writeStmts_dostmt(id, parent, idx, callable)
828860
tw.writeHasLocation(id, locId)
829-
extractExpression(e.condition, callable, id, 0)
861+
extractExpression(e.condition, callable, irCallable, id, 0)
830862
val body = e.body
831863
if(body != null) {
832-
extractExpression(body, callable, id, 1)
864+
extractExpression(body, callable, irCallable, id, 1)
833865
}
834866
loopIdMap.remove(e)
835867
}
@@ -847,8 +879,8 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
847879
val bLocId = tw.getLocation(b)
848880
tw.writeWhen_branch(bId, id, i)
849881
tw.writeHasLocation(bId, bLocId)
850-
extractExpression(b.condition, callable, bId, 0)
851-
extractExpression(b.result, callable, bId, 1)
882+
extractExpression(b.condition, callable, irCallable, bId, 0)
883+
extractExpression(b.result, callable, irCallable, bId, 1)
852884
if(b is IrElseBranch) {
853885
tw.writeWhen_branch_else(bId)
854886
}
@@ -860,7 +892,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
860892
val typeId = useType(e.type)
861893
tw.writeExprs_getclassexpr(id, typeId, parent, idx)
862894
tw.writeHasLocation(id, locId)
863-
extractExpression(e.argument, callable, id, 0)
895+
extractExpression(e.argument, callable, irCallable, id, 0)
864896
}
865897
else -> {
866898
logger.warnElement(Severity.ErrorSevere, "Unrecognised IrExpression: " + e.javaClass, e)

0 commit comments

Comments
 (0)