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 #ifndef ES2PANDA_IR_EXPRESSION_UPDATE_EXPRESSION_H
17 #define ES2PANDA_IR_EXPRESSION_UPDATE_EXPRESSION_H
18 
19 #include "ir/expression.h"
20 #include "lexer/token/tokenType.h"
21 namespace ark::es2panda::checker {
22 class TSAnalyzer;
23 class ETSAnalyzer;
24 }  // namespace ark::es2panda::checker
25 namespace ark::es2panda::ir {
26 class UpdateExpression : public Expression {
27 public:
28     UpdateExpression() = delete;
29     ~UpdateExpression() override = default;
30 
31     NO_COPY_SEMANTIC(UpdateExpression);
32     NO_MOVE_SEMANTIC(UpdateExpression);
33 
UpdateExpression(Expression *const argument, lexer::TokenType const updateOperator, bool const isPrefix)34     explicit UpdateExpression(Expression *const argument, lexer::TokenType const updateOperator, bool const isPrefix)
35         : Expression(AstNodeType::UPDATE_EXPRESSION), argument_(argument), operator_(updateOperator), prefix_(isPrefix)
36     {
37         ASSERT(updateOperator == lexer::TokenType::PUNCTUATOR_PLUS_PLUS ||
38                updateOperator == lexer::TokenType::PUNCTUATOR_MINUS_MINUS);
39     }
40 
41     // NOTE (vivienvoros): these friend relationships can be removed once there are getters for private fields
42     friend class checker::TSAnalyzer;
43     friend class checker::ETSAnalyzer;
44 
45     [[nodiscard]] lexer::TokenType OperatorType() const noexcept
46     {
47         return operator_;
48     }
49 
50     [[nodiscard]] Expression *Argument() noexcept
51     {
52         return argument_;
53     }
54 
55     [[nodiscard]] const Expression *Argument() const noexcept
56     {
57         return argument_;
58     }
59 
60     [[nodiscard]] bool IsPrefix() const noexcept
61     {
62         return prefix_;
63     }
64 
65     [[nodiscard]] UpdateExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override;
66 
67     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
68     void Iterate(const NodeTraverser &cb) const override;
69     void Dump(ir::AstDumper *dumper) const override;
70     void Dump(ir::SrcDumper *dumper) const override;
71     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
72     void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
73     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
74     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
75 
76     void Accept(ASTVisitorT *v) override
77     {
78         v->Accept(this);
79     }
80 
81 private:
82     Expression *argument_;
83     lexer::TokenType operator_;
84     bool prefix_;
85 };
86 }  // namespace ark::es2panda::ir
87 
88 #endif
89