13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#ifndef ES2PANDA_COMPILER_CORE_DYNAMIC_CONTEXT_H 173af6ab5fSopenharmony_ci#define ES2PANDA_COMPILER_CORE_DYNAMIC_CONTEXT_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include <util/ustring.h> 203af6ab5fSopenharmony_ci#include <ir/irnode.h> 213af6ab5fSopenharmony_ci#include <compiler/core/labelTarget.h> 223af6ab5fSopenharmony_ci#include <compiler/base/iterators.h> 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_cinamespace panda::es2panda::ir { 253af6ab5fSopenharmony_ciclass TryStatement; 263af6ab5fSopenharmony_ciclass ForOfStatement; 273af6ab5fSopenharmony_ciclass LabelledStatement; 283af6ab5fSopenharmony_ci} // namespace panda::es2panda::ir 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_cinamespace panda::es2panda::compiler { 313af6ab5fSopenharmony_ciclass PandaGen; 323af6ab5fSopenharmony_ciclass VariableEnvScope; 333af6ab5fSopenharmony_ciclass CatchTable; 343af6ab5fSopenharmony_ciclass TryLabelSet; 353af6ab5fSopenharmony_ci 363af6ab5fSopenharmony_cienum class DynamicContextType { NONE, LABEL, LEX_ENV, ITERATOR, TRY }; 373af6ab5fSopenharmony_ci 383af6ab5fSopenharmony_ciclass DynamicContext { 393af6ab5fSopenharmony_cipublic: 403af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(DynamicContext); 413af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(DynamicContext); 423af6ab5fSopenharmony_ci virtual ~DynamicContext(); 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_ci void *operator new(size_t) = delete; 453af6ab5fSopenharmony_ci void *operator new[](size_t) = delete; 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_ci virtual void AbortContext([[maybe_unused]] ControlFlowChange cfc, 483af6ab5fSopenharmony_ci [[maybe_unused]] const util::StringView &targetLabel) {}; 493af6ab5fSopenharmony_ci 503af6ab5fSopenharmony_ci virtual bool HasTryCatch() const 513af6ab5fSopenharmony_ci { 523af6ab5fSopenharmony_ci return false; 533af6ab5fSopenharmony_ci } 543af6ab5fSopenharmony_ci 553af6ab5fSopenharmony_ci virtual bool HasFinalizer() const 563af6ab5fSopenharmony_ci { 573af6ab5fSopenharmony_ci return HasTryCatch(); 583af6ab5fSopenharmony_ci } 593af6ab5fSopenharmony_ci 603af6ab5fSopenharmony_ci virtual DynamicContextType Type() const = 0; 613af6ab5fSopenharmony_ci 623af6ab5fSopenharmony_ci DynamicContext *Prev() 633af6ab5fSopenharmony_ci { 643af6ab5fSopenharmony_ci return prev_; 653af6ab5fSopenharmony_ci } 663af6ab5fSopenharmony_ci 673af6ab5fSopenharmony_ci const DynamicContext *Prev() const 683af6ab5fSopenharmony_ci { 693af6ab5fSopenharmony_ci return prev_; 703af6ab5fSopenharmony_ci } 713af6ab5fSopenharmony_ci 723af6ab5fSopenharmony_ci const LabelTarget &Target() const 733af6ab5fSopenharmony_ci { 743af6ab5fSopenharmony_ci return target_; 753af6ab5fSopenharmony_ci } 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_ciprotected: 783af6ab5fSopenharmony_ci explicit DynamicContext(PandaGen *pg, LabelTarget target); 793af6ab5fSopenharmony_ci 803af6ab5fSopenharmony_ci PandaGen *pg_; 813af6ab5fSopenharmony_ci LabelTarget target_; 823af6ab5fSopenharmony_ci DynamicContext *prev_ {}; 833af6ab5fSopenharmony_ci}; 843af6ab5fSopenharmony_ci 853af6ab5fSopenharmony_ciclass LabelContext : public DynamicContext { 863af6ab5fSopenharmony_cipublic: 873af6ab5fSopenharmony_ci explicit LabelContext(PandaGen *pg, LabelTarget target) : DynamicContext(pg, target) {} 883af6ab5fSopenharmony_ci explicit LabelContext(PandaGen *pg, const ir::LabelledStatement *labelledStmt); 893af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(LabelContext); 903af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(LabelContext); 913af6ab5fSopenharmony_ci ~LabelContext(); 923af6ab5fSopenharmony_ci 933af6ab5fSopenharmony_ci DynamicContextType Type() const override 943af6ab5fSopenharmony_ci { 953af6ab5fSopenharmony_ci return DynamicContextType::LABEL; 963af6ab5fSopenharmony_ci } 973af6ab5fSopenharmony_ci 983af6ab5fSopenharmony_ciprivate: 993af6ab5fSopenharmony_ci Label *label_ {}; 1003af6ab5fSopenharmony_ci const ir::LabelledStatement *labelledStmt_ {}; 1013af6ab5fSopenharmony_ci}; 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_ciclass LexEnvContext : public DynamicContext { 1043af6ab5fSopenharmony_cipublic: 1053af6ab5fSopenharmony_ci explicit LexEnvContext(VariableEnvScope *envScope, PandaGen *pg, LabelTarget target); 1063af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(LexEnvContext); 1073af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(LexEnvContext); 1083af6ab5fSopenharmony_ci ~LexEnvContext(); 1093af6ab5fSopenharmony_ci 1103af6ab5fSopenharmony_ci DynamicContextType Type() const override 1113af6ab5fSopenharmony_ci { 1123af6ab5fSopenharmony_ci return DynamicContextType::LEX_ENV; 1133af6ab5fSopenharmony_ci } 1143af6ab5fSopenharmony_ci 1153af6ab5fSopenharmony_ci bool HasTryCatch() const override; 1163af6ab5fSopenharmony_ci void AbortContext([[maybe_unused]] ControlFlowChange cfc, 1173af6ab5fSopenharmony_ci [[maybe_unused]] const util::StringView &targetLabel) override; 1183af6ab5fSopenharmony_ci 1193af6ab5fSopenharmony_ciprivate: 1203af6ab5fSopenharmony_ci VariableEnvScope *envScope_; 1213af6ab5fSopenharmony_ci CatchTable *catchTable_ {}; 1223af6ab5fSopenharmony_ci}; 1233af6ab5fSopenharmony_ci 1243af6ab5fSopenharmony_ciclass IteratorContext : public DynamicContext { 1253af6ab5fSopenharmony_cipublic: 1263af6ab5fSopenharmony_ci explicit IteratorContext(PandaGen *pg, const Iterator &iterator, LabelTarget target); 1273af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(IteratorContext); 1283af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(IteratorContext); 1293af6ab5fSopenharmony_ci ~IteratorContext(); 1303af6ab5fSopenharmony_ci 1313af6ab5fSopenharmony_ci DynamicContextType Type() const override 1323af6ab5fSopenharmony_ci { 1333af6ab5fSopenharmony_ci return DynamicContextType::ITERATOR; 1343af6ab5fSopenharmony_ci } 1353af6ab5fSopenharmony_ci 1363af6ab5fSopenharmony_ci const Iterator &GetIterator() const 1373af6ab5fSopenharmony_ci { 1383af6ab5fSopenharmony_ci return iterator_; 1393af6ab5fSopenharmony_ci } 1403af6ab5fSopenharmony_ci 1413af6ab5fSopenharmony_ci bool HasTryCatch() const override 1423af6ab5fSopenharmony_ci { 1433af6ab5fSopenharmony_ci return true; 1443af6ab5fSopenharmony_ci } 1453af6ab5fSopenharmony_ci 1463af6ab5fSopenharmony_ci void AbortContext([[maybe_unused]] ControlFlowChange cfc, 1473af6ab5fSopenharmony_ci [[maybe_unused]] const util::StringView &targetLabel) override; 1483af6ab5fSopenharmony_ci 1493af6ab5fSopenharmony_ciprivate: 1503af6ab5fSopenharmony_ci const Iterator &iterator_; 1513af6ab5fSopenharmony_ci CatchTable *catchTable_; 1523af6ab5fSopenharmony_ci}; 1533af6ab5fSopenharmony_ci 1543af6ab5fSopenharmony_ciclass DestructuringIteratorContext : public DynamicContext { 1553af6ab5fSopenharmony_cipublic: 1563af6ab5fSopenharmony_ci explicit DestructuringIteratorContext(PandaGen *pg, const DestructuringIterator &iterator); 1573af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(DestructuringIteratorContext); 1583af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(DestructuringIteratorContext); 1593af6ab5fSopenharmony_ci ~DestructuringIteratorContext() override; 1603af6ab5fSopenharmony_ci 1613af6ab5fSopenharmony_ci DynamicContextType Type() const override 1623af6ab5fSopenharmony_ci { 1633af6ab5fSopenharmony_ci return DynamicContextType::ITERATOR; 1643af6ab5fSopenharmony_ci } 1653af6ab5fSopenharmony_ci 1663af6ab5fSopenharmony_ci const DestructuringIterator &GetIterator() const 1673af6ab5fSopenharmony_ci { 1683af6ab5fSopenharmony_ci return iterator_; 1693af6ab5fSopenharmony_ci } 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ci bool HasTryCatch() const override 1723af6ab5fSopenharmony_ci { 1733af6ab5fSopenharmony_ci return true; 1743af6ab5fSopenharmony_ci } 1753af6ab5fSopenharmony_ci 1763af6ab5fSopenharmony_ci void AbortContext(ControlFlowChange cfc, const util::StringView &targetLabel) override; 1773af6ab5fSopenharmony_ci 1783af6ab5fSopenharmony_ciprivate: 1793af6ab5fSopenharmony_ci const DestructuringIterator &iterator_; 1803af6ab5fSopenharmony_ci CatchTable *catchTable_; 1813af6ab5fSopenharmony_ci}; 1823af6ab5fSopenharmony_ci 1833af6ab5fSopenharmony_ciclass TryContext : public DynamicContext { 1843af6ab5fSopenharmony_cipublic: 1853af6ab5fSopenharmony_ci explicit TryContext(PandaGen *pg, const ir::TryStatement *tryStmt, bool hasFinalizer = true) 1863af6ab5fSopenharmony_ci : DynamicContext(pg, {}), tryStmt_(tryStmt), hasFinalizer_(hasFinalizer) 1873af6ab5fSopenharmony_ci { 1883af6ab5fSopenharmony_ci InitCatchTable(); 1893af6ab5fSopenharmony_ci InitFinalizer(); 1903af6ab5fSopenharmony_ci } 1913af6ab5fSopenharmony_ci 1923af6ab5fSopenharmony_ci explicit TryContext(PandaGen *pg) : DynamicContext(pg, {}) 1933af6ab5fSopenharmony_ci { 1943af6ab5fSopenharmony_ci InitCatchTable(); 1953af6ab5fSopenharmony_ci } 1963af6ab5fSopenharmony_ci 1973af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(TryContext); 1983af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(TryContext); 1993af6ab5fSopenharmony_ci ~TryContext() = default; 2003af6ab5fSopenharmony_ci 2013af6ab5fSopenharmony_ci DynamicContextType Type() const override 2023af6ab5fSopenharmony_ci { 2033af6ab5fSopenharmony_ci return DynamicContextType::TRY; 2043af6ab5fSopenharmony_ci } 2053af6ab5fSopenharmony_ci 2063af6ab5fSopenharmony_ci bool HasTryCatch() const override 2073af6ab5fSopenharmony_ci { 2083af6ab5fSopenharmony_ci return true; 2093af6ab5fSopenharmony_ci } 2103af6ab5fSopenharmony_ci 2113af6ab5fSopenharmony_ci VReg FinalizerRun() const 2123af6ab5fSopenharmony_ci { 2133af6ab5fSopenharmony_ci return finalizerRun_; 2143af6ab5fSopenharmony_ci } 2153af6ab5fSopenharmony_ci 2163af6ab5fSopenharmony_ci CatchTable *GetCatchTable() const 2173af6ab5fSopenharmony_ci { 2183af6ab5fSopenharmony_ci return catchTable_; 2193af6ab5fSopenharmony_ci } 2203af6ab5fSopenharmony_ci 2213af6ab5fSopenharmony_ci const TryLabelSet &LabelSet() const; 2223af6ab5fSopenharmony_ci bool HasFinalizer() const override; 2233af6ab5fSopenharmony_ci void InitFinalizer(); 2243af6ab5fSopenharmony_ci void EmitFinalizer(); 2253af6ab5fSopenharmony_ci 2263af6ab5fSopenharmony_ci void AbortContext([[maybe_unused]] ControlFlowChange cfc, 2273af6ab5fSopenharmony_ci [[maybe_unused]] const util::StringView &targetLabel) override; 2283af6ab5fSopenharmony_ci 2293af6ab5fSopenharmony_ciprivate: 2303af6ab5fSopenharmony_ci void InitCatchTable(); 2313af6ab5fSopenharmony_ci 2323af6ab5fSopenharmony_ci const ir::TryStatement *tryStmt_ {}; 2333af6ab5fSopenharmony_ci CatchTable *catchTable_ {}; 2343af6ab5fSopenharmony_ci VReg finalizerRun_ {}; 2353af6ab5fSopenharmony_ci bool hasFinalizer_ {}; 2363af6ab5fSopenharmony_ci bool inFinalizer_ {}; 2373af6ab5fSopenharmony_ci}; 2383af6ab5fSopenharmony_ci} // namespace panda::es2panda::compiler 2393af6ab5fSopenharmony_ci 2403af6ab5fSopenharmony_ci#endif 241