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

Skip to content

Commit 0b3754c

Browse files
committed
Kotlin: Fix handling Unit in various places
1 parent ec827d2 commit 0b3754c

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,12 @@ open class KotlinFileExtractor(
354354
val paramsSignature = paramTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { it.javaResult.signature!! }
355355

356356
if (f.symbol is IrConstructorSymbol) {
357-
val returnType = useType(erase(f.returnType))
357+
val returnType = useType(erase(f.returnType), TypeContext.RETURN)
358358
val shortName = if (f.returnType.isAnonymous) "" else f.returnType.classFqName?.shortName()?.asString() ?: f.name.asString()
359359
@Suppress("UNCHECKED_CAST")
360360
tw.writeConstrs(id as Label<DbConstructor>, shortName, "$shortName$paramsSignature", returnType.javaResult.id, returnType.kotlinResult.id, parentId, id)
361361
} else {
362-
val returnType = useType(f.returnType)
362+
val returnType = useType(f.returnType, TypeContext.RETURN)
363363
val shortName = f.name.asString()
364364
@Suppress("UNCHECKED_CAST")
365365
tw.writeMethods(id as Label<DbMethod>, shortName, "$shortName$paramsSignature", returnType.javaResult.id, returnType.kotlinResult.id, parentId, id)

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ open class KotlinUsesExtractor(
101101
this.withSourceFileOfClass(extractClass).extractClassInstance(extractClass, typeArgs)
102102
}
103103

104-
// Extract both the Kotlin and equivalent Java classes, so that we have database entries
105-
// for both even if all internal references to the Kotlin type are substituted.
106-
extractClassLaterIfExternal(c)
107104
substituteClass?.let { extractClassLaterIfExternal(it) }
108105
})
106+
// Extract both the Kotlin and equivalent Java classes, so that we have database entries
107+
// for both even if all internal references to the Kotlin type are substituted.
108+
// TODO: Should we do this inside the label initialisation? That would require all
109+
// initialisers of the label to do it.
110+
extractClassLaterIfExternal(c)
109111

