Skip to content

Default initialize function argument, closes #1090 #1092

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

Merged
merged 2 commits into from
Jun 16, 2024
Merged
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
14 changes: 14 additions & 0 deletions regression-tests/mixed-default-arguments.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
auto cxx(int x, std::string y) -> void {
std::cout << x << " \"" << y << "\"" << "\n";
}

cxx2 : (x : int, y : std::string) = {
std::cout << "(x)$ \"(y)$\"\n";
}

main : () = {
cxx(1, "test");
cxx((), ());
cxx2(1, "test");
cxx2((), ());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 "test"
0 ""
1 "test"
0 ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 "test"
0 ""
1 "test"
0 ""
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
g++-14 (Ubuntu 14-20240412-0ubuntu1) 14.0.1 20240412 (experimental) [master r14-9935-g67e1433a94f]
g++ (GCC) 14.1.1 20240607 (Red Hat 14.1.1-5)
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 "test"
0 ""
1 "test"
0 ""
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is intended to be run in the /test-results/gcc-13 subdirectory
# in a Linux shell with gcc 13 installed
# This is intended to be run in the /test-results/gcc-14 subdirectory
# in a Linux shell with gcc 14 installed
#
cp ../*.cpp .
rm -f *.output
Expand All @@ -9,7 +9,7 @@ g++ --version > gcc-version.output 2>&1
for f in *.cpp
do
let count=count+1
printf "[%s] Starting gcc 13 %s\n" "$count" "$f"
printf "[%s] Starting gcc 14 %s\n" "$count" "$f"
g++ -I../../../include -std=c++2b -pthread -Wold-style-cast -Wunused-parameter -o test.exe $f > $f.output 2>&1
rm -f $f
if test -f "test.exe"; then
Expand Down
40 changes: 40 additions & 0 deletions regression-tests/test-results/mixed-default-arguments.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "mixed-default-arguments.cpp2"


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-default-arguments.cpp2"
auto cxx(int x, std::string y) -> void {
std::cout << x << " \"" << y << "\"" << "\n";
}

#line 5 "mixed-default-arguments.cpp2"
auto cxx2(cpp2::impl::in<int> x, cpp2::impl::in<std::string> y) -> void;

#line 9 "mixed-default-arguments.cpp2"
auto main() -> int;

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

#line 1 "mixed-default-arguments.cpp2"

#line 5 "mixed-default-arguments.cpp2"
auto cxx2(cpp2::impl::in<int> x, cpp2::impl::in<std::string> y) -> void{
std::cout << (cpp2::to_string(x) + " \"" + cpp2::to_string(y) + "\"\n");
}

#line 9 "mixed-default-arguments.cpp2"
auto main() -> int{
cxx(1, "test");
cxx({}, {});
cxx2(1, "test");
cxx2({}, {});
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mixed-default-arguments.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x86
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33811 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 "test"
0 ""
1 "test"
0 ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mixed-default-arguments.cpp
8 changes: 7 additions & 1 deletion source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ struct expression_list_node
token const* open_paren = {};
token const* close_paren = {};
bool inside_initializer = false;
bool default_initializer = false;

struct term {
passing_style pass = {};
Expand Down Expand Up @@ -5393,6 +5394,7 @@ class parser
};
mutable std::vector<function_body_extent> function_body_extents;
mutable bool is_function_body_extents_sorted = false;
bool is_inside_call_expr = false;

public:
auto is_within_function_body(source_position p) const
Expand Down Expand Up @@ -5747,6 +5749,8 @@ class parser
expr_list->inside_initializer = false;
}
n->expression_list_is_fold_expression = expr_list->is_fold_expression();
expr_list->default_initializer =
is_inside_call_expr && std::empty(expr_list->expressions);
n->expr = std::move(expr_list);
return n;
}
Expand Down Expand Up @@ -5899,8 +5903,10 @@ class parser
// Next should be an expression-list followed by a ')'
// If not, then this wasn't a call expression so backtrack to
// the '(' which will be part of the next grammar production

is_inside_call_expr = true;
term.expr_list = expression_list(term.op, lexeme::RightParen);
is_inside_call_expr = false;

if (
term.expr_list
&& curr().type() == lexeme::RightParen
Expand Down
5 changes: 5 additions & 0 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -3959,6 +3959,11 @@ class cppfront
)
-> void
{ STACKINSTR
if (n.default_initializer) {
printer.print_cpp2("{}", n.position());
return;
}

auto add_parens =
should_add_expression_list_parens()
&& !n.inside_initializer
Expand Down
Loading