Skip to content

Commit 24be481

Browse files
authored
Merge pull request github#11429 from github/redsun82/swift-type-mapping
Swift: make mapping from swift types to tags explicit
2 parents ef72e22 + 1b6a501 commit 24be481

File tree

6 files changed

+357
-93
lines changed

6 files changed

+357
-93
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// this is a sanity check that the hierarchy in swift is mapped by the mapping in
2+
// SwiftTypesToTagsMap.def to the hierarchy in schema.py
3+
// We rely on that so that the LabelStore will preserve correct typing.
4+
// For a class Derived: Base we could store the label for a Derived* pointer, and then fetch the
5+
// label for the same pointer as Base*. If the mapping did not preserve inheritance, we would end up
6+
// using a trap key of the DB type associated with Derived in a position expecting the incompatible
7+
// DB type associated with Base.
8+
9+
#include "swift/extractor/infra/SwiftTagTraits.h"
10+
11+
using namespace codeql;
12+
13+
#define CHECK(KIND, TYPE, PARENT) \
14+
static_assert(std::is_same_v<TrapTagOf<swift::TYPE##KIND>, void> || \
15+
std::is_base_of_v<TrapTagOf<swift::PARENT>, TrapTagOf<swift::TYPE##KIND>>, \
16+
"Tag of " #PARENT " must be a base of the tag of " #TYPE #KIND);
17+
#define CHECK_CONCRETE(KIND, TYPE, PARENT) \
18+
CHECK(KIND, TYPE, PARENT) \
19+
static_assert( \
20+
std::is_same_v<TrapTagOf<swift::TYPE##KIND>, void> || \
21+
std::is_base_of_v<TrapTagOf<swift::TYPE##KIND>, ConcreteTrapTagOf<swift::TYPE##KIND>>, \
22+
"Tag of " #TYPE #KIND " must be a base of its concrete tag");
23+
24+
#define STMT(CLASS, PARENT) CHECK_CONCRETE(Stmt, CLASS, PARENT)
25+
#define ABSTRACT_STMT(CLASS, PARENT) CHECK(Stmt, CLASS, PARENT)
26+
#include <swift/AST/StmtNodes.def>
27+
28+
#define EXPR(CLASS, PARENT) CHECK_CONCRETE(Expr, CLASS, PARENT)
29+
#define ABSTRACT_EXPR(CLASS, PARENT) CHECK(Expr, CLASS, PARENT)
30+
#include <swift/AST/ExprNodes.def>
31+
32+
#define DECL(CLASS, PARENT) CHECK_CONCRETE(Decl, CLASS, PARENT)
33+
#define ABSTRACT_DECL(CLASS, PARENT) CHECK(Decl, CLASS, PARENT)
34+
#include <swift/AST/DeclNodes.def>
35+
36+
#define PATTERN(CLASS, PARENT) CHECK_CONCRETE(Pattern, CLASS, PARENT)
37+
#define ABSTRACT_PATTERN(CLASS, PARENT) CHECK(Pattern, CLASS, PARENT)
38+
#include <swift/AST/PatternNodes.def>
39+
40+
#define TYPE(CLASS, PARENT) CHECK_CONCRETE(Type, CLASS, PARENT)
41+
#define ABSTRACT_TYPE(CLASS, PARENT) CHECK(Type, CLASS, PARENT)
42+
#include <swift/AST/TypeNodes.def>

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,16 @@ class SwiftDispatcher {
179179
// it actually gets emitted to handle recursive cases such as recursive calls, or recursive type
180180
// declarations
181181
template <typename E, typename... Args, std::enable_if_t<IsStorable<E>>* = nullptr>
182-
TrapLabelOf<E> assignNewLabel(const E& e, Args&&... args) {
182+
TrapLabel<ConcreteTrapTagOf<E>> assignNewLabel(const E& e, Args&&... args) {
183183
assert(waitingForNewLabel == Store::Handle{e} && "assignNewLabel called on wrong entity");
184-
auto label = trap.createLabel<TrapTagOf<E>>(std::forward<Args>(args)...);
184+
auto label = trap.createLabel<ConcreteTrapTagOf<E>>(std::forward<Args>(args)...);
185185
store.insert(e, label);
186186
waitingForNewLabel = std::monostate{};
187187
return label;
188188
}
189189

190190
template <typename E, typename... Args, std::enable_if_t<IsStorable<E*>>* = nullptr>
191-
TrapLabelOf<E> assignNewLabel(const E& e, Args&&... args) {
191+
TrapLabel<ConcreteTrapTagOf<E>> assignNewLabel(const E& e, Args&&... args) {
192192
return assignNewLabel(&e, std::forward<Args>(args)...);
193193
}
194194

0 commit comments

Comments
 (0)