Skip to content

Commit 53dd125

Browse files
committed
Reorder functions to make the diff smaller
1 parent cfb6907 commit 53dd125

4 files changed

Lines changed: 96 additions & 96 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ RsExpr *Converter::Convert(clang::QualType qual_type) {
117117
return Text("");
118118
}
119119

120+
std::string Converter::RenderType(clang::QualType qual_type) {
121+
return std::string(Trim(Convert(qual_type)->print()));
122+
}
123+
124+
RsExpr *Converter::ConvertPointeeType(clang::QualType ptr_type) {
125+
assert(!ptr_type.isNull() && ptr_type->isPointerType());
126+
auto pointee = ptr_type->getPointeeType();
127+
if (!pointee->isRecordType()) {
128+
return Convert(pointee);
129+
}
130+
131+
auto str = RenderType(ptr_type);
132+
Unwrap(str, "*mut ", "");
133+
Unwrap(str, "*const ", "");
134+
return Text(std::move(str));
135+
}
136+
120137
RsExpr *Converter::VisitBuiltinType(const clang::BuiltinType *type) {
121138
switch (type->getKind()) {
122139
case clang::BuiltinType::Bool:
@@ -197,8 +214,22 @@ Converter::VisitLValueReferenceType(const clang::LValueReferenceType *type) {
197214
Convert(pointee_type));
198215
}
199216

200-
RsExpr *Converter::VisitDecayedType(const clang::DecayedType *type) {
201-
return Convert(type->getDecayedType());
217+
RsExpr *
218+
Converter::ConvertFunctionPointerType(const clang::FunctionProtoType *proto,
219+
FnProtoType kind) {
220+
std::vector<RsExpr *> parts;
221+
parts.push_back(
222+
Text(kind == FnProtoType::LambdaCallOperator ? "impl Fn(" : "fn("));
223+
for (auto p_ty : proto->param_types()) {
224+
parts.push_back(Convert(p_ty));
225+
parts.push_back(Text(','));
226+
}
227+
parts.push_back(Text(')'));
228+
if (!proto->getReturnType()->isVoidType()) {
229+
parts.push_back(Text("->"));
230+
parts.push_back(Convert(proto->getReturnType()));
231+
}
232+
return arena_.New<Concat>(std::move(parts));
202233
}
203234

204235
RsExpr *Converter::VisitPointerType(const clang::PointerType *type) {
@@ -222,6 +253,10 @@ RsExpr *Converter::VisitPointerType(const clang::PointerType *type) {
222253
return arena_.New<Concat>(std::move(parts));
223254
}
224255

256+
RsExpr *Converter::VisitDecayedType(const clang::DecayedType *type) {
257+
return Convert(type->getDecayedType());
258+
}
259+
225260
RsExpr *Converter::VisitTypedefType(const clang::TypedefType *type) {
226261
return Convert(type->desugar());
227262
}
@@ -230,41 +265,6 @@ RsExpr *Converter::VisitUsingType(const clang::UsingType *type) {
230265
return Convert(type->desugar());
231266
}
232267

233-
std::string Converter::RenderType(clang::QualType qual_type) {
234-
return std::string(Trim(Convert(qual_type)->print()));
235-
}
236-
237-
RsExpr *Converter::ConvertPointeeType(clang::QualType ptr_type) {
238-
assert(!ptr_type.isNull() && ptr_type->isPointerType());
239-
auto pointee = ptr_type->getPointeeType();
240-
if (!pointee->isRecordType()) {
241-
return Convert(pointee);
242-
}
243-
244-
auto str = RenderType(ptr_type);
245-
Unwrap(str, "*mut ", "");
246-
Unwrap(str, "*const ", "");
247-
return Text(std::move(str));
248-
}
249-
250-
RsExpr *
251-
Converter::ConvertFunctionPointerType(const clang::FunctionProtoType *proto,
252-
FnProtoType kind) {
253-
std::vector<RsExpr *> parts;
254-
parts.push_back(
255-
Text(kind == FnProtoType::LambdaCallOperator ? "impl Fn(" : "fn("));
256-
for (auto p_ty : proto->param_types()) {
257-
parts.push_back(Convert(p_ty));
258-
parts.push_back(Text(','));
259-
}
260-
parts.push_back(Text(')'));
261-
if (!proto->getReturnType()->isVoidType()) {
262-
parts.push_back(Text("->"));
263-
parts.push_back(Convert(proto->getReturnType()));
264-
}
265-
return arena_.New<Concat>(std::move(parts));
266-
}
267-
268268
bool Converter::Convert(clang::Decl *decl) {
269269
if (decl == nullptr) {
270270
return true;

cpp2rust/converter/converter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ class Converter {
6767
virtual RsExpr *
6868
VisitLValueReferenceType(const clang::LValueReferenceType *type);
6969

70-
virtual RsExpr *VisitDecayedType(const clang::DecayedType *type);
71-
7270
virtual RsExpr *VisitPointerType(const clang::PointerType *type);
7371

74-
virtual RsExpr *VisitTypedefType(const clang::TypedefType *type);
75-
76-
virtual RsExpr *VisitUsingType(const clang::UsingType *type);
77-
7872
enum class FnProtoType { LambdaCallOperator, FnPtr };
7973

8074
virtual RsExpr *
8175
ConvertFunctionPointerType(const clang::FunctionProtoType *proto,
8276
FnProtoType kind = FnProtoType::FnPtr);
8377

78+
virtual RsExpr *VisitDecayedType(const clang::DecayedType *type);
79+
80+
virtual RsExpr *VisitTypedefType(const clang::TypedefType *type);
81+
82+
virtual RsExpr *VisitUsingType(const clang::UsingType *type);
83+
8484
virtual RsExpr *VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl);
8585

8686
virtual RsExpr *VisitFunctionDecl(clang::FunctionDecl *decl);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -172,55 +172,6 @@ RsExpr *ConverterRefCount::VisitLValueReferenceType(
172172
return Cat(Text("Ptr<"), Convert(type->getPointeeType()), Text('>'));
173173
}
174174

175-
RsExpr *ConverterRefCount::VisitPointerType(const clang::PointerType *type) {
176-
if (auto proto = type->getPointeeType()->getAs<clang::FunctionProtoType>()) {
177-
return Cat(Text("FnPtr<"), ConvertFunctionPointerType(proto), Text('>'));
178-
}
179-
180-
if (IsVaListType(clang::QualType(type, 0))) {
181-
return Text("VaList");
182-
}
183-
184-
if (type->isVoidPointerType()) {
185-
return Text("AnyPtr");
186-
}
187-
188-
auto pointee_type = type->getPointeeType();
189-
PushConversionKind push1(*this, ConversionKind::Ptr,
190-
!pointee_type->isArrayType());
191-
PushConversionKind push2(*this, ConversionKind::FullRefCount,
192-
pointee_type->isArrayType());
193-
const char *open = "Ptr<";
194-
if (pointee_type->isRecordType() &&
195-
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
196-
open = "PtrDyn<dyn";
197-
}
198-
return Cat(Text(open), Convert(pointee_type), Text('>'));
199-
}
200-
201-
RsExpr *ConverterRefCount::VisitRecordType(const clang::RecordType *type) {
202-
PushConversionKind push(*this, ConversionKind::Unboxed);
203-
return Converter::VisitRecordType(type);
204-
}
205-
206-
RsExpr *ConverterRefCount::VisitConstantArrayType(
207-
const clang::ConstantArrayType *type) {
208-
auto conv = getConversionKind();
209-
PushConversionKind push(*this, ConversionKind::Unboxed);
210-
211-
switch (conv) {
212-
case ConversionKind::Unboxed:
213-
return Cat(
214-
Text('['), Convert(type->getElementType()),
215-
Text(std::format("; {}]", GetNumAsString(type->getSize()).c_str())));
216-
case ConversionKind::Ptr:
217-
return Convert(type->getElementType());
218-
case ConversionKind::FullRefCount:
219-
return Cat(Text("Box<["), Convert(type->getElementType()), Text("]>"));
220-
}
221-
std::unreachable();
222-
}
223-
224175
RsExpr *ConverterRefCount::BuildFnAdapter(
225176
const clang::FunctionDecl *src_fn,
226177
const clang::FunctionProtoType *src_proto,
@@ -291,6 +242,55 @@ RsExpr *ConverterRefCount::ConvertFunctionPointerType(
291242
return Converter::ConvertFunctionPointerType(proto, kind);
292243
}
293244

245+
RsExpr *ConverterRefCount::VisitPointerType(const clang::PointerType *type) {
246+
if (auto proto = type->getPointeeType()->getAs<clang::FunctionProtoType>()) {
247+
return Cat(Text("FnPtr<"), ConvertFunctionPointerType(proto), Text('>'));
248+
}
249+
250+
if (IsVaListType(clang::QualType(type, 0))) {
251+
return Text("VaList");
252+
}
253+
254+
if (type->isVoidPointerType()) {
255+
return Text("AnyPtr");
256+
}
257+
258+
auto pointee_type = type->getPointeeType();
259+
PushConversionKind push1(*this, ConversionKind::Ptr,
260+
!pointee_type->isArrayType());
261+
PushConversionKind push2(*this, ConversionKind::FullRefCount,
262+
pointee_type->isArrayType());
263+
const char *open = "Ptr<";
264+
if (pointee_type->isRecordType() &&
265+
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
266+
open = "PtrDyn<dyn";
267+
}
268+
return Cat(Text(open), Convert(pointee_type), Text('>'));
269+
}
270+
271+
RsExpr *ConverterRefCount::VisitRecordType(const clang::RecordType *type) {
272+
PushConversionKind push(*this, ConversionKind::Unboxed);
273+
return Converter::VisitRecordType(type);
274+
}
275+
276+
RsExpr *ConverterRefCount::VisitConstantArrayType(
277+
const clang::ConstantArrayType *type) {
278+
auto conv = getConversionKind();
279+
PushConversionKind push(*this, ConversionKind::Unboxed);
280+
281+
switch (conv) {
282+
case ConversionKind::Unboxed:
283+
return Cat(
284+
Text('['), Convert(type->getElementType()),
285+
Text(std::format("; {}]", GetNumAsString(type->getSize()).c_str())));
286+
case ConversionKind::Ptr:
287+
return Convert(type->getElementType());
288+
case ConversionKind::FullRefCount:
289+
return Cat(Text("Box<["), Convert(type->getElementType()), Text("]>"));
290+
}
291+
std::unreachable();
292+
}
293+
294294
RsExpr *ConverterRefCount::ConvertFreshLValue(clang::Expr *expr) {
295295
auto *node = ConvertLValue(expr);
296296
if (isFresh()) {

cpp2rust/converter/models/converter_refcount.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class ConverterRefCount final : public Converter {
1414

1515
RsExpr *Convert(clang::QualType qual_type) override;
1616

17+
RsExpr *VisitRecordType(const clang::RecordType *type) override;
18+
19+
RsExpr *VisitConstantArrayType(const clang::ConstantArrayType *type) override;
20+
1721
RsExpr *
1822
VisitIncompleteArrayType(const clang::IncompleteArrayType *type) override;
1923

@@ -22,10 +26,6 @@ class ConverterRefCount final : public Converter {
2226

2327
RsExpr *VisitPointerType(const clang::PointerType *type) override;
2428

25-
RsExpr *VisitRecordType(const clang::RecordType *type) override;
26-
27-
RsExpr *VisitConstantArrayType(const clang::ConstantArrayType *type) override;
28-
2929
RsExpr *
3030
ConvertFunctionPointerType(const clang::FunctionProtoType *proto,
3131
FnProtoType kind = FnProtoType::FnPtr) override;

0 commit comments

Comments
 (0)