| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2e2bece commit fe990ca
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + #include <vector> | ||
| 2 | + | ||
| 3 | + template<bool> | ||
| 4 | + auto f(auto, auto) -> void { } | ||
| 5 | + | ||
| 6 | + main: () -> int = { | ||
| 7 | + v : std::vector<int> = ( 1, 2, 3 ); | ||
| 8 | + std::cout << (1+2)*(3+v[0]); | ||
| 9 | + f<(1>2)>(3,4); | ||
| 10 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + // ----- Cpp2 support ----- | ||
| 2 | + #include "cpp2util.h" | ||
| 3 | + | ||
| 4 | + #line 1 "mixed-test-parens.cpp2" | ||
| 5 | + #include <vector> | ||
| 6 | + | ||
| 7 | + template<bool> | ||
| 8 | + auto f(auto, auto) -> void { } | ||
| 9 | + | ||
| 10 | + [[nodiscard]] auto main() -> int; | ||
| 11 | + | ||
| 12 | + //=== Cpp2 definitions ========================================================== | ||
| 13 | + | ||
| 14 | + #line 5 "mixed-test-parens.cpp2" | ||
| 15 | + | ||
| 16 | + [[nodiscard]] auto main() -> int{ | ||
| 17 | + std::vector<int> v { 1, 2, 3 }; | ||
| 18 | + std::cout << (1 + 2) * (3 + v[0]); | ||
| 19 | + f<(1 > 2)>(3, 4); | ||
| 20 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + mixed-test-parens.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks) | ||
| 2 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -631,6 +631,15 @@ class cppfront | |||
| 631 | 631 | // special value - hack for now to note single-anon-return type kind in this function_returns working list | |
| 632 | 632 | std::vector<std::string> function_requires_conditions; | |
| 633 | 633 | ||
| 634 | + std::vector<bool> need_expression_list_parens = { true }; | ||
| 635 | + auto push_need_expression_list_parens( bool b ) -> void { need_expression_list_parens.push_back(b); } | ||
| 636 | + auto pop_need_expression_list_parens() -> void { assert(std::ssize(need_expression_list_parens) > 1); | ||
| 637 | + need_expression_list_parens.pop_back(); } | ||
| 638 | + auto should_add_expression_list_parens() -> bool { assert(!need_expression_list_parens.empty()); | ||
| 639 | + return need_expression_list_parens.back(); } | ||
| 640 | + auto consumed_expression_list_parens() -> void { if( std::ssize(need_expression_list_parens) > 1 ) | ||
| 641 | + need_expression_list_parens.back() = false; } | ||
| 642 | + | ||
| 634 | 643 | public: | |
| 635 | 644 | //----------------------------------------------------------------------- | |
| 636 | 645 | // Constructor | |
@@ -1677,7 +1686,9 @@ class cppfront | |||
| 1677 | 1686 | // Then tack on any additional arguments | |
| 1678 | 1687 | if (!n.ops[1].expr_list->expressions.empty()) { | |
| 1679 | 1688 | printer.print_cpp2(", ", n.position()); | |
| 1689 | + push_need_expression_list_parens(false); | ||
| 1680 | 1690 | emit(*n.ops[1].expr_list); | |
| 1691 | + pop_need_expression_list_parens(); | ||
| 1681 | 1692 | } | |
| 1682 | 1693 | printer.print_cpp2(")", n.position()); | |
| 1683 | 1694 | ||
@@ -1773,7 +1784,9 @@ class cppfront | |||
| 1773 | 1784 | if (i->expr_list) { | |
| 1774 | 1785 | auto text = std::vector<text_with_pos>{}; | |
| 1775 | 1786 | printer.emit_to_text_chunks(&text); | |
| 1787 | + push_need_expression_list_parens(false); | ||
| 1776 | 1788 | emit(*i->expr_list); | |
| 1789 | + pop_need_expression_list_parens(); | ||
| 1777 | 1790 | printer.emit_to_text_chunks(); | |
| 1778 | 1791 | for (auto&& e: text) { | |
| 1779 | 1792 | suffix.push_back(e); | |
@@ -1971,6 +1984,10 @@ class cppfront | |||
| 1971 | 1984 | // | |
| 1972 | 1985 | auto emit(expression_list_node const& n) -> void | |
| 1973 | 1986 | { | |
| 1987 | + if (should_add_expression_list_parens() && !n.expressions.empty()) { | ||
| 1988 | + printer.print_cpp2("(", n.position()); | ||
| 1989 | + } | ||
| 1990 | + | ||
| 1974 | 1991 | auto first = true; | |
| 1975 | 1992 | for (auto const& x : n.expressions) { | |
| 1976 | 1993 | if (!first) { | |
@@ -2000,6 +2017,12 @@ class cppfront | |||
| 2000 | 2017 | printer.print_cpp2(")", n.position()); | |
| 2001 | 2018 | } | |
| 2002 | 2019 | } | |
| 2020 | + | ||
| 2021 | + if (should_add_expression_list_parens() && !n.expressions.empty()) { | ||
| 2022 | + printer.print_cpp2(")", n.position()); | ||
| 2023 | + } | ||
| 2024 | + // We want to consume only one of these | ||
| 2025 | + consumed_expression_list_parens(); | ||
| 2003 | 2026 | } | |
| 2004 | 2027 | ||
| 2005 | 2028 | ||
@@ -2519,16 +2542,17 @@ class cppfront | |||
| 2519 | 2542 | printer.print_cpp2( " ", n.position()); | |
| 2520 | 2543 | assert(n.identifier); | |
| 2521 | 2544 | emit(*n.identifier); | |
| 2522 | - //printer.print_cpp2( *n.identifier->identifier, n.position() ); | ||
| 2523 | 2545 | ||
| 2524 | 2546 | // If there's an initializer, emit it | |
| 2525 | 2547 | if (n.initializer) | |
| 2526 | 2548 | { | |
| 2527 | 2549 | printer.add_pad_in_this_line(-100); | |
| 2528 | 2550 | printer.print_cpp2( " { ", n.position() ); | |
| 2529 | 2551 | ||
| 2552 | + push_need_expression_list_parens(false); | ||
| 2530 | 2553 | assert( n.initializer ); | |
| 2531 | 2554 | emit( *n.initializer, false ); | |
| 2555 | + pop_need_expression_list_parens(); | ||
| 2532 | 2556 | ||
| 2533 | 2557 | printer.print_cpp2( " }", n.position() ); | |
| 2534 | 2558 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,11 +255,11 @@ auto to_string_view(passing_style pass) -> std::string_view { | |||
| 255 | 255 | ||
| 256 | 256 | struct expression_list_node | |
| 257 | 257 | { | |
| 258 | - source_position open_paren = {}; // TODO: these should probably be in primary_expression_node | ||
| 258 | + source_position open_paren = {}; | ||
| 259 | 259 | source_position close_paren = {}; | |
| 260 | 260 | ||
| 261 | 261 | struct term { | |
| 262 | - passing_style pass = {}; // for `out` | ||
| 262 | + passing_style pass = {}; | ||
| 263 | 263 | std::unique_ptr<expression_node> expr; | |
| 264 | 264 | }; | |
| 265 | 265 | std::vector< term > expressions; | |
@@ -269,10 +269,6 @@ struct expression_list_node | |||
| 269 | 269 | // Make sure this got set | |
| 270 | 270 | assert (open_paren != source_position()); | |
| 271 | 271 | return open_paren; | |
| 272 | - | ||
| 273 | - // TODO this could be removed now that we're tracking position via open_paren | ||
| 274 | - //assert (std::ssize(expressions) > 0 && expressions.front().expr); | ||
| 275 | - //return expressions.front().expr->position(); | ||
| 276 | 272 | } | |
| 277 | 273 | ||
| 278 | 274 | auto visit(auto& v, int depth) -> void | |
@@ -1765,8 +1761,8 @@ class parser | |||
| 1765 | 1761 | //G template-argument-list , template-argument | |
| 1766 | 1762 | //G | |
| 1767 | 1763 | //G template-argument: | |
| 1768 | - //G expression | ||
| 1769 | 1764 | //G id-expression | |
| 1765 | + //G expression | ||
| 1770 | 1766 | //G | |
| 1771 | 1767 | auto unqualified_id() -> std::unique_ptr<unqualified_id_node> | |
| 1772 | 1768 | { | |
@@ -2481,7 +2477,6 @@ class parser | |||
| 2481 | 2477 | next(); | |
| 2482 | 2478 | auto s = std::unique_ptr<statement_node>(); | |
| 2483 | 2479 | ||
| 2484 | - | ||
| 2485 | 2480 | while (curr().type() != lexeme::RightBrace) { | |
| 2486 | 2481 | auto s = statement(true); | |
| 2487 | 2482 | if (!s) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments