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 #ifndef ES2PANDA_COMPILER_TYPESCRIPT_TYPES_TUPLE_TYPE_H 17 #define ES2PANDA_COMPILER_TYPESCRIPT_TYPES_TUPLE_TYPE_H 18 19 #include <macros.h> 20 21 #include <binder/variable.h> 22 #include <typescript/types/elementFlags.h> 23 #include <typescript/types/objectType.h> 24 25 namespace panda::es2panda::checker { 26 27 using NamedTupleMemberPool = std::unordered_map<binder::LocalVariable *, util::StringView>; 28 29 class TupleType : public ObjectType { 30 public: TupleType(ArenaAllocator * allocator)31 explicit TupleType(ArenaAllocator *allocator) 32 : ObjectType(ObjectTypeKind::TUPLE), elementFlags_(allocator->Adapter()) 33 { 34 } 35 TupleType(ObjectDescriptor * desc,ArenaVector<ElementFlags> && elementFlags,ElementFlags combinedFlags,uint32_t minLength,uint32_t fixedLength,bool readonly)36 TupleType(ObjectDescriptor *desc, ArenaVector<ElementFlags> &&elementFlags, ElementFlags combinedFlags, 37 uint32_t minLength, uint32_t fixedLength, bool readonly) 38 : ObjectType(ObjectType::ObjectTypeKind::TUPLE, desc), 39 elementFlags_(std::move(elementFlags)), 40 combinedFlags_(combinedFlags), 41 minLength_(minLength), 42 fixedLength_(fixedLength), 43 readonly_(readonly) 44 { 45 if (readonly_) { 46 for (auto *it : Properties()) { 47 it->AddFlag(binder::VariableFlags::READONLY); 48 } 49 } 50 } 51 TupleType(ObjectDescriptor * desc,ArenaVector<ElementFlags> && elementFlags,ElementFlags combinedFlags,uint32_t minLength,uint32_t fixedLength,bool readonly,NamedTupleMemberPool && namedMembers)52 TupleType(ObjectDescriptor *desc, ArenaVector<ElementFlags> &&elementFlags, ElementFlags combinedFlags, 53 uint32_t minLength, uint32_t fixedLength, bool readonly, NamedTupleMemberPool &&namedMembers) 54 : ObjectType(ObjectType::ObjectTypeKind::TUPLE, desc), 55 elementFlags_(std::move(elementFlags)), 56 combinedFlags_(combinedFlags), 57 minLength_(minLength), 58 fixedLength_(fixedLength), 59 namedMembers_(std::move(namedMembers)), 60 readonly_(readonly) 61 { 62 if (readonly_) { 63 for (auto *it : Properties()) { 64 it->AddFlag(binder::VariableFlags::READONLY); 65 } 66 } 67 } 68 CombinedFlags()69 ElementFlags CombinedFlags() const 70 { 71 return combinedFlags_; 72 } 73 MinLength()74 uint32_t MinLength() const 75 { 76 return minLength_; 77 } 78 FixedLength()79 uint32_t FixedLength() const 80 { 81 return fixedLength_; 82 } 83 HasCombinedFlag(ElementFlags combinedFlag)84 bool HasCombinedFlag(ElementFlags combinedFlag) const 85 { 86 return (combinedFlags_ & combinedFlag) != 0; 87 } 88 IsReadOnly()89 bool IsReadOnly() const 90 { 91 return readonly_; 92 } 93 NamedMembers()94 const NamedTupleMemberPool &NamedMembers() const 95 { 96 return namedMembers_; 97 } 98 FindNamedMemberName(binder::LocalVariable * member)99 const util::StringView &FindNamedMemberName(binder::LocalVariable *member) const 100 { 101 auto res = namedMembers_.find(member); 102 return res->second; 103 } 104 105 Type *ConvertToArrayType(Checker *checker); 106 107 void ToString(std::stringstream &ss) const override; 108 void Identical(TypeRelation *relation, Type *other) override; 109 void AssignmentTarget(TypeRelation *relation, Type *source) override; 110 TypeFacts GetTypeFacts() const override; 111 Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; 112 113 private: 114 ArenaVector<ElementFlags> elementFlags_; 115 ElementFlags combinedFlags_ {}; 116 uint32_t minLength_ {}; 117 uint32_t fixedLength_ {}; 118 NamedTupleMemberPool namedMembers_ {}; 119 bool readonly_ {}; 120 }; 121 122 } // namespace panda::es2panda::checker 123 124 #endif /* TYPESCRIPT_TYPES_TUPLE_TYPE_H */ 125