110112
return UseClassInstanceResult(TypeResult(classLabel, extractClass.fqNameWhenAvailable?.asString(), classId.shortName), extractClass)
111113
}
@@ -280,9 +282,10 @@ open class KotlinUsesExtractor(
280282
return classId
281283
}
282284
fun primitiveType(kotlinClass: IrClass, primitiveName: String?,
285+
otherIsPrimitive: Boolean,
283286
javaPackageName: String, javaClassName: String,
284287
kotlinPackageName: String, kotlinClassName: String): TypeResults {
285-
val javaResult = if (context != TypeContext.GENERIC_ARGUMENT && !s.hasQuestionMark && primitiveName != null) {
288+
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.hasQuestionMark && primitiveName != null) {
286289
val label: Label<DbPrimitive> = tw.getLabelFor("@\"type;$primitiveName\"", {
287290
tw.writePrimitives(it, primitiveName)
288291
})
@@ -327,8 +330,9 @@ XXX delete?
327330
*/
328331
primitiveInfo != null -> return primitiveType(
329332
s.classifier.owner as IrClass,
330-
primitiveInfo.primitiveName, primitiveInfo.javaPackageName,
331-
primitiveInfo.javaClassName, primitiveInfo.kotlinPackageName, primitiveInfo.kotlinClassName
333+
primitiveInfo.primitiveName, primitiveInfo.otherIsPrimitive,
334+
primitiveInfo.javaPackageName, primitiveInfo.javaClassName,
335+
primitiveInfo.kotlinPackageName, primitiveInfo.kotlinClassName
332336
)
333337
/*
334338
TODO: Test case: nullable and has-question-mark type variables:
@@ -644,13 +648,13 @@ class X {
644648
fun useValueParameter(vp: IrValueParameter): Label<out DbParam> =
645649
tw.getLabelFor(getValueParameterLabel(vp))
646650

647-
fun getFieldLabel(p: IrField): String {
648-
val parentId = useDeclarationParent(p.parent)
649-
return "@\"field;{$parentId};${p.name.asString()}\""
651+
fun getFieldLabel(f: IrField): String {
652+
val parentId = useDeclarationParent(f.parent)
653+
return "@\"field;{$parentId};${f.name.asString()}\""
650654
}
651655

652-
fun useField(p: IrField): Label<out DbField> =
653-
tw.getLabelFor(getFieldLabel(p))
656+
fun useField(f: IrField): Label<out DbField> =
657+
tw.getLabelFor(getFieldLabel(f))
654658

655659
fun getPropertyLabel(p: IrProperty): String {
656660
val parentId = useDeclarationParent(p.parent)

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@ import org.jetbrains.kotlin.ir.types.IdSignatureValues
44

55
data class PrimitiveTypeInfo(
66
val primitiveName: String?,
7+
val otherIsPrimitive: Boolean,
78
val javaPackageName: String, val javaClassName: String,
89
val kotlinPackageName: String, val kotlinClassName: String
910
)
1011

1112
val primitiveTypeMapping = mapOf(
12-
IdSignatureValues._byte to PrimitiveTypeInfo("byte", "java.lang", "Byte", "kotlin", "Byte"),
13-
IdSignatureValues._short to PrimitiveTypeInfo("short", "java.lang", "Short", "kotlin", "Short"),
14-
IdSignatureValues._int to PrimitiveTypeInfo("int", "java.lang", "Integer", "kotlin", "Int"),
15-
IdSignatureValues._long to PrimitiveTypeInfo("long", "java.lang", "Long", "kotlin", "Long"),
13+
IdSignatureValues._byte to PrimitiveTypeInfo("byte", true, "java.lang", "Byte", "kotlin", "Byte"),
14+
IdSignatureValues._short to PrimitiveTypeInfo("short", true, "java.lang", "Short", "kotlin", "Short"),
15+
IdSignatureValues._int to PrimitiveTypeInfo("int", true, "java.lang", "Integer", "kotlin", "Int"),
16+
IdSignatureValues._long to PrimitiveTypeInfo("long", true, "java.lang", "Long", "kotlin", "Long"),
1617

17-
IdSignatureValues.uByte to PrimitiveTypeInfo("byte", "kotlin", "UByte", "kotlin", "UByte"),
18-
IdSignatureValues.uShort to PrimitiveTypeInfo("short", "kotlin", "UShort", "kotlin", "UShort"),
19-
IdSignatureValues.uInt to PrimitiveTypeInfo("int", "kotlin", "UInt", "kotlin", "UInt"),
20-
IdSignatureValues.uLong to PrimitiveTypeInfo("long", "kotlin", "ULong", "kotlin", "ULong"),
18+
IdSignatureValues.uByte to PrimitiveTypeInfo("byte", true, "kotlin", "UByte", "kotlin", "UByte"),
19+
IdSignatureValues.uShort to PrimitiveTypeInfo("short", true, "kotlin", "UShort", "kotlin", "UShort"),
20+
IdSignatureValues.uInt to PrimitiveTypeInfo("int", true, "kotlin", "UInt", "kotlin", "UInt"),
21+
IdSignatureValues.uLong to PrimitiveTypeInfo("long", true, "kotlin", "ULong", "kotlin", "ULong"),
2122

22-
IdSignatureValues._double to PrimitiveTypeInfo("double", "java.lang", "Double", "kotlin", "Double"),
23-
IdSignatureValues._float to PrimitiveTypeInfo("float", "java.lang", "Float", "kotlin", "Float"),
23+
IdSignatureValues._double to PrimitiveTypeInfo("double", true, "java.lang", "Double", "kotlin", "Double"),
24+
IdSignatureValues._float to PrimitiveTypeInfo("float", true, "java.lang", "Float", "kotlin", "Float"),
2425

25-
IdSignatureValues._boolean to PrimitiveTypeInfo("boolean", "java.lang", "Boolean", "kotlin", "Boolean"),
26+
IdSignatureValues._boolean to PrimitiveTypeInfo("boolean", true, "java.lang", "Boolean", "kotlin", "Boolean"),
2627

27-
IdSignatureValues._char to PrimitiveTypeInfo("char", "java.lang", "Character", "kotlin", "Char"),
28+
IdSignatureValues._char to PrimitiveTypeInfo("char", true, "java.lang", "Character", "kotlin", "Char"),
2829

29-
IdSignatureValues.unit to PrimitiveTypeInfo("void", "java.lang", "Void", "kotlin", "Nothing"), // TODO: Is this right?
30-
IdSignatureValues.nothing to PrimitiveTypeInfo(null, "java.lang", "Void", "kotlin", "Nothing"), // TODO: Is this right?
30+
IdSignatureValues.unit to PrimitiveTypeInfo("void", false, "kotlin", "Unit", "kotlin", "Unit"),
31+
IdSignatureValues.nothing to PrimitiveTypeInfo(null, true, "java.lang", "Void", "kotlin", "Nothing"),
3132
)

0 commit comments

Comments
 (0)