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 22 namespace ark::es2panda::ir { 23 class TSTypeParameterDeclaration; 24 class TypeNode; 25 26 class FunctionSignature { 27 public: 28 using FunctionParams = ArenaVector<Expression *>; 29 FunctionSignature(TSTypeParameterDeclaration *typeParams, FunctionParams &¶ms, TypeNode *returnTypeAnnotation)30 FunctionSignature(TSTypeParameterDeclaration *typeParams, FunctionParams &¶ms, TypeNode *returnTypeAnnotation) 31 : typeParams_(typeParams), params_(std::move(params)), returnTypeAnnotation_(returnTypeAnnotation) 32 { 33 } 34 Params() const35 const FunctionParams &Params() const 36 { 37 return params_; 38 } 39 Params()40 FunctionParams &Params() 41 { 42 return params_; 43 } 44 TypeParams()45 TSTypeParameterDeclaration *TypeParams() 46 { 47 return typeParams_; 48 } 49 TypeParams() const50 const TSTypeParameterDeclaration *TypeParams() const 51 { 52 return typeParams_; 53 } 54 ReturnType()55 TypeNode *ReturnType() 56 { 57 return returnTypeAnnotation_; 58 } 59 SetReturnType(TypeNode *type)60 void SetReturnType(TypeNode *type) 61 { 62 returnTypeAnnotation_ = type; 63 } 64 ReturnType() const65 const TypeNode *ReturnType() const 66 { 67 return returnTypeAnnotation_; 68 } 69 DefaultParamIndex() const70 [[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 86 private: 87 TSTypeParameterDeclaration *typeParams_; 88 ArenaVector<Expression *> params_; 89 TypeNode *returnTypeAnnotation_; 90 }; 91 92 } // namespace ark::es2panda::ir 93 94 #endif 95