Skip to content

Commit 896e16e

Browse files
authored
Translate operators (#335)
This PR adds support for the following operators: * arithmetic: `+, -, unary +, unary -, *, /, %, ++ prefix and postfix, -- prefix and postfix` * relational: `==, !=, >, <, >=, <=, <=>` * logical: `!, &&, ||` * bitwise: `~a, &, |, ^, <<, >>` * assignment: `=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=` * member and pointer: `[] and static [], *, &, ->` * other: `() and static (), , (comma), user defined conversion` Not supported: * `<<` for streams * `a->*b` pointer to member * `"a"_b` user defined literal * `new`, `delete` In Rust, operators are translated as `operator_call`, `operator_comma`, etc. Operators that are cv or ref-qualified contain that information in the name, for example `operator_call_const_volatile_rref`. We support both method versions and free function versions of operators. Furthermore, we also support static `operator[]` and static `operator()`. The following operators are used to generate Rust traits: * `operator<` or `operator<=>` generate PartialOrd and Ord * `operator==` or `operator<` or `operator<=>` are used for PartialEq and Eq * the operators are implemented on `Ptr<Self>` while the traits are implemented on `Self` I also added rules and tests for `operator<=>`. Since this is a C++20 feature, I had to change CMakeLists, rules/ and Cpp2RustTest.py in order to compile everything related to `operator<=>` with `-std=c++20`.
1 parent f99ddd3 commit 896e16e

82 files changed

Lines changed: 6283 additions & 232 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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,20 @@ foreach(_rule_dir IN LISTS rule_subdirs)
153153
get_filename_component(_rule_name ${_rule_dir} NAME)
154154
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
155155
set(_out ${_out_dir}/ir_src.json)
156+
set(_cxxflags)
157+
set(_cxxflags_file)
158+
if(EXISTS ${_rule_dir}/cxxflags)
159+
set(_cxxflags_file ${_rule_dir}/cxxflags)
160+
file(STRINGS ${_cxxflags_file} _flags)
161+
foreach(_flag IN LISTS _flags)
162+
list(APPEND _cxxflags --cxxflags=${_flag})
163+
endforeach()
164+
endif()
156165
add_custom_command(
157166
OUTPUT ${_out}
158167
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
159-
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_out}
160-
DEPENDS ${_srcs} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER}
168+
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_out} ${_cxxflags}
169+
DEPENDS ${_srcs} ${_cxxflags_file} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER}
161170
VERBATIM
162171
)
163172
list(APPEND cpp_rules_ir_outputs ${_out})

0 commit comments

Comments
 (0)