Skip to content

Commit f96c18a

Browse files
committed
Swift: subsume IterableDeclContext into Decl [hand-written]
1 parent 6c0b50c commit f96c18a

File tree

8 files changed

+31
-36
lines changed

8 files changed

+31
-36
lines changed

swift/extractor/translators/DeclTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ void DeclTranslator::fillTypeDecl(const swift::TypeDecl& decl, codeql::TypeDecl&
354354
}
355355

356356
void DeclTranslator::fillIterableDeclContext(const swift::IterableDeclContext& decl,
357-
codeql::IterableDeclContext& entry) {
357+
codeql::Decl& entry) {
358358
entry.members = dispatcher.fetchRepeatedLabels(decl.getAllMembers());
359359
}
360360

swift/extractor/translators/DeclTranslator.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class DeclTranslator : public AstTranslatorBase<DeclTranslator> {
5555
codeql::AbstractFunctionDecl& entry);
5656
void fillOperatorDecl(const swift::OperatorDecl& decl, codeql::OperatorDecl& entry);
5757
void fillTypeDecl(const swift::TypeDecl& decl, codeql::TypeDecl& entry);
58-
void fillIterableDeclContext(const swift::IterableDeclContext& decl,
59-
codeql::IterableDeclContext& entry);
58+
void fillIterableDeclContext(const swift::IterableDeclContext& decl, codeql::Decl& entry);
6059
void fillVarDecl(const swift::VarDecl& decl, codeql::VarDecl& entry);
6160
void fillNominalTypeDecl(const swift::NominalTypeDecl& decl, codeql::NominalTypeDecl& entry);
6261
void fillGenericContext(const swift::GenericContext& decl, codeql::GenericContext& entry);

swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,33 +424,33 @@ private Element interpretElement0(
424424
)
425425
or
426426
// Member functions
427-
exists(NominalTypeDecl nomTypeDecl, IterableDeclContext decl, MethodDecl method |
427+
exists(NominalTypeDecl nomTypeDecl, Decl decl, MethodDecl method |
428428
method.getName() = name and
429429
method = decl.getAMember() and
430430
nomTypeDecl.getFullName() = type and
431431
matchesSignature(method, signature) and
432432
result = method
433433
|
434434
subtypes = true and
435-
decl.getNominalTypeDecl() = nomTypeDecl.getADerivedTypeDecl*()
435+
decl.asNominalTypeDecl() = nomTypeDecl.getADerivedTypeDecl*()
436436
or
437437
subtypes = false and
438-
decl.getNominalTypeDecl() = nomTypeDecl
438+
decl.asNominalTypeDecl() = nomTypeDecl
439439
)
440440
or
441441
// Fields
442442
signature = "" and
443-
exists(NominalTypeDecl nomTypeDecl, IterableDeclContext decl, FieldDecl field |
443+
exists(NominalTypeDecl nomTypeDecl, Decl decl, FieldDecl field |
444444
field.getName() = name and
445445
field = decl.getAMember() and
446446
nomTypeDecl.getFullName() = type and
447447
result = field
448448
|
449449
subtypes = true and
450-
decl.getNominalTypeDecl() = nomTypeDecl.getADerivedTypeDecl*()
450+
decl.asNominalTypeDecl() = nomTypeDecl.getADerivedTypeDecl*()
451451
or
452452
subtypes = false and
453-
decl.getNominalTypeDecl() = nomTypeDecl
453+
decl.asNominalTypeDecl() = nomTypeDecl
454454
)
455455
)
456456
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
private import codeql.swift.generated.decl.Decl
2+
private import codeql.swift.elements.decl.NominalTypeDecl
3+
private import codeql.swift.elements.decl.ExtensionDecl
24

35
class Decl extends Generated::Decl {
46
override string toString() { result = super.toString() }
7+
8+
/**
9+
* Gets the `NominalTypeDecl` corresponding to this `Decl`, if any. This
10+
* resolves an `ExtensionDecl` to the `NominalTypeDecl` that it extends.
11+
*/
12+
NominalTypeDecl asNominalTypeDecl() {
13+
result = this
14+
or
15+
result = this.(ExtensionDecl).getExtendedTypeDecl()
16+
}
17+
18+
/**
19+
* Gets the declaration that declares this declaration as a member, if any.
20+
*/
21+
Decl getDeclaringDecl() { this = result.getAMember() }
522
}

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
private import swift
22

3-
private Decl getAMember(IterableDeclContext ctx) {
3+
private Decl getAMember(Decl ctx) {
44
ctx.getAMember() = result
55
or
66
exists(VarDecl var |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
private import codeql.swift.generated.decl.VarDecl
22
private import codeql.swift.elements.expr.DeclRefExpr
3-
private import codeql.swift.elements.decl.IterableDeclContext
3+
private import codeql.swift.elements.decl.Decl
44

55
class VarDecl extends Generated::VarDecl {
66
override string toString() { result = this.getName() }
@@ -9,5 +9,5 @@ class VarDecl extends Generated::VarDecl {
99
}
1010

1111
class FieldDecl extends VarDecl {
12-
FieldDecl() { this = any(IterableDeclContext ctx).getAMember() }
12+
FieldDecl() { this = any(Decl ctx).getAMember() }
1313
}

swift/schema.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Type(Element):
7777
@group("decl")
7878
class Decl(AstNode):
7979
module: "ModuleDecl"
80+
members: list["Decl"] | child
8081

8182
@group("expr")
8283
class Expr(AstNode):
@@ -95,14 +96,10 @@ class Stmt(AstNode):
9596
class GenericContext(Element):
9697
generic_type_params: list["GenericTypeParamDecl"] | child
9798

98-
@group("decl")
99-
class IterableDeclContext(Element):
100-
members: list[Decl] | child
101-
10299
class EnumCaseDecl(Decl):
103100
elements: list["EnumElementDecl"]
104101

105-
class ExtensionDecl(GenericContext, IterableDeclContext, Decl):
102+
class ExtensionDecl(GenericContext, Decl):
106103
extended_type_decl: "NominalTypeDecl"
107104
protocols: list["ProtocolDecl"]
108105

@@ -303,7 +300,7 @@ class ConcreteVarDecl(VarDecl):
303300
class GenericTypeParamDecl(AbstractTypeParamDecl):
304301
pass
305302

306-
class NominalTypeDecl(GenericTypeDecl, IterableDeclContext):
303+
class NominalTypeDecl(GenericTypeDecl):
307304
type: Type
308305

309306
class OpaqueTypeDecl(GenericTypeDecl):

0 commit comments

Comments
 (0)