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