Skip to content

Commit fb4a9fc

Browse files
committed
Commit the last few evenings' changes
This is an omnibus commit of the last few evenings' changes. Primarily it was to start laying the groundwork for constructors, but it includes other fixes and closes a few issues. Details: - Begin infrastructure preparation for constructors - Started creating navigation APIs to replace the low-level graph node chasing; this makes cppfront's own code cleaner and the tree easier to change if needed, but it's also a step toward a reflection API - Extended `main:(args)` to require the name "args" for the single-parameter `main`, and to support `args.argc` and `args.argv`, further on #262 (see comment thread) - Changed default return type for unnamed functions to be `-> void`, same as named functions, closes #257 - Disallow single-expression function body to be just `return`, closes #267 - Make `make_args` inline, closes #268 - Bug fix: emit prolog also for single-expression function body. Specifically, this enables main:(args)=expression; to work correctly. Generally, this also corrects the code gen for examples that were trying (but failing) to inject prologs in single-expression functions... in the regression tests this corrected the code gen for `pure2-forward-return.cpp2` which was missing the contract check before.
1 parent 890908c commit fb4a9fc

24 files changed

+482
-201
lines changed

include/cpp2util.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,17 @@ inline auto to_string(std::tuple<Ts...> const& t) -> std::string
14191419
//
14201420
//-----------------------------------------------------------------------
14211421
//
1422-
auto args(int argc, char **argv) -> std::vector<std::string_view> {
1423-
auto ret = std::vector<std::string_view>{(size_t)argc};
1422+
struct args_t : std::vector<std::string_view>
1423+
{
1424+
args_t(int c, char const* const* v) : vector{(size_t)c}, argc{c}, argv{v} {}
1425+
1426+
int argc = 0;
1427+
char const* const* argv = nullptr;
1428+
};
1429+
1430+
inline auto make_args(int argc, char const* const* argv) -> args_t
1431+
{
1432+
auto ret = args_t{argc, argv};
14241433
for (auto i = 0; i < argc; ++i) {
14251434
ret[i] = std::string_view{argv[i]};
14261435
}

regression-tests/pure2-cpp1-multitoken-fundamental-types-error.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
main: (argc: int, argv: **char) -> int = {
2+
main: () -> int = {
33
a: signed short int = 1;
44
b: short int signed = 2;
55
c: long long unsigned int = 3;

regression-tests/pure2-main-args.cpp2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main: (args) =
2+
std::cout << "args.argc is (args.argc)$, and args.argv[0] is (args.argv[0])$\n";

regression-tests/pure2-more-wildcards.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
less_than: (value:_) -> _ =
3-
:(x:_) = x < value$;
3+
:(x:_) -> _ = x < value$;
44

55
main: () -> int
66
= {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
args.argc is 1, and args.argv[0] is ./test.exe

regression-tests/test-results/clang-12/pure2-main-args.cpp.output

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
args.argc is 1, and args.argv[0] is ./test.exe

regression-tests/test-results/gcc-10/pure2-main-args.cpp.output

Whitespace-only changes.

regression-tests/test-results/mixed-captures-in-expressions-and-postconditions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ auto insert_at(cpp2::in<int> where, cpp2::in<int> val) -> void;
2222
"hello", "2022"};
2323

2424
std::string y {"\n"};
25-
auto callback {[_0 = (&y)](auto const& x){std::cout << x << *cpp2::assert_not_null(_0);}};
25+
auto callback {[_0 = (&y)](auto const& x) -> void{std::cout << x << *cpp2::assert_not_null(_0);}};
2626

2727
std::ranges::for_each( vec, callback );
2828
y = "-ish\n";

regression-tests/test-results/mixed-fixed-type-aliases.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace my {
1313
#line 8 "mixed-fixed-type-aliases.cpp2"
1414
auto test(auto const& x) -> void;
1515
#line 15 "mixed-fixed-type-aliases.cpp2"
16-
[[nodiscard]] auto main(int argc, char **argv) -> int;
16+
[[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int;
1717

1818
//=== Cpp2 definitions ==========================================================
1919

@@ -26,8 +26,8 @@ auto test(auto const& x) -> void{
2626
<< "\n";
2727
}
2828

29-
[[nodiscard]] auto main(int argc, char **argv) -> int{
30-
auto args = cpp2::args(argc, argv);
29+
[[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int{
30+
auto args = cpp2::make_args(argc_, argv_);
3131
#line 16 "mixed-fixed-type-aliases.cpp2"
3232
my::u16 y {42};
3333
test(std::move(y));

regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
std::for_each(
2626
CPP2_UFCS_0(begin, view),
2727
CPP2_UFCS_0(end, view),
28-
[](auto& x ) { return x += "-ish"; }
28+
[](auto& x ) -> void { x += "-ish"; }
2929
);
3030

3131
// Initializating from a function expression
32-
auto callback {[](auto& x) { return x += " maybe"; }};
32+
auto callback {[](auto& x) -> void { x += " maybe"; }};
3333
std::for_each(
3434
CPP2_UFCS_0(begin, view),
3535
CPP2_UFCS_0(end, view),

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
auto y {"\n"};
2626
std::ranges::for_each
27-
( view, [_0 = std::move(y)](auto const& x){std::cout << x << _0;});
27+
( view, [_0 = std::move(y)](auto const& x) -> void{std::cout << x << _0;});
2828

29-
auto callback {[](auto& x) { return x += "-ish"; }};
29+
auto callback {[](auto& x) -> void { x += "-ish"; }};
3030
std::ranges::for_each( view, std::move(callback));
3131

3232
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
std::span view {vec};
2424

2525
std::ranges::for_each
26-
( view, [](auto const& x){std::cout << x << "\n";});
26+
( view, [](auto const& x) -> void{std::cout << x << "\n";});
2727

28-
auto callback {[](auto& x) { return x += "-ish"; }};
28+
auto callback {[](auto& x) -> void { x += "-ish"; }};
2929
std::ranges::for_each( view, std::move(callback));
3030

3131
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )

regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
std::span view {vec};
2424

2525
std::string y {"\n"};
26-
std::ranges::for_each( view, [_0 = (&y)](auto const& x){
26+
std::ranges::for_each( view, [_0 = (&y)](auto const& x) -> void{
2727
std::cout << CPP2_UFCS_0(c_str, (*cpp2::assert_not_null((_0)))) << x << *cpp2::assert_not_null(_0);
2828
} );
2929

30-
auto callback {[](auto& x) { return x += "-ish"; }};
30+
auto callback {[](auto& x) -> void { x += "-ish"; }};
3131
std::ranges::for_each( view, std::move(callback));
3232

3333
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )

regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
auto y {"\n"};
2626
std::ranges::for_each
27-
( view, [_0 = std::move(y)](auto const& x){std::cout << _0 << x << _0;});
27+
( view, [_0 = std::move(y)](auto const& x) -> void{std::cout << _0 << x << _0;});
2828

29-
auto callback {[](auto& x) { return x += "-ish"; }};
29+
auto callback {[](auto& x) -> void { x += "-ish"; }};
3030
std::ranges::for_each( view, std::move(callback));
3131

3232
for ( auto const& cpp2_range = view; auto const& str : cpp2_range )
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
args.argc is 1, and args.argv[0] is test.exe
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-main-args.cpp

regression-tests/test-results/pure2-forward-return.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#line 1 "pure2-forward-return.cpp2"
1818

1919
[[nodiscard]] auto first(auto&& rng) -> decltype(auto) {
20+
cpp2::Bounds.expects(!(std::empty(rng)), "");
21+
#line 3 "pure2-forward-return.cpp2"
2022

2123

2224
return *cpp2::assert_not_null(std::begin(CPP2_FORWARD(rng))); }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
#include "cpp2util.h"
5+
6+
7+
#line 1 "pure2-main-args.cpp2"
8+
auto main(int const argc_, char const* const* const argv_) -> int;
9+
10+
//=== Cpp2 definitions ==========================================================
11+
12+
#line 1 "pure2-main-args.cpp2"
13+
auto main(int const argc_, char const* const* const argv_) -> int {
14+
auto args = cpp2::make_args(argc_, argv_);
15+
#line 2 "pure2-main-args.cpp2"
16+
std::cout << "args.argc is " + cpp2::to_string(args.argc) + ", and args.argv[0] is " + cpp2::to_string(cpp2::assert_in_bounds(args.argv, 0)) + "\n"; }
17+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-main-args.cpp2... ok (all Cpp2, passes safety checks)
2+

regression-tests/test-results/pure2-more-wildcards.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#line 1 "pure2-more-wildcards.cpp2"
1515

1616
[[nodiscard]] auto less_than(auto const& value) -> auto {
17-
return [_0 = value](auto const& x) { return cpp2::cmp_less(x,_0); }; }
17+
return [_0 = value](auto const& x) -> auto { return cpp2::cmp_less(x,_0); }; }
1818

1919
[[nodiscard]] auto main() -> int
2020
{

0 commit comments

Comments
 (0)