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