Skip to content

Commit 94bea67

Browse files
sookachhsutter
andauthored
Default initialize function argument, closes #1090 (#1092)
* Default initialize function argument * Add output to test to show that defaults were applied --------- Co-authored-by: Herb Sutter <[email protected]>
1 parent 6156b51 commit 94bea67

29 files changed

+90
-5
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
auto cxx(int x, std::string y) -> void {
2+
std::cout << x << " \"" << y << "\"" << "\n";
3+
}
4+
5+
cxx2 : (x : int, y : std::string) = {
6+
std::cout << "(x)$ \"(y)$\"\n";
7+
}
8+
9+
main : () = {
10+
cxx(1, "test");
11+
cxx((), ());
12+
cxx2(1, "test");
13+
cxx2((), ());
14+
}

regression-tests/test-results/apple-clang-14-c++2b/mixed-default-arguments.cpp.execution

Whitespace-only changes.

regression-tests/test-results/apple-clang-14-c++2b/mixed-default-arguments.cpp.output

Whitespace-only changes.

regression-tests/test-results/apple-clang-15-c++2b/mixed-default-arguments.cpp.execution

Whitespace-only changes.

regression-tests/test-results/apple-clang-15-c++2b/mixed-default-arguments.cpp.output

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 "test"
2+
0 ""
3+
1 "test"
4+
0 ""

regression-tests/test-results/clang-12-c++20/mixed-default-arguments.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-15-c++20-libcpp/mixed-default-arguments.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-15-c++20-libcpp/mixed-default-arguments.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-15-c++20/mixed-default-arguments.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-15-c++20/mixed-default-arguments.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-18-c++20/mixed-default-arguments.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18-c++20/mixed-default-arguments.cpp.output

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 "test"
2+
0 ""
3+
1 "test"
4+
0 ""

regression-tests/test-results/gcc-10-c++20/mixed-default-arguments.cpp.output

Whitespace-only changes.

regression-tests/test-results/gcc-13-c++2b/mixed-default-arguments.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13-c++2b/mixed-default-arguments.cpp.output

Whitespace-only changes.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
g++-14 (Ubuntu 14-20240412-0ubuntu1) 14.0.1 20240412 (experimental) [master r14-9935-g67e1433a94f]
1+
g++ (GCC) 14.1.1 20240607 (Red Hat 14.1.1-5)
22
Copyright (C) 2024 Free Software Foundation, Inc.
33
This is free software; see the source for copying conditions. There is NO
44
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 "test"
2+
0 ""
3+
1 "test"
4+
0 ""

regression-tests/test-results/gcc-14-c++2b/mixed-default-arguments.cpp.output

Whitespace-only changes.

regression-tests/test-results/gcc-14-c++2b/run-tests-gcc-13.sh renamed to regression-tests/test-results/gcc-14-c++2b/run-tests-gcc-14.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# This is intended to be run in the /test-results/gcc-13 subdirectory
2-
# in a Linux shell with gcc 13 installed
1+
# This is intended to be run in the /test-results/gcc-14 subdirectory
2+
# in a Linux shell with gcc 14 installed
33
#
44
cp ../*.cpp .
55
rm -f *.output
@@ -9,7 +9,7 @@ g++ --version > gcc-version.output 2>&1
99
for f in *.cpp
1010
do
1111
let count=count+1
12-
printf "[%s] Starting gcc 13 %s\n" "$count" "$f"
12+
printf "[%s] Starting gcc 14 %s\n" "$count" "$f"
1313
g++ -I../../../include -std=c++2b -pthread -Wold-style-cast -Wunused-parameter -o test.exe $f > $f.output 2>&1
1414
rm -f $f
1515
if test -f "test.exe"; then
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
//=== Cpp2 type declarations ====================================================
4+
5+
6+
#include "cpp2util.h"
7+
8+
#line 1 "mixed-default-arguments.cpp2"
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
#line 1 "mixed-default-arguments.cpp2"
14+
auto cxx(int x, std::string y) -> void {
15+
std::cout << x << " \"" << y << "\"" << "\n";
16+
}
17+
18+
#line 5 "mixed-default-arguments.cpp2"
19+
auto cxx2(cpp2::impl::in<int> x, cpp2::impl::in<std::string> y) -> void;
20+
21+
#line 9 "mixed-default-arguments.cpp2"
22+
auto main() -> int;
23+
24+
//=== Cpp2 function definitions =================================================
25+
26+
#line 1 "mixed-default-arguments.cpp2"
27+
28+
#line 5 "mixed-default-arguments.cpp2"
29+
auto cxx2(cpp2::impl::in<int> x, cpp2::impl::in<std::string> y) -> void{
30+
std::cout << (cpp2::to_string(x) + " \"" + cpp2::to_string(y) + "\"\n");
31+
}
32+
33+
#line 9 "mixed-default-arguments.cpp2"
34+
auto main() -> int{
35+
cxx(1, "test");
36+
cxx({}, {});
37+
cxx2(1, "test");
38+
cxx2({}, {});
39+
}
40+

regression-tests/test-results/mixed-default-arguments.cpp.out

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-default-arguments.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+

regression-tests/test-results/msvc-2022-c++20/mixed-default-arguments.cpp.output

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 "test"
2+
0 ""
3+
1 "test"
4+
0 ""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mixed-default-arguments.cpp

source/parse.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ struct expression_list_node
657657
token const* open_paren = {};
658658
token const* close_paren = {};
659659
bool inside_initializer = false;
660+
bool default_initializer = false;
660661

661662
struct term {
662663
passing_style pass = {};
@@ -5389,6 +5390,7 @@ class parser
53895390
};
53905391
mutable std::vector<function_body_extent> function_body_extents;
53915392
mutable bool is_function_body_extents_sorted = false;
5393+
bool is_inside_call_expr = false;
53925394

53935395
public:
53945396
auto is_within_function_body(source_position p) const
@@ -5743,6 +5745,8 @@ class parser
57435745
expr_list->inside_initializer = false;
57445746
}
57455747
n->expression_list_is_fold_expression = expr_list->is_fold_expression();
5748+
expr_list->default_initializer =
5749+
is_inside_call_expr && std::empty(expr_list->expressions);
57465750
n->expr = std::move(expr_list);
57475751
return n;
57485752
}
@@ -5897,8 +5901,10 @@ class parser
58975901
// Next should be an expression-list followed by a ')'
58985902
// If not, then this wasn't a call expression so backtrack to
58995903
// the '(' which will be part of the next grammar production
5900-
5904+
is_inside_call_expr = true;
59015905
term.expr_list = expression_list(term.op, lexeme::RightParen);
5906+
is_inside_call_expr = false;
5907+
59025908
if (
59035909
term.expr_list
59045910
&& curr().type() == lexeme::RightParen

source/to_cpp1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,6 +3966,11 @@ class cppfront
39663966
)
39673967
-> void
39683968
{ STACKINSTR
3969+
if (n.default_initializer) {
3970+
printer.print_cpp2("{}", n.position());
3971+
return;
3972+
}
3973+
39693974
auto add_parens =
39703975
should_add_expression_list_parens()
39713976
&& !n.inside_initializer

0 commit comments

Comments
 (0)