Skip to content

Commit a929df9

Browse files
committed
Add ConvertPointeeType
1 parent f138af5 commit a929df9

6 files changed

Lines changed: 32 additions & 19 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,21 +1391,6 @@ void Converter::EmitFnPtrCall(clang::Expr *callee) {
13911391
StrCat(").unwrap()");
13921392
}
13931393

1394-
std::string Converter::GetPointeeRustType(clang::QualType ptr_type) {
1395-
auto pointee = ptr_type->getPointeeType();
1396-
if (pointee->isIntegerType()) {
1397-
return ToString(pointee);
1398-
}
1399-
auto str = ToString(ptr_type);
1400-
while (!str.empty() && std::isspace(str.back())) {
1401-
str.pop_back();
1402-
}
1403-
if (str.starts_with("Ptr<") && str.ends_with(">")) {
1404-
return str.substr(4, str.size() - 5);
1405-
}
1406-
return ToString(pointee);
1407-
}
1408-
14091394
void Converter::ConvertFunctionToFunctionPointer(
14101395
const clang::FunctionDecl *fn_decl) {
14111396
StrCat(std::format("Some({})", Mapper::MapFunctionName(fn_decl)));

cpp2rust/converter/converter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
207207

208208
virtual void EmitFnPtrCall(clang::Expr *callee);
209209

210-
std::string GetPointeeRustType(clang::QualType ptr_type);
211-
212210
virtual void
213211
ConvertFunctionToFunctionPointer(const clang::FunctionDecl *fn_decl);
214212

cpp2rust/converter/converter_lib.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include <clang/AST/ParentMapContext.h>
99
#include <clang/Basic/SourceManager.h>
1010

11+
#include <algorithm>
1112
#include <array>
13+
#include <cctype>
1214
#include <filesystem>
1315
#include <unordered_set>
1416

@@ -660,4 +662,18 @@ clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx) {
660662
/*BasePath=*/nullptr, clang::VK_PRValue, clang::FPOptionsOverride());
661663
}
662664

665+
static void Trim(std::string &s) {
666+
auto is_space = [](unsigned char c) { return std::isspace(c); };
667+
s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), is_space));
668+
s.erase(std::find_if_not(s.rbegin(), s.rend(), is_space).base(), s.end());
669+
}
670+
671+
void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix) {
672+
Trim(s);
673+
if (s.size() >= prefix.size() + suffix.size() && s.starts_with(prefix) &&
674+
s.ends_with(suffix)) {
675+
s = s.substr(prefix.size(), s.size() - prefix.size() - suffix.size());
676+
}
677+
}
678+
663679
} // namespace cpp2rust

cpp2rust/converter/converter_lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <optional>
1313
#include <regex>
1414
#include <string>
15+
#include <string_view>
1516
#include <vector>
1617

1718
namespace cpp2rust {
@@ -154,4 +155,6 @@ bool ContainsVAArgExpr(const clang::Stmt *stmt);
154155

155156
clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx);
156157

158+
void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);
159+
157160
} // namespace cpp2rust

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ std::string ConverterRefCount::BuildFnAdapter(
205205
} else if (src_pty->isPointerType() && tgt_pty->isPointerType()) {
206206
if (tgt_pty->isVoidPointerType()) {
207207
closure += std::format("a{}.cast::<{}>().unwrap()", i,
208-
GetPointeeRustType(src_pty));
208+
ConvertPointeeType(src_pty));
209209
} else if (src_pty->isVoidPointerType()) {
210210
closure += std::format("a{}.to_any()", i);
211211
} else if (tgt_pty->getPointeeType()->isCharType()) {
212212
closure += std::format("a{}.reinterpret_cast::<{}>()", i,
213-
GetPointeeRustType(src_pty));
213+
ConvertPointeeType(src_pty));
214214
} else if (src_pty->getPointeeType()->isCharType()) {
215215
closure += std::format("a{}.reinterpret_cast::<u8>()", i);
216216
}
@@ -2158,4 +2158,14 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
21582158
return std::format("{}.with_mut(|__v: {}| __v{})", ptr, param_type, body);
21592159
}
21602160

2161+
std::string ConverterRefCount::ConvertPointeeType(clang::QualType ptr_type) {
2162+
// Pointee of a pointer to incomplete type is an incomplete type that does
2163+
// not have a translation rule. Hence ToString(ptr_type->getPointeeType()) is
2164+
// not enough
2165+
assert(ptr_type->isPointerType());
2166+
auto str = ToString(ptr_type);
2167+
Unwrap(str, "Ptr<", ">");
2168+
return str;
2169+
}
2170+
21612171
} // namespace cpp2rust

cpp2rust/converter/models/converter_refcount.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class ConverterRefCount final : public Converter {
205205
std::string ConvertFreshPointer(clang::Expr *expr) override;
206206

207207
std::string ConvertPtrType(clang::QualType type);
208+
std::string ConvertPointeeType(clang::QualType ptr_type);
208209

209210
/// The kind of conversion that should be performed.
210211
enum class ConversionKind : uint8_t {

0 commit comments

Comments
 (0)