Skip to content

Commit 4b6b368

Browse files
committed
Unified: Introduce CallableEx and ParameterEx
1 parent 605dc1c commit 4b6b368

7 files changed

Lines changed: 542 additions & 0 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/AstPluginSwift.qll‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ private class AstPluginSwift extends AstPlugin {
1010
exists(f) and result = "func"
1111
}
1212

13+
bindingset[c]
14+
override string getConstructorDeclarationKeyword(ConstructorDeclaration c) {
15+
c.hasModifier(result) and
16+
result = "convenience"
17+
}
18+
1319
override string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) {
1420
cls.hasModifier(result) and
1521
result in ["class", "struct", "enum", "actor", "extension", "protocol"]
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
/**
2+
* Provides logic for synthesizing callables and parameters.
3+
*/
4+
5+
private import unified
6+
private import CallableExPlugin
7+
private import NameBinding
8+
9+
private predicate mayHaveImplicitConstructor(ClassLikeDeclaration cls, string name) {
10+
mayHaveConstructor(cls, name) and
11+
forall(ConstructorDeclaration c | c = cls.getAMember() | not constructorPreventsImplicit(c))
12+
}
13+
14+
bindingset[cls]
15+
private predicate mayInheritConstructorFrom(ClassLikeDeclaration cls, ClassLikeDeclaration base) {
16+
mayInheritConstructor(cls) and
17+
mayHaveImplicitConstructor(cls, _) and
18+
base.getNameNode() = getStaticBindingTargetFromRef(cls.getABaseType().getType()) and
19+
mayHaveConstructor(base, _)
20+
}
21+
22+
private newtype TCallableEx =
23+
TAstCallableEx(Callable c) or
24+
TDefaultConstructor(ClassLikeDeclaration cls, string name) {
25+
mayHaveImplicitConstructor(cls, name) and
26+
forall(ClassLikeDeclaration base | mayInheritConstructorFrom(cls, base) |
27+
not mayHaveConstructor(cls, _)
28+
)
29+
} or
30+
TInheritedConstructor(ClassLikeDeclaration cls, CallableEx baseCtor) {
31+
exists(ClassLikeDeclaration baseClass |
32+
mayInheritConstructorFrom(cls, baseClass) and
33+
baseCtor.isConstructor(baseClass)
34+
)
35+
}
36+
37+
private newtype TParameterEx =
38+
TAstParameter(Parameter p) or
39+
TImplicitReceiverParameter(Callable c) {
40+
exists(LocalVariable v | v.isImplicitReceiverParameter(c))
41+
} or
42+
TDefaultConstructorParameter(ClassLikeDeclaration cls, int i, string name) {
43+
defaultConstructorParameter(cls, i, name)
44+
} or
45+
TInheritedConstructorParameter(CallableEx ctor, CallableEx baseCtor, int i) {
46+
ctor.isInheritedConstructor(_, baseCtor) and
47+
exists(baseCtor.getParameter(i))
48+
}
49+
50+
final class CallableEx = CallableExImpl;
51+
52+
/**
53+
* A callable from source code or a synthesized callable.
54+
*/
55+
abstract private class CallableExImpl extends TCallableEx {
56+
/**
57+
* Gets the source code callable that this entity represents, if any.
58+
*/
59+
Callable asCallable() { this = TAstCallableEx(result) }
60+
61+
/**
62+
* Holds if this entity represents a default constructor for `cls`, for example a
63+
* parameterless Swift `class` constructor or a Swift `struct` constructor
64+
* with parameters for relevant fields.
65+
*/
66+
predicate isDefaultConstructor(ClassLikeDeclaration cls) { this = TDefaultConstructor(cls, _) }
67+
68+
/**
69+
* Holds if this entity represents an inherited constructor for `cls` from the
70+
* base constructor `baseCtor` (which may itself be inherited).
71+
*/
72+
predicate isInheritedConstructor(ClassLikeDeclaration cls, CallableEx baseCtor) {
73+
this = TInheritedConstructor(cls, baseCtor)
74+
}
75+
76+
/**
77+
* Holds if this entity represents a constructor for `cls`.
78+
*/
79+
predicate isConstructor(ClassLikeDeclaration cls) {
80+
this.asCallable().(ConstructorDeclaration) = cls.getAMember()
81+
or
82+
this.isDefaultConstructor(cls)
83+
or
84+
this.isInheritedConstructor(cls, _)
85+
}
86+
87+
/**
88+
* Holds if this entity is a direct member of `cls`.
89+
*/
90+
predicate isMemberOf(ClassLikeDeclaration cls) {
91+
this.asCallable() = cls.getAMember()
92+
or
93+
this.isConstructor(cls)
94+
}
95+
96+
/** Gets the `i`-th type parameter of this callable. */
97+
abstract TypeParameter getTypeParameter(int i);
98+
99+
/**
100+
* Gets the `i`-th parameter of this callable. Implicit receiver parameters are
101+
* at index `0`, and all other parameters start at index `1`.
102+
*/
103+
abstract ParameterEx getParameter(int i);
104+
105+
/** Gets a parameter of this callable. */
106+
final ParameterEx getAParameter() { result = this.getParameter(_) }
107+
108+
/** Gets the declared return type of this callable, if any. */
109+
abstract Expr getReturnType();
110+
111+
/** Gets the body of this callable, if any. */
112+
abstract AstNode getBody();
113+
114+
abstract Identifier getNameNode();
115+
116+
/** Gets the name of this callable. */
117+
abstract string getName();
118+
119+
/** Gets a textual representation of this callable. */
120+
abstract string toString();
121+
122+
/** Gets the location of this callable. */
123+
abstract Location getLocation();
124+
}
125+
126+
private class AstCallableEx extends CallableExImpl, TAstCallableEx {
127+
Callable c;
128+
129+
AstCallableEx() { this = TAstCallableEx(c) }
130+
131+
override TypeParameter getTypeParameter(int i) {
132+
result = c.(FunctionDeclaration).getTypeParameter(i)
133+
}
134+
135+
override ParameterEx getParameter(int i) {
136+
result.isImplicitReceiverParameter(c) and
137+
i = 0
138+
or
139+
exists(int j, Parameter p |
140+
result.asParameter() = p and
141+
j = i - 1
142+
|
143+
p = c.(FunctionDeclaration).getParameter(j)
144+
or
145+
p = c.(ConstructorDeclaration).getParameter(j)
146+
or
147+
p = c.(FunctionExpr).getParameter(j)
148+
or
149+
p = c.(AccessorDeclaration).getParameter(j)
150+
)
151+
}
152+
153+
override Expr getReturnType() {
154+
result = c.(FunctionDeclaration).getReturnType()
155+
or
156+
result = c.(FunctionExpr).getReturnType()
157+
}
158+
159+
override AstNode getBody() { result = c.getBody() }
160+
161+
override Identifier getNameNode() {
162+
result = c.(AccessorDeclaration).getNameNode()
163+
or
164+
result = c.(ConstructorDeclaration).getNameNode()
165+
or
166+
result = c.(FunctionDeclaration).getNameNode()
167+
}
168+
169+
override string getName() {
170+
result = this.getNameNode().getValue()
171+
or
172+
c instanceof DestructorDeclaration and
173+
result = "<destructor>"
174+
or
175+
c instanceof InitializerDeclaration and
176+
result = "<initializer>"
177+
}
178+
179+
override string toString() { result = c.toString() }
180+
181+
override Location getLocation() { result = c.getLocation() }
182+
}
183+
184+
private class DefaultConstructor extends CallableExImpl, TDefaultConstructor {
185+
ClassLikeDeclaration c;
186+
string name;
187+
188+
DefaultConstructor() { this = TDefaultConstructor(c, name) }
189+
190+
override TypeParameter getTypeParameter(int i) { none() }
191+
192+
override ParameterEx getParameter(int i) { result = TDefaultConstructorParameter(c, i, _) }
193+
194+
override Expr getReturnType() { none() }
195+
196+
override AstNode getBody() { none() }
197+
198+
override Identifier getNameNode() { none() }
199+
200+
override string getName() { result = name }
201+
202+
override string toString() { result = c.getName() + " [default constructor]" }
203+
204+
override Location getLocation() { result = c.getLocation() }
205+
}
206+
207+
private class InheritedConstructor extends CallableExImpl, TInheritedConstructor {
208+
ClassLikeDeclaration cls;
209+
CallableEx baseCtor;
210+
211+
InheritedConstructor() { this = TInheritedConstructor(cls, baseCtor) }
212+
213+
override TypeParameter getTypeParameter(int i) { none() }
214+
215+
override ParameterEx getParameter(int i) {
216+
result.isInheritedConstructorParameter(this, baseCtor, i)
217+
}
218+
219+
override Expr getReturnType() { none() }
220+
221+
override AstNode getBody() { none() }
222+
223+
override Identifier getNameNode() { none() }
224+
225+
override string getName() { result = baseCtor.getName() }
226+
227+
override string toString() {
228+
exists(ClassLikeDeclaration baseCls |
229+
baseCtor.isMemberOf(baseCls) and
230+
result = cls.getName() + " [inherited from " + baseCls.getName() + "]"
231+
)
232+
}
233+
234+
override Location getLocation() { result = cls.getLocation() }
235+
}
236+
237+
final class ParameterEx = ParameterExImpl;
238+
239+
/**
240+
* A parameter from source code or a synthesized parameter.
241+
*/
242+
abstract private class ParameterExImpl extends TParameterEx {
243+
/**
244+
* Gets the source code parameter that this entity represents, if any.
245+
*/
246+
Parameter asParameter() { this = TAstParameter(result) }
247+
248+
/**
249+
* Holds if this entity represents a regular parameter from source code.
250+
*/
251+
predicate isParameter() { exists(this.asParameter()) }
252+
253+
/**
254+
* Holds if this entity represents an implicit receiver parameter of `c`.
255+
*/
256+
predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) }
257+
258+
predicate isDefaultConstructorParameter(ClassLikeDeclaration cls, int i, string name) {
259+
this = TDefaultConstructorParameter(cls, i, name)
260+
}
261+
262+
/**
263+
* Holds if this entity represents the `i`th parameter of the inherited constructor
264+
* `ctor` where `baseCtor` is the base constructor.
265+
*/
266+
predicate isInheritedConstructorParameter(CallableEx ctor, CallableEx baseCtor, int i) {
267+
this = TInheritedConstructorParameter(ctor, baseCtor, i)
268+
}
269+
270+
/** Gets a textual representation of this parameter. */
271+
abstract string toString();
272+
273+
/** Gets the location of this parameter. */
274+
abstract Location getLocation();
275+
}
276+
277+
private class AstParameterEx extends ParameterExImpl, TAstParameter {
278+
Parameter p;
279+
280+
AstParameterEx() { this = TAstParameter(p) }
281+
282+
override string toString() { result = p.toString() }
283+
284+
override Location getLocation() { result = p.getLocation() }
285+
}
286+
287+
private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitReceiverParameter {
288+
Callable c;
289+
290+
ImplicitReceiverParameterEx() { this = TImplicitReceiverParameter(c) }
291+
292+
override string toString() {
293+
exists(LocalVariable v |
294+
v.isImplicitReceiverParameter(c) and
295+
result = v.getName()
296+
)
297+
}
298+
299+
override Location getLocation() { result = c.getLocation() }
300+
}
301+
302+
private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultConstructorParameter {
303+
ClassLikeDeclaration c;
304+
int i;
305+
string name;
306+
307+
DefaultConstructorParameterEx() { this = TDefaultConstructorParameter(c, i, name) }
308+
309+
override string toString() { result = name + " [" + c.getName() + " default constructor]" }
310+
311+
override Location getLocation() { result = c.getLocation() }
312+
}
313+
314+
private class InheritedConstructorParameterEx extends ParameterExImpl,
315+
TInheritedConstructorParameter
316+
{
317+
CallableEx ctor;
318+
CallableEx baseCtor;
319+
int i;
320+
321+
InheritedConstructorParameterEx() { this = TInheritedConstructorParameter(ctor, baseCtor, i) }
322+
323+
override string toString() {
324+
exists(ParameterEx baseParam, ClassLikeDeclaration baseCls |
325+
baseCtor.isMemberOf(baseCls) and
326+
baseParam = baseCtor.getParameter(i) and
327+
result =
328+
"parameter " + i + " of " + ctor.getName() + " [inherited from " + baseCls.getName() + "]"
329+
)
330+
}
331+
332+
override Location getLocation() { result = ctor.getLocation() }
333+
}

0 commit comments

Comments
 (0)