Skip to content

Commit 9231013

Browse files
committed
Swift: use C++ entry style visitor in DeclVisitor
1 parent 42ec635 commit 9231013

File tree

8 files changed

+185
-203
lines changed

8 files changed

+185
-203
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ File:
1919
name: string
2020

2121
Locatable:
22-
location: Location
22+
location: Location?
2323

2424
Location:
2525
file: File

swift/codegen/templates/cpp_classes.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <vector>
88

99
#include "{{include_dir}}/{{trap_affix}}Label.h"
10+
#include "{{include_dir}}/{{trap_affix}}TagTraits.h"
1011
#include "./{{trap_affix}}Entries.h"
1112

1213
namespace {{namespace}} {
@@ -54,5 +55,10 @@ struct {{name}}{{#final}} : Binding<{{name}}Tag>{{#bases}}, {{ref.name}}{{/bases
5455
{{/fields}}
5556
}
5657
};
58+
59+
template <>
60+
struct detail::ToTrapClassFunctor<{{name}}Tag> {
61+
using type = {{name}};
62+
};
5763
{{/classes}}
5864
}

swift/extractor/SwiftDispatcher.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ class SwiftDispatcher {
120120
locatableLabel);
121121
}
122122

123+
// return `std::optional(fetchLabel(arg))` if arg converts to true, otherwise std::nullopt
124+
template <typename Arg>
125+
auto fetchOptionalLabel(Arg&& arg)
126+
-> std::optional<decltype(fetchLabel(std::forward<Arg>(arg)))> {
127+
if (arg) {
128+
return fetchLabel(std::forward<Arg>(arg));
129+
}
130+
return std::nullopt;
131+
}
132+
133+
// map `fetchLabel` on the iterable `arg`, returning a vector of all labels
134+
template <typename Iterable>
135+
auto fetchRepeatedLabels(Iterable&& arg) -> std::vector<decltype(fetchLabel(*arg.begin()))> {
136+
std::vector<decltype(fetchLabel(*arg.begin()))> ret;
137+
ret.reserve(arg.size());
138+
for (const auto& e : arg) {
139+
ret.push_back(fetchLabel(e));
140+
}
141+
return ret;
142+
}
143+
123144
private:
124145
// types to be supported by assignNewLabel/fetchLabel need to be listed here
125146
using Store = TrapLabelStore<swift::Decl,
@@ -147,7 +168,7 @@ class SwiftDispatcher {
147168
auto locLabel = createLabel<LocationTag>('{', fileLabel, "}:", startLine, ':', startColumn, ':',
148169
endLine, ':', endColumn);
149170
trap.emit(LocationsTrap{locLabel, fileLabel, startLine, startColumn, endLine, endColumn});
150-
trap.emit(LocatablesTrap{locatableLabel, locLabel});
171+
trap.emit(LocatableLocationsTrap{locatableLabel, locLabel});
151172
}
152173

153174
template <typename Tag, typename... Ts>

swift/extractor/trap/TrapTagTraits.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ struct ToTagOverride : ToTagFunctor<T> {};
1919
// must be instantiated to map trap labels to the corresponding generated binding trap entry
2020
template <typename Label>
2121
struct ToBindingTrapFunctor;
22+
23+
// must be instantiated to map trap tags to the corresponding generated trap class
24+
template <typename Tag>
25+
struct ToTrapClassFunctor;
2226
} // namespace detail
2327

2428
template <typename T>
@@ -30,4 +34,7 @@ using TrapLabelOf = TrapLabel<TrapTagOf<T>>;
3034
template <typename T>
3135
using BindingTrapOf = typename detail::ToBindingTrapFunctor<TrapLabelOf<T>>::type;
3236

37+
template <typename T>
38+
using TrapClassOf = typename detail::ToTrapClassFunctor<TrapTagOf<T>>::type;
39+
3340
} // namespace codeql

0 commit comments

Comments
 (0)