• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "variableDeclarator.h"
17 
18 #include "compiler/core/pandagen.h"
19 #include "compiler/core/ETSGen.h"
20 #include "checker/TSchecker.h"
21 #include "checker/ETSchecker.h"
22 
23 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)24 void VariableDeclarator::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
25 {
26     if (auto *transformedNode = cb(id_); id_ != transformedNode) {
27         id_->SetTransformedNode(transformationName, transformedNode);
28         id_ = transformedNode->AsExpression();
29     }
30 
31     if (init_ != nullptr) {
32         if (auto *transformedNode = cb(init_); init_ != transformedNode) {
33             init_->SetTransformedNode(transformationName, transformedNode);
34             init_ = transformedNode->AsExpression();
35         }
36     }
37 }
38 
Iterate(const NodeTraverser & cb) const39 void VariableDeclarator::Iterate(const NodeTraverser &cb) const
40 {
41     cb(id_);
42 
43     if (init_ != nullptr) {
44         cb(init_);
45     }
46 }
47 
Dump(ir::AstDumper * dumper) const48 void VariableDeclarator::Dump(ir::AstDumper *dumper) const
49 {
50     dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullish(init_)}});
51 }
52 
Dump(ir::SrcDumper * dumper) const53 void VariableDeclarator::Dump(ir::SrcDumper *dumper) const
54 {
55     if (id_ != nullptr) {
56         id_->Dump(dumper);
57         if (id_->IsOptionalDeclaration()) {
58             dumper->Add("?");
59         }
60         if (id_->IsAnnotatedExpression()) {
61             auto *type = id_->AsAnnotatedExpression()->TypeAnnotation();
62             if (type != nullptr) {
63                 dumper->Add(": ");
64                 type->Dump(dumper);
65             }
66         }
67     }
68     if (init_ != nullptr) {
69         dumper->Add(" = ");
70         init_->Dump(dumper);
71     }
72 }
73 
Clone(ArenaAllocator * const allocator,AstNode * const parent)74 VariableDeclarator *VariableDeclarator::Clone(ArenaAllocator *const allocator, AstNode *const parent)
75 {
76     auto *const id = id_ != nullptr ? id_->Clone(allocator, nullptr)->AsExpression() : nullptr;
77     auto *const init = init_ != nullptr ? init_->Clone(allocator, nullptr)->AsExpression() : nullptr;
78     auto *const clone = allocator->New<VariableDeclarator>(flag_, id, init);
79     ES2PANDA_ASSERT(clone != nullptr);
80 
81     if (id != nullptr) {
82         id->SetParent(clone);
83     }
84 
85     if (init != nullptr) {
86         init->SetParent(clone);
87     }
88 
89     if (parent != nullptr) {
90         clone->SetParent(parent);
91     }
92 
93     clone->SetRange(range_);
94     return clone;
95 }
96 
Compile(compiler::PandaGen * pg) const97 void VariableDeclarator::Compile([[maybe_unused]] compiler::PandaGen *pg) const
98 {
99     pg->GetAstCompiler()->Compile(this);
100 }
101 
Compile(compiler::ETSGen * etsg) const102 void VariableDeclarator::Compile(compiler::ETSGen *etsg) const
103 {
104     etsg->GetAstCompiler()->Compile(this);
105 }
106 
Check(checker::TSChecker * checker)107 checker::Type *VariableDeclarator::Check([[maybe_unused]] checker::TSChecker *checker)
108 {
109     return checker->GetAnalyzer()->Check(this);
110 }
111 
Check(checker::ETSChecker * checker)112 checker::VerifiedType VariableDeclarator::Check(checker::ETSChecker *checker)
113 {
114     return {this, checker->GetAnalyzer()->Check(this)};
115 }
116 }  // namespace ark::es2panda::ir
117