• 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     CHECK_NOT_NULL(newUnionType);
92 
93     return UnionType::HandleUnionType(newUnionType, globalTypes_);
94 }
95 
CreateUnionType(ArenaVector<Type * > && constituentTypes)96 Type *Checker::CreateUnionType(ArenaVector<Type *> &&constituentTypes)
97 {
98     if (constituentTypes.empty()) {
99         return nullptr;
100     }
101 
102     ArenaVector<Type *> newConstituentTypes(allocator_->Adapter());
103 
104     for (auto *it : constituentTypes) {
105         if (it->IsUnionType()) {
106             for (auto *type : it->AsUnionType()->ConstituentTypes()) {
107                 newConstituentTypes.push_back(type);
108             }
109 
110             continue;
111         }
112 
113         newConstituentTypes.push_back(it);
114     }
115 
116     UnionType::RemoveDuplicatedTypes(relation_, newConstituentTypes);
117 
118     if (newConstituentTypes.size() == 1) {
119         return newConstituentTypes[0];
120     }
121 
122     auto *newUnionType = allocator_->New<UnionType>(std::move(newConstituentTypes));
123     CHECK_NOT_NULL(newUnionType);
124 
125     return UnionType::HandleUnionType(newUnionType, globalTypes_);
126 }
127 
CreateObjectTypeWithCallSignature(Signature * callSignature)128 Type *Checker::CreateObjectTypeWithCallSignature(Signature *callSignature)
129 {
130     auto *objType = allocator_->New<ObjectLiteralType>(allocator_->New<ObjectDescriptor>(allocator_));
131     CHECK_NOT_NULL(objType);
132     objType->AddCallSignature(callSignature);
133     return objType;
134 }
135 
CreateObjectTypeWithConstructSignature(Signature * constructSignature)136 Type *Checker::CreateObjectTypeWithConstructSignature(Signature *constructSignature)
137 {
138     auto *objType = allocator_->New<ObjectLiteralType>(allocator_->New<ObjectDescriptor>(allocator_));
139     CHECK_NOT_NULL(objType);
140     objType->AddConstructSignature(constructSignature);
141     return objType;
142 }
143 
CreateFunctionTypeWithSignature(Signature * callSignature)144 Type *Checker::CreateFunctionTypeWithSignature(Signature *callSignature)
145 {
146     auto *funcObjType = allocator_->New<FunctionType>(allocator_->New<ObjectDescriptor>(allocator_));
147     CHECK_NOT_NULL(funcObjType);
148     funcObjType->AddCallSignature(callSignature);
149     return funcObjType;
150 }
151 
CreateConstructorTypeWithSignature(Signature * constructSignature)152 Type *Checker::CreateConstructorTypeWithSignature(Signature *constructSignature)
153 {
154     auto *constructObjType = allocator_->New<ConstructorType>(allocator_->New<ObjectDescriptor>(allocator_));
155     CHECK_NOT_NULL(constructObjType);
156     constructObjType->AddConstructSignature(constructSignature);
157     return constructObjType;
158 }
159 
CreateTupleType(ObjectDescriptor * desc,ArenaVector<ElementFlags> && elementFlags,ElementFlags combinedFlags,uint32_t minLength,uint32_t fixedLength,bool readonly)160 Type *Checker::CreateTupleType(ObjectDescriptor *desc, ArenaVector<ElementFlags> &&elementFlags,
161                                ElementFlags combinedFlags, uint32_t minLength, uint32_t fixedLength, bool readonly)
162 {
163     CHECK_NOT_NULL(desc);
164     desc->stringIndexInfo = allocator_->New<IndexInfo>(GlobalAnyType(), "x", readonly);
165     return allocator_->New<TupleType>(desc, std::move(elementFlags), combinedFlags, minLength, fixedLength, readonly);
166 }
167 
CreateTupleType(ObjectDescriptor * desc,ArenaVector<ElementFlags> && elementFlags,ElementFlags combinedFlags,uint32_t minLength,uint32_t fixedLength,bool readonly,NamedTupleMemberPool && namedMembers)168 Type *Checker::CreateTupleType(ObjectDescriptor *desc, ArenaVector<ElementFlags> &&elementFlags,
169                                ElementFlags combinedFlags, uint32_t minLength, uint32_t fixedLength, bool readonly,
170                                NamedTupleMemberPool &&namedMembers)
171 {
172     CHECK_NOT_NULL(desc);
173     desc->stringIndexInfo = allocator_->New<IndexInfo>(GlobalAnyType(), "x", readonly);
174 
175     if (!namedMembers.empty()) {
176         return allocator_->New<TupleType>(desc, std::move(elementFlags), combinedFlags, minLength, fixedLength,
177                                           readonly, std::move(namedMembers));
178     }
179 
180     return allocator_->New<TupleType>(desc, std::move(elementFlags), combinedFlags, minLength, fixedLength, readonly);
181 }
182 }  // namespace panda::es2panda::checker
183