1 /**
2 * Copyright (c) 2021-2025 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 "tsEnumDeclaration.h"
17 #include <cstddef>
18
19 #include "checker/TSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "util/helpers.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25 #include "utils/arena_containers.h"
26
27 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)28 void TSEnumDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
29 {
30 for (auto *&it : VectorIterationGuard(decorators_)) {
31 if (auto *transformedNode = cb(it); it != transformedNode) {
32 it->SetTransformedNode(transformationName, transformedNode);
33 it = transformedNode->AsDecorator();
34 }
35 }
36
37 if (auto *transformedNode = cb(key_); key_ != transformedNode) {
38 key_->SetTransformedNode(transformationName, transformedNode);
39 key_ = transformedNode->AsIdentifier();
40 }
41
42 for (auto *&it : VectorIterationGuard(members_)) {
43 if (auto *transformedNode = cb(it); it != transformedNode) {
44 it->SetTransformedNode(transformationName, transformedNode);
45 it = transformedNode;
46 }
47 }
48 }
49
Iterate(const NodeTraverser & cb) const50 void TSEnumDeclaration::Iterate(const NodeTraverser &cb) const
51 {
52 for (auto *it : VectorIterationGuard(decorators_)) {
53 cb(it);
54 }
55
56 cb(key_);
57
58 for (auto *it : VectorIterationGuard(members_)) {
59 cb(it);
60 }
61 }
62
Dump(ir::AstDumper * dumper) const63 void TSEnumDeclaration::Dump(ir::AstDumper *dumper) const
64 {
65 dumper->Add({{"type", "TSEnumDeclaration"},
66 {"decorators", AstDumper::Optional(decorators_)},
67 {"id", key_},
68 {"members", members_},
69 {"const", isConst_},
70 {"declare", IsDeclare()}});
71 }
72
RegisterUnexportedForDeclGen(ir::SrcDumper * dumper) const73 bool TSEnumDeclaration::RegisterUnexportedForDeclGen(ir::SrcDumper *dumper) const
74 {
75 if (!dumper->IsDeclgen()) {
76 return false;
77 }
78
79 if (dumper->IsIndirectDepPhase()) {
80 return false;
81 }
82
83 if (key_->Parent()->IsDefaultExported() || key_->Parent()->IsExported()) {
84 return false;
85 }
86
87 auto name = key_->AsIdentifier()->Name().Mutf8();
88 dumper->AddNode(name, this);
89 return true;
90 }
91
Dump(ir::SrcDumper * dumper) const92 void TSEnumDeclaration::Dump(ir::SrcDumper *dumper) const
93 {
94 ES2PANDA_ASSERT(isConst_ == false);
95 ES2PANDA_ASSERT(key_ != nullptr);
96 if (RegisterUnexportedForDeclGen(dumper)) {
97 return;
98 }
99 if (key_->Parent()->IsExported() && dumper->IsDeclgen()) {
100 dumper->Add("export ");
101 } else if (key_->Parent()->IsDefaultExported() && dumper->IsDeclgen()) {
102 dumper->Add("export default ");
103 }
104 if (IsDeclare() || dumper->IsDeclgen()) {
105 dumper->Add("declare ");
106 }
107 dumper->Add("enum ");
108 key_->Dump(dumper);
109 dumper->Add(" {");
110 if (!members_.empty()) {
111 dumper->IncrIndent();
112 dumper->Endl();
113 for (auto member : members_) {
114 member->Dump(dumper);
115 if (member != members_.back()) {
116 dumper->Add(",");
117 dumper->Endl();
118 }
119 }
120 dumper->DecrIndent();
121 dumper->Endl();
122 }
123 dumper->Add("}");
124 dumper->Endl();
125 }
126
127 // NOTE (csabahurton): this method has not been moved to TSAnalyizer.cpp, because it is not used.
EvaluateMemberExpression(checker::TSChecker * checker,varbinder::EnumVariable * enumVar,ir::MemberExpression * expr)128 varbinder::EnumMemberResult EvaluateMemberExpression(checker::TSChecker *checker,
129 [[maybe_unused]] varbinder::EnumVariable *enumVar,
130 ir::MemberExpression *expr)
131 {
132 if (checker::TSChecker::IsConstantMemberAccess(expr->AsExpression())) {
133 if (expr->Check(checker)->TypeFlags() == checker::TypeFlag::ENUM) {
134 util::StringView name;
135 if (!expr->IsComputed()) {
136 name = expr->Property()->AsIdentifier()->Name();
137 } else {
138 ES2PANDA_ASSERT(checker::TSChecker::IsStringLike(expr->Property()));
139 name = reinterpret_cast<const ir::StringLiteral *>(expr->Property())->Str();
140 }
141
142 // NOTE: aszilagyi.
143 }
144 }
145
146 return false;
147 }
148
Compile(compiler::PandaGen * pg) const149 void TSEnumDeclaration::Compile(compiler::PandaGen *pg) const
150 {
151 pg->GetAstCompiler()->Compile(this);
152 }
153
Compile(compiler::ETSGen * etsg) const154 void TSEnumDeclaration::Compile(compiler::ETSGen *etsg) const
155 {
156 etsg->GetAstCompiler()->Compile(this);
157 }
158
Check(checker::TSChecker * checker)159 checker::Type *TSEnumDeclaration::Check(checker::TSChecker *checker)
160 {
161 return checker->GetAnalyzer()->Check(this);
162 }
163
Check(checker::ETSChecker * const checker)164 checker::VerifiedType TSEnumDeclaration::Check(checker::ETSChecker *const checker)
165 {
166 return {this, checker->GetAnalyzer()->Check(this)};
167 }
168
Construct(ArenaAllocator * allocator)169 TSEnumDeclaration *TSEnumDeclaration::Construct(ArenaAllocator *allocator)
170 {
171 ArenaVector<AstNode *> members(allocator->Adapter());
172 return allocator->New<TSEnumDeclaration>(allocator, nullptr, std::move(members),
173 ConstructorFlags {false, false, false});
174 }
175
CopyTo(AstNode * other) const176 void TSEnumDeclaration::CopyTo(AstNode *other) const
177 {
178 auto otherImpl = other->AsTSEnumDeclaration();
179
180 otherImpl->scope_ = scope_;
181 otherImpl->decorators_ = decorators_;
182 otherImpl->key_ = key_;
183 otherImpl->members_ = members_;
184 otherImpl->internalName_ = internalName_;
185 otherImpl->boxedClass_ = boxedClass_;
186 otherImpl->isConst_ = isConst_;
187
188 TypedStatement::CopyTo(other);
189 }
190
191 } // namespace ark::es2panda::ir
192