Skip to content

Commit 389fd3a

Browse files
committed
Unified: Introduce CallableEx and ParameterEx
1 parent 605dc1c commit 389fd3a

7 files changed

Lines changed: 540 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: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
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+
)
149+
}
150+
151+
override Expr getReturnType() {
152+
result = c.(FunctionDeclaration).getReturnType()
153+
or
154+
result = c.(FunctionExpr).getReturnType()
155+
}
156+
157+
override AstNode getBody() { result = c.getBody() }
158+
159+
override Identifier getNameNode() {
160+
result = c.(AccessorDeclaration).getNameNode()
161+
or
162+
result = c.(ConstructorDeclaration).getNameNode()
163+
or
164+
result = c.(FunctionDeclaration).getNameNode()
165+
}
166+
167+
override string getName() {
168+
result = this.getNameNode().getValue()
169+
or
170+
c instanceof DestructorDeclaration and
171+
result = "<destructor>"
172+
or
173+
c instanceof InitializerDeclaration and
174+
result = "<initializer>"
175+
}
176+
177+
override string toString() { result = c.toString() }
178+
179+
override Location getLocation() { result = c.getLocation() }
180+
}
181+
182+
private class DefaultConstructor extends CallableExImpl, TDefaultConstructor {
183+
ClassLikeDeclaration c;
184+
string name;
185+
186+
DefaultConstructor() { this = TDefaultConstructor(c, name) }
187+
188+
override TypeParameter getTypeParameter(int i) { none() }
189+
190+
override ParameterEx getParameter(int i) { result = TDefaultConstructorParameter(c, i, _) }
191+
192+
override Expr getReturnType() { none() }
193+
194+
override AstNode getBody() { none() }
195+
196+
override Identifier getNameNode() { none() }
197+
198+
override string getName() { result = name }
199+
200+
override string toString() { result = c.getName() + " [default constructor]" }
201+
202+
override Location getLocation() { result = c.getLocation() }
203+
}
204+
205+
private class InheritedConstructor extends CallableExImpl, TInheritedConstructor {
206+
ClassLikeDeclaration cls;
207+
CallableEx baseCtor;
208+
209+
InheritedConstructor() { this = TInheritedConstructor(cls, baseCtor) }
210+
211+
override TypeParameter getTypeParameter(int i) { none() }
212+
213+
override ParameterEx getParameter(int i) {
214+
result.isInheritedConstructorParameter(this, baseCtor, i)
215+
}
216+
217+
override Expr getReturnType() { none() }
218+
219+
override AstNode getBody() { none() }
220+
221+
override Identifier getNameNode() { none() }
222+
223+
override string getName() { result = baseCtor.getName() }
224+
225+
override string toString() {
226+
exists(ClassLikeDeclaration baseCls |
227+
baseCtor.isMemberOf(baseCls) and
228+
result = cls.getName() + " [inherited from " + baseCls.getName() + "]"
229+
)
230+
}
231+
232+
override Location getLocation() { result = cls.getLocation() }
233+
}
234+
235+
final class ParameterEx = ParameterExImpl;
236+
237+
/**
238+
* A parameter from source code or a synthesized parameter.
239+
*/
240+
abstract private class ParameterExImpl extends TParameterEx {
241+
/**
242+
* Gets the source code parameter that this entity represents, if any.
243+
*/
244+
Parameter asParameter() { this = TAstParameter(result) }
245+
246+
/**
247+
* Holds if this entity represents a regular parameter from source code.
248+
*/
249+
predicate isParameter() { exists(this.asParameter()) }
250+
251+
/**
252+
* Holds if this entity represents an implicit receiver parameter of `c`.
253+
*/
254+
predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) }
255+
256+
predicate isDefaultConstructorParameter(ClassLikeDeclaration cls, int i, string name) {
257+
this = TDefaultConstructorParameter(cls, i, name)
258+
}
259+
260+
/**
261+
* Holds if this entity represents the `i`th parameter of the inherited constructor
262+
* `ctor` where `baseCtor` is the base constructor.
263+
*/
264+
predicate isInheritedConstructorParameter(CallableEx ctor, CallableEx baseCtor, int i) {
265+
this = TInheritedConstructorParameter(ctor, baseCtor, i)
266+
}
267+
268+
/** Gets a textual representation of this parameter. */
269+
abstract string toString();
270+
271+
/** Gets the location of this parameter. */
272+
abstract Location getLocation();
273+
}
274+
275+
private class AstParameterEx extends ParameterExImpl, TAstParameter {
276+
Parameter p;
277+
278+
AstParameterEx() { this = TAstParameter(p) }
279+
280+
override string toString() { result = p.toString() }
281+
282+
override Location getLocation() { result = p.getLocation() }
283+
}
284+
285+
private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitReceiverParameter {
286+
Callable c;
287+
288+
ImplicitReceiverParameterEx() { this = TImplicitReceiverParameter(c) }
289+
290+
override string toString() {
291+
exists(LocalVariable v |
292+
v.isImplicitReceiverParameter(c) and
293+
result = v.getName()
294+
)
295+
}
296+
297+
override Location getLocation() { result = c.getLocation() }
298+
}
299+
300+
private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultConstructorParameter {
301+
ClassLikeDeclaration c;
302+
int i;
303+
string name;
304+
305+
DefaultConstructorParameterEx() { this = TDefaultConstructorParameter(c, i, name) }
306+
307+
override string toString() { result = name + " [" + c.getName() + " default constructor]" }
308+
309+
override Location getLocation() { result = c.getLocation() }
310+
}
311+
312+
private class InheritedConstructorParameterEx extends ParameterExImpl,
313+
TInheritedConstructorParameter
314+
{
315+
CallableEx ctor;
316+
CallableEx baseCtor;
317+
int i;
318+
319+
InheritedConstructorParameterEx() { this = TInheritedConstructorParameter(ctor, baseCtor, i) }
320+
321+
override string toString() {
322+
exists(ParameterEx baseParam, ClassLikeDeclaration baseCls |
323+
baseCtor.isMemberOf(baseCls) and
324+
baseParam = baseCtor.getParameter(i) and
325+
result =
326+
"parameter " + i + " of " + ctor.getName() + " [inherited from " + baseCls.getName() + "]"
327+
)
328+
}
329+
330+
override Location getLocation() { result = ctor.getLocation() }
331+
}

0 commit comments

Comments
 (0)