Skip to content

Commit c371a7e

Browse files
committed
Work
1 parent 240cd0f commit c371a7e

112 files changed

Lines changed: 2539 additions & 4494 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CMakeLists.txt‎

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ add_custom_target("check"
162162
DEPENDS check-libcc2rs check-libcc2rs-macros check-unit
163163
)
164164

165-
set(RULES_IR_DIR "${CMAKE_BINARY_DIR}/rules")
166-
167165
set(RUSTLS_HEADER "${PROJECT_SOURCE_DIR}/rules/rustls/rustls.h")
168166
set(BROTLI_HEADERS
169167
"${PROJECT_SOURCE_DIR}/rules/brotli/brotli/decode.h"
@@ -222,45 +220,33 @@ file(GLOB rule_subdirs CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/rules/*)
222220
file(GLOB rule_common_headers CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/rules/common/*.h)
223221
file(GLOB rule_indexer_sources CONFIGURE_DEPENDS
224222
${PROJECT_SOURCE_DIR}/cpp2rust/rule_preprocessing/*
225-
${PROJECT_SOURCE_DIR}/cpp2rust/converter/rules/rules_loader.*)
223+
${PROJECT_SOURCE_DIR}/cpp2rust/converter/rules/rules_loader.*
224+
${PROJECT_SOURCE_DIR}/cpp2rust/converter/rules/decls.*)
226225
set(_common_headers)
227226
foreach(_header IN LISTS rule_common_headers)
228227
list(APPEND _common_headers --common-header=${_header})
229228
endforeach()
230-
set(cpp_rules_ir_outputs)
231-
set(cpp_rules_index_inputs)
232-
set(cpp_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/c.json ${CMAKE_BINARY_DIR}/rules_index/cpp.json)
229+
set(rules_index_parts)
230+
set(rules_index_parts_dir ${CMAKE_BINARY_DIR}/rules_index/parts)
231+
set(RULES_IR_DIR "${CMAKE_BINARY_DIR}/rules")
233232
set(rust_rules_ir_outputs)
234233
foreach(_rule_dir IN LISTS rule_subdirs)
235-
file(GLOB _cpp_srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
234+
file(GLOB _cpp_srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp ${_rule_dir}/src_cxx*.cpp)
236235
if(NOT _cpp_srcs)
237236
continue()
238237
endif()
239238
get_filename_component(_rule_name ${_rule_dir} NAME)
240-
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
241-
set(_cpp_out ${_out_dir}/ir_src.json)
242-
set(_cxxflags)
243-
set(_cxxflags_file)
244-
if(EXISTS ${_rule_dir}/cxxflags)
245-
set(_cxxflags_file ${_rule_dir}/cxxflags)
246-
file(STRINGS ${_cxxflags_file} _flags)
247-
foreach(_flag IN LISTS _flags)
248-
list(APPEND _cxxflags --cxxflags=${_flag})
249-
endforeach()
250-
endif()
239+
set(_part ${rules_index_parts_dir}/${_rule_name}.json)
251240
add_custom_command(
252-
OUTPUT ${_cpp_out}
253-
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
254-
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_cpp_out} ${_cxxflags}
255-
DEPENDS ${_cpp_srcs} ${_cxxflags_file} ${rule_common_headers} ${PROJECT_SOURCE_DIR}/cpp2rust/rule_preprocessing/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER} ${BROTLI_HEADERS}
241+
OUTPUT ${_part}
242+
COMMAND ${CMAKE_COMMAND} -E make_directory ${rules_index_parts_dir}
243+
COMMAND $<TARGET_FILE:cpp-rule-indexer> --dir ${_rule_dir} -o ${_part}
244+
DEPENDS ${_cpp_srcs} ${rule_common_headers} ${rule_indexer_sources} ${RUSTLS_HEADER} ${BROTLI_HEADERS}
256245
VERBATIM
257246
)
258-
list(APPEND cpp_rules_ir_outputs ${_cpp_out})
259-
260-
if(NOT _rule_name STREQUAL "builtin_types")
261-
list(APPEND cpp_rules_index_inputs ${_cpp_srcs} ${_cxxflags_file})
262-
endif()
247+
list(APPEND rules_index_parts ${_part})
263248

249+
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
264250
set(_rust_srcs)
265251
set(_rust_out)
266252
if(EXISTS ${_rule_dir}/tgt_unsafe.rs)
@@ -289,12 +275,11 @@ foreach(_rule_dir IN LISTS rule_subdirs)
289275
list(APPEND rust_rules_ir_outputs ${_rust_out})
290276
endforeach()
291277

292-
add_custom_target("preprocess-cpp-rules" ALL DEPENDS ${cpp_rules_ir_outputs})
293-
278+
set(cpp_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/c.json ${CMAKE_BINARY_DIR}/rules_index/cpp.json)
294279
add_custom_command(
295280
OUTPUT ${cpp_rules_index_outputs}
296-
COMMAND $<TARGET_FILE:cpp-rule-indexer> --exclude builtin_types ${_common_headers}
297-
DEPENDS ${cpp_rules_index_inputs} ${rule_common_headers} ${rule_indexer_sources} ${RUSTLS_HEADER} ${BROTLI_HEADERS}
281+
COMMAND $<TARGET_FILE:cpp-rule-indexer> ${_common_headers} -o ${CMAKE_BINARY_DIR}/rules_index ${rules_index_parts}
282+
DEPENDS ${rules_index_parts} ${rule_common_headers} ${rule_indexer_sources}
298283
VERBATIM
299284
)
300285

‎cpp2rust/CMakeLists.txt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_RESOURCE_DIR=\"${LLVM_L
2020
target_compile_definitions(cpp2rust_core PUBLIC "-DCOMPAT_INCLUDE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/compat\"")
2121
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_SRC_DIR=\"${PROJECT_SOURCE_DIR}/rules\"")
2222
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_INDEX_DIR=\"${CMAKE_BINARY_DIR}/rules_index\"")
23+
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_IR_DIR=\"${CMAKE_BINARY_DIR}/rules\"")
2324
target_compile_definitions(cpp2rust_core PUBLIC "-DRUST_STABLE_VERSION=\"${RUST_STABLE_VERSION}\"")
2425

2526
if (APPLE)
@@ -37,10 +38,6 @@ target_link_libraries(cpp2rust_core PUBLIC ${llvm_libs})
3738
add_clang_executable(cpp2rust PARTIAL_SOURCES_INTENDED cpp2rust.cpp)
3839
target_link_libraries(cpp2rust PRIVATE cpp2rust_core)
3940

40-
add_clang_executable(cpp-rule-preprocessor PARTIAL_SOURCES_INTENDED
41-
rule_preprocessing/cpp_rule_preprocessor.cpp)
42-
target_link_libraries(cpp-rule-preprocessor PRIVATE cpp2rust_core)
43-
4441
add_clang_executable(cpp-rule-indexer PARTIAL_SOURCES_INTENDED
4542
rule_preprocessing/cpp_rule_indexer.cpp)
4643
target_link_libraries(cpp-rule-indexer PRIVATE cpp2rust_core)

‎cpp2rust/ast_consumer.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <llvm/Support/ErrorHandling.h>
77

88
#include "converter/converter.h"
9+
#include "converter/rules/registry.h"
910

1011
namespace cpp2rust {
1112
void ASTConsumer::HandleTranslationUnit(clang::ASTContext &ctx) {
1213
if (CI_.getDiagnostics().hasErrorOccurred()) {
1314
llvm::report_fatal_error(
1415
"the translation unit and its loaded rules do not compile");
1516
}
17+
RuleRegistry::Load(CI_.getSema(), model_);
1618
auto converter = CreateConverter(rs_code_, ctx, model_, rules_dir_);
1719
converter->SetSema(CI_.getSema());
1820
if (first_) {

‎cpp2rust/converter/converter.cpp‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ bool Converter::Convert(clang::QualType qual_type) {
134134
return TraverseType(qual_type);
135135
}
136136

137+
std::string Converter::GetUnboxedTypeAsString(clang::QualType qual_type) {
138+
return ToString(qual_type);
139+
}
140+
137141
bool Converter::ConvertMappedType(clang::QualType qual_type) {
138142
std::string type_as_string = Mapper::Map(*this, qual_type);
139143
if (type_as_string == token::kIgnoreRule) {
@@ -181,7 +185,11 @@ bool Converter::VisitRecordType(clang::RecordType *type) {
181185
}
182186

183187
StrCat(GetRecordName(decl));
184-
RuleRegistry::AddRuleForUserDefinedType(ctx_, decl);
188+
return false;
189+
}
190+
191+
bool Converter::VisitEnumType(clang::EnumType *type) {
192+
StrCat(GetRecordName(type->getDecl()));
185193
return false;
186194
}
187195

@@ -731,11 +739,19 @@ bool Converter::IsPassThroughRule(clang::Expr *expr) const {
731739
rule->body[0]);
732740
}
733741

734-
bool Converter::RecordDerivesCopy(const clang::RecordDecl *decl) const {
735-
auto *derives =
736-
RuleRegistry::MappedDerives(ctx_, ctx_.getCanonicalTagType(decl));
737-
return derives &&
738-
std::find(derives->begin(), derives->end(), "Copy") != derives->end();
742+
bool Converter::RecordDerivesCopy(const clang::RecordDecl *decl) {
743+
if (auto rule =
744+
RuleRegistry::Search(ctx_, ctx_.getCanonicalTagType(decl)).first) {
745+
auto &derives = rule->type_info.derives;
746+
return std::find(derives.begin(), derives.end(), "Copy") != derives.end();
747+
}
748+
decl = decl->getDefinition();
749+
if (!decl) {
750+
return false;
751+
}
752+
auto attrs = GetStructAttributes(decl);
753+
return std::find(attrs.begin(), attrs.end(), std::string_view("Copy")) !=
754+
attrs.end();
739755
}
740756

741757
bool Converter::RecordHasCopyableFields(const clang::RecordDecl *decl) {
@@ -794,7 +810,6 @@ bool Converter::VisitRecordDecl(clang::RecordDecl *decl) {
794810
return false;
795811
}
796812

797-
RuleRegistry::AddRuleForUserDefinedType(ctx_, decl);
798813
EmitRustStructOrUnion(decl);
799814

800815
return false;
@@ -861,9 +876,6 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) {
861876
EmitReprC(decl);
862877
}
863878
auto attrs = GetStructAttributes(decl);
864-
RuleRegistry::SetDerives(
865-
ctx_, ctx_.getCanonicalTagType(decl),
866-
std::vector<std::string>(attrs.begin(), attrs.end()));
867879
StrCat("#[derive(");
868880
for (auto *attr : attrs) {
869881
StrCat(attr, ',');
@@ -961,9 +973,6 @@ void Converter::EmitReprC(clang::RecordDecl *decl) {
961973
void Converter::EmitRustUnion(clang::RecordDecl *decl) {
962974
EmitReprC(decl);
963975
auto attrs = GetStructAttributes(decl);
964-
RuleRegistry::SetDerives(
965-
ctx_, ctx_.getCanonicalTagType(decl),
966-
std::vector<std::string>(attrs.begin(), attrs.end()));
967976
StrCat("#[derive(");
968977
for (auto *attr : attrs) {
969978
StrCat(attr, ',');
@@ -987,7 +996,6 @@ void Converter::EmitRustUnion(clang::RecordDecl *decl) {
987996
bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
988997
decl->dump(log());
989998

990-
RuleRegistry::AddRuleForUserDefinedType(ctx_, decl);
991999
if (!IsConvertibleCXXRecordDecl(decl)) {
9921000
return false;
9931001
}
@@ -1943,7 +1951,7 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) {
19431951
}
19441952

19451953
auto ty = GetReturnTypeOfFunction(expr);
1946-
auto ref = clang::dyn_cast<clang::ReferenceType>(ty);
1954+
auto ref = ty->getAs<clang::ReferenceType>();
19471955

19481956
if (ref && !isAddrOf() && !isVoid()) {
19491957
{
@@ -3198,6 +3206,10 @@ std::string Converter::ConvertDeclRef(clang::Expr *expr,
31983206

31993207
if (ShouldReplaceWithMappedBody(decl)) {
32003208
if (auto str = GetMappedAsString(expr); !str.empty()) {
3209+
if (RuleRegistry::ReturnsReference(expr) && !isAddrOf()) {
3210+
return std::format("({}{})", GetPointerDerefPrefix(expr->getType()),
3211+
str);
3212+
}
32013213
return str;
32023214
}
32033215
}
@@ -3950,7 +3962,6 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
39503962
if (Mapper::Contains(ctx_, ctx_.getCanonicalTagType(decl))) {
39513963
return false;
39523964
}
3953-
RuleRegistry::AddRuleForUserDefinedType(ctx_, decl);
39543965
auto name = GetRecordName(decl);
39553966
StrCat(std::format("pub type {} = {};", name,
39563967
GetUnsafeTypeAsString(decl->getIntegerType())));
@@ -5316,13 +5327,10 @@ std::string
53165327
Converter::ConvertInitFragment(clang::Expr *expr,
53175328
const std::vector<clang::Expr *> &all_args) {
53185329
const auto *tgt_ir = RuleRegistry::GetExprRule(ctx_, expr);
5319-
assert(tgt_ir && tgt_ir->init_type.valid());
5320-
auto *callee = clang::cast<clang::CallExpr>(expr)->getDirectCallee();
5321-
assert(callee);
5322-
auto type = GetSema()
5323-
.getTemplateInstantiationArgs(callee)(tgt_ir->init_type.depth,
5324-
tgt_ir->init_type.index)
5325-
.getAsType();
5330+
if (!tgt_ir || !tgt_ir->usesInit()) {
5331+
llvm::report_fatal_error("expression rule does not use Init");
5332+
}
5333+
auto type = RuleRegistry::GetInitType(expr);
53265334

53275335
Buffer buf(*this);
53285336
ConvertConstructFromArgs(

‎cpp2rust/converter/converter.h‎

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

5151
clang::ASTContext &GetASTContext() { return ctx_; }
5252

53+
virtual std::string GetUnboxedTypeAsString(clang::QualType qual_type);
54+
5355
bool VisitRecoveryExpr(clang::RecoveryExpr *expr);
5456

5557
virtual void EmitFilePreamble();
@@ -63,6 +65,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
6365

6466
virtual bool VisitRecordType(clang::RecordType *type);
6567

68+
bool VisitEnumType(clang::EnumType *type);
69+
6670
virtual bool VisitConstantArrayType(clang::ConstantArrayType *type);
6771

6872
virtual bool VisitIncompleteArrayType(clang::IncompleteArrayType *type);
@@ -352,7 +356,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
352356
// Option<fn> implements Copy
353357
virtual bool FunctionPointerImplementsCopy() const { return true; }
354358

355-
bool TypeIsCopyable(clang::QualType ty) const {
359+
bool TypeIsCopyable(clang::QualType ty) {
356360
if (ty->isFunctionPointerType() || ty->isFunctionType()) {
357361
return FunctionPointerImplementsCopy();
358362
}
@@ -774,7 +778,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
774778

775779
virtual bool TypeDerivesDefault(clang::QualType qual_type);
776780

777-
bool RecordDerivesCopy(const clang::RecordDecl *decl) const;
781+
bool RecordDerivesCopy(const clang::RecordDecl *decl);
778782

779783
bool IsPassThroughRule(clang::Expr *expr) const;
780784

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ bool IsAbstractUserClass(const clang::RecordDecl *decl) {
317317
definition->isAbstract();
318318
}
319319

320-
bool RefersToUserDefinedDecl(const clang::Expr *expr) {
320+
bool ExprHasUserDefinedDecl(const clang::Expr *expr) {
321321
expr = expr->IgnoreParenImpCasts();
322322
const clang::Decl *decl = nullptr;
323323
if (const auto *call = llvm::dyn_cast<clang::CallExpr>(expr)) {
@@ -1070,15 +1070,15 @@ const char *AccessSpecifierAsString(clang::AccessSpecifier spec) {
10701070
clang::QualType GetReturnTypeOfFunction(const clang::CallExpr *expr) {
10711071
if (auto *decl = expr->getCalleeDecl()) {
10721072
if (auto *fn = decl->getAsFunction()) {
1073-
return fn->getReturnType().getCanonicalType();
1073+
return fn->getReturnType();
10741074
}
10751075
}
10761076

10771077
auto callee_ty = expr->getCallee()->getType();
10781078
if (auto *ptr_ty = callee_ty->getAs<clang::PointerType>()) {
10791079
if (auto *fn_ty =
10801080
ptr_ty->getPointeeType()->getAs<clang::FunctionProtoType>()) {
1081-
return fn_ty->getReturnType().getCanonicalType();
1081+
return fn_ty->getReturnType();
10821082
}
10831083
}
10841084

@@ -1624,18 +1624,6 @@ clang::CXXConstructExpr *MakeConstructExpr(clang::ASTContext &ctx,
16241624
clang::SourceRange());
16251625
}
16261626

1627-
std::vector<clang::CXXRecordDecl *>
1628-
GetNestedStructs(const clang::CXXRecordDecl *decl) {
1629-
std::vector<clang::CXXRecordDecl *> nested_record_decls;
1630-
for (auto *d : decl->decls()) {
1631-
if (auto *rec = clang::dyn_cast<clang::CXXRecordDecl>(d);
1632-
rec && !rec->isImplicit()) {
1633-
nested_record_decls.push_back(rec);
1634-
}
1635-
}
1636-
return nested_record_decls;
1637-
}
1638-
16391627
std::optional<clang::ArrayRef<clang::TemplateArgument>>
16401628
GetTemplateArgs(clang::QualType qual_type, clang::Expr *expr) {
16411629
if (auto ty = clang::dyn_cast<clang::TemplateSpecializationType>(qual_type)) {

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bool IsUserDefinedDecl(const clang::Decl *decl);
7070

7171
bool IsAbstractUserClass(const clang::RecordDecl *decl);
7272

73-
bool RefersToUserDefinedDecl(const clang::Expr *expr);
73+
bool ExprHasUserDefinedDecl(const clang::Expr *expr);
7474

7575
bool IsUnsignedArithOp(const clang::BinaryOperator *expr);
7676

@@ -271,9 +271,6 @@ clang::CXXConstructExpr *MakeConstructExpr(clang::ASTContext &ctx,
271271
clang::CXXConstructorDecl *ctor,
272272
llvm::ArrayRef<clang::Expr *> args);
273273

274-
std::vector<clang::CXXRecordDecl *>
275-
GetNestedStructs(const clang::CXXRecordDecl *decl);
276-
277274
std::optional<clang::ArrayRef<clang::TemplateArgument>>
278275
GetTemplateArgs(clang::QualType qual_type, clang::Expr *expr = nullptr);
279276

‎cpp2rust/converter/factory.cpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
#include "converter/factory.h"
55

66
#include "converter/models/converter_refcount.h"
7-
#include "converter/rules/registry.h"
87

98
namespace cpp2rust {
109

1110
std::unique_ptr<Converter> CreateConverter(std::string &rs_code,
1211
clang::ASTContext &ctx, Model model,
1312
const std::string &rules_dir) {
14-
RuleRegistry::Load(model, rules_dir);
1513
switch (model) {
1614
case Model::kUnsafe:
1715
return std::make_unique<Converter>(rs_code, ctx);

0 commit comments

Comments
 (0)