• 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 "newExpression.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 
22 namespace ark::es2panda::ir {
NewExpression(Tag const tag,NewExpression const & other,ArenaAllocator * const allocator)23 NewExpression::NewExpression([[maybe_unused]] Tag const tag, NewExpression const &other,
24                              ArenaAllocator *const allocator)
25     : Expression(static_cast<Expression const &>(other)), arguments_(allocator->Adapter())
26 {
27     if (other.callee_ != nullptr) {
28         callee_ = other.callee_->Clone(allocator, this)->AsExpression();
29     }
30 
31     for (auto *argument : other.arguments_) {
32         arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression());
33     }
34 }
35 
Clone(ArenaAllocator * const allocator,AstNode * const parent)36 NewExpression *NewExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
37 {
38     auto *const clone = allocator->New<NewExpression>(Tag {}, *this, allocator);
39     ES2PANDA_ASSERT(clone != nullptr);
40     if (parent != nullptr) {
41         clone->SetParent(parent);
42     }
43     clone->SetRange(Range());
44     return clone;
45 }
46 
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)47 void NewExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
48 {
49     if (auto *transformedNode = cb(callee_); callee_ != transformedNode) {
50         callee_->SetTransformedNode(transformationName, transformedNode);
51         callee_ = transformedNode->AsExpression();
52     }
53 
54     for (auto *&it : VectorIterationGuard(arguments_)) {
55         if (auto *transformedNode = cb(it); it != transformedNode) {
56             it->SetTransformedNode(transformationName, transformedNode);
57             it = transformedNode->AsExpression();
58         }
59     }
60 }
61 
Iterate(const NodeTraverser & cb) const62 void NewExpression::Iterate(const NodeTraverser &cb) const
63 {
64     cb(callee_);
65 
66     for (auto *it : VectorIterationGuard(arguments_)) {
67         cb(it);
68     }
69 }
70 
Dump(ir::AstDumper * dumper) const71 void NewExpression::Dump(ir::AstDumper *dumper) const
72 {
73     dumper->Add({{"type", "NewExpression"}, {"callee", callee_}, {"arguments", arguments_}});
74 }
75 
Dump(ir::SrcDumper * dumper) const76 void NewExpression::Dump(ir::SrcDumper *dumper) const
77 {
78     dumper->Add("NewExpression");
79 }
80 
Compile(compiler::PandaGen * pg) const81 void NewExpression::Compile(compiler::PandaGen *pg) const
82 {
83     pg->GetAstCompiler()->Compile(this);
84 }
85 
Compile(compiler::ETSGen * etsg) const86 void NewExpression::Compile(compiler::ETSGen *etsg) const
87 {
88     etsg->GetAstCompiler()->Compile(this);
89 }
90 
Check(checker::TSChecker * checker)91 checker::Type *NewExpression::Check(checker::TSChecker *checker)
92 {
93     return checker->GetAnalyzer()->Check(this);
94 }
95 
Check(checker::ETSChecker * checker)96 checker::VerifiedType NewExpression::Check(checker::ETSChecker *checker)
97 {
98     return {this, checker->GetAnalyzer()->Check(this)};
99 }
100 }  // namespace ark::es2panda::ir
101