Skip to content

Commit

Permalink
Default initialize function argument, closes #1090 (#1092)
Browse files Browse the repository at this point in the history
* Default initialize function argument

* Add output to test to show that defaults were applied

---------

Co-authored-by: Herb Sutter <[email protected]>
  • Loading branch information
sookach and hsutter authored Jun 16, 2024
1 parent 6156b51 commit 94bea67
Show file tree
Hide file tree
Showing 29 changed files with 90 additions and 5 deletions.
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((), ());
}
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 "test"
0 ""
1 "test"
0 ""
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 "test"
0 ""
1 "test"
0 ""
Empty file.
Empty file.
Empty file.
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 ""
Empty file.
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)

Empty file.
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 @@ -657,6 +657,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 @@ -5389,6 +5390,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 @@ -5743,6 +5745,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 @@ -5897,8 +5901,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 @@ -3966,6 +3966,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

0 comments on commit 94bea67

Please sign in to comment.