|
| 1 | +private import powershell |
| 2 | +private import semmle.code.powershell.controlflow.internal.Scope |
| 3 | +private import internal.Internal as Internal |
| 4 | + |
| 5 | +bindingset[scope] |
| 6 | +pragma[inline_late] |
| 7 | +private predicate isParameterImpl(string name, Scope scope) { |
| 8 | + exists(Internal::Parameter p | p.getName() = name and p.getEnclosingScope() = scope) |
| 9 | + or |
| 10 | + name = "_" |
| 11 | +} |
| 12 | + |
| 13 | +private newtype TParameterImpl = |
| 14 | + TInternalParameter(Internal::Parameter p) or |
| 15 | + TUnderscore(Scope scope) { |
| 16 | + exists(VarAccess va | va.getUserPath() = "_" and scope = va.getEnclosingScope()) |
| 17 | + } |
| 18 | + |
| 19 | +private class ParameterImpl extends TParameterImpl { |
| 20 | + abstract Location getLocation(); |
| 21 | + |
| 22 | + string toString() { result = this.getName() } |
| 23 | + |
| 24 | + abstract string getName(); |
| 25 | + |
| 26 | + abstract Scope getEnclosingScope(); |
| 27 | + |
| 28 | + predicate hasParameterBlock(ParamBlock block, int i) { none() } |
| 29 | + |
| 30 | + predicate isFunctionParameter(Function f, int i) { none() } |
| 31 | + |
| 32 | + Expr getDefaultValue() { none() } |
| 33 | + |
| 34 | + VarAccess getAnAccess() { |
| 35 | + // TODO: This won't join order nicely. |
| 36 | + result.getUserPath() = this.getName() and |
| 37 | + result.getEnclosingScope() = this.getEnclosingScope() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +private class InternalParameter extends ParameterImpl, TInternalParameter { |
| 42 | + Internal::Parameter p; |
| 43 | + |
| 44 | + InternalParameter() { this = TInternalParameter(p) } |
| 45 | + |
| 46 | + override Location getLocation() { result = p.getLocation() } |
| 47 | + |
| 48 | + override string getName() { result = p.getName() } |
| 49 | + |
| 50 | + final override Scope getEnclosingScope() { result = p.getEnclosingScope() } |
| 51 | + |
| 52 | + override predicate hasParameterBlock(ParamBlock block, int i) { |
| 53 | + param_block_parameter(block, i, p) |
| 54 | + } |
| 55 | + |
| 56 | + override predicate isFunctionParameter(Function f, int i) { |
| 57 | + function_definition_parameter(f, i, p) |
| 58 | + or |
| 59 | + function_member_parameter(f, i, p) |
| 60 | + } |
| 61 | + |
| 62 | + override Expr getDefaultValue() { result = p.getDefaultValue() } |
| 63 | +} |
| 64 | + |
| 65 | +private class Underscore extends ParameterImpl, TUnderscore { |
| 66 | + Scope scope; |
| 67 | + |
| 68 | + Underscore() { this = TUnderscore(scope) } |
| 69 | + |
| 70 | + override Location getLocation() { |
| 71 | + // The location is the first access (ordered by location) to the variable in the scope |
| 72 | + exists(VarAccess va | |
| 73 | + va = |
| 74 | + min(VarAccess cand, Location location | |
| 75 | + cand = this.getAnAccess() and location = cand.getLocation() |
| 76 | + | |
| 77 | + cand order by location.getStartLine(), location.getStartColumn() |
| 78 | + ) and |
| 79 | + result = va.getLocation() |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + override string getName() { result = "_" } |
| 84 | + |
| 85 | + final override Scope getEnclosingScope() { result = scope } |
| 86 | +} |
| 87 | + |
| 88 | +private newtype TVariable = |
| 89 | + TLocalVariable(string name, Scope scope) { |
| 90 | + not isParameterImpl(name, scope) and |
| 91 | + exists(VarAccess va | va.getUserPath() = name and scope = va.getEnclosingScope()) |
| 92 | + } or |
| 93 | + TParameter(ParameterImpl p) |
| 94 | + |
| 95 | +private class AbstractVariable extends TVariable { |
| 96 | + abstract Location getLocation(); |
| 97 | + |
| 98 | + string toString() { result = this.getName() } |
| 99 | + |
| 100 | + abstract string getName(); |
| 101 | + |
| 102 | + abstract Scope getDeclaringScope(); |
| 103 | + |
| 104 | + VarAccess getAnAccess() { |
| 105 | + exists(string s | |
| 106 | + s = concat(this.getAQlClass(), ", ") and |
| 107 | + // TODO: This won't join order nicely. |
| 108 | + result.getUserPath() = this.getName() and |
| 109 | + result.getEnclosingScope() = this.getDeclaringScope() |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class LocalVariable extends AbstractVariable, TLocalVariable { |
| 115 | + string name; |
| 116 | + Scope scope; |
| 117 | + |
| 118 | + LocalVariable() { this = TLocalVariable(name, scope) } |
| 119 | + |
| 120 | + override Location getLocation() { |
| 121 | + // The location is the first access (ordered by location) to the variable in the scope |
| 122 | + exists(VarAccess va | |
| 123 | + va = |
| 124 | + min(VarAccess cand, Location location | |
| 125 | + cand = this.getAnAccess() and location = cand.getLocation() |
| 126 | + | |
| 127 | + cand order by location.getStartLine(), location.getStartColumn() |
| 128 | + ) and |
| 129 | + result = va.getLocation() |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + override string getName() { result = name } |
| 134 | + |
| 135 | + final override Scope getDeclaringScope() { result = scope } |
| 136 | +} |
| 137 | + |
| 138 | +class Parameter extends AbstractVariable, TParameter { |
| 139 | + ParameterImpl p; |
| 140 | + |
| 141 | + Parameter() { this = TParameter(p) } |
| 142 | + |
| 143 | + override Location getLocation() { result = p.getLocation() } |
| 144 | + |
| 145 | + override string getName() { result = p.getName() } |
| 146 | + |
| 147 | + final override Scope getDeclaringScope() { result = p.getEnclosingScope() } |
| 148 | + |
| 149 | + predicate hasParameterBlock(ParamBlock block, int i) { p.hasParameterBlock(block, i) } |
| 150 | + |
| 151 | + predicate isFunctionParameter(Function f, int i) { p.isFunctionParameter(f, i) } |
| 152 | + |
| 153 | + Expr getDefaultValue() { result = p.getDefaultValue() } |
| 154 | + |
| 155 | + predicate hasDefaultValue() { exists(this.getDefaultValue()) } |
| 156 | + |
| 157 | + int getIndex() { this.isFunctionParameter(_, result) } |
| 158 | +} |
| 159 | + |
| 160 | +final class Variable = AbstractVariable; |
0 commit comments