• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (prefix_) {
53         dumper->Add(TokenToString(operator_));
54         argument_->Dump(dumper);
55     } else {
56         argument_->Dump(dumper);
57         dumper->Add(TokenToString(operator_));
58     }
59 }
60 
Compile(compiler::PandaGen * pg) const61 void UpdateExpression::Compile(compiler::PandaGen *pg) const
62 {
63     pg->GetAstCompiler()->Compile(this);
64 }
65 
Compile(compiler::ETSGen * etsg) const66 void UpdateExpression::Compile(compiler::ETSGen *etsg) const
67 {
68     etsg->GetAstCompiler()->Compile(this);
69 }
70 
Check(checker::TSChecker * checker)71 checker::Type *UpdateExpression::Check(checker::TSChecker *checker)
72 {
73     return checker->GetAnalyzer()->Check(this);
74 }
75 
Check(checker::ETSChecker * checker)76 checker::Type *UpdateExpression::Check(checker::ETSChecker *checker)
77 {
78     return checker->GetAnalyzer()->Check(this);
79 }
80 
Clone(ArenaAllocator * const allocator,AstNode * const parent)81 UpdateExpression *UpdateExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
82 {
83     auto *const argument = argument_ != nullptr ? argument_->Clone(allocator, nullptr)->AsExpression() : nullptr;
84 
85     if (auto *const clone = allocator->New<UpdateExpression>(argument, operator_, prefix_); clone != nullptr) {
86         if (argument != nullptr) {
87             argument->SetParent(clone);
88         }
89 
90         if (parent != nullptr) {
91             clone->SetParent(parent);
92         }
93 
94         clone->SetRange(Range());
95         return clone;
96     }
97 
98     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
99 }
100 }  // namespace ark::es2panda::ir
101