• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 panda::es2panda::checker {
22 class TSAnalyzer;
23 }  // namespace panda::es2panda::checker
24 namespace panda::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()54     const Expression *Init() const
55     {
56         return init_;
57     }
58 
Id()59     Expression *Id()
60     {
61         return id_;
62     }
63 
Id()64     const Expression *Id() const
65     {
66         return id_;
67     }
68 
Flag()69     VariableDeclaratorFlag Flag()
70     {
71         return flag_;
72     }
73 
74     void TransformChildren(const NodeTransformer &cb) override;
75     void Iterate(const NodeTraverser &cb) const override;
76     void Dump(ir::AstDumper *dumper) const override;
77     void Dump(ir::SrcDumper *dumper) const override;
78     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
79     void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
80     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
81     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
82 
Accept(ASTVisitorT * v)83     void Accept(ASTVisitorT *v) override
84     {
85         v->Accept(this);
86     }
87 
88 private:
89     Expression *id_;
90     Expression *init_ {};
91     const VariableDeclaratorFlag flag_;
92 };
93 }  // namespace panda::es2panda::ir
94 
95 #endif
96