@@ -173,7 +173,7 @@ fun doFile(invocationTrapFile: String,
173
173
}
174
174
175
175
fun <T > fakeLabel (): Label <T > {
176
- if (true ) {
176
+ if (false ) {
177
177
println (" Fake label" )
178
178
} else {
179
179
val sw = StringWriter ()
@@ -1415,53 +1415,144 @@ open class KotlinFileExtractor(
1415
1415
}
1416
1416
1417
1417
fun extractCall (c : IrCall , callable : Label <out DbCallable >, parent : Label <out DbExprparent >, idx : Int ) {
1418
- val exprId: Label <out DbExpr > = when (c.origin) {
1419
- PLUS -> {
1418
+ fun isFunction (pkgName : String , className : String? , fName : String ): Boolean {
1419
+ val verbose = false
1420
+ fun verboseln (s : String ) { if (verbose) println (s) }
1421
+ verboseln(" Attempting match for $pkgName $className $fName " )
1422
+ val target = c.symbol.owner
1423
+ if (target.name.asString() != fName) {
1424
+ verboseln(" No match as function name is ${target.name.asString()} not $fName " )
1425
+ return false
1426
+ }
1427
+ val extensionReceiverParameter = target.extensionReceiverParameter
1428
+ val targetClass = if (extensionReceiverParameter == null ) target.parent
1429
+ else (extensionReceiverParameter.type as ? IrSimpleType )?.classifier?.owner
1430
+ val targetPkg =
1431
+ if (className != null ) {
1432
+ if (targetClass !is IrClass ) {
1433
+ verboseln(" No match as didn't find target class" )
1434
+ return false
1435
+ }
1436
+ if (targetClass.name.asString() != className) {
1437
+ verboseln(" No match as class name is ${targetClass.name.asString()} not $className " )
1438
+ return false
1439
+ }
1440
+ targetClass.parent
1441
+ } else {
1442
+ targetClass
1443
+ }
1444
+ if (targetPkg !is IrPackageFragment ) {
1445
+ verboseln(" No match as didn't find target package" )
1446
+ return false
1447
+ }
1448
+ if (targetPkg.fqName.asString() != pkgName) {
1449
+ verboseln(" No match as class name is ${targetPkg.fqName.asString()} not $pkgName " )
1450
+ return false
1451
+ }
1452
+ verboseln(" Match" )
1453
+ return true
1454
+ }
1455
+
1456
+ fun binopDisp (id : Label <out DbExpr >) {
1457
+ val locId = tw.getLocation(c)
1458
+ tw.writeHasLocation(id, locId)
1459
+ tw.writeCallableEnclosingExpr(id, callable)
1460
+
1461
+ val dr = c.dispatchReceiver
1462
+ if (dr == null ) {
1463
+ logger.warnElement(Severity .ErrorSevere , " Dispatch receiver not found" , c)
1464
+ } else {
1465
+ extractExpressionExpr(dr, callable, id, 0 )
1466
+ }
1467
+ if (c.valueArgumentsCount < 1 ) {
1468
+ logger.warnElement(Severity .ErrorSevere , " No RHS found" , c)
1469
+ } else {
1470
+ if (c.valueArgumentsCount > 1 ) {
1471
+ logger.warnElement(Severity .ErrorSevere , " Extra arguments found" , c)
1472
+ }
1473
+ val arg = c.getValueArgument(0 )
1474
+ if (arg == null ) {
1475
+ logger.warnElement(Severity .ErrorSevere , " RHS null" , c)
1476
+ } else {
1477
+ extractExpressionExpr(arg, callable, id, 1 )
1478
+ }
1479
+ }
1480
+ }
1481
+
1482
+ fun binop (id : Label <out DbExpr >) {
1483
+ val locId = tw.getLocation(c)
1484
+ tw.writeHasLocation(id, locId)
1485
+ tw.writeCallableEnclosingExpr(id, callable)
1486
+
1487
+ val dr = c.dispatchReceiver
1488
+ if (dr != null ) {
1489
+ logger.warnElement(Severity .ErrorSevere , " Unexpected dispatch receiver found" , c)
1490
+ }
1491
+ if (c.valueArgumentsCount < 1 ) {
1492
+ logger.warnElement(Severity .ErrorSevere , " No arguments found" , c)
1493
+ } else {
1494
+ val lhs = c.getValueArgument(0 )
1495
+ if (lhs == null ) {
1496
+ logger.warnElement(Severity .ErrorSevere , " LHS null" , c)
1497
+ } else {
1498
+ extractExpressionExpr(lhs, callable, id, 0 )
1499
+ }
1500
+ if (c.valueArgumentsCount < 2 ) {
1501
+ logger.warnElement(Severity .ErrorSevere , " No RHS found" , c)
1502
+ } else {
1503
+ val rhs = c.getValueArgument(1 )
1504
+ if (rhs == null ) {
1505
+ logger.warnElement(Severity .ErrorSevere , " RHS null" , c)
1506
+ } else {
1507
+ extractExpressionExpr(rhs, callable, id, 1 )
1508
+ }
1509
+ }
1510
+ if (c.valueArgumentsCount > 2 ) {
1511
+ logger.warnElement(Severity .ErrorSevere , " Extra arguments found" , c)
1512
+ }
1513
+ }
1514
+ }
1515
+
1516
+ when {
1517
+ c.origin == PLUS &&
1518
+ (isFunction(" kotlin" , " Int" , " plus" ) || isFunction(" kotlin" , " String" , " plus" )) -> {
1420
1519
val id = tw.getFreshIdLabel<DbAddexpr >()
1421
1520
val type = useType(c.type)
1422
- val locId = tw.getLocation(c)
1423
1521
tw.writeExprs_addexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1424
- tw.writeHasLocation(id, locId)
1425
- tw.writeCallableEnclosingExpr(id, callable)
1522
+ binopDisp(id)
1426
1523
id
1427
1524
}
1428
- MINUS -> {
1525
+ c.origin == MINUS && isFunction( " kotlin " , " Int " , " minus " ) -> {
1429
1526
val id = tw.getFreshIdLabel<DbSubexpr >()
1430
1527
val type = useType(c.type)
1431
- val locId = tw.getLocation(c)
1432
1528
tw.writeExprs_subexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1433
- tw.writeHasLocation(id, locId)
1434
- tw.writeCallableEnclosingExpr(id, callable)
1529
+ binopDisp(id)
1435
1530
id
1436
1531
}
1437
- DIV -> {
1532
+ c.origin == DIV && isFunction( " kotlin " , " Int " , " div " ) -> {
1438
1533
val id = tw.getFreshIdLabel<DbDivexpr >()
1439
1534
val type = useType(c.type)
1440
- val locId = tw.getLocation(c)
1441
1535
tw.writeExprs_divexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1442
- tw.writeHasLocation(id, locId)
1443
- tw.writeCallableEnclosingExpr(id, callable)
1536
+ binopDisp(id)
1444
1537
id
1445
1538
}
1446
- PERC -> {
1539
+ c.origin == PERC && isFunction( " kotlin " , " Int " , " rem " ) -> {
1447
1540
val id = tw.getFreshIdLabel<DbRemexpr >()
1448
1541
val type = useType(c.type)
1449
- val locId = tw.getLocation(c)
1450
1542
tw.writeExprs_remexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1451
- tw.writeHasLocation(id, locId)
1452
- tw.writeCallableEnclosingExpr(id, callable)
1543
+ binopDisp(id)
1453
1544
id
1454
1545
}
1455
- EQEQ -> {
1546
+ c.origin == EQEQ && isFunction( " kotlin.internal.ir " , null , " EQEQ " ) -> {
1456
1547
val id = tw.getFreshIdLabel<DbEqexpr >()
1457
1548
val type = useType(c.type)
1458
- val locId = tw.getLocation(c)
1459
1549
tw.writeExprs_eqexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1460
- tw.writeHasLocation(id, locId)
1461
- tw.writeCallableEnclosingExpr(id, callable)
1550
+ binop(id)
1462
1551
id
1463
1552
}
1464
- EXCLEQ -> {
1553
+ /*
1554
+ TODO
1555
+ c.origin == EXCLEQ -> {
1465
1556
val id = tw.getFreshIdLabel<DbNeexpr>()
1466
1557
val type = useType(c.type)
1467
1558
val locId = tw.getLocation(c)
@@ -1470,40 +1561,33 @@ open class KotlinFileExtractor(
1470
1561
tw.writeCallableEnclosingExpr(id, callable)
1471
1562
id
1472
1563
}
1473
- LT -> {
1564
+ */
1565
+ c.origin == LT && isFunction(" kotlin.internal.ir" , null , " less" ) -> {
1474
1566
val id = tw.getFreshIdLabel<DbLtexpr >()
1475
1567
val type = useType(c.type)
1476
- val locId = tw.getLocation(c)
1477
1568
tw.writeExprs_ltexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1478
- tw.writeHasLocation(id, locId)
1479
- tw.writeCallableEnclosingExpr(id, callable)
1569
+ binop(id)
1480
1570
id
1481
1571
}
1482
- LTEQ -> {
1572
+ c.origin == LTEQ && isFunction( " kotlin.internal.ir " , null , " lessOrEqual " ) -> {
1483
1573
val id = tw.getFreshIdLabel<DbLeexpr >()
1484
1574
val type = useType(c.type)
1485
- val locId = tw.getLocation(c)
1486
1575
tw.writeExprs_leexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1487
- tw.writeHasLocation(id, locId)
1488
- tw.writeCallableEnclosingExpr(id, callable)
1576
+ binop(id)
1489
1577
id
1490
1578
}
1491
- GT -> {
1579
+ c.origin == GT && isFunction( " kotlin.internal.ir " , null , " greater " ) -> {
1492
1580
val id = tw.getFreshIdLabel<DbGtexpr >()
1493
1581
val type = useType(c.type)
1494
- val locId = tw.getLocation(c)
1495
1582
tw.writeExprs_gtexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1496
- tw.writeHasLocation(id, locId)
1497
- tw.writeCallableEnclosingExpr(id, callable)
1583
+ binop(id)
1498
1584
id
1499
1585
}
1500
- GTEQ -> {
1586
+ c.origin == GTEQ && isFunction( " kotlin.internal.ir " , null , " greaterOrEqual " ) -> {
1501
1587
val id = tw.getFreshIdLabel<DbGeexpr >()
1502
1588
val type = useType(c.type)
1503
- val locId = tw.getLocation(c)
1504
1589
tw.writeExprs_geexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1505
- tw.writeHasLocation(id, locId)
1506
- tw.writeCallableEnclosingExpr(id, callable)
1590
+ binop(id)
1507
1591
id
1508
1592
}
1509
1593
else -> {
@@ -1519,16 +1603,17 @@ open class KotlinFileExtractor(
1519
1603
// type arguments at index -2, -3, ...
1520
1604
extractTypeArguments(c, id, callable, - 2 , true )
1521
1605
id
1522
- }
1523
- }
1524
- val dr = c.dispatchReceiver
1525
- if (dr != null ) {
1526
- extractExpressionExpr(dr, callable, exprId, - 1 )
1527
- }
1528
- for (i in 0 until c.valueArgumentsCount) {
1529
- val arg = c.getValueArgument(i)
1530
- if (arg != null ) {
1531
- extractExpressionExpr(arg, callable, exprId, i)
1606
+
1607
+ val dr = c.dispatchReceiver
1608
+ if (dr != null ) {
1609
+ extractExpressionExpr(dr, callable, id, - 1 )
1610
+ }
1611
+ for (i in 0 until c.valueArgumentsCount) {
1612
+ val arg = c.getValueArgument(i)
1613
+ if (arg != null ) {
1614
+ extractExpressionExpr(arg, callable, id, i)
1615
+ }
1616
+ }
1532
1617
}
1533
1618
}
1534
1619
}
0 commit comments