1 /**
2  * Copyright (c) 2021 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 <binder/variable.h>
19 #include <compiler/base/lreference.h>
20 #include <compiler/core/pandagen.h>
21 #include <compiler/core/regScope.h>
22 #include <typescript/checker.h>
23 #include <ir/astDump.h>
24 #include <ir/expressions/unaryExpression.h>
25 
26 namespace panda::es2panda::ir {
27 
Iterate(const NodeTraverser &cb) const28 void UpdateExpression::Iterate(const NodeTraverser &cb) const
29 {
30     cb(argument_);
31 }
32 
Dump(ir::AstDumper *dumper) const33 void UpdateExpression::Dump(ir::AstDumper *dumper) const
34 {
35     dumper->Add({{"type", "UpdateExpression"}, {"operator", operator_}, {"prefix", prefix_}, {"argument", argument_}});
36 }
37 
Compile(compiler::PandaGen *pg) const38 void UpdateExpression::Compile(compiler::PandaGen *pg) const
39 {
40     compiler::RegScope rs(pg);
41     compiler::VReg operandReg = pg->AllocReg();
42 
43     compiler::LReference lref = compiler::LReference::CreateLRef(pg, argument_, false);
44     lref.GetValue();
45 
46     if (!IsPrefix()) {
47         pg->StoreAccumulator(this, operandReg);
48         pg->ToNumeric(this, operandReg);
49     }
50 
51     pg->StoreAccumulator(this, operandReg);
52     pg->Unary(this, operator_, operandReg);
53 
54     lref.SetValue();
55 
56     if (!IsPrefix()) {
57         pg->LoadAccumulator(this, operandReg);
58     }
59 }
60 
Check(checker::Checker *checker) const61 checker::Type *UpdateExpression::Check(checker::Checker *checker) const
62 {
63     checker::Type *operandType = argument_->Check(checker);
64     checker->CheckNonNullType(operandType, Start());
65 
66     if (!operandType->HasTypeFlag(checker::TypeFlag::VALID_ARITHMETIC_TYPE)) {
67         checker->ThrowTypeError("An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.",
68                                 Start());
69     }
70 
71     checker->CheckReferenceExpression(
72         argument_, "The operand of an increment or decrement operator must be a variable or a property access",
73         "The operand of an increment or decrement operator may not be an optional property access");
74 
75     return checker->GetUnaryResultType(operandType);
76 }
77 
UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)78 void UpdateExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
79 {
80     argument_ = std::get<ir::AstNode *>(cb(argument_))->AsExpression();
81 }
82 
83 }  // namespace panda::es2panda::ir
84