Skip to content

Commit 631156d

Browse files
committed
Swift: extract Nested- and PrimaryArchetypeType
1 parent 6230a3a commit 631156d

25 files changed

+233
-29
lines changed

swift/codegen/schema.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ WeakStorageType:
239239

240240
ArchetypeType:
241241
_extends: SubstitutableType
242+
name: string
243+
interface_type: Type
244+
superclass: Type?
245+
protocols: ProtocolDecl*
242246

243247
GenericTypeParamType:
244248
_extends: SubstitutableType
@@ -694,6 +698,8 @@ BuiltinIntegerType:
694698

695699
NestedArchetypeType:
696700
_extends: ArchetypeType
701+
parent: ArchetypeType
702+
associated_type_declaration: AssociatedTypeDecl
697703

698704
SequenceArchetypeType:
699705
_extends: ArchetypeType
@@ -706,7 +712,6 @@ OpenedArchetypeType:
706712

707713
PrimaryArchetypeType:
708714
_extends: ArchetypeType
709-
interface_type: GenericTypeParamType
710715

711716
DictionaryType:
712717
_extends: SyntaxSugarType

swift/extractor/visitors/TypeVisitor.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ void TypeVisitor::visitLValueType(swift::LValueType* type) {
166166
dispatcher_.emit(LValueTypesTrap{label, dispatcher_.fetchLabel(type->getObjectType())});
167167
}
168168

169-
void TypeVisitor::visitPrimaryArchetypeType(swift::PrimaryArchetypeType* type) {
170-
auto label = dispatcher_.assignNewLabel(type);
171-
assert(type->getInterfaceType() && "expect PrimaryArchetypeType to have InterfaceType");
172-
dispatcher_.emit(
173-
PrimaryArchetypeTypesTrap{label, dispatcher_.fetchLabel(type->getInterfaceType())});
169+
codeql::PrimaryArchetypeType TypeVisitor::translatePrimaryArchetypeType(
170+
const swift::PrimaryArchetypeType& type) {
171+
PrimaryArchetypeType entry{dispatcher_.assignNewLabel(type)};
172+
fillArchetypeType(type, entry);
173+
return entry;
174174
}
175175

176176
void TypeVisitor::visitUnboundGenericType(swift::UnboundGenericType* type) {
@@ -232,4 +232,26 @@ void TypeVisitor::emitAnyGenericType(swift::AnyGenericType* type,
232232
}
233233
}
234234

235+
codeql::NestedArchetypeType TypeVisitor::translateNestedArchetypeType(
236+
const swift::NestedArchetypeType& type) {
237+
codeql::NestedArchetypeType entry{dispatcher_.assignNewLabel(type)};
238+
entry.parent = dispatcher_.fetchLabel(type.getParent());
239+
entry.associated_type_declaration = dispatcher_.fetchLabel(type.getAssocType());
240+
fillArchetypeType(type, entry);
241+
return entry;
242+
}
243+
244+
void TypeVisitor::fillType(const swift::TypeBase& type, codeql::Type& entry) {
245+
entry.diagnostics_name = type.getString();
246+
entry.canonical_type = dispatcher_.fetchLabel(type.getCanonicalType());
247+
}
248+
249+
void TypeVisitor::fillArchetypeType(const swift::ArchetypeType& type, ArchetypeType& entry) {
250+
entry.interface_type = dispatcher_.fetchLabel(type.getInterfaceType());
251+
entry.name = type.getName().str().str();
252+
entry.protocols = dispatcher_.fetchRepeatedLabels(type.getConformsTo());
253+
entry.superclass = dispatcher_.fetchOptionalLabel(type.getSuperclass());
254+
fillType(type, entry);
255+
}
256+
235257
} // namespace codeql

swift/extractor/visitors/TypeVisitor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
3333
void visitGenericFunctionType(swift::GenericFunctionType* type);
3434
void visitGenericTypeParamType(swift::GenericTypeParamType* type);
3535
void visitLValueType(swift::LValueType* type);
36-
void visitPrimaryArchetypeType(swift::PrimaryArchetypeType* type);
3736
void visitUnboundGenericType(swift::UnboundGenericType* type);
3837
void visitBoundGenericType(swift::BoundGenericType* type);
38+
codeql::PrimaryArchetypeType translatePrimaryArchetypeType(
39+
const swift::PrimaryArchetypeType& type);
40+
codeql::NestedArchetypeType translateNestedArchetypeType(const swift::NestedArchetypeType& type);
3941

4042
private:
43+
void fillType(const swift::TypeBase& type, codeql::Type& entry);
44+
void fillArchetypeType(const swift::ArchetypeType& type, codeql::ArchetypeType& entry);
4145
void emitUnarySyntaxSugarType(const swift::UnarySyntaxSugarType* type,
4246
TrapLabel<UnarySyntaxSugarTypeTag> label);
4347
void emitAnyFunctionType(const swift::AnyFunctionType* type, TrapLabel<AnyFunctionTypeTag> label);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.type.ArchetypeType
2+
private import codeql.swift.elements.type.ClassType
33

