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 "tsIndexSignature.h"
17 #include "ir/astDump.h"
18 #include "ir/srcDump.h"
19 #include "checker/TSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22
23 namespace ark::es2panda::ir {
24 TSIndexSignature::TSIndexSignatureKind TSIndexSignature::Kind() const noexcept
25 {
26 return param_->AsIdentifier()->TypeAnnotation()->IsTSNumberKeyword() ? TSIndexSignatureKind::NUMBER
27 : TSIndexSignatureKind::STRING;
28 }
29
TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)30 void TSIndexSignature::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
31 {
32 if (auto *transformedNode = cb(param_); param_ != transformedNode) {
33 param_->SetTransformedNode(transformationName, transformedNode);
34 param_ = transformedNode->AsExpression();
35 }
36
37 if (auto *transformedNode = cb(typeAnnotation_); typeAnnotation_ != transformedNode) {
38 typeAnnotation_->SetTransformedNode(transformationName, transformedNode);
39 typeAnnotation_ = static_cast<TypeNode *>(transformedNode);
40 }
41 }
42
Iterate(const NodeTraverser &cb) const43 void TSIndexSignature::Iterate(const NodeTraverser &cb) const
44 {
45 cb(param_);
46 cb(typeAnnotation_);
47 }
48
Dump(ir::AstDumper *dumper) const49 void TSIndexSignature::Dump(ir::AstDumper *dumper) const
50 {
51 dumper->Add({{"type", "TSIndexSignature"},
52 {"parameters", param_},
53 {"typeAnnotation", typeAnnotation_},
54 {"readonly", readonly_}});
55 }
56
Dump(ir::SrcDumper *dumper) const57 void TSIndexSignature::Dump(ir::SrcDumper *dumper) const
58 {
59 dumper->Add("TSIndexSignature");
60 }
61
Compile(compiler::PandaGen *pg) const62 void TSIndexSignature::Compile(compiler::PandaGen *pg) const
63 {
64 pg->GetAstCompiler()->Compile(this);
65 }
66
Compile(compiler::ETSGen *etsg) const67 void TSIndexSignature::Compile(compiler::ETSGen *etsg) const
68 {
69 etsg->GetAstCompiler()->Compile(this);
70 }
71
Check(checker::TSChecker *checker)72 checker::Type *TSIndexSignature::Check(checker::TSChecker *checker)
73 {
74 return checker->GetAnalyzer()->Check(this);
75 }
76
Check(checker::ETSChecker *checker)77 checker::Type *TSIndexSignature::Check(checker::ETSChecker *checker)
78 {
79 return checker->GetAnalyzer()->Check(this);
80 }
81
Clone(ArenaAllocator *const allocator, AstNode *const parent)82 TSIndexSignature *TSIndexSignature::Clone(ArenaAllocator *const allocator, AstNode *const parent)
83 {
84 auto *const param = param_ != nullptr ? param_->Clone(allocator, nullptr)->AsExpression() : nullptr;
85 auto *const typeAnnotation = typeAnnotation_->Clone(allocator, nullptr);
86
87 if (auto *const clone = allocator->New<TSIndexSignature>(param, typeAnnotation, readonly_); clone != nullptr) {
88 if (parent != nullptr) {
89 clone->SetParent(parent);
90 }
91 if (param != nullptr) {
92 param->SetParent(clone);
93 }
94 typeAnnotation->SetParent(clone);
95 return clone;
96 }
97
98 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
99 }
100 } // namespace ark::es2panda::ir
101