From 83fc84ca22f0f1368150bf61b5399a907a0dc1fc Mon Sep 17 00:00:00 2001 From: Sergey Chernykh Date: Sun, 9 Mar 2025 19:23:52 +0800 Subject: [PATCH] Support clone TryStatement Description: * Support clone for TryStatement and CatchClause Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBRWSH Signed-off-by: Sergey Chernykh --- ets2panda/ir/base/catchClause.cpp | 18 +++++++++++++ ets2panda/ir/base/catchClause.h | 3 ++- ets2panda/ir/statements/tryStatement.cpp | 27 +++++++++++++++++++ ets2panda/ir/statements/tryStatement.h | 3 +++ .../ets/try_catch_lambda_interface.sts | 24 +++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/try_catch_lambda_interface.sts diff --git a/ets2panda/ir/base/catchClause.cpp b/ets2panda/ir/base/catchClause.cpp index ae47992eba..53b37af715 100644 --- a/ets2panda/ir/base/catchClause.cpp +++ b/ets2panda/ir/base/catchClause.cpp @@ -93,4 +93,22 @@ checker::VerifiedType CatchClause::Check(checker::ETSChecker *checker) { return {this, checker->GetAnalyzer()->Check(this)}; } + +CatchClause::CatchClause(CatchClause const &other, ArenaAllocator *allocator) : TypedStatement(other) +{ + param_ = other.param_ == nullptr ? nullptr : other.param_->Clone(allocator, this)->AsExpression(); + body_ = other.body_ == nullptr ? nullptr : other.body_->Clone(allocator, this)->AsBlockStatement(); +} + +CatchClause *CatchClause::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const clone = allocator->New(*this, allocator); + if (parent != nullptr) { + clone->SetParent(parent); + } + + clone->SetRange(Range()); + return clone; +} + } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/base/catchClause.h b/ets2panda/ir/base/catchClause.h index d5f3ab34c6..3c03d6ddc5 100644 --- a/ets2panda/ir/base/catchClause.h +++ b/ets2panda/ir/base/catchClause.h @@ -29,7 +29,7 @@ public: : TypedStatement(AstNodeType::CATCH_CLAUSE), param_(param), body_(body) { } - + explicit CatchClause(CatchClause const &other, ArenaAllocator *allocator); Expression *Param() { return param_; @@ -85,6 +85,7 @@ public: { v->Accept(this); } + [[nodiscard]] CatchClause *Clone(ArenaAllocator *allocator, AstNode *parent) override; private: varbinder::CatchScope *scope_ {nullptr}; diff --git a/ets2panda/ir/statements/tryStatement.cpp b/ets2panda/ir/statements/tryStatement.cpp index 3b8549531a..7281fa5e60 100644 --- a/ets2panda/ir/statements/tryStatement.cpp +++ b/ets2panda/ir/statements/tryStatement.cpp @@ -119,4 +119,31 @@ checker::VerifiedType TryStatement::Check([[maybe_unused]] checker::ETSChecker * return {this, checker->GetAnalyzer()->Check(this)}; } +TryStatement::TryStatement(TryStatement const &other, ArenaAllocator *allocator) + : Statement(other), + catchClauses_(allocator->Adapter()), + finalizerInsertions_(allocator->Adapter()), + finallyCanCompleteNormally_(other.finallyCanCompleteNormally_) +{ + block_ = other.block_ == nullptr ? nullptr : other.block_->Clone(allocator, this)->AsBlockStatement(); + for (auto &cc : other.catchClauses_) { + catchClauses_.push_back(cc == nullptr ? nullptr : cc->Clone(allocator, this)->AsCatchClause()); + } + finalizer_ = other.finalizer_ == nullptr ? nullptr : other.finalizer_->Clone(allocator, this)->AsBlockStatement(); + for (auto &[label, st] : other.finalizerInsertions_) { + finalizerInsertions_.emplace_back(label, st); + } +} + +TryStatement *TryStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const clone = allocator->New(*this, allocator); + if (parent != nullptr) { + clone->SetParent(parent); + } + + clone->SetRange(Range()); + return clone; +} + } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/statements/tryStatement.h b/ets2panda/ir/statements/tryStatement.h index 4100a7a795..248d97a785 100644 --- a/ets2panda/ir/statements/tryStatement.h +++ b/ets2panda/ir/statements/tryStatement.h @@ -68,6 +68,7 @@ public: } } + explicit TryStatement(TryStatement const &other, ArenaAllocator *allocator); friend class checker::TSAnalyzer; friend class checker::ETSAnalyzer; friend class compiler::JSCompiler; @@ -127,6 +128,8 @@ public: v->Accept(this); } + [[nodiscard]] TryStatement *Clone(ArenaAllocator *allocator, AstNode *parent) override; + private: BlockStatement *block_; ArenaVector catchClauses_; diff --git a/ets2panda/test/ast/compiler/ets/try_catch_lambda_interface.sts b/ets2panda/test/ast/compiler/ets/try_catch_lambda_interface.sts new file mode 100644 index 0000000000..aba67949a0 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/try_catch_lambda_interface.sts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +interface AsyncCallback{ + callback: Object +} + +class A implements AsyncCallback{ + callback: Object = async (a?:string[])=>{ + try{} catch(error){} + }; +} \ No newline at end of file -- Gitee