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 "tsEnumDeclaration.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "util/helpers.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 TSEnumDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27 {
28 for (auto *&it : decorators_) {
29 if (auto *transformedNode = cb(it); it != transformedNode) {
30 it->SetTransformedNode(transformationName, transformedNode);
31 it = transformedNode->AsDecorator();
32 }
33 }
34
35 if (auto *transformedNode = cb(key_); key_ != transformedNode) {
36 key_->SetTransformedNode(transformationName, transformedNode);
37 key_ = transformedNode->AsIdentifier();
38 }
39
40 for (auto *&it : members_) {
41 if (auto *transformedNode = cb(it); it != transformedNode) {
42 it->SetTransformedNode(transformationName, transformedNode);
43 it = transformedNode;
44 }
45 }
46 }
47
Iterate(const NodeTraverser &cb) const48 void TSEnumDeclaration::Iterate(const NodeTraverser &cb) const
49 {
50 for (auto *it : decorators_) {
51 cb(it);
52 }
53
54 cb(key_);
55
56 for (auto *it : members_) {
57 cb(it);
58 }
59 }
60
Dump(ir::AstDumper *dumper) const61 void TSEnumDeclaration::Dump(ir::AstDumper *dumper) const
62 {
63 dumper->Add({{"type", "TSEnumDeclaration"},
64 {"decorators", AstDumper::Optional(decorators_)},
65 {"id", key_},
66 {"members", members_},
67 {"const", isConst_}});
68 }
69
Dump(ir::SrcDumper *dumper) const70 void TSEnumDeclaration::Dump(ir::SrcDumper *dumper) const
71 {
72 ASSERT(isConst_ == false);
73 ASSERT(key_ != nullptr);
74 dumper->Add("enum ");
75 key_->Dump(dumper);
76 dumper->Add(" {");
77 if (!members_.empty()) {
78 dumper->IncrIndent();
79 dumper->Endl();
80 for (auto member : members_) {
81 member->Dump(dumper);
82 if (member != members_.back()) {
83 dumper->Add(",");
84 dumper->Endl();
85 }
86 }
87 dumper->DecrIndent();
88 dumper->Endl();
89 }
90 dumper->Add("}");
91 dumper->Endl();
92 }
93
94 // NOTE (csabahurton): this method has not been moved to TSAnalyizer.cpp, because it is not used.
EvaluateMemberExpression(checker::TSChecker *checker, [[maybe_unused]] varbinder::EnumVariable *enumVar, ir::MemberExpression *expr)95 varbinder::EnumMemberResult EvaluateMemberExpression(checker::TSChecker *checker,
96 [[maybe_unused]] varbinder::EnumVariable *enumVar,
97 ir::MemberExpression *expr)
98 {
99 if (checker::TSChecker::IsConstantMemberAccess(expr->AsExpression())) {
100 if (expr->Check(checker)->TypeFlags() == checker::TypeFlag::ENUM) {
101 util::StringView name;
102 if (!expr->IsComputed()) {
103 name = expr->Property()->AsIdentifier()->Name();
104 } else {
105 ASSERT(checker::TSChecker::IsStringLike(expr->Property()));
106 name = reinterpret_cast<const ir::StringLiteral *>(expr->Property())->Str();
107 }
108
109 // NOTE: aszilagyi.
110 }
111 }
112
113 return false;
114 }
115
Compile(compiler::PandaGen *pg) const116 void TSEnumDeclaration::Compile(compiler::PandaGen *pg) const
117 {
118 pg->GetAstCompiler()->Compile(this);
119 }
120
Compile(compiler::ETSGen *etsg) const121 void TSEnumDeclaration::Compile(compiler::ETSGen *etsg) const
122 {
123 etsg->GetAstCompiler()->Compile(this);
124 }
125
Check(checker::TSChecker *checker)126 checker::Type *TSEnumDeclaration::Check(checker::TSChecker *checker)
127 {
128 return checker->GetAnalyzer()->Check(this);
129 }
130
Check(checker::ETSChecker *const checker)131 checker::Type *TSEnumDeclaration::Check(checker::ETSChecker *const checker)
132 {
133 return checker->GetAnalyzer()->Check(this);
134 }
135 } // namespace ark::es2panda::ir
136