WIP: Extract annotation values (tests are missing)

This commit is contained in:
Tamas Vajk
2022-04-12 16:03:21 +02:00
committed by Chris Smowton
parent 7b075c2190
commit 3b09cb2224
6 changed files with 191 additions and 93 deletions

View File

@@ -457,53 +457,93 @@ open class KotlinFileExtractor(
extractDeclInitializers(c.declarations, false) { Pair(blockId, obinitId) }
}
fun extractAnnotations(c: IrClass): Int {
var count = 0
for (constructorCall: IrConstructorCall in c.annotations) {
private fun extractAnnotations(c: IrClass) {
for ((idx, constructorCall: IrConstructorCall) in c.annotations.withIndex()) {
extractAnnotation(constructorCall, useClassSource(c), idx)
}
}
// todo: do not extract JvmName, what else?
private fun extractAnnotation(
constructorCall: IrConstructorCall,
parent: Label<out DbExprparent>,
idx: Int
): Label<out DbExpr> {
val t = useType(constructorCall.type)
val t = useType(constructorCall.type)
val annotated = useClassSource(c)
val id = tw.getLabelFor<DbDeclannotation>("@\"annotation;{$parent};{${t.javaResult.id}}\"")
tw.writeExprs_declannotation(id, t.javaResult.id, parent, idx)
tw.writeExprsKotlinType(id, t.kotlinResult.id)
val id = tw.getLabelFor<DbDeclannotation>("@\"annotation;{$annotated};{${t.javaResult.id}}\"")
tw.writeExprs_declannotation(id, t.javaResult.id, annotated, count++)
tw.writeExprsKotlinType(id, t.kotlinResult.id)
val locId = tw.getLocation(constructorCall)
tw.writeHasLocation(id, locId)
val locId = tw.getLocation(constructorCall)
tw.writeHasLocation(id, locId)
for (i in 0 until constructorCall.valueArgumentsCount) {
val param = constructorCall.symbol.owner.valueParameters[i]
val prop = constructorCall.symbol.owner.parentAsClass.declarations.filterIsInstance<IrProperty>().firstOrNull { it.name == param.name }
if (prop == null) {
continue
}
val v = constructorCall.getValueArgument(i) ?: param.defaultValue?.expression
when (v) {
is IrConst<*> -> {
val exprId = extractConstant(v, id, i)
if (exprId != null) {
tw.writeAnnotValue(id, useFunction<DbMethod>(prop.getter!!), exprId)
}
}
/*
Integer types;
Enum types;
String type;
Classes;
Other annotation types;
Arrays of any type listed above.
*/
}
for (i in 0 until constructorCall.valueArgumentsCount) {
val param = constructorCall.symbol.owner.valueParameters[i]
val prop = constructorCall.symbol.owner.parentAsClass.declarations
.filterIsInstance<IrProperty>()
.first { it.name == param.name }
val v = constructorCall.getValueArgument(i) ?: param.defaultValue?.expression
val exprId = extractAnnotationValueExpression(v, id, i)
if (exprId != null) {
tw.writeAnnotValue(id, useFunction(prop.getter!!), exprId)
}
}
return id
}
return count
private fun extractAnnotationValueExpression(
v: IrExpression?,
parent: Label<out DbExprparent>,
idx: Int): Label<out DbExpr>? {
return when (v) {
is IrConst<*> -> {
extractConstant(v, parent, idx)
}
is IrGetEnumValue -> {
extractEnumValue(v, parent, idx, null, null)
}
is IrClassReference -> {
extractClassReference(v, parent, idx, null, null)
}
is IrConstructorCall -> {
extractAnnotation(v, parent, idx)
}
is IrVararg -> {
val eId = tw.getFreshIdLabel<DbArrayinit>()
val type = useType(v.type)
tw.writeExprs_arrayinit(eId, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(eId, type.kotlinResult.id)
tw.writeHasLocation(eId, tw.getLocation(v))
v.elements.forEachIndexed { index, irVarargElement -> run {
val argExpr = when(irVarargElement) {
is IrExpression -> irVarargElement
is IrSpreadElement -> irVarargElement.expression
else -> {
logger.errorElement("Unrecognised IrVarargElement: " + irVarargElement.javaClass, irVarargElement)
null
}
}
extractAnnotationValueExpression(argExpr, eId, index)
} }
eId
}
// is IrErrorExpression
// null
else -> {
val eId = tw.getFreshIdLabel<DbErrorexpr>()
val type = useType(v?.type ?: pluginContext.irBuiltIns.unitType)
tw.writeExprs_errorexpr(eId, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(eId, type.kotlinResult.id)
if (v != null) {
tw.writeHasLocation(eId, tw.getLocation(v))
}
eId
}
}
}
fun extractClassSource(c: IrClass, extractDeclarations: Boolean, extractStaticInitializer: Boolean, extractPrivateMembers: Boolean, extractFunctionBodies: Boolean): Label<out DbClassorinterface> {
@@ -2160,7 +2200,7 @@ open class KotlinFileExtractor(
extractValueArguments(argParent, idxOffset)
}
private fun extractStaticTypeAccessQualifierUnchecked(parent: IrDeclarationParent, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) {
private fun extractStaticTypeAccessQualifierUnchecked(parent: IrDeclarationParent, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?) {
if (parent is IrClass) {
extractTypeAccessRecursive(parent.toRawType(), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
} else if (parent is IrFile) {
@@ -2170,7 +2210,7 @@ open class KotlinFileExtractor(
}
}
private fun extractStaticTypeAccessQualifier(target: IrDeclaration, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) {
private fun extractStaticTypeAccessQualifier(target: IrDeclaration, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?) {
if (target.shouldExtractAsStatic) {
extractStaticTypeAccessQualifierUnchecked(target.parent, parentExpr, locId, enclosingCallable, enclosingStmt)
}
@@ -3691,19 +3731,7 @@ open class KotlinFileExtractor(
}
is IrGetEnumValue -> {
val exprParent = parent.expr(e, callable)
val id = tw.getFreshIdLabel<DbVaraccess>()
val type = useType(e.type)
val locId = tw.getLocation(e)
tw.writeExprs_varaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
extractExprContext(id, locId, callable, exprParent.enclosingStmt)
val owner = getBoundSymbolOwner(e.symbol, e) ?: return
val vId = useEnumEntry(owner)
tw.writeVariableBinding(id, vId)
extractStaticTypeAccessQualifier(owner, id, locId, callable, exprParent.enclosingStmt)
val id = extractEnumValue(e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
}
is IrSetValue,
is IrSetField -> {
@@ -3947,14 +3975,7 @@ open class KotlinFileExtractor(
}
is IrClassReference -> {
val exprParent = parent.expr(e, callable)
val id = tw.getFreshIdLabel<DbTypeliteral>()
val locId = tw.getLocation(e)
val type = useType(e.type)
tw.writeExprs_typeliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
extractExprContext(id, locId, callable, exprParent.enclosingStmt)
extractTypeAccessRecursive(e.classType, locId, id, 0, callable, exprParent.enclosingStmt)
extractClassReference(e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
}
is IrPropertyReference -> {
extractPropertyReference("property reference", e, e.getter, e.setter, e.field, parent, callable)
@@ -4153,6 +4174,53 @@ open class KotlinFileExtractor(
extractExpressionExpr(loop.condition, callable, id, 0, id)
}
private fun extractClassReference(
e: IrClassReference,
parent: Label<out DbExprparent>,
idx: Int,
enclosingCallable: Label<out DbCallable>?,
enclosingStmt: Label<out DbStmt>?
): Label<out DbExpr> {
val id = tw.getFreshIdLabel<DbTypeliteral>()
val locId = tw.getLocation(e)
val type = useType(e.type)
tw.writeExprs_typeliteral(id, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
tw.writeHasLocation(id, locId)
enclosingCallable?.let { tw.writeCallableEnclosingExpr(id, it) }
enclosingStmt?.let { tw.writeStatementEnclosingExpr(id, it) }
extractTypeAccessRecursive(e.classType, locId, id, 0, enclosingCallable, enclosingStmt)
return id
}
private fun extractEnumValue(
e: IrGetEnumValue,
parent: Label<out DbExprparent>,
idx: Int,
enclosingCallable: Label<out DbCallable>?,
enclosingStmt: Label<out DbStmt>?
): Label<out DbExpr> {
val id = tw.getFreshIdLabel<DbVaraccess>()
val type = useType(e.type)
val locId = tw.getLocation(e)
tw.writeExprs_varaccess(id, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
tw.writeHasLocation(id, locId)
enclosingCallable?.let { tw.writeCallableEnclosingExpr(id, it) }
enclosingStmt?.let { tw.writeStatementEnclosingExpr(id, it) }
val owner = getBoundSymbolOwner(e.symbol, e) ?: return id
val vId = useEnumEntry(owner)
tw.writeVariableBinding(id, vId)
extractStaticTypeAccessQualifier(owner, id, locId, enclosingCallable, enclosingStmt)
return id
}
// Render a string literal as it might occur in Kotlin source. Note this is a reasonable guess; the real source
// could use other escape sequences to describe the same String. Importantly, this is the same guess the Java
// extractor makes regarding string literals occurring within annotations, which we need to coincide with to ensure
@@ -5091,10 +5159,14 @@ open class KotlinFileExtractor(
/**
* Extracts a single type access expression with enclosing callable and statement.
*/
private fun extractTypeAccess(type: TypeResults, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>): Label<out DbExpr> {
private fun extractTypeAccess(type: TypeResults, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?): Label<out DbExpr> {
val id = extractTypeAccess(type, location, parent, idx)
tw.writeCallableEnclosingExpr(id, enclosingCallable)
tw.writeStatementEnclosingExpr(id, enclosingStmt)
if (enclosingCallable != null) {
tw.writeCallableEnclosingExpr(id, enclosingCallable)
}
if (enclosingStmt != null) {
tw.writeStatementEnclosingExpr(id, enclosingStmt)
}
return id
}
@@ -5136,7 +5208,7 @@ open class KotlinFileExtractor(
/**
* Extracts a type access expression and its child type access expressions in case of a generic type. Nested generics are also handled.
*/
private fun extractTypeAccessRecursive(t: IrType, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>, typeContext: TypeContext = TypeContext.OTHER): Label<out DbExpr> {
private fun extractTypeAccessRecursive(t: IrType, location: Label<DbLocation>, parent: Label<out DbExprparent>, idx: Int, enclosingCallable: Label<out DbCallable>?, enclosingStmt: Label<out DbStmt>?, typeContext: TypeContext = TypeContext.OTHER): Label<out DbExpr> {
// TODO: `useType` substitutes types to their java equivalent, and sometimes that also means changing the number of type arguments. The below logic doesn't take this into account.
// For example `KFunction2<Int,Double,String>` becomes `KFunction<String>` with three child type access expressions: `Int`, `Double`, `String`.
val typeAccessId = extractTypeAccess(useType(t, typeContext), location, parent, idx, enclosingCallable, enclosingStmt)
@@ -5154,8 +5226,8 @@ open class KotlinFileExtractor(
typeArgs: List<IrType>,
location: Label<DbLocation>,
parentExpr: Label<out DbExprparent>,
enclosingCallable: Label<out DbCallable>,
enclosingStmt: Label<out DbStmt>,
enclosingCallable: Label<out DbCallable>?,
enclosingStmt: Label<out DbStmt>?,
startIndex: Int = 0,
reverse: Boolean = false
) {

View File

@@ -53,6 +53,8 @@ predicate shouldBeDeadEnd(ControlFlowNode n) {
not exists(n.getFile().getRelativePath()) // TODO
or
n = any(ConstCase c).getValue(_) // TODO
or
n instanceof ErrorExpr // TODO
}
from ControlFlowNode n, string s

View File

@@ -1 +0,0 @@
| file:///modules/java.base/java/util/Random.class:0:0:0:0 | RandomGeneratorProperties | Annotation | 1 | gap | 0, 2, 3, 4, 5 |

View File

@@ -16,14 +16,18 @@ def.kt:
#-----| 1: (Annotations)
# 1| 2: [Method] y
# 3| 3: [Interface] ReplaceWith
#-----| -3: (Annotations)
# 3| 1: [Method] expression
# 5| 4: [Interface] Deprecated
#-----| -3: (Annotations)
# 6| 1: [Method] message
# 7| 2: [Method] replaceWith
# 9| 5: [Class] X
#-----| -3: (Annotations)
# 9| 1: [Annotation] Deprecated
# 0| 1: [StringLiteral] This class is deprecated
# 0| 1: [Annotation] ReplaceWith
# 0| 1: [StringLiteral] Y
# 10| 2: [Annotation] SomeAnnotation
# 0| 1: [IntegerLiteral] 5
# 0| 1: [StringLiteral] a
@@ -33,29 +37,36 @@ def.kt:
# 11| 1: [BlockStmt] { ... }
use.java:
# 0| [CompilationUnit] use
# 2| 1: [Class] use
# 6| 1: [Class] use
#-----| -3: (Annotations)
# 2| 1: [Annotation] Deprecated
# 3| 1: [StringLiteral] "This class is deprecated"
# 4| 2: [Annotation] ReplaceWith
# 4| 1: [StringLiteral] "Y"
# 5| 2: [Annotation] SomeAnnotation
# 5| 2: [StringLiteral] "b"
#-----| -1: (Base Types)
# 2| 0: [TypeAccess] SomeAnnotation
# 4| 2: [Method] abc
# 6| 0: [TypeAccess] SomeAnnotation
# 8| 2: [Method] abc
#-----| 1: (Annotations)
# 3| 1: [Annotation] Override
# 4| 3: [TypeAccess] int
# 4| 5: [BlockStmt] { ... }
# 4| 0: [ReturnStmt] return ...
# 4| 0: [IntegerLiteral] 1
# 6| 3: [Method] y
# 7| 1: [Annotation] Override
# 8| 3: [TypeAccess] int
# 8| 5: [BlockStmt] { ... }
# 8| 0: [ReturnStmt] return ...
# 8| 0: [IntegerLiteral] 1
# 10| 3: [Method] y
#-----| 1: (Annotations)
# 5| 1: [Annotation] Override
# 6| 3: [TypeAccess] String
# 6| 5: [BlockStmt] { ... }
# 6| 0: [ReturnStmt] return ...
# 6| 0: [StringLiteral] ""
# 9| 4: [Method] annotationType
#-----| 1: (Annotations)
# 8| 1: [Annotation] Override
# 9| 3: [TypeAccess] Class<? extends Annotation>
# 9| 0: [WildcardTypeAccess] ? ...
# 9| 0: [TypeAccess] Annotation
# 9| 5: [BlockStmt] { ... }
# 9| 1: [Annotation] Override
# 10| 3: [TypeAccess] String
# 10| 5: [BlockStmt] { ... }
# 10| 0: [ReturnStmt] return ...
# 10| 0: [NullLiteral] null
# 10| 0: [StringLiteral] ""
# 13| 4: [Method] annotationType
#-----| 1: (Annotations)
# 12| 1: [Annotation] Override
# 13| 3: [TypeAccess] Class<? extends Annotation>
# 13| 0: [WildcardTypeAccess] ? ...
# 13| 0: [TypeAccess] Annotation
# 13| 5: [BlockStmt] { ... }
# 14| 0: [ReturnStmt] return ...
# 14| 0: [NullLiteral] null

View File

@@ -5,16 +5,26 @@ annotationDeclarations
| def.kt:5:1:7:51 | Deprecated | def.kt:6:5:6:23 | message |
| def.kt:5:1:7:51 | Deprecated | def.kt:7:5:7:50 | replaceWith |
annotations
| def.kt:0:0:0:0 | ReplaceWith | def.kt:9:1:9:57 | Deprecated | def.kt:3:1:3:52 | ReplaceWith |
| def.kt:9:1:9:57 | Deprecated | def.kt:9:1:11:7 | X | def.kt:5:1:7:51 | Deprecated |
| def.kt:10:1:10:24 | SomeAnnotation | def.kt:9:1:11:7 | X | def.kt:1:1:1:87 | SomeAnnotation |
| use.java:2:1:4:48 | Deprecated | use.java:6:14:6:16 | use | def.kt:5:1:7:51 | Deprecated |
| use.java:4:18:4:47 | ReplaceWith | use.java:2:1:4:48 | Deprecated | def.kt:3:1:3:52 | ReplaceWith |
| use.java:5:1:5:24 | SomeAnnotation | use.java:6:14:6:16 | use | def.kt:1:1:1:87 | SomeAnnotation |
annotationValues
| def.kt:0:0:0:0 | ReplaceWith | def.kt:0:0:0:0 | Y |
| def.kt:9:1:9:57 | Deprecated | def.kt:0:0:0:0 | ReplaceWith |
| def.kt:9:1:9:57 | Deprecated | def.kt:0:0:0:0 | This class is deprecated |
| def.kt:10:1:10:24 | SomeAnnotation | def.kt:0:0:0:0 | 5 |
| def.kt:10:1:10:24 | SomeAnnotation | def.kt:0:0:0:0 | a |
| use.java:2:1:4:48 | Deprecated | use.java:3:14:3:39 | "This class is deprecated" |
| use.java:2:1:4:48 | Deprecated | use.java:4:18:4:47 | ReplaceWith |
| use.java:4:18:4:47 | ReplaceWith | use.java:4:44:4:46 | "Y" |
| use.java:5:1:5:24 | SomeAnnotation | use.java:5:21:5:23 | "b" |
#select
| def.kt:0:0:0:0 | DefKt | Class |
| def.kt:1:1:1:87 | SomeAnnotation | Interface |
| def.kt:3:1:3:52 | ReplaceWith | Interface |
| def.kt:5:1:7:51 | Deprecated | Interface |
| def.kt:9:1:11:7 | X | Class |
| use.java:2:14:2:16 | use | Class |
| use.java:6:14:6:16 | use | Class |

View File

@@ -1,4 +1,8 @@
@Deprecated(
message = "This class is deprecated",
replaceWith = @ReplaceWith(expression = "Y"))
@SomeAnnotation(y = "b")
public class use implements SomeAnnotation {
@Override
public int abc() { return 1; }