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_EXPRESSION_ETS_PARAMETER_EXPRESSION_H
17 #define ES2PANDA_IR_EXPRESSION_ETS_PARAMETER_EXPRESSION_H
18 
19 #include "ir/expression.h"
20 
21 namespace ark::es2panda::checker {
22 class ETSAnalyzer;
23 }  // namespace ark::es2panda::checker
24 
25 namespace ark::es2panda::ir {
26 
27 class ETSParameterExpression final : public Expression {
28 public:
29     ETSParameterExpression() = delete;
30     ~ETSParameterExpression() override = default;
31 
32     NO_COPY_SEMANTIC(ETSParameterExpression);
33     NO_MOVE_SEMANTIC(ETSParameterExpression);
34 
35     explicit ETSParameterExpression(AnnotatedExpression *identOrSpread, Expression *initializer);
36 
37     // NOTE (csabahurton): friend relationship can be removed once there are getters for private fields
38     friend class checker::ETSAnalyzer;
39 
40     [[nodiscard]] const Identifier *Ident() const noexcept;
41     [[nodiscard]] Identifier *Ident() noexcept;
42 
43     [[nodiscard]] const SpreadElement *RestParameter() const noexcept;
44     [[nodiscard]] SpreadElement *RestParameter() noexcept;
45 
46     [[nodiscard]] const Expression *Initializer() const noexcept;
47     [[nodiscard]] Expression *Initializer() noexcept;
48 
49     void SetLexerSaved(util::StringView s) noexcept;
50     [[nodiscard]] util::StringView LexerSaved() const noexcept;
51 
52     [[nodiscard]] varbinder::Variable *Variable() const noexcept;
53     void SetVariable(varbinder::Variable *variable) noexcept;
54 
55     [[nodiscard]] TypeNode const *TypeAnnotation() const noexcept;
56     [[nodiscard]] TypeNode *TypeAnnotation() noexcept;
57 
58     [[nodiscard]] bool IsDefault() const noexcept
59     {
60         return initializer_ != nullptr;
61     }
62 
63     [[nodiscard]] bool IsRestParameter() const noexcept
64     {
65         return spread_ != nullptr;
66     }
67 
68     [[nodiscard]] std::size_t GetRequiredParams() const noexcept
69     {
70         return extraValue_;
71     }
72 
73     void SetRequiredParams(std::size_t const value) noexcept
74     {
75         extraValue_ = value;
76     }
77 
78     [[nodiscard]] ETSParameterExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override;
79 
80     void Iterate(const NodeTraverser &cb) const override;
81     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
82     void Dump(ir::AstDumper *dumper) const override;
83     void Dump(ir::SrcDumper *dumper) const override;
84     void Compile(compiler::PandaGen *pg) const override;
85     void Compile(compiler::ETSGen *etsg) const override;
86     checker::Type *Check(checker::TSChecker *checker) override;
87     checker::Type *Check(checker::ETSChecker *checker) override;
SetInitializer(Expression *initExpr = nullptr)88     void SetInitializer(Expression *initExpr = nullptr)
89     {
90         initializer_ = initExpr;
91     };
92 
93     void Accept(ASTVisitorT *v) override
94     {
95         v->Accept(this);
96     }
97 
98 private:
99     Identifier *ident_;
100     Expression *initializer_;
101     SpreadElement *spread_ = nullptr;
102     util::StringView savedLexer_ = "";
103     std::size_t extraValue_ = 0U;
104 };
105 }  // namespace ark::es2panda::ir
106 
107 #endif
108