-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathemplace_back.cpp
More file actions
194 lines (161 loc) · 5.68 KB
/
Copy pathemplace_back.cpp
File metadata and controls
194 lines (161 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.
#include <clang/Sema/Initialization.h>
#include "converter/converter_lib.h"
#include "converter/mapper.h"
#include "converter/models/converter_refcount.h"
namespace cpp2rust {
bool Converter::emplace_back_plugin_match(clang::CallExpr *call) {
if (auto member_call = clang::dyn_cast<clang::CXXMemberCallExpr>(call)) {
return Mapper::ToString(member_call).contains("emplace_back");
}
return false;
}
namespace {
clang::QualType getElemTypeFromEmplaceObj(clang::CXXMemberCallExpr *call) {
auto *obj = GetCallObject(call);
if (!obj) {
return clang::QualType();
}
auto base = obj->IgnoreParenImpCasts()
->getType()
.getNonReferenceType()
.getUnqualifiedType();
while (base->isPointerType()) {
base = base->getPointeeType().getUnqualifiedType();
}
if (auto record_ty = base->getAs<clang::RecordType>()) {
if (auto template_specialization =
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
record_ty->getDecl())) {
auto &args = template_specialization->getTemplateArgs();
if (args.size() && args[0].getKind() == clang::TemplateArgument::Type) {
return args[0].getAsType().getUnqualifiedType();
}
}
}
return clang::QualType();
}
clang::InitializationKind
buildInitKindAndArgs(clang::CXXMemberCallExpr *call,
llvm::SmallVector<clang::Expr *, 4> &args) {
if (call->getNumArgs() == 1 &&
clang::isa<clang::InitListExpr>(call->getArg(0)->IgnoreParenImpCasts())) {
args.push_back(const_cast<clang::Expr *>(call->getArg(0)));
return clang::InitializationKind::CreateDirectList(call->getExprLoc());
} else {
for (unsigned i = 0; i < call->getNumArgs(); ++i) {
args.push_back(const_cast<clang::Expr *>(call->getArg(i)));
}
return clang::InitializationKind::CreateDirect(call->getExprLoc(), {}, {});
}
}
clang::Expr *performInitAndPeel(clang::Sema &sema, clang::QualType elem_ty,
clang::InitializationKind &kind,
llvm::SmallVectorImpl<clang::Expr *> &args) {
auto ent = clang::InitializedEntity::InitializeTemporary(elem_ty);
clang::InitializationSequence seq(sema, ent, kind, args);
if (!seq) {
return nullptr;
}
auto result = seq.Perform(sema, ent, kind, args);
if (result.isInvalid()) {
return nullptr;
}
auto expr = result.get();
while (true) {
expr = expr->IgnoreParenImpCasts();
if (auto materialize_temp =
clang::dyn_cast<clang::MaterializeTemporaryExpr>(expr)) {
expr = materialize_temp->getSubExpr();
continue;
}
if (auto bind_temp = clang::dyn_cast<clang::CXXBindTemporaryExpr>(expr)) {
expr = bind_temp->getSubExpr();
continue;
}
break;
}
return expr;
}
std::pair<clang::QualType, const clang::CXXConstructorDecl *>
analyzeEmplaceCall(clang::CXXMemberCallExpr *call, clang::Sema &sema) {
auto elem_ty = getElemTypeFromEmplaceObj(call);
const clang::CXXConstructorDecl *ctor = nullptr;
if (elem_ty.isNull()) {
return {clang::QualType(), nullptr};
}
if (!elem_ty->isRecordType()) {
return {elem_ty, nullptr};
}
llvm::SmallVector<clang::Expr *, 4> args;
auto kind = buildInitKindAndArgs(call, args);
if (auto expr = performInitAndPeel(sema, elem_ty, kind, args)) {
if (auto ctor_expr = clang::dyn_cast<clang::CXXConstructExpr>(expr)) {
ctor = ctor_expr->getConstructor();
}
}
return {elem_ty, ctor};
}
clang::CXXConstructExpr *buildConstructExpr(clang::CXXMemberCallExpr *call,
clang::Sema &sema) {
auto [elem_ty, /*expected*/ ctor] = analyzeEmplaceCall(call, sema);
if (elem_ty.isNull() || !elem_ty->isRecordType()) {
return nullptr;
}
llvm::SmallVector<clang::Expr *, 4> args;
auto kind = buildInitKindAndArgs(call, args);
return clang::dyn_cast_or_null<clang::CXXConstructExpr>(
performInitAndPeel(sema, elem_ty, kind, args));
}
} // namespace
void Converter::emplace_back_emit_push_open(clang::CXXMemberCallExpr *call) {
{
PushExprKind push(*this, ExprKind::LValue);
StrCat(ReplaceAll(ToString(call->getCallee()), "emplace_back", "push"));
}
StrCat('(');
}
void Converter::emplace_back_emit_push_close(clang::CXXMemberCallExpr *call) {
StrCat(')');
}
bool Converter::emplace_back_plugin_convert(clang::CallExpr *call) {
auto member_call = clang::dyn_cast<clang::CXXMemberCallExpr>(call);
assert(member_call);
auto [elem_ty, ctor] = analyzeEmplaceCall(member_call, GetSema());
assert(!elem_ty.isNull() && "Could not analyze emplace_back type");
emplace_back_emit_push_open(member_call);
if (ctor) {
auto is_argument_moved = false;
if (call->getNumArgs() > 0) {
if (auto arg_call = clang::dyn_cast<clang::CallExpr>(call->getArg(0))) {
is_argument_moved = arg_call->isCallToStdMove();
}
}
if (is_argument_moved) {
StrCat("std::mem::take(&mut");
}
emplace_back_plugin_construct_arg(
elem_ty, buildConstructExpr(member_call, GetSema()));
if (is_argument_moved) {
StrCat(')');
}
} else if (elem_ty.isPODType(ctx_)) {
if (call->getNumArgs() == 0) {
StrCat(GetDefaultAsString(elem_ty));
} else {
assert(call->getNumArgs() == 1 &&
"multiple arguments passed for building POD type");
Convert(call->getArg(0));
StrCat("as");
StrCat(GetUnsafeTypeAsString(elem_ty));
}
} else {
call->dump();
assert(0 && "no ctor and no pod type");
return false;
}
emplace_back_emit_push_close(member_call);
return true;
}
} // namespace cpp2rust