1 /**
2  * Copyright (c) 2021-2024 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_IR_STATEMENT_VARIABLE_DECLARATOR_H
17 #define ES2PANDA_IR_STATEMENT_VARIABLE_DECLARATOR_H
18 
19 #include "ir/expression.h"
20 #include "ir/statement.h"
21 namespace ark::es2panda::checker {
22 class TSAnalyzer;
23 }  // namespace ark::es2panda::checker
24 namespace ark::es2panda::ir {
25 class Expression;
26 
27 enum class VariableDeclaratorFlag {
28     LET,
29     CONST,
30     VAR,
31     UNKNOWN,
32 };
33 
34 class VariableDeclarator : public TypedStatement {
35 public:
VariableDeclarator(VariableDeclaratorFlag flag, Expression *ident)36     explicit VariableDeclarator(VariableDeclaratorFlag flag, Expression *ident)
37         : TypedStatement(AstNodeType::VARIABLE_DECLARATOR), id_(ident), flag_(flag)
38     {
39     }
40 
VariableDeclarator(VariableDeclaratorFlag flag, Expression *ident, Expression *init)41     explicit VariableDeclarator(VariableDeclaratorFlag flag, Expression *ident, Expression *init)
42         : TypedStatement(AstNodeType::VARIABLE_DECLARATOR), id_(ident), init_(init), flag_(flag)
43     {
44     }
45 
46     // NOTE (vivienvoros): these friend relationships can be removed once there are getters for private fields
47     friend class checker::TSAnalyzer;
48 
Init()49     Expression *Init()
50     {
51         return init_;
52     }
53 
Init() const54     const Expression *Init() const
55     {
56         return init_;
57     }
58 
SetInit(Expression *init)59     void SetInit(Expression *init)
60     {
61         init_ = init;
62         init_->SetParent(this);
63     }
64 
Id()65     Expression *Id()
66     {
67         return id_;
68     }
69 
Id() const70     const Expression *Id() const
71     {
72         return id_;
73     }
74 
Flag()75     VariableDeclaratorFlag Flag()
76     {
77         return flag_;
78     }
79 
80     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
81     void Iterate(const NodeTraverser &cb) const override;
82     void Dump(ir::AstDumper *dumper) const override;
83     void Dump(ir::SrcDumper *dumper) const override;
84     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
85     void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
86     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
87     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
88 
89     void Accept(ASTVisitorT *v) override
90     {
91         v->Accept(this);
92     }
93 
94     [[nodiscard]] VariableDeclarator *Clone(ArenaAllocator *allocator, AstNode *parent) override;
95 
96 private:
97     Expression *id_;
98     Expression *init_ {};
99     const VariableDeclaratorFlag flag_;
100 };
101 }  // namespace ark::es2panda::ir
102 
103 #endif
104