1 /**
2 * Copyright (c) 2021-2022 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 "functionType.h"
17
18 #include <typescript/types/signature.h>
19
20 namespace panda::es2panda::checker {
21
ToString(std::stringstream & ss) const22 void FunctionType::ToString(std::stringstream &ss) const
23 {
24 static std::unordered_set<const FunctionType *> stack;
25
26 if (stack.find(this) != stack.end()) {
27 stack.erase(this);
28 ss << "...";
29 return;
30 }
31
32 stack.insert(this);
33
34 if (desc_->callSignatures.size() > 1) {
35 ss << "{ ";
36 }
37
38 for (auto it = desc_->callSignatures.begin(); it != desc_->callSignatures.end(); it++) {
39 (*it)->ToString(ss, variable_, desc_->callSignatures.size() > 1);
40 if (std::next(it) != desc_->callSignatures.end()) {
41 ss << ", ";
42 }
43 }
44
45 if (desc_->callSignatures.size() > 1) {
46 ss << " }";
47 }
48 }
49
GetTypeFacts() const50 TypeFacts FunctionType::GetTypeFacts() const
51 {
52 return TypeFacts::FUNCTION_FACTS;
53 }
54
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)55 Type *FunctionType::Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes)
56 {
57 ObjectDescriptor *copiedDesc = allocator->New<ObjectDescriptor>(allocator);
58 desc_->Copy(allocator, copiedDesc, relation, globalTypes);
59 return allocator->New<FunctionType>(copiedDesc);
60 }
61
62 } // namespace panda::es2panda::checker
63