Skip to content
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

feat(reflect): add function_declaration API to set initializer and inspect parameters #831

Merged
2 changes: 1 addition & 1 deletion include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class contract_group {
public:
using handler = void (*)(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM);

constexpr contract_group (handler h = {}) : reporter(h) { }
constexpr contract_group (handler h) : reporter{h} { }
constexpr auto set_handler(handler h);
constexpr auto get_handler() const -> handler { return reporter; }
constexpr auto expects (bool b, CPP2_MESSAGE_PARAM msg = "" CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT)
Expand Down
60 changes: 48 additions & 12 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,7 @@ struct declaration_node
}


auto add_type_member( std::unique_ptr<statement_node> statement )
auto add_type_member( std::unique_ptr<statement_node>&& statement )
-> bool
{
if (
Expand Down Expand Up @@ -3019,6 +3019,23 @@ struct declaration_node
}


auto add_function_initializer( std::unique_ptr<statement_node>&& statement )
-> bool
{
if (
!is_function()
|| initializer
)
{
return false;
}

// Adopt it as our initializer statement
initializer = std::move( statement );
return true;
}


auto get_decl_if_type_scope_object_name_before_a_base_type( std::string_view s ) const
-> declaration_node const*
{
Expand Down Expand Up @@ -3388,6 +3405,20 @@ struct declaration_node
return false;
}

auto get_function_parameters()
-> std::vector<parameter_declaration_node const*>
{
if (!is_function()) {
return {};
}
// else
auto ret = std::vector<parameter_declaration_node const*>{};
for (auto& param : std::get<a_function>(type)->parameters->parameters) {
ret.push_back( param.get() );
}
return ret;
}

auto unnamed_return_type_to_string() const
-> std::string
{
Expand Down Expand Up @@ -5281,17 +5312,22 @@ class parser
tokens = &tokens_;
generated_tokens = &generated_tokens_;

// Parse one declaration - we succeed if the parse succeeded,
// and there were no new errors, and all tokens were consumed
auto errors_size = std::ssize(errors);
pos = 0;
if (auto d = statement();
d
&& std::ssize(errors) == errors_size
&& done()
)
{
return d;
try {
// Parse one declaration - we succeed if the parse succeeded,
// and there were no new errors, and all tokens were consumed
auto errors_size = std::ssize(errors);
pos = 0;
if (auto d = statement();
d
&& std::ssize(errors) == errors_size
&& done()
)
{
return d;
}
}
catch(std::runtime_error& e) {
error(e.what(), true, {}, true);
}

return {};
Expand Down
Loading