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