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 #include "ir/base/scriptFunctionSignature.h"
17 #include "ir/ts/tsTypeParameterDeclaration.h"
18 #include "ir/typeNode.h"
19
20 namespace ark::es2panda::ir {
21
Iterate(const NodeTraverser &cb) const22 void FunctionSignature::Iterate(const NodeTraverser &cb) const
23 {
24 if (typeParams_ != nullptr) {
25 cb(typeParams_);
26 }
27
28 for (auto *it : Params()) {
29 cb(it);
30 }
31
32 if (returnTypeAnnotation_ != nullptr) {
33 cb(returnTypeAnnotation_);
34 }
35 }
36
TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)37 void FunctionSignature::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
38 {
39 if (typeParams_ != nullptr) {
40 if (auto *transformedNode = cb(typeParams_); typeParams_ != transformedNode) {
41 typeParams_->SetTransformedNode(transformationName, transformedNode);
42 typeParams_ = transformedNode->AsTSTypeParameterDeclaration();
43 }
44 }
45
46 for (auto *&it : params_) {
47 if (auto *transformedNode = cb(it); it != transformedNode) {
48 it->SetTransformedNode(transformationName, transformedNode);
49 it = transformedNode->AsExpression();
50 }
51 }
52
53 if (returnTypeAnnotation_ != nullptr) {
54 if (auto *transformedNode = cb(returnTypeAnnotation_); returnTypeAnnotation_ != transformedNode) {
55 returnTypeAnnotation_->SetTransformedNode(transformationName, transformedNode);
56 returnTypeAnnotation_ = static_cast<TypeNode *>(transformedNode);
57 }
58 }
59 }
60
Clone(ArenaAllocator *allocator)61 FunctionSignature FunctionSignature::Clone(ArenaAllocator *allocator)
62 {
63 ArenaVector<Expression *> clonedParams(allocator->Adapter());
64
65 for (auto *const param : params_) {
66 clonedParams.emplace_back(param->Clone(allocator, param->Parent())->AsExpression());
67 }
68
69 auto *const typeParamClone =
70 typeParams_ != nullptr ? typeParams_->Clone(allocator, nullptr)->AsTSTypeParameterDeclaration() : nullptr;
71 auto *const returnTypeAnnotationClone =
72 returnTypeAnnotation_ != nullptr ? returnTypeAnnotation_->Clone(allocator, nullptr) : nullptr;
73
74 return {typeParamClone, std::move(clonedParams), returnTypeAnnotationClone};
75 }
76 } // namespace ark::es2panda::ir
77