Skip to content

Commit 2884b34

Browse files
committed
Add param/return list reflection + autodiff stub (so far just differentiated signature generation)
My short-term goal is to implement a narrow slice of functionality that's just enough to unblock basic autodiff, then once that's working to later go back and generalize those features. Example: This commit supports return lists (e.g., `-> (r: double)`), then in the future I'll add support for single anonymous returns (e.g., `-> double`). Example: This commit still supports metafunctions only on types and so focuses on param/return lists for type-scope functions, then in the future I'll add support for reflecting namespace-scope functions and generating additional functions in the same namespace scope. As a test case, this commit also adds baby-step scaffolding for an `autodiff` metafunction that can be used like this: test: @autodiff @print type = { // A simple first function would be: func: (a: double, b: double) -> (r: double) = { r = a + b; } // The generated autodiff forward transformation would be: // func_diff: (a: double, a_d: double, b: double, b_d: double) -> (r: double, r_d: double) = { // r_d = a * b_d + b * a_d; // this body is not yet // r = a * b; // generated in this commit // } } This commit does not yet reflect the function body, but does generate the following `@print` pretty-preinted output for the above to show what is generated for the function signature: test:/* @autodiff @print */ type = { func:( in a: double, in b: double, ) -> (out r: double, ) = { r = a + b; return; } func_diff:( in a: double, in a_d: double, in b: double, in b_d: double, ) -> ( out r: double = 1, out r_d: double = 1, ) = { return; } } Note: Thanks again to the folks who persisted in convincing me to allow extra optional trailing commas in lists throughout Cpp2. It's only a little thing to avoid writing the extra code to generate the commas only for non-last parameters, but avoiding that little extra work is surprisingly "nice" when generating code and makes the metafunction that much cleaner and simpler.
1 parent ec1f98a commit 2884b34

File tree

4 files changed

+718
-554
lines changed

4 files changed

+718
-554
lines changed

regression-tests/test-results/pure2-regex_20_lookbehind.cpp.output

Lines changed: 0 additions & 3 deletions
This file was deleted.

source/parse.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3979,14 +3979,33 @@ struct declaration_node
39793979
if (!is_function()) {
39803980
return {};
39813981
}
3982-
// else
3982+
39833983
auto ret = std::vector<parameter_declaration_node const*>{};
39843984
for (auto& param : std::get<a_function>(type)->parameters->parameters) {
39853985
ret.push_back( param.get() );
39863986
}
39873987
return ret;
39883988
}
39893989

3990+
auto get_function_returns()
3991+
-> std::vector<parameter_declaration_node const*>
3992+
{
3993+
if (!is_function()) {
3994+
return {};
3995+
}
3996+
3997+
auto returns = std::get_if<function_type_node::list>(&std::get<a_function>(type)->returns);
3998+
if (!returns) {
3999+
return {};
4000+
}
4001+
4002+
auto ret = std::vector<parameter_declaration_node const*>{};
4003+
for (auto& param : (*returns)->parameters) {
4004+
ret.push_back( param.get() );
4005+
}
4006+
return ret;
4007+
}
4008+
39904009
auto unnamed_return_type_to_string() const
39914010
-> std::string
39924011
{

0 commit comments

Comments
 (0)