Skip to content

Commit 7f9df00

Browse files
committed
Move builtin rules in rules/
1 parent de0dfa2 commit 7f9df00

12 files changed

Lines changed: 480 additions & 160 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -151,61 +151,12 @@ std::string Converter::ConvertPointeeType(clang::QualType ptr_type) {
151151
}
152152

153153
bool Converter::VisitBuiltinType(clang::BuiltinType *type) {
154-
switch (type->getKind()) {
155-
case clang::BuiltinType::Bool:
156-
StrCat("bool");
157-
break;
158-
case clang::BuiltinType::Float:
159-
StrCat("f32");
160-
break;
161-
case clang::BuiltinType::Double:
162-
case clang::BuiltinType::LongDouble:
163-
StrCat("f64");
164-
break;
165-
case clang::BuiltinType::Char_S:
166-
case clang::BuiltinType::Char_U:
167-
StrCat(CharRustType());
168-
break;
169-
case clang::BuiltinType::SChar:
170-
StrCat("i8");
171-
break;
172-
case clang::BuiltinType::UChar:
173-
StrCat("u8");
174-
break;
175-
case clang::BuiltinType::UShort:
176-
case clang::BuiltinType::UInt:
177-
case clang::BuiltinType::ULong:
178-
case clang::BuiltinType::ULongLong:
179-
case clang::BuiltinType::Short:
180-
case clang::BuiltinType::Int:
181-
case clang::BuiltinType::Long:
182-
case clang::BuiltinType::LongLong:
183-
case clang::BuiltinType::WChar_S:
184-
case clang::BuiltinType::WChar_U:
185-
case clang::BuiltinType::Char8:
186-
case clang::BuiltinType::Char16:
187-
case clang::BuiltinType::Char32:
188-
StrCat(std::format("{}{}", type->isSignedInteger() ? 'i' : 'u',
189-
ctx_.getTypeSize(type)));
190-
break;
191-
case clang::BuiltinType::Void:
192-
StrCat("::libc::c_void");
193-
break;
194-
case clang::BuiltinType::UInt128:
195-
StrCat("u128");
196-
break;
197-
case clang::BuiltinType::Int128:
198-
StrCat("i128");
199-
break;
200-
case clang::BuiltinType::NullPtr:
201-
Convert(ctx_.VoidPtrTy);
202-
break;
203-
default:
204-
llvm::errs() << "unsupported builtin type: "
205-
<< type->getName(ctx_.getPrintingPolicy()) << '\n';
206-
assert(0 && "unsupported builtin type\n");
207-
break;
154+
auto mapped = Mapper::Map(clang::QualType(type, 0));
155+
if (mapped.empty()) {
156+
llvm::report_fatal_error(llvm::Twine("no type rule for builtin type: ") +
157+
type->getName(ctx_.getPrintingPolicy()));
208158
}
159+
StrCat(mapped);
209160
return false;
210161
}
211162

‎cpp2rust/converter/mapper.cpp‎

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -458,110 +458,6 @@ void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
458458
}
459459
}
460460

