|
| 1 | +private import codeql_ql.ast.Ast |
| 2 | +private import codeql_ql.ast.internal.AstNodeNumbering |
| 3 | + |
| 4 | +/** Gets the disjunction immediately containing another disjunction `inner`. */ |
| 5 | +private Disjunction getOuterDisjunction(Disjunction inner) { result.getAnOperand() = inner } |
| 6 | + |
| 7 | +/** |
| 8 | + * Get the root of a disjunction tree containing `f`, if any. |
| 9 | + */ |
| 10 | +private Disjunction getRootDisjunction(Disjunction f) { |
| 11 | + not exists(getOuterDisjunction(result)) and |
| 12 | + result = getOuterDisjunction(f) |
| 13 | + or |
| 14 | + result = getRootDisjunction(getOuterDisjunction(f)) |
| 15 | +} |
| 16 | + |
| 17 | +/** Get the root disjunction for `f` if there is one, other gets `f` itself. */ |
| 18 | +pragma[inline] |
| 19 | +private AstNode tryGetRootDisjunction(AstNode f) { |
| 20 | + result = getRootDisjunction(f) |
| 21 | + or |
| 22 | + not exists(getRootDisjunction(f)) and |
| 23 | + result = f |
| 24 | +} |
| 25 | + |
| 26 | +AstNode getADisjunctionOperand(AstNode disjunction) { |
| 27 | + exists(Disjunction d | |
| 28 | + result = d.getAnOperand() and |
| 29 | + // skip intermediate nodes in large disjunctions |
| 30 | + disjunction = tryGetRootDisjunction(d) and |
| 31 | + not result instanceof Disjunction |
| 32 | + ) |
| 33 | + or |
| 34 | + result = disjunction.(Implication).getAChild() |
| 35 | + or |
| 36 | + result = disjunction.(IfFormula).getThenPart() |
| 37 | + or |
| 38 | + result = disjunction.(IfFormula).getElsePart() |
| 39 | + or |
| 40 | + exists(Forall all | |
| 41 | + disjunction = all and |
| 42 | + exists(all.getFormula()) and |
| 43 | + exists(all.getRange()) and |
| 44 | + result = [all.getRange(), all.getFormula()] |
| 45 | + ) |
| 46 | + or |
| 47 | + result = disjunction.(Set).getAnElement() |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * A node that acts as a disjunction: |
| 52 | + * - The root in a tree of `or` operators, or |
| 53 | + * - An `implies`, `if`, `forall`, or set literal. |
| 54 | + */ |
| 55 | +class DisjunctionOperator extends AstNode { |
| 56 | + DisjunctionOperator() { exists(getADisjunctionOperand(this)) } |
| 57 | + |
| 58 | + AstNode getAnOperand() { result = getADisjunctionOperand(this) } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Gets the scope of `var`, such as the predicate or `exists` clause that binds it. |
| 63 | + */ |
| 64 | +AstNode getVarDefScope(VarDef var) { |
| 65 | + // TODO: not valid for `as` expressions |
| 66 | + result = var.getParent() |
| 67 | +} |
| 68 | + |
| 69 | +/** A `VarAccess` or disjunct, representing the input to refinement of a variable. */ |
| 70 | +class VarAccessOrDisjunct = AstNode; |
| 71 | + |
| 72 | +/** |
| 73 | + * Walks upwards from an access to `varDef` until encountering either the scope of `varDef` |
| 74 | + * or a disjunct. When a disjunct is found, the disjunct becomes the new `access`, representing |
| 75 | + * a refinement we intend to insert there. |
| 76 | + */ |
| 77 | +private AstNode getVarScope(VarDef varDef, VarAccessOrDisjunct access) { |
| 78 | + access.(VarAccess).getDeclaration() = varDef and |
| 79 | + result = access |
| 80 | + or |
| 81 | + exists(AstNode scope | scope = getVarScope(varDef, access) | |
| 82 | + not scope = getADisjunctionOperand(_) and |
| 83 | + not scope = getVarDefScope(varDef) and |
| 84 | + result = scope.getParent() |
| 85 | + ) |
| 86 | + or |
| 87 | + isRefinement(varDef, _, access) and |
| 88 | + result = tryGetRootDisjunction(access.getParent()) |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Holds if `inner` should be seen as a refinement of `outer`. |
| 93 | + * |
| 94 | + * `outer` is always a disjunct, and `inner` is either a `VarAccess` or another disjunct. |
| 95 | + */ |
| 96 | +predicate isRefinement(VarDef varDef, VarAccessOrDisjunct inner, VarAccessOrDisjunct outer) { |
| 97 | + getVarScope(varDef, inner) = outer and |
| 98 | + ( |
| 99 | + outer = getADisjunctionOperand(_) |
| 100 | + or |
| 101 | + outer = getVarDefScope(varDef) |
| 102 | + ) |
| 103 | +} |
0 commit comments