• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ES2PANDA_COMPILER_CHECKER_ETS_ALIVE_ANALYZER_H
17 #define ES2PANDA_COMPILER_CHECKER_ETS_ALIVE_ANALYZER_H
18 
19 #include "checker/ETSchecker.h"
20 #include "checker/ets/baseAnalyzer.h"
21 
22 #include "utils/arena_containers.h"
23 
24 namespace panda::es2panda::ir {
25 class AstNode;
26 class Statement;
27 class ClassDefinition;
28 class MethodDefinition;
29 class DoWhileStatement;
30 class VariableDeclaration;
31 }  // namespace panda::es2panda::ir
32 
33 namespace panda::es2panda::checker {
34 class AliveAnalyzer : public BaseAnalyzer {
35 public:
36     // NOLINTNEXTLINE(readability-redundant-member-init)
AliveAnalyzer(const ir::AstNode * node,ETSChecker * checker)37     AliveAnalyzer(const ir::AstNode *node, ETSChecker *checker) : BaseAnalyzer(), checker_(checker)
38     {
39         AnalyzeNodes(node);
40     }
41 
MarkDead()42     void MarkDead() override
43     {
44         status_ = LivenessStatus::DEAD;
45     }
46 
Or(LivenessStatus left,LivenessStatus right)47     LivenessStatus Or(LivenessStatus left, LivenessStatus right)
48     {
49         return static_cast<LivenessStatus>(left | right);
50     }
51 
And(LivenessStatus left,LivenessStatus right)52     LivenessStatus And(LivenessStatus left, LivenessStatus right)
53     {
54         return static_cast<LivenessStatus>(left & right);
55     }
56 
57 private:
58     void AnalyzeNodes(const ir::AstNode *node);
59     void AnalyzeNode(const ir::AstNode *node);
60     void AnalyzeDef(const ir::AstNode *node);
61     void AnalyzeStat(const ir::AstNode *node);
62     void AnalyzeStats(const ArenaVector<ir::Statement *> &stats);
63     void AnalyzeStructDecl(const ir::ETSStructDeclaration *structDecl);
64     void AnalyzeClassDecl(const ir::ClassDeclaration *classDecl);
65     void AnalyzeClassDef(const ir::ClassDefinition *classDef);
66     void AnalyzeMethodDef(const ir::MethodDefinition *methodDef);
67     void AnalyzeVarDef(const ir::VariableDeclaration *varDef);
68     void AnalyzeDoLoop(const ir::DoWhileStatement *doWhile);
69     void AnalyzeWhileLoop(const ir::WhileStatement *whileStmt);
70     void AnalyzeForLoop(const ir::ForUpdateStatement *forStmt);
71     void AnalyzeForOfLoop(const ir::ForOfStatement *forOfStmt);
72     void AnalyzeIf(const ir::IfStatement *ifStmt);
73     void AnalyzeLabelled(const ir::LabelledStatement *labelledStmt);
74     void AnalyzeNewClass(const ir::ETSNewClassInstanceExpression *newClass);
75     void AnalyzeCall(const ir::CallExpression *callExpr);
76     void AnalyzeThrow(const ir::ThrowStatement *throwStmt);
77     void AnalyzeSwitch(const ir::SwitchStatement *switchStmt);
78     void AnalyzeTry(const ir::TryStatement *tryStmt);
79     void AnalyzeBreak(const ir::BreakStatement *breakStmt);
80     void AnalyzeContinue(const ir::ContinueStatement *contStmt);
81     void AnalyzeReturn(const ir::ReturnStatement *retStmt);
82 
83     ETSChecker *checker_;
84     LivenessStatus status_ {LivenessStatus::ALIVE};
85 };
86 }  // namespace panda::es2panda::checker
87 
88 #endif
89