Kotlin: Implement JvmOverloads annotation

This generates functions that omit parameters with default values, rightmost first, such that Java can achieve a similar experience to Kotlin (which represents calls internally as if the default was supplied explicitly, and/or uses a $default method that supplies the needed arguments).

A complication: combining JvmOverloads with JvmStatic means that both the companion object and the surrounding class get overloads.
This commit is contained in:
Chris Smowton
2022-07-12 19:39:24 +01:00
parent d52d3d7b75
commit d3d3ce843a
23 changed files with 1261 additions and 67 deletions

View File

@@ -0,0 +1,591 @@
test.kt:
# 0| [CompilationUnit] test
# 0| 1: [Class] TestKt
# 1| 1: [Method] getString
# 1| 3: [TypeAccess] String
# 1| 5: [BlockStmt] { ... }
# 1| 0: [ReturnStmt] return ...
# 1| 0: [StringLiteral] Hello world
# 45| 2: [ExtensionMethod] testExtensionFunction
# 45| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 45| 0: [Parameter] <this>
# 45| 0: [TypeAccess] Test
# 45| 1: [Parameter] a
# 45| 0: [TypeAccess] int
# 45| 2: [Parameter] c
# 45| 0: [TypeAccess] double
# 45| 3: [Parameter] e
# 45| 0: [TypeAccess] boolean
# 45| 5: [BlockStmt] { ... }
# 45| 0: [ReturnStmt] return ...
# 45| 0: [MethodAccess] testExtensionFunction(...)
# 45| -1: [TypeAccess] TestKt
# 45| 0: [ExtensionReceiverAccess] this
# 45| 1: [VarAccess] a
# 45| 2: [MethodAccess] getString(...)
# 45| -1: [TypeAccess] TestKt
# 45| 3: [VarAccess] c
# 45| 4: [FloatLiteral] 1.0
# 45| 5: [VarAccess] e
# 45| 3: [ExtensionMethod] testExtensionFunction
# 45| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 45| 0: [Parameter] <this>
# 45| 0: [TypeAccess] Test
# 45| 1: [Parameter] a
# 45| 0: [TypeAccess] int
# 45| 2: [Parameter] b
# 45| 0: [TypeAccess] String
# 45| 3: [Parameter] c
# 45| 0: [TypeAccess] double
# 45| 4: [Parameter] e
# 45| 0: [TypeAccess] boolean
# 45| 5: [BlockStmt] { ... }
# 45| 0: [ReturnStmt] return ...
# 45| 0: [MethodAccess] testExtensionFunction(...)
# 45| -1: [TypeAccess] TestKt
# 45| 0: [ExtensionReceiverAccess] this
# 45| 1: [VarAccess] a
# 45| 2: [VarAccess] b
# 45| 3: [VarAccess] c
# 45| 4: [FloatLiteral] 1.0
# 45| 5: [VarAccess] e
# 45| 4: [ExtensionMethod] testExtensionFunction
# 45| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 45| 0: [Parameter] <this>
# 45| 0: [TypeAccess] Test
# 45| 1: [Parameter] a
# 45| 0: [TypeAccess] int
# 45| 2: [Parameter] b
# 45| 0: [TypeAccess] String
# 45| 3: [Parameter] c
# 45| 0: [TypeAccess] double
# 45| 4: [Parameter] d
# 45| 0: [TypeAccess] float
# 45| 5: [Parameter] e
# 45| 0: [TypeAccess] boolean
# 45| 5: [BlockStmt] { ... }
# 45| 0: [ReturnStmt] return ...
# 45| 0: [VarAccess] a
# 3| 2: [Class] Test
# 3| 1: [Constructor] Test
# 3| 5: [BlockStmt] { ... }
# 3| 0: [SuperConstructorInvocationStmt] super(...)
# 3| 1: [BlockStmt] { ... }
# 6| 2: [Method] testStaticFunction
# 6| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 6| 0: [Parameter] a
# 6| 0: [TypeAccess] int
# 6| 1: [Parameter] c
# 6| 0: [TypeAccess] double
# 6| 2: [Parameter] e
# 6| 0: [TypeAccess] boolean
# 6| 5: [BlockStmt] { ... }
# 6| 0: [ReturnStmt] return ...
# 6| 0: [MethodAccess] testStaticFunction(...)
# 6| -1: [TypeAccess] Test
# 6| 0: [VarAccess] a
# 6| 1: [MethodAccess] getString(...)
# 6| -1: [TypeAccess] TestKt
# 6| 2: [VarAccess] c
# 6| 3: [FloatLiteral] 1.0
# 6| 4: [VarAccess] e
# 6| 3: [Method] testStaticFunction
# 6| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 6| 0: [Parameter] a
# 6| 0: [TypeAccess] int
# 6| 1: [Parameter] b
# 6| 0: [TypeAccess] String
# 6| 2: [Parameter] c
# 6| 0: [TypeAccess] double
# 6| 3: [Parameter] e
# 6| 0: [TypeAccess] boolean
# 6| 5: [BlockStmt] { ... }
# 6| 0: [ReturnStmt] return ...
# 6| 0: [MethodAccess] testStaticFunction(...)
# 6| -1: [TypeAccess] Test
# 6| 0: [VarAccess] a
# 6| 1: [VarAccess] b
# 6| 2: [VarAccess] c
# 6| 3: [FloatLiteral] 1.0
# 6| 4: [VarAccess] e
# 6| 4: [Method] testStaticFunction
# 6| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 6| 0: [Parameter] a
# 6| 0: [TypeAccess] int
# 6| 1: [Parameter] b
# 6| 0: [TypeAccess] String
# 6| 2: [Parameter] c
# 6| 0: [TypeAccess] double
# 6| 3: [Parameter] d
# 6| 0: [TypeAccess] float
# 6| 4: [Parameter] e
# 6| 0: [TypeAccess] boolean
# 6| 5: [BlockStmt] { ... }
# 6| 0: [ReturnStmt] return ...
# 6| 0: [VarAccess] a
# 9| 5: [Method] testMemberFunction
# 9| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 9| 0: [Parameter] a
# 9| 0: [TypeAccess] int
# 9| 1: [Parameter] c
# 9| 0: [TypeAccess] double
# 9| 2: [Parameter] e
# 9| 0: [TypeAccess] boolean
# 9| 5: [BlockStmt] { ... }
# 9| 0: [ReturnStmt] return ...
# 9| 0: [MethodAccess] testMemberFunction(...)
# 9| -1: [ThisAccess] this
# 9| 0: [VarAccess] a
# 9| 1: [MethodAccess] getString(...)
# 9| -1: [TypeAccess] TestKt
# 9| 2: [VarAccess] c
# 9| 3: [FloatLiteral] 1.0
# 9| 4: [VarAccess] e
# 9| 6: [Method] testMemberFunction
# 9| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 9| 0: [Parameter] a
# 9| 0: [TypeAccess] int
# 9| 1: [Parameter] b
# 9| 0: [TypeAccess] String
# 9| 2: [Parameter] c
# 9| 0: [TypeAccess] double
# 9| 3: [Parameter] e
# 9| 0: [TypeAccess] boolean
# 9| 5: [BlockStmt] { ... }
# 9| 0: [ReturnStmt] return ...
# 9| 0: [MethodAccess] testMemberFunction(...)
# 9| -1: [ThisAccess] this
# 9| 0: [VarAccess] a
# 9| 1: [VarAccess] b
# 9| 2: [VarAccess] c
# 9| 3: [FloatLiteral] 1.0
# 9| 4: [VarAccess] e
# 9| 7: [Method] testMemberFunction
# 9| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 9| 0: [Parameter] a
# 9| 0: [TypeAccess] int
# 9| 1: [Parameter] b
# 9| 0: [TypeAccess] String
# 9| 2: [Parameter] c
# 9| 0: [TypeAccess] double
# 9| 3: [Parameter] d
# 9| 0: [TypeAccess] float
# 9| 4: [Parameter] e
# 9| 0: [TypeAccess] boolean
# 9| 5: [BlockStmt] { ... }
# 9| 0: [ReturnStmt] return ...
# 9| 0: [VarAccess] a
# 12| 8: [ExtensionMethod] testMemberExtensionFunction
# 12| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 12| 0: [Parameter] <this>
# 12| 0: [TypeAccess] Test2
# 12| 1: [Parameter] a
# 12| 0: [TypeAccess] int
# 12| 2: [Parameter] c
# 12| 0: [TypeAccess] double
# 12| 3: [Parameter] e
# 12| 0: [TypeAccess] boolean
# 12| 5: [BlockStmt] { ... }
# 12| 0: [ReturnStmt] return ...
# 12| 0: [MethodAccess] testMemberExtensionFunction(...)
# 12| -1: [ThisAccess] this
# 12| 0: [ExtensionReceiverAccess] this
# 12| 1: [VarAccess] a
# 12| 2: [MethodAccess] getString(...)
# 12| -1: [TypeAccess] TestKt
# 12| 3: [VarAccess] c
# 12| 4: [FloatLiteral] 1.0
# 12| 5: [VarAccess] e
# 12| 9: [ExtensionMethod] testMemberExtensionFunction
# 12| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 12| 0: [Parameter] <this>
# 12| 0: [TypeAccess] Test2
# 12| 1: [Parameter] a
# 12| 0: [TypeAccess] int
# 12| 2: [Parameter] b
# 12| 0: [TypeAccess] String
# 12| 3: [Parameter] c
# 12| 0: [TypeAccess] double
# 12| 4: [Parameter] e
# 12| 0: [TypeAccess] boolean
# 12| 5: [BlockStmt] { ... }
# 12| 0: [ReturnStmt] return ...
# 12| 0: [MethodAccess] testMemberExtensionFunction(...)
# 12| -1: [ThisAccess] this
# 12| 0: [ExtensionReceiverAccess] this
# 12| 1: [VarAccess] a
# 12| 2: [VarAccess] b
# 12| 3: [VarAccess] c
# 12| 4: [FloatLiteral] 1.0
# 12| 5: [VarAccess] e
# 12| 10: [ExtensionMethod] testMemberExtensionFunction
# 12| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 12| 0: [Parameter] <this>
# 12| 0: [TypeAccess] Test2
# 12| 1: [Parameter] a
# 12| 0: [TypeAccess] int
# 12| 2: [Parameter] b
# 12| 0: [TypeAccess] String
# 12| 3: [Parameter] c
# 12| 0: [TypeAccess] double
# 12| 4: [Parameter] d
# 12| 0: [TypeAccess] float
# 12| 5: [Parameter] e
# 12| 0: [TypeAccess] boolean
# 12| 5: [BlockStmt] { ... }
# 12| 0: [ReturnStmt] return ...
# 12| 0: [VarAccess] a
# 16| 3: [Class] Test2
# 16| 1: [Constructor] Test2
#-----| 4: (Parameters)
# 16| 0: [Parameter] a
# 16| 0: [TypeAccess] int
# 16| 1: [Parameter] c
# 16| 0: [TypeAccess] double
# 16| 2: [Parameter] e
# 16| 0: [TypeAccess] boolean
# 16| 5: [BlockStmt] { ... }
# 16| 0: [ThisConstructorInvocationStmt] this(...)
# 16| 0: [VarAccess] a
# 16| 1: [MethodAccess] getString(...)
# 16| -1: [TypeAccess] TestKt
# 16| 2: [VarAccess] c
# 16| 3: [FloatLiteral] 1.0
# 16| 4: [VarAccess] e
# 16| 2: [Constructor] Test2
#-----| 4: (Parameters)
# 16| 0: [Parameter] a
# 16| 0: [TypeAccess] int
# 16| 1: [Parameter] b
# 16| 0: [TypeAccess] String
# 16| 2: [Parameter] c
# 16| 0: [TypeAccess] double
# 16| 3: [Parameter] e
# 16| 0: [TypeAccess] boolean
# 16| 5: [BlockStmt] { ... }
# 16| 0: [ThisConstructorInvocationStmt] this(...)
# 16| 0: [VarAccess] a
# 16| 1: [VarAccess] b
# 16| 2: [VarAccess] c
# 16| 3: [FloatLiteral] 1.0
# 16| 4: [VarAccess] e
# 16| 3: [Constructor] Test2
#-----| 4: (Parameters)
# 16| 0: [Parameter] a
# 16| 0: [TypeAccess] int
# 16| 1: [Parameter] b
# 16| 0: [TypeAccess] String
# 16| 2: [Parameter] c
# 16| 0: [TypeAccess] double
# 16| 3: [Parameter] d
# 16| 0: [TypeAccess] float
# 16| 4: [Parameter] e
# 16| 0: [TypeAccess] boolean
# 16| 5: [BlockStmt] { ... }
# 16| 0: [SuperConstructorInvocationStmt] super(...)
# 16| 1: [BlockStmt] { ... }
# 18| 4: [Class] Companion
# 18| 1: [Constructor] Companion
# 18| 5: [BlockStmt] { ... }
# 18| 0: [SuperConstructorInvocationStmt] super(...)
# 18| 1: [BlockStmt] { ... }
# 21| 2: [Method] testCompanionFunction
# 21| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 21| 0: [Parameter] a
# 21| 0: [TypeAccess] int
# 21| 1: [Parameter] c
# 21| 0: [TypeAccess] double
# 21| 2: [Parameter] e
# 21| 0: [TypeAccess] boolean
# 21| 5: [BlockStmt] { ... }
# 21| 0: [ReturnStmt] return ...
# 21| 0: [MethodAccess] testCompanionFunction(...)
# 21| -1: [ThisAccess] this
# 21| 0: [VarAccess] a
# 21| 1: [MethodAccess] getString(...)
# 21| -1: [TypeAccess] TestKt
# 21| 2: [VarAccess] c
# 21| 3: [FloatLiteral] 1.0
# 21| 4: [VarAccess] e
# 21| 3: [Method] testCompanionFunction
# 21| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 21| 0: [Parameter] a
# 21| 0: [TypeAccess] int
# 21| 1: [Parameter] b
# 21| 0: [TypeAccess] String
# 21| 2: [Parameter] c
# 21| 0: [TypeAccess] double
# 21| 3: [Parameter] e
# 21| 0: [TypeAccess] boolean
# 21| 5: [BlockStmt] { ... }
# 21| 0: [ReturnStmt] return ...
# 21| 0: [MethodAccess] testCompanionFunction(...)
# 21| -1: [ThisAccess] this
# 21| 0: [VarAccess] a
# 21| 1: [VarAccess] b
# 21| 2: [VarAccess] c
# 21| 3: [FloatLiteral] 1.0
# 21| 4: [VarAccess] e
# 21| 4: [Method] testCompanionFunction
# 21| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 21| 0: [Parameter] a
# 21| 0: [TypeAccess] int
# 21| 1: [Parameter] b
# 21| 0: [TypeAccess] String
# 21| 2: [Parameter] c
# 21| 0: [TypeAccess] double
# 21| 3: [Parameter] d
# 21| 0: [TypeAccess] float
# 21| 4: [Parameter] e
# 21| 0: [TypeAccess] boolean
# 21| 5: [BlockStmt] { ... }
# 21| 0: [ReturnStmt] return ...
# 21| 0: [VarAccess] a
# 24| 5: [Method] testStaticCompanionFunction
# 24| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 24| 0: [Parameter] a
# 24| 0: [TypeAccess] int
# 24| 1: [Parameter] c
# 24| 0: [TypeAccess] double
# 24| 2: [Parameter] e
# 24| 0: [TypeAccess] boolean
# 24| 5: [BlockStmt] { ... }
# 24| 0: [ReturnStmt] return ...
# 24| 0: [MethodAccess] testStaticCompanionFunction(...)
# 24| -1: [ThisAccess] this
# 24| 0: [VarAccess] a
# 24| 1: [MethodAccess] getString(...)
# 24| -1: [TypeAccess] TestKt
# 24| 2: [VarAccess] c
# 24| 3: [FloatLiteral] 1.0
# 24| 4: [VarAccess] e
# 24| 6: [Method] testStaticCompanionFunction
# 24| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 24| 0: [Parameter] a
# 24| 0: [TypeAccess] int
# 24| 1: [Parameter] b
# 24| 0: [TypeAccess] String
# 24| 2: [Parameter] c
# 24| 0: [TypeAccess] double
# 24| 3: [Parameter] e
# 24| 0: [TypeAccess] boolean
# 24| 5: [BlockStmt] { ... }
# 24| 0: [ReturnStmt] return ...
# 24| 0: [MethodAccess] testStaticCompanionFunction(...)
# 24| -1: [ThisAccess] this
# 24| 0: [VarAccess] a
# 24| 1: [VarAccess] b
# 24| 2: [VarAccess] c
# 24| 3: [FloatLiteral] 1.0
# 24| 4: [VarAccess] e
# 24| 7: [Method] testStaticCompanionFunction
# 24| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 24| 0: [Parameter] a
# 24| 0: [TypeAccess] int
# 24| 1: [Parameter] b
# 24| 0: [TypeAccess] String
# 24| 2: [Parameter] c
# 24| 0: [TypeAccess] double
# 24| 3: [Parameter] d
# 24| 0: [TypeAccess] float
# 24| 4: [Parameter] e
# 24| 0: [TypeAccess] boolean
# 24| 5: [BlockStmt] { ... }
# 24| 0: [ReturnStmt] return ...
# 24| 0: [VarAccess] a
# 24| 5: [Method] testStaticCompanionFunction
# 24| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 24| 0: [Parameter] a
# 24| 0: [TypeAccess] int
# 24| 1: [Parameter] c
# 24| 0: [TypeAccess] double
# 24| 2: [Parameter] e
# 24| 0: [TypeAccess] boolean
# 24| 5: [BlockStmt] { ... }
# 24| 0: [ReturnStmt] return ...
# 24| 0: [MethodAccess] testStaticCompanionFunction(...)
# 24| -1: [ThisAccess] this
# 24| 0: [VarAccess] a
# 24| 1: [MethodAccess] getString(...)
# 24| -1: [TypeAccess] TestKt
# 24| 2: [VarAccess] c
# 24| 3: [FloatLiteral] 1.0
# 24| 4: [VarAccess] e
# 24| 6: [Method] testStaticCompanionFunction
# 24| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 24| 0: [Parameter] a
# 24| 0: [TypeAccess] int
# 24| 1: [Parameter] b
# 24| 0: [TypeAccess] String
# 24| 2: [Parameter] c
# 24| 0: [TypeAccess] double
# 24| 3: [Parameter] e
# 24| 0: [TypeAccess] boolean
# 24| 5: [BlockStmt] { ... }
# 24| 0: [ReturnStmt] return ...
# 24| 0: [MethodAccess] testStaticCompanionFunction(...)
# 24| -1: [ThisAccess] this
# 24| 0: [VarAccess] a
# 24| 1: [VarAccess] b
# 24| 2: [VarAccess] c
# 24| 3: [FloatLiteral] 1.0
# 24| 4: [VarAccess] e
# 24| 7: [Method] testStaticCompanionFunction
# 24| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 24| 0: [Parameter] a
# 24| 0: [TypeAccess] int
# 24| 1: [Parameter] b
# 24| 0: [TypeAccess] String
# 24| 2: [Parameter] c
# 24| 0: [TypeAccess] double
# 24| 3: [Parameter] d
# 24| 0: [TypeAccess] float
# 24| 4: [Parameter] e
# 24| 0: [TypeAccess] boolean
# 24| 5: [BlockStmt] { ... }
# 24| 0: [ReturnStmt] return ...
# 24| 0: [MethodAccess] testStaticCompanionFunction(...)
# 24| -1: [VarAccess] Test2.Companion
# 24| -1: [TypeAccess] Test2
# 24| 0: [VarAccess] a
# 24| 1: [VarAccess] b
# 24| 2: [VarAccess] c
# 24| 3: [VarAccess] d
# 24| 4: [VarAccess] e
# 30| 4: [Class,GenericType,ParameterizedType] GenericTest
#-----| -2: (Generic Parameters)
# 30| 0: [TypeVariable] T
# 30| 1: [Constructor] GenericTest
#-----| 4: (Parameters)
# 30| 0: [Parameter] b
# 30| 0: [TypeAccess] T
# 30| 1: [Parameter] d
# 30| 0: [TypeAccess] T
# 30| 5: [BlockStmt] { ... }
# 30| 0: [ThisConstructorInvocationStmt] this(...)
# 30| 0: [IntegerLiteral] 1
# 30| 1: [VarAccess] b
# 30| 2: [StringLiteral] Hello world
# 30| 3: [VarAccess] d
# 30| 2: [Constructor] GenericTest
#-----| 4: (Parameters)
# 30| 0: [Parameter] a
# 30| 0: [TypeAccess] int
# 30| 1: [Parameter] b
# 30| 0: [TypeAccess] T
# 30| 2: [Parameter] d
# 30| 0: [TypeAccess] T
# 30| 5: [BlockStmt] { ... }
# 30| 0: [ThisConstructorInvocationStmt] this(...)
# 30| 0: [VarAccess] a
# 30| 1: [VarAccess] b
# 30| 2: [StringLiteral] Hello world
# 30| 3: [VarAccess] d
# 30| 3: [Constructor] GenericTest
#-----| 4: (Parameters)
# 30| 0: [Parameter] a
# 30| 0: [TypeAccess] int
# 30| 1: [Parameter] b
# 30| 0: [TypeAccess] T
# 30| 2: [Parameter] c
# 30| 0: [TypeAccess] String
# 30| 3: [Parameter] d
# 30| 0: [TypeAccess] T
# 30| 5: [BlockStmt] { ... }
# 30| 0: [SuperConstructorInvocationStmt] super(...)
# 30| 1: [BlockStmt] { ... }
# 33| 4: [Method] testMemberFunction
# 33| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 33| 0: [Parameter] b
# 33| 0: [TypeAccess] T
# 33| 1: [Parameter] d
# 33| 0: [TypeAccess] T
# 33| 5: [BlockStmt] { ... }
# 33| 0: [ReturnStmt] return ...
# 33| 0: [MethodAccess] testMemberFunction(...)
# 33| -1: [ThisAccess] this
# 33| 0: [IntegerLiteral] 1
# 33| 1: [VarAccess] b
# 33| 2: [StringLiteral] Hello world
# 33| 3: [VarAccess] d
# 33| 5: [Method] testMemberFunction
# 33| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 33| 0: [Parameter] a
# 33| 0: [TypeAccess] int
# 33| 1: [Parameter] b
# 33| 0: [TypeAccess] T
# 33| 2: [Parameter] d
# 33| 0: [TypeAccess] T
# 33| 5: [BlockStmt] { ... }
# 33| 0: [ReturnStmt] return ...
# 33| 0: [MethodAccess] testMemberFunction(...)
# 33| -1: [ThisAccess] this
# 33| 0: [VarAccess] a
# 33| 1: [VarAccess] b
# 33| 2: [StringLiteral] Hello world
# 33| 3: [VarAccess] d
# 33| 6: [Method] testMemberFunction
# 33| 3: [TypeAccess] int
#-----| 4: (Parameters)
# 33| 0: [Parameter] a
# 33| 0: [TypeAccess] int
# 33| 1: [Parameter] b
# 33| 0: [TypeAccess] T
# 33| 2: [Parameter] c
# 33| 0: [TypeAccess] String
# 33| 3: [Parameter] d
# 33| 0: [TypeAccess] T
# 33| 5: [BlockStmt] { ... }
# 33| 0: [ReturnStmt] return ...
# 33| 0: [VarAccess] a
# 35| 7: [Method] useSpecialised
# 35| 3: [TypeAccess] Unit
#-----| 4: (Parameters)
# 35| 0: [Parameter] spec1
# 35| 0: [TypeAccess] GenericTest<Float>
# 35| 0: [TypeAccess] Float
# 35| 1: [Parameter] spec2
# 35| 0: [TypeAccess] GenericTest<Double>
# 35| 0: [TypeAccess] Double
# 35| 5: [BlockStmt] { ... }
# 37| 0: [ExprStmt] <Expr>;
# 37| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
# 37| 0: [TypeAccess] Unit
# 37| 1: [MethodAccess] testMemberFunction(...)
# 37| -1: [VarAccess] spec1
# 37| 0: [IntegerLiteral] 1
# 37| 1: [FloatLiteral] 1.0
# 37| 2: [StringLiteral] Hello world
# 37| 3: [FloatLiteral] 2.0
# 38| 1: [ExprStmt] <Expr>;
# 38| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
# 38| 0: [TypeAccess] Unit
# 38| 1: [MethodAccess] testMemberFunction(...)
# 38| -1: [VarAccess] spec2
# 38| 0: [IntegerLiteral] 1
# 38| 1: [DoubleLiteral] 1.0
# 38| 2: [StringLiteral] Hello world
# 38| 3: [DoubleLiteral] 2.0

