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

Skip to content

Commit ee00877

Browse files
committed
Kotlin: Pull Kotlin type for fields out into its own table
1 parent 0f7f90d commit ee00877

8 files changed

Lines changed: 26 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ open class KotlinFileExtractor(
346346
// here.
347347
val instance = useObjectClassInstance(c)
348348
val type = useSimpleTypeClass(c, emptyList(), false)
349-
tw.writeFields(instance.id, instance.name, type.javaResult.id, type.kotlinResult.id, id, instance.id)
349+
tw.writeFields(instance.id, instance.name, type.javaResult.id, id, instance.id)
350+
tw.writeFieldsKotlinType(instance.id, type.kotlinResult.id)
350351
tw.writeHasLocation(instance.id, locId)
351352
addModifiers(instance.id, "public", "static", "final")
352353
@Suppress("UNCHECKED_CAST")
@@ -382,7 +383,8 @@ open class KotlinFileExtractor(
382383
val instance = useCompanionObjectClassInstance(innerClass)
383384
if(instance != null) {
384385
val type = useSimpleTypeClass(innerClass, emptyList(), false)
385-
tw.writeFields(instance.id, instance.name, type.javaResult.id, type.kotlinResult.id, innerId, instance.id)
386+
tw.writeFields(instance.id, instance.name, type.javaResult.id, innerId, instance.id)
387+
tw.writeFieldsKotlinType(instance.id, type.kotlinResult.id)
386388
tw.writeHasLocation(instance.id, innerLocId)
387389
addModifiers(instance.id, "public", "static", "final")
388390
@Suppress("UNCHECKED_CAST")
@@ -614,7 +616,8 @@ open class KotlinFileExtractor(
614616

615617
private fun extractField(id: Label<out DbField>, name: String, type: IrType, parentId: Label<out DbReftype>, locId: Label<DbLocation>, visibility: DescriptorVisibility, errorElement: IrElement, isExternalDeclaration: Boolean): Label<out DbField> {
616618
val t = useType(type)
617-
tw.writeFields(id, name, t.javaResult.id, t.kotlinResult.id, parentId, id)
619+
tw.writeFields(id, name, t.javaResult.id, parentId, id)
620+
tw.writeFieldsKotlinType(id, t.kotlinResult.id)
618621
tw.writeHasLocation(id, locId)
619622

620623
extractVisibility(errorElement, id, visibility)
@@ -694,7 +697,8 @@ open class KotlinFileExtractor(
694697
logger.warnElement(Severity.ErrorSevere, "Enum entry parent class has type parameters: " + parent.name, ee)
695698
} else {
696699
val type = useSimpleTypeClass(parent, emptyList(), false)
697-
tw.writeFields(id, ee.name.asString(), type.javaResult.id, type.kotlinResult.id, parentId, id)
700+
tw.writeFields(id, ee.name.asString(), type.javaResult.id, parentId, id)
701+
tw.writeFieldsKotlinType(id, type.kotlinResult.id)
698702
val locId = tw.getLocation(ee)
699703
tw.writeHasLocation(id, locId)
700704
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ open class KotlinUsesExtractor(
392392
// array.length
393393
val length = tw.getLabelFor<DbField>("@\"field;{$it};length\"")
394394
val intTypeIds = useType(pluginContext.irBuiltIns.intType)
395-
tw.writeFields(length, "length", intTypeIds.javaResult.id, intTypeIds.kotlinResult.id, it, length)
395+
tw.writeFields(length, "length", intTypeIds.javaResult.id, it, length)
396+
tw.writeFieldsKotlinType(length, intTypeIds.kotlinResult.id)
396397
addModifiers(length, "public", "final")
397398

398399
// Note we will only emit one `clone()` method per Java array type, so we choose `Array<C?>` as its Kotlin

java/ql/consistency-queries/kotlinTypes.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import java
22

33
predicate badKotlinType(Element e, int i) {
4-
e = any(Expr expr | count(expr.getKotlinType()) = i)
4+
e = any(Expr expr | count(expr.getKotlinType()) = i) or
5+
e = any(Field f | count(f.getKotlinType()) = i)
56
}
67

78
from Element e, int i

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,15 @@ fields(
399399
unique int id: @field,
400400
string nodeName: string ref,
401401
int typeid: @type ref,
402-
int kttypeid: @kt_type ref,
403402
int parentid: @reftype ref,
404403
int sourceid: @field ref
405404
);
406405

406+
fieldsKotlinType(
407+
unique int id: @field ref,
408+
int kttypeid: @kt_type ref
409+
)
410+
407411
constrs(
408412
unique int id: @constructor,
409413
string nodeName: string ref,

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,13 @@ class FieldDeclaration extends ExprParent, @fielddecl, Annotatable {
613613
/** A class or instance field. */
614614
class Field extends Member, ExprParent, @field, Variable {
615615
/** Gets the declared type of this field. */
616-
override Type getType() { fields(this, _, result, _, _, _) }
616+
override Type getType() { fields(this, _, result, _, _) }
617617

618618
/** Gets the Kotlin type of this field. */
619-
override KotlinType getKotlinType() { fields(this, _, _, result, _, _) }
619+
override KotlinType getKotlinType() { fieldsKotlinType(this, result) }
620620

621621
/** Gets the type in which this field is declared. */
622-
override RefType getDeclaringType() { fields(this, _, _, _, result, _) }
622+
override RefType getDeclaringType() { fields(this, _, _, result, _) }
623623

624624
/**
625625
* Gets the field declaration in which this field is declared.
@@ -649,7 +649,7 @@ class Field extends Member, ExprParent, @field, Variable {
649649
*
650650
* For all other fields, the source declaration is the field itself.
651651
*/
652-
Field getSourceDeclaration() { fields(this, _, _, _, _, result) }
652+
Field getSourceDeclaration() { fields(this, _, _, _, result) }
653653

654654
/** Holds if this field is the same as its source declaration. */
655655
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,
@@ -1136,11 +1136,11 @@ class EnumType extends Class {
11361136

11371137
/** Gets the enum constant with the specified name. */
11381138
EnumConstant getEnumConstant(string name) {
1139-
fields(result, _, _, _, this, _) and result.hasName(name)
1139+
fields(result, _, _, this, _) and result.hasName(name)
11401140
}
11411141

11421142
/** Gets an enum constant declared in this enum type. */
1143-
EnumConstant getAnEnumConstant() { fields(result, _, _, _, this, _) }
1143+
EnumConstant getAnEnumConstant() { fields(result, _, _, this, _) }
11441144

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

0 commit comments

Comments
 (0)