Skip to content

Commit

Permalink
All parameters default to in
Browse files Browse the repository at this point in the history
This is the way it used to be, and IMO it's better to have the same default everywhere

For a time I tried out making `copy` the default for statement/block parameter, because I had been writing several of those in `reflect.h2` (it was really only 7 places though)

But putting back the explicit `copy` in 7 places in `reflect.h2` was not bad, and it actually made the code's value flow clearer -- and clear value flow is super important and a foundational principle throughout Cpp2, from parameter directions to mandatory explicit discard to `const` by default
  • Loading branch information
hsutter committed Oct 1, 2023
1 parent 1686e41 commit d215985
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
6 changes: 3 additions & 3 deletions regression-tests/test-results/pure2-interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class item {
#line 9 "pure2-interpolation.cpp2"
[[nodiscard]] auto main() -> int{
{
auto x = 0;
auto const& x = 0;

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

#line 25 "pure2-interpolation.cpp2"
{
std::cout << std::left << std::setw(20) << CPP2_UFCS_0(name, x) << " color " << std::left << std::setw(10) << CPP2_UFCS_0(color, x) << " price " << std::setw(10) << std::setprecision(3) << CPP2_UFCS_0(price, x) << " in stock = " << std::boolalpha << (cpp2::cmp_greater(CPP2_UFCS_0(count, x),0)) << "\n";

std::cout << cpp2::to_string(CPP2_UFCS_0(name, x), "{:20}") + " color " + cpp2::to_string(CPP2_UFCS_0(color, x), "{:10}") + " price " + cpp2::to_string(CPP2_UFCS_0(price, x), "{: <10.2f}") + " in stock = " + cpp2::to_string(cpp2::cmp_greater(CPP2_UFCS_0(count, std::move(x)),0)) + "\n";
std::cout << cpp2::to_string(CPP2_UFCS_0(name, x), "{:20}") + " color " + cpp2::to_string(CPP2_UFCS_0(color, x), "{:10}") + " price " + cpp2::to_string(CPP2_UFCS_0(price, x), "{: <10.2f}") + " in stock = " + cpp2::to_string(cpp2::cmp_greater(CPP2_UFCS_0(count, x),0)) + "\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ auto main(int const argc_, char** argv_) -> int{
#line 3 "pure2-statement-scope-parameters.cpp2"
auto local_int {42};
{
auto i = local_int;
auto const& i = local_int;

// 'in' (read-only) statement scope variable
#line 6 "pure2-statement-scope-parameters.cpp2"
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.2.1 Build 8A01:0916
cppfront compiler v0.2.1 Build 8A01:1011
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8A01:0916"
"8A01:1011"
3 changes: 1 addition & 2 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -7112,8 +7112,7 @@ class parser

auto n = std::make_unique<parameter_declaration_node>();
n->pass =
is_returns ? passing_style::out :
is_statement ? passing_style::copy :
is_returns ? passing_style::out :
passing_style::in;
n->pos = curr().position();

Expand Down
28 changes: 14 additions & 14 deletions source/reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class alias_declaration;
#line 827 "reflect.h2"
class value_member_info;

#line 1281 "reflect.h2"
#line 1280 "reflect.h2"
}

}
Expand Down Expand Up @@ -693,14 +693,14 @@ auto flag_enum(meta::type_declaration& t) -> void;

auto cpp2_union(meta::type_declaration& t) -> void;

#line 1175 "reflect.h2"
#line 1174 "reflect.h2"
//-----------------------------------------------------------------------
//
// print - output a pretty-printed visualization of t
//
auto print(cpp2::in<meta::type_declaration> t) -> void;

#line 1185 "reflect.h2"
#line 1184 "reflect.h2"
//-----------------------------------------------------------------------
//
// apply_metafunctions
Expand All @@ -711,7 +711,7 @@ auto print(cpp2::in<meta::type_declaration> t) -> void;
auto const& error
) -> bool;

#line 1281 "reflect.h2"
#line 1280 "reflect.h2"
}

}
Expand Down Expand Up @@ -1552,8 +1552,8 @@ auto value = 0;
#line 1086 "reflect.h2"
for (

auto const& m : CPP2_UFCS_0(get_members, t) ) { do
if ( CPP2_UFCS_0(is_member_object, m))
auto const& m : CPP2_UFCS_0(get_members, t) ) { do
if ( CPP2_UFCS_0(is_member_object, m))
{
CPP2_UFCS(require, m, CPP2_UFCS_0(is_public, m) || CPP2_UFCS_0(is_default_access, m),
"a union alternative cannot be protected or private");
Expand Down Expand Up @@ -1599,22 +1599,22 @@ std::string storage = " _storage: std::aligned_storage_t<cpp2::max( ";
{
std::string comma = "";

#line 1129 "reflect.h2"
#line 1128 "reflect.h2"
for (

auto const& e : alternatives ) { do {
storage += comma + "sizeof(" + cpp2::to_string(e.type) + ")";
} while (false); comma = ", "; }
}

#line 1135 "reflect.h2"
#line 1134 "reflect.h2"
storage += " )> = ();\n";
CPP2_UFCS(add_member, t, std::move(storage));
}
}

