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 "etsNewClassInstanceExpression.h"
17
18 #include "compiler/core/ETSGen.h"
19 #include "compiler/core/pandagen.h"
20 #include "checker/TSchecker.h"
21
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)23 void ETSNewClassInstanceExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
24 {
25 if (auto *transformedNode = cb(typeReference_); typeReference_ != transformedNode) {
26 typeReference_->SetTransformedNode(transformationName, transformedNode);
27 typeReference_ = transformedNode->AsExpression();
28 }
29
30 for (auto *&arg : arguments_) {
31 if (auto *transformedNode = cb(arg); arg != transformedNode) {
32 arg->SetTransformedNode(transformationName, transformedNode);
33 arg = transformedNode->AsExpression();
34 }
35 }
36 }
37
Iterate(const NodeTraverser & cb) const38 void ETSNewClassInstanceExpression::Iterate([[maybe_unused]] const NodeTraverser &cb) const
39 {
40 cb(typeReference_);
41
42 for (auto *arg : arguments_) {
43 cb(arg);
44 }
45 }
46
Dump(ir::AstDumper * dumper) const47 void ETSNewClassInstanceExpression::Dump(ir::AstDumper *dumper) const
48 {
49 dumper->Add(
50 {{"type", "ETSNewClassInstanceExpression"}, {"typeReference", typeReference_}, {"arguments", arguments_}});
51 }
52
Dump(ir::SrcDumper * dumper) const53 void ETSNewClassInstanceExpression::Dump(ir::SrcDumper *dumper) const
54 {
55 dumper->Add("new ");
56 if (typeReference_ != nullptr) {
57 if (typeReference_->IsETSUnionType()) {
58 dumper->Add("(");
59 }
60 typeReference_->Dump(dumper);
61 if (typeReference_->IsETSUnionType()) {
62 dumper->Add(")");
63 }
64 }
65
66 dumper->Add("(");
67 for (auto argument : arguments_) {
68 argument->Dump(dumper);
69 if (argument != arguments_.back()) {
70 dumper->Add(", ");
71 }
72 }
73 dumper->Add(")");
74 }
75
Compile(compiler::PandaGen * pg) const76 void ETSNewClassInstanceExpression::Compile(compiler::PandaGen *pg) const
77 {
78 pg->GetAstCompiler()->Compile(this);
79 }
80
Compile(compiler::ETSGen * etsg) const81 void ETSNewClassInstanceExpression::Compile(compiler::ETSGen *etsg) const
82 {
83 etsg->GetAstCompiler()->Compile(this);
84 }
85
Check(checker::TSChecker * checker)86 checker::Type *ETSNewClassInstanceExpression::Check(checker::TSChecker *checker)
87 {
88 return checker->GetAnalyzer()->Check(this);
89 }
90
Check(checker::ETSChecker * checker)91 checker::VerifiedType ETSNewClassInstanceExpression::Check(checker::ETSChecker *checker)
92 {
93 return {this, checker->GetAnalyzer()->Check(this)};
94 }
95
ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const & other,ArenaAllocator * const allocator)96 ETSNewClassInstanceExpression::ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const &other,
97 ArenaAllocator *const allocator)
98 : Expression(static_cast<Expression const &>(other)), arguments_(allocator->Adapter()), signature_(other.signature_)
99 {
100 typeReference_ =
101 other.typeReference_ != nullptr ? other.typeReference_->Clone(allocator, this)->AsExpression() : nullptr;
102
103 for (auto *const argument : other.arguments_) {
104 arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression());
105 }
106 }
107
Clone(ArenaAllocator * const allocator,AstNode * const parent)108 ETSNewClassInstanceExpression *ETSNewClassInstanceExpression::Clone(ArenaAllocator *const allocator,
109 AstNode *const parent)
110 {
111 auto *const clone = allocator->New<ETSNewClassInstanceExpression>(*this, allocator);
112 ES2PANDA_ASSERT(clone);
113 if (parent != nullptr) {
114 clone->SetParent(parent);
115 }
116 return clone;
117 }
118
TypeIsAllowedForInstantiation(checker::Type * type)119 bool ETSNewClassInstanceExpression::TypeIsAllowedForInstantiation(checker::Type *type)
120 {
121 return !(type->IsETSNullType() || type->IsETSUndefinedType() || type->IsETSNeverType() || type->IsETSVoidType());
122 }
123
124 } // namespace ark::es2panda::ir
125