Skip to content

Commit 82eb002

Browse files
committed
Swift: AST library renamings
1 parent 2d9295a commit 82eb002

21 files changed

+136
-40
lines changed

swift/ql/lib/codeql/swift/elements/AstNode.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
private import codeql.swift.generated.AstNode
2-
private import codeql.swift.elements.decl.AbstractFunctionDecl
2+
private import codeql.swift.elements.decl.Function
33
private import codeql.swift.elements.decl.Decl
4-
private import codeql.swift.elements.expr.AbstractClosureExpr
4+
private import codeql.swift.elements.expr.ClosureExpr
55
private import codeql.swift.elements.Callable
66
private import codeql.swift.generated.ParentChild
77

@@ -15,12 +15,12 @@ private module Cached {
1515
Decl getEnclosingDecl(AstNode ast) { result = getEnclosingDeclStep*(getImmediateParent(ast)) }
1616

1717
private Element getEnclosingFunctionStep(Element e) {
18-
not e instanceof AbstractFunctionDecl and
18+
not e instanceof Function and
1919
result = getEnclosingDecl(e)
2020
}
2121

2222
cached
23-
AbstractFunctionDecl getEnclosingFunction(AstNode ast) {
23+
Function getEnclosingFunction(AstNode ast) {
2424
result = getEnclosingFunctionStep*(getEnclosingDecl(ast))
2525
}
2626

@@ -30,7 +30,7 @@ private module Cached {
3030
}
3131

3232
cached
33-
AbstractClosureExpr getEnclosingClosure(AstNode ast) {
33+
ClosureExpr getEnclosingClosure(AstNode ast) {
3434
result = getEnclosingClosureStep*(getImmediateParent(ast))
3535
}
3636
}
@@ -53,7 +53,7 @@ class AstNode extends Generated::AstNode {
5353
* }
5454
* ```
5555
*/
56-
final AbstractFunctionDecl getEnclosingFunction() { result = Cached::getEnclosingFunction(this) }
56+
final Function getEnclosingFunction() { result = Cached::getEnclosingFunction(this) }
5757

5858
/**
5959
* Gets the nearest declaration that contains this AST node, if any.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
private import codeql.swift.generated.decl.Accessor
2+
3+
private predicate isKnownAccessorKind(Accessor decl, string kind) {
4+
decl.isGetter() and kind = "get"
5+
or
6+
decl.isSetter() and kind = "set"
7+
or
8+
decl.isWillSet() and kind = "willSet"
9+
or
10+
decl.isDidSet() and kind = "didSet"
11+
or
12+
decl.isRead() and kind = "_read"
13+
or
14+
decl.isModify() and kind = "_modify"
15+
or
16+
decl.isUnsafeAddress() and kind = "unsafeAddress"
17+
or
18+
decl.isUnsafeMutableAddress() and kind = "unsafeMutableAddress"
19+
}
20+
21+
class Accessor extends Generated::Accessor {
22+
predicate isPropertyObserver() {
23+
this instanceof WillSetObserver or this instanceof DidSetObserver
24+
}
25+
26+
override string toString() {
27+
isKnownAccessorKind(this, result)
28+
or
29+
not isKnownAccessorKind(this, _) and
30+
result = super.toString()
31+
}
32+
}
33+
34+
class WillSetObserver extends Accessor {
35+
WillSetObserver() { this.isWillSet() }
36+
}
37+
38+
class DidSetObserver extends Accessor {
39+
DidSetObserver() { this.isDidSet() }
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
private import codeql.swift.generated.decl.Deinitializer
2+
private import codeql.swift.elements.decl.Method
3+
4+
/**
5+
* A deinitializer of a class.
6+
*/
7+
class Deinitializer extends Generated::Deinitializer, Method {
8+
override string toString() { result = this.getSelfParam().getType() + "." + super.toString() }
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
private import codeql.swift.generated.decl.Function
2+
private import codeql.swift.elements.decl.Method
3+
4+
/**
5+
* A function.
6+
*/
7+
class Function extends Generated::Function, Callable {
8+
override string toString() { result = this.getName() }
9+
}
10+
11+
/**
12+
* A free (non-member) function.
13+
*/
14+
class FreeFunction extends Function {
15+
FreeFunction() { not this instanceof Method }
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
private import codeql.swift.generated.decl.Initializer
2+
private import codeql.swift.elements.decl.Method
3+
private import codeql.swift.elements.type.FunctionType
4+
private import codeql.swift.elements.type.OptionalType
5+
6+
/**
7+
* An initializer of a class, struct, enum or protocol.
8+
*/
9+
class Initializer extends Generated::Initializer, Method {
10+
override string toString() { result = this.getSelfParam().getType() + "." + super.toString() }
11+
12+
/** Holds if this initializer returns an optional type. Failable initializers are written as `init?`. */
13+
predicate isFailable() {
14+
this.getInterfaceType().(FunctionType).getResult().(FunctionType).getResult() instanceof
15+
OptionalType
16+
}
17+
}

swift/ql/lib/codeql/swift/elements/decl/MethodDecl.qll renamed to swift/ql/lib/codeql/swift/elements/decl/Method.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ private Decl getAMember(Decl ctx) {
55
or
66
exists(VarDecl var |
77
ctx.getAMember() = var and
8-
var.getAnAccessorDecl() = result
8+
var.getAnAccessor() = result
99
)
1010
}
1111

1212
/**
1313
* A function that is a member of a class, struct, enum or protocol.
1414
*/
15-
class MethodDecl extends AbstractFunctionDecl {
16-
MethodDecl() {
15+
class Method extends Function {
16+
Method() {
1717
this = getAMember(any(ClassDecl c))
1818
or
1919
this = getAMember(any(StructDecl c))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ private import codeql.swift.elements.expr.DeclRefExpr
44
private import codeql.swift.elements.expr.MethodLookupExpr
55
private import codeql.swift.elements.expr.DotSyntaxBaseIgnoredExpr
66
private import codeql.swift.elements.expr.AutoClosureExpr
7-
private import codeql.swift.elements.decl.MethodDecl
7+
private import codeql.swift.elements.decl.Method
88

99
class ApplyExpr extends Generated::ApplyExpr {
1010
Callable getStaticTarget() { result = this.getFunction().(DeclRefExpr).getDecl() }
@@ -33,7 +33,7 @@ class MethodApplyExpr extends ApplyExpr {
3333

3434
MethodApplyExpr() { method = this.getFunction() }
3535

36-
override MethodDecl getStaticTarget() { result = method.getMethod() }
36+
override Method getStaticTarget() { result = method.getMethod() }
3737

3838
override Expr getQualifier() { result = method.getBase() }
3939
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
private import codeql.swift.generated.expr.BinaryExpr
22
private import codeql.swift.elements.expr.Expr
3-
private import codeql.swift.elements.decl.AbstractFunctionDecl
3+
private import codeql.swift.elements.decl.Function
44

55
/**
66
* A Swift binary expression, that is, an expression that appears between its
@@ -23,7 +23,7 @@ class BinaryExpr extends Generated::BinaryExpr {
2323
/**
2424
* Gets the operator of this binary expression (the function that is called).
2525
*/
26-
AbstractFunctionDecl getOperator() { result = this.getStaticTarget() }
26+
Function getOperator() { result = this.getStaticTarget() }
2727

2828
/**
2929
* Gets an operand of this binary expression (left or right).
@@ -32,5 +32,5 @@ class BinaryExpr extends Generated::BinaryExpr {
3232

3333
override string toString() { result = "... " + this.getFunction().toString() + " ..." }
3434

35-
override AbstractFunctionDecl getStaticTarget() { result = super.getStaticTarget() }
35+
override Function getStaticTarget() { result = super.getStaticTarget() }
3636
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ private import codeql.swift.generated.expr.DotSyntaxBaseIgnoredExpr
22
private import codeql.swift.elements.expr.AutoClosureExpr
33
private import codeql.swift.elements.expr.CallExpr
44
private import codeql.swift.elements.expr.TypeExpr
5-
private import codeql.swift.elements.decl.MethodDecl
5+
private import codeql.swift.elements.decl.Method
66

77
/**
88
* An expression representing a partially applied lookup of an instance property via the receiver's type object.
@@ -28,7 +28,7 @@ class DotSyntaxBaseIgnoredExpr extends Generated::DotSyntaxBaseIgnoredExpr {
2828
* Gets the underlying instance method that is called when the result of this
2929
* expression is fully applied.
3030
*/
31-
MethodDecl getMethod() {
31+
Method getMethod() {
3232
result =
3333
this.getSubExpr()
3434
.(AutoClosureExpr)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
private import codeql.swift.elements.expr.MethodCallExpr
22
private import codeql.swift.elements.expr.InitializerLookupExpr
3-
private import codeql.swift.elements.decl.ConstructorDecl
3+
private import codeql.swift.elements.decl.Initializer
44

55
class InitializerCallExpr extends MethodCallExpr {
66
InitializerCallExpr() { this.getFunction() instanceof InitializerLookupExpr }
77

8-
override ConstructorDecl getStaticTarget() { result = super.getStaticTarget() }
8+
override Initializer getStaticTarget() { result = super.getStaticTarget() }
99
}

0 commit comments

Comments
 (0)