• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <typescript/checker.h>
17 #include <typescript/types/indexInfo.h>
18 
19 namespace panda::es2panda::checker {
20 
CreateNumberLiteralType(double value)21 Type *Checker::CreateNumberLiteralType(double value)
22 {
23     auto search = numberLiteralMap_.find(value);
24     if (search != numberLiteralMap_.end()) {
25         return search->second;
26     }
27 
28     auto *newNumLiteralType = allocator_->New<NumberLiteralType>(value);
29     numberLiteralMap_.insert({value, newNumLiteralType});
30     return newNumLiteralType;
31 }
32 
CreateBigintLiteralType(const util::StringView & str,bool negative)33 Type *Checker::CreateBigintLiteralType(const util::StringView &str, bool negative)
34 {
35     auto search = bigintLiteralMap_.find(str);
36     if (search != bigintLiteralMap_.end()) {
37         return search->second;
38     }
39 
40     auto *newBigiLiteralType = allocator_->New<BigintLiteralType>(str, negative);
41     bigintLiteralMap_.insert({str, newBigiLiteralType});
42     return newBigiLiteralType;
43 }
44 
CreateStringLiteralType(const util::StringView & str)45 Type *Checker::CreateStringLiteralType(const util::StringView &str)
46 {
47     auto search = stringLiteralMap_.find(str);
48     if (search != stringLiteralMap_.end()) {
49         return search->second;
50     }
51 
52     auto *newStrLiteralType = allocator_->New<StringLiteralType>(str);
53     stringLiteralMap_.insert({str, newStrLiteralType});
54     return newStrLiteralType;
55 }
56 
CreateUnionType(std::initializer_list<Type * > constituentTypes)57 Type *Checker::CreateUnionType(std::initializer_list<Type *> constituentTypes)
58 {
59     ArenaVector<Type *> newConstituentTypes(allocator_->Adapter());
60 
61     for (auto *it : constituentTypes) {
62         newConstituentTypes.push_back(it);
63     }
64 
65     return CreateUnionType(std::move(newConstituentTypes));
66 }
67 
CreateUnionType(ArenaVector<Type * > & constituentTypes)68 Type *Checker::CreateUnionType(ArenaVector<Type *> &constituentTypes)
69 {
70     ArenaVector<Type *> newConstituentTypes(allocator_->Adapter());
71 
72     for (auto *it : constituentTypes) {
73         if (it->IsUnionType()) {
74             for (auto *type : it->AsUnionType()->ConstituentTypes()) {
75                 newConstituentTypes.push_back(type);
76             }
77 
78             continue;
79         }
80 
81         newConstituentTypes.push_back(it);
82     }
83 
84     UnionType::RemoveDuplicatedTypes(relation_, newConstituentTypes);
85 
86     if (newConstituentTypes.size() == 1) {
87         return newConstituentTypes[0];
88     }
89 
90     auto *newUnionType = allocator_->New<UnionType>(newConstituentTypes);
91 
92     return UnionType::HandleUnionType(newUnionType, globalTypes_);
93 }
94 
CreateUnionType(ArenaVector<Type * > && constituentTypes)95 Type *Checker::CreateUnionType(ArenaVector<Type *> &&constituentTypes)
96 {
97     if (constituentTypes.empty()) {
98         return nullptr;
99     }
100 
101     ArenaVector<Type *> newConstituentTypes(allocator_->Adapter());
102 
103     for (auto *it : constituentTypes) {
104         if (it->IsUnionType()) {
105             for (auto *type : it->AsUnionType()->ConstituentTypes()) {
106                 newConstituentTypes.push_back(type);
107             }
108 
109             continue;
110         }
111 
112         newConstituentTypes.push_back(it);
113     }
114 
115     UnionType::RemoveDuplicatedTypes(relation_, newConstituentTypes);
116 
117     if (newConstituentTypes.size() == 1) {
118         return newConstituentTypes[0];
119     }
120 
121     auto *newUnionType = allocator_->New<UnionType>(std::move(newConstituentTypes));
122 
123     return UnionType::HandleUnionType(newUnionType, globalTypes_);
124 }
125 
CreateObjectTypeWithCallSignature(Signature * callSignature)126 Type *Checker::CreateObjectTypeWithCallSignature(Signature *callSignature)
127 {
128     auto *objType = allocator_->New<ObjectLiteralType>(allocator_->New<ObjectDescriptor>(allocator_));
129     objType->AddCallSignature(callSignature);
130     return objType;
131 }
132 
CreateObjectTypeWithConstructSignature(Signature * constructSignature)133 Type *Checker::CreateObjectTypeWithConstructSignature(Signature *constructSignature)
134 {
135     auto *objType = allocator_->New<ObjectLiteralType>(allocator_->New<ObjectDescriptor>(allocator_));
136     objType->AddConstructSignature(constructSignature);
137     return objType;
138 }
139 
CreateFunctionTypeWithSignature(Signature * callSignature)140 Type *Checker::CreateFunctionTypeWithSignature(Signature *callSignature)
141 {
142     auto *funcObjType = allocator_->New<FunctionType>(allocator_->New<ObjectDescriptor>(allocator_));
143     funcObjType->AddCallSignature(callSignature);
144     return funcObjType;
145 }
146 
CreateConstructorTypeWithSignature(Signature * constructSignature)147 Type *Checker::CreateConstructorTypeWithSignature(Signature *constructSignature)
148 {
149     auto *constructObjType = allocator_->New<ConstructorType>(allocator_->New<ObjectDescriptor>(allocator_));
150     constructObjType->AddConstructSignature(constructSignature);
151     return constructObjType;
152 }
153 
CreateTupleType(ObjectDescriptor * desc,ArenaVector<ElementFlags> && elementFlags,ElementFlags combinedFlags,uint32_t minLength,uint32_t fixedLength,bool readonly)154 Type *Checker::CreateTupleType(ObjectDescriptor *desc, ArenaVector<ElementFlags> &&elementFlags,
155                                ElementFlags combinedFlags, uint32_t minLength, uint32_t fixedLength, bool readonly)
156 {
157     desc->stringIndexInfo = allocator_->New<IndexInfo>(GlobalAnyType(), "x", readonly);
158     return allocator_->New<TupleType>(desc, std::move(elementFlags), combinedFlags, minLength, fixedLength, readonly);
159 }
160 
CreateTupleType(ObjectDescriptor * desc,ArenaVector<ElementFlags> && elementFlags,ElementFlags combinedFlags,uint32_t minLength,uint32_t fixedLength,bool readonly,NamedTupleMemberPool && namedMembers)161 Type *Checker::CreateTupleType(ObjectDescriptor *desc, ArenaVector<ElementFlags> &&elementFlags,
162                                ElementFlags combinedFlags, uint32_t minLength, uint32_t fixedLength, bool readonly,
163                                NamedTupleMemberPool &&namedMembers)
164 {
165     desc->stringIndexInfo = allocator_->New<IndexInfo>(GlobalAnyType(), "x", readonly);
166 
167     if (!namedMembers.empty()) {
168         return allocator_->New<TupleType>(desc, std::move(elementFlags), combinedFlags, minLength, fixedLength,
169                                           readonly, std::move(namedMembers));
170     }
171 
172     return allocator_->New<TupleType>(desc, std::move(elementFlags), combinedFlags, minLength, fixedLength, readonly);
173 }
174 }  // namespace panda::es2panda::checker
175