1 /** 2 * Copyright (c) 2023-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_COMPILER_CHECKER_TYPES_ETS_TUPLE_TYPE_H 17 #define ES2PANDA_COMPILER_CHECKER_TYPES_ETS_TUPLE_TYPE_H 18 19 #include "checker/types/type.h" 20 #include "checker/types/ets/etsArrayType.h" 21 22 namespace ark::es2panda::checker { 23 24 class ETSTupleType : public ETSArrayType { 25 using TupleSizeType = int32_t; 26 27 public: 28 explicit ETSTupleType(ArenaAllocator *const allocator, Type *const elementType = nullptr, 29 Type *const spreadType = nullptr) ETSArrayType(elementType)30 : ETSArrayType(elementType), typeList_(allocator->Adapter()), spreadType_(spreadType) 31 { 32 typeFlags_ |= TypeFlag::ETS_TUPLE; 33 } 34 35 explicit ETSTupleType(ArenaAllocator *const allocator, const TupleSizeType size, Type *const elementType = nullptr, 36 Type *const spreadType = nullptr) ETSArrayType(elementType)37 : ETSArrayType(elementType), typeList_(allocator->Adapter()), spreadType_(spreadType), size_(size) 38 { 39 typeFlags_ |= TypeFlag::ETS_TUPLE; 40 } 41 explicit ETSTupleType(const ArenaVector<Type *> &typeList, Type *const elementType = nullptr, 42 Type *const spreadType = nullptr) ETSArrayType(elementType)43 : ETSArrayType(elementType), 44 typeList_(typeList), 45 spreadType_(spreadType), 46 size_(static_cast<TupleSizeType>(typeList.size())) 47 { 48 typeFlags_ |= TypeFlag::ETS_TUPLE; 49 } 50 GetTupleSize()51 [[nodiscard]] TupleSizeType GetTupleSize() const 52 { 53 return size_; 54 } 55 GetMinTupleSize()56 [[nodiscard]] TupleSizeType GetMinTupleSize() const 57 { 58 return size_ + (spreadType_ == nullptr ? 0 : 1); 59 } 60 GetTupleTypesList()61 [[nodiscard]] ArenaVector<Type *> const &GetTupleTypesList() const 62 { 63 return typeList_; 64 } 65 HasSpreadType()66 [[nodiscard]] bool HasSpreadType() const 67 { 68 return spreadType_ != nullptr; 69 } 70 GetSpreadType()71 [[nodiscard]] Type *GetSpreadType() const 72 { 73 return spreadType_; 74 } 75 SetSpreadType(Type * const newSpreadType)76 void SetSpreadType(Type *const newSpreadType) 77 { 78 spreadType_ = newSpreadType; 79 } 80 81 [[nodiscard]] Type *GetTypeAtIndex(int32_t index) const; 82 83 void ToString(std::stringstream &ss, bool precise) const override; 84 85 void Identical(TypeRelation *relation, Type *other) override; 86 void AssignmentTarget(TypeRelation *relation, Type *source) override; 87 bool AssignmentSource(TypeRelation *relation, Type *target) override; 88 Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; 89 void Cast(TypeRelation *relation, Type *target) override; 90 Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; 91 92 private: 93 ArenaVector<Type *> const typeList_; 94 Type *spreadType_ {}; 95 TupleSizeType size_ {0}; 96 }; 97 98 } // namespace ark::es2panda::checker 99 100 #endif /* ES2PANDA_COMPILER_CHECKER_TYPES_ETS_TUPLE_TYPE_H */ 101