|
1 | 1 | private import swift |
| 2 | +private import codeql.swift.generated.GetImmediateParent |
2 | 3 |
|
3 | 4 | module CallableBase { |
4 | 5 | class TypeRange = @abstract_function_decl; |
@@ -27,224 +28,13 @@ class Scope extends AstNode instanceof Scope::Range { |
27 | 28 |
|
28 | 29 | cached |
29 | 30 | private module Cached { |
30 | | - private AstNode getChild(AstNode ast) { |
31 | | - result = ast.(TopLevelCodeDecl).getBody() |
32 | | - or |
33 | | - result = ast.(AnyTryExpr).getSubExpr() |
34 | | - or |
35 | | - result = ast.(ClosureExpr).getBody() |
36 | | - or |
37 | | - result = ast.(ExplicitCastExpr).getSubExpr() |
38 | | - or |
39 | | - result = ast.(ForceValueExpr).getSubExpr() |
40 | | - or |
41 | | - result = ast.(IdentityExpr).getSubExpr() |
42 | | - or |
43 | | - result = ast.(ImplicitConversionExpr).getSubExpr() |
44 | | - or |
45 | | - result = ast.(InOutExpr).getSubExpr() |
46 | | - or |
47 | | - result = ast.(MemberRefExpr).getBaseExpr() |
48 | | - or |
49 | | - result = ast.(SelfApplyExpr).getBaseExpr() |
50 | | - or |
51 | | - result = ast.(VarargExpansionExpr).getSubExpr() |
52 | | - or |
53 | | - result = ast.(BindingPattern).getSubPattern() |
54 | | - or |
55 | | - result = ast.(EnumElementPattern).getSubPattern() |
56 | | - or |
57 | | - result = ast.(ExprPattern).getSubExpr() |
58 | | - or |
59 | | - result = ast.(IsPattern).getSubPattern() |
60 | | - or |
61 | | - result = ast.(OptionalSomePattern).getSubPattern() |
62 | | - or |
63 | | - result = ast.(ParenPattern).getSubPattern() |
64 | | - or |
65 | | - result = ast.(TypedPattern).getSubPattern() |
66 | | - or |
67 | | - result = ast.(DeferStmt).getBody() |
68 | | - or |
69 | | - result = ast.(DoStmt).getBody() |
70 | | - or |
71 | | - result = ast.(ReturnStmt).getResult() |
72 | | - or |
73 | | - result = ast.(ThrowStmt).getSubExpr() |
| 31 | + private AstNode getParentOfAstStep(Element e) { |
| 32 | + not e instanceof AstNode and |
| 33 | + result = getImmediateParent(e) |
74 | 34 | } |
75 | 35 |
|
76 | 36 | cached |
77 | | - AstNode getChild(AstNode ast, int index) { |
78 | | - exists(AbstractFunctionDecl afd | afd = ast | |
79 | | - index = -1 and |
80 | | - result = afd.getBody() |
81 | | - or |
82 | | - result = afd.getParam(index) |
83 | | - ) |
84 | | - or |
85 | | - result = ast.(EnumCaseDecl).getElement(index) |
86 | | - or |
87 | | - result = ast.(EnumElementDecl).getParam(index) |
88 | | - or |
89 | | - exists(PatternBindingDecl pbd, int i | pbd = ast | |
90 | | - index = 2 * i and |
91 | | - result = pbd.getPattern(i) |
92 | | - or |
93 | | - index = 2 * i + 1 and |
94 | | - result = pbd.getInit(i) |
95 | | - ) |
96 | | - or |
97 | | - exists(ApplyExpr apply | apply = ast | |
98 | | - index = -1 and |
99 | | - result = apply.getFunction() |
100 | | - or |
101 | | - result = apply.getArgument(index).getExpr() |
102 | | - ) |
103 | | - or |
104 | | - result = ast.(ArrayExpr).getElement(index) |
105 | | - or |
106 | | - // `x` is evaluated before `y` in `x = y`. |
107 | | - exists(AssignExpr assign | assign = ast | |
108 | | - index = 0 and |
109 | | - result = assign.getDest() |
110 | | - or |
111 | | - index = 1 and |
112 | | - result = assign.getSource() |
113 | | - ) |
114 | | - or |
115 | | - exists(BinaryExpr binary | binary = ast | |
116 | | - index = 0 and |
117 | | - result = binary.getLeftOperand() |
118 | | - or |
119 | | - index = 1 and |
120 | | - result = binary.getRightOperand() |
121 | | - ) |
122 | | - or |
123 | | - // TODO: There are two other getters. Should they be included? |
124 | | - exists(InterpolatedStringLiteralExpr interpolated | interpolated = ast | |
125 | | - index = 0 and |
126 | | - result = interpolated.getInterpolationExpr() |
127 | | - or |
128 | | - index = 1 and |
129 | | - result = interpolated.getAppendingExpr() |
130 | | - ) |
131 | | - or |
132 | | - exists(KeyPathExpr kp | kp = ast | |
133 | | - index = 0 and |
134 | | - result = kp.getParsedRoot() |
135 | | - or |
136 | | - index = 1 and |
137 | | - result = kp.getParsedPath() |
138 | | - ) |
139 | | - or |
140 | | - exists(SubscriptExpr sub | sub = ast | |
141 | | - index = -1 and |
142 | | - result = sub.getBaseExpr() |
143 | | - or |
144 | | - result = sub.getArgument(index).getExpr() |
145 | | - ) |
146 | | - or |
147 | | - exists(TapExpr tap | tap = ast | |
148 | | - index = 0 and |
149 | | - result = tap.getVar() |
150 | | - or |
151 | | - index = 1 and |
152 | | - result = tap.getSubExpr() |
153 | | - or |
154 | | - index = 2 and |
155 | | - result = tap.getBody() |
156 | | - ) |
157 | | - or |
158 | | - result = ast.(TupleExpr).getElement(index) |
159 | | - or |
160 | | - result = ast.(TuplePattern).getElement(index) |
161 | | - or |
162 | | - result = ast.(BraceStmt).getElement(index) |
163 | | - or |
164 | | - exists(CaseStmt case | case = ast | |
165 | | - index = -1 and |
166 | | - result = case.getBody() |
167 | | - or |
168 | | - result = case.getLabel(index) |
169 | | - or |
170 | | - index >= case.getNumberOfLabels() and |
171 | | - result = case.getVariable(case.getNumberOfLabels() - index) |
172 | | - ) |
173 | | - or |
174 | | - exists(DoCatchStmt catch | catch = ast | |
175 | | - index = -1 and |
176 | | - result = catch.getBody() |
177 | | - or |
178 | | - result = catch.getCatch(index) |
179 | | - ) |
180 | | - or |
181 | | - exists(ForEachStmt forEach | forEach = ast | |
182 | | - index = 0 and |
183 | | - result = forEach.getWhere() |
184 | | - or |
185 | | - index = 1 and |
186 | | - result = forEach.getBody() |
187 | | - ) |
188 | | - or |
189 | | - exists(GuardStmt guard | guard = ast | |
190 | | - index = 0 and |
191 | | - result = guard.getBody() |
192 | | - or |
193 | | - index = 1 and |
194 | | - result = guard.getCondition() |
195 | | - ) |
196 | | - or |
197 | | - exists(StmtCondition stmtCond | stmtCond = ast | |
198 | | - index = 0 and |
199 | | - result = stmtCond.getElement(index).getPattern() |
200 | | - or |
201 | | - index = 1 and |
202 | | - result = stmtCond.getElement(index).getInitializer() |
203 | | - or |
204 | | - index = 2 and |
205 | | - result = stmtCond.getElement(index).getBoolean() |
206 | | - ) |
207 | | - or |
208 | | - exists(IfStmt ifStmt | ifStmt = ast | |
209 | | - index = 0 and |
210 | | - result = ifStmt.getCondition().getFullyUnresolved() |
211 | | - or |
212 | | - index = 1 and |
213 | | - result = ifStmt.getThen() |
214 | | - or |
215 | | - index = 2 and |
216 | | - result = ifStmt.getElse() |
217 | | - ) |
218 | | - or |
219 | | - exists(RepeatWhileStmt repeat | repeat = ast | |
220 | | - index = 0 and |
221 | | - result = repeat.getCondition() |
222 | | - or |
223 | | - index = 1 and |
224 | | - result = repeat.getBody() |
225 | | - ) |
226 | | - or |
227 | | - exists(SwitchStmt switch | switch = ast | |
228 | | - index = -1 and |
229 | | - result = switch.getExpr() |
230 | | - or |
231 | | - result = switch.getCase(index) |
232 | | - ) |
233 | | - or |
234 | | - exists(WhileStmt while | while = ast | |
235 | | - index = 0 and |
236 | | - result = while.getCondition().getFullyUnresolved() |
237 | | - or |
238 | | - index = 1 and |
239 | | - result = while.getBody() |
240 | | - ) |
241 | | - or |
242 | | - index = 0 and |
243 | | - result = getChild(ast) |
244 | | - } |
245 | | - |
246 | | - cached |
247 | | - AstNode getParentOfAst(AstNode ast) { getChild(result, _) = ast } |
| 37 | + AstNode getParentOfAst(AstNode ast) { result = getParentOfAstStep*(getImmediateParent(ast)) } |
248 | 38 | } |
249 | 39 |
|
250 | 40 | /** Gets the enclosing scope of a node */ |
|
0 commit comments