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 "tsTypeLiteral.h"
17
18 #include <ir/astDump.h>
19 #include <ir/ts/tsPropertySignature.h>
20 #include <ir/expressions/identifier.h>
21 #include <binder/variable.h>
22 #include <binder/declaration.h>
23 #include <typescript/checker.h>
24 #include <typescript/types/signature.h>
25
26 namespace panda::es2panda::ir {
27
Iterate(const NodeTraverser & cb) const28 void TSTypeLiteral::Iterate(const NodeTraverser &cb) const
29 {
30 for (auto *it : members_) {
31 cb(it);
32 }
33 }
34
Dump(ir::AstDumper * dumper) const35 void TSTypeLiteral::Dump(ir::AstDumper *dumper) const
36 {
37 dumper->Add({{"type", "TSTypeLiteral"}, {"members", members_}});
38 }
39
Compile(compiler::PandaGen * pg) const40 void TSTypeLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
41
Check(checker::Checker * checker) const42 checker::Type *TSTypeLiteral::Check(checker::Checker *checker) const
43 {
44 for (auto *it : members_) {
45 it->Check(checker);
46 }
47
48 checker::Type *type = GetType(checker);
49 checker->CheckIndexConstraints(type);
50
51 return nullptr;
52 }
53
GetType(checker::Checker * checker) const54 checker::Type *TSTypeLiteral::GetType(checker::Checker *checker) const
55 {
56 auto found = checker->NodeCache().find(this);
57
58 if (found != checker->NodeCache().end()) {
59 return found->second;
60 }
61
62 checker::ObjectDescriptor *desc = checker->Allocator()->New<checker::ObjectDescriptor>(checker->Allocator());
63 checker::Type *type = checker->Allocator()->New<checker::ObjectLiteralType>(desc);
64 type->SetVariable(Variable());
65
66 checker->NodeCache().insert({this, type});
67
68 return type;
69 }
70
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)71 void TSTypeLiteral::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
72 {
73 for (auto iter = members_.begin(); iter != members_.end(); iter++) {
74 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
75 }
76 }
77
78 } // namespace panda::es2panda::ir
79