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_DECLARATION_H
17#define ES2PANDA_IR_STATEMENT_VARIABLE_DECLARATION_H
18
19#include "ir/statement.h"
20
21namespace ark::es2panda::ir {
22class VariableDeclarator;
23
24class VariableDeclaration : public Statement {
25private:
26    struct Tag {};
27
28public:
29    enum class VariableDeclarationKind { CONST, LET, VAR };
30
31    explicit VariableDeclaration(VariableDeclarationKind kind, ArenaAllocator *allocator,
32                                 ArenaVector<VariableDeclarator *> &&declarators, bool declare)
33        : Statement(AstNodeType::VARIABLE_DECLARATION),
34          kind_(kind),
35          decorators_(allocator->Adapter()),
36          declarators_(std::move(declarators)),
37          declare_(declare)
38    {
39    }
40
41    explicit VariableDeclaration(Tag tag, VariableDeclaration const &other, ArenaAllocator *allocator);
42
43    const ArenaVector<VariableDeclarator *> &Declarators() const
44    {
45        return declarators_;
46    }
47
48    VariableDeclarationKind Kind() const
49    {
50        return kind_;
51    }
52
53    bool Declare() const
54    {
55        return declare_;
56    }
57
58    const ArenaVector<Decorator *> &Decorators() const
59    {
60        return decorators_;
61    }
62
63    const ArenaVector<Decorator *> *DecoratorsPtr() const override
64    {
65        return &Decorators();
66    }
67
68    void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override
69    {
70        decorators_ = std::move(decorators);
71    }
72
73    bool CanHaveDecorator([[maybe_unused]] bool inTs) const override
74    {
75        return true;
76    }
77
78    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
79    void Iterate(const NodeTraverser &cb) const override;
80    void Dump(ir::AstDumper *dumper) const override;
81    void Dump(ir::SrcDumper *dumper) const override;
82    void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
83    void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
84    checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
85    checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
86
87    void Accept(ASTVisitorT *v) override
88    {
89        v->Accept(this);
90    }
91
92    [[nodiscard]] VariableDeclaration *Clone(ArenaAllocator *allocator, AstNode *parent) override;
93
94private:
95    VariableDeclarationKind kind_;
96    ArenaVector<Decorator *> decorators_;
97    ArenaVector<VariableDeclarator *> declarators_;
98    bool declare_;
99};
100}  // namespace ark::es2panda::ir
101
102#endif
103