Skip to content

Commit

Permalink
Added @noisy metafunction - initial version
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hsutter committed Nov 8, 2024
1 parent 9901b01 commit 44a7255
Show file tree
Hide file tree
Showing 6 changed files with 726 additions and 558 deletions.
12 changes: 6 additions & 6 deletions regression-tests/test-results/pure2-last-use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class issue_857_4 {
public: std::move_only_function<int()> mf;
public: std::move_only_function<int([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)> mg;
public: issue_857_4(auto&& f_, auto&& g_, auto&& mf_, auto&& mg_)
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_)>>&>) ;
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)>>&>) ;

// h0: (move this) = _ = mf();
// h1: (move this) = _ = this.mf();
Expand Down Expand Up @@ -772,11 +772,11 @@ requires (std::is_convertible_v<CPP2_TYPEOF(i_), std::add_const_t<std::add_lvalu
return *this;}
issue_857_6::issue_857_6(){}
issue_857_4::issue_857_4(auto&& f_, auto&& g_, auto&& mf_, auto&& mg_)
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_)>>&>)
: f{ CPP2_FORWARD(f_) }
, g{ CPP2_FORWARD(g_) }
, mf{ CPP2_FORWARD(mf_) }
, mg{ CPP2_FORWARD(mg_) }{}
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)>>&>)
: f{ CPP2_FORWARD(f_) }
, g{ CPP2_FORWARD(g_) }
, mf{ CPP2_FORWARD(mf_) }
, mg{ CPP2_FORWARD(mg_) }{}
#line 267 "pure2-last-use.cpp2"
// OK: The implicit `this` is moved, not `i`.

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

cppfront compiler v0.8.0 Build 9B01:1726
cppfront compiler v0.8.0 Build 9B08:1148
SPDX-License-Identifier Apache-2.0 WITH LLVM-exception
Copyright (c) 2022-2024 Herb Sutter
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"9B01:1726"
"9B08:1148"
129 changes: 105 additions & 24 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,21 @@ struct compound_statement_node
return open_brace;
}

auto add_statement(
std::unique_ptr<statement_node>&& statement,
int before_pos
)
-> bool
{
// Adopt this statement into our list of statements
statements.insert(
statements.begin() + std::clamp( before_pos, 0, unchecked_narrow<int>(std::ssize(statements)) ),
std::move(statement)
);

return true;
}

auto visit(auto& v, int depth) -> void;
};

Expand Down Expand Up @@ -2341,7 +2356,9 @@ struct parameter_declaration_node

// API
//
auto to_string() const
auto to_string(
bool verbose = true
) const
-> std::string;

auto has_name() const
Expand Down Expand Up @@ -2439,15 +2456,25 @@ struct parameter_declaration_list_node

// API
//
auto to_string() const
auto to_string(
bool verbose = true
) const
-> std::string
{
assert(open_paren && close_paren);

auto ret = open_paren->to_string();

for (auto const& p: parameters) {
ret += p->to_string() + ", ";
ret += p->to_string(verbose) + ", ";
}

if (
!verbose
&& std::ssize(ret) > 3
)
{
ret.resize( std::ssize(ret) - 2 ); // omit the final ", "
}

ret += close_paren->as_string_view();
Expand Down Expand Up @@ -2660,6 +2687,15 @@ struct function_type_node
return {};
}

auto parameters_to_string(
bool verbose = true
) const
-> std::string
{
assert (parameters);
return parameters->to_string(verbose);
}

auto has_bool_return_type() const
-> bool
{
Expand Down Expand Up @@ -3034,7 +3070,12 @@ struct declaration_node

// API
//
auto to_string() const
auto to_string(
bool verbose = true
) const
-> std::string;

auto signature_to_string() const
-> std::string;

auto is_template_parameter() const
Expand Down Expand Up @@ -3635,6 +3676,12 @@ struct declaration_node
;
}

auto get_compound_initializer() const
-> compound_statement_node*
{
return initializer->get_if<compound_statement_node>();
}

auto is_function_with_this() const
-> bool
{
Expand Down Expand Up @@ -4087,7 +4134,9 @@ struct declaration_node
};


auto parameter_declaration_node::to_string() const
auto parameter_declaration_node::to_string(
bool verbose /*= true*/
) const
-> std::string
{
auto ret = std::string{};
Expand All @@ -4105,7 +4154,15 @@ auto parameter_declaration_node::to_string() const
;
}

ret += to_string_view(pass) + declaration->to_string();
if (
verbose
|| pass != passing_style::in
)
{
ret += to_string_view(pass);
ret += " ";
}
ret += declaration->to_string( verbose );

return ret;
}
Expand Down Expand Up @@ -4870,15 +4927,32 @@ auto pretty_print_visualize(type_node const& n, int indent)
-> std::string;
auto pretty_print_visualize(namespace_node const& n, int indent)
-> std::string;
auto pretty_print_visualize(declaration_node const& n, int indent, bool include_metafunctions_list = false)
auto pretty_print_visualize(declaration_node const& n, int indent, bool include_metafunctions_list = false, bool verbose = true)
-> std::string;


auto declaration_node::to_string() const
auto declaration_node::to_string(
bool verbose /*= true*/
) const
-> std::string
{
// These need to be unified someday... let's not duplicate this long function...
return pretty_print_visualize(*this, 0);
return pretty_print_visualize(*this, 0, false, verbose);
}


auto declaration_node::signature_to_string() const
-> std::string
{
auto ret = std::string{};
if (auto fname = name()) {
ret += *fname;
}
if (auto func = std::get_if<a_function>(&type)) {
assert ((*func)->parameters);
ret += (*func)->parameters->to_string(false);
}
return ret;
}


Expand Down Expand Up @@ -5593,7 +5667,12 @@ auto pretty_print_visualize(namespace_node const&)
}


auto pretty_print_visualize(declaration_node const& n, int indent, bool include_metafunctions_list /* = false */ )
auto pretty_print_visualize(
declaration_node const& n,
int indent,
bool include_metafunctions_list /* = false */,
bool verbose /* = true */
)
-> std::string
{
indent_spaces = 4;
Expand Down Expand Up @@ -5627,23 +5706,25 @@ auto pretty_print_visualize(declaration_node const& n, int indent, bool include_
}

auto initializer = std::string{};
if (n.initializer) {
auto adjusted_indent = indent;
if (!n.name()) {
++adjusted_indent;
}
initializer = " =";
if (n.is_function() && n.is_constexpr) {
initializer += "=";
if (verbose) {
if (n.initializer) {
auto adjusted_indent = indent;
if (!n.name()) {
++adjusted_indent;
}
initializer = " =";
if (n.is_function() && n.is_constexpr) {
initializer += "=";
}
initializer += " " + pretty_print_visualize(*n.initializer, adjusted_indent);
if (initializer.ends_with(";;")) {
initializer.pop_back();
}
}
initializer += " " + pretty_print_visualize(*n.initializer, adjusted_indent);
if (initializer.ends_with(";;")) {
initializer.pop_back();
else if (!n.is_parameter()) {
initializer = ";";
}
}
else if (!n.is_parameter()) {
initializer = ";";
}

// Then slot them in where appropriate

Expand Down
Loading

0 comments on commit 44a7255

Please sign in to comment.