1 /** 2 * Copyright (c) 2021-2025 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_ASSIGNMENT_EXPRESSION_H 17 #define ES2PANDA_IR_EXPRESSION_ASSIGNMENT_EXPRESSION_H 18 19 #include "ir/expression.h" 20 #include "generated/tokenType.h" 21 namespace ark::es2panda::checker { 22 class ETSAnalyzer; 23 } // namespace ark::es2panda::checker 24 namespace ark::es2panda::ir { 25 class AssignmentExpression : public Expression { 26 private: 27 struct Tag {}; 28 29 public: 30 AssignmentExpression() = delete; 31 ~AssignmentExpression() override = default; 32 33 AssignmentExpression &operator=(const AssignmentExpression &) = delete; 34 NO_MOVE_SEMANTIC(AssignmentExpression); 35 AssignmentExpression(Expression * const left,Expression * const right,lexer::TokenType const assignmentOperator)36 explicit AssignmentExpression(Expression *const left, Expression *const right, 37 lexer::TokenType const assignmentOperator) 38 : AssignmentExpression(AstNodeType::ASSIGNMENT_EXPRESSION, left, right, assignmentOperator) 39 { 40 } 41 AssignmentExpression(AstNodeType const type,Expression * const left,Expression * const right,lexer::TokenType const assignmentOperator)42 explicit AssignmentExpression(AstNodeType const type, Expression *const left, Expression *const right, 43 lexer::TokenType const assignmentOperator) 44 : Expression(type), left_(left), right_(right), operator_(assignmentOperator) 45 { 46 } 47 48 explicit AssignmentExpression(Tag tag, AssignmentExpression const &other, Expression *left, Expression *right); 49 50 // NOTE (vivienvoros): these friend relationships can be removed once there are getters for private fields 51 friend class checker::ETSAnalyzer; 52 Left()53 [[nodiscard]] const Expression *Left() const noexcept 54 { 55 return left_; 56 } 57 Left()58 [[nodiscard]] Expression *Left() noexcept 59 { 60 return left_; 61 } 62 Right()63 [[nodiscard]] Expression *Right() noexcept 64 { 65 return right_; 66 } 67 Right()68 [[nodiscard]] const Expression *Right() const noexcept 69 { 70 return right_; 71 } 72 SetRight(Expression * const expr)73 void SetRight(Expression *const expr) noexcept 74 { 75 right_ = expr; 76 77 if (right_ != nullptr) { 78 right_->SetParent(this); 79 } 80 } 81 SetLeft(Expression * const expr)82 void SetLeft(Expression *const expr) noexcept 83 { 84 left_ = expr; 85 86 if (left_ != nullptr) { 87 left_->SetParent(this); 88 } 89 } 90 Result()91 [[nodiscard]] const Expression *Result() const noexcept 92 { 93 return result_; 94 } 95 Result()96 [[nodiscard]] Expression *Result() noexcept 97 { 98 return result_; 99 } 100 OperatorType()101 [[nodiscard]] lexer::TokenType OperatorType() const noexcept 102 { 103 return operator_; 104 } 105 SetOperatorType(lexer::TokenType tokenType)106 lexer::TokenType SetOperatorType(lexer::TokenType tokenType) noexcept 107 { 108 return operator_ = tokenType; 109 } 110 SetResult(Expression * expr)111 void SetResult(Expression *expr) noexcept 112 { 113 result_ = expr; 114 if (result_ != nullptr) { 115 result_->SetParent(this); 116 } 117 } 118 IsLogicalExtended()119 [[nodiscard]] bool IsLogicalExtended() const noexcept 120 { 121 return operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND || 122 operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR; 123 } 124 Target()125 [[nodiscard]] varbinder::Variable *Target() noexcept 126 { 127 return target_; 128 } 129 Target()130 [[nodiscard]] varbinder::Variable *Target() const noexcept 131 { 132 return target_; 133 } 134 SetIgnoreConstAssign()135 void SetIgnoreConstAssign() 136 { 137 ignoreConstAssign_ = true; 138 } 139 IsIgnoreConstAssign()140 [[nodiscard]] bool IsIgnoreConstAssign() const 141 { 142 return ignoreConstAssign_; 143 } 144 145 [[nodiscard]] AssignmentExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override; 146 147 [[nodiscard]] bool ConvertibleToAssignmentPatternLeft(bool mustBePattern); 148 [[nodiscard]] bool ConvertibleToAssignmentPatternRight(); 149 [[nodiscard]] bool ConvertibleToAssignmentPattern(bool mustBePattern = true); 150 151 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 152 void Iterate(const NodeTraverser &cb) const override; 153 void Dump(ir::AstDumper *dumper) const override; 154 void Dump(ir::SrcDumper *dumper) const override; 155 void Compile(compiler::PandaGen *pg) const override; 156 void Compile(compiler::ETSGen *etsg) const override; 157 void CompilePattern(compiler::PandaGen *pg) const; 158 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 159 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 160 Accept(ASTVisitorT * v)161 void Accept(ASTVisitorT *v) override 162 { 163 v->Accept(this); 164 } 165 CleanUp()166 void CleanUp() override 167 { 168 AstNode::CleanUp(); 169 target_ = nullptr; 170 operationType_ = nullptr; 171 } 172 173 protected: AssignmentExpression(AssignmentExpression const & other)174 AssignmentExpression(AssignmentExpression const &other) : Expression(static_cast<Expression const &>(other)) 175 { 176 operator_ = other.operator_; 177 target_ = other.target_; 178 operationType_ = other.operationType_; 179 } 180 181 private: 182 Expression *left_ = nullptr; 183 Expression *right_ = nullptr; 184 Expression *result_ = nullptr; 185 lexer::TokenType operator_; 186 varbinder::Variable *target_ {}; 187 checker::Type *operationType_ {}; 188 bool ignoreConstAssign_ = false; 189 }; 190 } // namespace ark::es2panda::ir 191 192 #endif 193