-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting over on function types - initial support for function types …
…in <> lists (#1183) * Starting over on function types - initial support for function types in <> lists Should generally support std::function<> style uses Aside: I'm happy to find that C++ allows parameter names in function types, thank you WG21! I hadn't seen those used in examples so I had been expecting that cppfront would have to suppress those, but std::function< int ( std::string& param_name ) > is legal code. Sweet. This commit does not yet support pointer-to-function local variables... * Add support for pointer-to-function variables * Add docs examples for function typeids with std::function & *pfn * Add support for pointer to function parameter types * Support function type ids in type aliases
- Loading branch information
Showing
19 changed files
with
660 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
|
||
// --- Scaffolding | ||
|
||
f: () = std::cout << "hello world!\n"; | ||
|
||
g_in : ( s: std::string) = std::cout << "Come in, (s)$\n"; | ||
g_inout: (inout s: std::string) = std::cout << "Come in awhile, but take some biscuits on your way out, (s)$!\n"; | ||
g_out : (out s: std::string) = s = "A Powerful Mage"; | ||
g_move : (move s: std::string) = std::cout << "I hear you've moving, (s)$?\n"; | ||
|
||
h_forward: (inout s: std::string) -> forward std::string = { std::cout << "Inout (s)$ ... "; return s; } | ||
h_out : ( s: std::string) -> std::string = { std::cout << "In (s)$ ... "; return "yohoho"; } | ||
|
||
f1: (a: std::function< (x:int) -> int >) -> int = a(1); | ||
f2: (a: * (x:int) -> int ) -> int = a(2); | ||
g : (x:int) -> int = x+42; | ||
|
||
|
||
// --- Tests for type aliases | ||
|
||
A_h_forward: type == (inout s: std::string) -> forward std::string; | ||
|
||
|
||
main: () = | ||
{ | ||
// --- Test basic/degenerate cases | ||
|
||
// Test std::function< void() > | ||
ff: std::function< () -> void > = f&; | ||
ff(); | ||
|
||
// Ordinary pointer to function, deduced (always worked) | ||
pf: * () -> void = f&; | ||
pf(); | ||
|
||
|
||
// --- Tests for parameters | ||
// Note: Not forward parameters which imply a template... | ||
// function type-ids are for single function signatures | ||
|
||
fg_in : std::function< ( s: std::string) -> void > = g_in&; | ||
fg_inout: std::function< (inout s: std::string) -> void > = g_inout&; | ||
fg_out : std::function< (out s: std::string) -> void > = g_out&; | ||
fg_move : std::function< (move s: std::string) -> void > = g_move&; | ||
pg_in : * ( s: std::string) -> void = g_in&; | ||
pg_inout: * (inout s: std::string) -> void = g_inout&; | ||
pg_out : * (out s: std::string) -> void = g_out&; | ||
pg_move : * (move s: std::string) -> void = g_move&; | ||
|
||
frodo: std::string = "Frodo"; | ||
sam : std::string = "Sam"; | ||
|
||
// Test in param | ||
fg_in(frodo); | ||
pg_in(sam); | ||
|
||
// Test inout | ||
fg_inout(frodo); | ||
pg_inout(sam); | ||
|
||
// Test out | ||
gandalf : std::string; | ||
galadriel: std::string; | ||
fg_out(out gandalf); | ||
std::cout << "fg_out initialized gandalf to: (gandalf)$\n"; | ||
pg_out(out galadriel); | ||
std::cout << "pg_out initialized galadriel to: (galadriel)$\n"; | ||
gandalf = "Gandalf"; | ||
galadriel = "Galadriel"; | ||
|
||
// Test move | ||
fg_move(frodo); // last use, so (move frodo) is not required | ||
pg_move(sam); // last use, so (move sam) is not required | ||
|
||
|
||
// --- Tests for single anonymous returns | ||
// Note: Not multiple named return values... function-type-ids | ||
// are for Cpp1-style (single anonymous, possibly void) returns | ||
|
||
fh_forward: std::function< (inout s: std::string) -> forward std::string > = h_forward&; | ||
fh_out : std::function< ( s: std::string) -> std::string > = h_out&; | ||
ph_forward: * (inout s: std::string) -> forward std::string = h_forward&; | ||
ph_out : * ( s: std::string) -> std::string = h_out&; | ||
|
||
ph_forward2: * A_h_forward = h_forward&; | ||
|
||
// Test forward return | ||
std::cout << "fh_forward returned: (fh_forward(gandalf))$\n"; | ||
std::cout << "ph_forward returned: (ph_forward(galadriel))$\n"; | ||
std::cout << "ph_forward2 returned: (ph_forward2(galadriel))$\n"; | ||
|
||
// Test out return | ||
std::cout << "fh_out returned: (fh_out(gandalf))$\n"; | ||
std::cout << "ph_out returned: (ph_out(galadriel))$\n"; | ||
|
||
|
||
// --- Tests for function parameters | ||
std::cout << "(f1(g&))$\n"; | ||
std::cout << "(f2(g&))$\n"; | ||
|
||
|
||
} |
17 changes: 17 additions & 0 deletions
17
regression-tests/test-results/clang-12-c++20/pure2-function-typeids.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
hello world! | ||
hello world! | ||
Come in, Frodo | ||
Come in, Sam | ||
Come in awhile, but take some biscuits on your way out, Frodo! | ||
Come in awhile, but take some biscuits on your way out, Sam! | ||
fg_out initialized gandalf to: A Powerful Mage | ||
pg_out initialized galadriel to: A Powerful Mage | ||
I hear you've moving, Frodo? | ||
I hear you've moving, Sam? | ||
Inout Gandalf ... fh_forward returned: Gandalf | ||
Inout Galadriel ... ph_forward returned: Galadriel | ||
Inout Galadriel ... ph_forward2 returned: Galadriel | ||
In Gandalf ... fh_out returned: yohoho | ||
In Galadriel ... ph_out returned: yohoho | ||
43 | ||
44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
regression-tests/test-results/gcc-10-c++20/pure2-function-typeids.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
hello world! | ||
hello world! | ||
Come in, Frodo | ||
Come in, Sam | ||
Come in awhile, but take some biscuits on your way out, Frodo! | ||
Come in awhile, but take some biscuits on your way out, Sam! | ||
fg_out initialized gandalf to: A Powerful Mage | ||
pg_out initialized galadriel to: A Powerful Mage | ||
I hear you've moving, Frodo? | ||
I hear you've moving, Sam? | ||
Inout Gandalf ... fh_forward returned: Gandalf | ||
Inout Galadriel ... ph_forward returned: Galadriel | ||
Inout Galadriel ... ph_forward2 returned: Galadriel | ||
In Gandalf ... fh_out returned: yohoho | ||
In Galadriel ... ph_out returned: yohoho | ||
43 | ||
44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
regression-tests/test-results/gcc-14-c++2b/mixed-bugfix-for-ufcs-non-local.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
In file included from mixed-bugfix-for-ufcs-non-local.cpp:6: | ||
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type | ||
2100 | class finally_success | ||
2100 | | ||
| ^ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | finally(finally&& that) noexcept | ||
2137 | ~finally() noexcept { f(); } | ||
| ^ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:13:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:13:36: error: template argument 1 is invalid | ||
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type | ||
2100 | class finally_success | ||
2100 | | ||
| ^ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | finally(finally&& that) noexcept | ||
2137 | ~finally() noexcept { f(); } | ||
| ^ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:21:36: error: template argument 1 is invalid | ||
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type | ||
2100 | class finally_success | ||
2100 | | ||
| ^ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | finally(finally&& that) noexcept | ||
2137 | ~finally() noexcept { f(); } | ||
| ^ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:31:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:31:36: error: template argument 1 is invalid | ||
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type | ||
2100 | class finally_success | ||
2100 | | ||
| ^ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | finally(finally&& that) noexcept | ||
2137 | ~finally() noexcept { f(); } | ||
| ^ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:33:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:33:36: error: template argument 1 is invalid | ||
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type | ||
2100 | class finally_success | ||
2100 | | ||
| ^ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | finally(finally&& that) noexcept | ||
2137 | ~finally() noexcept { f(); } | ||
| ^ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
mixed-bugfix-for-ufcs-non-local.cpp2:21:36: error: template argument 1 is invalid |
17 changes: 17 additions & 0 deletions
17
regression-tests/test-results/gcc-14-c++2b/pure2-function-typeids.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
hello world! | ||
hello world! | ||
Come in, Frodo | ||
Come in, Sam | ||
Come in awhile, but take some biscuits on your way out, Frodo! | ||
Come in awhile, but take some biscuits on your way out, Sam! | ||
fg_out initialized gandalf to: A Powerful Mage | ||
pg_out initialized galadriel to: A Powerful Mage | ||
I hear you've moving, Frodo? | ||
I hear you've moving, Sam? | ||
Inout Gandalf ... fh_forward returned: Gandalf | ||
Inout Galadriel ... ph_forward returned: Galadriel | ||
Inout Galadriel ... ph_forward2 returned: Galadriel | ||
In Gandalf ... fh_out returned: yohoho | ||
In Galadriel ... ph_out returned: yohoho | ||
43 | ||
44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
regression-tests/test-results/msvc-2022-c++latest/pure2-function-typeids.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
hello world! | ||
hello world! | ||
Come in, Frodo | ||
Come in, Sam | ||
Come in awhile, but take some biscuits on your way out, Frodo! | ||
Come in awhile, but take some biscuits on your way out, Sam! | ||
fg_out initialized gandalf to: A Powerful Mage | ||
pg_out initialized galadriel to: A Powerful Mage | ||
I hear you've moving, Frodo? | ||
I hear you've moving, Sam? | ||
Inout Gandalf ... fh_forward returned: Gandalf | ||
Inout Galadriel ... ph_forward returned: Galadriel | ||
Inout Galadriel ... ph_forward2 returned: Galadriel | ||
In Gandalf ... fh_out returned: yohoho | ||
In Galadriel ... ph_out returned: yohoho | ||
43 | ||
44 |
1 change: 1 addition & 0 deletions
1
regression-tests/test-results/msvc-2022-c++latest/pure2-function-typeids.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pure2-function-typeids.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's some lowering errors (https://cpp2.godbolt.org/z/b9T19oj17):
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I'll take a look...
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support is also missing for unnamed objects (https://cpp2.godbolt.org/z/94z75M196):
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about Cpp1
noexcept
support?There are API and performance use cases.
For example (https://cpp2.godbolt.org/z/G6ahnz5d9):
For #526, I just allowed
!
before the defaultthrows
specifier(https://github.com/hsutter/cppfront/pull/526/files#diff-b5598a268a2d4647f67dac83fa5706e1326597671587bbc59115cf8a68f61c34R72-R73):
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within a template argument list, a parameter can't be anonymous (https://cpp2.godbolt.org/z/41GP7ezzr):
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also seems like you can't declare a function with a function type return type (https://cpp2.godbolt.org/z/hdfesPnbh):
https://cpp2.godbolt.org/z/5d5fefMEh:
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to adapt https://github.com/hsutter/cppfront/pull/526/files#diff-b5598a268a2d4647f67dac83fa5706e1326597671587bbc59115cf8a68f61c34R100.
The pointer type is being lost (https://cpp2.godbolt.org/z/dK8TbddnW):
By the way, I think a function type should also be a type-id, so that this works:
212eb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same as 212eb15#commitcomment-147043762 is happening in the
main
branch (as evidenced by the red CI):