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 "updateExpression.h"
17 
18 #include "macros.h"
19 #include "varbinder/variable.h"
20 #include "compiler/base/lreference.h"
21 #include "compiler/core/pandagen.h"
22 #include "compiler/core/ETSGen.h"
23 #include "compiler/core/regScope.h"
24 #include "checker/TSchecker.h"
25 #include "checker/ETSchecker.h"
26 #include "ir/astDump.h"
27 #include "ir/srcDump.h"
28 #include "ir/expressions/unaryExpression.h"
29 
30 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view transformationName)31 void UpdateExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
32 {
33     if (auto *transformedNode = cb(argument_); argument_ != transformedNode) {
34         argument_->SetTransformedNode(transformationName, transformedNode);
35         argument_ = transformedNode->AsExpression();
36     }
37 }
38 
Iterate(const NodeTraverser &cb) const39 void UpdateExpression::Iterate(const NodeTraverser &cb) const
40 {
41     cb(argument_);
42 }
43 
Dump(ir::AstDumper *dumper) const44 void UpdateExpression::Dump(ir::AstDumper *dumper) const
45 {
46     dumper->Add({{"type", "UpdateExpression"}, {"operator", operator_}, {"prefix", prefix_}, {"argument", argument_}});
47 }
48 
Dump(ir::SrcDumper *dumper) const49 void UpdateExpression::Dump(ir::SrcDumper *dumper) const
50 {
51     ASSERT(argument_);
52     dumper->Add("(");
53     if (prefix_) {
54         dumper->Add(TokenToString(operator_));
55         argument_->Dump(dumper);
56     } else {
57         argument_->Dump(dumper);
58         dumper->Add(TokenToString(operator_));
59     }
60     dumper->Add(")");
61 }
62 
Compile(compiler::PandaGen *pg) const63 void UpdateExpression::Compile(compiler::PandaGen *pg) const
64 {
65     pg->GetAstCompiler()->Compile(this);
66 }
67 
Compile(compiler::ETSGen *etsg) const68 void UpdateExpression::Compile(compiler::ETSGen *etsg) const
69 {
70     etsg->GetAstCompiler()->Compile(this);
71 }
72 
Check(checker::TSChecker *checker)73 checker::Type *UpdateExpression::Check(checker::TSChecker *checker)
74 {
75     return checker->GetAnalyzer()->Check(this);
76 }
77 
Check(checker::ETSChecker *checker)78 checker::Type *UpdateExpression::Check(checker::ETSChecker *checker)
79 {
80     return checker->GetAnalyzer()->Check(this);
81 }
82 
Clone(ArenaAllocator *const allocator, AstNode *const parent)83 UpdateExpression *UpdateExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
84 {
85     auto *const argument = argument_ != nullptr ? argument_->Clone(allocator, nullptr)->AsExpression() : nullptr;
86 
87     if (auto *const clone = allocator->New<UpdateExpression>(argument, operator_, prefix_); clone != nullptr) {
88         if (argument != nullptr) {
89             argument->SetParent(clone);
90         }
91 
92         if (parent != nullptr) {
93             clone->SetParent(parent);
94         }
95 
96         clone->SetRange(Range());
97         return clone;
98     }
99 
100     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
101 }
102 }  // namespace ark::es2panda::ir
103