Kotlin: When expressions

This commit is contained in:
Ian Lynagh
2021-08-16 14:05:30 +01:00
parent bbb9d013e0
commit a64fedf764
9 changed files with 119 additions and 34 deletions

View File

@@ -633,8 +633,22 @@ case @expr.kind of
| 72 = @intersectiontypeaccess
| 73 = @switchexpr
| 74 = @errorexpr
| 75 = @whenexpr
;
/** Holds if this `when` expression was written as an `if` expression. */
when_if(unique int id: @whenexpr ref);
#keyset[whenid,idx]
when_branch(
int id: @whenbranch,
int whenid: @whenexpr ref,
int idx: int ref
);
/** Holds if this `when` branch was written as an `else` branch. */
when_branch_else(unique int id: @whenbranch ref);
@classinstancexpr = @newexpr | @lambdaexpr | @memberref
@annotation = @declannotation | @typeannotation
@@ -713,7 +727,7 @@ memberRefBinding(
int callable: @callable ref
);
@exprparent = @stmt | @expr | @callable | @field | @fielddecl | @class | @interface | @param | @localvar | @typevariable;
@exprparent = @stmt | @expr | @whenbranch | @callable | @field | @fielddecl | @class | @interface | @param | @localvar | @typevariable;
variableBinding(
unique int expr: @varaccess ref,
@@ -881,7 +895,7 @@ javadocText(
@locatable = @file | @class | @interface | @fielddecl | @field | @constructor | @method | @param | @exception
| @boundedtype | @typebound | @array | @primitive
| @import | @stmt | @expr | @localvar | @javadoc | @javadocTag | @javadocText
| @import | @stmt | @expr | @whenbranch | @localvar | @javadoc | @javadocTag | @javadocText
| @xmllocatable;
@top = @element | @locatable | @folder;

View File

@@ -2133,3 +2133,33 @@ class Argument extends Expr {
)
}
}
/** A Kotlin `when` expression. */
class WhenExpr extends Expr, @whenexpr {
override string toString() { result = "when ..." }
override string getHalsteadID() { result = "WhenExpr" }
override string getAPrimaryQlClass() { result = "WhenExpr" }
/** Gets the `i`th branch. */
WhenBranch getBranch(int i) {
when_branch(result, this, i)
}
}
/** A Kotlin `when` branch. */
class WhenBranch extends Top, @whenbranch {
/** Gets the condition of this branch. */
Expr getCondition() { result.isNthChildOf(this, 0) }
/** Gets the result of this branch. */
Top getResult() {
result.(Expr).isNthChildOf(this, 1) or
result.(Stmt).isNthChildOf(this, 1)
}
override string toString() { result = "... -> ..." }
override string getAPrimaryQlClass() { result = "WhenBranch" }
}