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_ARROW_FUNCTION_EXPRESSION_H 17 #define ES2PANDA_IR_EXPRESSION_ARROW_FUNCTION_EXPRESSION_H 18 19 #include "ir/expression.h" 20 #include "ir/annotationAllowed.h" 21 #include "ir/jsDocAllowed.h" 22 23 namespace ark::es2panda::compiler { 24 class ETSCompiler; 25 } // namespace ark::es2panda::compiler 26 27 namespace ark::es2panda::ir { 28 class ScriptFunction; 29 30 class ArrowFunctionExpression : public JsDocAllowed<AnnotationAllowed<Expression>> { 31 public: 32 ArrowFunctionExpression() = delete; 33 ~ArrowFunctionExpression() override = default; 34 35 NO_COPY_SEMANTIC(ArrowFunctionExpression); 36 NO_MOVE_SEMANTIC(ArrowFunctionExpression); 37 ArrowFunctionExpression(ScriptFunction * const func,ArenaAllocator * const allocator)38 explicit ArrowFunctionExpression(ScriptFunction *const func, ArenaAllocator *const allocator) 39 : JsDocAllowed<AnnotationAllowed<Expression>>(AstNodeType::ARROW_FUNCTION_EXPRESSION, allocator), func_(func) 40 { 41 } 42 43 explicit ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *const allocator); 44 Function()45 [[nodiscard]] const ScriptFunction *Function() const noexcept 46 { 47 return func_; 48 } 49 Function()50 [[nodiscard]] ScriptFunction *Function() noexcept 51 { 52 return func_; 53 } 54 SetPreferredType(checker::Type * preferredType)55 void SetPreferredType(checker::Type *preferredType) noexcept 56 { 57 preferredType_ = preferredType; 58 } 59 GetPreferredType()60 [[nodiscard]] checker::Type *GetPreferredType() noexcept 61 { 62 return preferredType_; 63 } 64 65 [[nodiscard]] ArrowFunctionExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override; 66 67 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 68 void Iterate(const NodeTraverser &cb) const override; 69 void Dump(ir::AstDumper *dumper) const override; 70 void Dump(ir::SrcDumper *dumper) const override; 71 void Compile(compiler::PandaGen *pg) const override; 72 void Compile(compiler::ETSGen *etsg) const override; 73 checker::Type *Check(checker::TSChecker *checker) override; 74 checker::VerifiedType Check(checker::ETSChecker *checker) override; 75 ir::TypeNode *CreateTypeAnnotation(checker::ETSChecker *checker); 76 ir::TypeNode *CreateReturnNodeFromType(checker::ETSChecker *checker, checker::Type *returnType); 77 bool IsVarFromSubscope(const varbinder::Variable *var) const; 78 Accept(ASTVisitorT * v)79 void Accept(ASTVisitorT *v) override 80 { 81 v->Accept(this); 82 } 83 84 private: 85 ScriptFunction *func_; 86 checker::Type *preferredType_ {}; 87 }; 88 } // namespace ark::es2panda::ir 89 90 #endif 91