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 "unaryExpression.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 UnaryExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
25 {
26 if (auto *transformedNode = cb(argument_); argument_ != transformedNode) {
27 argument_->SetTransformedNode(transformationName, transformedNode);
28 argument_ = transformedNode->AsExpression();
29 }
30 }
31
Iterate(const NodeTraverser & cb) const32 void UnaryExpression::Iterate(const NodeTraverser &cb) const
33 {
34 cb(argument_);
35 }
36
Dump(ir::AstDumper * dumper) const37 void UnaryExpression::Dump(ir::AstDumper *dumper) const
38 {
39 dumper->Add({{"type", "UnaryExpression"}, {"operator", operator_}, {"prefix", true}, {"argument", argument_}});
40 }
41
Dump(ir::SrcDumper * dumper) const42 void UnaryExpression::Dump(ir::SrcDumper *dumper) const
43 {
44 dumper->Add(TokenToString(operator_));
45 if (!argument_->IsIdentifier()) {
46 dumper->Add("(");
47 argument_->Dump(dumper);
48 dumper->Add(")");
49 } else {
50 argument_->Dump(dumper);
51 }
52 }
53
Compile(compiler::PandaGen * pg) const54 void UnaryExpression::Compile(compiler::PandaGen *pg) const
55 {
56 pg->GetAstCompiler()->Compile(this);
57 }
58
Compile(compiler::ETSGen * etsg) const59 void UnaryExpression::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
60 {
61 etsg->GetAstCompiler()->Compile(this);
62 }
63
Check(checker::TSChecker * checker)64 checker::Type *UnaryExpression::Check([[maybe_unused]] checker::TSChecker *checker)
65 {
66 return checker->GetAnalyzer()->Check(this);
67 }
68
Check(checker::ETSChecker * checker)69 checker::VerifiedType UnaryExpression::Check(checker::ETSChecker *checker)
70 {
71 return {this, checker->GetAnalyzer()->Check(this)};
72 }
73
Clone(ArenaAllocator * const allocator,AstNode * const parent)74 UnaryExpression *UnaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
75 {
76 auto *const argument = argument_ != nullptr ? argument_->Clone(allocator, nullptr)->AsExpression() : nullptr;
77 auto *const clone = allocator->New<UnaryExpression>(argument, operator_);
78
79 if (argument != nullptr) {
80 argument->SetParent(clone);
81 }
82
83 ES2PANDA_ASSERT(clone != nullptr);
84 if (parent != nullptr) {
85 clone->SetParent(parent);
86 }
87
88 clone->SetRange(Range());
89 return clone;
90 }
91 } // namespace ark::es2panda::ir
92