Skip to content

Commit c77fc26

Browse files
committed
Swift: Callable abstraction
This new class encompasses both `AbstractFunctionDecl` and `AbstractClosureExpr`, together with their common parts (namely parameters and the body). `ClosureExpr` and `AutoClosureExpr` got ported to structured C++ generated translation in the process.
1 parent 0912996 commit c77fc26

File tree

12 files changed

+84
-117
lines changed

12 files changed

+84
-117
lines changed

swift/codegen/schema.yml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ NominalTypeDecl:
5454
AstNode:
5555
_extends: Locatable
5656

57+
Callable:
58+
_children:
59+
params: ParamDecl*
60+
body: BraceStmt?
61+
5762
ConditionElement:
5863
_extends: Locatable
5964
_children:
@@ -290,9 +295,9 @@ ValueDecl:
290295
interface_type: Type
291296

292297
AbstractClosureExpr:
293-
_extends: Expr
294-
_children:
295-
params: ParamDecl*
298+
_extends:
299+
- Expr
300+
- Callable
296301

297302
AnyTryExpr:
298303
_extends: Expr
@@ -712,10 +717,8 @@ AbstractFunctionDecl:
712717
_extends:
713718
- GenericContext
714719
- ValueDecl
720+
- Callable
715721
name: string
716-
_children:
717-
body: BraceStmt?
718-
params: ParamDecl*
719722

720723
AbstractStorageDecl:
721724
_extends: ValueDecl
@@ -735,13 +738,9 @@ TypeDecl:
735738

736739
AutoClosureExpr:
737740
_extends: AbstractClosureExpr
738-
_children:
739-
body: BraceStmt
740741

741742
ClosureExpr:
742743
_extends: AbstractClosureExpr
743-
_children:
744-
body: BraceStmt
745744

746745
ForceTryExpr:
747746
_extends: AnyTryExpr

swift/extractor/visitors/ExprVisitor.h

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,16 @@ class ExprVisitor : public AstVisitorBase<ExprVisitor> {
363363
dispatcher_.emit(DiscardAssignmentExprsTrap{label});
364364
}
365365

