Skip to content

Commit 84e15b2

Browse files
committed
edits
1 parent a0eef7d commit 84e15b2

6 files changed

Lines changed: 23 additions & 15 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,9 +3582,9 @@ std::string Converter::ConvertIRFragment(
35823582
if (auto *t = std::get_if<TextFragment>(&frag)) {
35833583
result += t->text;
35843584
} else if (auto *g = std::get_if<GenericFragment>(&frag)) {
3585-
result += Mapper::InstantiateTemplate(GetCalleeOrExpr(expr), g->name);
3585+
result += Mapper::InstantiateTemplate(GetCalleeOrExpr(expr), g->n);
35863586
} else if (auto *ph = std::get_if<PlaceholderFragment>(&frag)) {
3587-
auto arg_idx = std::stoi(ph->arg.substr(1)); // "a0" -> 0
3587+
auto arg_idx = ph->n;
35883588
assert(arg_idx < static_cast<int>(all_args.size()));
35893589
auto *arg = all_args[arg_idx];
35903590
bool is_receiver = HasReceiver(expr) && arg_idx == 0;
@@ -3593,7 +3593,7 @@ std::string Converter::ConvertIRFragment(
35933593
.param_type = Mapper::GetParamType(GetCalleeOrExpr(expr), arg_idx),
35943594
.materialize_ctx = ctx,
35953595
.materialize_idx =
3596-
is_receiver ? -1 : (arg_idx - (HasReceiver(expr) ? 1 : 0)),
3596+
is_receiver ? -1 : ((int)arg_idx - HasReceiver(expr)),
35973597
.access = ph->access,
35983598
.is_receiver = is_receiver,
35993599
.is_cpp_ptr = arg->getType()->isPointerType(),

cpp2rust/converter/mapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ std::string GetMapKey(const std::string &str) {
4747
return str.substr(0, n);
4848
}
4949
// something like int[][] or T1[] -> []
50-
return str.substr(n+1);
50+
return str.substr(n + 1);
5151
}
5252

5353
void AddTypeRule(std::string src, TranslationRule::TypeRule &&rule) {
@@ -628,8 +628,8 @@ std::string MapFunctionName(const clang::FunctionDecl *decl) {
628628
return GetNamedDeclAsString(decl->getCanonicalDecl());
629629
}
630630

631-
std::string InstantiateTemplate(const clang::Expr *expr,
632-
const std::string &text) {
631+
std::string InstantiateTemplate(const clang::Expr *expr, unsigned n) {
632+
auto text = 'T' + std::to_string(n);
633633
auto it = search(expr);
634634
if (it == exprs_.end()) {
635635
return text;

cpp2rust/converter/mapper.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ bool Contains(const clang::Expr *expr);
3232
std::string Map(clang::QualType qual_type);
3333
const TranslationRule::ExprTgt *GetExprTgt(const clang::Expr *expr);
3434
std::string MapFunctionName(const clang::FunctionDecl *decl);
35-
std::string InstantiateTemplate(const clang::Expr *expr,
36-
const std::string &text);
35+
std::string InstantiateTemplate(const clang::Expr *expr, unsigned n);
3736
bool ReturnsPointer(const clang::Expr *expr);
3837
std::string GetParamType(const clang::Expr *expr, unsigned index);
3938
bool ParamIsPointer(const clang::Expr *expr, unsigned index);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
21942194
return Converter::ConvertMappedMethodCall(expr, mc, args, num_args, ctx);
21952195
}
21962196

2197-
auto arg_idx = std::stoi(receiver_ph->arg.substr(1));
2197+
auto arg_idx = receiver_ph->n;
21982198
auto *arg = BuildUnifiedArgs(expr, args, num_args)[arg_idx];
21992199

22002200
if (!arg->getType()->isPointerType() && !IsReferenceType(arg)) {

cpp2rust/converter/translation_rule.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,12 @@ TextFragment ParseTextFragmentJSON(const llvm::json::Object &obj) {
733733
}
734734

735735
GenericFragment ParseGenericFragmentJSON(const llvm::json::Object &obj) {
736-
return {obj.getString("generic")->str()};
736+
auto s = obj.getString("generic");
737+
if (s->size() < 2 || s->front() != 'T') {
738+
llvm::errs() << "Invalid generic fragment: " << s << '\n';
739+
assert(0);
740+
}
741+
return {(unsigned)std::atoi(s->data() + 1)};
737742
}
738743

739744
TypeInfo ParseTypeInfoJSON(const llvm::json::Object &obj) {
@@ -767,7 +772,11 @@ ParsePlaceholderFragmentJSON(const llvm::json::Object &obj) {
767772
auto arg = obj.getString("arg");
768773
auto access = obj.getString("access");
769774
assert(arg && access);
770-
return {arg->str(), ParseAccessJSON(*access)};
775+
if (arg->size() < 2 || arg->front() != 'a') {
776+
llvm::errs() << "Invalid arg fragment: " << arg << '\n';
777+
assert(0);
778+
}
779+
return {(unsigned)atoi(arg->data() + 1), ParseAccessJSON(*access)};
771780
}
772781

773782
std::vector<BodyFragment> ParseBodyFragmentsJSON(const llvm::json::Array &arr);
@@ -938,7 +947,7 @@ void TextFragment::dump() const {
938947
}
939948

940949
void PlaceholderFragment::dump() const {
941-
llvm::errs() << " placeholder: " << arg << " (";
950+
llvm::errs() << " placeholder: " << n << " (";
942951
switch (access) {
943952
case Access::kRead:
944953
llvm::errs() << "read\n";
@@ -997,7 +1006,7 @@ void ExprTgt::dump() const {
9971006
}
9981007

9991008
void GenericFragment::dump() const {
1000-
llvm::errs() << " generic: " << name << '\n';
1009+
llvm::errs() << " generic: " << n << '\n';
10011010
}
10021011

10031012
void TypeInfo::dump() const {

cpp2rust/converter/translation_rule.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ struct TextFragment {
2424
enum class Access { kRead, kWrite, kMove };
2525

2626
struct PlaceholderFragment {
27-
std::string arg; // "a0", "a1", ...
27+
unsigned n; // "a0", "a1", ...
2828
Access access;
2929

3030
void dump() const;
3131
};
3232

3333
struct GenericFragment {
34-
std::string name; // "T1", "T2", ...
34+
unsigned n; // "T1", "T2", ...
3535

3636
void dump() const;
3737
};

0 commit comments

Comments
 (0)