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

Skip to content

Commit 0d5e471

Browse files
committed
Kotlin: Give methods and constructors a KotlinType
1 parent d982226 commit 0d5e471

8 files changed

Lines changed: 44 additions & 30 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,8 @@ open class KotlinFileExtractor(
911911
var obinitLabel = getFunctionLabel(c, "<obinit>", listOf(), pluginContext.irBuiltIns.unitType)
912912
val obinitId = tw.getLabelFor<DbMethod>(obinitLabel)
913913
val signature = "TODO"
914-
val returnTypeId = useTypeOld(pluginContext.irBuiltIns.unitType)
915-
tw.writeMethods(obinitId, "<obinit>", signature, returnTypeId, parentId, obinitId)
914+
val returnType = useType(pluginContext.irBuiltIns.unitType)
915+
tw.writeMethods(obinitId, "<obinit>", signature, returnType.javaResult.id, returnType.kotlinResult.id, parentId, obinitId)
916916

917917
val locId = tw.getLocation(c)
918918
tw.writeHasLocation(obinitId, locId)
@@ -976,13 +976,13 @@ open class KotlinFileExtractor(
976976

977977
val id: Label<out DbCallable>
978978
if (f.symbol is IrConstructorSymbol) {
979-
val returnTypeId = useTypeOld(erase(f.returnType))
979+
val returnType = useType(erase(f.returnType))
980980
id = useFunction<DbConstructor>(f)
981-
tw.writeConstrs(id, f.returnType.classFqName?.shortName()?.asString() ?: f.name.asString(), signature, returnTypeId, parentId, id)
981+
tw.writeConstrs(id, f.returnType.classFqName?.shortName()?.asString() ?: f.name.asString(), signature, returnType.javaResult.id, returnType.kotlinResult.id, parentId, id)
982982
} else {
983-
val returnTypeId = useTypeOld(f.returnType)
983+
val returnType = useType(f.returnType)
984984
id = useFunction<DbMethod>(f)
985-
tw.writeMethods(id, f.name.asString(), signature, returnTypeId, parentId, id)
985+
tw.writeMethods(id, f.name.asString(), signature, returnType.javaResult.id, returnType.kotlinResult.id, parentId, id)
986986

987987
val extReceiver = f.extensionReceiverParameter
988988
if (extReceiver != null) {

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ constrs(
363363
string nodeName: string ref,
364364
string signature: string ref,
365365
int typeid: @type ref,
366+
int kttypeid: @kt_type ref,
366367
int parentid: @reftype ref,
367368
int sourceid: @constructor ref
368369
);
@@ -372,6 +373,7 @@ methods(
372373
string nodeName: string ref,
373374
string signature: string ref,
374375
int typeid: @type ref,
376+
int kttypeid: @kt_type ref,
375377
int parentid: @reftype ref,
376378
int sourceid: @method ref
377379
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ predicate hasName(Element e, string name) {
1616
or
1717
primitives(e, name)
1818
or
19-
constrs(e, name, _, _, _, _)
19+
constrs(e, name, _, _, _, _, _)
2020
or
21-
methods(e, name, _, _, _, _)
21+
methods(e, name, _, _, _, _, _)
2222
or
2323
fields(e, name, _, _, _, _)
2424
or

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ class AnnotationType extends Interface {
138138

139139
/** Gets the annotation element with the specified `name`. */
140140
AnnotationElement getAnnotationElement(string name) {
141-
methods(result, _, _, _, this, _) and result.hasName(name)
141+
methods(result, _, _, _, _, this, _) and result.hasName(name)
142142
}
143143

144144
/** Gets an annotation element that is a member of this annotation type. */
145-
AnnotationElement getAnAnnotationElement() { methods(result, _, _, _, this, _) }
145+
AnnotationElement getAnAnnotationElement() { methods(result, _, _, _, _, this, _) }
146146

147147
/** Holds if this annotation type is annotated with the meta-annotation `@Inherited`. */
148148
predicate isInherited() {
@@ -158,5 +158,8 @@ class AnnotationElement extends Member {
158158
AnnotationElement() { isAnnotElem(this) }
159159

160160
/** Gets the type of this annotation element. */
161-
Type getType() { methods(this, _, _, result, _, _) }
161+
Type getType() { methods(this, _, _, result, _, _, _) }
162+
163+
/** Gets the Kotlin type of this annotation element. */
164+
KotlinType getKotlinType() { methods(this, _, _, _, result, _, _) }
162165
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ private predicate hasChildElement(Element parent, Element e) {
5757
not enclInReftype(e, _) and
5858
e.(Interface).getCompilationUnit() = parent
5959
or
60-
methods(e, _, _, _, parent, _)
60+
methods(e, _, _, _, _, parent, _)
6161
or
62-
constrs(e, _, _, _, parent, _)
62+
constrs(e, _, _, _, _, parent, _)
6363
or
6464
params(e, _, _, parent, _)
6565
or

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class RawInterface extends Interface, RawType {
439439
class GenericCallable extends Callable {
440440
GenericCallable() {
441441
exists(Callable srcDecl |
442-
methods(this, _, _, _, _, srcDecl) or constrs(this, _, _, _, _, srcDecl)
442+
methods(this, _, _, _, _, _, srcDecl) or constrs(this, _, _, _, _, _, srcDecl)
443443
|
444444
typeVars(_, _, _, _, srcDecl)
445445
)

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,17 @@ class Callable extends StmtParent, Member, @callable {
5757
* constructors).
5858
*/
5959
Type getReturnType() {
60-
constrs(this, _, _, result, _, _) or
61-
methods(this, _, _, result, _, _)
60+
constrs(this, _, _, result, _, _, _) or
61+
methods(this, _, _, result, _, _, _)
62+
}
63+
64+
/**
65+
* Gets the declared return Kotlin type of this callable (`Nothing` for
66+
* constructors).
67+
*/
68+
KotlinType getReturnKotlinType() {
69+
constrs(this, _, _, _, result, _, _) or
70+
methods(this, _, _, _, result, _, _)
6271
}
6372

6473
/**
@@ -273,8 +282,8 @@ class Callable extends StmtParent, Member, @callable {
273282
* For example, method `void m(String s, int i)` has the signature `m(java.lang.String,int)`.
274283
*/
275284
string getSignature() {
276-
constrs(this, _, result, _, _, _) or
277-
methods(this, _, result, _, _, _)
285+
constrs(this, _, result, _, _, _, _) or
286+
methods(this, _, result, _, _, _, _)
278287
}
279288
}
280289

@@ -316,7 +325,7 @@ predicate overridesIgnoringAccess(Method m1, RefType t1, Method m2, RefType t2)
316325
}
317326

318327
private predicate virtualMethodWithSignature(string sig, RefType t, Method m) {
319-
methods(m, _, _, _, t, _) and
328+
methods(m, _, _, _, _, t, _) and
320329
sig = m.getSignature() and
321330
m.isVirtual()
322331
}
@@ -365,7 +374,7 @@ class Method extends Callable, @method {
365374
exists(Method m | this.overrides(m) and result = m.getSourceDeclaration())
366375
}
367376

368-
override string getSignature() { methods(this, _, result, _, _, _) }
377+
override string getSignature() { methods(this, _, result, _, _, _, _) }
369378

370379
/**
371380
* Holds if this method and method `m` are declared in the same type
@@ -382,7 +391,7 @@ class Method extends Callable, @method {
382391
not exists(int n | this.getParameterType(n) != m.getParameterType(n))
383392
}
384393

385-
override SrcMethod getSourceDeclaration() { methods(this, _, _, _, _, result) }
394+
override SrcMethod getSourceDeclaration() { methods(this, _, _, _, _, _, result) }
386395

387396
/**
388397
* All the methods that could possibly be called when this method
@@ -456,7 +465,7 @@ class Method extends Callable, @method {
456465

457466
/** A method that is the same as its source declaration. */
458467
class SrcMethod extends Method {
459-
SrcMethod() { methods(_, _, _, _, _, this) }
468+
SrcMethod() { methods(_, _, _, _, _, _, this) }
460469

461470
/**
462471
* All the methods that could possibly be called when this method
@@ -542,9 +551,9 @@ class Constructor extends Callable, @constructor {
542551
/** Holds if this is a default constructor, not explicitly declared in source code. */
543552
predicate isDefaultConstructor() { isDefConstr(this) }
544553

545-
override Constructor getSourceDeclaration() { constrs(this, _, _, _, _, result) }
554+
override Constructor getSourceDeclaration() { constrs(this, _, _, _, _, _, result) }
546555

547-
override string getSignature() { constrs(this, _, result, _, _, _) }
556+
override string getSignature() { constrs(this, _, result, _, _, _, _) }
548557

549558
override string getAPrimaryQlClass() { result = "Constructor" }
550559
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ private predicate hasSubtypeStar2(RefType t, RefType sub) {
315315

316316
/** Holds if type `t` declares member `m`. */
317317
predicate declaresMember(Type t, @member m) {
318-
methods(m, _, _, _, t, _)
318+
methods(m, _, _, _, _, t, _)
319319
or
320-
constrs(m, _, _, _, t, _)
320+
constrs(m, _, _, _, _, t, _)
321321
or
322322
fields(m, _, _, _, t, _)
323323
or
@@ -511,16 +511,16 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
511511
sup.hasNonInterfaceMethod(m, declaringType, h2) and
512512
hidden = h1.booleanOr(h2) and
513513
exists(string signature |
514-
methods(m, _, signature, _, _, _) and not methods(_, _, signature, _, this, _)
514+
methods(m, _, signature, _, _, _, _) and not methods(_, _, signature, _, _, this, _)
515515
) and
516516
m.isInheritable()
517517
)
518518
}
519519

520520
private predicate cannotInheritInterfaceMethod(string signature) {
521-
methods(_, _, signature, _, this, _)
521+
methods(_, _, signature, _, _, this, _)
522522
or
523-
exists(Method m | this.hasNonInterfaceMethod(m, _, false) and methods(m, _, signature, _, _, _))
523+
exists(Method m | this.hasNonInterfaceMethod(m, _, false) and methods(m, _, signature, _, _, _, _))
524524
}
525525

526526
private predicate interfaceMethodCandidateWithSignature(
@@ -529,7 +529,7 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
529529
m = this.getAMethod() and
530530
this = declaringType and
531531
declaringType instanceof Interface and
532-
methods(m, _, signature, _, _, _)
532+
methods(m, _, signature, _, _, _, _)
533533
or
534534
exists(RefType sup |
535535
sup.interfaceMethodCandidateWithSignature(m, signature, declaringType) and

0 commit comments

Comments
 (0)