Skip to content

Commit c88391f

Browse files
committed
Distinguish between empty match and non-match
1 parent 1ac73cc commit c88391f

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

cpp2rust/converter/mapper.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <llvm/Support/ThreadPool.h>
1010

1111
#include <format>
12+
#include <optional>
1213
#include <regex>
1314
#include <unordered_map>
1415
#include <utility>
@@ -93,7 +94,7 @@ void AddTypeRule(std::string src, TranslationRule::TypeRule &&rule) {
9394
// template_str = "std::vector<T1>::vector()"
9495
// instantiated = "std::vector<int>::vector()"
9596
// result = { "int" }
96-
std::optional<std::vector<std::string>>
97+
std::optional<std::vector<std::optional<std::string>>>
9798
matchTemplate(const std::string &template_str,
9899
const std::string &instantiated) {
99100
auto matchLiteralAt = [&](const std::string &input_str, size_t pos,
@@ -220,7 +221,7 @@ matchTemplate(const std::string &template_str,
220221
return std::string::npos;
221222
};
222223

223-
std::vector<std::string> captured;
224+
std::vector<std::optional<std::string>> captured;
224225

225226
size_t ti = 0;
226227
size_t si = 0;
@@ -250,9 +251,9 @@ matchTemplate(const std::string &template_str,
250251

251252
captured.resize(std::max(captured.size(), type_idx + 1));
252253
auto &repl = captured[type_idx];
253-
if (!repl.empty()) {
254+
if (repl.has_value()) {
254255
size_t end_pos = 0;
255-
if (!matchLiteralAt(instantiated, si, repl, end_pos)) {
256+
if (!matchLiteralAt(instantiated, si, *repl, end_pos)) {
256257
return std::nullopt;
257258
}
258259
si = end_pos;
@@ -338,7 +339,7 @@ matchTemplate(const std::string &template_str,
338339
// types = { {"i32"} }
339340
// tgt_template = "Vec<T1>"
340341
// result = "Vec<i32>"
341-
std::string instantiateTgt(const std::vector<std::string> &types,
342+
std::string instantiateTgt(const std::vector<std::optional<std::string>> &types,
342343
const std::string &tgt_template) {
343344
assert(types.size() <= 9);
344345
std::string instantiated_template = tgt_template;
@@ -351,20 +352,20 @@ std::string instantiateTgt(const std::vector<std::string> &types,
351352
++pos;
352353
continue;
353354
}
354-
auto &repl = types.at(instantiated_template[pos + 1] - '1');
355+
const auto &repl = types.at(instantiated_template[pos + 1] - '1').value();
355356
instantiated_template.replace(pos, 2, repl);
356357
pos += repl.length();
357358
}
358359
return instantiated_template;
359360
}
360361

361362
template <typename T>
362-
std::pair<T *, std::vector<std::string>>
363+
std::pair<T *, std::vector<std::optional<std::string>>>
363364
search(std::unordered_multimap<std::string, T> &map, const std::string &txt,
364365
const std::string &key) {
365366
auto [it, end] = map.equal_range(key);
366367
T *rule = nullptr;
367-
std::vector<std::string> subs;
368+
std::vector<std::optional<std::string>> subs;
368369

369370
for (; it != end; ++it) {
370371
auto &this_rule = it->second;
@@ -540,7 +541,9 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) {
540541
assert(0 && "Type is not present in types_");
541542
}
542543
for (auto &ty : subs) {
543-
ty = mapTypeStringRecursive(ty);
544+
if (ty) {
545+
ty = mapTypeStringRecursive(*ty);
546+
}
544547
}
545548
return instantiateTgt(subs, rule->type_info.type);
546549
}
@@ -608,7 +611,9 @@ std::string InstantiateTemplate(const clang::Expr *expr, unsigned n) {
608611
return text;
609612
}
610613
for (auto &ty : subs) {
611-
ty = mapTypeStringRecursive(ty);
614+
if (ty) {
615+
ty = mapTypeStringRecursive(*ty);
616+
}
612617
}
613618
return instantiateTgt(subs, text);
614619
}
@@ -618,7 +623,9 @@ std::string Map(clang::QualType qual_type) {
618623
auto [rule, subs] = search(types_, type_str, GetTypeMapKey(type_str));
619624
if (rule) {
620625
for (auto &ty : subs) {
621-
ty = mapTypeStringRecursive(ty);
626+
if (ty) {
627+
ty = mapTypeStringRecursive(*ty);
628+
}
622629
}
623630
return instantiateTgt(subs, rule->type_info.type);
624631
}
@@ -651,7 +658,9 @@ std::string GetParamType(const clang::Expr *expr, unsigned index) {
651658
auto expr_str = ToString(expr);
652659
auto [rule, subs] = search(exprs_, expr_str, GetExprMapKey(expr_str));
653660
for (auto &ty : subs) {
654-
ty = mapTypeStringRecursive(ty);
661+
if (ty) {
662+
ty = mapTypeStringRecursive(*ty);
663+
}
655664
}
656665
return instantiateTgt(subs, rule->params.at(index).type);
657666
}

0 commit comments

Comments
 (0)