Skip to content

Commit ed2e522

Browse files
committed
Add function call inout passing style
In current implementation of cppfront the following code ```cpp f2: (inout x) -> _ = { return x * 2; } main: () -> int = { x := 21; std::cout << f2(x) << std::endl; } ``` Generates: ```cpp [[nodiscard]] auto f2(auto& x) -> auto{ return x * 2; } [[nodiscard]] auto main() -> int{ auto x {21}; std::cout << f2(std::move(x)) << std::endl; } ``` and fail to compile as move from last use make the call to `f2` incompatible with requirements of the `f2` function (function requires lvalue reference and receives rvalue reference). This change introduce possibility to add `inout` passing style to function call to emphasize that it has to be passed without the move. After this change the original code can be fixed to: ```cpp f2: (inout x) -> _ = { return x * 2; } main: () -> int = { x := 21; std::cout << f2(inout x) << std::endl; } ``` which will generate: ```cpp [[nodiscard]] auto f2(auto& x) -> auto{ return x * 2; } [[nodiscard]] auto main() -> int{ auto x {21}; std::cout << f2( x) << std::endl; } ```
1 parent fbf55ad commit ed2e522

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

source/cppfront.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,7 @@ class cppfront
32303230
x.pass == passing_style::out
32313231
|| x.pass == passing_style::move
32323232
|| x.pass == passing_style::forward
3233+
|| x.pass == passing_style::inout
32333234
);
32343235
if (x.pass == passing_style::out) {
32353236
is_out = true;
@@ -3240,6 +3241,10 @@ class cppfront
32403241
printer.print_cpp2("std::move(", n.position());
32413242
offset = 6; // because we're replacing "move " (followed by at least one space) with "std::move("
32423243
}
3244+
else if (x.pass == passing_style::inout) {
3245+
is_out = true;
3246+
offset = -6; // because we're replacing "inout " (followed by at least one space) with nothing
3247+
}
32433248
}
32443249

32453250
if (is_out) {

source/parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,7 @@ class parser
31263126
dir == passing_style::out
31273127
|| dir == passing_style::move
31283128
|| dir == passing_style::forward
3129+
|| dir == passing_style::inout
31293130
)
31303131
&& peek(1)
31313132
&& peek(1)->type() == lexeme::Identifier

0 commit comments

Comments
 (0)