// Provide discriminator
#line 1140 "reflect.h2"
#line 1139 "reflect.h2"
CPP2_UFCS(add_member, t, " _discriminator: " + cpp2::to_string(std::move(discriminator_type)) + " = -1;\n");

// Add the alternatives: is_alternative, get_alternative, and set_alternative
Expand All @@ -1636,7 +1636,7 @@ std::string destroy = " private destroy: (inout this) = {\n";

// Add destroy

#line 1159 "reflect.h2"
#line 1158 "reflect.h2"
{
for (
auto const& a : alternatives ) {
Expand All @@ -1650,17 +1650,17 @@ std::string destroy = " private destroy: (inout this) = {\n";
}

// Add the destructor
#line 1171 "reflect.h2"
#line 1170 "reflect.h2"
CPP2_UFCS(add_member, t, " operator=: (move this) = { destroy(); } ");
}

#line 1179 "reflect.h2"
#line 1178 "reflect.h2"
auto print(cpp2::in<meta::type_declaration> t) -> void
{
std::cout << CPP2_UFCS_0(print, t) << "\n";
}

#line 1189 "reflect.h2"
#line 1188 "reflect.h2"
[[nodiscard]] auto apply_metafunctions(
declaration_node& n,
type_declaration& rtype,
Expand Down Expand Up @@ -1752,7 +1752,7 @@ auto print(cpp2::in<meta::type_declaration> t) -> void
return true;
}

#line 1281 "reflect.h2"
#line 1280 "reflect.h2"
}

}
Expand Down
23 changes: 11 additions & 12 deletions source/reflect.h2
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ compiler_services: @polymorphic_base @copyable type =

// First split this string into source_lines
//
(newline_pos := source.find('\n'))
(copy newline_pos := source.find('\n'))
if source.ssize() > 1
&& newline_pos != source.npos
{
Expand Down Expand Up @@ -847,7 +847,7 @@ basic_enum: (

found_non_numeric := false;

(value: std::string = "-1")
(copy value: std::string = "-1")
for t.get_members()
do (m)
if m.is_member_object()
Expand Down Expand Up @@ -960,7 +960,7 @@ basic_enum: (
}

// Provide a 'to_string' function to print enumerator name(s)
(to_string: std::string = " to_string: (this) -> std::string = { \n")
(copy to_string: std::string = " to_string: (this) -> std::string = { \n")
{
if bitwise {
to_string += " ret : std::string = \"(\";\n";
Expand Down Expand Up @@ -1082,11 +1082,11 @@ union: (inout t : meta::type_declaration)

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

(value := 0)
for t.get_members()
next value++
do (m)
if m.is_member_object()
(copy value := 0)
for t.get_members()
next value++
do (m)
if m.is_member_object()
{
m.require( m.is_public() || m.is_default_access(),
"a union alternative cannot be protected or private");
Expand Down Expand Up @@ -1122,10 +1122,9 @@ union: (inout t : meta::type_declaration)
t.remove_marked_members();

// Provide storage
(storage: std::string = " _storage: std::aligned_storage_t<cpp2::max( ")
(copy storage: std::string = " _storage: std::aligned_storage_t<cpp2::max( ")
{

(comma: std::string = "")
(copy comma: std::string = "")
for alternatives
next comma = ", "
do (e) {
Expand Down Expand Up @@ -1155,7 +1154,7 @@ union: (inout t : meta::type_declaration)
}

// Add destroy
(destroy: std::string = " private destroy: (inout this) = {\n")
(copy destroy: std::string = " private destroy: (inout this) = {\n")
{
for alternatives
do (a) {
Expand Down

0 comments on commit d215985

Please sign in to comment.