Skip to content

fix(to_cpp1): direct-initialize statement parameter and constexpr variable #924

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 5 commits into from
Jan 9, 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
1 change: 1 addition & 0 deletions regression-tests/pure2-print.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ outer: @print type = {

object_alias_1: i8 == 42;
object_alias_2: == 42;
object_alias_3: std::array == (4,5,6);


var: ::outer::mytype = ();
Expand Down
5 changes: 5 additions & 0 deletions regression-tests/pure2-statement-scope-parameters.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ main: (args) = {
i++;
}
std::cout << local_int << "\n"; // prints 43

_: std::chrono::duration<i32> = 0;
(copy _: std::chrono::duration<i32> = 0) { }
_: std::in_place_t = ();
(copy _: std::in_place_t = ()) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ auto main() -> int{
if (cpp2::Default.has_handler() && !(std::move(f) == 0) ) { cpp2::Default.report_violation(""); }
}
{
auto const& f = t().f();
auto const& f{t().f()};
#line 33 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
if (cpp2::Default.has_handler() && !(f == 0) ) { cpp2::Default.report_violation(""); }
}
{
auto const& f = t().f();
auto const& f{t().f()};
#line 34 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
if (cpp2::Default.has_handler() && !(f == 0) ) { cpp2::Default.report_violation(""); }
}
Expand Down Expand Up @@ -113,12 +113,12 @@ auto const& f = t().f();
{
auto f {[]() mutable -> void{
{
cpp2::in<identity> f = identity();
cpp2::in<identity> f{identity()};
#line 59 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
if (cpp2::Default.has_handler() && !(CPP2_UFCS(f)(t()) == 0) ) { cpp2::Default.report_violation(""); }
}
{
cpp2::in<identity> f = identity();
cpp2::in<identity> f{identity()};
#line 60 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
static_cast<void>(CPP2_UFCS(f)(u()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ extern cpp2::i32 y;
#line 5 "pure2-bugfix-for-unbraced-function-expression.cpp2"
[[nodiscard]] auto main() -> int{
{
auto const& x = t();
auto const& x{t()};
#line 6 "pure2-bugfix-for-unbraced-function-expression.cpp2"
{CPP2_ASSERT_IN_BOUNDS(x, []() mutable -> auto { return 0; }); }
}
{
auto const& x = t();
auto const& x{t()};
#line 7 "pure2-bugfix-for-unbraced-function-expression.cpp2"
{CPP2_ASSERT_IN_BOUNDS(x, []() mutable -> auto { return 0; }); }
}
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/test-results/pure2-interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class item {
#line 9 "pure2-interpolation.cpp2"
[[nodiscard]] auto main() -> int{
{
auto const& x = 0;
auto const& x{0};

#line 11 "pure2-interpolation.cpp2"
{
Expand All @@ -57,7 +57,7 @@ auto const& x = 0;
}
}
{
auto const& x = item();
auto const& x{item()};

#line 25 "pure2-interpolation.cpp2"
{
Expand Down
17 changes: 9 additions & 8 deletions regression-tests/test-results/pure2-print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ CPP2_REQUIRES_ ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) ;

public: static auto test() -> void;

#line 91 "pure2-print.cpp2"
#line 92 "pure2-print.cpp2"
public: template<typename ...Ts> class x {
private: std::tuple<Ts...> tup {};
public: x() = default;
public: x(x const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(x const&) -> void = delete;

#line 93 "pure2-print.cpp2"
#line 94 "pure2-print.cpp2"
};

public: template<typename ...Args> static auto print(std::ostream& out, Args const& ...args) -> void
CPP2_REQUIRES_ (cpp2::cmp_greater_eq(sizeof...(Args),0u)) ;

#line 99 "pure2-print.cpp2"
#line 100 "pure2-print.cpp2"
public: template<typename ...Args> [[nodiscard]] static auto all(Args const& ...args) -> bool;
public: outer() = default;
public: outer(outer const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(outer const&) -> void = delete;


#line 102 "pure2-print.cpp2"
#line 103 "pure2-print.cpp2"
};

auto main() -> int;
Expand Down Expand Up @@ -169,8 +169,9 @@ requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) {(std::cout << ..

cpp2::i8 constexpr object_alias_1 = 42;
auto constexpr object_alias_2 = 42;
std::array constexpr object_alias_3{ 4, 5, 6 };

#line 82 "pure2-print.cpp2"
#line 83 "pure2-print.cpp2"
::outer::mytype var {};
cout << CPP2_UFCS(g)(var, 42) << "\n";

Expand All @@ -180,17 +181,17 @@ requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) {(std::cout << ..
() << "\n";
}

#line 95 "pure2-print.cpp2"
#line 96 "pure2-print.cpp2"
template<typename ...Args> auto outer::print(std::ostream& out, Args const& ...args) -> void
requires (cpp2::cmp_greater_eq(sizeof...(Args),0u)) {
#line 96 "pure2-print.cpp2"
#line 97 "pure2-print.cpp2"
(out << ... << args);
}

template<typename ...Args> [[nodiscard]] auto outer::all(Args const& ...args) -> bool {
return (... && args); }

#line 104 "pure2-print.cpp2"
#line 105 "pure2-print.cpp2"
auto main() -> int{
outer::test();
}
Expand Down
1 change: 1 addition & 0 deletions regression-tests/test-results/pure2-print.cpp2.output
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ outer:/* @print */ type =
type_alias: type == array<int, 10>;
object_alias_1: i8 == 42;
object_alias_2: == 42;
object_alias_3: std::array == (4, 5, 6);
var: ::outer::mytype = ();
cout << var.g(42) << "\n";
cout << inspect var.g(42) -> namespace_alias::string {
Expand Down
19 changes: 17 additions & 2 deletions regression-tests/test-results/pure2-statement-scope-parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ auto main(int const argc_, char** argv_) -> int{
#line 3 "pure2-statement-scope-parameters.cpp2"
auto local_int {42};
{
auto const& i = local_int;
auto const& i{local_int};

// 'in' (read-only) statement scope variable
#line 6 "pure2-statement-scope-parameters.cpp2"
Expand All @@ -35,7 +35,7 @@ auto const& i = local_int;
}
}
{
auto& i = local_int;
auto& i{local_int};

// 'inout' (read-write) statement scope variable
#line 11 "pure2-statement-scope-parameters.cpp2"
Expand All @@ -45,5 +45,20 @@ auto& i = local_int;
}
#line 14 "pure2-statement-scope-parameters.cpp2"
std::cout << std::move(local_int) << "\n";// prints 43

std::chrono::duration<cpp2::i32> auto_16_5 {0};
{
[[maybe_unused]] std::chrono::duration<cpp2::i32> unnamed_param_1{0};
#line 17 "pure2-statement-scope-parameters.cpp2"
{}
}
#line 18 "pure2-statement-scope-parameters.cpp2"
std::in_place_t auto_18_5 {};
{
[[maybe_unused]] std::in_place_t unnamed_param_1{};
#line 19 "pure2-statement-scope-parameters.cpp2"
{}
}
#line 20 "pure2-statement-scope-parameters.cpp2"
}

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace N {
auto myfunc() -> void{
N1::pmr_vec<myclass::str> v {"xyzzy", "plugh"};
{
auto const& v2 = std::move(v);
auto const& v2{std::move(v)};

#line 21 "pure2-type-and-namespace-aliases.cpp2"
for ( auto const& s : v2 )
Expand Down
18 changes: 9 additions & 9 deletions source/reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ namespace meta {
static_cast<void>(CPP2_UFCS(emplace_back)((*cpp2::assert_not_null(_1)), s, source_line::category::cpp2));
}};
{
auto newline_pos = CPP2_UFCS(find)(source, '\n');
auto newline_pos{CPP2_UFCS(find)(source, '\n')};

// First split this string into source_lines
//
Expand Down Expand Up @@ -1483,7 +1483,7 @@ auto basic_enum(

auto found_non_numeric {false};
{
std::string value = "-1";
std::string value{"-1"};

#line 957 "reflect.h2"
for (
Expand Down Expand Up @@ -1613,7 +1613,7 @@ std::string value = "-1";
CPP2_UFCS(add_member)(t, " operator= : (out this, that) == { }");
CPP2_UFCS(add_member)(t, " operator<=> : (this, that) -> std::strong_ordering;");
{
std::string to_string = " to_string: (this) -> std::string = { \n";
std::string to_string{" to_string: (this) -> std::string = { \n"};

// Provide a 'to_string' function to print enumerator name(s)

Expand Down Expand Up @@ -1696,7 +1696,7 @@ auto cpp2_union(meta::type_declaration& t) -> void
{
std::vector<value_member_info> alternatives {};
{
auto value = 0;
auto value{0};

// 1. Gather: All the user-written members, and find/compute the max size

Expand Down Expand Up @@ -1745,14 +1745,14 @@ auto value = 0;

CPP2_UFCS(remove_marked_members)(t);
{
std::string storage = " _storage: cpp2::aligned_storage<cpp2::max( ";
std::string storage{" _storage: cpp2::aligned_storage<cpp2::max( "};

// Provide storage

#line 1250 "reflect.h2"
{
{
std::string comma = "";
std::string comma{""};

#line 1252 "reflect.h2"
for (
Expand All @@ -1765,7 +1765,7 @@ std::string comma = "";
#line 1258 "reflect.h2"
storage += "), cpp2::max( ";
{
std::string comma = "";
std::string comma{""};

#line 1261 "reflect.h2"
for (
Expand Down Expand Up @@ -1800,7 +1800,7 @@ std::string comma = "";
CPP2_UFCS(add_member)(t, " set_" + cpp2::to_string(a.name) + ": (inout this, forward _args...: _) = { if !is_" + cpp2::to_string(a.name) + "() { _destroy(); std::construct_at( reinterpret_cast<*" + cpp2::to_string(a.type) + ">(_storage&), _args...); } else { reinterpret_cast<*" + cpp2::to_string(a.type) + ">(_storage&)* = :" + cpp2::to_string(a.type) + " = (_args...); } _discriminator = " + cpp2::to_string(a.value) + "; }\n");
}
{
std::string destroy = " private _destroy: (inout this) = {\n";
std::string destroy{" private _destroy: (inout this) = {\n"};

// Add destroy

Expand All @@ -1824,7 +1824,7 @@ std::string destroy = " private _destroy: (inout this) = {\n";
// Add default constructor
CPP2_UFCS(add_member)(t, " operator=: (out this) = { }");
{
std::string value_set = "";
std::string value_set{""};

// Add copy/move construction and assignment

Expand Down
45 changes: 34 additions & 11 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ auto pad(int padding)
};
}

auto multi_return_type_name(declaration_node const& n)
auto multi_return_type_name(declaration_node const& n)
-> std::string
{
// When generating a multi-return struct, also enable multi-return
Expand Down Expand Up @@ -1326,7 +1326,7 @@ class cppfront
printer.print_extra(
"\n// Generated by cppfront "
#include "version.info"
" build "
" build "
#include "build.info"
);
}
Expand Down Expand Up @@ -4083,7 +4083,7 @@ class cppfront
printer.print_extra( "{");
for (auto& param : n.parameters->parameters) {
printer.print_extra( "\n");
printer.print_extra( print_to_string(*param) );
printer.print_extra( print_to_string(*param, false, false, true) );
}
}

Expand Down Expand Up @@ -4170,7 +4170,8 @@ class cppfront
auto emit(
parameter_declaration_node const& n,
bool is_returns = false,
bool is_template_parameter = false
bool is_template_parameter = false,
bool is_statement = false
)
-> void
{ STACKINSTR
Expand Down Expand Up @@ -4492,8 +4493,16 @@ class cppfront
)
{
auto guard = stack_element(current_declarations, &*n.declaration);
printer.print_cpp2( " = ", n.declaration->initializer->position() );
emit(*n.declaration->initializer);
if (is_statement) {
printer.print_cpp2( "{", n.declaration->initializer->position() );
}
else {
printer.print_cpp2( " = ", n.declaration->initializer->position() );
}
emit(*n.declaration->initializer, !is_statement);
if (is_statement) {
printer.print_cpp2( "};", n.declaration->initializer->position() );
}
}
}

Expand Down Expand Up @@ -4679,7 +4688,7 @@ class cppfront
);
}
else {
emit(*n.parameters, false, false, generating_postfix_inc_dec);
emit(*n.parameters, false, false, generating_postfix_inc_dec);
}

// For an anonymous function, the emitted lambda is 'constexpr' or 'mutable'
Expand Down Expand Up @@ -5511,6 +5520,22 @@ class cppfront
type = print_to_string(*a->type_id);
}

auto print_initializer_to_string = [&](expression_node const& n) -> std::string {
// If this expression is just a single expression-list, we can
// take over direct control of emitting it without needing to
// go through the whole grammar, and surround it with braces
if (n.is_expression_list()) {
return "{ "
+ print_to_string(*n.get_expression_list(), false)
+ " }";
}
// Otherwise, just emit the general expression as usual
else {
return " = "
+ print_to_string(n);
}
};

// (*) If this is at type scope, Cpp1 requires an out-of-line declaration dance
// for some cases to work - see https://stackoverflow.com/questions/11928089/
if (n.parent_is_type())
Expand Down Expand Up @@ -5542,8 +5567,7 @@ class cppfront
+ " "
+ type_qualification_if_any_for(n)
+ print_to_string(*n.identifier)
+ " = "
+ print_to_string( *std::get<alias_node::an_object>(a->initializer) )
+ print_initializer_to_string( *std::get<alias_node::an_object>(a->initializer) )
+ ";\n",
n.position()
);
Expand All @@ -5564,8 +5588,7 @@ class cppfront
type + " "
+ intro + " "
+ print_to_string(*n.identifier)
+ " = "
+ print_to_string( *std::get<alias_node::an_object>(a->initializer) )
+ print_initializer_to_string( *std::get<alias_node::an_object>(a->initializer) )
+ ";\n",
n.position()
);
Expand Down