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 "objectLiteralType.h"
17
18 #include <typescript/types/indexInfo.h>
19 #include <typescript/types/signature.h>
20
21 namespace panda::es2panda::checker {
22
23 class Checker;
24
ToString(std::stringstream & ss) const25 void ObjectLiteralType::ToString(std::stringstream &ss) const
26 {
27 ss << "{ ";
28
29 if (desc_->stringIndexInfo) {
30 desc_->stringIndexInfo->ToString(ss, false);
31 ss << "; ";
32 }
33
34 if (desc_->numberIndexInfo) {
35 desc_->numberIndexInfo->ToString(ss, true);
36 ss << "; ";
37 }
38
39 for (auto *it : desc_->callSignatures) {
40 it->ToString(ss, nullptr, true);
41 ss << "; ";
42 }
43
44 for (auto *it : desc_->constructSignatures) {
45 ss << "new ";
46 it->ToString(ss, nullptr, true);
47 ss << "; ";
48 }
49
50 for (auto *it : desc_->properties) {
51 if (it->HasFlag(binder::VariableFlags::READONLY)) {
52 ss << "readonly ";
53 }
54 ss << it->Name();
55
56 if (it->HasFlag(binder::VariableFlags::OPTIONAL)) {
57 ss << "?";
58 }
59 if (it->HasFlag(binder::VariableFlags::PROPERTY)) {
60 ss << ": ";
61 }
62
63 if (it->TsType()) {
64 it->TsType()->ToString(ss);
65 } else {
66 ss << "any";
67 }
68
69 ss << "; ";
70 }
71
72 ss << "}";
73 }
74
GetTypeFacts() const75 TypeFacts ObjectLiteralType::GetTypeFacts() const
76 {
77 if (desc_->properties.empty() && desc_->callSignatures.empty() && desc_->constructSignatures.empty() &&
78 !desc_->numberIndexInfo && !desc_->stringIndexInfo) {
79 return TypeFacts::EMPTY_OBJECT_FACTS;
80 }
81
82 return TypeFacts::OBJECT_FACTS;
83 }
84
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)85 Type *ObjectLiteralType::Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes)
86 {
87 ObjectDescriptor *copiedDesc = allocator->New<ObjectDescriptor>(allocator);
88 desc_->Copy(allocator, copiedDesc, relation, globalTypes);
89 return allocator->New<ObjectLiteralType>(copiedDesc);
90 }
91
92 } // namespace panda::es2panda::checker
93