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_COMPILER_CORE_SCRIPT_FUNCTION_SIGNATURE_H 17#define ES2PANDA_COMPILER_CORE_SCRIPT_FUNCTION_SIGNATURE_H 18 19#include "ir/astNode.h" 20#include "ir/ets/etsParameterExpression.h" 21 22namespace ark::es2panda::ir { 23class TSTypeParameterDeclaration; 24class TypeNode; 25 26class FunctionSignature { 27public: 28 using FunctionParams = ArenaVector<Expression *>; 29 30 FunctionSignature(TSTypeParameterDeclaration *typeParams, FunctionParams &¶ms, TypeNode *returnTypeAnnotation) 31 : typeParams_(typeParams), params_(std::move(params)), returnTypeAnnotation_(returnTypeAnnotation) 32 { 33 } 34 35 const FunctionParams &Params() const 36 { 37 return params_; 38 } 39 40 FunctionParams &Params() 41 { 42 return params_; 43 } 44 45 TSTypeParameterDeclaration *TypeParams() 46 { 47 return typeParams_; 48 } 49 50 const TSTypeParameterDeclaration *TypeParams() const 51 { 52 return typeParams_; 53 } 54 55 TypeNode *ReturnType() 56 { 57 return returnTypeAnnotation_; 58 } 59 60 void SetReturnType(TypeNode *type) 61 { 62 returnTypeAnnotation_ = type; 63 } 64 65 const TypeNode *ReturnType() const 66 { 67 return returnTypeAnnotation_; 68 } 69 70 [[nodiscard]] size_t DefaultParamIndex() const 71 { 72 for (size_t i = 0; i < params_.size(); i++) { 73 if (params_[i]->AsETSParameterExpression()->IsDefault()) { 74 return i; 75 } 76 } 77 return params_.size(); 78 } 79 80 void Iterate(const NodeTraverser &cb) const; 81 82 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName); 83 84 [[nodiscard]] FunctionSignature Clone(ArenaAllocator *allocator); 85 86private: 87 TSTypeParameterDeclaration *typeParams_; 88 ArenaVector<Expression *> params_; 89 TypeNode *returnTypeAnnotation_; 90}; 91 92} // namespace ark::es2panda::ir 93 94#endif 95