1 /* 2 * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "baseAnalyzer.h" 17 #include "ir/astNode.h" 18 #include "ir/statements/breakStatement.h" 19 #include "ir/statements/continueStatement.h" 20 21 namespace panda::es2panda::checker { ClearPendingExits()22void BaseAnalyzer::ClearPendingExits() 23 { 24 pendingExits_.clear(); 25 } 26 PendingExits()27PendingExitsVector &BaseAnalyzer::PendingExits() 28 { 29 return pendingExits_; 30 } 31 SetPendingExits(const PendingExitsVector & pendingExits)32void BaseAnalyzer::SetPendingExits(const PendingExitsVector &pendingExits) 33 { 34 pendingExits_ = pendingExits; 35 } 36 OldPendingExits()37PendingExitsVector &BaseAnalyzer::OldPendingExits() 38 { 39 return oldPendingExits_; 40 } 41 SetOldPendingExits(const PendingExitsVector & oldPendingExits)42void BaseAnalyzer::SetOldPendingExits(const PendingExitsVector &oldPendingExits) 43 { 44 oldPendingExits_ = oldPendingExits; 45 } 46 GetJumpTarget(const ir::AstNode * node) const47const ir::AstNode *BaseAnalyzer::GetJumpTarget(const ir::AstNode *node) const 48 { 49 if (node->IsBreakStatement()) { 50 return node->AsBreakStatement()->Target(); 51 } 52 53 ASSERT(node->IsContinueStatement()); 54 return node->AsContinueStatement()->Target(); 55 } 56 ResolveJump(const ir::AstNode * node,ir::AstNodeType jumpKind)57LivenessStatus BaseAnalyzer::ResolveJump(const ir::AstNode *node, ir::AstNodeType jumpKind) 58 { 59 bool resolved = false; 60 PendingExitsVector exits = pendingExits_; 61 pendingExits_ = oldPendingExits_; 62 63 for (auto &it : exits) { 64 if (it.Node()->Type() == jumpKind && node == GetJumpTarget(it.Node())) { 65 it.ResolveJump(); 66 resolved = true; 67 } else { 68 pendingExits_.push_back(it); 69 } 70 } 71 72 return From(resolved); 73 } 74 ResolveContinues(const ir::AstNode * node)75LivenessStatus BaseAnalyzer::ResolveContinues(const ir::AstNode *node) 76 { 77 oldPendingExits_.clear(); 78 return ResolveJump(node, ir::AstNodeType::CONTINUE_STATEMENT); 79 } 80 ResolveBreaks(const ir::AstNode * node)81LivenessStatus BaseAnalyzer::ResolveBreaks(const ir::AstNode *node) 82 { 83 return ResolveJump(node, ir::AstNodeType::BREAK_STATEMENT); 84 } 85 86 } // namespace panda::es2panda::checker 87