461-
void addBuiltinTypes(Model model) {
462-
assert(ctx_);
463-
464-
auto add_scalar_rule = [&](const std::string &cxx, const std::string &rust,
465-
const std::string &initializer = {}) {
466-
auto plain = TranslationRule::TypeRule::Plain(rust);
467-
plain.initializer = initializer;
468-
std::vector<std::string> derives = {"Copy", "Clone", "Default",
469-
"Debug", "PartialEq", "PartialOrd"};
470-
if (!(rust == "f32" || rust == "f64")) {
471-
derives.insert(derives.end(), {"Eq", "Ord", "Hash"});
472-
}
473-
plain.type_info.derives = std::move(derives);
474-
AddTypeRule(cxx, TranslationRule::TypeRule(plain));
475-
AddTypeRule("const " + cxx, std::move(plain));
476-
477-
switch (model) {
478-
case Model::kUnsafe:
479-
AddTypeRule(cxx + " *",
480-
TranslationRule::TypeRule::UnsafePtr("*mut " + rust));
481-
AddTypeRule("const " + cxx + " *",
482-
TranslationRule::TypeRule::UnsafePtr("*const " + rust));
483-
break;
484-
case Model::kRefCount:
485-
AddTypeRule(cxx + " *", TranslationRule::TypeRule::RefcountPtr(
486-
"Ptr::<" + rust + ">"));
487-
AddTypeRule("const " + cxx + " *", TranslationRule::TypeRule::RefcountPtr(
488-
"Ptr::<" + rust + ">"));
489-
break;
490-
}
491-
};
492-
493-
auto add_builtin_rule = [&](clang::QualType qt, const std::string &rust) {
494-
add_scalar_rule(ToString(qt), rust);
495-
};
496-
497-
auto add_size_rules = [&](clang::QualType size_type,
498-
std::initializer_list<const char *> aliases,
499-
const std::string &rust) {
500-
auto initializer = "0_" + rust;
501-
for (const char *alias : aliases) {
502-
add_scalar_rule(alias, rust, initializer);
503-
}
504-
if (const auto *predef = clang::dyn_cast<clang::PredefinedSugarType>(
505-
size_type.getTypePtr())) {
506-
add_scalar_rule(predef->getIdentifier()->getName().str(), rust,
507-
initializer);
508-
}
509-
};
510-
511-
auto build_rust_type = [&](clang::QualType qt) {
512-
unsigned bits = ctx_->getTypeSize(qt);
513-
char sign = qt->isSignedIntegerType() ? 'i' : 'u';
514-
return std::format("{}{}", sign, bits);
515-
};
516-
517-
// Misc
518-
add_builtin_rule(ctx_->BoolTy, "bool");
519-
add_builtin_rule(ctx_->FloatTy, "f32");
520-
add_builtin_rule(ctx_->DoubleTy, "f64");
521-
522-
switch (model) {
523-
case Model::kUnsafe:
524-
AddTypeRule(ToString(ctx_->VoidTy) + " *",
525-
TranslationRule::TypeRule::UnsafePtr("*mut ::libc::c_void"));
526-
AddTypeRule("const " + ToString(ctx_->VoidTy) + " *",
527-
TranslationRule::TypeRule::UnsafePtr("*const ::libc::c_void"));
528-
break;
529-
case Model::kRefCount:
530-
AddTypeRule(ToString(ctx_->VoidTy) + " *",
531-
TranslationRule::TypeRule::RefcountPtr("AnyPtr"));
532-
AddTypeRule("const " + ToString(ctx_->VoidTy) + " *",
533-
TranslationRule::TypeRule::RefcountPtr("AnyPtr"));
534-
break;
535-
}
536-
537-
// Char
538-
switch (model) {
539-
case Model::kUnsafe:
540-
add_builtin_rule(ctx_->CharTy, "libc::c_char");
541-
break;
542-
case Model::kRefCount:
543-
add_builtin_rule(ctx_->CharTy, "u8");
544-
break;
545-
}
546-
add_builtin_rule(ctx_->SignedCharTy, "i8");
547-
add_builtin_rule(ctx_->UnsignedCharTy, "u8");
548-
549-
// Integers
550-
add_builtin_rule(ctx_->ShortTy, build_rust_type(ctx_->ShortTy));
551-
add_builtin_rule(ctx_->UnsignedShortTy,
552-
build_rust_type(ctx_->UnsignedShortTy));
553-
add_builtin_rule(ctx_->IntTy, build_rust_type(ctx_->IntTy));
554-
add_builtin_rule(ctx_->UnsignedIntTy, build_rust_type(ctx_->UnsignedIntTy));
555-
add_builtin_rule(ctx_->LongTy, build_rust_type(ctx_->LongTy));
556-
add_builtin_rule(ctx_->UnsignedLongTy, build_rust_type(ctx_->UnsignedLongTy));
557-
add_builtin_rule(ctx_->LongLongTy, build_rust_type(ctx_->LongLongTy));
558-
add_builtin_rule(ctx_->UnsignedLongLongTy,
559-
build_rust_type(ctx_->UnsignedLongLongTy));
560-
561-
add_size_rules(ctx_->getSizeType(), {"size_t", "size_type"}, "usize");
562-
add_size_rules(ctx_->getSignedSizeType(), {"ssize_t"}, "isize");
563-
}
564-
565461
clang::QualType normalizeQualType(clang::QualType qual_type) {
566462
assert(ctx_);
567463

@@ -862,7 +758,10 @@ std::string ToString(clang::QualType qual_type, ScalarSugar sugar) {
862758
} else if (const auto *ptr = t->getAs<clang::PointerType>()) {
863759
auto pointee = ptr->getPointeeType();
864760
auto canonical = pointee.getCanonicalType().getDesugaredType(*ctx_);
865-
if (Map(pointee) == Map(canonical)) {
761+
bool builtin_alias = canonical->isBuiltinType() &&
762+
(pointee->getAs<clang::TypedefType>() ||
763+
pointee->getAs<clang::PredefinedSugarType>());
764+
if (!builtin_alias && Map(pointee) == Map(canonical)) {
866765
pointee = canonical;
867766
}
868767
std::string out;
@@ -1123,7 +1022,6 @@ void LoadTranslationRules(Model model, clang::ASTContext &ctx,
11231022
}
11241023
translation_rules_loaded_ = true;
11251024

1126-
addBuiltinTypes(model);
11271025
addRulesFromDirectory(rules_dir, model);
11281026

11291027
#if 0

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,10 @@ ConverterRefCount::GetArrayDefaultAsString(clang::QualType qual_type) {
20892089
}
20902090

20912091
std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) {
2092+
if (qual_type->isVoidType()) {
2093+
return "()";
2094+
}
2095+
20922096
if (IsVaListType(qual_type)) {
20932097
computed_expr_type_ = ComputedExprType::FreshValue;
20942098
return BoxValue("VaList::default()");

‎rules/builtin_types/cxxflags‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-std=c++20

‎rules/builtin_types/src.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <cstddef>
5+
6+
typedef bool t1;
7+
typedef bool *t2;
8+
typedef const bool *t3;
9+
typedef float t4;
10+
typedef float *t5;
11+
typedef const float *t6;
12+
typedef double t7;
13+
typedef double *t8;
14+
typedef const double *t9;
15+
typedef char t10;
16+
typedef char *t11;
17+
typedef const char *t12;
18+
typedef signed char t13;
19+
typedef signed char *t14;
20+
typedef const signed char *t15;
21+
typedef unsigned char t16;
22+
typedef unsigned char *t17;
23+
typedef const unsigned char *t18;
24+
typedef short t19;
25+
typedef short *t20;
26+
typedef const short *t21;
27+
typedef unsigned short t22;
28+
typedef unsigned short *t23;
29+
typedef const unsigned short *t24;
30+
typedef int t25;
31+
typedef int *t26;
32+
typedef const int *t27;
33+
typedef unsigned int t28;
34+
typedef unsigned int *t29;
35+
typedef const unsigned int *t30;
36+
typedef long t31;
37+
typedef long *t32;
38+
typedef const long *t33;
39+
typedef unsigned long t34;
40+
typedef unsigned long *t35;
41+
typedef const unsigned long *t36;
42+
typedef long long t37;
43+
typedef long long *t38;
44+
typedef const long long *t39;
45+
typedef unsigned long long t40;
46+
typedef unsigned long long *t41;
47+
typedef const unsigned long long *t42;
48+
typedef void *t43;
49+
typedef const void *t44;
50+
typedef decltype(nullptr) t45;
51+
typedef void t46;
52+
typedef long double t47;
53+
typedef wchar_t t48;
54+
typedef char8_t t49;
55+
typedef char16_t t50;
56+
typedef char32_t t51;
57+
typedef __int128 t52;
58+
typedef unsigned __int128 t53;

0 commit comments

Comments
 (0)