4-
class ArchetypeType extends ArchetypeTypeBase { }
4+
class ArchetypeType extends ArchetypeTypeBase {
5+
override ClassType getSuperclass() { result = super.getSuperclass() }
6+
}
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
// generated by codegen/codegen.py
2+
import codeql.swift.elements.decl.ProtocolDecl
23
import codeql.swift.elements.type.SubstitutableType
4+
import codeql.swift.elements.type.Type
35

4-
class ArchetypeTypeBase extends @archetype_type, SubstitutableType { }
6+
class ArchetypeTypeBase extends @archetype_type, SubstitutableType {
7+
string getName() { archetype_types(this, result, _) }
8+
9+
Type getInterfaceType() {
10+
exists(Type x |
11+
archetype_types(this, _, x) and
12+
result = x.resolve()
13+
)
14+
}
15+
16+
Type getSuperclass() {
17+
exists(Type x |
18+
archetype_type_superclasses(this, x) and
19+
result = x.resolve()
20+
)
21+
}
22+
23+
predicate hasSuperclass() { exists(getSuperclass()) }
24+
25+
ProtocolDecl getProtocol(int index) {
26+
exists(ProtocolDecl x |
27+
archetype_type_protocols(this, index, x) and
28+
result = x.resolve()
29+
)
30+
}
31+
32+
ProtocolDecl getAProtocol() { result = getProtocol(_) }
33+
34+
int getNumberOfProtocols() { result = count(getAProtocol()) }
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
// generated by codegen/codegen.py
22
import codeql.swift.elements.type.ArchetypeType
3+
import codeql.swift.elements.decl.AssociatedTypeDecl
34

45
class NestedArchetypeTypeBase extends @nested_archetype_type, ArchetypeType {
56
override string getAPrimaryQlClass() { result = "NestedArchetypeType" }
7+
8+
ArchetypeType getParent() {
9+
exists(ArchetypeType x |
10+
nested_archetype_types(this, x, _) and
11+
result = x.resolve()
12+
)
13+
}
14+
15+
AssociatedTypeDecl getAssociatedTypeDeclaration() {
16+
exists(AssociatedTypeDecl x |
17+
nested_archetype_types(this, _, x) and
18+
result = x.resolve()
19+
)
20+
}
621
}
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.type.ArchetypeType
3-
import codeql.swift.elements.type.GenericTypeParamType
43

54
class PrimaryArchetypeTypeBase extends @primary_archetype_type, ArchetypeType {
65
override string getAPrimaryQlClass() { result = "PrimaryArchetypeType" }
7-
8-
GenericTypeParamType getInterfaceType() {
9-
exists(GenericTypeParamType x |
10-
primary_archetype_types(this, x) and
11-
result = x.resolve()
12-
)
13-
}
146
}

swift/ql/lib/swift.dbscheme

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,26 @@ weak_storage_types( //dir=type
574574
| @sequence_archetype_type
575575
;
576576

577+
#keyset[id]
578+
archetype_types( //dir=type
579+
int id: @archetype_type ref,
580+
string name: string ref,
581+
int interface_type: @type ref
582+
);
583+
584+
#keyset[id]
585+
archetype_type_superclasses( //dir=type
586+
int id: @archetype_type ref,
587+
int superclass: @type ref
588+
);
589+
590+
#keyset[id, index]
591+
archetype_type_protocols( //dir=type
592+
int id: @archetype_type ref,
593+
int index: int ref,
594+
int protocol: @protocol_decl ref
595+
);
596+
577597
generic_type_param_types( //dir=type
578598
unique int id: @generic_type_param_type,
579599
string name: string ref
@@ -1357,7 +1377,9 @@ builtin_integer_type_widths( //dir=type
13571377
);
13581378

13591379
nested_archetype_types( //dir=type
1360-
unique int id: @nested_archetype_type
1380+
unique int id: @nested_archetype_type,
1381+
int parent: @archetype_type ref,
1382+
int associated_type_declaration: @associated_type_decl ref
13611383
);
13621384

13631385
sequence_archetype_types( //dir=type
@@ -1373,8 +1395,7 @@ opened_archetype_types( //dir=type
13731395
);
13741396

13751397
primary_archetype_types( //dir=type
1376-
unique int id: @primary_archetype_type,
1377-
int interface_type: @generic_type_param_type ref
1398+
unique int id: @primary_archetype_type
13781399
);
13791400

13801401
dictionary_types( //dir=type

swift/ql/test/TestUtils.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ predicate toBeTested(Element e) {
1212
e = loc.(ValueDecl).getInterfaceType()
1313
or
1414
e = loc.(NominalTypeDecl).getType()
15+
or
16+
e = loc.(VarDecl).getType()
17+
or
18+
e = loc.(Expr).getType()
1519
)
1620
)
1721
}

swift/ql/test/extractor-tests/generated/type/NestedArchetypeType/MISSING_SOURCE.txt

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

0 commit comments

Comments
 (0)