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