Skip to content

Commit cee1527

Browse files
authored
Merge pull request #10144 from MathiasVP/swift-extract-this-param-decl
Swift: Extract `self` parameter declarations
2 parents c514c88 + 1d50dd5 commit cee1527

File tree

19 files changed

+386
-16
lines changed

19 files changed

+386
-16
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ AstNode:
8181
Callable:
8282
_children:
8383
params: ParamDecl*
84+
self_param: ParamDecl?
8485
body: BraceStmt?
8586

8687
ConditionElement:

swift/extractor/visitors/DeclVisitor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ void DeclVisitor::fillAbstractFunctionDecl(const swift::AbstractFunctionDecl& de
305305
entry.name = !decl.hasName() ? "(unnamed function decl)" : constructName(decl.getName());
306306
entry.body = dispatcher_.fetchOptionalLabel(decl.getBody());
307307
entry.params = dispatcher_.fetchRepeatedLabels(*decl.getParameters());
308+
auto self = const_cast<swift::ParamDecl* const>(decl.getImplicitSelfDecl());
309+
entry.self_param = dispatcher_.fetchOptionalLabel(self);
308310
fillValueDecl(decl, entry);
309311
fillGenericContext(decl, entry);
310312
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
| Sources/cross-references/lib.swift:4:5:4:5 | self |
2+
| Sources/cross-references/lib.swift:5:5:5:5 | self |
13
| Sources/cross-references/lib.swift:10:5:10:5 | X |
4+
| Sources/cross-references/lib.swift:14:8:14:8 | self |
25
| Sources/cross-references/lib.swift:17:16:17:19 | v |
36
| Sources/cross-references/lib.swift:22:16:22:19 | v |
47
| Sources/cross-references/lib.swift:27:8:27:13 | lhs |
58
| Sources/cross-references/lib.swift:27:18:27:23 | rhs |
9+
| Sources/cross-references/main.swift:11:8:11:8 | self |

swift/ql/lib/codeql/swift/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,9 @@ module Decls {
993993
AbstractFunctionDecl getAst() { result = ast }
994994

995995
final override ControlFlowElement getChildElement(int i) {
996+
i = -1 and
997+
result.asAstNode() = ast.getSelfParam()
998+
or
996999
result.asAstNode() = ast.getParam(i)
9971000
or
9981001
result.asAstNode() = ast.getBody() and

swift/ql/lib/codeql/swift/elements/decl/ParamDecl.qll

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ class ParamDecl extends ParamDeclBase {
55
/** Gets the function which declares this parameter. */
66
Callable getDeclaringFunction() { result.getAParam() = this }
77

8-
/** Gets the index of this parameter in its declaring function's parameter list. */
8+
/**
9+
* Gets the index of this parameter in its declaring function's parameter list,
10+
* or -1 if this is `self`.
11+
*/
912
int getIndex() { exists(Callable func | func.getParam(result) = this) }
1013
}
14+
15+
/** A `self` parameter. */
16+
class SelfParamDecl extends ParamDecl {
17+
Callable call;
18+
19+
SelfParamDecl() { call.getSelfParam() = this }
20+
21+
override Callable getDeclaringFunction() { result = call }
22+
23+
override int getIndex() { result = -1 }
24+
}

swift/ql/lib/codeql/swift/generated/Callable.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ class CallableBase extends Synth::TCallable, Element {
1919

2020
final int getNumberOfParams() { result = count(getAParam()) }
2121

22+
ParamDecl getImmediateSelfParam() {
23+
result =
24+
Synth::convertParamDeclFromRaw(Synth::convertCallableToRaw(this)
25+
.(Raw::Callable)
26+
.getSelfParam())
27+
}
28+
29+
final ParamDecl getSelfParam() { result = getImmediateSelfParam().resolve() }
30+
31+
final predicate hasSelfParam() { exists(getSelfParam()) }
32+
2233
BraceStmt getImmediateBody() {
2334
result =
2435
Synth::convertBraceStmtFromRaw(Synth::convertCallableToRaw(this).(Raw::Callable).getBody())

swift/ql/lib/codeql/swift/generated/GetImmediateParent.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Element getAnImmediateChild(Element e) {
1515
or
1616
result = e.(Callable).getImmediateParam(_)
1717
or
18+
result = e.(Callable).getImmediateSelfParam()
19+
or
1820
result = e.(Callable).getImmediateBody()
1921
or
2022
result = e.(AbstractStorageDecl).getImmediateAccessorDecl(_)

swift/ql/lib/codeql/swift/generated/Raw.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Raw {
88
class Callable extends @callable, Element {
99
ParamDecl getParam(int index) { callable_params(this, index, result) }
1010

11+
ParamDecl getSelfParam() { callable_self_params(this, result) }
12+
1113
BraceStmt getBody() { callable_bodies(this, result) }
1214
}
1315

swift/ql/lib/swift.dbscheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ callable_params(
173173
int param: @param_decl ref
174174
);
175175

176+
#keyset[id]
177+
callable_self_params(
178+
int id: @callable ref,
179+
int self_param: @param_decl ref
180+
);
181+
176182
#keyset[id]
177183
callable_bodies(
178184
int id: @callable ref,

swift/ql/test/extractor-tests/declarations/all.expected

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
| declarations.swift:1:1:7:1 | Foo |
22
| declarations.swift:1:8:1:8 | init |
33
| declarations.swift:1:8:1:8 | init |
4+
| declarations.swift:1:8:1:8 | self |
5+
| declarations.swift:1:8:1:8 | self |
46
| declarations.swift:1:8:1:8 | x |
57
| declarations.swift:2:3:2:11 | var ... = ... |
68
| declarations.swift:2:7:2:7 | (unnamed function decl) |
@@ -23,6 +25,8 @@
2325
| declarations.swift:9:1:9:34 | Bar |
2426
| declarations.swift:9:7:9:7 | deinit |
2527
| declarations.swift:9:7:9:7 | init |
28+
| declarations.swift:9:7:9:7 | self |
29+
| declarations.swift:9:7:9:7 | self |
2630
| declarations.swift:9:13:9:30 | var ... = ... |
2731
| declarations.swift:9:17:9:17 | (unnamed function decl) |
2832
| declarations.swift:9:17:9:17 | get |
@@ -56,13 +60,18 @@
5660
| declarations.swift:23:5:23:39 | var ... = ... |
5761
| declarations.swift:23:9:23:9 | (unnamed function decl) |
5862
| declarations.swift:23:9:23:9 | mustBeSettable |
63+
| declarations.swift:23:9:23:9 | self |
5964
| declarations.swift:23:31:23:31 | get |
65+
| declarations.swift:23:31:23:31 | self |
6066
| declarations.swift:23:35:23:35 | newValue |
67+
| declarations.swift:23:35:23:35 | self |
6168
| declarations.swift:23:35:23:35 | set |
6269
| declarations.swift:24:5:24:44 | var ... = ... |
6370
| declarations.swift:24:9:24:9 | doesNotNeedToBeSettable |
6471
| declarations.swift:24:40:24:40 | get |
72+
| declarations.swift:24:40:24:40 | self |
6573
| declarations.swift:25:5:25:22 | random() |
74+
| declarations.swift:25:10:25:10 | self |
6675
| declarations.swift:28:1:28:37 | a_function(a_parameter:) |
6776
| declarations.swift:28:17:28:31 | a_parameter |
6877
| declarations.swift:30:1:30:18 | var ... = ... |
@@ -90,15 +99,18 @@
9099
| declarations.swift:46:3:46:3 | self |
91100
| declarations.swift:46:3:48:3 | deinit |
92101
| declarations.swift:50:3:52:3 | +-(_:) |
102+
| declarations.swift:50:22:50:22 | self |
93103
| declarations.swift:50:26:50:33 | other |
94104
| declarations.swift:55:8:55:17 | +- |
95105
| declarations.swift:57:1:62:1 | precedencegroup ... |
96106
| declarations.swift:64:7:64:16 | +++ |
97107
| declarations.swift:66:7:66:21 | *** |
98108
| declarations.swift:68:18:74:1 | ZeroWrapper |
99109
| declarations.swift:68:25:68:25 | init |
110+
| declarations.swift:68:25:68:25 | self |
100111
| declarations.swift:69:3:73:3 | var ... = ... |
101112
| declarations.swift:69:7:69:7 | wrappedValue |
113+
| declarations.swift:70:5:70:5 | self |
102114
| declarations.swift:70:5:72:5 | get |
103115
| declarations.swift:76:1:79:1 | foo() |
104116
| declarations.swift:77:16:77:23 | var ... = ... |
@@ -113,18 +125,23 @@
113125
| declarations.swift:81:8:81:8 | hasWillSet2 |
114126
| declarations.swift:81:8:81:8 | init |
115127
| declarations.swift:81:8:81:8 | normalField |
128+
| declarations.swift:81:8:81:8 | self |
116129
| declarations.swift:82:3:87:3 | var ... = ... |
117130
| declarations.swift:82:7:82:7 | (unnamed function decl) |
118131
| declarations.swift:82:7:82:7 | self |
119132
| declarations.swift:82:7:82:7 | settableField |
120133
| declarations.swift:83:5:83:5 | newValue |
134+
| declarations.swift:83:5:83:5 | self |
121135
| declarations.swift:83:5:83:11 | set |
136+
| declarations.swift:84:5:84:5 | self |
122137
| declarations.swift:84:5:86:5 | get |
123138
| declarations.swift:91:3:93:3 | var ... = ... |
124139
| declarations.swift:91:7:91:7 | readOnlyField1 |
140+
| declarations.swift:91:27:91:27 | self |
125141
| declarations.swift:91:27:93:3 | get |
126142
| declarations.swift:96:3:100:3 | var ... = ... |
127143
| declarations.swift:96:7:96:7 | readOnlyField2 |
144+
| declarations.swift:97:5:97:5 | self |
128145
| declarations.swift:97:5:99:5 | get |
129146
| declarations.swift:102:3:102:21 | var ... = ... |
130147
| declarations.swift:102:7:102:7 | (unnamed function decl) |
@@ -141,14 +158,17 @@
141158
| declarations.swift:104:13:104:13 | x |
142159
| declarations.swift:104:13:104:13 | x |
143160
| declarations.swift:104:13:104:16 | x |
161+
| declarations.swift:105:5:105:5 | self |
144162
| declarations.swift:105:5:107:5 | get |
145163
| declarations.swift:108:5:108:5 | newValue |
164+
| declarations.swift:108:5:108:5 | self |
146165
| declarations.swift:108:5:108:11 | set |
147166
| declarations.swift:111:3:113:3 | subscript ... |
148167
| declarations.swift:111:13:111:13 | x |
149168
| declarations.swift:111:13:111:16 | x |
150169
| declarations.swift:111:21:111:21 | y |
151170
| declarations.swift:111:21:111:25 | y |
171+
| declarations.swift:111:37:111:37 | self |
152172
| declarations.swift:111:37:113:3 | get |
153173
| declarations.swift:115:3:117:3 | var ... = ... |
154174
| declarations.swift:115:7:115:7 | (unnamed function decl) |
@@ -159,6 +179,7 @@
159179
| declarations.swift:115:7:115:7 | self |
160180
| declarations.swift:115:7:115:7 | set |
161181
| declarations.swift:115:7:115:7 | value |
182+
| declarations.swift:116:5:116:5 | self |
162183
| declarations.swift:116:5:116:25 | willSet |
163184
| declarations.swift:116:13:116:13 | newValue |
164185
| declarations.swift:119:3:121:3 | var ... = ... |
@@ -171,6 +192,7 @@
171192
| declarations.swift:119:7:119:7 | set |
172193
| declarations.swift:119:7:119:7 | value |
173194
| declarations.swift:120:5:120:5 | newValue |
195+
| declarations.swift:120:5:120:5 | self |
174196
| declarations.swift:120:5:120:15 | willSet |
175197
| declarations.swift:123:3:125:3 | var ... = ... |
176198
| declarations.swift:123:7:123:7 | (unnamed function decl) |
@@ -181,6 +203,7 @@
181203
| declarations.swift:123:7:123:7 | self |
182204
| declarations.swift:123:7:123:7 | set |
183205
| declarations.swift:123:7:123:7 | value |
206+
| declarations.swift:124:5:124:5 | self |
184207
| declarations.swift:124:5:124:24 | didSet |
185208
| declarations.swift:124:12:124:12 | oldValue |
186209
| declarations.swift:127:3:129:3 | var ... = ... |
@@ -192,6 +215,7 @@
192215
| declarations.swift:127:7:127:7 | self |
193216
| declarations.swift:127:7:127:7 | set |
194217
| declarations.swift:127:7:127:7 | value |
218+
| declarations.swift:128:5:128:5 | self |
195219
| declarations.swift:128:5:128:14 | didSet |
196220
| declarations.swift:131:3:135:3 | var ... = ... |
197221
| declarations.swift:131:7:131:7 | (unnamed function decl) |
@@ -203,7 +227,9 @@
203227
| declarations.swift:131:7:131:7 | set |
204228
| declarations.swift:131:7:131:7 | value |
205229
| declarations.swift:132:5:132:5 | newValue |
230+
| declarations.swift:132:5:132:5 | self |
206231
| declarations.swift:132:5:132:15 | willSet |
232+
| declarations.swift:134:5:134:5 | self |
207233
| declarations.swift:134:5:134:14 | didSet |
208234
| declarations.swift:138:1:142:1 | extension |
209235
| declarations.swift:139:3:141:3 | id() |

0 commit comments

Comments
 (0)