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