Extract enum entry initializers

This commit is contained in:
Tamas Vajk
2022-04-19 10:57:15 +02:00
committed by Ian Lynagh
parent 2647a45239
commit 4efb87a7dd
5 changed files with 155 additions and 50 deletions

View File

@@ -583,10 +583,29 @@ open class KotlinFileExtractor(
// Extract field initializers and init blocks (the latter can only occur in object initializers)
var idx = 0
fun extractFieldInitializer(f: IrField) {
val initializer = f.initializer
fun extractFieldInitializer(f: IrDeclaration) {
val static: Boolean
val initializer: IrExpressionBody?
val lhsType: TypeResults?
val vId: Label<out DbVariable>?
if (f is IrField) {
static = f.isStatic
initializer = f.initializer
lhsType = useType(f.type)
vId = useField(f)
} else if (f is IrEnumEntry) {
static = true
initializer = f.initializerExpression
lhsType = getEnumEntryType(f)
if (lhsType == null) {
return
}
vId = useEnumEntry(f)
} else {
return
}
if (f.isStatic != extractStaticInitializers || initializer == null) {
if (static != extractStaticInitializers || initializer == null) {
return
}
@@ -606,16 +625,15 @@ open class KotlinFileExtractor(
tw.writeKtInitializerAssignment(assignmentId)
val lhsId = tw.getFreshIdLabel<DbVaraccess>()
val lhsType = useType(f.type)
tw.writeExprs_varaccess(lhsId, lhsType.javaResult.id, assignmentId, 0)
tw.writeExprsKotlinType(lhsId, lhsType.kotlinResult.id)
tw.writeHasLocation(lhsId, declLocId)
tw.writeCallableEnclosingExpr(lhsId, blockAndFunctionId.second)
tw.writeStatementEnclosingExpr(lhsId, stmtId)
val vId = useField(f)
tw.writeVariableBinding(lhsId, vId)
if (f.isStatic) {
if (static) {
extractStaticTypeAccessQualifier(f, lhsId, declLocId, blockAndFunctionId.second, stmtId)
}
@@ -630,6 +648,9 @@ open class KotlinFileExtractor(
is IrField -> {
extractFieldInitializer(decl)
}
is IrEnumEntry -> {
extractFieldInitializer(decl)
}
is IrAnonymousInitializer -> {
if (decl.isStatic != extractStaticInitializers) {
continue
@@ -819,17 +840,24 @@ open class KotlinFileExtractor(
}
}
private fun getEnumEntryType(ee: IrEnumEntry): TypeResults? {
val parent = ee.parent
if (parent !is IrClass) {
logger.errorElement("Enum entry with unexpected parent: " + parent.javaClass, ee)
return null
} else if (parent.typeParameters.isNotEmpty()) {
logger.errorElement("Enum entry parent class has type parameters: " + parent.name, ee)
return null
} else {
return useSimpleTypeClass(parent, emptyList(), false)
}
}
fun extractEnumEntry(ee: IrEnumEntry, parentId: Label<out DbReftype>) {
with("enum entry", ee) {
DeclarationStackAdjuster(ee).use {
val id = useEnumEntry(ee)
val parent = ee.parent
if (parent !is IrClass) {
logger.errorElement("Enum entry with unexpected parent: " + parent.javaClass, ee)
} else if (parent.typeParameters.isNotEmpty()) {
logger.errorElement("Enum entry parent class has type parameters: " + parent.name, ee)
} else {
val type = useSimpleTypeClass(parent, emptyList(), false)
val type = getEnumEntryType(ee) ?: return
tw.writeFields(id, ee.name.asString(), type.javaResult.id, parentId, id)
tw.writeFieldsKotlinType(id, type.kotlinResult.id)
val locId = tw.getLocation(ee)
@@ -846,7 +874,6 @@ open class KotlinFileExtractor(
}
}
}
}
fun extractTypeAlias(ta: IrTypeAlias) {
with("type alias", ta) {

View File

@@ -130,21 +130,29 @@ classes.kt:
#-----| 4: (Parameters)
# 0| 0: [Parameter] value
# 0| 0: [TypeAccess] String
# 49| 3: [Constructor] Direction
# 49| 4: [Constructor] Direction
# 49| 5: [BlockStmt] { ... }
# 49| 0: [ExprStmt] <Expr>;
# 49| 0: [ClassInstanceExpr] new Enum(...)
# 49| -3: [TypeAccess] Unit
# 49| 0: [TypeAccess] Direction
# 49| 1: [BlockStmt] { ... }
# 50| 4: [FieldDeclaration] Direction NORTH;
# 50| 5: [FieldDeclaration] Direction NORTH;
# 50| -1: [TypeAccess] Direction
# 50| 5: [FieldDeclaration] Direction SOUTH;
# 50| 0: [ClassInstanceExpr] new Direction(...)
# 50| -3: [TypeAccess] Direction
# 50| 6: [FieldDeclaration] Direction SOUTH;
# 50| -1: [TypeAccess] Direction
# 50| 6: [FieldDeclaration] Direction WEST;
# 50| 0: [ClassInstanceExpr] new Direction(...)
# 50| -3: [TypeAccess] Direction
# 50| 7: [FieldDeclaration] Direction WEST;
# 50| -1: [TypeAccess] Direction
# 50| 7: [FieldDeclaration] Direction EAST;
# 50| 0: [ClassInstanceExpr] new Direction(...)
# 50| -3: [TypeAccess] Direction
# 50| 8: [FieldDeclaration] Direction EAST;
# 50| -1: [TypeAccess] Direction
# 50| 0: [ClassInstanceExpr] new Direction(...)
# 50| -3: [TypeAccess] Direction
# 53| 12: [Class] Color
# 0| 1: [Method] values
# 0| 3: [TypeAccess] Color[]
@@ -154,7 +162,7 @@ classes.kt:
#-----| 4: (Parameters)
# 0| 0: [Parameter] value
# 0| 0: [TypeAccess] String
# 53| 3: [Constructor] Color
# 53| 4: [Constructor] Color
#-----| 4: (Parameters)
# 53| 0: [Parameter] rgb
# 53| 0: [TypeAccess] int
@@ -167,21 +175,30 @@ classes.kt:
# 53| 0: [ExprStmt] <Expr>;
# 53| 0: [KtInitializerAssignExpr] ...=...
# 53| 0: [VarAccess] rgb
# 53| 4: [Method] getRgb
# 53| 5: [Method] getRgb
# 53| 3: [TypeAccess] int
# 53| 5: [BlockStmt] { ... }
# 53| 0: [ReturnStmt] return ...
# 53| 0: [VarAccess] this.rgb
# 53| -1: [ThisAccess] this
# 53| 4: [FieldDeclaration] int rgb;
# 53| 5: [FieldDeclaration] int rgb;
# 53| -1: [TypeAccess] int
# 53| 0: [VarAccess] rgb
# 54| 6: [FieldDeclaration] Color RED;
# 54| 7: [FieldDeclaration] Color RED;
# 54| -1: [TypeAccess] Color
# 55| 7: [FieldDeclaration] Color GREEN;
# 54| 0: [ClassInstanceExpr] new Color(...)
# 54| -3: [TypeAccess] Color
# 54| 0: [IntegerLiteral] 16711680
# 55| 8: [FieldDeclaration] Color GREEN;
# 55| -1: [TypeAccess] Color
# 56| 8: [FieldDeclaration] Color BLUE;
# 55| 0: [ClassInstanceExpr] new Color(...)
# 55| -3: [TypeAccess] Color
# 55| 0: [IntegerLiteral] 65280
# 56| 9: [FieldDeclaration] Color BLUE;
# 56| -1: [TypeAccess] Color
# 56| 0: [ClassInstanceExpr] new Color(...)
# 56| -3: [TypeAccess] Color
# 56| 0: [IntegerLiteral] 255
# 59| 13: [Interface] Interface1
# 60| 14: [Interface] Interface2
# 61| 15: [GenericType,Interface,ParameterizedType] Interface3

View File

@@ -2889,21 +2889,29 @@ exprs.kt:
#-----| 4: (Parameters)
# 0| 0: [Parameter] value
# 0| 0: [TypeAccess] String
# 168| 3: [Constructor] Direction
# 168| 4: [Constructor] Direction
# 168| 5: [BlockStmt] { ... }
# 168| 0: [ExprStmt] <Expr>;
# 168| 0: [ClassInstanceExpr] new Enum(...)
# 168| -3: [TypeAccess] Unit
# 168| 0: [TypeAccess] Direction
# 168| 1: [BlockStmt] { ... }
# 169| 4: [FieldDeclaration] Direction NORTH;
# 169| 5: [FieldDeclaration] Direction NORTH;
# 169| -1: [TypeAccess] Direction
# 169| 5: [FieldDeclaration] Direction SOUTH;
# 169| 0: [ClassInstanceExpr] new Direction(...)
# 169| -3: [TypeAccess] Direction
# 169| 6: [FieldDeclaration] Direction SOUTH;
# 169| -1: [TypeAccess] Direction
# 169| 6: [FieldDeclaration] Direction WEST;
# 169| 0: [ClassInstanceExpr] new Direction(...)
# 169| -3: [TypeAccess] Direction
# 169| 7: [FieldDeclaration] Direction WEST;
# 169| -1: [TypeAccess] Direction
# 169| 7: [FieldDeclaration] Direction EAST;
# 169| 0: [ClassInstanceExpr] new Direction(...)
# 169| -3: [TypeAccess] Direction
# 169| 8: [FieldDeclaration] Direction EAST;
# 169| -1: [TypeAccess] Direction
# 169| 0: [ClassInstanceExpr] new Direction(...)
# 169| -3: [TypeAccess] Direction
# 172| 7: [Class] Color
# 0| 1: [Method] values
# 0| 3: [TypeAccess] Color[]
@@ -2913,7 +2921,7 @@ exprs.kt:
#-----| 4: (Parameters)
# 0| 0: [Parameter] value
# 0| 0: [TypeAccess] String
# 172| 3: [Constructor] Color
# 172| 4: [Constructor] Color
#-----| 4: (Parameters)
# 172| 0: [Parameter] rgb
# 172| 0: [TypeAccess] int
@@ -2926,21 +2934,30 @@ exprs.kt:
# 172| 0: [ExprStmt] <Expr>;
# 172| 0: [KtInitializerAssignExpr] ...=...
# 172| 0: [VarAccess] rgb
# 172| 4: [Method] getRgb
# 172| 5: [Method] getRgb
# 172| 3: [TypeAccess] int
# 172| 5: [BlockStmt] { ... }
# 172| 0: [ReturnStmt] return ...
# 172| 0: [VarAccess] this.rgb
# 172| -1: [ThisAccess] this
# 172| 4: [FieldDeclaration] int rgb;
# 172| 5: [FieldDeclaration] int rgb;
# 172| -1: [TypeAccess] int
# 172| 0: [VarAccess] rgb
# 173| 6: [FieldDeclaration] Color RED;
# 173| 7: [FieldDeclaration] Color RED;
# 173| -1: [TypeAccess] Color
# 174| 7: [FieldDeclaration] Color GREEN;
# 173| 0: [ClassInstanceExpr] new Color(...)
# 173| -3: [TypeAccess] Color
# 173| 0: [IntegerLiteral] 16711680
# 174| 8: [FieldDeclaration] Color GREEN;
# 174| -1: [TypeAccess] Color
# 175| 8: [FieldDeclaration] Color BLUE;
# 174| 0: [ClassInstanceExpr] new Color(...)
# 174| -3: [TypeAccess] Color
# 174| 0: [IntegerLiteral] 65280
# 175| 9: [FieldDeclaration] Color BLUE;
# 175| -1: [TypeAccess] Color
# 175| 0: [ClassInstanceExpr] new Color(...)
# 175| -3: [TypeAccess] Color
# 175| 0: [IntegerLiteral] 255
# 183| 8: [Interface] Interface1
# 185| 9: [Class] Class1
# 185| 1: [Constructor] Class1

View File

@@ -1446,10 +1446,30 @@
| exprs.kt:168:1:170:1 | Direction | exprs.kt:168:6:170:1 | Direction | TypeAccess |
| exprs.kt:168:1:170:1 | Unit | exprs.kt:168:6:170:1 | Direction | TypeAccess |
| exprs.kt:168:1:170:1 | new Enum(...) | exprs.kt:168:6:170:1 | Direction | ClassInstanceExpr |
| exprs.kt:169:5:169:10 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:169:5:169:10 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:5:169:10 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:5:169:10 | Direction | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:169:5:169:10 | Direction.NORTH | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:169:5:169:10 | new Direction(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:169:12:169:17 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:169:12:169:17 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:12:169:17 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:12:169:17 | Direction | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:169:12:169:17 | Direction.SOUTH | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:169:12:169:17 | new Direction(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:169:19:169:23 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:169:19:169:23 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:19:169:23 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:19:169:23 | Direction | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:169:19:169:23 | Direction.WEST | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:169:19:169:23 | new Direction(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:169:25:169:28 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:169:25:169:28 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:25:169:28 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:169:25:169:28 | Direction | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:169:25:169:28 | Direction.EAST | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:169:25:169:28 | new Direction(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:172:1:176:1 | Color | exprs.kt:172:6:176:1 | Color | TypeAccess |
| exprs.kt:172:1:176:1 | Unit | exprs.kt:172:6:176:1 | Color | TypeAccess |
| exprs.kt:172:1:176:1 | new Enum(...) | exprs.kt:172:6:176:1 | Color | ClassInstanceExpr |
@@ -1461,9 +1481,27 @@
| exprs.kt:172:18:172:29 | rgb | exprs.kt:172:6:176:1 | Color | VarAccess |
| exprs.kt:172:18:172:29 | this | exprs.kt:172:18:172:29 | getRgb | ThisAccess |
| exprs.kt:172:18:172:29 | this.rgb | exprs.kt:172:18:172:29 | getRgb | VarAccess |
| exprs.kt:173:5:173:18 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:173:5:173:18 | Color | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:173:5:173:18 | Color | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:173:5:173:18 | Color | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:173:5:173:18 | Color.RED | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:173:5:173:18 | new Color(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:173:9:173:16 | 16711680 | exprs.kt:0:0:0:0 | <clinit> | IntegerLiteral |
| exprs.kt:174:5:174:20 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:174:5:174:20 | Color | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:174:5:174:20 | Color | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:174:5:174:20 | Color | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:174:5:174:20 | Color.GREEN | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:174:5:174:20 | new Color(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:174:11:174:18 | 65280 | exprs.kt:0:0:0:0 | <clinit> | IntegerLiteral |
| exprs.kt:175:5:175:18 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
| exprs.kt:175:5:175:18 | Color | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:175:5:175:18 | Color | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
| exprs.kt:175:5:175:18 | Color | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:175:5:175:18 | Color.BLUE | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
| exprs.kt:175:5:175:18 | new Color(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
| exprs.kt:175:10:175:17 | 255 | exprs.kt:0:0:0:0 | <clinit> | IntegerLiteral |
| exprs.kt:178:1:181:1 | Unit | file://:0:0:0:0 | <none> | TypeAccess |
| exprs.kt:179:5:179:31 | south | exprs.kt:178:1:181:1 | enums | LocalVariableDeclExpr |
| exprs.kt:179:27:179:31 | Direction | exprs.kt:178:1:181:1 | enums | TypeAccess |

View File

@@ -82,19 +82,25 @@ A.kt:
#-----| 4: (Parameters)
# 0| 0: [Parameter] value
# 0| 0: [TypeAccess] String
# 23| 3: [Constructor] Enu
# 23| 4: [Constructor] Enu
# 23| 5: [BlockStmt] { ... }
# 23| 0: [ExprStmt] <Expr>;
# 23| 0: [ClassInstanceExpr] new Enum(...)
# 23| -3: [TypeAccess] Unit
# 23| 0: [TypeAccess] Enu
# 23| 1: [BlockStmt] { ... }
# 24| 4: [FieldDeclaration] Enu A;
# 24| 5: [FieldDeclaration] Enu A;
# 24| -1: [TypeAccess] Enu
# 24| 5: [FieldDeclaration] Enu B;
# 24| 0: [ClassInstanceExpr] new Enu(...)
# 24| -3: [TypeAccess] Enu
# 24| 6: [FieldDeclaration] Enu B;
# 24| -1: [TypeAccess] Enu
# 24| 6: [FieldDeclaration] Enu C;
# 24| 0: [ClassInstanceExpr] new Enu(...)
# 24| -3: [TypeAccess] Enu
# 24| 7: [FieldDeclaration] Enu C;
# 24| -1: [TypeAccess] Enu
# 24| 0: [ClassInstanceExpr] new Enu(...)
# 24| -3: [TypeAccess] Enu
B.java:
# 0| [CompilationUnit] B
# 1| 1: [Class] B