• 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 <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     pg->StoreAccumulator(this, operandReg);
47     pg->Unary(this, operator_, operandReg);
48 
49     lref.SetValue();
50 
51     if (!IsPrefix()) {
52         pg->ToNumeric(this, operandReg);
53     }
54 }
55 
Check(checker::Checker * checker) const56 checker::Type *UpdateExpression::Check(checker::Checker *checker) const
57 {
58     checker::Type *operandType = argument_->Check(checker);
59     checker->CheckNonNullType(operandType, Start());
60 
61     if (!operandType->HasTypeFlag(checker::TypeFlag::VALID_ARITHMETIC_TYPE)) {
62         checker->ThrowTypeError("An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.",
63                                 Start());
64     }
65 
66     checker->CheckReferenceExpression(
67         argument_, "The operand of an increment or decrement operator must be a variable or a property access",
68         "The operand of an increment or decrement operator may not be an optional property access");
69 
70     return checker->GetUnaryResultType(operandType);
71 }
72 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)73 void UpdateExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
74 {
75     argument_ = std::get<ir::AstNode *>(cb(argument_))->AsExpression();
76 }
77 
78 }  // namespace panda::es2panda::ir
79