From 5f8b2313229d1f081c091eafc7f587aa18fdda61 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 1 Sep 2021 15:19:58 -0700 Subject: [PATCH] Switch Clause to Ptr (#800) This is consistent with other lists, doesn't seem to benefit from being a Ptr. --- executable_semantics/ast/statement.cpp | 2 +- executable_semantics/ast/statement.h | 8 +++---- .../interpreter/interpreter.cpp | 6 +++--- .../interpreter/type_checker.cpp | 20 +++++++++--------- executable_semantics/syntax/parser.ypp | 21 ++++++------------- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/executable_semantics/ast/statement.cpp b/executable_semantics/ast/statement.cpp index 5eb2dd513fc0..3ecc280d3b4e 100644 --- a/executable_semantics/ast/statement.cpp +++ b/executable_semantics/ast/statement.cpp @@ -23,7 +23,7 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const { out << "match (" << *match.Exp() << ") {"; if (depth < 0 || depth > 1) { out << "\n"; - for (auto& clause : *match.Clauses()) { + for (auto& clause : match.Clauses()) { out << "case " << *clause.first << " =>\n"; clause.second->PrintDepth(depth - 1, out); out << "\n"; diff --git a/executable_semantics/ast/statement.h b/executable_semantics/ast/statement.h index 255ab8db0365..ac15a69fdfdd 100644 --- a/executable_semantics/ast/statement.h +++ b/executable_semantics/ast/statement.h @@ -226,8 +226,8 @@ class Continue : public Statement { class Match : public Statement { public: Match(SourceLocation loc, Ptr exp, - std::list, Ptr>>* clauses) - : Statement(Kind::Match, loc), exp(exp), clauses(clauses) {} + std::list, Ptr>> clauses) + : Statement(Kind::Match, loc), exp(exp), clauses(std::move(clauses)) {} static auto classof(const Statement* stmt) -> bool { return stmt->Tag() == Kind::Match; @@ -235,13 +235,13 @@ class Match : public Statement { auto Exp() const -> Ptr { return exp; } auto Clauses() const - -> const std::list, Ptr>>* { + -> const std::list, Ptr>>& { return clauses; } private: Ptr exp; - std::list, Ptr>>* clauses; + std::list, Ptr>> clauses; }; // A continuation statement. diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 88f5982fe3db..fa9550f70b0d 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -765,12 +765,12 @@ auto Interpreter::StepStmt() -> Transition { // * 2: the pattern for clause 1 // * ... auto clause_num = (act->Pos() - 1) / 2; - if (clause_num >= static_cast(match_stmt.Clauses()->size())) { + if (clause_num >= static_cast(match_stmt.Clauses().size())) { DeallocateScope(frame->scopes.Top()); frame->scopes.Pop(); return Done{}; } - auto c = match_stmt.Clauses()->begin(); + auto c = match_stmt.Clauses().begin(); std::advance(c, clause_num); if (act->Pos() % 2 == 1) { @@ -784,7 +784,7 @@ auto Interpreter::StepStmt() -> Transition { std::optional matches = PatternMatch(pat, v, stmt->SourceLoc()); if (matches) { // we have a match, start the body // Ensure we don't process any more clauses. - act->SetPos(2 * match_stmt.Clauses()->size() + 1); + act->SetPos(2 * match_stmt.Clauses().size() + 1); for (const auto& [name, value] : *matches) { frame->scopes.Top()->values.Set(name, value); diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index a3dcafd14290..02580ee2c252 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -667,12 +667,12 @@ auto TypeChecker::TypeCheckStmt(Ptr s, TypeEnv types, const auto& match = cast(*s); auto res = TypeCheckExp(match.Exp(), types, values); auto res_type = res.type; - auto new_clauses = global_arena->RawNew< - std::list, Ptr>>>(); - for (auto& clause : *match.Clauses()) { - new_clauses->push_back(TypeCheckCase(res_type, clause.first, - clause.second, types, values, - ret_type, is_omitted_ret_type)); + std::list, Ptr>> + new_clauses; + for (auto& clause : match.Clauses()) { + new_clauses.push_back(TypeCheckCase(res_type, clause.first, + clause.second, types, values, + ret_type, is_omitted_ret_type)); } auto new_s = global_arena->New(s->SourceLoc(), res.exp, new_clauses); @@ -826,12 +826,12 @@ static auto CheckOrEnsureReturn(std::optional> opt_stmt, switch (stmt->Tag()) { case Statement::Kind::Match: { const auto& match = cast(*stmt); - auto new_clauses = global_arena->RawNew< - std::list, Ptr>>>(); - for (const auto& clause : *match.Clauses()) { + std::list, Ptr>> + new_clauses; + for (const auto& clause : match.Clauses()) { auto s = CheckOrEnsureReturn(clause.second, omitted_ret_type, stmt->SourceLoc()); - new_clauses->push_back(std::make_pair(clause.first, s)); + new_clauses.push_back(std::make_pair(clause.first, s)); } return global_arena->New(stmt->SourceLoc(), match.Exp(), new_clauses); diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index c2aaad4365ec..141203ac1b3e 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -128,8 +128,8 @@ %type > paren_pattern_contents %type >>> alternative %type >>> alternative_list -%type , Ptr>*> clause -%type , Ptr>>*> clause_list +%type , Ptr>>> clause +%type , Ptr>>> clause_list %token // Most tokens have their spelling defined in lexer.lpp. @@ -460,31 +460,22 @@ maybe_empty_tuple_pattern: ; clause: CASE pattern DOUBLE_ARROW statement - { - $$ = global_arena - ->RawNew, Ptr>>( - $2, $4); - } + { $$ = std::pair, Ptr>($2, $4); } | DEFAULT DOUBLE_ARROW statement { auto vp = global_arena -> New( context.SourceLoc(), std::nullopt, global_arena->New(context.SourceLoc())); - $$ = global_arena - ->RawNew, Ptr>>( - vp, $3); + $$ = std::pair, Ptr>(vp, $3); } ; clause_list: // Empty - { - $$ = global_arena->RawNew< - std::list, Ptr>>>(); - } + { $$ = std::list, Ptr>>(); } | clause clause_list { $$ = $2; - $$->push_front(*$1); + $$.push_front($1); } ; statement: