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

Skip to content

Commit 87b4331

Browse files
committed
Kotlin: Add support for Kotlin type aliases
1 parent 8330a40 commit 87b4331

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,18 @@ class X {
769769
return id
770770
}
771771

772+
private fun getTypeAliasLabel(ta: IrTypeAlias) : String {
773+
val parentId = useDeclarationParent(ta.parent)
774+
val label = "@\"type_alias;{$parentId};${ta.name.asString()}\""
775+
return label
776+
}
777+
778+
fun useTypeAlias(ta: IrTypeAlias): Label<out DbKt_type_alias> {
779+
var label = getTypeAliasLabel(ta)
780+
val id: Label<DbKt_type_alias> = tw.getLabelFor(label)
781+
return id
782+
}
783+
772784
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
773785
return tw.getVariableLabelFor<DbLocalvar>(v)
774786
}
@@ -793,6 +805,7 @@ open class KotlinFileExtractor(
793805
}
794806
is IrProperty -> extractProperty(declaration, parentId)
795807
is IrEnumEntry -> extractEnumEntry(declaration, parentId)
808+
is IrTypeAlias -> extractTypeAlias(declaration) // TODO: Pass in and use parentId
796809
else -> logger.warnElement(Severity.ErrorSevere, "Unrecognised IrDeclaration: " + declaration.javaClass, declaration)
797810
}
798811
}
@@ -1031,6 +1044,19 @@ open class KotlinFileExtractor(
10311044
}
10321045
}
10331046

1047+
fun extractTypeAlias(ta: IrTypeAlias) {
1048+
if (ta.typeParameters.isNotEmpty()) {
1049+
// TODO: Extract this information
1050+
logger.warn(Severity.ErrorSevere, "Type alias type parameters ignored for " + ta.render())
1051+
}
1052+
val id = useTypeAlias(ta)
1053+
val locId = tw.getLocation(ta)
1054+
// TODO: We don't really want to generate any Java types here; we only want the KT type:
1055+
val type = useType(ta.expandedType)
1056+
tw.writeKt_type_alias(id, ta.name.asString(), type.kotlinResult.id)
1057+
tw.writeHasLocation(id, locId)
1058+
}
1059+
10341060
fun extractBody(b: IrBody, callable: Label<out DbCallable>) {
10351061
when(b) {
10361062
is IrBlockBody -> extractBlockBody(b, callable, callable, 0)

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ kt_notnull_types(
324324
int classid: @classorinterface ref
325325
)
326326

327+
kt_type_alias(
328+
unique int id: @kt_type_alias,
329+
string name: string ref,
330+
int kttypeid: @kt_type ref
331+
)
332+
327333
@kt_type = @kt_nullable_type | @kt_notnull_type
328334

329335
isRecord(
@@ -928,19 +934,22 @@ javadocText(
928934
@classorarray = @class | @array;
929935
@type = @primitive | @reftype;
930936
@callable = @method | @constructor;
937+
938+
/** A program element that has a name. */
931939
@element = @file | @package | @primitive | @class | @interface | @method | @constructor | @modifier | @param | @exception | @field |
932-
@annotation | @boundedtype | @array | @localvar | @expr | @stmt | @import | @fielddecl | @kt_type;
940+
@annotation | @boundedtype | @array | @localvar | @expr | @stmt | @import | @fielddecl | @kt_type | @kt_type_alias;
933941

934942
@modifiable = @member_modifiable| @param | @localvar ;
935943

936944
@member_modifiable = @class | @interface | @method | @constructor | @field ;
937945

938946
@member = @method | @constructor | @field | @reftype ;
939947

948+
/** A program element that has a location. */
940949
@locatable = @file | @class | @interface | @fielddecl | @field | @constructor | @method | @param | @exception
941950
| @boundedtype | @typebound | @array | @primitive
942951
| @import | @stmt | @expr | @whenbranch | @localvar | @javadoc | @javadocTag | @javadocText
943-
| @xmllocatable | @ktcomment;
952+
| @xmllocatable | @ktcomment | @kt_type_alias;
944953

945954
@top = @element | @locatable | @folder;
946955

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ predicate hasName(Element e, string name) {
4343
arrays(e, name, _, _, _, _, _)
4444
or
4545
modifiers(e, name)
46+
or
47+
kt_type_alias(e, name, _)
4648
}
4749

4850
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ class KotlinNotnullType extends KotlinType, @kt_notnull_type {
2525
override string getAPrimaryQlClass() { result = "KotlinNotnullType" }
2626
}
2727

28+
class KotlinTypeAlias extends Element, @kt_type_alias {
29+
override string getAPrimaryQlClass() { result = "KotlinTypeAlias" }
30+
31+
KotlinType getKotlinType() {
32+
kt_type_alias(this, _, result)
33+
}
34+
}

0 commit comments

Comments
 (0)