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 "etsFunctionType.h"
17
18 #include "checker/TSchecker.h"
19 #include "checker/ETSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)26 void ETSFunctionType::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27 {
28 signature_.TransformChildren(cb, transformationName);
29 }
30
Iterate(const NodeTraverser & cb) const31 void ETSFunctionType::Iterate(const NodeTraverser &cb) const
32 {
33 signature_.Iterate(cb);
34 }
35
Dump(ir::AstDumper * dumper) const36 void ETSFunctionType::Dump(ir::AstDumper *dumper) const
37 {
38 const char *throwMarker = nullptr;
39 if (IsThrowing()) {
40 throwMarker = "throws";
41 } else if (IsRethrowing()) {
42 throwMarker = "rethrows";
43 }
44 dumper->Add({{"type", "ETSFunctionType"},
45 {"params", signature_.Params()},
46 {"typeParameters", AstDumper::Optional(signature_.TypeParams())},
47 {"returnType", signature_.ReturnType()},
48 {"throwMarker", AstDumper::Optional(throwMarker)}});
49 }
50
Dump(ir::SrcDumper * dumper) const51 void ETSFunctionType::Dump(ir::SrcDumper *dumper) const
52 {
53 dumper->Add("((");
54 for (auto *param : Params()) {
55 param->Dump(dumper);
56 if (param != Params().back()) {
57 dumper->Add(", ");
58 }
59 }
60 dumper->Add(")");
61
62 if (TypeParams() != nullptr) {
63 TypeParams()->Dump(dumper);
64 }
65
66 if (ReturnType() != nullptr) {
67 dumper->Add("=> ");
68 ReturnType()->Dump(dumper);
69 }
70
71 if (IsThrowing()) {
72 dumper->Add(" throws");
73 } else if (IsRethrowing()) {
74 dumper->Add(" rethrows");
75 }
76
77 dumper->Add(")");
78 }
79
Compile(compiler::PandaGen * pg) const80 void ETSFunctionType::Compile(compiler::PandaGen *pg) const
81 {
82 pg->GetAstCompiler()->Compile(this);
83 }
84
Compile(compiler::ETSGen * etsg) const85 void ETSFunctionType::Compile(compiler::ETSGen *etsg) const
86 {
87 etsg->GetAstCompiler()->Compile(this);
88 }
89
Check(checker::TSChecker * checker)90 checker::Type *ETSFunctionType::Check(checker::TSChecker *checker)
91 {
92 return checker->GetAnalyzer()->Check(this);
93 }
94
GetType(checker::TSChecker * checker)95 checker::Type *ETSFunctionType::GetType([[maybe_unused]] checker::TSChecker *checker)
96 {
97 return nullptr;
98 }
99
Check(checker::ETSChecker * checker)100 checker::Type *ETSFunctionType::Check(checker::ETSChecker *checker)
101 {
102 return checker->GetAnalyzer()->Check(this);
103 }
104
GetType(checker::ETSChecker * checker)105 checker::Type *ETSFunctionType::GetType(checker::ETSChecker *checker)
106 {
107 return Check(checker);
108 }
109
Clone(ArenaAllocator * const allocator,AstNode * const parent)110 ETSFunctionType *ETSFunctionType::Clone(ArenaAllocator *const allocator, AstNode *const parent)
111 {
112 ArenaVector<Expression *> paramsClone(allocator->Adapter());
113
114 for (auto *const param : signature_.Params()) {
115 paramsClone.emplace_back(param->Clone(allocator, nullptr)->AsExpression());
116 }
117
118 auto *const typeParamsClone =
119 signature_.TypeParams() != nullptr
120 ? signature_.TypeParams()->Clone(allocator, nullptr)->AsTSTypeParameterDeclaration()
121 : nullptr;
122 auto *const returnTypeClone =
123 signature_.ReturnType() != nullptr ? signature_.ReturnType()->Clone(allocator, nullptr)->AsTypeNode() : nullptr;
124
125 if (auto *const clone = allocator->New<ETSFunctionType>(
126 FunctionSignature(typeParamsClone, std::move(paramsClone), returnTypeClone), funcFlags_);
127 clone != nullptr) {
128 if (typeParamsClone != nullptr) {
129 typeParamsClone->SetParent(clone);
130 }
131
132 if (returnTypeClone != nullptr) {
133 returnTypeClone->SetParent(clone);
134 }
135
136 for (auto *param : clone->Params()) {
137 param->SetParent(clone);
138 }
139
140 if (parent != nullptr) {
141 clone->SetParent(parent);
142 }
143
144 // Reset scope for clone
145 // Using old scopes with clone may lead to incorrect ast-structure
146 clone->SetScope(nullptr);
147
148 return clone;
149 }
150
151 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
152 }
153 } // namespace ark::es2panda::ir
154