• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                  {"declare", IsDeclare()}});
69 }
70 
Dump(ir::SrcDumper * dumper) const71 void TSEnumDeclaration::Dump(ir::SrcDumper *dumper) const
72 {
73     ASSERT(isConst_ == false);
74     ASSERT(key_ != nullptr);
75     if (IsDeclare()) {
76         dumper->Add("declare ");
77     }
78     dumper->Add("enum ");
79     key_->Dump(dumper);
80     dumper->Add(" {");
81     if (!members_.empty()) {
82         dumper->IncrIndent();
83         dumper->Endl();
84         for (auto member : members_) {
85             member->Dump(dumper);
86             if (member != members_.back()) {
87                 dumper->Add(",");
88                 dumper->Endl();
89             }
90         }
91         dumper->DecrIndent();
92         dumper->Endl();
93     }
94     dumper->Add("}");
95     dumper->Endl();
96 }
97 
98 // 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)99 varbinder::EnumMemberResult EvaluateMemberExpression(checker::TSChecker *checker,
100                                                      [[maybe_unused]] varbinder::EnumVariable *enumVar,
101                                                      ir::MemberExpression *expr)
102 {
103     if (checker::TSChecker::IsConstantMemberAccess(expr->AsExpression())) {
104         if (expr->Check(checker)->TypeFlags() == checker::TypeFlag::ENUM) {
105             util::StringView name;
106             if (!expr->IsComputed()) {
107                 name = expr->Property()->AsIdentifier()->Name();
108             } else {
109                 ASSERT(checker::TSChecker::IsStringLike(expr->Property()));
110                 name = reinterpret_cast<const ir::StringLiteral *>(expr->Property())->Str();
111             }
112 
113             // NOTE: aszilagyi.
114         }
115     }
116 
117     return false;
118 }
119 
Compile(compiler::PandaGen * pg) const120 void TSEnumDeclaration::Compile(compiler::PandaGen *pg) const
121 {
122     pg->GetAstCompiler()->Compile(this);
123 }
124 
Compile(compiler::ETSGen * etsg) const125 void TSEnumDeclaration::Compile(compiler::ETSGen *etsg) const
126 {
127     etsg->GetAstCompiler()->Compile(this);
128 }
129 
Check(checker::TSChecker * checker)130 checker::Type *TSEnumDeclaration::Check(checker::TSChecker *checker)
131 {
132     return checker->GetAnalyzer()->Check(this);
133 }
134 
Check(checker::ETSChecker * const checker)135 checker::Type *TSEnumDeclaration::Check(checker::ETSChecker *const checker)
136 {
137     return checker->GetAnalyzer()->Check(this);
138 }
139 }  // namespace ark::es2panda::ir
140