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

Skip to content

Commit 86c31cb

Browse files
committed
Kotlin: Add Label.cast()
1 parent c89f316 commit 86c31cb

4 files changed

Lines changed: 38 additions & 65 deletions

File tree

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

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,22 @@ open class KotlinFileExtractor(
104104
}
105105
}
106106
is IrFunction -> {
107-
@Suppress("UNCHECKED_CAST")
108-
val parentId = useDeclarationParent(declaration.parent, false) as Label<DbReftype>
107+
val parentId = useDeclarationParent(declaration.parent, false).cast<DbReftype>()
109108
extractFunction(declaration, parentId, true, null, listOf())
110109
}
111110
is IrAnonymousInitializer -> {
112111
// Leaving this intentionally empty. init blocks are extracted during class extraction.
113112
}
114113
is IrProperty -> {
115-
@Suppress("UNCHECKED_CAST")
116-
val parentId = useDeclarationParent(declaration.parent, false) as Label<DbReftype>
114+
val parentId = useDeclarationParent(declaration.parent, false).cast<DbReftype>()
117115
extractProperty(declaration, parentId, true, null, listOf())
118116
}
119117
is IrEnumEntry -> {
120-
@Suppress("UNCHECKED_CAST")
121-
val parentId = useDeclarationParent(declaration.parent, false) as Label<DbReftype>
118+
val parentId = useDeclarationParent(declaration.parent, false).cast<DbReftype>()
122119
extractEnumEntry(declaration, parentId)
123120
}
124121
is IrField -> {
125-
@Suppress("UNCHECKED_CAST")
126-
val parentId = useDeclarationParent(declaration.parent, false) as Label<DbReftype>
122+
val parentId = useDeclarationParent(declaration.parent, false).cast<DbReftype>()
127123
extractField(declaration, parentId)
128124
}
129125
is IrTypeAlias -> extractTypeAlias(declaration)
@@ -252,16 +248,12 @@ open class KotlinFileExtractor(
252248
// TODO: There's lots of duplication between this and extractClassSource.
253249
// Can we share it?
254250
if(kind == ClassKind.INTERFACE || kind == ClassKind.ANNOTATION_CLASS) {
255-
@Suppress("UNCHECKED_CAST")
256-
val interfaceId = id as Label<out DbInterface>
257-
@Suppress("UNCHECKED_CAST")
258-
val sourceInterfaceId = useClassSource(c) as Label<out DbInterface>
251+
val interfaceId = id.cast<DbInterface>()
252+
val sourceInterfaceId = useClassSource(c).cast<DbInterface>()
259253
tw.writeInterfaces(interfaceId, cls, pkgId, sourceInterfaceId)
260254
} else {
261-
@Suppress("UNCHECKED_CAST")
262-
val classId = id as Label<out DbClass>
263-
@Suppress("UNCHECKED_CAST")
264-
val sourceClassId = useClassSource(c) as Label<out DbClass>
255+
val classId = id.cast<DbClass>()
256+
val sourceClassId = useClassSource(c).cast<DbClass>()
265257
tw.writeClasses(classId, cls, pkgId, sourceClassId)
266258

267259
if (kind == ClassKind.ENUM_CLASS) {
@@ -344,8 +336,7 @@ open class KotlinFileExtractor(
344336

345337
private fun extractLocalTypeDeclStmt(c: IrClass, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
346338
val extractStaticInit = c.declarations.none { it is IrAnonymousInitializer }
347-
@Suppress("UNCHECKED_CAST")
348-
val id = extractClassSource(c, true, extractStaticInit) as Label<out DbClass>
339+
val id = extractClassSource(c, true, extractStaticInit).cast<DbClass>()
349340
extractLocalTypeDeclStmt(id, c, callable, parent, idx)
350341
}
351342

@@ -362,8 +353,7 @@ open class KotlinFileExtractor(
362353
DeclarationStackAdjuster(c).use {
363354

364355
val id = if (c.isAnonymousObject) {
365-
@Suppress("UNCHECKED_CAST")
366-
useAnonymousClass(c).javaResult.id as Label<out DbClass>
356+
useAnonymousClass(c).javaResult.id.cast<DbClass>()
367357
} else {
368358
useClassSource(c)
369359
}
@@ -372,12 +362,10 @@ open class KotlinFileExtractor(
372362
val pkgId = extractPackage(pkg)
373363
val kind = c.kind
374364
if (kind == ClassKind.INTERFACE || kind == ClassKind.ANNOTATION_CLASS) {
375-
@Suppress("UNCHECKED_CAST")
376-
val interfaceId = id as Label<out DbInterface>
365+
val interfaceId = id.cast<DbInterface>()
377366
tw.writeInterfaces(interfaceId, cls, pkgId, interfaceId)
378367
} else {
379-
@Suppress("UNCHECKED_CAST")
380-
val classId = id as Label<out DbClass>
368+
val classId = id.cast<DbClass>()
381369
tw.writeClasses(classId, cls, pkgId, classId)
382370

383371
if (kind == ClassKind.ENUM_CLASS) {
@@ -410,8 +398,7 @@ open class KotlinFileExtractor(
410398
tw.writeFieldsKotlinType(instance.id, type.kotlinResult.id)
411399
tw.writeHasLocation(instance.id, locId)
412400
addModifiers(instance.id, "public", "static", "final")
413-
@Suppress("UNCHECKED_CAST")
414-
tw.writeClass_object(id as Label<DbClass>, instance.id)
401+
tw.writeClass_object(id.cast<DbClass>(), instance.id)
415402
}
416403

417404
extractClassModifiers(c, id)
@@ -430,8 +417,7 @@ open class KotlinFileExtractor(
430417
if (parent is IrClass) {
431418
val parentId =
432419
if (parent.isAnonymousObject) {
433-
@Suppress("UNCHECKED_CAST")
434-
useAnonymousClass(parent).javaResult.id as Label<out DbClass>
420+
useAnonymousClass(parent).javaResult.id.cast<DbClass>()
435421
} else {
436422
useClassInstance(parent, parentClassTypeArguments).typeResult.id
437423
}
@@ -447,8 +433,7 @@ open class KotlinFileExtractor(
447433
tw.writeFieldsKotlinType(instance.id, type.kotlinResult.id)
448434
tw.writeHasLocation(instance.id, innerLocId)
449435
addModifiers(instance.id, "public", "static", "final")
450-
@Suppress("UNCHECKED_CAST")
451-
tw.writeType_companion_object(parentId, instance.id, innerId as Label<DbClass>)
436+
tw.writeType_companion_object(parentId, instance.id, innerId.cast<DbClass>())
452437
}
453438
}
454439

@@ -660,8 +645,7 @@ open class KotlinFileExtractor(
660645
}
661646
val allParamTypes = if (extReceiver != null) {
662647
val extendedType = useType(extReceiver.type)
663-
@Suppress("UNCHECKED_CAST")
664-
tw.writeKtExtensionFunctions(id as Label<DbMethod>, extendedType.javaResult.id, extendedType.kotlinResult.id)
648+
tw.writeKtExtensionFunctions(id.cast<DbMethod>(), extendedType.javaResult.id, extendedType.kotlinResult.id)
665649

666650
val t = extractValueParameter(extReceiver, id, 0, null, sourceDeclaration, classTypeArgsIncludingOuterClasses)
667651
listOf(t) + paramTypes
@@ -680,15 +664,13 @@ open class KotlinFileExtractor(
680664
typeSubstitution != null -> useType(substReturnType).javaResult.shortName
681665
else -> f.returnType.classFqName?.shortName()?.asString() ?: f.name.asString()
682666
}
683-
@Suppress("UNCHECKED_CAST")
684-
val constrId = id as Label<DbConstructor>
667+
val constrId = id.cast<DbConstructor>()
685668
tw.writeConstrs(constrId, shortName, "$shortName$paramsSignature", unitType.javaResult.id, parentId, sourceDeclaration as Label<DbConstructor>)
686669
tw.writeConstrsKotlinType(constrId, unitType.kotlinResult.id)
687670
} else {
688671
val returnType = useType(substReturnType, TypeContext.RETURN)
689672
val shortName = getFunctionShortName(f)
690-
@Suppress("UNCHECKED_CAST")
691-
val methodId = id as Label<DbMethod>
673+
val methodId = id.cast<DbMethod>()
692674
tw.writeMethods(methodId, shortName, "$shortName$paramsSignature", returnType.javaResult.id, parentId, sourceDeclaration as Label<DbMethod>)
693675
tw.writeMethodsKotlinType(methodId, returnType.kotlinResult.id)
694676
}
@@ -755,8 +737,7 @@ open class KotlinFileExtractor(
755737
val setter = p.setter
756738

757739
if (getter != null) {
758-
@Suppress("UNCHECKED_CAST")
759-
val getterId = extractFunction(getter, parentId, extractBackingField, typeSubstitution, classTypeArgs) as Label<out DbMethod>?
740+
val getterId = extractFunction(getter, parentId, extractBackingField, typeSubstitution, classTypeArgs)?.cast<DbMethod>()
760741
if (getterId != null) {
761742
tw.writeKtPropertyGetters(id, getterId)
762743
}
@@ -770,8 +751,7 @@ open class KotlinFileExtractor(
770751
if (!p.isVar) {
771752
logger.errorElement("!isVar property with a setter", p)
772753
}
773-
@Suppress("UNCHECKED_CAST")
774-
val setterId = extractFunction(setter, parentId, extractBackingField, typeSubstitution, classTypeArgs) as Label<out DbMethod>?
754+
val setterId = extractFunction(setter, parentId, extractBackingField, typeSubstitution, classTypeArgs)?.cast<DbMethod>()
775755
if (setterId != null) {
776756
tw.writeKtPropertySetters(id, setterId)
777757
}
@@ -1937,8 +1917,7 @@ open class KotlinFileExtractor(
19371917
val id = extractNewExpr(e.symbol.owner, (e.type as? IrSimpleType)?.arguments, type, locId, parent, idx, callable, enclosingStmt)
19381918

19391919
if (isAnonymous) {
1940-
@Suppress("UNCHECKED_CAST")
1941-
tw.writeIsAnonymClass(type.javaResult.id as Label<DbClass>, id)
1920+
tw.writeIsAnonymClass(type.javaResult.id.cast<DbClass>(), id)
19421921
}
19431922

19441923
extractCallValueArguments(id, e, enclosingStmt, callable, 0)
@@ -2147,8 +2126,7 @@ open class KotlinFileExtractor(
21472126
val methodId = useFunction<DbConstructor>(e.symbol.owner)
21482127

21492128
tw.writeHasLocation(id, locId)
2150-
@Suppress("UNCHECKED_CAST")
2151-
tw.writeCallableBinding(id as Label<DbCaller>, methodId)
2129+
tw.writeCallableBinding(id.cast<DbCaller>(), methodId)
21522130
extractCallValueArguments(id, e, id, callable, 0)
21532131
val dr = e.dispatchReceiver
21542132
if (dr != null) {
@@ -2919,8 +2897,7 @@ open class KotlinFileExtractor(
29192897
writeExpressionMetadataToTrapFile(callId, labels.methodId, retId)
29202898

29212899
val callableId = useFunction<DbCallable>(target.owner.realOverrideTarget, classTypeArgsIncludingOuterClasses)
2922-
@Suppress("UNCHECKED_CAST")
2923-
tw.writeCallableBinding(callId as Label<out DbCaller>, callableId)
2900+
tw.writeCallableBinding(callId.cast<DbCaller>(), callableId)
29242901

29252902
val useFirstArgAsDispatch: Boolean
29262903
if (dispatchReceiver != null) {
@@ -3399,8 +3376,7 @@ open class KotlinFileExtractor(
33993376
locId: Label<DbLocation>,
34003377
parameters: List<IrValueParameter>
34013378
) {
3402-
@Suppress("UNCHECKED_CAST")
3403-
val funLabels = addFunctionNInvoke(tw.getFreshIdLabel(), lambda.returnType, ids.type.javaResult.id as Label<DbReftype>, locId)
3379+
val funLabels = addFunctionNInvoke(tw.getFreshIdLabel(), lambda.returnType, ids.type.javaResult.id.cast<DbReftype>(), locId)
34043380

34053381
// Return
34063382
val retId = tw.getFreshIdLabel<DbReturnstmt>()
@@ -3911,8 +3887,7 @@ open class KotlinFileExtractor(
39113887

39123888
val idNewexpr = extractNewExpr(ids.constructor, ids.type, locId, id, 1, callable, enclosingStmt)
39133889

3914-
@Suppress("UNCHECKED_CAST")
3915-
tw.writeIsAnonymClass(ids.type.javaResult.id as Label<DbClass>, idNewexpr)
3890+
tw.writeIsAnonymClass(ids.type.javaResult.id.cast<DbClass>(), idNewexpr)
39163891

39173892
extractTypeAccessRecursive(e.typeOperand, locId, idNewexpr, -3, callable, enclosingStmt)
39183893

@@ -3964,8 +3939,7 @@ open class KotlinFileExtractor(
39643939
currentDeclaration: IrDeclaration
39653940
): Label<out DbClass> {
39663941
// Write class
3967-
@Suppress("UNCHECKED_CAST")
3968-
val id = ids.type.javaResult.id as Label<out DbClass>
3942+
val id = ids.type.javaResult.id.cast<DbClass>()
39693943
val pkgId = extractPackage("")
39703944
tw.writeClasses(id, "", pkgId, id)
39713945
tw.writeHasLocation(id, locId)
@@ -3990,8 +3964,7 @@ open class KotlinFileExtractor(
39903964
val baseConstructorId = useFunction<DbConstructor>(baseConstructor as IrFunction)
39913965

39923966
tw.writeHasLocation(superCallId, locId)
3993-
@Suppress("UNCHECKED_CAST")
3994-
tw.writeCallableBinding(superCallId as Label<DbCaller>, baseConstructorId)
3967+
tw.writeCallableBinding(superCallId.cast<DbCaller>(), baseConstructorId)
39953968

39963969
addModifiers(id, "final")
39973970
addVisibilityModifierToLocalOrAnonymousClass(id)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ open class KotlinUsesExtractor(
838838
fun <T: DbCallable> useFunction(f: IrFunction, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>? = null): Label<out T> {
839839
if (f.isLocalFunction()) {
840840
val ids = getLocallyVisibleFunctionLabels(f)
841-
@Suppress("UNCHECKED_CAST")
842-
return ids.function as Label<out T>
841+
return ids.function.cast<T>()
843842
} else {
844843
return useFunctionCommon<T>(f, getFunctionLabel(f, classTypeArgsIncludingOuterClasses))
845844
}
@@ -864,14 +863,12 @@ open class KotlinUsesExtractor(
864863
// Note this function doesn't return a signature because type arguments are never incorporated into function signatures.
865864
return when (arg) {
866865
is IrStarProjection -> {
867-
@Suppress("UNCHECKED_CAST")
868-
val anyTypeLabel = useType(pluginContext.irBuiltIns.anyType).javaResult.id as Label<out DbReftype>
866+
val anyTypeLabel = useType(pluginContext.irBuiltIns.anyType).javaResult.id.cast<DbReftype>()
869867
TypeResult(extractBoundedWildcard(1, "@\"wildcard;\"", "?", anyTypeLabel), null, "?")
870868
}
871869
is IrTypeProjection -> {
872870
val boundResults = useType(arg.type, TypeContext.GENERIC_ARGUMENT)
873-
@Suppress("UNCHECKED_CAST")
874-
val boundLabel = boundResults.javaResult.id as Label<out DbReftype>
871+
val boundLabel = boundResults.javaResult.id.cast<DbReftype>()
875872

876873
return if(arg.variance == Variance.INVARIANT)
877874
@Suppress("UNCHECKED_CAST")
@@ -949,8 +946,7 @@ open class KotlinUsesExtractor(
949946

950947
fun useClassSource(c: IrClass): Label<out DbClassorinterface> {
951948
if (c.isAnonymousObject) {
952-
@Suppress("UNCHECKED_CAST")
953-
return useAnonymousClass(c).javaResult.id as Label<DbClass>
949+
return useAnonymousClass(c).javaResult.id.cast<DbClass>()
954950
}
955951

956952
// For source classes, the label doesn't include and type arguments

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import java.io.StringWriter
66
/**
77
* This represents a label (`#...`) in a TRAP file.
88
*/
9-
interface Label<T>
9+
interface Label<T> {
10+
fun <U> cast(): Label<U> {
11+
@Suppress("UNCHECKED_CAST")
12+
return this as Label<U>
13+
}
14+
}
1015

1116
/**
1217
* The label `#i`, e.g. `#123`. Most labels we generate are of this

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ open class TrapWriter (protected val loggerBase: LoggerBase, val lm: TrapLabelMa
5757
* initialised.
5858
*/
5959
fun <T> getExistingLabelFor(key: String): Label<T>? {
60-
@Suppress("UNCHECKED_CAST")
61-
return lm.labelMapping.get(key) as Label<T>?
60+
return lm.labelMapping.get(key)?.cast<T>()
6261
}
6362
/**
6463
* Returns the label for the given key, if one exists.

0 commit comments

Comments
 (0)