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_ARRAY_EXPRESSION_H 17 #define ES2PANDA_IR_EXPRESSION_ARRAY_EXPRESSION_H 18 19 #include "ir/expression.h" 20 #include "ir/validationInfo.h" 21 #include "checker/types/ets/etsArrayType.h" 22 23 namespace ark::es2panda::checker { 24 class ETSAnalyzer; 25 } // namespace ark::es2panda::checker 26 namespace ark::es2panda::compiler { 27 class ETSCompiler; 28 } // namespace ark::es2panda::compiler 29 namespace ark::es2panda::ir { 30 class ArrayExpression : public AnnotatedExpression { 31 private: 32 struct Tag {}; 33 34 public: 35 ArrayExpression() = delete; 36 ~ArrayExpression() override = default; 37 38 NO_COPY_SEMANTIC(ArrayExpression); 39 NO_MOVE_SEMANTIC(ArrayExpression); 40 41 public: ArrayExpression(ArenaVector<Expression * > && elements,ArenaAllocator * const allocator)42 explicit ArrayExpression(ArenaVector<Expression *> &&elements, ArenaAllocator *const allocator) 43 : ArrayExpression(AstNodeType::ARRAY_EXPRESSION, std::move(elements), allocator, false) 44 { 45 } 46 ArrayExpression(AstNodeType nodeType,ArenaVector<Expression * > && elements,ArenaAllocator * const allocator,bool const trailingComma)47 explicit ArrayExpression(AstNodeType nodeType, ArenaVector<Expression *> &&elements, 48 ArenaAllocator *const allocator, bool const trailingComma) 49 : AnnotatedExpression(nodeType), 50 decorators_(allocator->Adapter()), 51 elements_(std::move(elements)), 52 trailingComma_(trailingComma) 53 { 54 } 55 56 explicit ArrayExpression(Tag tag, ArrayExpression const &other, ArenaAllocator *allocator); 57 58 // NOTE (vivienvoros): these friend relationships can be removed once there are getters for private fields 59 friend class checker::ETSAnalyzer; 60 friend class compiler::ETSCompiler; 61 GetElementNodeAtIdx(const size_t idx)62 [[nodiscard]] Expression *GetElementNodeAtIdx(const size_t idx) const noexcept 63 { 64 return elements_[idx]; 65 } 66 Elements()67 [[nodiscard]] const ArenaVector<Expression *> &Elements() const noexcept 68 { 69 return elements_; 70 } 71 Elements()72 [[nodiscard]] ArenaVector<Expression *> &Elements() noexcept 73 { 74 return elements_; 75 } 76 SetElements(ArenaVector<Expression * > && elements)77 void SetElements(ArenaVector<Expression *> &&elements) noexcept 78 { 79 elements_ = std::move(elements); 80 } 81 IsDeclaration()82 [[nodiscard]] bool IsDeclaration() const noexcept 83 { 84 return isDeclaration_; 85 } 86 IsOptional()87 [[nodiscard]] bool IsOptional() const noexcept 88 { 89 return optional_; 90 } 91 SetDeclaration()92 void SetDeclaration() noexcept 93 { 94 isDeclaration_ = true; 95 } 96 SetOptional(bool optional)97 void SetOptional(bool optional) noexcept 98 { 99 optional_ = optional; 100 } 101 SetPreferredType(checker::Type * preferredType)102 void SetPreferredType(checker::Type *preferredType) noexcept 103 { 104 preferredType_ = preferredType; 105 } 106 GetPreferredType()107 [[nodiscard]] checker::Type *GetPreferredType() noexcept 108 { 109 return preferredType_; 110 } 111 GetPreferredType()112 [[nodiscard]] checker::Type const *GetPreferredType() const noexcept 113 { 114 return preferredType_; 115 } 116 Decorators()117 [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept 118 { 119 return decorators_; 120 } 121 DecoratorsPtr()122 const ArenaVector<Decorator *> *DecoratorsPtr() const override 123 { 124 return &Decorators(); 125 } 126 AddDecorators(ArenaVector<ir::Decorator * > && decorators)127 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 128 { 129 decorators_ = std::move(decorators); 130 } 131 CanHaveDecorator(bool inTs)132 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 133 { 134 return true; 135 } 136 CleanUp()137 void CleanUp() override 138 { 139 AstNode::CleanUp(); 140 preferredType_ = nullptr; 141 } 142 143 void ClearPreferredType(); 144 145 [[nodiscard]] ArrayExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override; 146 147 [[nodiscard]] bool ConvertibleToArrayPattern(); 148 [[nodiscard]] ValidationInfo ValidateExpression(); 149 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 150 void Iterate(const NodeTraverser &cb) const override; 151 void Dump(ir::AstDumper *dumper) const override; 152 void Dump(ir::SrcDumper *dumper) const override; 153 void Compile(compiler::PandaGen *pg) const override; 154 void Compile(compiler::ETSGen *etsg) const override; 155 checker::Type *Check(checker::TSChecker *checker) override; 156 checker::VerifiedType Check(checker::ETSChecker *checker) override; 157 checker::Type *CheckPattern(checker::TSChecker *checker); 158 bool TrySetPreferredTypeForNestedArrayExpr(checker::ETSChecker *checker, ArrayExpression *nestedArrayExpr, 159 std::size_t idx) const; 160 Accept(ASTVisitorT * v)161 void Accept(ASTVisitorT *v) override 162 { 163 v->Accept(this); 164 } 165 static std::optional<checker::Type *> ExtractPossiblePreferredType(checker::Type *type); 166 167 void SetPreferredTypeBasedOnFuncParam(checker::ETSChecker *checker, checker::Type *param, 168 checker::TypeRelationFlag flags); 169 170 private: 171 ArenaVector<Decorator *> decorators_; 172 ArenaVector<Expression *> elements_; 173 checker::Type *preferredType_ {}; 174 bool isDeclaration_ {}; 175 bool trailingComma_ {}; 176 bool optional_ {}; 177 }; 178 } // namespace ark::es2panda::ir 179 180 #endif 181