Skip to content

Commit 580b83a

Browse files
JohelEGPhsutter
andauthored
feat(reflect): add function_declaration API to set initializer and inspect parameters (#831)
* feat(reflect): add `function_declaration::get_parameters` * feat(reflect): add `function_declaration::add_initializer` * fix(reflect): actually return the parent * feat(reflect): add `declaration::get_arguments` * feat(reflect): add `get_member_functions_needing_initializer` * Pre-merge review pass Johel, can you give this a try in your test code and make sure your test code still works as expected? I had to create a little test to exercise it, since the new functions aren't used in this PR. * Restore preconditions for `add_initializer` And: - Add proper early returns for failed null checks - Disable the `contract_group` default constructor, since we aren't checking the handler before trying to invoke it. * Second review: Add graceful termination for a metafunction precondition violation And restore `source` string to error output, and make it just come out routinely as soon as a parse error is detected * Report original source string on parse errors * regenerate `reflect.h` --------- Signed-off-by: Herb Sutter <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent 0c0204e commit 580b83a

File tree

4 files changed

+404
-192
lines changed

4 files changed

+404
-192
lines changed

include/cpp2util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class contract_group {
391391
public:
392392
using handler = void (*)(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM);
393393

394-
constexpr contract_group (handler h = {}) : reporter(h) { }
394+
constexpr contract_group (handler h) : reporter{h} { }
395395
constexpr auto set_handler(handler h);
396396
constexpr auto get_handler() const -> handler { return reporter; }
397397
constexpr auto expects (bool b, CPP2_MESSAGE_PARAM msg = "" CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT)

source/parse.h

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,7 +2987,7 @@ struct declaration_node
29872987
}
29882988

29892989

2990-
auto add_type_member( std::unique_ptr<statement_node> statement )
2990+
auto add_type_member( std::unique_ptr<statement_node>&& statement )
29912991
-> bool
29922992
{
29932993
if (
@@ -3019,6 +3019,23 @@ struct declaration_node
30193019
}
30203020

30213021

3022+
auto add_function_initializer( std::unique_ptr<statement_node>&& statement )
3023+
-> bool
3024+
{
3025+
if (
3026+
!is_function()
3027+
|| initializer
3028+
)
3029+
{
3030+
return false;
3031+
}
3032+
3033+
// Adopt it as our initializer statement
3034+
initializer = std::move( statement );
3035+
return true;
3036+
}
3037+
3038+
30223039
auto get_decl_if_type_scope_object_name_before_a_base_type( std::string_view s ) const
30233040
-> declaration_node const*
30243041
{
@@ -3388,6 +3405,20 @@ struct declaration_node
33883405
return false;
33893406
}
33903407

3408+
auto get_function_parameters()
3409+
-> std::vector<parameter_declaration_node const*>
3410+
{
3411+
if (!is_function()) {
3412+
return {};
3413+
}
3414+
// else
3415+
auto ret = std::vector<parameter_declaration_node const*>{};
3416+
for (auto& param : std::get<a_function>(type)->parameters->parameters) {
3417+
ret.push_back( param.get() );
3418+
}
3419+
return ret;
3420+
}
3421+
33913422
auto unnamed_return_type_to_string() const
33923423
-> std::string
33933424
{
@@ -5281,17 +5312,22 @@ class parser
52815312
tokens = &tokens_;
52825313
generated_tokens = &generated_tokens_;
52835314

5284-
// Parse one declaration - we succeed if the parse succeeded,
5285-
// and there were no new errors, and all tokens were consumed
5286-
auto errors_size = std::ssize(errors);
5287-
pos = 0;
5288-
if (auto d = statement();
5289-
d
5290-
&& std::ssize(errors) == errors_size
5291-
&& done()
5292-
)
5293-
{
5294-
return d;
5315+
try {
5316+
// Parse one declaration - we succeed if the parse succeeded,
5317+
// and there were no new errors, and all tokens were consumed
5318+
auto errors_size = std::ssize(errors);
5319+
pos = 0;
5320+
if (auto d = statement();
5321+
d
5322+
&& std::ssize(errors) == errors_size
5323+
&& done()
5324+
)
5325+
{
5326+
return d;
5327+
}
5328+
}
5329+
catch(std::runtime_error& e) {
5330+
error(e.what(), true, {}, true);
52955331
}
52965332

52975333
return {};

0 commit comments

Comments
 (0)