Skip to content

Commit 0c55561

Browse files
committed
Swift: initial schema change (hand-written part)
1 parent 1ede851 commit 0c55561

File tree

9 files changed

+78
-19
lines changed

9 files changed

+78
-19
lines changed

swift/ql/lib/codeql/swift/elements/expr/ApplyExpr.qll

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
private import codeql.swift.generated.expr.ApplyExpr
2-
private import codeql.swift.elements.decl.AbstractFunctionDecl
2+
private import codeql.swift.elements.Callable
33
private import codeql.swift.elements.expr.DeclRefExpr
4-
private import codeql.swift.elements.expr.MethodRefExpr
4+
private import codeql.swift.elements.expr.MethodLookupExpr
55
private import codeql.swift.elements.expr.ConstructorRefCallExpr
6+
private import codeql.swift.elements.decl.MethodDecl
67

78
class ApplyExpr extends Generated::ApplyExpr {
8-
AbstractFunctionDecl getStaticTarget() {
9+
Callable getStaticTarget() {
910
exists(Expr f |
1011
f = this.getFunction() and
1112
(
1213
result = f.(DeclRefExpr).getDecl()
1314
or
14-
result = f.(ConstructorRefCallExpr).getFunction().(DeclRefExpr).getDecl()
15+
result = f.(ConstructorRefCallExpr).getFunction().(DeclRefExpr).getDecl() // TODO: fix this
1516
)
1617
)
1718
}
@@ -36,11 +37,11 @@ class ApplyExpr extends Generated::ApplyExpr {
3637
}
3738

3839
class MethodApplyExpr extends ApplyExpr {
39-
private MethodRefExpr method;
40+
private MethodLookupExpr method;
4041

4142
MethodApplyExpr() { method = this.getFunction() }
4243

43-
override AbstractFunctionDecl getStaticTarget() { result = method.getMethod() }
44+
override MethodDecl getStaticTarget() { result = method.getMethod() }
4445

4546
override Expr getQualifier() { result = method.getBase() }
4647
}

swift/ql/lib/codeql/swift/elements/expr/BinaryExpr.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ class BinaryExpr extends Generated::BinaryExpr {
3131
Expr getAnOperand() { result = [this.getLeftOperand(), this.getRightOperand()] }
3232

3333
override string toString() { result = "... " + this.getFunction().toString() + " ..." }
34+
35+
override AbstractFunctionDecl getStaticTarget() { result = super.getStaticTarget() }
3436
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.Raw
32

4-
predicate constructConstructorRefCallExpr(Raw::ConstructorRefCallExpr id) { any() }
3+
predicate constructConstructorRefCallExpr(Raw::ConstructorRefCallExpr id) { none() }
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.Raw
3-
private import codeql.swift.generated.PureSynthConstructors
42

5-
predicate constructDotSyntaxCallExpr(Raw::DotSyntaxCallExpr id) { not constructMethodRefExpr(id) }
3+
predicate constructDotSyntaxCallExpr(Raw::DotSyntaxCallExpr id) { none() }
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
private import codeql.swift.generated.expr.MethodRefExpr
1+
private import codeql.swift.generated.expr.MethodLookupExpr
22
private import codeql.swift.elements.expr.Expr
33
private import codeql.swift.elements.decl.Decl
4-
private import codeql.swift.elements.decl.AbstractFunctionDecl
4+
private import codeql.swift.elements.decl.MethodDecl
55
private import codeql.swift.generated.Raw
66
private import codeql.swift.generated.Synth
77

8-
class MethodRefExpr extends Generated::MethodRefExpr {
8+
class MethodLookupExpr extends Generated::MethodLookupExpr {
99
override string toString() { result = "." + this.getMember().toString() }
1010

1111
override Expr getImmediateBase() {
@@ -14,11 +14,11 @@ class MethodRefExpr extends Generated::MethodRefExpr {
1414

1515
override Decl getImmediateMember() {
1616
result =
17-
Synth::convertDeclFromRaw(this.getUnderlying().getFunction().(Raw::DeclRefExpr).getDecl())
17+
Synth::convertDeclFromRaw(this.getUnderlying().getFunction().(Raw::DeclRefExpr).getDecl()) // TODO: FIX THIS
1818
}
1919

20-
AbstractFunctionDecl getMethod() { result = this.getMember() }
20+
MethodDecl getMethod() { result = this.getMember() }
2121

2222
cached
23-
private Raw::DotSyntaxCallExpr getUnderlying() { this = Synth::TMethodRefExpr(result) }
23+
private Raw::SelfApplyExpr getUnderlying() { this = Synth::TMethodLookupExpr(result) }
2424
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
private import codeql.swift.generated.Raw
2+
3+
predicate constructMethodLookupExpr(Raw::SelfApplyExpr id) { any() }

swift/ql/lib/codeql/swift/elements/expr/PrefixUnaryExpr.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ class PrefixUnaryExpr extends Generated::PrefixUnaryExpr {
1919
* Gets the operator of this prefix unary expression (the function that is called).
2020
*/
2121
AbstractFunctionDecl getOperator() { result = this.getStaticTarget() }
22+
23+
override AbstractFunctionDecl getStaticTarget() { result = super.getStaticTarget() }
2224
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class X {
2+
static func foo(_: Int, _:Int) {}
3+
class func bar() {}
4+
func baz(_: Int) {}
5+
6+
init() {
7+
let f = baz
8+
}
9+
}
10+
11+
actor Y {
12+
static func foo(_: Int, _:Int) {}
13+
func baz(_: Int) {}
14+
15+
init() {
16+
let f = baz
17+
}
18+
}
19+
20+
@MainActor
21+
class Z {
22+
static func foo(_: Int, _:Int) {}
23+
class func bar() {}
24+
func baz(_: Int) {}
25+
26+
init() {
27+
let f = baz
28+
}
29+
}
30+
31+
do {
32+
X.foo(1, 2)
33+
X.bar()
34+
X().baz(42)
35+
36+
let f = X.bar
37+
let g = X().baz
38+
}
39+
40+
Task {
41+
Y.foo(1, 2)
42+
await Y().baz(42)
43+
44+
let f = Y.foo
45+
}
46+
47+
Task {
48+
await Z.foo(1, 2)
49+
await Z.bar()
50+
await Z().baz(42)
51+
52+
let f = Z.bar
53+
let g = (await Z()).baz
54+
}

swift/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,8 @@ class ConstructorRefCallExpr(SelfApplyExpr):
674674
class DotSyntaxCallExpr(SelfApplyExpr):
675675
pass
676676

677-
@synth.from_class(DotSyntaxCallExpr)
678-
class MethodRefExpr(LookupExpr):
677+
@synth.from_class(SelfApplyExpr)
678+
class MethodLookupExpr(LookupExpr):
679679
pass
680680

681681
class DynamicMemberRefExpr(DynamicLookupExpr):

0 commit comments

Comments
 (0)