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

Skip to content

Commit c20ee76

Browse files
committed
Kotlin: Give fields a Kotlin type
This meant refactoring the EnumEntry extraction a bit. The IR doesn't give us a type for fields, so we have to make it up based on the parent.
1 parent e120059 commit c20ee76

6 files changed

Lines changed: 50 additions & 34 deletions

File tree

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,31 @@ open class KotlinUsesExtractor(
389389
return id
390390
}
391391

392+
fun useSimpleTypeClass(c: IrClass, args: List<IrTypeArgument>, hasQuestionMark: Boolean): TypeResults {
393+
val classInstanceResult = useClassInstance(c, args)
394+
val javaClassId = classInstanceResult.classLabel
395+
val kotlinQualClassName = getUnquotedClassLabel(c, args)
396+
val javaQualClassName = classInstanceResult.javaClass.fqNameForIrSerialization.asString()
397+
val javaSignature = javaQualClassName // TODO: Is this right?
398+
val javaResult = TypeResult(javaClassId, javaSignature)
399+
val kotlinResult = if (hasQuestionMark) {
400+
val kotlinSignature = "$kotlinQualClassName?" // TODO: Is this right?
401+
val kotlinLabel = "@\"kt_type;nullable;$kotlinQualClassName\""
402+
val kotlinId: Label<DbKt_nullable_type> = tw.getLabelFor(kotlinLabel, {
403+
tw.writeKt_nullable_types(it, javaClassId)
404+
})
405+
TypeResult(kotlinId, kotlinSignature)
406+
} else {
407+
val kotlinSignature = kotlinQualClassName // TODO: Is this right?
408+
val kotlinLabel = "@\"kt_type;notnull;$kotlinQualClassName\""
409+
val kotlinId: Label<DbKt_notnull_type> = tw.getLabelFor(kotlinLabel, {
410+
tw.writeKt_notnull_types(it, javaClassId)
411+
})
412+
TypeResult(kotlinId, kotlinSignature)
413+
}
414+
return TypeResults(javaResult, kotlinResult)
415+
}
416+
392417
fun useSimpleType(s: IrSimpleType, canReturnPrimitiveTypes: Boolean): TypeResults {
393418
// We use this when we don't actually have an IrClass for a class
394419
// we want to refer to
@@ -514,28 +539,7 @@ class X {
514539
val classifier: IrClassifierSymbol = s.classifier
515540
val cls: IrClass = classifier.owner as IrClass
516541

517-
val classInstanceResult = useClassInstance(cls, s.arguments)
518-
val javaClassId = classInstanceResult.classLabel
519-
val kotlinQualClassName = getUnquotedClassLabel(cls, s.arguments)
520-
val javaQualClassName = classInstanceResult.javaClass.fqNameForIrSerialization.asString()
521-
val javaSignature = javaQualClassName // TODO: Is this right?
522-
val javaResult = TypeResult(javaClassId, javaSignature)
523-
val kotlinResult = if (s.hasQuestionMark) {
524-
val kotlinSignature = "$kotlinQualClassName?" // TODO: Is this right?
525-
val kotlinLabel = "@\"kt_type;nullable;$kotlinQualClassName\""
526-
val kotlinId: Label<DbKt_nullable_type> = tw.getLabelFor(kotlinLabel, {
527-
tw.writeKt_nullable_types(it, javaClassId)
528-
})
529-
TypeResult(kotlinId, kotlinSignature)
530-
} else {
531-
val kotlinSignature = kotlinQualClassName // TODO: Is this right?
532-
val kotlinLabel = "@\"kt_type;notnull;$kotlinQualClassName\""
533-
val kotlinId: Label<DbKt_notnull_type> = tw.getLabelFor(kotlinLabel, {
534-
tw.writeKt_notnull_types(it, javaClassId)
535-
})
536-
TypeResult(kotlinId, kotlinSignature)
537-
}
538-
return TypeResults(javaResult, kotlinResult)
542+
return useSimpleTypeClass(cls, s.arguments, s.hasQuestionMark)
539543
}
540544
s.classifier.owner is IrTypeParameter -> {
541545
val javaId = useTypeParameter(s.classifier.owner as IrTypeParameter)
@@ -1006,17 +1010,25 @@ open class KotlinFileExtractor(
10061010
} else {
10071011
val id = useProperty(p)
10081012
val locId = tw.getLocation(p)
1009-
val typeId = useTypeOld(bf.type)
1010-
tw.writeFields(id, p.name.asString(), typeId, parentId, id)
1013+
val type = useType(bf.type)
1014+
tw.writeFields(id, p.name.asString(), type.javaResult.id, type.kotlinResult.id, parentId, id)
10111015
tw.writeHasLocation(id, locId)
10121016
}
10131017
}
10141018

10151019
fun extractEnumEntry(ee: IrEnumEntry, parentId: Label<out DbReftype>) {
10161020
val id = useEnumEntry(ee)
10171021
val locId = tw.getLocation(ee)
1018-
tw.writeFields(id, ee.name.asString(), parentId, parentId, id)
1019-
tw.writeHasLocation(id, locId)
1022+
val parent = ee.parent
1023+
if(parent !is IrClass) {
1024+
logger.warnElement(Severity.ErrorSevere, "Enum entry with unexpected parent: " + parent.javaClass, ee)
1025+
} else if (!parent.typeParameters.isEmpty()) {
1026+
logger.warnElement(Severity.ErrorSevere, "Enum entry parent class has type parameters: " + parent.name, ee)
1027+
} else {
1028+
val type = useSimpleTypeClass(parent, emptyList(), false)
1029+
tw.writeFields(id, ee.name.asString(), type.javaResult.id, type.kotlinResult.id, parentId, id)
1030+
tw.writeHasLocation(id, locId)
1031+
}
10201032
}
10211033

10221034
fun extractBody(b: IrBody, callable: Label<out DbCallable>) {

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ fields(
353353
unique int id: @field,
354354
string nodeName: string ref,
355355
int typeid: @type ref,
356+
int kttypeid: @kt_type ref,
356357
int parentid: @reftype ref,
357358
int sourceid: @field ref
358359
);

java/ql/lib/semmle/code/Location.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ predicate hasName(Element e, string name) {
2020
or
2121
methods(e, name, _, _, _, _)
2222
or
23-
fields(e, name, _, _, _)
23+
fields(e, name, _, _, _, _)
2424
or
2525
packages(e, name)
2626
or

java/ql/lib/semmle/code/java/Element.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private predicate hasChildElement(Element parent, Element e) {
6363
or
6464
params(e, _, _, parent, _)
6565
or
66-
fields(e, _, _, parent, _)
66+
fields(e, _, _, _, parent, _)
6767
or
6868
typeVars(e, _, _, _, parent)
6969
}

java/ql/lib/semmle/code/java/Member.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,13 @@ class FieldDeclaration extends ExprParent, @fielddecl, Annotatable {
598598
/** A class or instance field. */
599599
class Field extends Member, ExprParent, @field, Variable {
600600
/** Gets the declared type of this field. */
601-
override Type getType() { fields(this, _, result, _, _) }
601+
override Type getType() { fields(this, _, result, _, _, _) }
602+
603+
/** Gets the Kotlin type of this field. */
604+
KotlinType getKotlinType() { fields(this, _, _, result, _, _) }
602605

603606
/** Gets the type in which this field is declared. */
604-
override RefType getDeclaringType() { fields(this, _, _, result, _) }
607+
override RefType getDeclaringType() { fields(this, _, _, _, result, _) }
605608

606609
/**
607610
* Gets the field declaration in which this field is declared.
@@ -631,7 +634,7 @@ class Field extends Member, ExprParent, @field, Variable {
631634
*
632635
* For all other fields, the source declaration is the field itself.
633636
*/
634-
Field getSourceDeclaration() { fields(this, _, _, _, result) }
637+
Field getSourceDeclaration() { fields(this, _, _, _, _, result) }
635638

636639
/** Holds if this field is the same as its source declaration. */
637640
predicate isSourceDeclaration() { this.getSourceDeclaration() = this }

java/ql/lib/semmle/code/java/Type.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ predicate declaresMember(Type t, @member m) {
319319
or
320320
constrs(m, _, _, _, t, _)
321321
or
322-
fields(m, _, _, t, _)
322+
fields(m, _, _, _, t, _)
323323
or
324324
enclInReftype(m, t) and
325325
// Since the type `@member` in the dbscheme includes all `@reftype`s,
@@ -1109,11 +1109,11 @@ class EnumType extends Class {
11091109

11101110
/** Gets the enum constant with the specified name. */
11111111
EnumConstant getEnumConstant(string name) {
1112-
fields(result, _, _, this, _) and result.hasName(name)
1112+
fields(result, _, _, _, this, _) and result.hasName(name)
11131113
}
11141114

11151115
/** Gets an enum constant declared in this enum type. */
1116-
EnumConstant getAnEnumConstant() { fields(result, _, _, this, _) }
1116+
EnumConstant getAnEnumConstant() { fields(result, _, _, _, this, _) }
11171117

11181118
override predicate isFinal() {
11191119
// JLS 8.9: An enum declaration is implicitly `final` unless it contains

0 commit comments

Comments
 (0)