Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function call inout passing style - closes #231 #294

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions regression-tests/pure2-inout-passing-style-call.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
f2: (inout x) -> _ = {
return x * 2;
}

main: () -> int = {
x := 21;
std::cout << f2(inout x) << std::endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
23 changes: 23 additions & 0 deletions regression-tests/test-results/pure2-inout-passing-style-call.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#define CPP2_USE_MODULES Yes

#include "cpp2util.h"


#line 1 "pure2-inout-passing-style-call.cpp2"
[[nodiscard]] auto f2(auto& x) -> auto;
#line 5 "pure2-inout-passing-style-call.cpp2"
[[nodiscard]] auto main() -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "pure2-inout-passing-style-call.cpp2"
[[nodiscard]] auto f2(auto& x) -> auto{
return x * 2;
}

[[nodiscard]] auto main() -> int{
auto x {21};
std::cout << f2( x) << std::endl;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
f2: (inout x) -> _ = {
return x * 2;
}

main: () -> int = {
x := 21;
std::cout << f2(inout x) << std::endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-inout-passing-style-call.cpp2... ok (all Cpp2, passes safety checks)

5 changes: 5 additions & 0 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3280,6 +3280,7 @@ class cppfront
x.pass == passing_style::out
|| x.pass == passing_style::move
|| x.pass == passing_style::forward
|| x.pass == passing_style::inout
);
if (x.pass == passing_style::out) {
is_out = true;
Expand All @@ -3290,6 +3291,10 @@ class cppfront
printer.print_cpp2("std::move(", n.position());
offset = 6; // because we're replacing "move " (followed by at least one space) with "std::move("
}
else if (x.pass == passing_style::inout) {
is_out = true;
offset = -6; // because we're replacing "inout " (followed by at least one space) with nothing
}
}

if (is_out) {
Expand Down
1 change: 1 addition & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,7 @@ class parser
dir == passing_style::out
|| dir == passing_style::move
|| dir == passing_style::forward
|| dir == passing_style::inout
)
&& peek(1)
&& peek(1)->type() == lexeme::Identifier
Expand Down