Adapt instanceof CFG and DFG to general patterns

This commit is contained in:
Chris Smowton
2023-11-02 13:45:58 +00:00
parent 20b97af02f
commit f037030c26
3 changed files with 16 additions and 11 deletions

View File

@@ -1559,20 +1559,26 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr {
class InstanceOfExpr extends Expr, @instanceofexpr {
/** Gets the expression on the left-hand side of the `instanceof` operator. */
Expr getExpr() {
if this.isPattern()
then result = this.getLocalVariableDeclExpr().getInit()
else result.isNthChildOf(this, 0)
result.isNthChildOf(this, 0)
}
/**
* Gets the pattern of an `x instanceof T pattern` expression, if any.
*/
Pattern getPattern() { result.isNthChildOf(this, 2) }
/**
* Holds if this `instanceof` expression uses pattern matching.
*/
predicate isPattern() { exists(this.getLocalVariableDeclExpr()) }
predicate isPattern() { exists(this.getPattern()) }
/**
* Gets the local variable declaration of this `instanceof` expression if pattern matching is used.
* Gets the local variable declaration of this `instanceof` expression if simple pattern matching is used.
*
* Note that this won't get anything when record pattern matching is used-- for more general patterns,
* use `getPattern`.
*/
LocalVariableDeclExpr getLocalVariableDeclExpr() { result.isNthChildOf(this, 0) }
LocalVariableDeclExpr getLocalVariableDeclExpr() { result = this.getPattern().asBindingPattern() }
/** Gets the access to the type on the right-hand side of the `instanceof` operator. */
Expr getTypeName() { result.isNthChildOf(this, 1) }