View File

@@ -0,0 +1 @@
semmle/code/java/PrintAst.ql

View File

@@ -0,0 +1,48 @@
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | GenericTest<Double>(int,java.lang.Double,java.lang.Double) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | GenericTest<Double>(int,java.lang.Double,java.lang.String,java.lang.Double) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | GenericTest<Double>(java.lang.Double,java.lang.Double) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | testMemberFunction | testMemberFunction(int,java.lang.Double,java.lang.Double) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | testMemberFunction | testMemberFunction(int,java.lang.Double,java.lang.String,java.lang.Double) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | testMemberFunction | testMemberFunction(java.lang.Double,java.lang.Double) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Double> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | useSpecialised | useSpecialised(GenericTest,GenericTest) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | GenericTest<Float>(int,java.lang.Float,java.lang.Float) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | GenericTest<Float>(int,java.lang.Float,java.lang.String,java.lang.Float) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | GenericTest<Float>(java.lang.Float,java.lang.Float) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | testMemberFunction | testMemberFunction(int,java.lang.Float,java.lang.Float) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | testMemberFunction | testMemberFunction(int,java.lang.Float,java.lang.String,java.lang.Float) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | testMemberFunction | testMemberFunction(java.lang.Float,java.lang.Float) |
| file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | GenericTest<Float> | file:///!unknown-binary-location/GenericTest.class:0:0:0:0 | useSpecialised | useSpecialised(GenericTest,GenericTest) |
| test.kt:0:0:0:0 | TestKt | test.kt:1:1:1:31 | getString | getString() |
| test.kt:0:0:0:0 | TestKt | test.kt:45:1:45:112 | testExtensionFunction | testExtensionFunction(Test,int,double,boolean) |
| test.kt:0:0:0:0 | TestKt | test.kt:45:1:45:112 | testExtensionFunction | testExtensionFunction(Test,int,java.lang.String,double,boolean) |
| test.kt:0:0:0:0 | TestKt | test.kt:45:1:45:112 | testExtensionFunction | testExtensionFunction(Test,int,java.lang.String,double,float,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:3:1:14:1 | Test | Test() |
| test.kt:3:1:14:1 | Test | test.kt:6:3:6:106 | testStaticFunction | testStaticFunction(int,double,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:6:3:6:106 | testStaticFunction | testStaticFunction(int,java.lang.String,double,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:6:3:6:106 | testStaticFunction | testStaticFunction(int,java.lang.String,double,float,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:9:3:9:106 | testMemberFunction | testMemberFunction(int,double,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:9:3:9:106 | testMemberFunction | testMemberFunction(int,java.lang.String,double,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:9:3:9:106 | testMemberFunction | testMemberFunction(int,java.lang.String,double,float,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:12:3:12:121 | testMemberExtensionFunction | testMemberExtensionFunction(Test2,int,double,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:12:3:12:121 | testMemberExtensionFunction | testMemberExtensionFunction(Test2,int,java.lang.String,double,boolean) |
| test.kt:3:1:14:1 | Test | test.kt:12:3:12:121 | testMemberExtensionFunction | testMemberExtensionFunction(Test2,int,java.lang.String,double,float,boolean) |
| test.kt:16:1:28:1 | Test2 | test.kt:16:34:28:1 | Test2 | Test2(int,double,boolean) |
| test.kt:16:1:28:1 | Test2 | test.kt:16:34:28:1 | Test2 | Test2(int,java.lang.String,double,boolean) |
| test.kt:16:1:28:1 | Test2 | test.kt:16:34:28:1 | Test2 | Test2(int,java.lang.String,double,float,boolean) |
| test.kt:16:1:28:1 | Test2 | test.kt:24:5:24:117 | testStaticCompanionFunction | testStaticCompanionFunction(int,double,boolean) |
| test.kt:16:1:28:1 | Test2 | test.kt:24:5:24:117 | testStaticCompanionFunction | testStaticCompanionFunction(int,java.lang.String,double,boolean) |
| test.kt:16:1:28:1 | Test2 | test.kt:24:5:24:117 | testStaticCompanionFunction | testStaticCompanionFunction(int,java.lang.String,double,float,boolean) |
| test.kt:18:3:26:3 | Companion | test.kt:18:3:26:3 | Companion | Companion() |
| test.kt:18:3:26:3 | Companion | test.kt:21:5:21:111 | testCompanionFunction | testCompanionFunction(int,double,boolean) |
| test.kt:18:3:26:3 | Companion | test.kt:21:5:21:111 | testCompanionFunction | testCompanionFunction(int,java.lang.String,double,boolean) |
| test.kt:18:3:26:3 | Companion | test.kt:21:5:21:111 | testCompanionFunction | testCompanionFunction(int,java.lang.String,double,float,boolean) |
| test.kt:18:3:26:3 | Companion | test.kt:24:5:24:117 | testStaticCompanionFunction | testStaticCompanionFunction(int,double,boolean) |
| test.kt:18:3:26:3 | Companion | test.kt:24:5:24:117 | testStaticCompanionFunction | testStaticCompanionFunction(int,java.lang.String,double,boolean) |
| test.kt:18:3:26:3 | Companion | test.kt:24:5:24:117 | testStaticCompanionFunction | testStaticCompanionFunction(int,java.lang.String,double,float,boolean) |
| test.kt:30:1:42:1 | GenericTest | test.kt:30:43:42:1 | GenericTest | GenericTest(int,java.lang.Object,java.lang.Object) |
| test.kt:30:1:42:1 | GenericTest | test.kt:30:43:42:1 | GenericTest | GenericTest(int,java.lang.Object,java.lang.String,java.lang.Object) |
| test.kt:30:1:42:1 | GenericTest | test.kt:30:43:42:1 | GenericTest | GenericTest(java.lang.Object,java.lang.Object) |
| test.kt:30:1:42:1 | GenericTest | test.kt:33:3:33:84 | testMemberFunction | testMemberFunction(int,java.lang.Object,java.lang.Object) |
| test.kt:30:1:42:1 | GenericTest | test.kt:33:3:33:84 | testMemberFunction | testMemberFunction(int,java.lang.Object,java.lang.String,java.lang.Object) |
| test.kt:30:1:42:1 | GenericTest | test.kt:33:3:33:84 | testMemberFunction | testMemberFunction(java.lang.Object,java.lang.Object) |
| test.kt:30:1:42:1 | GenericTest | test.kt:35:3:40:3 | useSpecialised | useSpecialised(GenericTest,GenericTest) |

View File

@@ -0,0 +1,45 @@
fun getString() = "Hello world"
object Test {
@JvmOverloads @JvmStatic
fun testStaticFunction(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean): Int = a
@JvmOverloads
fun testMemberFunction(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean): Int = a
@JvmOverloads
fun Test2.testMemberExtensionFunction(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean): Int = a
}
public class Test2 @JvmOverloads constructor(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean) {
companion object {
@JvmOverloads
fun testCompanionFunction(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean): Int = a
@JvmOverloads @JvmStatic
fun testStaticCompanionFunction(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean): Int = a
}
}
public class GenericTest<T> @JvmOverloads constructor(a: Int = 1, b: T, c: String = "Hello world", d: T) {
@JvmOverloads
fun testMemberFunction(a: Int = 1, b: T, c: String = "Hello world", d: T): Int = a
fun useSpecialised(spec1: GenericTest<Float>, spec2: GenericTest<Double>) {
spec1.testMemberFunction(1, 1.0f, "Hello world", 2.0f)
spec2.testMemberFunction(1, 1.0, "Hello world", 2.0)
}
}
@JvmOverloads
fun Test.testExtensionFunction(a: Int, b: String = getString(), c: Double, d: Float = 1.0f, e: Boolean): Int = a

View File

@@ -0,0 +1,5 @@
import java
from Callable c
where c.getSourceDeclaration().fromSource()
select c.getDeclaringType(), c, c.getSignature()

View File

@@ -0,0 +1,43 @@
public class User {
public static String source() { return "taint"; }
public static void test(Test2 t2, GenericTest<Integer> gt, TestDefaultParameterReference paramRefTest) {
Test.taintSuppliedAsDefault(1, "no taint", 2);
Test.taintSuppliedAsDefault(1, 2);
Test.noTaintByDefault(1, source(), 2, 3);
Test.noTaintByDefault(1, source(), 2);
Test2.taintSuppliedAsDefaultStatic(1, "no taint", 2);
Test2.taintSuppliedAsDefaultStatic(1, 2);
Test2.noTaintByDefaultStatic(1, source(), 2, 3);
Test2.noTaintByDefaultStatic(1, source(), 2);
t2.taintSuppliedAsDefault(1, "no taint", 2);
t2.taintSuppliedAsDefault(1, 2);
t2.noTaintByDefault(1, source(), 2, 3);
t2.noTaintByDefault(1, source(), 2);
gt.taintSuppliedAsDefault(1, "no taint", 2);
gt.taintSuppliedAsDefault(1, 2);
gt.noTaintByDefault(1, source(), 2, 3);
gt.noTaintByDefault(1, source(), 2);
new ConstructorTaintsByDefault(1, "no taint", 2);
new ConstructorTaintsByDefault(1, 2);
new ConstructorDoesNotTaintByDefault(1, source(), 2, 3);
new ConstructorDoesNotTaintByDefault(1, source(), 2);
new GenericConstructorTaintsByDefault<Integer>(1, "no taint", 2);
new GenericConstructorTaintsByDefault<Integer>(1, 2);
new GenericConstructorDoesNotTaintByDefault<Integer>(1, source(), 2, 3);
new GenericConstructorDoesNotTaintByDefault<Integer>(1, source(), 2);
paramRefTest.f(source(), "no flow");
paramRefTest.f("flow", source());
paramRefTest.f(source()); // Should also have flow due to the default for the second parameter being the value of the first
}
}

View File

@@ -0,0 +1,21 @@
| User.java:9:30:9:37 | source(...) | test.kt:13:97:13:97 | s |
| User.java:10:30:10:37 | source(...) | test.kt:13:97:13:97 | s |
| User.java:14:37:14:44 | source(...) | test.kt:25:105:25:105 | s |
| User.java:15:37:15:44 | source(...) | test.kt:25:105:25:105 | s |
| User.java:19:28:19:35 | source(...) | test.kt:33:97:33:97 | s |
| User.java:20:28:20:35 | source(...) | test.kt:33:97:33:97 | s |
| User.java:24:28:24:35 | source(...) | test.kt:43:93:43:93 | s |
| User.java:25:28:25:35 | source(...) | test.kt:43:93:43:93 | s |
| User.java:29:45:29:52 | source(...) | test.kt:58:10:58:10 | s |
| User.java:30:45:30:52 | source(...) | test.kt:58:10:58:10 | s |
| User.java:34:61:34:68 | source(...) | test.kt:74:10:74:10 | s |
| User.java:35:61:35:68 | source(...) | test.kt:74:10:74:10 | s |
| User.java:38:28:38:35 | source(...) | test.kt:84:10:84:10 | y |
| User.java:39:20:39:27 | source(...) | test.kt:84:10:84:10 | y |
| test.kt:10:55:10:62 | source(...) | test.kt:10:84:10:84 | s |
| test.kt:22:63:22:70 | source(...) | test.kt:22:92:22:92 | s |
| test.kt:22:63:22:70 | source(...) | test.kt:22:92:22:92 | s |
| test.kt:30:55:30:62 | source(...) | test.kt:30:84:30:84 | s |
| test.kt:40:53:40:60 | source(...) | test.kt:40:80:40:80 | s |
| test.kt:47:92:47:99 | source(...) | test.kt:50:10:50:10 | s |
| test.kt:63:100:63:107 | source(...) | test.kt:66:10:66:10 | s |

View File

@@ -0,0 +1,87 @@
fun getString() = "Hello world"
fun source() = "tainted"
fun sink(s: String) { }
object Test {
@JvmOverloads @JvmStatic
fun taintSuppliedAsDefault(before: Int, s: String = source(), after: Int) { sink(s) }
@JvmOverloads @JvmStatic
fun noTaintByDefault(before: Int, s: String = "no taint", after: Int, after2: Int = 1) { sink(s) }
}
public class Test2 {
companion object {
@JvmOverloads @JvmStatic
fun taintSuppliedAsDefaultStatic(before: Int, s: String = source(), after: Int) { sink(s) }
@JvmOverloads @JvmStatic
fun noTaintByDefaultStatic(before: Int, s: String = "no taint", after: Int, after2: Int = 1) { sink(s) }
}
@JvmOverloads
fun taintSuppliedAsDefault(before: Int, s: String = source(), after: Int) { sink(s) }
@JvmOverloads
fun noTaintByDefault(before: Int, s: String = "no taint", after: Int, after2: Int = 1) { sink(s) }
}
public class GenericTest<T> {
@JvmOverloads
fun taintSuppliedAsDefault(before: T, s: String = source(), after: T) { sink(s) }
@JvmOverloads
fun noTaintByDefault(before: T, s: String = "no taint", after: T, after2: Int = 1) { sink(s) }
}
public class ConstructorTaintsByDefault @JvmOverloads constructor(before: Int, s: String = source(), after: Int) {
init {
sink(s)
}
}
public class ConstructorDoesNotTaintByDefault @JvmOverloads constructor(before: Int, s: String = "no taint", after: Int, after2: Int = 1) {
init {
sink(s)
}
}
public class GenericConstructorTaintsByDefault<T> @JvmOverloads constructor(before: T, s: String = source(), after: T) {
init {
sink(s)
}
}
public class GenericConstructorDoesNotTaintByDefault<T> @JvmOverloads constructor(before: T, s: String = "no taint", after: T, after2: T? = null) {
init {
sink(s)
}
}
fun ident(s: String) = s
public class TestDefaultParameterReference {
@JvmOverloads fun f(x: String, y: String = ident(x)) {
sink(y)
}
}

View File

@@ -0,0 +1,18 @@
import java
import semmle.code.java.dataflow.DataFlow
class Config extends DataFlow::Configuration {
Config() { this = "config" }
override predicate isSource(DataFlow::Node n) {
n.asExpr().(MethodAccess).getCallee().getName() = "source"
}
override predicate isSink(DataFlow::Node n) {
n.asExpr().(Argument).getCall().getCallee().getName() = "sink"
}
}
from Config c, DataFlow::Node source, DataFlow::Node sink
where c.hasFlow(source, sink)
select source, sink

View File

@@ -0,0 +1,9 @@
public class User {
public static void test(A a) {
a.genericFunctionWithOverloads(null, null);
}
}

View File

@@ -0,0 +1,9 @@
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads() | return | T | genericFunctionWithOverloads() |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object) | param | T | genericFunctionWithOverloads(java.lang.Object) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object) | return | T | genericFunctionWithOverloads(java.lang.Object) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object,java.util.List) | param | List<? extends T> | genericFunctionWithOverloads(java.lang.Object,java.util.List) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object,java.util.List) | param | T | genericFunctionWithOverloads(java.lang.Object,java.util.List) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object,java.util.List) | return | T | genericFunctionWithOverloads(java.lang.Object,java.util.List) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object,java.util.List,java.lang.Object) | param | List<? extends T> | genericFunctionWithOverloads(java.lang.Object,java.util.List,java.lang.Object) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object,java.util.List,java.lang.Object) | param | T | genericFunctionWithOverloads(java.lang.Object,java.util.List,java.lang.Object) |
| test.kt:4:3:4:94 | genericFunctionWithOverloads | genericFunctionWithOverloads(java.lang.Object,java.util.List,java.lang.Object) | return | T | genericFunctionWithOverloads(java.lang.Object,java.util.List,java.lang.Object) |

View File

@@ -0,0 +1,6 @@
public class A {
@JvmOverloads
fun <T> genericFunctionWithOverloads(x: T? = null, y: List<T>? = null, z: T? = null): T? = z
}

View File

@@ -0,0 +1,16 @@
import java
from Method m, string kind, Type t
where
m.fromSource() and
(
kind = "param" and t = m.getAParamType()
or
kind = "return" and t = m.getReturnType()
)
// 't.(ParameterizedType).getATypeArgument().(Wildcard).getUpperBound().getType()' is pulling the 'T' out of 'List<? extends T>'
select m, m.getSignature(), kind, t.toString(),
[t, t.(ParameterizedType).getATypeArgument().(Wildcard).getUpperBound().getType()]
.(TypeVariable)
.getGenericCallable()
.getSignature()