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_ARROW_FUNCTION_EXPRESSION_H 17 #define ES2PANDA_IR_EXPRESSION_ARROW_FUNCTION_EXPRESSION_H 18 19 #include "ir/expression.h" 20 21 namespace panda::es2panda::compiler { 22 class ETSCompiler; 23 } // namespace panda::es2panda::compiler 24 25 namespace panda::es2panda::ir { 26 class ScriptFunction; 27 28 class ArrowFunctionExpression : public Expression { 29 public: 30 ArrowFunctionExpression() = delete; 31 ~ArrowFunctionExpression() override = default; 32 33 NO_COPY_SEMANTIC(ArrowFunctionExpression); 34 NO_MOVE_SEMANTIC(ArrowFunctionExpression); 35 ArrowFunctionExpression(ArenaAllocator * const allocator,ScriptFunction * const func)36 explicit ArrowFunctionExpression(ArenaAllocator *const allocator, ScriptFunction *const func) 37 : Expression(AstNodeType::ARROW_FUNCTION_EXPRESSION), func_(func), capturedVars_(allocator->Adapter()) 38 { 39 } 40 41 explicit ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *allocator); 42 43 // NOTE (csabahurton): friend relationship can be removed once there are getters for private fields 44 friend class compiler::ETSCompiler; 45 Function()46 [[nodiscard]] const ScriptFunction *Function() const noexcept 47 { 48 return func_; 49 } 50 Function()51 [[nodiscard]] ScriptFunction *Function() noexcept 52 { 53 return func_; 54 } 55 ResolvedLambda()56 [[nodiscard]] const ClassDefinition *ResolvedLambda() const noexcept 57 { 58 return resolvedLambda_; 59 } 60 ResolvedLambda()61 [[nodiscard]] ClassDefinition *ResolvedLambda() noexcept 62 { 63 return resolvedLambda_; 64 } 65 CapturedVars()66 [[nodiscard]] ArenaVector<varbinder::Variable *> &CapturedVars() noexcept 67 { 68 return capturedVars_; 69 } 70 CapturedVars()71 [[nodiscard]] const ArenaVector<varbinder::Variable *> &CapturedVars() const noexcept 72 { 73 return capturedVars_; 74 } 75 SetResolvedLambda(ClassDefinition * const lambda)76 void SetResolvedLambda(ClassDefinition *const lambda) noexcept 77 { 78 resolvedLambda_ = lambda; 79 } 80 SetPropagateThis()81 void SetPropagateThis() noexcept 82 { 83 propagateThis_ = true; 84 } 85 86 // NOLINTNEXTLINE(google-default-arguments) 87 [[nodiscard]] ArrowFunctionExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; 88 89 void TransformChildren(const NodeTransformer &cb) override; 90 void Iterate(const NodeTraverser &cb) const override; 91 void Dump(ir::AstDumper *dumper) const override; 92 void Dump(ir::SrcDumper *dumper) const override; 93 void Compile(compiler::PandaGen *pg) const override; 94 void Compile(compiler::ETSGen *etsg) const override; 95 checker::Type *Check(checker::TSChecker *checker) override; 96 checker::Type *Check(checker::ETSChecker *checker) override; 97 ir::TypeNode *CreateTypeAnnotation(checker::ETSChecker *checker); 98 ir::TypeNode *CreateReturnNodeFromType(checker::ETSChecker *checker, checker::Type *returnType); 99 Accept(ASTVisitorT * v)100 void Accept(ASTVisitorT *v) override 101 { 102 v->Accept(this); 103 } 104 105 private: 106 ScriptFunction *func_; 107 ArenaVector<varbinder::Variable *> capturedVars_; 108 ir::ClassDefinition *resolvedLambda_ {nullptr}; 109 bool propagateThis_ {false}; 110 }; 111 } // namespace panda::es2panda::ir 112 113 #endif 114