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 dumper->Add({{"type", "ETSFunctionType"},
39 {"params", signature_.Params()},
40 {"typeParameters", AstDumper::Optional(signature_.TypeParams())},
41 {"returnType", signature_.ReturnType()}});
42
43 if (IsThrowing()) {
44 dumper->Add({"throwMarker", "throws"});
45 }
46 }
47
Dump(ir::SrcDumper * dumper) const48 void ETSFunctionType::Dump(ir::SrcDumper *dumper) const
49 {
50 dumper->Add("(");
51 for (auto *param : Params()) {
52 param->Dump(dumper);
53 if (param != Params().back()) {
54 dumper->Add(", ");
55 }
56 }
57 dumper->Add(")");
58
59 if (TypeParams() != nullptr) {
60 TypeParams()->Dump(dumper);
61 }
62
63 if (ReturnType() != nullptr) {
64 dumper->Add("=> ");
65 ReturnType()->Dump(dumper);
66 }
67 }
68
Compile(compiler::PandaGen * pg) const69 void ETSFunctionType::Compile(compiler::PandaGen *pg) const
70 {
71 pg->GetAstCompiler()->Compile(this);
72 }
73
Compile(compiler::ETSGen * etsg) const74 void ETSFunctionType::Compile(compiler::ETSGen *etsg) const
75 {
76 etsg->GetAstCompiler()->Compile(this);
77 }
78
Check(checker::TSChecker * checker)79 checker::Type *ETSFunctionType::Check(checker::TSChecker *checker)
80 {
81 return checker->GetAnalyzer()->Check(this);
82 }
83
GetType(checker::TSChecker * checker)84 checker::Type *ETSFunctionType::GetType([[maybe_unused]] checker::TSChecker *checker)
85 {
86 return nullptr;
87 }
88
Check(checker::ETSChecker * checker)89 checker::Type *ETSFunctionType::Check(checker::ETSChecker *checker)
90 {
91 return checker->GetAnalyzer()->Check(this);
92 }
93
GetType(checker::ETSChecker * checker)94 checker::Type *ETSFunctionType::GetType(checker::ETSChecker *checker)
95 {
96 return Check(checker);
97 }
98
Clone(ArenaAllocator * const allocator,AstNode * const parent)99 ETSFunctionType *ETSFunctionType::Clone(ArenaAllocator *const allocator, AstNode *const parent)
100 {
101 ArenaVector<Expression *> paramsClone(allocator->Adapter());
102
103 for (auto *const param : signature_.Params()) {
104 paramsClone.emplace_back(param->Clone(allocator, nullptr)->AsExpression());
105 }
106
107 auto *const typeParamsClone =
108 signature_.TypeParams() != nullptr
109 ? signature_.TypeParams()->Clone(allocator, nullptr)->AsTSTypeParameterDeclaration()
110 : nullptr;
111 auto *const returnTypeClone =
112 signature_.ReturnType() != nullptr ? signature_.ReturnType()->Clone(allocator, nullptr)->AsTypeNode() : nullptr;
113
114 if (auto *const clone = allocator->New<ETSFunctionType>(
115 FunctionSignature(typeParamsClone, std::move(paramsClone), returnTypeClone), funcFlags_);
116 clone != nullptr) {
117 if (typeParamsClone != nullptr) {
118 typeParamsClone->SetParent(clone);
119 }
120
121 if (returnTypeClone != nullptr) {
122 returnTypeClone->SetParent(clone);
123 }
124
125 for (auto *param : clone->Params()) {
126 param->SetParent(clone);
127 }
128
129 if (parent != nullptr) {
130 clone->SetParent(parent);
131 }
132
133 clone->SetScope(scope_);
134
135 return clone;
136 }
137
138 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
139 }
140 } // namespace ark::es2panda::ir
141