Skip to content

Commit 44a7255

Browse files
committed
Added @noisy metafunction - initial version
Note: This is the first metafunction that reflects and generates function bodies. This type metafunction makes each of the type's functions print its name and signature, so the programmer can see what functions are being called. This initial version ignores functions named `operator=` or with single-statement (or no) bodies. In future commits I intend to add support for: - `operator=`, that inserts new statements only after the member object value-set statement - functions with single-statement or no bodies, that are first converted to compound-statement bodies
1 parent 9901b01 commit 44a7255

File tree

6 files changed

+726
-558
lines changed

6 files changed

+726
-558
lines changed

regression-tests/test-results/pure2-last-use.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ class issue_857_4 {
287287
public: std::move_only_function<int()> mf;
288288
public: std::move_only_function<int([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)> mg;
289289
public: issue_857_4(auto&& f_, auto&& g_, auto&& mf_, auto&& mg_)
290-
CPP2_REQUIRES_ (std::is_convertible_v<CPP2_TYPEOF(f_), std::add_const_t<std::add_pointer_t<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(g_), std::add_const_t<std::add_pointer_t<int(cpp2::impl::in<int> in_)>>&> && std::is_convertible_v<CPP2_TYPEOF(mf_), std::add_const_t<std::move_only_function<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(mg_), std::add_const_t<std::move_only_function<int(cpp2::impl::in<int> in_)>>&>) ;
290+
CPP2_REQUIRES_ (std::is_convertible_v<CPP2_TYPEOF(f_), std::add_const_t<std::add_pointer_t<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(g_), std::add_const_t<std::add_pointer_t<int([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)>>&> && std::is_convertible_v<CPP2_TYPEOF(mf_), std::add_const_t<std::move_only_function<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(mg_), std::add_const_t<std::move_only_function<int([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)>>&>) ;
291291

292292
// h0: (move this) = _ = mf();
293293
// h1: (move this) = _ = this.mf();
@@ -772,11 +772,11 @@ requires (std::is_convertible_v<CPP2_TYPEOF(i_), std::add_const_t<std::add_lvalu
772772
return *this;}
773773
issue_857_6::issue_857_6(){}
774774
issue_857_4::issue_857_4(auto&& f_, auto&& g_, auto&& mf_, auto&& mg_)
775-
requires (std::is_convertible_v<CPP2_TYPEOF(f_), std::add_const_t<std::add_pointer_t<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(g_), std::add_const_t<std::add_pointer_t<int(cpp2::impl::in<int> in_)>>&> && std::is_convertible_v<CPP2_TYPEOF(mf_), std::add_const_t<std::move_only_function<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(mg_), std::add_const_t<std::move_only_function<int(cpp2::impl::in<int> in_)>>&>)
776-
: f{ CPP2_FORWARD(f_) }
777-
, g{ CPP2_FORWARD(g_) }
778-
, mf{ CPP2_FORWARD(mf_) }
779-
, mg{ CPP2_FORWARD(mg_) }{}
775+
requires (std::is_convertible_v<CPP2_TYPEOF(f_), std::add_const_t<std::add_pointer_t<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(g_), std::add_const_t<std::add_pointer_t<int([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)>>&> && std::is_convertible_v<CPP2_TYPEOF(mf_), std::add_const_t<std::move_only_function<int()>>&> && std::is_convertible_v<CPP2_TYPEOF(mg_), std::add_const_t<std::move_only_function<int([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)>>&>)
776+
: f{ CPP2_FORWARD(f_) }
777+
, g{ CPP2_FORWARD(g_) }
778+
, mf{ CPP2_FORWARD(mf_) }
779+
, mg{ CPP2_FORWARD(mg_) }{}
780780
#line 267 "pure2-last-use.cpp2"
781781
// OK: The implicit `this` is moved, not `i`.
782782

regression-tests/test-results/version

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

2-
cppfront compiler v0.8.0 Build 9B01:1726
2+
cppfront compiler v0.8.0 Build 9B08:1148
33
SPDX-License-Identifier Apache-2.0 WITH LLVM-exception
44
Copyright (c) 2022-2024 Herb Sutter

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"9B01:1726"
1+
"9B08:1148"

source/parse.h

Lines changed: 105 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,21 @@ struct compound_statement_node
19131913
return open_brace;
19141914
}
19151915

1916+
auto add_statement(
1917+
std::unique_ptr<statement_node>&& statement,
1918+
int before_pos
1919+
)
1920+
-> bool
1921+
{
1922+
// Adopt this statement into our list of statements
1923+
statements.insert(
1924+
statements.begin() + std::clamp( before_pos, 0, unchecked_narrow<int>(std::ssize(statements)) ),
1925+
std::move(statement)
1926+
);
1927+
1928+
return true;
1929+
}
1930+
19161931
auto visit(auto& v, int depth) -> void;
19171932
};
19181933

@@ -2341,7 +2356,9 @@ struct parameter_declaration_node
23412356

23422357
// API
23432358
//
2344-
auto to_string() const
2359+
auto to_string(
2360+
bool verbose = true
2361+
) const
23452362
-> std::string;
23462363

23472364
auto has_name() const
@@ -2439,15 +2456,25 @@ struct parameter_declaration_list_node
24392456

24402457
// API
24412458
//
2442-
auto to_string() const
2459+
auto to_string(
2460+
bool verbose = true
2461+
) const
24432462
-> std::string
24442463
{
24452464
assert(open_paren && close_paren);
24462465

24472466
auto ret = open_paren->to_string();
24482467

24492468
for (auto const& p: parameters) {
2450-
ret += p->to_string() + ", ";
2469+
ret += p->to_string(verbose) + ", ";
2470+
}
2471+
2472+
if (
2473+
!verbose
2474+
&& std::ssize(ret) > 3
2475+
)
2476+
{
2477+
ret.resize( std::ssize(ret) - 2 ); // omit the final ", "
24512478
}
24522479

24532480
ret += close_paren->as_string_view();
@@ -2660,6 +2687,15 @@ struct function_type_node
26602687
return {};
26612688
}
26622689

2690+
auto parameters_to_string(
2691+
bool verbose = true
2692+
) const
2693+
-> std::string
2694+
{
2695+
assert (parameters);
2696+
return parameters->to_string(verbose);
2697+
}
2698+
26632699
auto has_bool_return_type() const
26642700
-> bool
26652701
{
@@ -3034,7 +3070,12 @@ struct declaration_node
30343070

30353071
// API
30363072
//
3037-
auto to_string() const
3073+
auto to_string(
3074+
bool verbose = true
3075+
) const
3076+
-> std::string;
3077+
3078+
auto signature_to_string() const
30383079
-> std::string;
30393080

30403081
auto is_template_parameter() const
@@ -3635,6 +3676,12 @@ struct declaration_node
36353676
;
36363677
}
36373678

3679+
auto get_compound_initializer() const
3680+
-> compound_statement_node*
3681+
{
3682+
return initializer->get_if<compound_statement_node>();
3683+
}
3684+
36383685
auto is_function_with_this() const
36393686
-> bool
36403687
{
@@ -4087,7 +4134,9 @@ struct declaration_node
40874134
};
40884135

40894136

4090-
auto parameter_declaration_node::to_string() const
4137+
auto parameter_declaration_node::to_string(
4138+
bool verbose /*= true*/
4139+
) const
40914140
-> std::string
40924141
{
40934142
auto ret = std::string{};
@@ -4105,7 +4154,15 @@ auto parameter_declaration_node::to_string() const
41054154
;
41064155
}
41074156

4108-
ret += to_string_view(pass) + declaration->to_string();
4157+
if (
4158+
verbose
4159+
|| pass != passing_style::in
4160+
)
4161+
{
4162+
ret += to_string_view(pass);
4163+
ret += " ";
4164+
}
4165+
ret += declaration->to_string( verbose );
41094166

41104167
return ret;
41114168
}
@@ -4870,15 +4927,32 @@ auto pretty_print_visualize(type_node const& n, int indent)
48704927
-> std::string;
48714928
auto pretty_print_visualize(namespace_node const& n, int indent)
48724929
-> std::string;
4873-
auto pretty_print_visualize(declaration_node const& n, int indent, bool include_metafunctions_list = false)
4930+
auto pretty_print_visualize(declaration_node const& n, int indent, bool include_metafunctions_list = false, bool verbose = true)
48744931
-> std::string;
48754932

48764933

4877-
auto declaration_node::to_string() const
4934+
auto declaration_node::to_string(
4935+
bool verbose /*= true*/
4936+
) const
48784937
-> std::string
48794938
{
48804939
// These need to be unified someday... let's not duplicate this long function...
4881-
return pretty_print_visualize(*this, 0);
4940+
return pretty_print_visualize(*this, 0, false, verbose);
4941+
}
4942+
4943+
4944+
auto declaration_node::signature_to_string() const
4945+
-> std::string
4946+
{
4947+
auto ret = std::string{};
4948+
if (auto fname = name()) {
4949+
ret += *fname;
4950+
}
4951+
if (auto func = std::get_if<a_function>(&type)) {
4952+
assert ((*func)->parameters);
4953+
ret += (*func)->parameters->to_string(false);
4954+
}
4955+
return ret;
48824956
}
48834957

48844958

@@ -5593,7 +5667,12 @@ auto pretty_print_visualize(namespace_node const&)
55935667
}
55945668

55955669

5596-
auto pretty_print_visualize(declaration_node const& n, int indent, bool include_metafunctions_list /* = false */ )
5670+
auto pretty_print_visualize(
5671+
declaration_node const& n,
5672+
int indent,
5673+
bool include_metafunctions_list /* = false */,
5674+
bool verbose /* = true */
5675+
)
55975676
-> std::string
55985677
{
55995678
indent_spaces = 4;
@@ -5627,23 +5706,25 @@ auto pretty_print_visualize(declaration_node const& n, int indent, bool include_
56275706
}
56285707

56295708
auto initializer = std::string{};
5630-
if (n.initializer) {
5631-
auto adjusted_indent = indent;
5632-
if (!n.name()) {
5633-
++adjusted_indent;
5634-
}
5635-
initializer = " =";
5636-
if (n.is_function() && n.is_constexpr) {
5637-
initializer += "=";
5709+
if (verbose) {
5710+
if (n.initializer) {
5711+
auto adjusted_indent = indent;
5712+
if (!n.name()) {
5713+
++adjusted_indent;
5714+
}
5715+
initializer = " =";
5716+
if (n.is_function() && n.is_constexpr) {
5717+
initializer += "=";
5718+
}
5719+
initializer += " " + pretty_print_visualize(*n.initializer, adjusted_indent);
5720+
if (initializer.ends_with(";;")) {
5721+
initializer.pop_back();
5722+
}
56385723
}
5639-
initializer += " " + pretty_print_visualize(*n.initializer, adjusted_indent);
5640-
if (initializer.ends_with(";;")) {
5641-
initializer.pop_back();
5724+
else if (!n.is_parameter()) {
5725+
initializer = ";";
56425726
}
56435727
}
5644-
else if (!n.is_parameter()) {
5645-
initializer = ";";
5646-
}
56475728

56485729
// Then slot them in where appropriate
56495730

0 commit comments

Comments
 (0)