366-
void visitClosureExpr(swift::ClosureExpr* expr) {
367-
auto label = dispatcher_.assignNewLabel(expr);
368-
assert(expr->getBody() && "ClosureExpr has getBody()");
369-
auto bodyLabel = dispatcher_.fetchLabel(expr->getBody());
370-
dispatcher_.emit(ClosureExprsTrap{label, bodyLabel});
371-
emitAbstractClosureExpr(expr, label);
366+
codeql::ClosureExpr translateClosureExpr(const swift::ClosureExpr& expr) {
367+
ClosureExpr entry{dispatcher_.assignNewLabel(expr)};
368+
fillAbstractClosureExpr(expr, entry);
369+
return entry;
372370
}
373371

374-
void visitAutoClosureExpr(swift::AutoClosureExpr* expr) {
375-
auto label = dispatcher_.assignNewLabel(expr);
376-
assert(expr->getBody() && "AutoClosureExpr has getBody()");
377-
auto bodyLabel = dispatcher_.fetchLabel(expr->getBody());
378-
dispatcher_.emit(AutoClosureExprsTrap{label, bodyLabel});
379-
emitAbstractClosureExpr(expr, label);
372+
codeql::AutoClosureExpr translateAutoClosureExpr(const swift::AutoClosureExpr& expr) {
373+
AutoClosureExpr entry{dispatcher_.assignNewLabel(expr)};
374+
fillAbstractClosureExpr(expr, entry);
375+
return entry;
380376
}
381377

382378
void visitCoerceExpr(swift::CoerceExpr* expr) {
@@ -536,14 +532,12 @@ class ExprVisitor : public AstVisitorBase<ExprVisitor> {
536532
}
537533

538534
private:
539-
void emitAbstractClosureExpr(swift::AbstractClosureExpr* expr,
540-
TrapLabel<AbstractClosureExprTag> label) {
541-
assert(expr->getParameters() && "AbstractClosureExpr has getParameters()");
542-
auto params = expr->getParameters();
543-
for (auto i = 0u; i < params->size(); ++i) {
544-
dispatcher_.emit(
545-
AbstractClosureExprParamsTrap{label, i, dispatcher_.fetchLabel(params->get(i))});
546-
}
535+
void fillAbstractClosureExpr(const swift::AbstractClosureExpr& expr,
536+
codeql::AbstractClosureExpr& entry) {
537+
assert(expr.getParameters() && "AbstractClosureExpr has getParameters()");
538+
assert(expr.getBody() && "AbstractClosureExpr has getBody()");
539+
entry.params = dispatcher_.fetchRepeatedLabels(*expr.getParameters());
540+
entry.body = dispatcher_.fetchLabel(expr.getBody());
547541
}
548542

549543
TrapLabel<ArgumentTag> emitArgument(const swift::Argument& arg) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// generated by codegen/codegen.py
22
import codeql.swift.elements.AstNode
3+
import codeql.swift.elements.Callable
34
import codeql.swift.elements.Element
45
import codeql.swift.elements.File
56
import codeql.swift.elements.Locatable
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
private import codeql.swift.generated.Callable
2+
private import codeql.swift.elements.AstNode
3+
4+
class Callable extends CallableBase, AstNode { }
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
private import codeql.swift.generated.decl.ParamDecl
2-
private import codeql.swift.elements.decl.AbstractFunctionDecl
2+
private import codeql.swift.elements.Callable
33

44
class ParamDecl extends ParamDeclBase {
55
/** Gets the function which declares this parameter. */
6-
AbstractFunctionDecl getDeclaringFunction() { result.getAParam() = this }
6+
Callable getDeclaringFunction() { result.getAParam() = this }
77

88
/** Gets the index of this parameter in its declaring function's parameter list. */
9-
int getIndex() { exists(AbstractFunctionDecl func | func.getParam(result) = this) }
9+
int getIndex() { exists(Callable func | func.getParam(result) = this) }
1010
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// generated by codegen/codegen.py
2+
import codeql.swift.elements.stmt.BraceStmt
3+
import codeql.swift.elements.Element
4+
import codeql.swift.elements.decl.ParamDecl
5+
6+
class CallableBase extends @callable, Element {
7+
ParamDecl getParam(int index) {
8+
exists(ParamDecl x |
9+
callable_params(this, index, x) and
10+
result = x.resolve()
11+
)
12+
}
13+
14+
ParamDecl getAParam() { result = getParam(_) }
15+
16+
int getNumberOfParams() { result = count(getAParam()) }
17+
18+
BraceStmt getBody() {
19+
exists(BraceStmt x |
20+
callable_bodies(this, x) and
21+
result = x.resolve()
22+
)
23+
}
24+
25+
predicate hasBody() { exists(getBody()) }
26+
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Element getAnImmediateChild(Element e) {
1818
(
1919
none()
2020
or
21-
abstract_function_decl_bodies(e, x)
21+
callable_params(e, _, x)
2222
or
23-
abstract_function_decl_params(e, _, x)
23+
callable_bodies(e, x)
2424
or
2525
abstract_storage_decl_accessor_decls(e, _, x)
2626
or
@@ -36,8 +36,6 @@ Element getAnImmediateChild(Element e) {
3636
or
3737
top_level_code_decls(e, x)
3838
or
39-
abstract_closure_expr_params(e, _, x)
40-
or
4139
any_try_exprs(e, x)
4240
or
4341
apply_exprs(e, x)
@@ -52,16 +50,12 @@ Element getAnImmediateChild(Element e) {
5250
or
5351
assign_exprs(e, _, x)
5452
or
55-
auto_closure_exprs(e, x)
56-
or
5753
bind_optional_exprs(e, x)
5854
or
5955
capture_list_expr_binding_decls(e, _, x)
6056
or
6157
capture_list_exprs(e, x)
6258
or
63-
closure_exprs(e, x)
64-
or
6559
dictionary_expr_elements(e, _, x)
6660
or
6761
dot_syntax_base_ignored_exprs(e, x, _)
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
11
// generated by codegen/codegen.py
2-
import codeql.swift.elements.stmt.BraceStmt
2+
import codeql.swift.elements.Callable
33
import codeql.swift.elements.decl.GenericContext
4-
import codeql.swift.elements.decl.ParamDecl
54
import codeql.swift.elements.decl.ValueDecl
65

7-
class AbstractFunctionDeclBase extends @abstract_function_decl, GenericContext, ValueDecl {
6+
class AbstractFunctionDeclBase extends @abstract_function_decl, Callable, GenericContext, ValueDecl {
87
string getName() { abstract_function_decls(this, result) }
9-
10-
BraceStmt getBody() {
11-
exists(BraceStmt x |
12-
abstract_function_decl_bodies(this, x) and
13-
result = x.resolve()
14-
)
15-
}
16-
17-
predicate hasBody() { exists(getBody()) }
18-
19-
ParamDecl getParam(int index) {
20-
exists(ParamDecl x |
21-
abstract_function_decl_params(this, index, x) and
22-
result = x.resolve()
23-
)
24-
}
25-
26-
ParamDecl getAParam() { result = getParam(_) }
27-
28-
int getNumberOfParams() { result = count(getAParam()) }
298
}
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
// generated by codegen/codegen.py
2+
import codeql.swift.elements.Callable
23
import codeql.swift.elements.expr.Expr
3-
import codeql.swift.elements.decl.ParamDecl
44

5-
class AbstractClosureExprBase extends @abstract_closure_expr, Expr {
6-
ParamDecl getParam(int index) {
7-
exists(ParamDecl x |
8-
abstract_closure_expr_params(this, index, x) and
9-
result = x.resolve()
10-
)
11-
}
12-
13-
ParamDecl getAParam() { result = getParam(_) }
14-
15-
int getNumberOfParams() { result = count(getAParam()) }
16-
}
5+
class AbstractClosureExprBase extends @abstract_closure_expr, Callable, Expr { }
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
// generated by codegen/codegen.py
22
import codeql.swift.elements.expr.AbstractClosureExpr
3-
import codeql.swift.elements.stmt.BraceStmt
43

54
class AutoClosureExprBase extends @auto_closure_expr, AbstractClosureExpr {
65
override string getAPrimaryQlClass() { result = "AutoClosureExpr" }
7-
8-
BraceStmt getBody() {
9-
exists(BraceStmt x |
10-
auto_closure_exprs(this, x) and
11-
result = x.resolve()
12-
)
13-
}
146
}

0 commit comments

Comments
 (0)