1 /* 2 * Copyright (c) 2021-2025 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/annotationAllowed.h" 20 #include "ir/expression.h" 21 #include "ir/expressions/identifier.h" 22 #include "ir/statements/annotationUsage.h" 23 24 namespace ark::es2panda::checker { 25 class ETSAnalyzer; 26 } // namespace ark::es2panda::checker 27 28 namespace ark::es2panda::ir { 29 30 class ETSParameterExpression final : public AnnotationAllowed<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, bool isOptional, 39 ArenaAllocator *const allocator); 40 41 explicit ETSParameterExpression(AnnotatedExpression *identOrSpread, ir::Expression *initializer, 42 ArenaAllocator *const allocator); 43 44 [[nodiscard]] const util::StringView &Name() const noexcept; 45 46 [[nodiscard]] const Identifier *Ident() const noexcept; 47 [[nodiscard]] Identifier *Ident() noexcept; 48 SetIdent(Identifier * ident)49 void SetIdent(Identifier *ident) noexcept 50 { 51 ident_ = ident; 52 ES2PANDA_ASSERT(ident_); 53 ident_->SetParent(this); 54 } 55 56 [[nodiscard]] const SpreadElement *RestParameter() const noexcept; 57 [[nodiscard]] SpreadElement *RestParameter() noexcept; 58 59 [[nodiscard]] const Expression *Initializer() const noexcept; 60 [[nodiscard]] Expression *Initializer() noexcept; 61 62 void SetLexerSaved(util::StringView s) noexcept; 63 [[nodiscard]] util::StringView LexerSaved() const noexcept; 64 65 [[nodiscard]] varbinder::Variable *Variable() const noexcept; 66 void SetVariable(varbinder::Variable *variable) noexcept; 67 68 [[nodiscard]] TypeNode const *TypeAnnotation() const noexcept; 69 [[nodiscard]] TypeNode *TypeAnnotation() noexcept; 70 71 void SetTypeAnnotation(TypeNode *typeNode) noexcept; 72 IsOptional()73 [[nodiscard]] bool IsOptional() const noexcept 74 { 75 return isOptional_; 76 } 77 SetOptional(bool value)78 void SetOptional(bool value) noexcept 79 { 80 isOptional_ = value; 81 ES2PANDA_ASSERT(isOptional_ || initializer_ == nullptr); 82 } 83 SetInitializer(Expression * initExpr)84 void SetInitializer(Expression *initExpr) noexcept 85 { 86 initializer_ = initExpr; 87 ES2PANDA_ASSERT(isOptional_ || initializer_ == nullptr); 88 } 89 IsRestParameter()90 [[nodiscard]] bool IsRestParameter() const noexcept 91 { 92 return spread_ != nullptr; 93 } 94 GetRequiredParams()95 [[nodiscard]] std::size_t GetRequiredParams() const noexcept 96 { 97 return extraValue_; 98 } 99 SetRequiredParams(std::size_t const value)100 void SetRequiredParams(std::size_t const value) noexcept 101 { 102 extraValue_ = value; 103 } 104 105 [[nodiscard]] ETSParameterExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override; 106 107 void Iterate(const NodeTraverser &cb) const override; 108 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 109 void Dump(ir::AstDumper *dumper) const override; 110 void Dump(ir::SrcDumper *dumper) const override; 111 void Compile(compiler::PandaGen *pg) const override; 112 void Compile(compiler::ETSGen *etsg) const override; 113 checker::Type *Check(checker::TSChecker *checker) override; 114 checker::VerifiedType Check(checker::ETSChecker *checker) override; 115 Accept(ASTVisitorT * v)116 void Accept(ASTVisitorT *v) override 117 { 118 v->Accept(this); 119 } 120 121 ETSParameterExpression *Construct(ArenaAllocator *allocator) override; 122 void CopyTo(AstNode *other) const override; 123 124 private: 125 friend class SizeOfNodeTest; 126 Identifier *ident_; 127 Expression *initializer_ = nullptr; 128 SpreadElement *spread_ = nullptr; 129 util::StringView savedLexer_ = ""; 130 std::size_t extraValue_ = 0U; 131 bool isOptional_ = false; 132 }; 133 } // namespace ark::es2panda::ir 134 135 #endif 136