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_STATEMENT_FUNCTION_DECLARATION_H 17 #define ES2PANDA_IR_STATEMENT_FUNCTION_DECLARATION_H 18 19 #include "ir/statement.h" 20 #include "ir/statements/annotationUsage.h" 21 22 namespace ark::es2panda::ir { 23 class ScriptFunction; 24 class AnnotationUsage; 25 26 class FunctionDeclaration : public Statement { 27 public: 28 explicit FunctionDeclaration(ArenaAllocator *allocator, ScriptFunction *func, 29 ArenaVector<AnnotationUsage *> &&annotations, bool isAnonymous = false) Statement(AstNodeType::FUNCTION_DECLARATION)30 : Statement(AstNodeType::FUNCTION_DECLARATION), 31 decorators_(allocator->Adapter()), 32 func_(func), 33 isAnonymous_(isAnonymous), 34 annotations_(std::move(annotations)) 35 { 36 } 37 38 explicit FunctionDeclaration(ArenaAllocator *allocator, ScriptFunction *func, bool isAnonymous = false) Statement(AstNodeType::FUNCTION_DECLARATION)39 : Statement(AstNodeType::FUNCTION_DECLARATION), 40 decorators_(allocator->Adapter()), 41 func_(func), 42 isAnonymous_(isAnonymous), 43 annotations_(allocator->Adapter()) 44 { 45 } 46 Function()47 ScriptFunction *Function() 48 { 49 return func_; 50 } 51 IsAnonymous()52 bool IsAnonymous() const 53 { 54 return isAnonymous_; 55 } 56 Function()57 const ScriptFunction *Function() const 58 { 59 return func_; 60 } 61 AddDecorators(ArenaVector<ir::Decorator * > && decorators)62 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 63 { 64 decorators_ = std::move(decorators); 65 } 66 CanHaveDecorator(bool inTs)67 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 68 { 69 return !inTs; 70 } 71 Annotations()72 [[nodiscard]] ArenaVector<ir::AnnotationUsage *> &Annotations() noexcept 73 { 74 return annotations_; 75 } 76 Annotations()77 [[nodiscard]] const ArenaVector<ir::AnnotationUsage *> &Annotations() const noexcept 78 { 79 return annotations_; 80 } 81 SetAnnotations(ArenaVector<ir::AnnotationUsage * > && annotations)82 void SetAnnotations(ArenaVector<ir::AnnotationUsage *> &&annotations) 83 { 84 annotations_ = std::move(annotations); 85 for (auto anno : annotations_) { 86 anno->SetParent(this); 87 } 88 } 89 AddAnnotations(AnnotationUsage * const annotations)90 void AddAnnotations(AnnotationUsage *const annotations) 91 { 92 annotations_.emplace_back(annotations); 93 } 94 95 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 96 void Iterate(const NodeTraverser &cb) const override; 97 void Dump(ir::AstDumper *dumper) const override; 98 void Dump(ir::SrcDumper *dumper) const override; 99 void Compile(compiler::PandaGen *pg) const override; 100 void Compile(compiler::ETSGen *etsg) const override; 101 checker::Type *Check(checker::TSChecker *checker) override; 102 checker::Type *Check(checker::ETSChecker *checker) override; 103 Accept(ASTVisitorT * v)104 void Accept(ASTVisitorT *v) override 105 { 106 v->Accept(this); 107 } 108 109 private: 110 ArenaVector<Decorator *> decorators_; 111 ScriptFunction *func_; 112 const bool isAnonymous_; 113 ArenaVector<AnnotationUsage *> annotations_; 114 }; 115 } // namespace ark::es2panda::ir 116 117 #endif 118