Skip to content

Commit

Permalink
refactor(parse): put implicit else statements in the actual else node
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Dec 14, 2023
1 parent c0a8338 commit e9cc033
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 65 deletions.
8 changes: 8 additions & 0 deletions regression-tests/pure2-last-use.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ issue_350: () = {
x++;
}

issue_440: () = {
i: int;
if true {
i = 1;
}
i = 2;
}

issue_683: (args) = {
for args do (n) {
_ = n;
Expand Down
55 changes: 33 additions & 22 deletions regression-tests/test-results/pure2-last-use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

#line 1 "pure2-last-use.cpp2"

#line 48 "pure2-last-use.cpp2"
#line 56 "pure2-last-use.cpp2"
class issue_857;


#line 72 "pure2-last-use.cpp2"
#line 80 "pure2-last-use.cpp2"
class issue_857_2;


#line 77 "pure2-last-use.cpp2"
#line 85 "pure2-last-use.cpp2"
class issue_857_3;


#line 123 "pure2-last-use.cpp2"
#line 131 "pure2-last-use.cpp2"
class my_string;


Expand All @@ -33,15 +33,18 @@ class my_string;
auto issue_350() -> void;

#line 18 "pure2-last-use.cpp2"
auto issue_440() -> void;

#line 26 "pure2-last-use.cpp2"
auto issue_683(auto const& args) -> void;

#line 27 "pure2-last-use.cpp2"
#line 35 "pure2-last-use.cpp2"
auto issue_825() -> void;

#line 33 "pure2-last-use.cpp2"
#line 41 "pure2-last-use.cpp2"
auto issue_832() -> void;

#line 38 "pure2-last-use.cpp2"
#line 46 "pure2-last-use.cpp2"
[[nodiscard]] auto make_copy(auto x) -> auto;

auto issue_847_0(std::vector<std::unique_ptr<int>> v) -> void;
Expand All @@ -56,7 +59,7 @@ class issue_857 {
private: std::unique_ptr<int> a;
private: std::unique_ptr<int> b;
public: issue_857(issue_857&& that) noexcept;
#line 51 "pure2-last-use.cpp2"
#line 59 "pure2-last-use.cpp2"
public: auto operator=(issue_857&& that) noexcept -> issue_857& ;
public: ~issue_857() noexcept;
public: auto f() && -> void;
Expand Down Expand Up @@ -88,22 +91,22 @@ class issue_857_3 {
public: auto f() && -> void;
};

#line 83 "pure2-last-use.cpp2"
#line 91 "pure2-last-use.cpp2"
auto issue_884_3() -> void;

#line 93 "pure2-last-use.cpp2"
#line 101 "pure2-last-use.cpp2"
auto issue_884() -> void;

#line 102 "pure2-last-use.cpp2"
#line 110 "pure2-last-use.cpp2"
auto issue_884_2() -> void;

#line 111 "pure2-last-use.cpp2"
#line 119 "pure2-last-use.cpp2"
auto issue_888(std::string r, int size) -> void;

#line 117 "pure2-last-use.cpp2"
#line 125 "pure2-last-use.cpp2"
auto draw() -> void;

#line 123 "pure2-last-use.cpp2"
#line 131 "pure2-last-use.cpp2"
class my_string {
public: std::string string;
public: std::size_t size {CPP2_UFCS(size)(string)};
Expand Down Expand Up @@ -132,6 +135,14 @@ auto issue_350() -> void{
++x;
}

auto issue_440() -> void{
cpp2::deferred_init<int> i;
if (true) {
i.construct(1);
}
i.construct(2);
}

auto issue_683(auto const& args) -> void{
for ( auto const& n : args ) {
static_cast<void>(n);
Expand Down Expand Up @@ -162,16 +173,16 @@ auto issue_847_3(int x) -> void{for ( [[maybe_unused]] auto const& unnamed_param
auto f_inout([[maybe_unused]] std::unique_ptr<int>& unnamed_param_1) -> void{}
auto f_copy([[maybe_unused]] auto ...unnamed_param_1) -> void{}

#line 51 "pure2-last-use.cpp2"
#line 59 "pure2-last-use.cpp2"
issue_857::issue_857(issue_857&& that) noexcept
: a{ std::move(that).a }
, b{ std::move(that).b }{}
#line 51 "pure2-last-use.cpp2"
#line 59 "pure2-last-use.cpp2"
auto issue_857::operator=(issue_857&& that) noexcept -> issue_857& {
a = std::move(that).a;
b = std::move(that).b;
return *this; }
#line 52 "pure2-last-use.cpp2"
#line 60 "pure2-last-use.cpp2"
issue_857::~issue_857() noexcept { f_copy(std::move(*this).a, std::move((*this)).b); }
auto issue_857::f() && -> void { f_copy(std::move((*this))); }
auto issue_857::f(issue_857&& that) && -> void { f_copy(std::move((*this)), std::move(that)); }
Expand All @@ -191,13 +202,13 @@ auto f_copy([[maybe_unused]] auto ...unnamed_param_1) -> void{}
auto issue_857::o3() && -> void { std::move(*this).n(0); }
auto issue_857::o4() && -> void { std::move(*this).n(std::move((*this))); }

#line 76 "pure2-last-use.cpp2"
#line 84 "pure2-last-use.cpp2"
int gi {0};

#line 79 "pure2-last-use.cpp2"
#line 87 "pure2-last-use.cpp2"
auto issue_857_3::f() && -> void { static_cast<void>(f_inout(std::move(*this).i)); }

#line 83 "pure2-last-use.cpp2"
#line 91 "pure2-last-use.cpp2"
auto issue_884_3() -> void{
auto x {cpp2_new<int>(0)};
if (true) {}
Expand Down Expand Up @@ -238,10 +249,10 @@ auto draw() -> void{
static_cast<void>(CPP2_UFCS_MOVE(vertex)((std::move(pos))));
}

#line 128 "pure2-last-use.cpp2"
#line 136 "pure2-last-use.cpp2"
auto main(int const argc_, char** argv_) -> int{
auto const args = cpp2::make_args(argc_, argv_);
#line 129 "pure2-last-use.cpp2"
#line 137 "pure2-last-use.cpp2"
issue_683(args);
issue_847_2(std::vector<std::unique_ptr<int>>());
}
Expand Down
12 changes: 6 additions & 6 deletions regression-tests/test-results/pure2-print.cpp2.output
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ outer: type =
{
ret = -p*;
}
ret += strlen(s) - 10 + s.strlen() * (16 / (3 & 2)) % 3;
m: map<const int, string> = ();
m[0] = "har" as string;
ret -= h("x", m).length();
_ = m;
return ret;
ret += strlen(s) - 10 + s.strlen() * (16 / (3 & 2)) % 3;
m: map<const int, string> = ();
m[0] = "har" as string;
ret -= h("x", m).length();
_ = m;
return ret;
}

private h:(
Expand Down
28 changes: 23 additions & 5 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4186,7 +4186,7 @@ auto pretty_print_visualize(is_as_expression_node const& n, int indent)
-> std::string;
auto pretty_print_visualize(id_expression_node const& n, int indent)
-> std::string;
auto pretty_print_visualize(compound_statement_node const& n, int indent)
auto pretty_print_visualize(compound_statement_node const& n, int indent, bool explicit_scope = true)
-> std::string;
auto pretty_print_visualize(selection_statement_node const& n, int indent)
-> std::string;
Expand Down Expand Up @@ -4506,17 +4506,23 @@ auto pretty_print_visualize(id_expression_node const& n, int indent)
}


auto pretty_print_visualize(compound_statement_node const& n, int indent)
auto pretty_print_visualize(compound_statement_node const& n, int indent, bool explicit_scope)
-> std::string
{
auto ret = std::string{"\n"} + pre(indent) + "{";
auto ret = std::string{};

if (explicit_scope) {
ret += std::string{"\n"} + pre(indent) + "{";
}

for (auto& stmt : n.statements) {
assert (stmt);
ret += pretty_print_visualize(*stmt, indent+1);
}

ret += std::string{"\n"} + pre(indent) + "}";
if (explicit_scope) {
ret += std::string{"\n"} + pre(indent) + "}";
}

return ret;
}
Expand All @@ -4541,6 +4547,8 @@ auto pretty_print_visualize(selection_statement_node const& n, int indent)
if (n.has_source_false_branch) {
ret += std::string{"\n"} + pre(indent) + "else "
+ pretty_print_visualize(*n.false_branch, indent);
} else if (!n.false_branch->statements.empty()) {
ret += pretty_print_visualize(*n.false_branch, indent, false);
}

return ret;
Expand Down Expand Up @@ -7321,6 +7329,7 @@ class parser
next();
}

auto statements = &n->statements;
while (
curr().type() != lexeme::RightBrace
&& (
Expand All @@ -7341,7 +7350,16 @@ class parser
pos = start_pos; // backtrack
return {};
}
n->statements.push_back( std::move(s) );
statements->push_back( std::move(s) );

// Add statements to the most recent implicit else branch
if (auto selection = statements->back()->get_if<selection_statement_node>();
selection
&& !selection->has_source_false_branch
)
{
statements = &selection->false_branch->statements;
}
}

if (is_braced) {
Expand Down
30 changes: 2 additions & 28 deletions source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -1882,18 +1882,11 @@ class sema
++scope_depth;
}

auto end(selection_statement_node const& s, int) -> void
auto end(selection_statement_node const&, int) -> void
{
symbols.emplace_back( scope_depth, selection_sym{ false, active_selections.back() } );
active_selections.pop_back();
// Extend the scope of an implicit else branch
// to the closing brace that contains it.
if (s.false_branch->position() == source_position(0, 0)) {
++scope_depth;
}
else {
--scope_depth;
}
--scope_depth;
}

auto kind_of(compound_statement_node const& n)
Expand Down Expand Up @@ -1934,25 +1927,6 @@ class sema
compound_sym{ false, &n, kind_of(n) }
);
--scope_depth;

auto pop_implicit_else_branch = [&]() -> bool {
if (auto s = std::find_if(
symbols.rbegin(),
symbols.rend(),
[=](symbol s) {
return s.depth <= scope_depth;
});
s != symbols.rend()
&& std::get_if<symbol::selection>(&s->sym)
&& s->depth == scope_depth
)
{
--scope_depth;
return true;
}
return false;
};
while (pop_implicit_else_branch()) { }
}

auto start(assignment_expression_node const& n, int)
Expand Down
15 changes: 11 additions & 4 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1962,15 +1962,18 @@ class cppfront
//-----------------------------------------------------------------------
//
auto emit(
compound_statement_node const& n,
compound_statement_node const& n,
bool explicit_scope = true,
function_prolog const& function_prolog = {},
std::vector<std::string> const& function_epilog = {}
)
-> void
{ STACKINSTR
emit_prolog_mem_inits(function_prolog, n.body_indent+1);

printer.print_cpp2( "{", n.open_brace );
if (explicit_scope) {
printer.print_cpp2( "{", n.open_brace );
}

emit_prolog_statements(function_prolog, n.body_indent+1);

Expand All @@ -1981,7 +1984,9 @@ class cppfront

emit_epilog_statements( function_epilog, n.body_indent+1);

printer.print_cpp2( "}", n.close_brace );
if (explicit_scope) {
printer.print_cpp2( "}", n.close_brace );
}
}


Expand Down Expand Up @@ -2167,6 +2172,8 @@ class cppfront
if (n.has_source_false_branch) {
printer.print_cpp2("else ", n.else_pos);
emit(*n.false_branch);
} else if (!n.false_branch->statements.empty()) {
emit(*n.false_branch, false);
}
}

Expand Down Expand Up @@ -4151,7 +4158,7 @@ class cppfront

printer.disable_indent_heuristic_for_next_text();

try_emit<statement_node::compound >(n.statement, function_prolog, function_epilog);
try_emit<statement_node::compound >(n.statement, true, function_prolog, function_epilog);

// NOTE: Reset preemption here because
// - for compound statements written as "= { ... }", we want to keep the
Expand Down

0 comments on commit e9cc033

Please sign in to comment.